ToDoInfoWindow.xaml.cs 8.2 KB

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