MainWindow.xaml.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  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. List<string> menuList = new List<string>();
  29. Dictionary<string, List<IconInfo>> iconMap = new Dictionary<string, List<IconInfo>>();
  30. //this.DataContext = mainModel;
  31. //menu.Items = mainModel;
  32. //System.Diagnostics.Process.Start(@"D:\SoftWare\WeGame\wegame.exe");
  33. this.Loaded += Window_Loaded;
  34. this.SizeChanged += MainWindow_Resize;
  35. }
  36. private void loadData()
  37. {
  38. this.DataContext = appData;
  39. //menus.ItemsSource = appData.MenuList;
  40. appData.MenuList.Add(new MenuInfo() { MenuName = "test1", MenuId = "1", MenuEdit = (int)Visibility.Collapsed });
  41. this.Width = appData.AppConfig.WindowWidth;
  42. this.Height = appData.AppConfig.WindowHeight;
  43. ObservableCollection<IconInfo> iconList;
  44. if (appData.IconMap.ContainsKey("1"))
  45. {
  46. iconList = appData.IconMap["1"];
  47. }
  48. else
  49. {
  50. iconList = new ObservableCollection<IconInfo>();
  51. appData.IconMap.Add("1", iconList);
  52. }
  53. icons.ItemsSource = iconList;
  54. }
  55. DelegateCommand<int[]> _swap;
  56. public DelegateCommand<int[]> SwapCommand
  57. {
  58. get
  59. {
  60. if (_swap == null)
  61. _swap = new DelegateCommand<int[]>(
  62. (indexes) =>
  63. {
  64. int fromS = indexes[0];
  65. int to = indexes[1];
  66. var elementSource = icons.Items[to];
  67. var dragged = icons.Items[fromS];
  68. if (fromS > to)
  69. {
  70. icons.Items.Remove(dragged);
  71. icons.Items.Insert(to, dragged);
  72. }
  73. else
  74. {
  75. icons.Items.Remove(dragged);
  76. icons.Items.Insert(to, dragged);
  77. }
  78. }
  79. );
  80. return _swap;
  81. }
  82. }
  83. DelegateCommand<int[]> _swap2;
  84. public DelegateCommand<int[]> SwapCommand2
  85. {
  86. get
  87. {
  88. if (_swap2 == null)
  89. _swap2 = new DelegateCommand<int[]>(
  90. (indexes) =>
  91. {
  92. int fromS = indexes[0];
  93. int to = indexes[1];
  94. ObservableCollection<MenuInfo> menuList = appData.MenuList;
  95. var elementSource = menuList[to];
  96. var dragged = menuList[fromS];
  97. if (fromS > to)
  98. {
  99. menuList.Remove(dragged);
  100. menuList.Insert(to, dragged);
  101. }
  102. else
  103. {
  104. menuList.Remove(dragged);
  105. menuList.Insert(to, dragged);
  106. }
  107. appData.MenuList = menuList;
  108. //menus.Items.Refresh();
  109. }
  110. );
  111. return _swap2;
  112. }
  113. }
  114. private void Wrap_Drop(object sender, DragEventArgs e)
  115. {
  116. Array dropObject = (System.Array)e.Data.GetData(DataFormats.FileDrop);
  117. if (dropObject == null) return;
  118. foreach (object obj in dropObject)
  119. {
  120. string path = (string)obj;
  121. if (File.Exists(path))
  122. {
  123. // 文件
  124. BitmapImage bi = FileIcon.GetBitmapImage(path);
  125. IconInfo iconInfo = new IconInfo();
  126. iconInfo.Path = path;
  127. iconInfo.BitmapImage = bi;
  128. iconInfo.Name = Path.GetFileNameWithoutExtension(path);
  129. ObservableCollection<IconInfo> iconList;
  130. if (appData.IconMap.ContainsKey("1"))
  131. {
  132. iconList = appData.IconMap["1"];
  133. }
  134. else
  135. {
  136. iconList = new ObservableCollection<IconInfo>();
  137. appData.IconMap.Add("1", iconList);
  138. }
  139. iconList.Add(iconInfo);
  140. icons.ItemsSource = iconList;
  141. CommonCode.SaveAppData(appData);
  142. }
  143. else if (Directory.Exists(path))
  144. {
  145. //文件夹
  146. }
  147. }
  148. icons.Items.Refresh();
  149. }
  150. //菜单点击事件
  151. private void menuClick(object sender, MouseButtonEventArgs e)
  152. {
  153. }
  154. /// <summary>
  155. /// 图标点击事件
  156. /// </summary>
  157. /// <param name="sender"></param>
  158. /// <param name="e"></param>
  159. private void dataClick(object sender, MouseButtonEventArgs e)
  160. {
  161. IconInfo icon = (IconInfo)((StackPanel)sender).Tag;
  162. System.Diagnostics.Process.Start(icon.Path);
  163. icon.Count++;
  164. CommonCode.SaveAppData(appData);
  165. }
  166. /// <summary>
  167. /// data选中事件 设置不可选中
  168. /// </summary>
  169. /// <param name="sender"></param>
  170. /// <param name="e"></param>
  171. private void data_SelectionChanged(object sender, SelectionChangedEventArgs e)
  172. {
  173. if (icons.SelectedIndex != -1) icons.SelectedIndex = -1;
  174. }
  175. #region Window_Loaded
  176. void Window_Loaded(object sender, RoutedEventArgs e)
  177. {
  178. //this.menus.Items.Add(new ViewModel.Menu() { menu = "test1" });
  179. //this.menus.Items.Add(new ViewModel.Menu() { menu = "test2" });
  180. //this.menus.Items.Add(new ViewModel.Menu() { menu = "test3" });
  181. }
  182. #endregion // Window_Loaded
  183. //#region Window_Closing
  184. //void Window_Closing(object sender, CancelEventArgs e)
  185. //{
  186. // Rect rect = this.RestoreBounds;
  187. // AppConfig config = this.DataContext as AppConfig;
  188. // config.WindowWidth = rect.Width;
  189. // config.WindowHeight = rect.Height;
  190. // CommonCode.SaveAppConfig(config);
  191. //}
  192. //#endregion // Window_Closing
  193. void MainWindow_Resize(object sender, System.EventArgs e)
  194. {
  195. if (this.DataContext != null)
  196. {
  197. AppData appData = this.DataContext as AppData;
  198. appData.AppConfig.WindowWidth = this.Width;
  199. appData.AppConfig.WindowHeight = this.Height;
  200. CommonCode.SaveAppData(appData);
  201. }
  202. }
  203. private void leftCard_MouseRightButtonDown(object sender, MouseButtonEventArgs e)
  204. {
  205. }
  206. /// <summary>
  207. /// 删除菜单
  208. /// </summary>
  209. /// <param name="sender"></param>
  210. /// <param name="e"></param>
  211. private void DeleteMenu(object sender, RoutedEventArgs e)
  212. {
  213. MenuInfo menuInfo = ((MenuItem)sender).Tag as MenuInfo;
  214. appData.MenuList.Remove(menuInfo);
  215. CommonCode.SaveAppData(appData);
  216. }
  217. private void StackPanel_MouseMove(object sender, MouseEventArgs e)
  218. {
  219. UIElementCollection childs = ((StackPanel)sender).Children;
  220. IEnumerator iEnumerator = childs.GetEnumerator();
  221. //((Image)iEnumerator.Current).Style;
  222. }
  223. /// <summary>
  224. /// 重命名菜单 将textbox 设置为可见
  225. /// </summary>
  226. /// <param name="sender"></param>
  227. /// <param name="e"></param>
  228. private void RenameMenu(object sender, RoutedEventArgs e)
  229. {
  230. MenuInfo menuInfo = ((MenuItem)sender).Tag as MenuInfo;
  231. menuInfo.MenuEdit = (int)Visibility.Visible;
  232. }
  233. /// <summary>
  234. /// 编辑菜单失焦或者敲下Enter键时保存修改后的菜单
  235. /// </summary>
  236. /// <param name="sender"></param>
  237. /// <param name="e"></param>
  238. private void LostFocusOrEnterDown(object sender, EventArgs e)
  239. {
  240. TextBox menuBox = null;
  241. if (e.GetType() == typeof(KeyEventArgs))
  242. {
  243. KeyEventArgs eKey = e as KeyEventArgs;
  244. if (eKey.Key == Key.Enter)
  245. {
  246. menuBox = ((TextBox)sender);
  247. }
  248. } else if(e.GetType() == typeof(RoutedEventArgs))
  249. {
  250. menuBox = ((TextBox)sender);
  251. }
  252. if (menuBox != null)
  253. {
  254. MenuInfo menuInfo = menuBox.Tag as MenuInfo;
  255. string text = menuBox.Text;
  256. menuInfo.MenuName = text;
  257. menuInfo.MenuEdit = (int)Visibility.Collapsed;
  258. CommonCode.SaveAppData(appData);
  259. }
  260. }
  261. /// <summary>
  262. /// 当修改菜单元素可见时 设置全选并获得焦点
  263. /// </summary>
  264. /// <param name="sender"></param>
  265. /// <param name="e"></param>
  266. private void MenuEditWhenVisibilityChanged(object sender, DependencyPropertyChangedEventArgs e)
  267. {
  268. TextBox box = sender as TextBox;
  269. if (box.Visibility == Visibility.Visible)
  270. {
  271. Keyboard.Focus(box);
  272. box.SelectAll();
  273. }
  274. }
  275. /// <summary>
  276. /// 当修改菜单元素可见时 设置原菜单为不可见 并且不可选中
  277. /// 修改菜单元素不可见时 原菜单可见 并 选中
  278. /// </summary>
  279. /// <param name="sender"></param>
  280. /// <param name="e"></param>
  281. private void MenuWhenVisibilityChanged(object sender, DependencyPropertyChangedEventArgs e)
  282. {
  283. TextBlock tb = sender as TextBlock;
  284. if (tb.Visibility == Visibility.Collapsed)
  285. {
  286. if (menus.SelectedIndex != -1)
  287. {
  288. menuSelectIndexTemp = menus.SelectedIndex;
  289. menus.SelectedIndex = -1;
  290. } else
  291. {
  292. menus.SelectedIndex = menuSelectIndexTemp;
  293. }
  294. }
  295. }
  296. /// <summary>
  297. /// 新建菜单
  298. /// </summary>
  299. /// <param name="sender"></param>
  300. /// <param name="e"></param>
  301. private void CreateMenu(object sender, RoutedEventArgs e)
  302. {
  303. appData.MenuList.Add(new MenuInfo() { MenuEdit = (int)Visibility.Collapsed, MenuId = "zz", MenuName = "NewGouop" });
  304. menus.SelectedIndex = appData.MenuList.Count - 1;
  305. //appData.MenuList[appData.MenuList.Count - 1].MenuEdit = (int)Visibility.Visible;
  306. CommonCode.SaveAppData(appData);
  307. }
  308. }
  309. }