LeftCardControl.xaml.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465
  1. using DraggAnimatedPanelExample;
  2. using GeekDesk.Constant;
  3. using GeekDesk.Control.Windows;
  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.Input;
  12. using System.Windows.Media;
  13. namespace GeekDesk.Control.UserControls.PannelCard
  14. {
  15. /// <summary>
  16. /// LeftCardControl.xaml 的交互逻辑
  17. /// </summary>
  18. public partial class LeftCardControl : UserControl
  19. {
  20. private int menuSelectIndexTemp = -1;
  21. private AppData appData = MainWindow.appData;
  22. private SolidColorBrush bac = new SolidColorBrush(Color.FromRgb(236, 236, 236));
  23. //是否正在修改菜单
  24. public bool IS_EDIT = false;
  25. public LeftCardControl()
  26. {
  27. InitializeComponent();
  28. this.Loaded += (s, e) =>
  29. {
  30. SelectLastMenu();
  31. SetMenuListBoxItemEvent();
  32. };
  33. }
  34. private void SetMenuListBoxItemEvent()
  35. {
  36. int size = MenuListBox.Items.Count;
  37. for (int i = 0; i < size; i++)
  38. {
  39. ListBoxItem lbi = (ListBoxItem)(MenuListBox.ItemContainerGenerator.ContainerFromIndex(i));
  40. if (lbi != null)
  41. {
  42. SetListBoxItemEvent(lbi);
  43. }
  44. }
  45. //首次触发不了Selected事件
  46. object obj = MenuListBox.ItemContainerGenerator.ContainerFromIndex(MenuListBox.SelectedIndex);
  47. Lbi_Selected(obj, null);
  48. }
  49. private void SetListBoxItemEvent(ListBoxItem lbi)
  50. {
  51. lbi.MouseEnter += (s, me) =>
  52. {
  53. lbi.Background = bac;
  54. };
  55. lbi.Unselected += Lbi_Unselected;
  56. lbi.MouseLeave += Lbi_MouseLeave;
  57. lbi.Selected += Lbi_Selected;
  58. }
  59. private void SelectLastMenu()
  60. {
  61. if (appData.AppConfig.SelectedMenuIndex >= appData.MenuList.Count || appData.AppConfig.SelectedMenuIndex == -1)
  62. {
  63. MenuListBox.SelectedIndex = 0;
  64. appData.AppConfig.SelectedMenuIndex = MenuListBox.SelectedIndex;
  65. appData.AppConfig.SelectedMenuIcons = appData.MenuList[0].IconList;
  66. }
  67. else
  68. {
  69. MenuListBox.SelectedIndex = appData.AppConfig.SelectedMenuIndex;
  70. appData.AppConfig.SelectedMenuIcons = appData.MenuList[appData.AppConfig.SelectedMenuIndex].IconList;
  71. }
  72. }
  73. DelegateCommand<int[]> _swap;
  74. public DelegateCommand<int[]> SwapCommand
  75. {
  76. get
  77. {
  78. if (_swap == null)
  79. _swap = new DelegateCommand<int[]>(
  80. (indexes) =>
  81. {
  82. int fromS = indexes[0];
  83. int to = indexes[1];
  84. ObservableCollection<MenuInfo> menuList = MainWindow.appData.MenuList;
  85. var elementSource = menuList[to];
  86. var dragged = menuList[fromS];
  87. menuList.Remove(dragged);
  88. menuList.Insert(to, dragged);
  89. MenuListBox.SelectedIndex = to;
  90. MainWindow.appData.MenuList = menuList;
  91. }
  92. );
  93. return _swap;
  94. }
  95. }
  96. /// <summary>
  97. /// 当修改菜单元素可见时 设置原菜单为不可见 并且不可选中
  98. /// 修改菜单元素不可见时 原菜单可见 并 选中
  99. /// </summary>
  100. /// <param name="sender"></param>
  101. /// <param name="e"></param>
  102. private void MenuWhenVisibilityChanged(object sender, DependencyPropertyChangedEventArgs e)
  103. {
  104. StackPanel sp = sender as StackPanel;
  105. ListBoxItem lbi = (sp.TemplatedParent as ContentPresenter).TemplatedParent as ListBoxItem;
  106. if (sp.Visibility == Visibility.Collapsed)
  107. {
  108. lbi.MouseEnter += Lbi_MouseEnter;
  109. if (MenuListBox.SelectedIndex != -1)
  110. {
  111. menuSelectIndexTemp = MenuListBox.SelectedIndex;
  112. MenuListBox.SelectedIndex = -1;
  113. }
  114. else
  115. {
  116. MenuListBox.SelectedIndex = menuSelectIndexTemp;
  117. }
  118. }
  119. else
  120. {
  121. lbi.MouseEnter += (s, me) =>
  122. {
  123. lbi.Background = bac;
  124. };
  125. lbi.MouseLeave += Lbi_MouseLeave;
  126. lbi.Selected += Lbi_Selected;
  127. }
  128. }
  129. #region 设置菜单触发事件
  130. private void Lbi_MouseEnter(object sender, MouseEventArgs e)
  131. {
  132. ListBoxItem lbi = sender as ListBoxItem;
  133. lbi.Background = Brushes.Transparent;
  134. }
  135. private void Lbi_Unselected(object sender, RoutedEventArgs e)
  136. {
  137. //添加Leave效果
  138. ListBoxItem lbi = sender as ListBoxItem;
  139. lbi.Background = Brushes.Transparent;
  140. lbi.MouseLeave += Lbi_MouseLeave;
  141. }
  142. private void Lbi_Selected(object sender, RoutedEventArgs e)
  143. {
  144. ListBoxItem lbi = sender as ListBoxItem;
  145. SolidColorBrush fontColor = new SolidColorBrush(Colors.Black);
  146. lbi.MouseLeave -= Lbi_MouseLeave;
  147. lbi.Background = bac;
  148. lbi.Foreground = fontColor;
  149. }
  150. private void Lbi_MouseLeave(object sender, MouseEventArgs e)
  151. {
  152. ListBoxItem lbi = sender as ListBoxItem;
  153. lbi.Background = Brushes.Transparent;
  154. }
  155. #endregion
  156. /// <summary>
  157. /// 新建菜单
  158. /// </summary>
  159. /// <param name="sender"></param>
  160. /// <param name="e"></param>
  161. private void CreateMenu(object sender, RoutedEventArgs e)
  162. {
  163. MenuInfo info = new MenuInfo() { MenuEdit = Visibility.Collapsed, MenuId = System.Guid.NewGuid().ToString(), MenuName = "NewMenu" };
  164. appData.MenuList.Add(info);
  165. MenuListBox.SelectedIndex = appData.MenuList.Count - 1;
  166. appData.AppConfig.SelectedMenuIndex = MenuListBox.SelectedIndex;
  167. appData.AppConfig.SelectedMenuIcons = info.IconList;
  168. //首次触发不了Selected事件
  169. object obj = MenuListBox.ItemContainerGenerator.ContainerFromIndex(MenuListBox.SelectedIndex);
  170. SetListBoxItemEvent((ListBoxItem)obj);
  171. Lbi_Selected(obj, null);
  172. }
  173. /// <summary>
  174. /// 重命名菜单 将textbox 设置为可见
  175. /// </summary>
  176. /// <param name="sender"></param>
  177. /// <param name="e"></param>
  178. private void RenameMenu(object sender, RoutedEventArgs e)
  179. {
  180. IS_EDIT = true;
  181. MenuInfo menuInfo = ((MenuItem)sender).Tag as MenuInfo;
  182. menuInfo.MenuEdit = (int)Visibility.Visible;
  183. }
  184. /// <summary>
  185. /// 删除菜单
  186. /// </summary>
  187. /// <param name="sender"></param>
  188. /// <param name="e"></param>
  189. private void DeleteMenu(object sender, RoutedEventArgs e)
  190. {
  191. MenuInfo menuInfo = ((MenuItem)sender).Tag as MenuInfo;
  192. if (appData.MenuList.Count == 1)
  193. {
  194. //如果删除以后没有菜单的话 先创建一个
  195. CreateMenu(null, null);
  196. }
  197. int index = appData.MenuList.IndexOf(menuInfo);
  198. if (index == 0)
  199. {
  200. index = 0;
  201. }
  202. else
  203. {
  204. index--;
  205. }
  206. appData.MenuList.Remove(menuInfo);
  207. // 选中下一个菜单
  208. MenuListBox.SelectedIndex = index;
  209. appData.AppConfig.SelectedMenuIndex = MenuListBox.SelectedIndex;
  210. appData.AppConfig.SelectedMenuIcons = appData.MenuList[index].IconList;
  211. }
  212. /// <summary>
  213. /// 编辑菜单失焦或者敲下Enter键时保存修改后的菜单
  214. /// </summary>
  215. /// <param name="sender"></param>
  216. /// <param name="e"></param>
  217. private void LostFocusOrEnterDown(object sender, EventArgs e)
  218. {
  219. bool done = true;
  220. TextBox menuBox = null;
  221. if (e.GetType() == typeof(KeyEventArgs))
  222. {
  223. KeyEventArgs eKey = e as KeyEventArgs;
  224. if (eKey.Key == Key.Enter)
  225. {
  226. menuBox = ((TextBox)sender);
  227. }
  228. else
  229. {
  230. done = false;
  231. }
  232. }
  233. else if (e.GetType() == typeof(RoutedEventArgs))
  234. {
  235. menuBox = ((TextBox)sender);
  236. }
  237. if (done)
  238. {
  239. if (menuBox != null)
  240. {
  241. MenuInfo menuInfo = menuBox.Tag as MenuInfo;
  242. string text = menuBox.Text;
  243. menuInfo.MenuName = text;
  244. menuInfo.MenuEdit = Visibility.Collapsed;
  245. }
  246. IS_EDIT = false;
  247. //为了解决无法修改菜单的问题
  248. MainWindow.mainWindow.SearchBox.Focus();
  249. MenuListBox.SelectedIndex = menuSelectIndexTemp;
  250. }
  251. }
  252. /// <summary>
  253. /// 当修改菜单元素可见时 设置全选并获得焦点
  254. /// </summary>
  255. /// <param name="sender"></param>
  256. /// <param name="e"></param>
  257. private void MenuEditWhenVisibilityChanged(object sender, DependencyPropertyChangedEventArgs e)
  258. {
  259. TextBox box = sender as TextBox;
  260. MenuInfo mi = box.Tag as MenuInfo;
  261. if (box.Visibility == Visibility.Visible)
  262. {
  263. Keyboard.Focus(box);
  264. box.SelectAll();
  265. }
  266. }
  267. /// <summary>
  268. /// 修改菜单图标
  269. /// </summary>
  270. /// <param name="sender"></param>
  271. /// <param name="e"></param>
  272. private void EditMenuGeometry(object sender, RoutedEventArgs e)
  273. {
  274. MenuInfo menuInfo = ((MenuItem)sender).Tag as MenuInfo;
  275. IconfontWindow.Show(SvgToGeometry.GetIconfonts(), menuInfo);
  276. }
  277. private void Menu_SelectionChanged(object sender, SelectionChangedEventArgs e)
  278. {
  279. if (IS_EDIT) return;
  280. MainWindow.mainWindow.RightCard.WrapCard.Visibility = Visibility.Collapsed;
  281. //设置对应菜单的图标列表
  282. if (MenuListBox.SelectedIndex == -1)
  283. {
  284. //appData.AppConfig.SelectedMenuIcons = appData.MenuList[appData.MenuList.Count - 1].IconList;
  285. }
  286. else
  287. {
  288. appData.AppConfig.SelectedMenuIcons = appData.MenuList[MenuListBox.SelectedIndex].IconList;
  289. }
  290. MainWindow.mainWindow.RightCard.WrapCard.Visibility = Visibility.Visible;
  291. }
  292. /// <summary>
  293. /// 鼠标悬停切换菜单
  294. /// </summary>
  295. /// <param name="sender"></param>
  296. /// <param name="e"></param>
  297. private void Menu_MouseEnter(object sender, MouseEventArgs e)
  298. {
  299. if (appData.AppConfig.HoverMenu && !IS_EDIT)
  300. {
  301. Thread t = new Thread(() =>
  302. {
  303. Thread.Sleep(200);
  304. this.Dispatcher.Invoke(() =>
  305. {
  306. ListBoxItem lbi = sender as ListBoxItem;
  307. if (lbi.IsMouseOver)
  308. {
  309. int index = MenuListBox.ItemContainerGenerator.IndexFromContainer(lbi);
  310. MenuListBox.SelectedIndex = index;
  311. }
  312. });
  313. });
  314. t.IsBackground = true;
  315. t.Start();
  316. }
  317. }
  318. /// <summary>
  319. /// 点击菜单后 隐藏搜索框
  320. /// </summary>
  321. /// <param name="sender"></param>
  322. /// <param name="e"></param>
  323. private void ListBoxItem_MouseDown(object sender, MouseButtonEventArgs e)
  324. {
  325. if (RunTimeStatus.SEARCH_BOX_SHOW)
  326. {
  327. MainWindow.mainWindow.HidedSearchBox();
  328. }
  329. ListBoxItem lbi = sender as ListBoxItem;
  330. MenuInfo mi = lbi.DataContext as MenuInfo;
  331. int index = MenuListBox.Items.IndexOf(mi);
  332. MenuListBox.SelectedIndex = index;
  333. }
  334. ///// <summary>
  335. ///// 点击菜单后 隐藏搜索框
  336. ///// </summary>
  337. ///// <param name="sender"></param>
  338. ///// <param name="e"></param>
  339. //private void ListBoxItemPanel_MouseDown(object sender, MouseButtonEventArgs e)
  340. //{
  341. // if (RunTimeStatus.SEARCH_BOX_SHOW)
  342. // {
  343. // MainWindow.mainWindow.HidedSearchBox();
  344. // }
  345. // MenuInfo mi = (sender as StackPanel).Tag as MenuInfo;
  346. // int index = MenuListBox.Items.IndexOf(mi);
  347. // MenuListBox.SelectedIndex = index;
  348. //}
  349. /// <summary>
  350. /// 隐藏搜索框
  351. /// </summary>
  352. /// <param name="sender"></param>
  353. /// <param name="e"></param>
  354. private void MyCard_MouseDown(object sender, MouseButtonEventArgs e)
  355. {
  356. if (RunTimeStatus.SEARCH_BOX_SHOW)
  357. {
  358. MainWindow.mainWindow.HidedSearchBox();
  359. }
  360. }
  361. private void Menu_MouseWheel(object sender, MouseWheelEventArgs e)
  362. {
  363. if (e.Delta < 0)
  364. {
  365. int index = MenuListBox.SelectedIndex;
  366. if (index < MenuListBox.Items.Count - 1)
  367. {
  368. index ++;
  369. } else
  370. {
  371. index = 0;
  372. }
  373. MenuListBox.SelectedIndex = index;
  374. } else if (e.Delta > 0)
  375. {
  376. int index = MenuListBox.SelectedIndex;
  377. if (index > 0)
  378. {
  379. index --;
  380. }
  381. else
  382. {
  383. index = MenuListBox.Items.Count - 1;
  384. }
  385. MenuListBox.SelectedIndex = index;
  386. }
  387. }
  388. private void Menu_PreviewDragLeave(object sender, DragEventArgs e)
  389. {
  390. MyPoptip.IsOpen = false;
  391. }
  392. private void Menu_PreviewDragEnter(object sender, DragEventArgs e)
  393. {
  394. MenuInfo mi = (sender as ListBoxItem).DataContext as MenuInfo;
  395. MyPoptipContent.Text = "移动至:" + mi.MenuName;
  396. MyPoptip.VerticalOffset = 30;
  397. MyPoptip.IsOpen = true;
  398. }
  399. private void Menu_MouseLeave(object sender, MouseEventArgs e)
  400. {
  401. MyPoptip.IsOpen = false;
  402. }
  403. private void Menu_Drop(object sender, DragEventArgs e)
  404. {
  405. MyPoptip.IsOpen = false;
  406. MenuInfo mi = (sender as ListBoxItem).DataContext as MenuInfo;
  407. IconInfo iconInfo = (IconInfo)e.Data.GetData(typeof(IconInfo));
  408. appData.MenuList[MenuListBox.SelectedIndex].IconList.Remove(iconInfo);
  409. appData.MenuList[MenuListBox.Items.IndexOf(mi)].IconList.Add(iconInfo);
  410. }
  411. }
  412. }