LeftCardControl.xaml.cs 6.5 KB

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