LeftCardControl.xaml.cs 7.4 KB

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