ToDoInfoWindow.xaml.cs 7.6 KB

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