LeftCardControl.xaml.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354
  1. using DraggAnimatedPanelExample;
  2. using GeekDesk.Constant;
  3. using GeekDesk.Control.Windows;
  4. using GeekDesk.MyThread;
  5. using GeekDesk.Util;
  6. using GeekDesk.ViewModel;
  7. using System;
  8. using System.Collections.ObjectModel;
  9. using System.Threading;
  10. using System.Windows;
  11. using System.Windows.Controls;
  12. using System.Windows.Data;
  13. using System.Windows.Input;
  14. using System.Windows.Media;
  15. using System.Windows.Threading;
  16. namespace GeekDesk.Control.UserControls.PannelCard
  17. {
  18. /// <summary>
  19. /// LeftCardControl.xaml 的交互逻辑
  20. /// </summary>
  21. public partial class LeftCardControl : UserControl
  22. {
  23. private int menuSelectIndexTemp = -1;
  24. private AppData appData = MainWindow.appData;
  25. private SolidColorBrush bac = new SolidColorBrush(Color.FromRgb(236, 236, 236));
  26. //是否正在修改菜单
  27. private static bool IS_EDIT = false;
  28. public LeftCardControl()
  29. {
  30. InitializeComponent();
  31. this.Loaded += (s, e) =>
  32. {
  33. SelectLastMenu();
  34. SetMenuListBoxItemEvent();
  35. };
  36. }
  37. private void SetMenuListBoxItemEvent()
  38. {
  39. int size = MenuListBox.Items.Count;
  40. for (int i = 0; i < size; i++)
  41. {
  42. ListBoxItem lbi = (ListBoxItem)(MenuListBox.ItemContainerGenerator.ContainerFromIndex(i));
  43. if (lbi != null)
  44. {
  45. SetListBoxItemEvent(lbi);
  46. }
  47. }
  48. //首次触发不了Selected事件
  49. object obj = MenuListBox.ItemContainerGenerator.ContainerFromIndex(MenuListBox.SelectedIndex);
  50. Lbi_Selected(obj, null);
  51. }
  52. private void SetListBoxItemEvent(ListBoxItem lbi)
  53. {
  54. lbi.MouseEnter += (s, me) =>
  55. {
  56. lbi.Background = bac;
  57. };
  58. lbi.Unselected += Lbi_Unselected;
  59. lbi.MouseLeave += Lbi_MouseLeave;
  60. lbi.Selected += Lbi_Selected;
  61. }
  62. private void SelectLastMenu()
  63. {
  64. if (appData.AppConfig.SelectedMenuIndex >= appData.MenuList.Count || appData.AppConfig.SelectedMenuIndex == -1)
  65. {
  66. MenuListBox.SelectedIndex = 0;
  67. appData.AppConfig.SelectedMenuIndex = MenuListBox.SelectedIndex;
  68. appData.AppConfig.SelectedMenuIcons = appData.MenuList[0].IconList;
  69. }
  70. else
  71. {
  72. MenuListBox.SelectedIndex = appData.AppConfig.SelectedMenuIndex;
  73. appData.AppConfig.SelectedMenuIcons = appData.MenuList[appData.AppConfig.SelectedMenuIndex].IconList;
  74. }
  75. }
  76. DelegateCommand<int[]> _swap;
  77. public DelegateCommand<int[]> SwapCommand
  78. {
  79. get
  80. {
  81. if (_swap == null)
  82. _swap = new DelegateCommand<int[]>(
  83. (indexes) =>
  84. {
  85. int fromS = indexes[0];
  86. int to = indexes[1];
  87. ObservableCollection<MenuInfo> menuList = MainWindow.appData.MenuList;
  88. var elementSource = menuList[to];
  89. var dragged = menuList[fromS];
  90. menuList.Remove(dragged);
  91. menuList.Insert(to, dragged);
  92. MenuListBox.SelectedIndex = to;
  93. MainWindow.appData.MenuList = menuList;
  94. }
  95. );
  96. return _swap;
  97. }
  98. }
  99. /// <summary>
  100. /// 当修改菜单元素可见时 设置原菜单为不可见 并且不可选中
  101. /// 修改菜单元素不可见时 原菜单可见 并 选中
  102. /// </summary>
  103. /// <param name="sender"></param>
  104. /// <param name="e"></param>
  105. private void MenuWhenVisibilityChanged(object sender, DependencyPropertyChangedEventArgs e)
  106. {
  107. StackPanel sp = sender as StackPanel;
  108. ListBoxItem lbi = (sp.TemplatedParent as ContentPresenter).TemplatedParent as ListBoxItem;
  109. if (sp.Visibility == Visibility.Collapsed)
  110. {
  111. lbi.MouseEnter += Lbi_MouseEnter;
  112. if (MenuListBox.SelectedIndex != -1)
  113. {
  114. menuSelectIndexTemp = MenuListBox.SelectedIndex;
  115. MenuListBox.SelectedIndex = -1;
  116. }
  117. else
  118. {
  119. MenuListBox.SelectedIndex = menuSelectIndexTemp;
  120. }
  121. }
  122. else
  123. {
  124. lbi.MouseEnter += (s, me) =>
  125. {
  126. lbi.Background = bac;
  127. };
  128. lbi.MouseLeave += Lbi_MouseLeave;
  129. lbi.Selected += Lbi_Selected;
  130. }
  131. }
  132. #region 设置菜单触发事件
  133. private void Lbi_MouseEnter(object sender, MouseEventArgs e)
  134. {
  135. ListBoxItem lbi = sender as ListBoxItem;
  136. lbi.Background = Brushes.Transparent;
  137. }
  138. private void Lbi_Unselected(object sender, RoutedEventArgs e)
  139. {
  140. //添加Leave效果
  141. ListBoxItem lbi = sender as ListBoxItem;
  142. lbi.Background = Brushes.Transparent;
  143. lbi.MouseLeave += Lbi_MouseLeave;
  144. }
  145. private void Lbi_Selected(object sender, RoutedEventArgs e)
  146. {
  147. ListBoxItem lbi = sender as ListBoxItem;
  148. SolidColorBrush fontColor = new SolidColorBrush(Colors.Black);
  149. lbi.MouseLeave -= Lbi_MouseLeave;
  150. lbi.Background = bac;
  151. lbi.Foreground = fontColor;
  152. }
  153. private void Lbi_MouseLeave(object sender, MouseEventArgs e)
  154. {
  155. ListBoxItem lbi = sender as ListBoxItem;
  156. lbi.Background = Brushes.Transparent;
  157. }
  158. #endregion
  159. /// <summary>
  160. /// 新建菜单
  161. /// </summary>
  162. /// <param name="sender"></param>
  163. /// <param name="e"></param>
  164. private void CreateMenu(object sender, RoutedEventArgs e)
  165. {
  166. MenuInfo info = new MenuInfo() { MenuEdit = Visibility.Collapsed, MenuId = System.Guid.NewGuid().ToString(), MenuName = "NewMenu" };
  167. appData.MenuList.Add(info);
  168. MenuListBox.SelectedIndex = appData.MenuList.Count - 1;
  169. appData.AppConfig.SelectedMenuIndex = MenuListBox.SelectedIndex;
  170. appData.AppConfig.SelectedMenuIcons = info.IconList;
  171. //首次触发不了Selected事件
  172. object obj = MenuListBox.ItemContainerGenerator.ContainerFromIndex(MenuListBox.SelectedIndex);
  173. SetListBoxItemEvent((ListBoxItem)obj);
  174. Lbi_Selected(obj, null);
  175. }
  176. /// <summary>
  177. /// 重命名菜单 将textbox 设置为可见
  178. /// </summary>
  179. /// <param name="sender"></param>
  180. /// <param name="e"></param>
  181. private void RenameMenu(object sender, RoutedEventArgs e)
  182. {
  183. MenuInfo menuInfo = ((MenuItem)sender).Tag as MenuInfo;
  184. menuInfo.MenuEdit = (int)Visibility.Visible;
  185. IS_EDIT = true;
  186. }
  187. /// <summary>
  188. /// 删除菜单
  189. /// </summary>
  190. /// <param name="sender"></param>
  191. /// <param name="e"></param>
  192. private void DeleteMenu(object sender, RoutedEventArgs e)
  193. {
  194. MenuInfo menuInfo = ((MenuItem)sender).Tag as MenuInfo;
  195. if (appData.MenuList.Count == 1)
  196. {
  197. //如果删除以后没有菜单的话 先创建一个
  198. CreateMenu(null, null);
  199. }
  200. int index = appData.MenuList.IndexOf(menuInfo);
  201. if (index == 0)
  202. {
  203. index = 0;
  204. }
  205. else
  206. {
  207. index--;
  208. }
  209. appData.MenuList.Remove(menuInfo);
  210. // 选中下一个菜单
  211. MenuListBox.SelectedIndex = index;
  212. appData.AppConfig.SelectedMenuIndex = MenuListBox.SelectedIndex;
  213. appData.AppConfig.SelectedMenuIcons = appData.MenuList[index].IconList;
  214. }
  215. /// <summary>
  216. /// 编辑菜单失焦或者敲下Enter键时保存修改后的菜单
  217. /// </summary>
  218. /// <param name="sender"></param>
  219. /// <param name="e"></param>
  220. private void LostFocusOrEnterDown(object sender, EventArgs e)
  221. {
  222. bool done = true;
  223. TextBox menuBox = null;
  224. if (e.GetType() == typeof(KeyEventArgs))
  225. {
  226. KeyEventArgs eKey = e as KeyEventArgs;
  227. if (eKey.Key == Key.Enter)
  228. {
  229. menuBox = ((TextBox)sender);
  230. }
  231. else
  232. {
  233. done = false;
  234. }
  235. }
  236. else if (e.GetType() == typeof(RoutedEventArgs))
  237. {
  238. menuBox = ((TextBox)sender);
  239. }
  240. if (done)
  241. {
  242. if (menuBox != null)
  243. {
  244. MenuInfo menuInfo = menuBox.Tag as MenuInfo;
  245. string text = menuBox.Text;
  246. menuInfo.MenuName = text;
  247. menuInfo.MenuEdit = Visibility.Collapsed;
  248. }
  249. IS_EDIT = false;
  250. MenuListBox.SelectedIndex = menuSelectIndexTemp;
  251. }
  252. }
  253. /// <summary>
  254. /// 当修改菜单元素可见时 设置全选并获得焦点
  255. /// </summary>
  256. /// <param name="sender"></param>
  257. /// <param name="e"></param>
  258. private void MenuEditWhenVisibilityChanged(object sender, DependencyPropertyChangedEventArgs e)
  259. {
  260. TextBox box = sender as TextBox;
  261. MenuInfo mi = box.Tag as MenuInfo;
  262. if (box.Visibility == Visibility.Visible)
  263. {
  264. Keyboard.Focus(box);
  265. box.SelectAll();
  266. }
  267. }
  268. /// <summary>
  269. /// 修改菜单图标
  270. /// </summary>
  271. /// <param name="sender"></param>
  272. /// <param name="e"></param>
  273. private void EditMenuGeometry(object sender, RoutedEventArgs e)
  274. {
  275. MenuInfo menuInfo = ((MenuItem)sender).Tag as MenuInfo;
  276. IconfontWindow.Show(SvgToGeometry.GetIconfonts(), menuInfo);
  277. }
  278. private void Menu_SelectionChanged(object sender, SelectionChangedEventArgs e)
  279. {
  280. //设置对应菜单的图标列表
  281. if (MenuListBox.SelectedIndex == -1)
  282. {
  283. //appData.AppConfig.SelectedMenuIcons = appData.MenuList[appData.MenuList.Count - 1].IconList;
  284. }
  285. else
  286. {
  287. appData.AppConfig.SelectedMenuIcons = appData.MenuList[MenuListBox.SelectedIndex].IconList;
  288. }
  289. }
  290. /// <summary>
  291. /// 鼠标悬停切换菜单
  292. /// </summary>
  293. /// <param name="sender"></param>
  294. /// <param name="e"></param>
  295. private void Menu_MouseEnter(object sender, MouseEventArgs e)
  296. {
  297. if (appData.AppConfig.HoverMenu && !IS_EDIT)
  298. {
  299. new Thread(() =>
  300. {
  301. Thread.Sleep(200);
  302. this.Dispatcher.Invoke(() =>
  303. {
  304. ListBoxItem lbi = sender as ListBoxItem;
  305. if (lbi.IsMouseOver)
  306. {
  307. int index = MenuListBox.ItemContainerGenerator.IndexFromContainer(lbi);
  308. MenuListBox.SelectedIndex = index;
  309. }
  310. });
  311. }).Start();
  312. }
  313. }
  314. }
  315. }