LeftCardControl.xaml.cs 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. using DraggAnimatedPanelExample;
  2. using GeekDesk.Control.Windows;
  3. using GeekDesk.Util;
  4. using GeekDesk.ViewModel;
  5. using System;
  6. using System.Collections.ObjectModel;
  7. using System.Windows;
  8. using System.Windows.Controls;
  9. using System.Windows.Input;
  10. namespace GeekDesk.Control.UserControls.PannelCard
  11. {
  12. /// <summary>
  13. /// LeftCardControl.xaml 的交互逻辑
  14. /// </summary>
  15. public partial class LeftCardControl : UserControl
  16. {
  17. private int menuSelectIndexTemp = -1;
  18. private AppData appData = MainWindow.appData;
  19. public LeftCardControl()
  20. {
  21. InitializeComponent();
  22. if (appData.AppConfig.SelectedMenuIndex >= appData.MenuList.Count || appData.AppConfig.SelectedMenuIndex == -1)
  23. {
  24. appData.AppConfig.SelectedMenuIcons = appData.MenuList[0].IconList;
  25. } else
  26. {
  27. appData.AppConfig.SelectedMenuIcons = appData.MenuList[appData.AppConfig.SelectedMenuIndex].IconList;
  28. }
  29. }
  30. DelegateCommand<int[]> _swap;
  31. public DelegateCommand<int[]> SwapCommand
  32. {
  33. get
  34. {
  35. if (_swap == null)
  36. _swap = new DelegateCommand<int[]>(
  37. (indexes) =>
  38. {
  39. int fromS = indexes[0];
  40. int to = indexes[1];
  41. ObservableCollection<MenuInfo> menuList = MainWindow.appData.MenuList;
  42. var elementSource = menuList[to];
  43. var dragged = menuList[fromS];
  44. menuList.Remove(dragged);
  45. menuList.Insert(to, dragged);
  46. MenuListBox.SelectedIndex = to;
  47. MainWindow.appData.MenuList = menuList;
  48. }
  49. );
  50. return _swap;
  51. }
  52. }
  53. ////菜单点击事件
  54. private void MenuClick(object sender, MouseButtonEventArgs e)
  55. {
  56. //设置对应菜单的图标列表
  57. MenuInfo mi = (MenuInfo)(((StackPanel)sender).Tag);
  58. appData.AppConfig.SelectedMenuIcons = mi.IconList;
  59. }
  60. /// <summary>
  61. /// 当修改菜单元素可见时 设置原菜单为不可见 并且不可选中
  62. /// 修改菜单元素不可见时 原菜单可见 并 选中
  63. /// </summary>
  64. /// <param name="sender"></param>
  65. /// <param name="e"></param>
  66. private void MenuWhenVisibilityChanged(object sender, DependencyPropertyChangedEventArgs e)
  67. {
  68. TextBlock tb = sender as TextBlock;
  69. if (tb.Visibility == Visibility.Collapsed)
  70. {
  71. if (MenuListBox.SelectedIndex != -1)
  72. {
  73. menuSelectIndexTemp = MenuListBox.SelectedIndex;
  74. MenuListBox.SelectedIndex = -1;
  75. }
  76. else
  77. {
  78. MenuListBox.SelectedIndex = menuSelectIndexTemp;
  79. }
  80. }
  81. }
  82. /// <summary>
  83. /// 新建菜单
  84. /// </summary>
  85. /// <param name="sender"></param>
  86. /// <param name="e"></param>
  87. private void CreateMenu(object sender, RoutedEventArgs e)
  88. {
  89. MenuInfo info = new MenuInfo() { MenuEdit = Visibility.Collapsed, MenuId = System.Guid.NewGuid().ToString(), MenuName = "NewMenu" };
  90. appData.MenuList.Add(info);
  91. MenuListBox.Items.Refresh();
  92. MenuListBox.SelectedIndex = appData.MenuList.Count - 1;
  93. appData.AppConfig.SelectedMenuIndex = MenuListBox.SelectedIndex;
  94. appData.AppConfig.SelectedMenuIcons = info.IconList;
  95. }
  96. /// <summary>
  97. /// 重命名菜单 将textbox 设置为可见
  98. /// </summary>
  99. /// <param name="sender"></param>
  100. /// <param name="e"></param>
  101. private void RenameMenu(object sender, RoutedEventArgs e)
  102. {
  103. MenuInfo menuInfo = ((MenuItem)sender).Tag as MenuInfo;
  104. menuInfo.MenuEdit = (int)Visibility.Visible;
  105. }
  106. /// <summary>
  107. /// 删除菜单
  108. /// </summary>
  109. /// <param name="sender"></param>
  110. /// <param name="e"></param>
  111. private void DeleteMenu(object sender, RoutedEventArgs e)
  112. {
  113. MenuInfo menuInfo = ((MenuItem)sender).Tag as MenuInfo;
  114. if (appData.MenuList.Count == 1)
  115. {
  116. //如果删除以后没有菜单的话 先创建一个
  117. CreateMenu(null, null);
  118. }
  119. int index = appData.MenuList.IndexOf(menuInfo);
  120. if(index == 0)
  121. {
  122. index = 0;
  123. } else
  124. {
  125. index = index - 1;
  126. }
  127. appData.MenuList.Remove(menuInfo);
  128. // 选中下一个菜单
  129. MenuListBox.SelectedIndex = index;
  130. appData.AppConfig.SelectedMenuIndex = MenuListBox.SelectedIndex;
  131. appData.AppConfig.SelectedMenuIcons = appData.MenuList[index].IconList;
  132. }
  133. /// <summary>
  134. /// 编辑菜单失焦或者敲下Enter键时保存修改后的菜单
  135. /// </summary>
  136. /// <param name="sender"></param>
  137. /// <param name="e"></param>
  138. private void LostFocusOrEnterDown(object sender, EventArgs e)
  139. {
  140. TextBox menuBox = null;
  141. if (e.GetType() == typeof(KeyEventArgs))
  142. {
  143. KeyEventArgs eKey = e as KeyEventArgs;
  144. if (eKey.Key == Key.Enter)
  145. {
  146. menuBox = ((TextBox)sender);
  147. }
  148. }
  149. else if (e.GetType() == typeof(RoutedEventArgs))
  150. {
  151. menuBox = ((TextBox)sender);
  152. }
  153. if (menuBox != null)
  154. {
  155. MenuInfo menuInfo = menuBox.Tag as MenuInfo;
  156. string text = menuBox.Text;
  157. menuInfo.MenuName = text;
  158. menuInfo.MenuEdit = Visibility.Collapsed;
  159. }
  160. }
  161. /// <summary>
  162. /// 当修改菜单元素可见时 设置全选并获得焦点
  163. /// </summary>
  164. /// <param name="sender"></param>
  165. /// <param name="e"></param>
  166. private void MenuEditWhenVisibilityChanged(object sender, DependencyPropertyChangedEventArgs e)
  167. {
  168. TextBox box = sender as TextBox;
  169. if (box.Visibility == Visibility.Visible)
  170. {
  171. Keyboard.Focus(box);
  172. box.SelectAll();
  173. }
  174. }
  175. /// <summary>
  176. /// 修改菜单图标
  177. /// </summary>
  178. /// <param name="sender"></param>
  179. /// <param name="e"></param>
  180. private void EditMenuGeometry(object sender, RoutedEventArgs e)
  181. {
  182. MenuInfo menuInfo = ((MenuItem)sender).Tag as MenuInfo;
  183. IconfontWindow.Show(SvgToGeometry.GetIconfonts(), menuInfo);
  184. }
  185. private void menus_SelectionChanged(object sender, SelectionChangedEventArgs e)
  186. {
  187. //设置对应菜单的图标列表
  188. if (MenuListBox.SelectedIndex == -1)
  189. {
  190. appData.AppConfig.SelectedMenuIcons = appData.MenuList[appData.MenuList.Count-1].IconList;
  191. } else
  192. {
  193. appData.AppConfig.SelectedMenuIcons = appData.MenuList[MenuListBox.SelectedIndex].IconList;
  194. }
  195. }
  196. }
  197. }