ToDoInfoWindow.xaml.cs 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  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. appData.HiToDoList.Remove(this.DataContext as ToDoInfo);
  145. }
  146. appData.ToDoList.Add(info);
  147. DateTime dtNow = DateTime.Now;
  148. TimeSpan ts = dt.Subtract(dtNow);
  149. int minutes = (int)Math.Ceiling(ts.TotalMinutes);
  150. if (minutes < 0)
  151. {
  152. minutes = 0;
  153. }
  154. if (minutes > 60)
  155. {
  156. int m = minutes % 60;
  157. int h = minutes / 60;
  158. Growl.SuccessGlobal("设置待办任务成功, 系统将在 " + h + " 小时零 " + m + " 分钟后提醒您!");
  159. } else
  160. {
  161. Growl.SuccessGlobal("设置待办任务成功, 系统将在 " + minutes + " 分钟后提醒您!");
  162. }
  163. CommonCode.SaveAppData(MainWindow.appData);
  164. this.Close();
  165. }
  166. private static System.Windows.Window window = null;
  167. public static void ShowNone()
  168. {
  169. if (window == null || !window.Activate())
  170. {
  171. window = new ToDoInfoWindow();
  172. window.Show();
  173. }
  174. windowType = NEW_TODO;
  175. window.Visibility = Visibility.Visible;
  176. }
  177. public static void ShowOrHide()
  178. {
  179. if (window == null || !window.Activate())
  180. {
  181. window = new ToDoInfoWindow();
  182. window.Show();
  183. } else
  184. {
  185. window.Close();
  186. }
  187. }
  188. public static System.Windows.Window GetThis()
  189. {
  190. if (window == null || !window.Activate())
  191. {
  192. window = new ToDoInfoWindow();
  193. window.Show();
  194. Keyboard.Focus(window);
  195. }
  196. window.Visibility = Visibility.Collapsed;
  197. windowType = NEW_TODO;
  198. return window;
  199. }
  200. private static System.Windows.Window window2 = null;
  201. public static void ShowDetail(ToDoInfo info)
  202. {
  203. if (window2 == null || !window2.Activate())
  204. {
  205. window2 = new ToDoInfoWindow(info);
  206. }
  207. windowType = DETAIL_TODO;
  208. window2.Show();
  209. Keyboard.Focus(window2);
  210. }
  211. private void ExecType_Checked(object sender, RoutedEventArgs e)
  212. {
  213. TodoTaskExecType tag = (TodoTaskExecType)Convert.ToInt32((sender as RadioButton).Tag.ToString());
  214. switch (tag)
  215. {
  216. case TodoTaskExecType.SET_TIME:
  217. SetTimePanel.Visibility = Visibility.Visible;
  218. CronPanel.Visibility = Visibility.Collapsed;
  219. break;
  220. default:
  221. CronPanel.Visibility = Visibility.Visible;
  222. SetTimePanel.Visibility = Visibility.Collapsed;
  223. break;
  224. }
  225. }
  226. public void OnKeyDown(object sender, KeyEventArgs e)
  227. {
  228. if (e.Key == Key.Escape)
  229. {
  230. this.Close();
  231. }
  232. }
  233. }
  234. }