LeftCardControl.xaml.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377
  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. private static 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. MenuInfo menuInfo = ((MenuItem)sender).Tag as MenuInfo;
  181. menuInfo.MenuEdit = (int)Visibility.Visible;
  182. IS_EDIT = true;
  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. MenuListBox.SelectedIndex = menuSelectIndexTemp;
  248. }
  249. }
  250. /// <summary>
  251. /// 当修改菜单元素可见时 设置全选并获得焦点
  252. /// </summary>
  253. /// <param name="sender"></param>
  254. /// <param name="e"></param>
  255. private void MenuEditWhenVisibilityChanged(object sender, DependencyPropertyChangedEventArgs e)
  256. {
  257. TextBox box = sender as TextBox;
  258. MenuInfo mi = box.Tag as MenuInfo;
  259. if (box.Visibility == Visibility.Visible)
  260. {
  261. Keyboard.Focus(box);
  262. box.SelectAll();
  263. }
  264. }
  265. /// <summary>
  266. /// 修改菜单图标
  267. /// </summary>
  268. /// <param name="sender"></param>
  269. /// <param name="e"></param>
  270. private void EditMenuGeometry(object sender, RoutedEventArgs e)
  271. {
  272. MenuInfo menuInfo = ((MenuItem)sender).Tag as MenuInfo;
  273. IconfontWindow.Show(SvgToGeometry.GetIconfonts(), menuInfo);
  274. }
  275. private void Menu_SelectionChanged(object sender, SelectionChangedEventArgs e)
  276. {
  277. //设置对应菜单的图标列表
  278. if (MenuListBox.SelectedIndex == -1)
  279. {
  280. //appData.AppConfig.SelectedMenuIcons = appData.MenuList[appData.MenuList.Count - 1].IconList;
  281. }
  282. else
  283. {
  284. appData.AppConfig.SelectedMenuIcons = appData.MenuList[MenuListBox.SelectedIndex].IconList;
  285. }
  286. }
  287. /// <summary>
  288. /// 鼠标悬停切换菜单
  289. /// </summary>
  290. /// <param name="sender"></param>
  291. /// <param name="e"></param>
  292. private void Menu_MouseEnter(object sender, MouseEventArgs e)
  293. {
  294. if (appData.AppConfig.HoverMenu && !IS_EDIT)
  295. {
  296. Thread t = new Thread(() =>
  297. {
  298. Thread.Sleep(200);
  299. this.Dispatcher.Invoke(() =>
  300. {
  301. ListBoxItem lbi = sender as ListBoxItem;
  302. if (lbi.IsMouseOver)
  303. {
  304. int index = MenuListBox.ItemContainerGenerator.IndexFromContainer(lbi);
  305. MenuListBox.SelectedIndex = index;
  306. }
  307. });
  308. });
  309. t.IsBackground = true;
  310. t.Start();
  311. }
  312. }
  313. /// <summary>
  314. /// 点击菜单后 隐藏搜索框
  315. /// </summary>
  316. /// <param name="sender"></param>
  317. /// <param name="e"></param>
  318. private void ListBoxItem_MouseDown(object sender, MouseButtonEventArgs e)
  319. {
  320. if (RunTimeStatus.SEARCH_BOX_SHOW)
  321. {
  322. MainWindow.mainWindow.HidedSearchBox();
  323. }
  324. }
  325. /// <summary>
  326. /// 隐藏搜索框
  327. /// </summary>
  328. /// <param name="sender"></param>
  329. /// <param name="e"></param>
  330. private void MyCard_MouseDown(object sender, MouseButtonEventArgs e)
  331. {
  332. if (RunTimeStatus.SEARCH_BOX_SHOW)
  333. {
  334. MainWindow.mainWindow.HidedSearchBox();
  335. }
  336. }
  337. }
  338. }