LeftCardControl.xaml.cs 20 KB

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