MainWindow.xaml.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353
  1. using DraggAnimatedPanelExample;
  2. using GeekDesk.Util;
  3. using GeekDesk.ViewModel;
  4. using System;
  5. using System.Collections;
  6. using System.Collections.Generic;
  7. using System.Collections.ObjectModel;
  8. using System.ComponentModel;
  9. using System.IO;
  10. using System.Windows;
  11. using System.Windows.Controls;
  12. using System.Windows.Input;
  13. using System.Windows.Media.Imaging;
  14. namespace GeekDesk
  15. {
  16. /// <summary>
  17. /// MainWindow.xaml 的交互逻辑
  18. /// </summary>
  19. ///
  20. public partial class MainWindow : Window
  21. {
  22. private AppData appData = CommonCode.GetAppDataByFile();
  23. private int menuSelectIndexTemp = -1;
  24. public MainWindow()
  25. {
  26. InitializeComponent();
  27. loadData();
  28. //this.DataContext = mainModel;
  29. //menu.Items = mainModel;
  30. //System.Diagnostics.Process.Start(@"D:\SoftWare\WeGame\wegame.exe");
  31. this.Loaded += Window_Loaded;
  32. this.SizeChanged += MainWindow_Resize;
  33. this.Topmost = true;
  34. }
  35. private void loadData()
  36. {
  37. this.DataContext = appData;
  38. if (appData.MenuList.Count == 0)
  39. {
  40. appData.MenuList.Add(new MenuInfo() { MenuName = "NewGouop", MenuId = System.Guid.NewGuid().ToString(), MenuEdit = (int)Visibility.Collapsed});
  41. }
  42. //窗体大小
  43. this.Width = appData.AppConfig.WindowWidth;
  44. this.Height = appData.AppConfig.WindowHeight;
  45. //选中 菜单
  46. menus.SelectedIndex = appData.AppConfig.SelectedMenuIndex;
  47. //图标数据
  48. icons.ItemsSource = appData.MenuList[appData.AppConfig.SelectedMenuIndex].IconList;
  49. }
  50. #region 图标拖动
  51. DelegateCommand<int[]> _swap;
  52. public DelegateCommand<int[]> SwapCommand
  53. {
  54. get
  55. {
  56. if (_swap == null)
  57. _swap = new DelegateCommand<int[]>(
  58. (indexes) =>
  59. {
  60. int fromS = indexes[0];
  61. int to = indexes[1];
  62. var elementSource = icons.Items[to];
  63. var dragged = icons.Items[fromS];
  64. if (fromS > to)
  65. {
  66. icons.Items.Remove(dragged);
  67. icons.Items.Insert(to, dragged);
  68. }
  69. else
  70. {
  71. icons.Items.Remove(dragged);
  72. icons.Items.Insert(to, dragged);
  73. }
  74. }
  75. );
  76. return _swap;
  77. }
  78. }
  79. DelegateCommand<int[]> _swap2;
  80. public DelegateCommand<int[]> SwapCommand2
  81. {
  82. get
  83. {
  84. if (_swap2 == null)
  85. _swap2 = new DelegateCommand<int[]>(
  86. (indexes) =>
  87. {
  88. int fromS = indexes[0];
  89. int to = indexes[1];
  90. ObservableCollection<MenuInfo> menuList = appData.MenuList;
  91. var elementSource = menuList[to];
  92. var dragged = menuList[fromS];
  93. if (fromS > to)
  94. {
  95. menuList.Remove(dragged);
  96. menuList.Insert(to, dragged);
  97. }
  98. else
  99. {
  100. menuList.Remove(dragged);
  101. menuList.Insert(to, dragged);
  102. }
  103. appData.MenuList = menuList;
  104. //menus.Items.Refresh();
  105. }
  106. );
  107. return _swap2;
  108. }
  109. }
  110. #endregion 图标拖动
  111. private void Wrap_Drop(object sender, DragEventArgs e)
  112. {
  113. Array dropObject = (System.Array)e.Data.GetData(DataFormats.FileDrop);
  114. if (dropObject == null) return;
  115. foreach (object obj in dropObject)
  116. {
  117. string path = (string)obj;
  118. if (File.Exists(path))
  119. {
  120. // 文件
  121. BitmapImage bi = FileIcon.GetBitmapImage(path);
  122. IconInfo iconInfo = new IconInfo();
  123. iconInfo.Path = path;
  124. iconInfo.BitmapImage = bi;
  125. iconInfo.Name = Path.GetFileNameWithoutExtension(path);
  126. appData.MenuList[menus.SelectedIndex].IconList.Add(iconInfo);
  127. CommonCode.SaveAppData(appData);
  128. }
  129. else if (Directory.Exists(path))
  130. {
  131. //文件夹
  132. }
  133. }
  134. icons.Items.Refresh();
  135. }
  136. //菜单点击事件
  137. private void MenuClick(object sender, SelectionChangedEventArgs e)
  138. {
  139. //设置对应菜单的图标列表
  140. icons.ItemsSource = appData.MenuList[menus.SelectedIndex].IconList;
  141. appData.AppConfig.SelectedMenuIndex = menus.SelectedIndex;
  142. CommonCode.SaveAppData(appData);
  143. }
  144. /// <summary>
  145. /// 图标点击事件
  146. /// </summary>
  147. /// <param name="sender"></param>
  148. /// <param name="e"></param>
  149. private void IconClick(object sender, MouseButtonEventArgs e)
  150. {
  151. IconInfo icon = (IconInfo)((StackPanel)sender).Tag;
  152. System.Diagnostics.Process.Start(icon.Path);
  153. icon.Count++;
  154. CommonCode.SaveAppData(appData);
  155. }
  156. /// <summary>
  157. /// data选中事件 设置不可选中
  158. /// </summary>
  159. /// <param name="sender"></param>
  160. /// <param name="e"></param>
  161. private void IconSelectionChanged(object sender, SelectionChangedEventArgs e)
  162. {
  163. if (icons.SelectedIndex != -1) icons.SelectedIndex = -1;
  164. }
  165. void Window_Loaded(object sender, RoutedEventArgs e)
  166. {
  167. //加载完毕注册热键
  168. Hotkey.Regist(this, HotkeyModifiers.MOD_CONTROL, Key.Y, ()=>
  169. {
  170. if (this.Visibility == Visibility.Collapsed)
  171. {
  172. this.Visibility = Visibility.Visible;
  173. } else
  174. {
  175. this.Visibility = Visibility.Collapsed;
  176. }
  177. });
  178. }
  179. void MainWindow_Resize(object sender, System.EventArgs e)
  180. {
  181. if (this.DataContext != null)
  182. {
  183. AppData appData = this.DataContext as AppData;
  184. appData.AppConfig.WindowWidth = this.Width;
  185. appData.AppConfig.WindowHeight = this.Height;
  186. CommonCode.SaveAppData(appData);
  187. }
  188. }
  189. /// <summary>
  190. /// 删除菜单
  191. /// </summary>
  192. /// <param name="sender"></param>
  193. /// <param name="e"></param>
  194. private void DeleteMenu(object sender, RoutedEventArgs e)
  195. {
  196. MenuInfo menuInfo = ((MenuItem)sender).Tag as MenuInfo;
  197. appData.MenuList.Remove(menuInfo);
  198. CommonCode.SaveAppData(appData);
  199. }
  200. private void DragMove(object sender, MouseEventArgs e)
  201. {
  202. if (e.LeftButton == MouseButtonState.Pressed)
  203. {
  204. this.DragMove();
  205. }
  206. }
  207. /// <summary>
  208. /// 重命名菜单 将textbox 设置为可见
  209. /// </summary>
  210. /// <param name="sender"></param>
  211. /// <param name="e"></param>
  212. private void RenameMenu(object sender, RoutedEventArgs e)
  213. {
  214. MenuInfo menuInfo = ((MenuItem)sender).Tag as MenuInfo;
  215. menuInfo.MenuEdit = (int)Visibility.Visible;
  216. }
  217. /// <summary>
  218. /// 编辑菜单失焦或者敲下Enter键时保存修改后的菜单
  219. /// </summary>
  220. /// <param name="sender"></param>
  221. /// <param name="e"></param>
  222. private void LostFocusOrEnterDown(object sender, EventArgs e)
  223. {
  224. TextBox menuBox = null;
  225. if (e.GetType() == typeof(KeyEventArgs))
  226. {
  227. KeyEventArgs eKey = e as KeyEventArgs;
  228. if (eKey.Key == Key.Enter)
  229. {
  230. menuBox = ((TextBox)sender);
  231. }
  232. } else if(e.GetType() == typeof(RoutedEventArgs))
  233. {
  234. menuBox = ((TextBox)sender);
  235. }
  236. if (menuBox != null)
  237. {
  238. MenuInfo menuInfo = menuBox.Tag as MenuInfo;
  239. string text = menuBox.Text;
  240. menuInfo.MenuName = text;
  241. menuInfo.MenuEdit = (int)Visibility.Collapsed;
  242. CommonCode.SaveAppData(appData);
  243. }
  244. }
  245. /// <summary>
  246. /// 当修改菜单元素可见时 设置全选并获得焦点
  247. /// </summary>
  248. /// <param name="sender"></param>
  249. /// <param name="e"></param>
  250. private void MenuEditWhenVisibilityChanged(object sender, DependencyPropertyChangedEventArgs e)
  251. {
  252. TextBox box = sender as TextBox;
  253. if (box.Visibility == Visibility.Visible)
  254. {
  255. Keyboard.Focus(box);
  256. box.SelectAll();
  257. }
  258. }
  259. /// <summary>
  260. /// 当修改菜单元素可见时 设置原菜单为不可见 并且不可选中
  261. /// 修改菜单元素不可见时 原菜单可见 并 选中
  262. /// </summary>
  263. /// <param name="sender"></param>
  264. /// <param name="e"></param>
  265. private void MenuWhenVisibilityChanged(object sender, DependencyPropertyChangedEventArgs e)
  266. {
  267. TextBlock tb = sender as TextBlock;
  268. if (tb.Visibility == Visibility.Collapsed)
  269. {
  270. if (menus.SelectedIndex != -1)
  271. {
  272. menuSelectIndexTemp = menus.SelectedIndex;
  273. menus.SelectedIndex = -1;
  274. } else
  275. {
  276. menus.SelectedIndex = menuSelectIndexTemp;
  277. }
  278. }
  279. }
  280. /// <summary>
  281. /// 新建菜单
  282. /// </summary>
  283. /// <param name="sender"></param>
  284. /// <param name="e"></param>
  285. private void CreateMenu(object sender, RoutedEventArgs e)
  286. {
  287. appData.MenuList.Add(new MenuInfo() { MenuEdit = (int)Visibility.Collapsed, MenuId = System.Guid.NewGuid().ToString(), MenuName = "NewGouop" });
  288. menus.SelectedIndex = appData.MenuList.Count - 1;
  289. //appData.MenuList[appData.MenuList.Count - 1].MenuEdit = (int)Visibility.Visible;
  290. CommonCode.SaveAppData(appData);
  291. }
  292. /// <summary>
  293. /// 关闭按钮单击事件
  294. /// </summary>
  295. /// <param name="sender"></param>
  296. /// <param name="e"></param>
  297. private void CloseButtonClick(object sender, RoutedEventArgs e)
  298. {
  299. this.Visibility = Visibility.Collapsed;
  300. }
  301. /// <summary>
  302. /// 图片图标单击事件
  303. /// </summary>
  304. /// <param name="sender"></param>
  305. /// <param name="e"></param>
  306. private void NotifyIcon_Click(object sender, RoutedEventArgs e)
  307. {
  308. if (this.Visibility == Visibility.Collapsed)
  309. {
  310. this.Visibility = Visibility.Visible;
  311. } else
  312. {
  313. this.Visibility = Visibility.Collapsed;
  314. }
  315. }
  316. }
  317. }