ToDoInfoWindow.xaml.cs 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. using GeekDesk.Constant;
  2. using GeekDesk.Interface;
  3. using GeekDesk.Util;
  4. using GeekDesk.ViewModel;
  5. using HandyControl.Controls;
  6. using Quartz;
  7. using System;
  8. using System.Windows;
  9. using System.Windows.Controls;
  10. using System.Windows.Input;
  11. namespace GeekDesk.Control.Windows
  12. {
  13. /// <summary>
  14. /// BacklogInfoWindow.xaml 的交互逻辑
  15. /// </summary>
  16. public partial class ToDoInfoWindow : IWindowCommon
  17. {
  18. private static int windowType = -1;
  19. private static readonly int NEW_TODO = 1;
  20. private static readonly int DETAIL_TODO = 2;
  21. private AppData appData = MainWindow.appData;
  22. //private ToDoInfo info;
  23. private ToDoInfoWindow()
  24. {
  25. InitializeComponent();
  26. this.Topmost = true;
  27. DateTime time = DateTime.Now.AddMinutes(10);
  28. ExeTime.SelectedDateTime = time;
  29. SetTimePanel.Visibility = Visibility.Visible;
  30. this.DataContext = new ToDoInfo
  31. {
  32. ExeTime = time.ToString("yyyy-MM-dd HH:mm:ss")
  33. };
  34. }
  35. private ToDoInfoWindow(ToDoInfo info)
  36. {
  37. InitializeComponent();
  38. this.Topmost = true;
  39. this.DataContext = info;
  40. SetTimePanel.Visibility = Visibility.Visible;
  41. //Title.Text = info.Title;
  42. //Msg.Text = info.Msg;
  43. //ExeTime.Text = info.ExeTime;
  44. //DoneTime.Text = info.DoneTime;
  45. //this.info = info;
  46. }
  47. /// <summary>
  48. /// 点击关闭按钮
  49. /// </summary>
  50. /// <param name="sender"></param>
  51. /// <param name="e"></param>
  52. private void Close_Button_Click(object sender, RoutedEventArgs e)
  53. {
  54. this.Close();
  55. }
  56. /// <summary>
  57. /// 移动窗口
  58. /// </summary>
  59. /// <param name="sender"></param>
  60. /// <param name="e"></param>
  61. private void DragMove(object sender, System.Windows.Input.MouseButtonEventArgs e)
  62. {
  63. if (e.LeftButton == MouseButtonState.Pressed)
  64. {
  65. DragMove();
  66. }
  67. }
  68. /// <summary>
  69. /// 保存待办
  70. /// </summary>
  71. /// <param name="sender"></param>
  72. /// <param name="e"></param>
  73. private void Save_Button_Click(object sender, RoutedEventArgs e)
  74. {
  75. DateTime dt;
  76. string execTime;
  77. TodoTaskExecType execType;
  78. if (Title.Text.Trim() == "")
  79. {
  80. Growl.Warning("任务标题不能为空!");
  81. return;
  82. }
  83. else
  84. {
  85. if (SetTimePanel.Visibility == Visibility.Visible)
  86. {
  87. execType = TodoTaskExecType.SET_TIME;
  88. if (ExeTime.Text.Trim() == "")
  89. {
  90. Growl.Warning("执行时间不能为空!");
  91. return;
  92. }
  93. try
  94. {
  95. dt = Convert.ToDateTime(ExeTime.Text);
  96. }
  97. catch (Exception)
  98. {
  99. Growl.Warning("请输入正确的时间!");
  100. return;
  101. }
  102. execTime = ExeTime.Text;
  103. }
  104. else
  105. {
  106. execType = TodoTaskExecType.CRON;
  107. if (Cron.Text.Trim() == "")
  108. {
  109. Growl.Warning("Cron表达式不能为空!");
  110. return;
  111. }
  112. try
  113. {
  114. bool isValid = CronExpression.IsValidExpression(Cron.Text);
  115. if (!isValid) throw new Exception();
  116. }
  117. catch (Exception)
  118. {
  119. Growl.Warning("请输入正确的Cron表达式!");
  120. return;
  121. }
  122. CronExpression exp = new CronExpression(Cron.Text);
  123. DateTime dd = DateTime.Now;
  124. DateTimeOffset ddo = DateTime.SpecifyKind(dd, DateTimeKind.Local);
  125. ddo = (DateTimeOffset)exp.GetNextValidTimeAfter(ddo);
  126. execTime = ddo.LocalDateTime.ToString("yyyy-MM-dd HH:mm:ss");
  127. }
  128. }
  129. dt = Convert.ToDateTime(execTime);
  130. ToDoInfo info = new ToDoInfo
  131. {
  132. Title = Title.Text,
  133. Msg = Msg.Text,
  134. ExeTime = execTime,
  135. ExecType = execType,
  136. Cron = Cron.Text
  137. };
  138. if (windowType != NEW_TODO)
  139. {
  140. ToDoInfo tdi = this.DataContext as ToDoInfo;
  141. if (appData.HiToDoList.Contains(tdi))
  142. {
  143. appData.HiToDoList.Remove(tdi);
  144. }
  145. else if (appData.ToDoList.Contains(tdi))
  146. {
  147. appData.ToDoList.Remove(tdi);
  148. }
  149. }
  150. appData.ToDoList.Add(info);
  151. DateTime dtNow = DateTime.Now;
  152. TimeSpan ts = dt.Subtract(dtNow);
  153. int minutes = (int)Math.Ceiling(ts.TotalMinutes);
  154. if (minutes < 0)
  155. {
  156. minutes = 0;
  157. }
  158. if (minutes > 60)
  159. {
  160. int m = minutes % 60;
  161. int h = minutes / 60;
  162. Growl.SuccessGlobal("设置待办任务成功, 系统将在 " + h + " 小时零 " + m + " 分钟后提醒您!");
  163. }
  164. else
  165. {
  166. Growl.SuccessGlobal("设置待办任务成功, 系统将在 " + minutes + " 分钟后提醒您!");
  167. }
  168. CommonCode.SaveAppData(MainWindow.appData, Constants.DATA_FILE_PATH);
  169. this.Close();
  170. }
  171. private static System.Windows.Window window = null;
  172. public static void ShowNone()
  173. {
  174. if (window == null || !window.Activate())
  175. {
  176. window = new ToDoInfoWindow();
  177. window.Show();
  178. }
  179. windowType = NEW_TODO;
  180. window.Visibility = Visibility.Visible;
  181. }
  182. public static void ShowOrHide()
  183. {
  184. if (window == null || !window.Activate())
  185. {
  186. window = new ToDoInfoWindow();
  187. window.Show();
  188. }
  189. else
  190. {
  191. window.Close();
  192. }
  193. }
  194. public static System.Windows.Window GetThis()
  195. {
  196. if (window == null || !window.Activate())
  197. {
  198. window = new ToDoInfoWindow();
  199. window.Show();
  200. Keyboard.Focus(window);
  201. }
  202. window.Visibility = Visibility.Collapsed;
  203. windowType = NEW_TODO;
  204. return window;
  205. }
  206. private static System.Windows.Window window2 = null;
  207. public static void ShowDetail(ToDoInfo info)
  208. {
  209. if (window2 == null || !window2.Activate())
  210. {
  211. window2 = new ToDoInfoWindow(info);
  212. }
  213. windowType = DETAIL_TODO;
  214. window2.Show();
  215. Keyboard.Focus(window2);
  216. }
  217. private void ExecType_Checked(object sender, RoutedEventArgs e)
  218. {
  219. TodoTaskExecType tag = (TodoTaskExecType)Convert.ToInt32((sender as RadioButton).Tag.ToString());
  220. switch (tag)
  221. {
  222. case TodoTaskExecType.SET_TIME:
  223. SetTimePanel.Visibility = Visibility.Visible;
  224. CronPanel.Visibility = Visibility.Collapsed;
  225. break;
  226. default:
  227. CronPanel.Visibility = Visibility.Visible;
  228. SetTimePanel.Visibility = Visibility.Collapsed;
  229. break;
  230. }
  231. }
  232. public void OnKeyDown(object sender, KeyEventArgs e)
  233. {
  234. if (e.Key == Key.Escape)
  235. {
  236. this.DataContext = null;
  237. this.Close();
  238. }
  239. }
  240. }
  241. }