LeftCardControl.xaml.cs 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632
  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. try
  144. {
  145. ListBoxItem lbi = sender as ListBoxItem;
  146. SolidColorBrush fontColor = new SolidColorBrush(Colors.Black);
  147. lbi.MouseLeave -= Lbi_MouseLeave;
  148. lbi.Background = bac;
  149. lbi.Foreground = fontColor;
  150. }
  151. catch { }
  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. RunTimeStatus.IS_MENU_EDIT = true;
  184. MenuInfo menuInfo = ((MenuItem)sender).Tag as MenuInfo;
  185. menuInfo.MenuEdit = (int)Visibility.Visible;
  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. RunTimeStatus.IS_MENU_EDIT = false;
  250. //为了解决无法修改菜单的问题
  251. MainWindow.mainWindow.SearchBox.Focus();
  252. MenuListBox.SelectedIndex = menuSelectIndexTemp;
  253. }
  254. }
  255. /// <summary>
  256. /// 当修改菜单元素可见时 设置全选并获得焦点
  257. /// </summary>
  258. /// <param name="sender"></param>
  259. /// <param name="e"></param>
  260. private void MenuEditWhenVisibilityChanged(object sender, DependencyPropertyChangedEventArgs e)
  261. {
  262. TextBox box = sender as TextBox;
  263. MenuInfo mi = box.Tag as MenuInfo;
  264. if (box.Visibility == Visibility.Visible)
  265. {
  266. Keyboard.Focus(box);
  267. box.SelectAll();
  268. }
  269. }
  270. /// <summary>
  271. /// 修改菜单图标
  272. /// </summary>
  273. /// <param name="sender"></param>
  274. /// <param name="e"></param>
  275. private void EditMenuGeometry(object sender, RoutedEventArgs e)
  276. {
  277. MenuInfo menuInfo = ((MenuItem)sender).Tag as MenuInfo;
  278. IconfontWindow.Show(SvgToGeometry.GetIconfonts(), menuInfo);
  279. }
  280. private void Menu_SelectionChanged(object sender, SelectionChangedEventArgs e)
  281. {
  282. if (RunTimeStatus.IS_MENU_EDIT) return;
  283. if (appData.AppConfig.ItemSpradeAnimation)
  284. {
  285. //是否启用列表展开动画
  286. MainWindow.mainWindow.RightCard.WrapUFG.Visibility = Visibility.Collapsed;
  287. }
  288. //设置对应菜单的图标列表
  289. if (MenuListBox.SelectedIndex == -1)
  290. {
  291. //appData.AppConfig.SelectedMenuIcons = appData.MenuList[appData.MenuList.Count - 1].IconList;
  292. }
  293. else
  294. {
  295. if (appData.MenuList[MenuListBox.SelectedIndex].IsEncrypt)
  296. {
  297. appData.AppConfig.SelectedMenuIcons = null;
  298. RunTimeStatus.SHOW_MENU_PASSWORDBOX = true;
  299. MainWindow.mainWindow.RightCard.PDDialog.Title.Text = "输入密码";
  300. MainWindow.mainWindow.RightCard.PDDialog.type = PasswordType.INPUT;
  301. MainWindow.mainWindow.RightCard.PDDialog.Visibility = Visibility.Visible;
  302. }
  303. else
  304. {
  305. MainWindow.mainWindow.RightCard.PDDialog.Visibility = Visibility.Collapsed;
  306. appData.AppConfig.SelectedMenuIcons = appData.MenuList[MenuListBox.SelectedIndex].IconList;
  307. }
  308. }
  309. MainWindow.mainWindow.RightCard.WrapUFG.Visibility = Visibility.Visible;
  310. //App.DoEvents();
  311. }
  312. /// <summary>
  313. /// 鼠标悬停切换菜单
  314. /// </summary>
  315. /// <param name="sender"></param>
  316. /// <param name="e"></param>
  317. private void Menu_MouseEnter(object sender, MouseEventArgs e)
  318. {
  319. if (appData.AppConfig.HoverMenu && !RunTimeStatus.IS_MENU_EDIT)
  320. {
  321. Thread t = new Thread(() =>
  322. {
  323. Thread.Sleep(200);
  324. this.Dispatcher.Invoke(() =>
  325. {
  326. ListBoxItem lbi = sender as ListBoxItem;
  327. if (lbi.IsMouseOver)
  328. {
  329. int index = MenuListBox.ItemContainerGenerator.IndexFromContainer(lbi);
  330. MenuListBox.SelectedIndex = index;
  331. }
  332. });
  333. });
  334. t.IsBackground = true;
  335. t.Start();
  336. }
  337. }
  338. /// <summary>
  339. /// 点击菜单后 隐藏搜索框
  340. /// </summary>
  341. /// <param name="sender"></param>
  342. /// <param name="e"></param>
  343. private void ListBoxItem_MouseDown(object sender, MouseButtonEventArgs e)
  344. {
  345. if (RunTimeStatus.SEARCH_BOX_SHOW)
  346. {
  347. MainWindow.mainWindow.HidedSearchBox();
  348. }
  349. ListBoxItem lbi = sender as ListBoxItem;
  350. MenuInfo mi = lbi.DataContext as MenuInfo;
  351. int index = MenuListBox.Items.IndexOf(mi);
  352. MenuListBox.SelectedIndex = index;
  353. }
  354. ///// <summary>
  355. ///// 点击菜单后 隐藏搜索框
  356. ///// </summary>
  357. ///// <param name="sender"></param>
  358. ///// <param name="e"></param>
  359. //private void ListBoxItemPanel_MouseDown(object sender, MouseButtonEventArgs e)
  360. //{
  361. // if (RunTimeStatus.SEARCH_BOX_SHOW)
  362. // {
  363. // MainWindow.mainWindow.HidedSearchBox();
  364. // }
  365. // MenuInfo mi = (sender as StackPanel).Tag as MenuInfo;
  366. // int index = MenuListBox.Items.IndexOf(mi);
  367. // MenuListBox.SelectedIndex = index;
  368. //}
  369. /// <summary>
  370. /// 隐藏搜索框
  371. /// </summary>
  372. /// <param name="sender"></param>
  373. /// <param name="e"></param>
  374. private void MyCard_MouseDown(object sender, MouseButtonEventArgs e)
  375. {
  376. if (RunTimeStatus.SEARCH_BOX_SHOW)
  377. {
  378. MainWindow.mainWindow.HidedSearchBox();
  379. }
  380. }
  381. private void Menu_MouseWheel(object sender, MouseWheelEventArgs e)
  382. {
  383. if (RunTimeStatus.IS_MENU_EDIT) return;
  384. ScrollViewer scrollViewer = ScrollUtil.FindSimpleVisualChild<ScrollViewer>(MenuListBox);
  385. if (e.Delta < 0)
  386. {
  387. //判断是否到了最底部
  388. if (ScrollUtil.IsBootomScrollView(scrollViewer))
  389. {
  390. int index = MenuListBox.SelectedIndex;
  391. if (index < MenuListBox.Items.Count - 1)
  392. {
  393. index++;
  394. }
  395. else
  396. {
  397. index = 0;
  398. }
  399. MenuListBox.SelectedIndex = index;
  400. }
  401. }
  402. else if (e.Delta > 0)
  403. {
  404. if (ScrollUtil.IsTopScrollView(scrollViewer))
  405. {
  406. int index = MenuListBox.SelectedIndex;
  407. if (index > 0)
  408. {
  409. index--;
  410. }
  411. else
  412. {
  413. index = MenuListBox.Items.Count - 1;
  414. }
  415. MenuListBox.SelectedIndex = index;
  416. }
  417. }
  418. //滚动到选中项
  419. MenuListBox.ScrollIntoView(MenuListBox.SelectedItem);
  420. }
  421. private void Menu_PreviewDragLeave(object sender, DragEventArgs e)
  422. {
  423. MyPoptip.IsOpen = false;
  424. }
  425. private void Menu_PreviewDragEnter(object sender, DragEventArgs e)
  426. {
  427. MenuInfo mi = (sender as ListBoxItem).DataContext as MenuInfo;
  428. MyPoptipContent.Text = "移动至:" + mi.MenuName;
  429. MyPoptip.VerticalOffset = 30;
  430. MyPoptip.IsOpen = true;
  431. }
  432. private void Menu_MouseLeave(object sender, MouseEventArgs e)
  433. {
  434. MyPoptip.IsOpen = false;
  435. }
  436. /// <summary>
  437. /// 拖动移动图标到指定菜单
  438. /// </summary>
  439. /// <param name="sender"></param>
  440. /// <param name="e"></param>
  441. private void Menu_Drop(object sender, DragEventArgs e)
  442. {
  443. MyPoptip.IsOpen = false;
  444. MenuInfo mi = (sender as ListBoxItem).DataContext as MenuInfo;
  445. IconInfo iconInfo = (IconInfo)e.Data.GetData(typeof(IconInfo));
  446. if (iconInfo != null)
  447. {
  448. // 将已有图标移动到该菜单
  449. appData.MenuList[MenuListBox.SelectedIndex].IconList.Remove(iconInfo);
  450. appData.MenuList[MenuListBox.Items.IndexOf(mi)].IconList.Add(iconInfo);
  451. }
  452. else
  453. {
  454. // 直接将新图标移动到该菜单
  455. Array dropObject = (System.Array)e.Data.GetData(DataFormats.FileDrop);
  456. if (dropObject == null) return;
  457. foreach (object obj in dropObject)
  458. {
  459. string path = (string)obj;
  460. iconInfo = CommonCode.GetIconInfoByPath(path);
  461. if (iconInfo == null)
  462. {
  463. LogUtil.WriteErrorLog("添加项目失败,未能获取到项目图标:" + path);
  464. break;
  465. }
  466. appData.MenuList[MenuListBox.Items.IndexOf(mi)].IconList.Add(iconInfo);
  467. }
  468. CommonCode.SortIconList();
  469. CommonCode.SaveAppData(MainWindow.appData, Constants.DATA_FILE_PATH);
  470. }
  471. }
  472. private void EncryptMenu(object sender, RoutedEventArgs e)
  473. {
  474. MenuInfo menuInfo = ((MenuItem)sender).Tag as MenuInfo;
  475. if (menuInfo.IsEncrypt)
  476. {
  477. MainWindow.mainWindow.RightCard.PDDialog.menuInfo = menuInfo;
  478. MainWindow.mainWindow.RightCard.PDDialog.Title.Text = "输入密码";
  479. MainWindow.mainWindow.RightCard.PDDialog.type = PasswordType.CANCEL;
  480. RunTimeStatus.SHOW_MENU_PASSWORDBOX = true;
  481. MainWindow.mainWindow.RightCard.PDDialog.Visibility = Visibility.Visible;
  482. //单独设置焦点
  483. MainWindow.mainWindow.RightCard.PDDialog.SetFocus();
  484. }
  485. else
  486. {
  487. if (string.IsNullOrEmpty(appData.AppConfig.MenuPassword))
  488. {
  489. MainWindow.mainWindow.RightCard.PDDialog.menuInfo = menuInfo;
  490. MainWindow.mainWindow.RightCard.PDDialog.Title.Text = "设置新密码";
  491. MainWindow.mainWindow.RightCard.PDDialog.type = PasswordType.CREATE;
  492. RunTimeStatus.SHOW_MENU_PASSWORDBOX = true;
  493. MainWindow.mainWindow.RightCard.PDDialog.Visibility = Visibility.Visible;
  494. }
  495. else
  496. {
  497. menuInfo.IsEncrypt = true;
  498. HandyControl.Controls.Growl.Success(menuInfo.MenuName + " 已加密!", "MainWindowGrowl");
  499. }
  500. }
  501. }
  502. private void AlterPassword(object sender, RoutedEventArgs e)
  503. {
  504. MainWindow.mainWindow.RightCard.PDDialog.Title.Text = "输入旧密码";
  505. MainWindow.mainWindow.RightCard.PDDialog.type = PasswordType.ALTER;
  506. MainWindow.mainWindow.RightCard.PDDialog.Visibility = Visibility.Visible;
  507. //单独设置焦点
  508. MainWindow.mainWindow.RightCard.PDDialog.SetFocus();
  509. }
  510. /// <summary>
  511. /// 右键点击进行处理
  512. /// </summary>
  513. /// <param name="sender"></param>
  514. /// <param name="e"></param>
  515. private void MyCard_PreviewMouseRightButtonDown(object sender, MouseButtonEventArgs e)
  516. {
  517. RunTimeStatus.SHOW_RIGHT_BTN_MENU = true;
  518. new Thread(() =>
  519. {
  520. Thread.Sleep(50);
  521. RunTimeStatus.SHOW_RIGHT_BTN_MENU = false;
  522. }).Start();
  523. //在没有设置密码的情况下不弹出修改密码菜单
  524. if (string.IsNullOrEmpty(appData.AppConfig.MenuPassword))
  525. {
  526. AlterPW1.Visibility = Visibility.Collapsed;
  527. }
  528. else
  529. {
  530. AlterPW1.Visibility = Visibility.Visible;
  531. }
  532. }
  533. private void ListBoxItem_PreviewMouseRightButtonDown(object sender, MouseButtonEventArgs e)
  534. {
  535. ListBoxItem lbi = sender as ListBoxItem;
  536. MenuInfo info = lbi.DataContext as MenuInfo;
  537. ItemCollection ics = lbi.ContextMenu.Items;
  538. foreach (object obj in ics)
  539. {
  540. MenuItem mi = (MenuItem)obj;
  541. if (mi.Header.Equals("修改密码"))
  542. {
  543. if (string.IsNullOrEmpty(appData.AppConfig.MenuPassword))
  544. {
  545. mi.Visibility = Visibility.Collapsed;
  546. }
  547. else
  548. {
  549. mi.Visibility = Visibility.Visible;
  550. }
  551. break;
  552. }
  553. if (mi.Header.Equals("加密此列表") || mi.Header.Equals("取消加密此列表"))
  554. {
  555. if (info.IsEncrypt)
  556. {
  557. mi.Header = "取消加密此列表";
  558. }
  559. else
  560. {
  561. mi.Header = "加密此列表";
  562. }
  563. }
  564. }
  565. }
  566. }
  567. }