LeftCardControl.xaml.cs 12 KB

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