PageRenderTime 59ms CodeModel.GetById 33ms RepoModel.GetById 0ms app.codeStats 0ms

/V1_NET4/MouseExtender/Controls/UserControls/SystemButtonsBarUC.xaml.cs

#
C# | 259 lines | 215 code | 32 blank | 12 comment | 18 complexity | 0561718f9096dfc147a4167846ca792e MD5 | raw file
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Diagnostics;
  4. using System.Linq;
  5. using System.Windows;
  6. using System.Windows.Controls;
  7. using System.Windows.Controls.Primitives;
  8. using System.Windows.Input;
  9. using System.Windows.Media.Animation;
  10. using System.Windows.Threading;
  11. using MouseExtender.Logic;
  12. using MouseExtender.Logic.Common;
  13. using MouseExtender.Logic.Entities;
  14. using MouseExtender.Logic.Enums;
  15. using MouseExtender.Logic.Managers;
  16. using MouseExtender.Resources;
  17. namespace MouseExtender.Controls.UserControls
  18. {
  19. /// <summary>
  20. /// Interaction logic for SystemButtonsBarUC.xaml
  21. /// </summary>
  22. public partial class SystemButtonsBarUC : UserControl
  23. {
  24. DispatcherTimer _takeAnActionTimer;
  25. TimeSpan? _timeSpanToAct = null;
  26. private List<int> _timerScalePeriods = new List<int>() { 1, 60, 600, 1440 };
  27. private TimerScalePeriod _currentScale = TimerScalePeriod.Hour;
  28. public SystemButtonsBarUC()
  29. {
  30. InitializeComponent();
  31. }
  32. private void UserControl_Loaded(object sender, RoutedEventArgs e)
  33. {
  34. //initialize control here
  35. _takeAnActionTimer = new DispatcherTimer();
  36. _takeAnActionTimer.Interval = TimeSpan.FromSeconds(1);
  37. _takeAnActionTimer.Tick += new EventHandler(_hibernateTimer_Tick);
  38. }
  39. private void btnControlPanel_MouseDoubleClick(object sender, MouseButtonEventArgs e)
  40. {
  41. MainWindow mainWindow = (MainWindow)Application.Current.MainWindow;
  42. mainWindow.HideWindow();
  43. ProcessStartInfo startInfo = new ProcessStartInfo("control");
  44. Process.Start(startInfo);
  45. }
  46. private void btnReboot_MouseDoubleClick(object sender, MouseButtonEventArgs e)
  47. {
  48. WindowsController.ExitWindows(RestartOptions.Reboot, true);
  49. }
  50. private void btnShutdown_MouseDoubleClick(object sender, MouseButtonEventArgs e)
  51. {
  52. PerformShutdownAction();
  53. }
  54. private void btnShowMenu_Click(object sender, RoutedEventArgs e)
  55. {
  56. e.Handled = true;
  57. cmShutdown.Placement = PlacementMode.Bottom;
  58. cmShutdown.VerticalOffset = 1;
  59. cmShutdown.PlacementTarget = btnShutdown;
  60. cmShutdown.IsOpen = true;
  61. if (_takeAnActionTimer.IsEnabled)
  62. {
  63. Button btnHibernateEdit = (Button)shutdownMenuItem.Template.FindName("btnHibernateEdit", shutdownMenuItem);
  64. btnHibernateEdit.Content = UIResources.Buttons_Stop;
  65. }
  66. }
  67. private void btnHiberEdit_Click(object sender, RoutedEventArgs e)
  68. {
  69. Button btnHibernateEdit = ((Button)sender);
  70. if ((string)btnHibernateEdit.Content == UIResources.Buttons_Start)
  71. {
  72. ShutdownAction();
  73. }
  74. else if ((string)btnHibernateEdit.Content == UIResources.Buttons_Stop)
  75. {
  76. StopTakeAnActionProgress();
  77. }
  78. }
  79. private void ShutdownAction()
  80. {
  81. if (!_takeAnActionTimer.IsEnabled)
  82. StartTakeAnActionProgress();
  83. else
  84. throw new InvalidOperationException("Impossible start already started timer.");
  85. }
  86. private void StartTakeAnActionProgress()
  87. {
  88. //save initial interval to settings
  89. Slider slInterval = (Slider)shutdownMenuItem.Template.FindName("slInterval", shutdownMenuItem);
  90. _timeSpanToAct = TimeSpan.FromMinutes((double)slInterval.Value);
  91. SettingsManager.CurrentSettings.ActionSpan = _timeSpanToAct;
  92. //launch timer and animation
  93. TextBlock lblInterval = (TextBlock)shutdownMenuItem.Template.FindName("lblInterval", shutdownMenuItem);
  94. Storyboard animation = (Storyboard)shutdownMenuItem.Resources["sbActionInProgress"];
  95. animation.SetValue(Storyboard.TargetProperty, lblInterval);
  96. animation.Begin();
  97. _takeAnActionTimer.Start();
  98. //update GUI
  99. Button btnHibernateEdit = (Button)shutdownMenuItem.Template.FindName("btnHibernateEdit", shutdownMenuItem);
  100. btnHibernateEdit.Content = UIResources.Buttons_Stop;
  101. cmShutdown.IsOpen = false;
  102. Grid grdTakeAnActionRoot = (Grid)shutdownMenuItem.Template.FindName("grdTakeAnActionRoot", shutdownMenuItem);
  103. grdTakeAnActionRoot.IsEnabled = false;
  104. grdTakeAnActionRoot.IsHitTestVisible = false;
  105. }
  106. void _hibernateTimer_Tick(object sender, EventArgs e)
  107. {
  108. if (!_timeSpanToAct.HasValue)
  109. throw new ApplicationException("Timer cannot be started before action span is setup.");
  110. _timeSpanToAct = _timeSpanToAct.Value - _takeAnActionTimer.Interval;
  111. //showtime
  112. if (_timeSpanToAct.Value.TotalMilliseconds <= 0)
  113. {
  114. StopTakeAnActionProgress();
  115. SaveSystemPanelSettings();
  116. PerformShutdownAction();
  117. }
  118. else
  119. {
  120. //todo: make via binding
  121. UpdateActionTimeSpan();
  122. }
  123. }
  124. private void StopTakeAnActionProgress()
  125. {
  126. _takeAnActionTimer.Stop();
  127. Storyboard animation = (Storyboard)shutdownMenuItem.Resources["sbActionInProgress"];
  128. animation.Stop();
  129. Button btnHibernateEdit = (Button)shutdownMenuItem.Template.FindName("btnHibernateEdit", shutdownMenuItem);
  130. btnHibernateEdit.Content = UIResources.Buttons_Start;
  131. Grid grdTakeAnActionRoot = (Grid)shutdownMenuItem.Template.FindName("grdTakeAnActionRoot", shutdownMenuItem);
  132. grdTakeAnActionRoot.IsEnabled = true;
  133. grdTakeAnActionRoot.IsHitTestVisible = true;
  134. }
  135. private void UpdateActionTimeSpan()
  136. {
  137. if (_takeAnActionTimer.IsEnabled && cmShutdown.IsOpen)
  138. {
  139. Slider slInterval = (Slider)shutdownMenuItem.Template.FindName("slInterval", shutdownMenuItem);
  140. slInterval.Value = _timeSpanToAct.Value.TotalSeconds / 60;
  141. }
  142. }
  143. private void PerformShutdownAction()
  144. {
  145. #if DEBUG
  146. MessageBox.Show(string.Format("[{0}] with Force option [{1}] is now could be performed, but this is just a stub.", SettingsManager.CurrentSettings.ShutdownAction.Value, SettingsManager.CurrentSettings.ForceAction.Value ? "enabled" : "disabled"));
  147. #else
  148. WindowsController.ExitWindows(SettingsManager.CurrentSettings.ShutdownAction.Value, SettingsManager.CurrentSettings.ForceAction.Value);
  149. #endif
  150. }
  151. private void btnShutdown_PreviewMouseRightButtonUp(object sender, MouseButtonEventArgs e)
  152. {
  153. //prevent showing context menu
  154. e.Handled = true;
  155. }
  156. private void LoadSystemPanelSettings()
  157. {
  158. ComboBox cbShutdownAction = (ComboBox)shutdownMenuItem.Template.FindName("cbShutdownAction", shutdownMenuItem);
  159. CheckBox cbForceShutdown = (CheckBox)shutdownMenuItem.Template.FindName("cbForceShutdown", shutdownMenuItem);
  160. Slider slInterval = (Slider)shutdownMenuItem.Template.FindName("slInterval", shutdownMenuItem);
  161. UserSettings settings = SettingsManager.CurrentSettings;
  162. //select shutdown type
  163. string currentShutdownOptions = Constants.RestartOptionsDictionary[(int)settings.ShutdownAction];
  164. foreach (ComboBoxItem item in cbShutdownAction.Items)
  165. {
  166. if (((string)item.Content) == currentShutdownOptions)
  167. {
  168. item.IsSelected = true;
  169. break;
  170. }
  171. }
  172. cbForceShutdown.IsChecked = settings.ForceAction;
  173. _currentScale = settings.ActionSpanScale.Value;
  174. UpdateSliderCurrentScale();
  175. slInterval.Value = settings.ActionSpan.Value.TotalMinutes;
  176. }
  177. //perform two way biding
  178. private void SaveSystemPanelSettings()
  179. {
  180. UserSettings settings = SettingsManager.CurrentSettings;
  181. ComboBox cbShutdownAction = (ComboBox)shutdownMenuItem.Template.FindName("cbShutdownAction", shutdownMenuItem);
  182. CheckBox cbForceShutdown = (CheckBox)shutdownMenuItem.Template.FindName("cbForceShutdown", shutdownMenuItem);
  183. KeyValuePair<int, string> selectedValue = Constants.RestartOptionsDictionary.FirstOrDefault(i => i.Value == (string)((ComboBoxItem)cbShutdownAction.SelectedItem).Content);
  184. settings.ShutdownAction = (RestartOptions)selectedValue.Key;
  185. settings.ForceAction = cbForceShutdown.IsChecked;
  186. settings.ActionSpanScale = _currentScale;
  187. }
  188. private void cmShutdown_Closed(object sender, RoutedEventArgs e)
  189. {
  190. SaveSystemPanelSettings();
  191. }
  192. private void cmShutdown_Loaded(object sender, RoutedEventArgs e)
  193. {
  194. LoadSystemPanelSettings();
  195. UpdateActionTimeSpan();
  196. }
  197. private void ScaleSliderUp_Click(object sender, RoutedEventArgs e)
  198. {
  199. if ((int)_currentScale == _timerScalePeriods.Count - 1)
  200. {
  201. _currentScale = (TimerScalePeriod)0;
  202. }
  203. else
  204. {
  205. _currentScale = (TimerScalePeriod)((int)_currentScale + 1);
  206. }
  207. UpdateSliderCurrentScale();
  208. }
  209. private void ScaleSliderDown_Click(object sender, RoutedEventArgs e)
  210. {
  211. if ((int)_currentScale == 0)
  212. {
  213. _currentScale = (TimerScalePeriod)_timerScalePeriods.Count - 1;
  214. }
  215. else
  216. {
  217. _currentScale = (TimerScalePeriod)((int)_currentScale - 1);
  218. }
  219. UpdateSliderCurrentScale();
  220. }
  221. private void UpdateSliderCurrentScale()
  222. {
  223. Slider slInterval = (Slider)shutdownMenuItem.Template.FindName("slInterval", shutdownMenuItem);
  224. slInterval.Maximum = _timerScalePeriods[(int)_currentScale];
  225. }
  226. }
  227. }