LeftCardControl.xaml.cs 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578
  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.Visibility = Visibility.Visible;
  297. }
  298. else
  299. {
  300. MainWindow.mainWindow.RightCard.PDDialog.Visibility = Visibility.Collapsed;
  301. appData.AppConfig.SelectedMenuIcons = appData.MenuList[MenuListBox.SelectedIndex].IconList;
  302. }
  303. }
  304. MainWindow.mainWindow.RightCard.WrapUFG.Visibility = Visibility.Visible;
  305. }
  306. /// <summary>
  307. /// 鼠标悬停切换菜单
  308. /// </summary>
  309. /// <param name="sender"></param>
  310. /// <param name="e"></param>
  311. private void Menu_MouseEnter(object sender, MouseEventArgs e)
  312. {
  313. if (appData.AppConfig.HoverMenu && !RunTimeStatus.IS_MENU_EDIT)
  314. {
  315. Thread t = new Thread(() =>
  316. {
  317. Thread.Sleep(200);
  318. this.Dispatcher.Invoke(() =>
  319. {
  320. ListBoxItem lbi = sender as ListBoxItem;
  321. if (lbi.IsMouseOver)
  322. {
  323. int index = MenuListBox.ItemContainerGenerator.IndexFromContainer(lbi);
  324. MenuListBox.SelectedIndex = index;
  325. }
  326. });
  327. });
  328. t.IsBackground = true;
  329. t.Start();
  330. }
  331. }
  332. /// <summary>
  333. /// 点击菜单后 隐藏搜索框
  334. /// </summary>
  335. /// <param name="sender"></param>
  336. /// <param name="e"></param>
  337. private void ListBoxItem_MouseDown(object sender, MouseButtonEventArgs e)
  338. {
  339. if (RunTimeStatus.SEARCH_BOX_SHOW)
  340. {
  341. MainWindow.mainWindow.HidedSearchBox();
  342. }
  343. ListBoxItem lbi = sender as ListBoxItem;
  344. MenuInfo mi = lbi.DataContext as MenuInfo;
  345. int index = MenuListBox.Items.IndexOf(mi);
  346. MenuListBox.SelectedIndex = index;
  347. }
  348. ///// <summary>
  349. ///// 点击菜单后 隐藏搜索框
  350. ///// </summary>
  351. ///// <param name="sender"></param>
  352. ///// <param name="e"></param>
  353. //private void ListBoxItemPanel_MouseDown(object sender, MouseButtonEventArgs e)
  354. //{
  355. // if (RunTimeStatus.SEARCH_BOX_SHOW)
  356. // {
  357. // MainWindow.mainWindow.HidedSearchBox();
  358. // }
  359. // MenuInfo mi = (sender as StackPanel).Tag as MenuInfo;
  360. // int index = MenuListBox.Items.IndexOf(mi);
  361. // MenuListBox.SelectedIndex = index;
  362. //}
  363. /// <summary>
  364. /// 隐藏搜索框
  365. /// </summary>
  366. /// <param name="sender"></param>
  367. /// <param name="e"></param>
  368. private void MyCard_MouseDown(object sender, MouseButtonEventArgs e)
  369. {
  370. if (RunTimeStatus.SEARCH_BOX_SHOW)
  371. {
  372. MainWindow.mainWindow.HidedSearchBox();
  373. }
  374. }
  375. private void Menu_MouseWheel(object sender, MouseWheelEventArgs e)
  376. {
  377. if (RunTimeStatus.IS_MENU_EDIT) return;
  378. if (e.Delta < 0)
  379. {
  380. int index = MenuListBox.SelectedIndex;
  381. if (index < MenuListBox.Items.Count - 1)
  382. {
  383. index ++;
  384. } else
  385. {
  386. index = 0;
  387. }
  388. MenuListBox.SelectedIndex = index;
  389. } else if (e.Delta > 0)
  390. {
  391. int index = MenuListBox.SelectedIndex;
  392. if (index > 0)
  393. {
  394. index --;
  395. }
  396. else
  397. {
  398. index = MenuListBox.Items.Count - 1;
  399. }
  400. MenuListBox.SelectedIndex = index;
  401. }
  402. }
  403. private void Menu_PreviewDragLeave(object sender, DragEventArgs e)
  404. {
  405. MyPoptip.IsOpen = false;
  406. }
  407. private void Menu_PreviewDragEnter(object sender, DragEventArgs e)
  408. {
  409. MenuInfo mi = (sender as ListBoxItem).DataContext as MenuInfo;
  410. MyPoptipContent.Text = "移动至:" + mi.MenuName;
  411. MyPoptip.VerticalOffset = 30;
  412. MyPoptip.IsOpen = true;
  413. }
  414. private void Menu_MouseLeave(object sender, MouseEventArgs e)
  415. {
  416. MyPoptip.IsOpen = false;
  417. }
  418. private void Menu_Drop(object sender, DragEventArgs e)
  419. {
  420. MyPoptip.IsOpen = false;
  421. MenuInfo mi = (sender as ListBoxItem).DataContext as MenuInfo;
  422. IconInfo iconInfo = (IconInfo)e.Data.GetData(typeof(IconInfo));
  423. appData.MenuList[MenuListBox.SelectedIndex].IconList.Remove(iconInfo);
  424. appData.MenuList[MenuListBox.Items.IndexOf(mi)].IconList.Add(iconInfo);
  425. }
  426. private void EncryptMenu(object sender, RoutedEventArgs e)
  427. {
  428. MenuInfo menuInfo = ((MenuItem)sender).Tag as MenuInfo;
  429. if (menuInfo.IsEncrypt)
  430. {
  431. MainWindow.mainWindow.RightCard.PDDialog.menuInfo = menuInfo;
  432. MainWindow.mainWindow.RightCard.PDDialog.Title.Text = "输入密码";
  433. MainWindow.mainWindow.RightCard.PDDialog.type = PasswordType.CANCEL;
  434. RunTimeStatus.SHOW_MENU_PASSWORDBOX = true;
  435. MainWindow.mainWindow.RightCard.PDDialog.Visibility = Visibility.Visible;
  436. //单独设置焦点
  437. MainWindow.mainWindow.RightCard.PDDialog.SetFocus();
  438. } else
  439. {
  440. if (string.IsNullOrEmpty(appData.AppConfig.MenuPassword))
  441. {
  442. MainWindow.mainWindow.RightCard.PDDialog.menuInfo = menuInfo;
  443. MainWindow.mainWindow.RightCard.PDDialog.Title.Text = "设置新密码";
  444. MainWindow.mainWindow.RightCard.PDDialog.type = PasswordType.CREATE;
  445. RunTimeStatus.SHOW_MENU_PASSWORDBOX = true;
  446. MainWindow.mainWindow.RightCard.PDDialog.Visibility = Visibility.Visible;
  447. }
  448. else
  449. {
  450. menuInfo.IsEncrypt = true;
  451. HandyControl.Controls.Growl.Success(menuInfo.MenuName + " 已加密!", "MainWindowGrowl");
  452. }
  453. }
  454. }
  455. private void AlterPassword(object sender, RoutedEventArgs e)
  456. {
  457. MainWindow.mainWindow.RightCard.PDDialog.Title.Text = "输入旧密码";
  458. MainWindow.mainWindow.RightCard.PDDialog.type = PasswordType.ALTER;
  459. MainWindow.mainWindow.RightCard.PDDialog.Visibility = Visibility.Visible;
  460. //单独设置焦点
  461. MainWindow.mainWindow.RightCard.PDDialog.SetFocus();
  462. }
  463. /// <summary>
  464. /// 右键点击进行处理
  465. /// </summary>
  466. /// <param name="sender"></param>
  467. /// <param name="e"></param>
  468. private void MyCard_PreviewMouseRightButtonDown(object sender, MouseButtonEventArgs e)
  469. {
  470. RunTimeStatus.SHOW_RIGHT_BTN_MENU = true;
  471. new Thread(() =>
  472. {
  473. Thread.Sleep(50);
  474. RunTimeStatus.SHOW_RIGHT_BTN_MENU = false;
  475. }).Start();
  476. //在没有设置密码的情况下不弹出修改密码菜单
  477. if (string.IsNullOrEmpty(appData.AppConfig.MenuPassword))
  478. {
  479. AlterPW1.Visibility = Visibility.Collapsed;
  480. } else
  481. {
  482. AlterPW1.Visibility = Visibility.Visible;
  483. }
  484. }
  485. private void ListBoxItem_PreviewMouseRightButtonDown(object sender, MouseButtonEventArgs e)
  486. {
  487. ListBoxItem lbi = sender as ListBoxItem;
  488. MenuInfo info = lbi.DataContext as MenuInfo;
  489. ItemCollection ics = lbi.ContextMenu.Items;
  490. foreach (object obj in ics)
  491. {
  492. MenuItem mi = (MenuItem)obj;
  493. if (mi.Header.Equals("修改密码"))
  494. {
  495. if (string.IsNullOrEmpty(appData.AppConfig.MenuPassword))
  496. {
  497. mi.Visibility = Visibility.Collapsed;
  498. }
  499. else
  500. {
  501. mi.Visibility = Visibility.Visible;
  502. }
  503. break;
  504. }
  505. if (mi.Header.Equals("加密此列表") || mi.Header.Equals("取消加密此列表"))
  506. {
  507. if (info.IsEncrypt)
  508. {
  509. mi.Header = "取消加密此列表";
  510. } else
  511. {
  512. mi.Header = "加密此列表";
  513. }
  514. }
  515. }
  516. }
  517. }
  518. }