LeftCardControl.xaml.cs 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579
  1. using DraggAnimatedPanelExample;
  2. using GeekDesk.Constant;
  3. using GeekDesk.Control.Other;
  4. using GeekDesk.Control.Windows;
  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.Input;
  13. using System.Windows.Media;
  14. namespace GeekDesk.Control.UserControls.PannelCard
  15. {
  16. /// <summary>
  17. /// LeftCardControl.xaml 的交互逻辑
  18. /// </summary>
  19. public partial class LeftCardControl : UserControl
  20. {
  21. private int menuSelectIndexTemp = -1;
  22. private AppData appData = MainWindow.appData;
  23. private SolidColorBrush bac = new SolidColorBrush(Color.FromRgb(236, 236, 236));
  24. public LeftCardControl()
  25. {
  26. InitializeComponent();
  27. this.Loaded += (s, e) =>
  28. {
  29. SelectLastMenu();
  30. SetMenuListBoxItemEvent();
  31. };
  32. }
  33. private void SetMenuListBoxItemEvent()
  34. {
  35. int size = MenuListBox.Items.Count;
  36. for (int i = 0; i < size; i++)
  37. {
  38. ListBoxItem lbi = (ListBoxItem)(MenuListBox.ItemContainerGenerator.ContainerFromIndex(i));
  39. if (lbi != null)
  40. {
  41. SetListBoxItemEvent(lbi);
  42. }
  43. }
  44. //首次触发不了Selected事件
  45. object obj = MenuListBox.ItemContainerGenerator.ContainerFromIndex(MenuListBox.SelectedIndex);
  46. Lbi_Selected(obj, null);
  47. }
  48. private void SetListBoxItemEvent(ListBoxItem lbi)
  49. {
  50. lbi.MouseEnter += (s, me) =>
  51. {
  52. lbi.Background = bac;
  53. };
  54. lbi.Unselected += Lbi_Unselected;
  55. lbi.MouseLeave += Lbi_MouseLeave;
  56. lbi.Selected += Lbi_Selected;
  57. }
  58. private void SelectLastMenu()
  59. {
  60. if (appData.AppConfig.SelectedMenuIndex >= appData.MenuList.Count || appData.AppConfig.SelectedMenuIndex == -1)
  61. {
  62. MenuListBox.SelectedIndex = 0;
  63. appData.AppConfig.SelectedMenuIndex = MenuListBox.SelectedIndex;
  64. appData.AppConfig.SelectedMenuIcons = appData.MenuList[0].IconList;
  65. }
  66. else
  67. {
  68. MenuListBox.SelectedIndex = appData.AppConfig.SelectedMenuIndex;
  69. appData.AppConfig.SelectedMenuIcons = appData.MenuList[appData.AppConfig.SelectedMenuIndex].IconList;
  70. }
  71. }
  72. DelegateCommand<int[]> _swap;
  73. public DelegateCommand<int[]> SwapCommand
  74. {
  75. get
  76. {
  77. if (_swap == null)
  78. _swap = new DelegateCommand<int[]>(
  79. (indexes) =>
  80. {
  81. int fromS = indexes[0];
  82. int to = indexes[1];
  83. ObservableCollection<MenuInfo> menuList = MainWindow.appData.MenuList;
  84. var elementSource = menuList[to];
  85. var dragged = menuList[fromS];
  86. menuList.Remove(dragged);
  87. menuList.Insert(to, dragged);
  88. MenuListBox.SelectedIndex = to;
  89. MainWindow.appData.MenuList = menuList;
  90. }
  91. );
  92. return _swap;
  93. }
  94. }
  95. /// <summary>
  96. /// 当修改菜单元素可见时 设置原菜单为不可见 并且不可选中
  97. /// 修改菜单元素不可见时 原菜单可见 并 选中
  98. /// </summary>
  99. /// <param name="sender"></param>
  100. /// <param name="e"></param>
  101. private void MenuWhenVisibilityChanged(object sender, DependencyPropertyChangedEventArgs e)
  102. {
  103. StackPanel sp = sender as StackPanel;
  104. ListBoxItem lbi = (sp.TemplatedParent as ContentPresenter).TemplatedParent as ListBoxItem;
  105. if (sp.Visibility == Visibility.Collapsed)
  106. {
  107. lbi.MouseEnter += Lbi_MouseEnter;
  108. if (MenuListBox.SelectedIndex != -1)
  109. {
  110. menuSelectIndexTemp = MenuListBox.SelectedIndex;
  111. MenuListBox.SelectedIndex = -1;
  112. }
  113. else
  114. {
  115. MenuListBox.SelectedIndex = menuSelectIndexTemp;
  116. }
  117. }
  118. else
  119. {
  120. lbi.MouseEnter += (s, me) =>
  121. {
  122. lbi.Background = bac;
  123. };
  124. lbi.MouseLeave += Lbi_MouseLeave;
  125. lbi.Selected += Lbi_Selected;
  126. }
  127. }
  128. #region 设置菜单触发事件
  129. private void Lbi_MouseEnter(object sender, MouseEventArgs e)
  130. {
  131. ListBoxItem lbi = sender as ListBoxItem;
  132. lbi.Background = Brushes.Transparent;
  133. }
  134. private void Lbi_Unselected(object sender, RoutedEventArgs e)
  135. {
  136. //添加Leave效果
  137. ListBoxItem lbi = sender as ListBoxItem;
  138. lbi.Background = Brushes.Transparent;
  139. lbi.MouseLeave += Lbi_MouseLeave;
  140. }
  141. private void Lbi_Selected(object sender, RoutedEventArgs e)
  142. {
  143. ListBoxItem lbi = sender as ListBoxItem;
  144. SolidColorBrush fontColor = new SolidColorBrush(Colors.Black);
  145. lbi.MouseLeave -= Lbi_MouseLeave;
  146. lbi.Background = bac;
  147. lbi.Foreground = fontColor;
  148. }
  149. private void Lbi_MouseLeave(object sender, MouseEventArgs e)
  150. {
  151. ListBoxItem lbi = sender as ListBoxItem;
  152. lbi.Background = Brushes.Transparent;
  153. }
  154. #endregion
  155. /// <summary>
  156. /// 新建菜单
  157. /// </summary>
  158. /// <param name="sender"></param>
  159. /// <param name="e"></param>
  160. private void CreateMenu(object sender, RoutedEventArgs e)
  161. {
  162. MenuInfo info = new MenuInfo() { MenuEdit = Visibility.Collapsed, MenuId = System.Guid.NewGuid().ToString(), MenuName = "NewMenu" };
  163. appData.MenuList.Add(info);
  164. MenuListBox.SelectedIndex = appData.MenuList.Count - 1;
  165. appData.AppConfig.SelectedMenuIndex = MenuListBox.SelectedIndex;
  166. appData.AppConfig.SelectedMenuIcons = info.IconList;
  167. //首次触发不了Selected事件
  168. object obj = MenuListBox.ItemContainerGenerator.ContainerFromIndex(MenuListBox.SelectedIndex);
  169. SetListBoxItemEvent((ListBoxItem)obj);
  170. Lbi_Selected(obj, null);
  171. }
  172. /// <summary>
  173. /// 重命名菜单 将textbox 设置为可见
  174. /// </summary>
  175. /// <param name="sender"></param>
  176. /// <param name="e"></param>
  177. private void RenameMenu(object sender, RoutedEventArgs e)
  178. {
  179. RunTimeStatus.IS_MENU_EDIT = true;
  180. MenuInfo menuInfo = ((MenuItem)sender).Tag as MenuInfo;
  181. menuInfo.MenuEdit = (int)Visibility.Visible;
  182. }
  183. /// <summary>
  184. /// 删除菜单
  185. /// </summary>
  186. /// <param name="sender"></param>
  187. /// <param name="e"></param>
  188. private void DeleteMenu(object sender, RoutedEventArgs e)
  189. {
  190. MenuInfo menuInfo = ((MenuItem)sender).Tag as MenuInfo;
  191. if (appData.MenuList.Count == 1)
  192. {
  193. //如果删除以后没有菜单的话 先创建一个
  194. CreateMenu(null, null);
  195. }
  196. int index = appData.MenuList.IndexOf(menuInfo);
  197. if (index == 0)
  198. {
  199. index = 0;
  200. }
  201. else
  202. {
  203. index--;
  204. }
  205. appData.MenuList.Remove(menuInfo);
  206. // 选中下一个菜单
  207. MenuListBox.SelectedIndex = index;
  208. appData.AppConfig.SelectedMenuIndex = MenuListBox.SelectedIndex;
  209. appData.AppConfig.SelectedMenuIcons = appData.MenuList[index].IconList;
  210. }
  211. /// <summary>
  212. /// 编辑菜单失焦或者敲下Enter键时保存修改后的菜单
  213. /// </summary>
  214. /// <param name="sender"></param>
  215. /// <param name="e"></param>
  216. private void LostFocusOrEnterDown(object sender, EventArgs e)
  217. {
  218. bool done = true;
  219. TextBox menuBox = null;
  220. if (e.GetType() == typeof(KeyEventArgs))
  221. {
  222. KeyEventArgs eKey = e as KeyEventArgs;
  223. if (eKey.Key == Key.Enter)
  224. {
  225. menuBox = ((TextBox)sender);
  226. }
  227. else
  228. {
  229. done = false;
  230. }
  231. }
  232. else if (e.GetType() == typeof(RoutedEventArgs))
  233. {
  234. menuBox = ((TextBox)sender);
  235. }
  236. if (done)
  237. {
  238. if (menuBox != null)
  239. {
  240. MenuInfo menuInfo = menuBox.Tag as MenuInfo;
  241. string text = menuBox.Text;
  242. menuInfo.MenuName = text;
  243. menuInfo.MenuEdit = Visibility.Collapsed;
  244. }
  245. RunTimeStatus.IS_MENU_EDIT = false;
  246. //为了解决无法修改菜单的问题
  247. MainWindow.mainWindow.SearchBox.Focus();
  248. MenuListBox.SelectedIndex = menuSelectIndexTemp;
  249. }
  250. }
  251. /// <summary>
  252. /// 当修改菜单元素可见时 设置全选并获得焦点
  253. /// </summary>
  254. /// <param name="sender"></param>
  255. /// <param name="e"></param>
  256. private void MenuEditWhenVisibilityChanged(object sender, DependencyPropertyChangedEventArgs e)
  257. {
  258. TextBox box = sender as TextBox;
  259. MenuInfo mi = box.Tag as MenuInfo;
  260. if (box.Visibility == Visibility.Visible)
  261. {
  262. Keyboard.Focus(box);
  263. box.SelectAll();
  264. }
  265. }
  266. /// <summary>
  267. /// 修改菜单图标
  268. /// </summary>
  269. /// <param name="sender"></param>
  270. /// <param name="e"></param>
  271. private void EditMenuGeometry(object sender, RoutedEventArgs e)
  272. {
  273. MenuInfo menuInfo = ((MenuItem)sender).Tag as MenuInfo;
  274. IconfontWindow.Show(SvgToGeometry.GetIconfonts(), menuInfo);
  275. }
  276. private void Menu_SelectionChanged(object sender, SelectionChangedEventArgs e)
  277. {
  278. if (RunTimeStatus.IS_MENU_EDIT) return;
  279. if (appData.AppConfig.ItemSpradeAnimation)
  280. {
  281. //是否启用列表展开动画
  282. MainWindow.mainWindow.RightCard.WrapUFG.Visibility = Visibility.Collapsed;
  283. }
  284. //设置对应菜单的图标列表
  285. if (MenuListBox.SelectedIndex == -1)
  286. {
  287. //appData.AppConfig.SelectedMenuIcons = appData.MenuList[appData.MenuList.Count - 1].IconList;
  288. }
  289. else
  290. {
  291. if (appData.MenuList[MenuListBox.SelectedIndex].IsEncrypt)
  292. {
  293. appData.AppConfig.SelectedMenuIcons = null;
  294. RunTimeStatus.SHOW_MENU_PASSWORDBOX = true;
  295. MainWindow.mainWindow.RightCard.PDDialog.Title.Text = "输入密码";
  296. MainWindow.mainWindow.RightCard.PDDialog.type = PasswordType.INPUT;
  297. MainWindow.mainWindow.RightCard.PDDialog.Visibility = Visibility.Visible;
  298. }
  299. else
  300. {
  301. MainWindow.mainWindow.RightCard.PDDialog.Visibility = Visibility.Collapsed;
  302. appData.AppConfig.SelectedMenuIcons = appData.MenuList[MenuListBox.SelectedIndex].IconList;
  303. }
  304. }
  305. MainWindow.mainWindow.RightCard.WrapUFG.Visibility = Visibility.Visible;
  306. }
  307. /// <summary>
  308. /// 鼠标悬停切换菜单
  309. /// </summary>
  310. /// <param name="sender"></param>
  311. /// <param name="e"></param>
  312. private void Menu_MouseEnter(object sender, MouseEventArgs e)
  313. {
  314. if (appData.AppConfig.HoverMenu && !RunTimeStatus.IS_MENU_EDIT)
  315. {
  316. Thread t = new Thread(() =>
  317. {
  318. Thread.Sleep(200);
  319. this.Dispatcher.Invoke(() =>
  320. {
  321. ListBoxItem lbi = sender as ListBoxItem;
  322. if (lbi.IsMouseOver)
  323. {
  324. int index = MenuListBox.ItemContainerGenerator.IndexFromContainer(lbi);
  325. MenuListBox.SelectedIndex = index;
  326. }
  327. });
  328. });
  329. t.IsBackground = true;
  330. t.Start();
  331. }
  332. }
  333. /// <summary>
  334. /// 点击菜单后 隐藏搜索框
  335. /// </summary>
  336. /// <param name="sender"></param>
  337. /// <param name="e"></param>
  338. private void ListBoxItem_MouseDown(object sender, MouseButtonEventArgs e)
  339. {
  340. if (RunTimeStatus.SEARCH_BOX_SHOW)
  341. {
  342. MainWindow.mainWindow.HidedSearchBox();
  343. }
  344. ListBoxItem lbi = sender as ListBoxItem;
  345. MenuInfo mi = lbi.DataContext 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 ListBoxItemPanel_MouseDown(object sender, MouseButtonEventArgs e)
  355. //{
  356. // if (RunTimeStatus.SEARCH_BOX_SHOW)
  357. // {
  358. // MainWindow.mainWindow.HidedSearchBox();
  359. // }
  360. // MenuInfo mi = (sender as StackPanel).Tag as MenuInfo;
  361. // int index = MenuListBox.Items.IndexOf(mi);
  362. // MenuListBox.SelectedIndex = index;
  363. //}
  364. /// <summary>
  365. /// 隐藏搜索框
  366. /// </summary>
  367. /// <param name="sender"></param>
  368. /// <param name="e"></param>
  369. private void MyCard_MouseDown(object sender, MouseButtonEventArgs e)
  370. {
  371. if (RunTimeStatus.SEARCH_BOX_SHOW)
  372. {
  373. MainWindow.mainWindow.HidedSearchBox();
  374. }
  375. }
  376. private void Menu_MouseWheel(object sender, MouseWheelEventArgs e)
  377. {
  378. if (RunTimeStatus.IS_MENU_EDIT) return;
  379. if (e.Delta < 0)
  380. {
  381. int index = MenuListBox.SelectedIndex;
  382. if (index < MenuListBox.Items.Count - 1)
  383. {
  384. index ++;
  385. } else
  386. {
  387. index = 0;
  388. }
  389. MenuListBox.SelectedIndex = index;
  390. } else if (e.Delta > 0)
  391. {
  392. int index = MenuListBox.SelectedIndex;
  393. if (index > 0)
  394. {
  395. index --;
  396. }
  397. else
  398. {
  399. index = MenuListBox.Items.Count - 1;
  400. }
  401. MenuListBox.SelectedIndex = index;
  402. }
  403. }
  404. private void Menu_PreviewDragLeave(object sender, DragEventArgs e)
  405. {
  406. MyPoptip.IsOpen = false;
  407. }
  408. private void Menu_PreviewDragEnter(object sender, DragEventArgs e)
  409. {
  410. MenuInfo mi = (sender as ListBoxItem).DataContext as MenuInfo;
  411. MyPoptipContent.Text = "移动至:" + mi.MenuName;
  412. MyPoptip.VerticalOffset = 30;
  413. MyPoptip.IsOpen = true;
  414. }
  415. private void Menu_MouseLeave(object sender, MouseEventArgs e)
  416. {
  417. MyPoptip.IsOpen = false;
  418. }
  419. private void Menu_Drop(object sender, DragEventArgs e)
  420. {
  421. MyPoptip.IsOpen = false;
  422. MenuInfo mi = (sender as ListBoxItem).DataContext as MenuInfo;
  423. IconInfo iconInfo = (IconInfo)e.Data.GetData(typeof(IconInfo));
  424. appData.MenuList[MenuListBox.SelectedIndex].IconList.Remove(iconInfo);
  425. appData.MenuList[MenuListBox.Items.IndexOf(mi)].IconList.Add(iconInfo);
  426. }
  427. private void EncryptMenu(object sender, RoutedEventArgs e)
  428. {
  429. MenuInfo menuInfo = ((MenuItem)sender).Tag as MenuInfo;
  430. if (menuInfo.IsEncrypt)
  431. {
  432. MainWindow.mainWindow.RightCard.PDDialog.menuInfo = menuInfo;
  433. MainWindow.mainWindow.RightCard.PDDialog.Title.Text = "输入密码";
  434. MainWindow.mainWindow.RightCard.PDDialog.type = PasswordType.CANCEL;
  435. RunTimeStatus.SHOW_MENU_PASSWORDBOX = true;
  436. MainWindow.mainWindow.RightCard.PDDialog.Visibility = Visibility.Visible;
  437. //单独设置焦点
  438. MainWindow.mainWindow.RightCard.PDDialog.SetFocus();
  439. } else
  440. {
  441. if (string.IsNullOrEmpty(appData.AppConfig.MenuPassword))
  442. {
  443. MainWindow.mainWindow.RightCard.PDDialog.menuInfo = menuInfo;
  444. MainWindow.mainWindow.RightCard.PDDialog.Title.Text = "设置新密码";
  445. MainWindow.mainWindow.RightCard.PDDialog.type = PasswordType.CREATE;
  446. RunTimeStatus.SHOW_MENU_PASSWORDBOX = true;
  447. MainWindow.mainWindow.RightCard.PDDialog.Visibility = Visibility.Visible;
  448. }
  449. else
  450. {
  451. menuInfo.IsEncrypt = true;
  452. HandyControl.Controls.Growl.Success(menuInfo.MenuName + " 已加密!", "MainWindowGrowl");
  453. }
  454. }
  455. }
  456. private void AlterPassword(object sender, RoutedEventArgs e)
  457. {
  458. MainWindow.mainWindow.RightCard.PDDialog.Title.Text = "输入旧密码";
  459. MainWindow.mainWindow.RightCard.PDDialog.type = PasswordType.ALTER;
  460. MainWindow.mainWindow.RightCard.PDDialog.Visibility = Visibility.Visible;
  461. //单独设置焦点
  462. MainWindow.mainWindow.RightCard.PDDialog.SetFocus();
  463. }
  464. /// <summary>
  465. /// 右键点击进行处理
  466. /// </summary>
  467. /// <param name="sender"></param>
  468. /// <param name="e"></param>
  469. private void MyCard_PreviewMouseRightButtonDown(object sender, MouseButtonEventArgs e)
  470. {
  471. RunTimeStatus.SHOW_RIGHT_BTN_MENU = true;
  472. new Thread(() =>
  473. {
  474. Thread.Sleep(50);
  475. RunTimeStatus.SHOW_RIGHT_BTN_MENU = false;
  476. }).Start();
  477. //在没有设置密码的情况下不弹出修改密码菜单
  478. if (string.IsNullOrEmpty(appData.AppConfig.MenuPassword))
  479. {
  480. AlterPW1.Visibility = Visibility.Collapsed;
  481. } else
  482. {
  483. AlterPW1.Visibility = Visibility.Visible;
  484. }
  485. }
  486. private void ListBoxItem_PreviewMouseRightButtonDown(object sender, MouseButtonEventArgs e)
  487. {
  488. ListBoxItem lbi = sender as ListBoxItem;
  489. MenuInfo info = lbi.DataContext as MenuInfo;
  490. ItemCollection ics = lbi.ContextMenu.Items;
  491. foreach (object obj in ics)
  492. {
  493. MenuItem mi = (MenuItem)obj;
  494. if (mi.Header.Equals("修改密码"))
  495. {
  496. if (string.IsNullOrEmpty(appData.AppConfig.MenuPassword))
  497. {
  498. mi.Visibility = Visibility.Collapsed;
  499. }
  500. else
  501. {
  502. mi.Visibility = Visibility.Visible;
  503. }
  504. break;
  505. }
  506. if (mi.Header.Equals("加密此列表") || mi.Header.Equals("取消加密此列表"))
  507. {
  508. if (info.IsEncrypt)
  509. {
  510. mi.Header = "取消加密此列表";
  511. } else
  512. {
  513. mi.Header = "加密此列表";
  514. }
  515. }
  516. }
  517. }
  518. }
  519. }