123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465 |
- using DraggAnimatedPanelExample;
- using GeekDesk.Constant;
- using GeekDesk.Control.Windows;
- using GeekDesk.Util;
- using GeekDesk.ViewModel;
- using System;
- using System.Collections.ObjectModel;
- using System.Threading;
- using System.Windows;
- using System.Windows.Controls;
- using System.Windows.Input;
- using System.Windows.Media;
- namespace GeekDesk.Control.UserControls.PannelCard
- {
- /// <summary>
- /// LeftCardControl.xaml 的交互逻辑
- /// </summary>
- public partial class LeftCardControl : UserControl
- {
- private int menuSelectIndexTemp = -1;
- private AppData appData = MainWindow.appData;
- private SolidColorBrush bac = new SolidColorBrush(Color.FromRgb(236, 236, 236));
- //是否正在修改菜单
- public bool IS_EDIT = false;
- public LeftCardControl()
- {
- InitializeComponent();
- this.Loaded += (s, e) =>
- {
- SelectLastMenu();
- SetMenuListBoxItemEvent();
- };
- }
- private void SetMenuListBoxItemEvent()
- {
- int size = MenuListBox.Items.Count;
- for (int i = 0; i < size; i++)
- {
- ListBoxItem lbi = (ListBoxItem)(MenuListBox.ItemContainerGenerator.ContainerFromIndex(i));
- if (lbi != null)
- {
- SetListBoxItemEvent(lbi);
- }
- }
- //首次触发不了Selected事件
- object obj = MenuListBox.ItemContainerGenerator.ContainerFromIndex(MenuListBox.SelectedIndex);
- Lbi_Selected(obj, null);
- }
- private void SetListBoxItemEvent(ListBoxItem lbi)
- {
- lbi.MouseEnter += (s, me) =>
- {
- lbi.Background = bac;
- };
- lbi.Unselected += Lbi_Unselected;
- lbi.MouseLeave += Lbi_MouseLeave;
- lbi.Selected += Lbi_Selected;
- }
- private void SelectLastMenu()
- {
- if (appData.AppConfig.SelectedMenuIndex >= appData.MenuList.Count || appData.AppConfig.SelectedMenuIndex == -1)
- {
- MenuListBox.SelectedIndex = 0;
- appData.AppConfig.SelectedMenuIndex = MenuListBox.SelectedIndex;
- appData.AppConfig.SelectedMenuIcons = appData.MenuList[0].IconList;
- }
- else
- {
- MenuListBox.SelectedIndex = appData.AppConfig.SelectedMenuIndex;
- appData.AppConfig.SelectedMenuIcons = appData.MenuList[appData.AppConfig.SelectedMenuIndex].IconList;
- }
- }
- DelegateCommand<int[]> _swap;
- public DelegateCommand<int[]> SwapCommand
- {
- get
- {
- if (_swap == null)
- _swap = new DelegateCommand<int[]>(
- (indexes) =>
- {
- int fromS = indexes[0];
- int to = indexes[1];
- ObservableCollection<MenuInfo> menuList = MainWindow.appData.MenuList;
- var elementSource = menuList[to];
- var dragged = menuList[fromS];
- menuList.Remove(dragged);
- menuList.Insert(to, dragged);
- MenuListBox.SelectedIndex = to;
- MainWindow.appData.MenuList = menuList;
- }
- );
- return _swap;
- }
- }
- /// <summary>
- /// 当修改菜单元素可见时 设置原菜单为不可见 并且不可选中
- /// 修改菜单元素不可见时 原菜单可见 并 选中
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- private void MenuWhenVisibilityChanged(object sender, DependencyPropertyChangedEventArgs e)
- {
- StackPanel sp = sender as StackPanel;
- ListBoxItem lbi = (sp.TemplatedParent as ContentPresenter).TemplatedParent as ListBoxItem;
- if (sp.Visibility == Visibility.Collapsed)
- {
- lbi.MouseEnter += Lbi_MouseEnter;
- if (MenuListBox.SelectedIndex != -1)
- {
- menuSelectIndexTemp = MenuListBox.SelectedIndex;
- MenuListBox.SelectedIndex = -1;
- }
- else
- {
- MenuListBox.SelectedIndex = menuSelectIndexTemp;
- }
- }
- else
- {
- lbi.MouseEnter += (s, me) =>
- {
- lbi.Background = bac;
- };
- lbi.MouseLeave += Lbi_MouseLeave;
- lbi.Selected += Lbi_Selected;
- }
- }
- #region 设置菜单触发事件
- private void Lbi_MouseEnter(object sender, MouseEventArgs e)
- {
- ListBoxItem lbi = sender as ListBoxItem;
- lbi.Background = Brushes.Transparent;
- }
- private void Lbi_Unselected(object sender, RoutedEventArgs e)
- {
- //添加Leave效果
- ListBoxItem lbi = sender as ListBoxItem;
- lbi.Background = Brushes.Transparent;
- lbi.MouseLeave += Lbi_MouseLeave;
- }
- private void Lbi_Selected(object sender, RoutedEventArgs e)
- {
- ListBoxItem lbi = sender as ListBoxItem;
- SolidColorBrush fontColor = new SolidColorBrush(Colors.Black);
- lbi.MouseLeave -= Lbi_MouseLeave;
- lbi.Background = bac;
- lbi.Foreground = fontColor;
- }
- private void Lbi_MouseLeave(object sender, MouseEventArgs e)
- {
- ListBoxItem lbi = sender as ListBoxItem;
- lbi.Background = Brushes.Transparent;
- }
- #endregion
- /// <summary>
- /// 新建菜单
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- private void CreateMenu(object sender, RoutedEventArgs e)
- {
- MenuInfo info = new MenuInfo() { MenuEdit = Visibility.Collapsed, MenuId = System.Guid.NewGuid().ToString(), MenuName = "NewMenu" };
- appData.MenuList.Add(info);
- MenuListBox.SelectedIndex = appData.MenuList.Count - 1;
- appData.AppConfig.SelectedMenuIndex = MenuListBox.SelectedIndex;
- appData.AppConfig.SelectedMenuIcons = info.IconList;
- //首次触发不了Selected事件
- object obj = MenuListBox.ItemContainerGenerator.ContainerFromIndex(MenuListBox.SelectedIndex);
- SetListBoxItemEvent((ListBoxItem)obj);
- Lbi_Selected(obj, null);
- }
- /// <summary>
- /// 重命名菜单 将textbox 设置为可见
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- private void RenameMenu(object sender, RoutedEventArgs e)
- {
- IS_EDIT = true;
- MenuInfo menuInfo = ((MenuItem)sender).Tag as MenuInfo;
- menuInfo.MenuEdit = (int)Visibility.Visible;
- }
- /// <summary>
- /// 删除菜单
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- private void DeleteMenu(object sender, RoutedEventArgs e)
- {
- MenuInfo menuInfo = ((MenuItem)sender).Tag as MenuInfo;
- if (appData.MenuList.Count == 1)
- {
- //如果删除以后没有菜单的话 先创建一个
- CreateMenu(null, null);
- }
- int index = appData.MenuList.IndexOf(menuInfo);
- if (index == 0)
- {
- index = 0;
- }
- else
- {
- index--;
- }
- appData.MenuList.Remove(menuInfo);
- // 选中下一个菜单
- MenuListBox.SelectedIndex = index;
- appData.AppConfig.SelectedMenuIndex = MenuListBox.SelectedIndex;
- appData.AppConfig.SelectedMenuIcons = appData.MenuList[index].IconList;
- }
- /// <summary>
- /// 编辑菜单失焦或者敲下Enter键时保存修改后的菜单
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- private void LostFocusOrEnterDown(object sender, EventArgs e)
- {
- bool done = true;
- TextBox menuBox = null;
- if (e.GetType() == typeof(KeyEventArgs))
- {
- KeyEventArgs eKey = e as KeyEventArgs;
- if (eKey.Key == Key.Enter)
- {
- menuBox = ((TextBox)sender);
- }
- else
- {
- done = false;
- }
- }
- else if (e.GetType() == typeof(RoutedEventArgs))
- {
- menuBox = ((TextBox)sender);
- }
- if (done)
- {
- if (menuBox != null)
- {
- MenuInfo menuInfo = menuBox.Tag as MenuInfo;
- string text = menuBox.Text;
- menuInfo.MenuName = text;
- menuInfo.MenuEdit = Visibility.Collapsed;
- }
- IS_EDIT = false;
- //为了解决无法修改菜单的问题
- MainWindow.mainWindow.SearchBox.Focus();
- MenuListBox.SelectedIndex = menuSelectIndexTemp;
- }
- }
- /// <summary>
- /// 当修改菜单元素可见时 设置全选并获得焦点
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- private void MenuEditWhenVisibilityChanged(object sender, DependencyPropertyChangedEventArgs e)
- {
- TextBox box = sender as TextBox;
- MenuInfo mi = box.Tag as MenuInfo;
- if (box.Visibility == Visibility.Visible)
- {
- Keyboard.Focus(box);
- box.SelectAll();
- }
- }
- /// <summary>
- /// 修改菜单图标
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- private void EditMenuGeometry(object sender, RoutedEventArgs e)
- {
- MenuInfo menuInfo = ((MenuItem)sender).Tag as MenuInfo;
- IconfontWindow.Show(SvgToGeometry.GetIconfonts(), menuInfo);
- }
- private void Menu_SelectionChanged(object sender, SelectionChangedEventArgs e)
- {
- if (IS_EDIT) return;
- MainWindow.mainWindow.RightCard.WrapCard.Visibility = Visibility.Collapsed;
- //设置对应菜单的图标列表
- if (MenuListBox.SelectedIndex == -1)
- {
- //appData.AppConfig.SelectedMenuIcons = appData.MenuList[appData.MenuList.Count - 1].IconList;
- }
- else
- {
- appData.AppConfig.SelectedMenuIcons = appData.MenuList[MenuListBox.SelectedIndex].IconList;
- }
- MainWindow.mainWindow.RightCard.WrapCard.Visibility = Visibility.Visible;
- }
- /// <summary>
- /// 鼠标悬停切换菜单
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- private void Menu_MouseEnter(object sender, MouseEventArgs e)
- {
- if (appData.AppConfig.HoverMenu && !IS_EDIT)
- {
- Thread t = new Thread(() =>
- {
- Thread.Sleep(200);
- this.Dispatcher.Invoke(() =>
- {
- ListBoxItem lbi = sender as ListBoxItem;
- if (lbi.IsMouseOver)
- {
- int index = MenuListBox.ItemContainerGenerator.IndexFromContainer(lbi);
- MenuListBox.SelectedIndex = index;
- }
- });
- });
- t.IsBackground = true;
- t.Start();
- }
- }
- /// <summary>
- /// 点击菜单后 隐藏搜索框
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- private void ListBoxItem_MouseDown(object sender, MouseButtonEventArgs e)
- {
- if (RunTimeStatus.SEARCH_BOX_SHOW)
- {
- MainWindow.mainWindow.HidedSearchBox();
- }
- ListBoxItem lbi = sender as ListBoxItem;
- MenuInfo mi = lbi.DataContext as MenuInfo;
- int index = MenuListBox.Items.IndexOf(mi);
- MenuListBox.SelectedIndex = index;
- }
- ///// <summary>
- ///// 点击菜单后 隐藏搜索框
- ///// </summary>
- ///// <param name="sender"></param>
- ///// <param name="e"></param>
- //private void ListBoxItemPanel_MouseDown(object sender, MouseButtonEventArgs e)
- //{
- // if (RunTimeStatus.SEARCH_BOX_SHOW)
- // {
- // MainWindow.mainWindow.HidedSearchBox();
- // }
- // MenuInfo mi = (sender as StackPanel).Tag as MenuInfo;
- // int index = MenuListBox.Items.IndexOf(mi);
- // MenuListBox.SelectedIndex = index;
- //}
- /// <summary>
- /// 隐藏搜索框
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- private void MyCard_MouseDown(object sender, MouseButtonEventArgs e)
- {
- if (RunTimeStatus.SEARCH_BOX_SHOW)
- {
- MainWindow.mainWindow.HidedSearchBox();
- }
- }
- private void Menu_MouseWheel(object sender, MouseWheelEventArgs e)
- {
- if (e.Delta < 0)
- {
- int index = MenuListBox.SelectedIndex;
- if (index < MenuListBox.Items.Count - 1)
- {
- index ++;
- } else
- {
- index = 0;
- }
- MenuListBox.SelectedIndex = index;
- } else if (e.Delta > 0)
- {
- int index = MenuListBox.SelectedIndex;
- if (index > 0)
- {
- index --;
- }
- else
- {
- index = MenuListBox.Items.Count - 1;
- }
- MenuListBox.SelectedIndex = index;
- }
- }
-
- private void Menu_PreviewDragLeave(object sender, DragEventArgs e)
- {
- MyPoptip.IsOpen = false;
- }
- private void Menu_PreviewDragEnter(object sender, DragEventArgs e)
- {
- MenuInfo mi = (sender as ListBoxItem).DataContext as MenuInfo;
- MyPoptipContent.Text = "移动至:" + mi.MenuName;
- MyPoptip.VerticalOffset = 30;
- MyPoptip.IsOpen = true;
- }
- private void Menu_MouseLeave(object sender, MouseEventArgs e)
- {
- MyPoptip.IsOpen = false;
- }
- private void Menu_Drop(object sender, DragEventArgs e)
- {
- MyPoptip.IsOpen = false;
- MenuInfo mi = (sender as ListBoxItem).DataContext as MenuInfo;
- IconInfo iconInfo = (IconInfo)e.Data.GetData(typeof(IconInfo));
- appData.MenuList[MenuListBox.SelectedIndex].IconList.Remove(iconInfo);
- appData.MenuList[MenuListBox.Items.IndexOf(mi)].IconList.Add(iconInfo);
- }
- }
- }
|