优化修改菜单图标时的样式及逻辑

This commit is contained in:
liufei
2022-01-26 10:20:36 +08:00
parent 0495b8a121
commit 5a89b40244
5 changed files with 164 additions and 63 deletions

View File

@@ -11,6 +11,7 @@ using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Threading;
namespace GeekDesk.Control.UserControls.PannelCard
@@ -23,6 +24,9 @@ namespace GeekDesk.Control.UserControls.PannelCard
private int menuSelectIndexTemp = -1;
private AppData appData = MainWindow.appData;
//是否正在修改菜单
private static bool IS_EDIT = false;
public LeftCardControl()
{
InitializeComponent();
@@ -77,9 +81,14 @@ namespace GeekDesk.Control.UserControls.PannelCard
/// <param name="e"></param>
private void MenuWhenVisibilityChanged(object sender, DependencyPropertyChangedEventArgs e)
{
TextBlock tb = sender as TextBlock;
if (tb.Visibility == Visibility.Collapsed)
StackPanel sp = sender as StackPanel;
ListBoxItem lbi = (sp.TemplatedParent as ContentPresenter).TemplatedParent as ListBoxItem;
if (sp.Visibility == Visibility.Collapsed)
{
SolidColorBrush scb = new SolidColorBrush(Colors.Red);
lbi.MouseEnter += Lbi_MouseEnter;
if (MenuListBox.SelectedIndex != -1)
{
menuSelectIndexTemp = MenuListBox.SelectedIndex;
@@ -89,9 +98,33 @@ namespace GeekDesk.Control.UserControls.PannelCard
{
MenuListBox.SelectedIndex = menuSelectIndexTemp;
}
} else
{
SolidColorBrush bac = new SolidColorBrush(Color.FromRgb(236, 236, 236));
SolidColorBrush fontColor = new SolidColorBrush(Colors.Black);
lbi.MouseEnter += (s, me) =>
{
lbi.Background = bac;
};
lbi.MouseLeave += Lbi_MouseLeave;
lbi.Selected += (s, me) =>
{
lbi.MouseLeave -= Lbi_MouseLeave;
lbi.Background = bac;
lbi.Foreground = fontColor;
};
}
}
private void Lbi_MouseEnter(object sender, MouseEventArgs e)
{
ListBoxItem lbi = sender as ListBoxItem;
lbi.Background = Brushes.Transparent;
}
/// <summary>
/// 新建菜单
/// </summary>
@@ -105,6 +138,27 @@ namespace GeekDesk.Control.UserControls.PannelCard
MenuListBox.SelectedIndex = appData.MenuList.Count - 1;
appData.AppConfig.SelectedMenuIndex = MenuListBox.SelectedIndex;
appData.AppConfig.SelectedMenuIcons = info.IconList;
ItemCollection ic = MenuListBox.Items;
SolidColorBrush bac = new SolidColorBrush(Color.FromRgb(236, 236, 236));
SolidColorBrush fontColor = new SolidColorBrush(Colors.Black);
foreach (var icItem in ic)
{
ListBoxItem lbi = icItem as ListBoxItem;
lbi.MouseEnter += (s, me) =>
{
lbi.Background = bac;
};
lbi.MouseLeave += Lbi_MouseLeave;
lbi.Selected += (s, me) =>
{
lbi.MouseLeave -= Lbi_MouseLeave;
lbi.Background = bac;
lbi.Foreground = fontColor;
};
}
}
/// <summary>
@@ -116,6 +170,7 @@ namespace GeekDesk.Control.UserControls.PannelCard
{
MenuInfo menuInfo = ((MenuItem)sender).Tag as MenuInfo;
menuInfo.MenuEdit = (int)Visibility.Visible;
IS_EDIT = true;
}
/// <summary>
@@ -154,6 +209,7 @@ namespace GeekDesk.Control.UserControls.PannelCard
/// <param name="e"></param>
private void LostFocusOrEnterDown(object sender, EventArgs e)
{
bool done = true;
TextBox menuBox = null;
if (e.GetType() == typeof(KeyEventArgs))
{
@@ -161,19 +217,26 @@ namespace GeekDesk.Control.UserControls.PannelCard
if (eKey.Key == Key.Enter)
{
menuBox = ((TextBox)sender);
} else
{
done = false;
}
}
else if (e.GetType() == typeof(RoutedEventArgs))
} else if (e.GetType() == typeof(RoutedEventArgs))
{
menuBox = ((TextBox)sender);
}
if (menuBox != null)
if (done)
{
MenuInfo menuInfo = menuBox.Tag as MenuInfo;
string text = menuBox.Text;
menuInfo.MenuName = text;
menuInfo.MenuEdit = Visibility.Collapsed;
if (menuBox != null)
{
MenuInfo menuInfo = menuBox.Tag as MenuInfo;
string text = menuBox.Text;
menuInfo.MenuName = text;
menuInfo.MenuEdit = Visibility.Collapsed;
}
IS_EDIT = false;
MenuListBox.SelectedIndex = menuSelectIndexTemp;
}
}
@@ -185,6 +248,7 @@ namespace GeekDesk.Control.UserControls.PannelCard
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);
@@ -208,7 +272,7 @@ namespace GeekDesk.Control.UserControls.PannelCard
//设置对应菜单的图标列表
if (MenuListBox.SelectedIndex == -1)
{
appData.AppConfig.SelectedMenuIcons = appData.MenuList[appData.MenuList.Count - 1].IconList;
//appData.AppConfig.SelectedMenuIcons = appData.MenuList[appData.MenuList.Count - 1].IconList;
}
else
{
@@ -216,6 +280,20 @@ namespace GeekDesk.Control.UserControls.PannelCard
}
}
private void ListBoxItem_Unselected(object sender, RoutedEventArgs e)
{
//添加Leave效果
ListBoxItem lbi = sender as ListBoxItem;
lbi.Background = Brushes.Transparent;
lbi.MouseLeave += Lbi_MouseLeave;
}
private void Lbi_MouseLeave(object sender, MouseEventArgs e)
{
ListBoxItem lbi = sender as ListBoxItem;
lbi.Background = Brushes.Transparent;
}
/// <summary>
/// 鼠标悬停切换菜单
/// </summary>
@@ -223,7 +301,7 @@ namespace GeekDesk.Control.UserControls.PannelCard
/// <param name="e"></param>
private void Menu_MouseEnter(object sender, MouseEventArgs e)
{
if (appData.AppConfig.HoverMenu)
if (appData.AppConfig.HoverMenu && !IS_EDIT)
{
new Thread(() =>
{
@@ -241,5 +319,6 @@ namespace GeekDesk.Control.UserControls.PannelCard
}
}
}
}