LeftCardControl.xaml.cs 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. using DraggAnimatedPanelExample;
  2. using GeekDesk.Control.Windows;
  3. using GeekDesk.Util;
  4. using GeekDesk.ViewModel;
  5. using System;
  6. using System.Collections.Generic;
  7. using System.Collections.ObjectModel;
  8. using System.Linq;
  9. using System.Text;
  10. using System.Threading.Tasks;
  11. using System.Windows;
  12. using System.Windows.Controls;
  13. using System.Windows.Data;
  14. using System.Windows.Documents;
  15. using System.Windows.Input;
  16. using System.Windows.Media;
  17. using System.Windows.Media.Effects;
  18. using System.Windows.Media.Imaging;
  19. using System.Windows.Navigation;
  20. using System.Windows.Shapes;
  21. namespace GeekDesk.Control.UserControls.PannelCard
  22. {
  23. /// <summary>
  24. /// LeftCardControl.xaml 的交互逻辑
  25. /// </summary>
  26. public partial class LeftCardControl : UserControl
  27. {
  28. private int menuSelectIndexTemp = -1;
  29. private AppData appData = MainWindow.appData;
  30. public LeftCardControl()
  31. {
  32. InitializeComponent();
  33. appData.AppConfig.SelectedMenuIcons = appData.MenuList[appData.AppConfig.SelectedMenuIndex].IconList;
  34. }
  35. DelegateCommand<int[]> _swap;
  36. public DelegateCommand<int[]> SwapCommand
  37. {
  38. get
  39. {
  40. if (_swap == null)
  41. _swap = new DelegateCommand<int[]>(
  42. (indexes) =>
  43. {
  44. int fromS = indexes[0];
  45. int to = indexes[1];
  46. ObservableCollection<MenuInfo> menuList = MainWindow.appData.MenuList;
  47. var elementSource = menuList[to];
  48. var dragged = menuList[fromS];
  49. menuList.Remove(dragged);
  50. menuList.Insert(to, dragged);
  51. menus.SelectedIndex = to;
  52. MainWindow.appData.MenuList = menuList;
  53. }
  54. );
  55. return _swap;
  56. }
  57. }
  58. ////菜单点击事件
  59. private void MenuClick(object sender, MouseButtonEventArgs e)
  60. {
  61. //设置对应菜单的图标列表
  62. MenuInfo mi = (MenuInfo)(((StackPanel)sender).Tag);
  63. appData.AppConfig.SelectedMenuIcons = mi.IconList;
  64. }
  65. /// <summary>
  66. /// 当修改菜单元素可见时 设置原菜单为不可见 并且不可选中
  67. /// 修改菜单元素不可见时 原菜单可见 并 选中
  68. /// </summary>
  69. /// <param name="sender"></param>
  70. /// <param name="e"></param>
  71. private void MenuWhenVisibilityChanged(object sender, DependencyPropertyChangedEventArgs e)
  72. {
  73. TextBlock tb = sender as TextBlock;
  74. if (tb.Visibility == Visibility.Collapsed)
  75. {
  76. if (menus.SelectedIndex != -1)
  77. {
  78. menuSelectIndexTemp = menus.SelectedIndex;
  79. menus.SelectedIndex = -1;
  80. }
  81. else
  82. {
  83. menus.SelectedIndex = menuSelectIndexTemp;
  84. }
  85. }
  86. }
  87. /// <summary>
  88. /// 新建菜单
  89. /// </summary>
  90. /// <param name="sender"></param>
  91. /// <param name="e"></param>
  92. private void CreateMenu(object sender, RoutedEventArgs e)
  93. {
  94. MenuInfo info = new MenuInfo() { MenuEdit = Visibility.Collapsed, MenuId = System.Guid.NewGuid().ToString(), MenuName = "NewMenu" };
  95. appData.MenuList.Add(info);
  96. menus.Items.Refresh();
  97. menus.SelectedIndex = appData.MenuList.Count - 1;
  98. appData.AppConfig.SelectedMenuIndex = menus.SelectedIndex;
  99. appData.AppConfig.SelectedMenuIcons = info.IconList;
  100. }
  101. /// <summary>
  102. /// 重命名菜单 将textbox 设置为可见
  103. /// </summary>
  104. /// <param name="sender"></param>
  105. /// <param name="e"></param>
  106. private void RenameMenu(object sender, RoutedEventArgs e)
  107. {
  108. MenuInfo menuInfo = ((MenuItem)sender).Tag as MenuInfo;
  109. menuInfo.MenuEdit = (int)Visibility.Visible;
  110. }
  111. /// <summary>
  112. /// 删除菜单
  113. /// </summary>
  114. /// <param name="sender"></param>
  115. /// <param name="e"></param>
  116. private void DeleteMenu(object sender, RoutedEventArgs e)
  117. {
  118. MenuInfo menuInfo = ((MenuItem)sender).Tag as MenuInfo;
  119. if (appData.MenuList.Count == 1)
  120. {
  121. //如果删除以后没有菜单的话 先创建一个
  122. CreateMenu(null, null);
  123. }
  124. appData.MenuList.Remove(menuInfo);
  125. if (menus.SelectedIndex == -1)
  126. {
  127. // 选中下一个菜单
  128. menus.SelectedIndex = 0;
  129. appData.AppConfig.SelectedMenuIndex = menus.SelectedIndex;
  130. appData.AppConfig.SelectedMenuIcons = appData.MenuList[0].IconList;
  131. }
  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. }
  186. }