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 { /// /// LeftCardControl.xaml 的交互逻辑 /// 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 _swap; public DelegateCommand SwapCommand { get { if (_swap == null) _swap = new DelegateCommand( (indexes) => { int fromS = indexes[0]; int to = indexes[1]; ObservableCollection 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; } } /// /// 当修改菜单元素可见时 设置原菜单为不可见 并且不可选中 /// 修改菜单元素不可见时 原菜单可见 并 选中 /// /// /// 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 /// /// 新建菜单 /// /// /// 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); } /// /// 重命名菜单 将textbox 设置为可见 /// /// /// private void RenameMenu(object sender, RoutedEventArgs e) { IS_EDIT = true; MenuInfo menuInfo = ((MenuItem)sender).Tag as MenuInfo; menuInfo.MenuEdit = (int)Visibility.Visible; } /// /// 删除菜单 /// /// /// 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; } /// /// 编辑菜单失焦或者敲下Enter键时保存修改后的菜单 /// /// /// 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; } } /// /// 当修改菜单元素可见时 设置全选并获得焦点 /// /// /// 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(); } } /// /// 修改菜单图标 /// /// /// 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; } /// /// 鼠标悬停切换菜单 /// /// /// 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(); } } /// /// 点击菜单后 隐藏搜索框 /// /// /// 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; } ///// ///// 点击菜单后 隐藏搜索框 ///// ///// ///// //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; //} /// /// 隐藏搜索框 /// /// /// 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); } } }