1135 lines
37 KiB
C#
1135 lines
37 KiB
C#
using AntdUI;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Drawing;
|
|
using System.Linq;
|
|
using System.Threading.Tasks;
|
|
using System.Windows.Forms;
|
|
using System.Xml.Linq;
|
|
|
|
namespace JinYuan.ControlLib
|
|
{
|
|
|
|
public enum NavigationItemType
|
|
{
|
|
Normal, // 普通项
|
|
Settings, // 设置项
|
|
UserManagement // 用户管理项
|
|
}
|
|
|
|
public enum Position
|
|
{
|
|
Top,
|
|
Bottom
|
|
}
|
|
|
|
public class SidebarNavigationItem
|
|
{
|
|
public string Text { get; set; }
|
|
public string Tag { get; set; }
|
|
public string IconSvg { get; set; }
|
|
public Func<Control> CreatePageControl { get; set; }
|
|
public NavigationItemType ItemType { get; set; } = NavigationItemType.Normal;
|
|
public Position Position { get; set; } = Position.Top;
|
|
public bool IsSettings { get; set; }
|
|
}
|
|
|
|
public partial class SidebarNavigation : UserControl
|
|
{
|
|
private List<SidebarNavigationItem> items = new List<SidebarNavigationItem>();
|
|
private Dictionary<string, ButtonInfo> buttonCache = new Dictionary<string, ButtonInfo>();
|
|
private Color lightBackgroundColor = Color.FromArgb(240, 240, 240);
|
|
private Color darkBackgroundColor = Color.FromArgb(33, 33, 33);
|
|
private Color lightSelectedColor = Color.FromArgb(0, 122, 204);
|
|
private Color darkSelectedColor = Color.FromArgb(0, 122, 204);
|
|
private AntdUI.Button selectedButton;
|
|
private bool isReordering = false;
|
|
private Font buttonFont = new Font("Microsoft YaHei UI", 10F);
|
|
private ToolTip toolTip = new ToolTip(); // 添加 ToolTip 控件
|
|
|
|
public Size IconSize { get; set; } = new Size(42, 42);
|
|
public event EventHandler<SidebarNavigationItem> ItemSelected;
|
|
|
|
public class ButtonInfo
|
|
{
|
|
public SidebarNavigationItem Item { get; set; }
|
|
public AntdUI.Button Button { get; set; }
|
|
}
|
|
|
|
private bool showText = false;
|
|
public bool ShowText
|
|
{
|
|
get => showText;
|
|
set
|
|
{
|
|
if (showText != value)
|
|
{
|
|
showText = value;
|
|
UpdateButtonsLayout();
|
|
}
|
|
}
|
|
}
|
|
|
|
public SidebarNavigation()
|
|
{
|
|
InitializeComponent();
|
|
|
|
SetStyle(ControlStyles.OptimizedDoubleBuffer |
|
|
ControlStyles.AllPaintingInWmPaint |
|
|
ControlStyles.UserPaint, true);
|
|
|
|
this.BackColor = AntdUI.Config.IsDark ? darkBackgroundColor : lightBackgroundColor;
|
|
this.Width = 50;
|
|
this.Padding = new Padding(0);
|
|
this.Margin = new Padding(0);
|
|
this.AutoScroll = false;
|
|
|
|
// 配置 ToolTip
|
|
toolTip.AutoPopDelay = 5000;
|
|
toolTip.InitialDelay = 500;
|
|
toolTip.ReshowDelay = 500;
|
|
toolTip.ShowAlways = true;
|
|
}
|
|
|
|
public void UpdateVisibility(Func<SidebarNavigationItem, bool> visibilityPredicate)
|
|
{
|
|
this.SuspendLayout();
|
|
try
|
|
{
|
|
// 保存当前选中按钮的Tag
|
|
string selectedTag = null;
|
|
if (selectedButton?.Tag is ButtonInfo selectedInfo)
|
|
{
|
|
selectedTag = selectedInfo.Item.Tag;
|
|
}
|
|
|
|
// 缓存现有按钮的状态
|
|
foreach (Control control in this.Controls)
|
|
{
|
|
if (control is AntdUI.Button button && button.Tag is ButtonInfo info)
|
|
{
|
|
buttonCache[info.Item.Tag] = info;
|
|
}
|
|
}
|
|
|
|
// 清除现有控件
|
|
this.Controls.Clear();
|
|
|
|
// 重新添加符合条件的按钮
|
|
var topButtons = items.Where(i => i.Position == Position.Top && visibilityPredicate(i))
|
|
.OrderBy(i => items.IndexOf(i));
|
|
|
|
var bottomButtons = items.Where(i => i.Position == Position.Bottom && visibilityPredicate(i))
|
|
.OrderBy(i => items.IndexOf(i));
|
|
|
|
// 添加顶部按钮
|
|
foreach (var item in topButtons.Reverse())
|
|
{
|
|
CreateOrRestoreButton(item);
|
|
}
|
|
|
|
// 添加底部按钮
|
|
foreach (var item in bottomButtons)
|
|
{
|
|
CreateOrRestoreButton(item);
|
|
}
|
|
|
|
// 恢复选中状态
|
|
if (selectedTag != null)
|
|
{
|
|
foreach (Control control in this.Controls)
|
|
{
|
|
if (control is AntdUI.Button button &&
|
|
button.Tag is ButtonInfo info &&
|
|
info.Item.Tag == selectedTag)
|
|
{
|
|
SetSelectedButton(button);
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
finally
|
|
{
|
|
this.ResumeLayout();
|
|
}
|
|
}
|
|
|
|
private void CreateOrRestoreButton(SidebarNavigationItem item)
|
|
{
|
|
if (buttonCache.TryGetValue(item.Tag, out ButtonInfo cachedInfo))
|
|
{
|
|
AntdUI.Button btn = new AntdUI.Button
|
|
{
|
|
Text = showText ? item.Text : "",
|
|
TextMultiLine = true,
|
|
AutoEllipsis = true,
|
|
BackColor = cachedInfo.Button.BackColor,
|
|
ForeColor = AntdUI.Config.IsDark ? Color.White : Color.Black,
|
|
Width = this.Width,
|
|
Height = this.Width + 5,
|
|
Dock = item.Position == Position.Bottom ? DockStyle.Bottom : DockStyle.Top,
|
|
Font = buttonFont,
|
|
IconPosition = TAlignMini.Top,
|
|
Radius = 3,
|
|
BorderWidth = 1,
|
|
WaveSize = 1,
|
|
IconSvg = item.IconSvg,
|
|
IconRatio = 0.8f,
|
|
Margin = new Padding(0),
|
|
Padding = new Padding(0),
|
|
RespondRealAreas = true,
|
|
Type = cachedInfo.Button.Type,
|
|
Ghost = false
|
|
};
|
|
|
|
// 设置工具提示
|
|
toolTip.SetToolTip(btn, item.Text);
|
|
|
|
btn.Click += NavigationButton_Click;
|
|
|
|
var buttonInfo = new ButtonInfo
|
|
{
|
|
Item = item,
|
|
Button = btn
|
|
};
|
|
btn.Tag = buttonInfo;
|
|
|
|
this.Controls.Add(btn);
|
|
}
|
|
else
|
|
{
|
|
CreateNavigationButton(item);
|
|
}
|
|
}
|
|
|
|
private void CreateNavigationButton(SidebarNavigationItem item)
|
|
{
|
|
AntdUI.Button btn = new AntdUI.Button
|
|
{
|
|
Text = showText ? item.Text : "",
|
|
TextMultiLine = true,
|
|
AutoEllipsis = true,
|
|
BackColor = Color.Transparent,
|
|
ForeColor = AntdUI.Config.IsDark ? Color.White : Color.Black,
|
|
Width = this.Width,
|
|
Height = this.Width + 5,
|
|
Dock = item.Position == Position.Bottom ? DockStyle.Bottom : DockStyle.Top,
|
|
Font = buttonFont,
|
|
IconPosition = TAlignMini.Top,
|
|
Radius = 3,
|
|
BorderWidth = 1,
|
|
WaveSize = 1,
|
|
IconSvg = item.IconSvg,
|
|
IconRatio = 0.8f,
|
|
Margin = new Padding(0),
|
|
Padding = new Padding(0),
|
|
RespondRealAreas = true,
|
|
Type = TTypeMini.Primary,
|
|
Ghost = false
|
|
};
|
|
|
|
// 设置工具提示
|
|
toolTip.SetToolTip(btn, item.Text);
|
|
|
|
btn.Click += NavigationButton_Click;
|
|
|
|
var buttonInfo = new ButtonInfo
|
|
{
|
|
Item = item,
|
|
Button = btn
|
|
};
|
|
btn.Tag = buttonInfo;
|
|
|
|
this.Controls.Add(btn);
|
|
|
|
if (selectedButton != null &&
|
|
selectedButton.Tag is ButtonInfo selectedInfo &&
|
|
selectedInfo.Item.Tag == item.Tag)
|
|
{
|
|
SetSelectedButton(btn);
|
|
}
|
|
}
|
|
|
|
private void UpdateButtonsLayout()
|
|
{
|
|
this.SuspendLayout();
|
|
foreach (Control control in this.Controls)
|
|
{
|
|
if (control is AntdUI.Button button)
|
|
{
|
|
button.Height = this.Width;
|
|
if (button.Tag is ButtonInfo buttonInfo)
|
|
{
|
|
button.Text = showText ? buttonInfo.Item.Text : "";
|
|
}
|
|
}
|
|
}
|
|
this.PerformLayout();
|
|
this.ResumeLayout();
|
|
}
|
|
|
|
private void SetSelectedButton(AntdUI.Button button)
|
|
{
|
|
if (selectedButton != null)
|
|
{
|
|
selectedButton.BackColor = Color.Transparent;
|
|
selectedButton.Type = TTypeMini.Default;
|
|
selectedButton.Invalidate();
|
|
}
|
|
selectedButton = button;
|
|
if (button != null)
|
|
{
|
|
button.BackColor = AntdUI.Config.IsDark ? darkSelectedColor : lightSelectedColor;
|
|
button.Type = TTypeMini.Primary;
|
|
button.Invalidate();
|
|
}
|
|
}
|
|
|
|
public void UpdateTheme(bool isDark)
|
|
{
|
|
this.SuspendLayout();
|
|
this.BackColor = isDark ? darkBackgroundColor : lightBackgroundColor;
|
|
|
|
foreach (Control control in this.Controls)
|
|
{
|
|
if (control is AntdUI.Button button)
|
|
{
|
|
if (button == selectedButton)
|
|
{
|
|
button.BackColor = isDark ? darkSelectedColor : lightSelectedColor;
|
|
button.Type = TTypeMini.Primary;
|
|
}
|
|
else
|
|
{
|
|
button.BackColor = Color.Transparent;
|
|
button.Type = TTypeMini.Default;
|
|
}
|
|
button.ForeColor = isDark ? Color.White : Color.Black;
|
|
}
|
|
}
|
|
|
|
this.ResumeLayout();
|
|
}
|
|
|
|
public void AddItem(SidebarNavigationItem item)
|
|
{
|
|
this.SuspendLayout();
|
|
items.Add(item);
|
|
CreateNavigationButton(item);
|
|
ReorderButtons();
|
|
this.ResumeLayout();
|
|
}
|
|
|
|
protected override void OnResize(EventArgs e)
|
|
{
|
|
base.OnResize(e);
|
|
foreach (Control control in this.Controls)
|
|
{
|
|
if (control is AntdUI.Button button)
|
|
{
|
|
button.Width = this.Width;
|
|
button.Height = this.Width;
|
|
}
|
|
}
|
|
}
|
|
|
|
private void ReorderButtons()
|
|
{
|
|
if (isReordering) return;
|
|
|
|
try
|
|
{
|
|
isReordering = true;
|
|
this.SuspendLayout();
|
|
|
|
var visibleTopItems = items.Where(i => i.Position == Position.Top &&
|
|
this.Controls.Cast<Control>()
|
|
.Any(c => c is AntdUI.Button b &&
|
|
b.Tag is ButtonInfo bi &&
|
|
bi.Item == i))
|
|
.OrderBy(i => items.IndexOf(i));
|
|
|
|
var visibleBottomItems = items.Where(i => i.Position == Position.Bottom &&
|
|
this.Controls.Cast<Control>()
|
|
.Any(c => c is AntdUI.Button b &&
|
|
b.Tag is ButtonInfo bi &&
|
|
bi.Item == i))
|
|
.OrderBy(i => items.IndexOf(i));
|
|
|
|
this.Controls.Clear();
|
|
|
|
foreach (var item in visibleTopItems.Reverse())
|
|
{
|
|
CreateOrRestoreButton(item);
|
|
}
|
|
|
|
foreach (var item in visibleBottomItems)
|
|
{
|
|
CreateOrRestoreButton(item);
|
|
}
|
|
}
|
|
finally
|
|
{
|
|
this.ResumeLayout();
|
|
isReordering = false;
|
|
}
|
|
}
|
|
|
|
private string ResizeSvgIcon(string svgContent, Size targetSize)
|
|
{
|
|
try
|
|
{
|
|
XDocument doc = XDocument.Parse(svgContent);
|
|
XElement svg = doc.Root;
|
|
|
|
if (svg.Attribute("width") != null && svg.Attribute("height") != null)
|
|
{
|
|
svg.SetAttributeValue("width", targetSize.Width);
|
|
svg.SetAttributeValue("height", targetSize.Height);
|
|
}
|
|
|
|
if (svg.Attribute("viewBox") == null)
|
|
{
|
|
svg.SetAttributeValue("viewBox", $"0 0 {targetSize.Width} {targetSize.Height}");
|
|
}
|
|
|
|
return doc.ToString();
|
|
}
|
|
catch (Exception)
|
|
{
|
|
return svgContent;
|
|
}
|
|
}
|
|
|
|
private void NavigationButton_Click(object sender, EventArgs e)
|
|
{
|
|
if (sender is AntdUI.Button clickedButton && clickedButton.Tag is ButtonInfo info)
|
|
{
|
|
SetSelectedButton(clickedButton);
|
|
ItemSelected?.Invoke(this, info.Item);
|
|
}
|
|
}
|
|
|
|
public void UpdateButtonText(string tag, string text)
|
|
{
|
|
foreach (Control control in this.Controls)
|
|
{
|
|
if (control is AntdUI.Button button &&
|
|
button.Tag is ButtonInfo buttonInfo &&
|
|
buttonInfo.Item.Tag == tag)
|
|
{
|
|
buttonInfo.Item.Text = text;
|
|
button.Text = showText ? text : "";
|
|
toolTip.SetToolTip(button, text); // 更新工具提示
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
|
|
public void ClearItems()
|
|
{
|
|
this.SuspendLayout();
|
|
try
|
|
{
|
|
items.Clear();
|
|
buttonCache.Clear(); // 清除按钮缓存
|
|
this.Controls.Clear();
|
|
selectedButton = null;
|
|
}
|
|
finally
|
|
{
|
|
this.ResumeLayout();
|
|
}
|
|
this.Invalidate();
|
|
}
|
|
|
|
public IEnumerable<SidebarNavigationItem> GetItems()
|
|
{
|
|
return items;
|
|
}
|
|
|
|
|
|
}
|
|
//// 1. 首先定义导航项类型的枚举
|
|
//public enum NavigationItemType
|
|
//{
|
|
// Normal, // 普通项
|
|
// Settings, // 设置项
|
|
// UserManagement // 用户管理项
|
|
//}
|
|
|
|
//public enum Position
|
|
//{
|
|
// Top,
|
|
// Bottom
|
|
//}
|
|
|
|
//public class SidebarNavigationItem
|
|
//{
|
|
// public string Text { get; set; }
|
|
// public string Tag { get; set; }
|
|
// public string IconSvg { get; set; }
|
|
// public Func<Control> CreatePageControl { get; set; }
|
|
// public NavigationItemType ItemType { get; set; } = NavigationItemType.Normal; // 默认为普通项
|
|
// public Position Position { get; set; } = Position.Top;
|
|
|
|
// public bool IsSettings { get; set; } // 保留原有属性以保持兼容性
|
|
|
|
//}
|
|
|
|
//public partial class SidebarNavigation : UserControl
|
|
//{
|
|
// private List<SidebarNavigationItem> items = new List<SidebarNavigationItem>();
|
|
// private Color lightBackgroundColor = Color.FromArgb(240, 240, 240);
|
|
// private Color darkBackgroundColor = Color.FromArgb(33, 33, 33);
|
|
// private Color lightSelectedColor = Color.FromArgb(0, 122, 204);
|
|
// private Color darkSelectedColor = Color.FromArgb(0, 122, 204);
|
|
// private AntdUI.Button selectedButton;
|
|
// private bool isReordering = false;
|
|
// private Font buttonFont = new Font("Microsoft YaHei UI", 10F);
|
|
|
|
// public Size IconSize { get; set; } = new Size(42,42);
|
|
|
|
// public event EventHandler<SidebarNavigationItem> ItemSelected;
|
|
|
|
// public class ButtonInfo
|
|
// {
|
|
// public SidebarNavigationItem Item { get; set; }
|
|
// public AntdUI.Button Button { get; set; }
|
|
// }
|
|
|
|
// private bool showText = false;
|
|
// public bool ShowText
|
|
// {
|
|
// get => showText;
|
|
// set
|
|
// {
|
|
// if (showText != value)
|
|
// {
|
|
// showText = value;
|
|
// UpdateButtonsLayout();
|
|
// }
|
|
// }
|
|
// }
|
|
|
|
// public SidebarNavigation()
|
|
// {
|
|
// InitializeComponent();
|
|
|
|
// SetStyle(ControlStyles.OptimizedDoubleBuffer |
|
|
// ControlStyles.AllPaintingInWmPaint |
|
|
// ControlStyles.UserPaint, true);
|
|
|
|
// this.BackColor = AntdUI.Config.IsDark ? darkBackgroundColor : lightBackgroundColor;
|
|
// this.Width = 50;
|
|
// this.Padding = new Padding(0);// 移除控件的内边距
|
|
// this.Margin = new Padding(0);// 移除控件的外边距
|
|
// this.AutoScroll = false;// 禁用自动滚动以避免出现滚动条
|
|
// }
|
|
|
|
// public void UpdateVisibility(Func<SidebarNavigationItem, bool> visibilityPredicate)
|
|
// {
|
|
// this.SuspendLayout();
|
|
// try
|
|
// {
|
|
// // 先从 Controls 集合中移除所有按钮
|
|
// this.Controls.Clear();
|
|
|
|
// // 重新添加符合条件的按钮
|
|
// var topButtons = items.Where(i => i.Position == Position.Top && visibilityPredicate(i))
|
|
// .OrderBy(i => items.IndexOf(i));
|
|
|
|
// var bottomButtons = items.Where(i => i.Position == Position.Bottom && visibilityPredicate(i))
|
|
// .OrderBy(i => items.IndexOf(i));
|
|
|
|
// // 添加顶部按钮
|
|
// foreach (var item in topButtons.Reverse())
|
|
// {
|
|
// CreateNavigationButton(item);
|
|
// }
|
|
|
|
// // 添加底部按钮
|
|
// foreach (var item in bottomButtons)
|
|
// {
|
|
// CreateNavigationButton(item);
|
|
// }
|
|
// }
|
|
// finally
|
|
// {
|
|
// this.ResumeLayout();
|
|
// }
|
|
// }
|
|
|
|
|
|
// private void CreateNavigationButton(SidebarNavigationItem item)
|
|
// {
|
|
// AntdUI.Button btn = new AntdUI.Button
|
|
// {
|
|
// Text = showText ? item.Text : "",
|
|
// // 启用多行文本
|
|
// TextMultiLine = true,
|
|
// // 允许文本超出时自动处理
|
|
// AutoEllipsis = true,
|
|
// BackColor = Color.Transparent,
|
|
// ForeColor = AntdUI.Config.IsDark ? Color.White : Color.Black,
|
|
// Width = this.Width,
|
|
// Height = this.Width + 5,
|
|
// Dock = item.Position == Position.Bottom ? DockStyle.Bottom : DockStyle.Top,
|
|
// Font = buttonFont,
|
|
// IconPosition = TAlignMini.Top,
|
|
// Radius = 3,
|
|
// BorderWidth = 1,
|
|
// WaveSize = 1,
|
|
// IconSvg = item.IconSvg,
|
|
// // Increase icon ratio to make icons larger relative to button size
|
|
// IconRatio = 0.8f,
|
|
// Margin = new Padding(0),
|
|
// Padding = new Padding(0),
|
|
// RespondRealAreas = true,
|
|
// Type = TTypeMini.Primary,
|
|
// Ghost = false
|
|
// };
|
|
|
|
// btn.Click += NavigationButton_Click;
|
|
|
|
// var buttonInfo = new ButtonInfo
|
|
// {
|
|
// Item = item,
|
|
// Button = btn
|
|
// };
|
|
// btn.Tag = buttonInfo;
|
|
|
|
// this.Controls.Add(btn);
|
|
|
|
// // 如果是当前选中的按钮,恢复选中状态
|
|
// if (selectedButton != null &&
|
|
// selectedButton.Tag is ButtonInfo selectedInfo &&
|
|
// selectedInfo.Item.Tag == item.Tag)
|
|
// {
|
|
// SetSelectedButton(btn);
|
|
// }
|
|
// }
|
|
|
|
// private void UpdateButtonsLayout()
|
|
// {
|
|
// this.SuspendLayout();
|
|
// foreach (Control control in this.Controls)
|
|
// {
|
|
// if (control is AntdUI.Button button)
|
|
// {
|
|
// button.Height = this.Width;
|
|
// if (button.Tag is ButtonInfo buttonInfo)
|
|
// {
|
|
// button.Text = showText ? buttonInfo.Item.Text : "";
|
|
// }
|
|
// }
|
|
// }
|
|
// this.PerformLayout();
|
|
// this.ResumeLayout();
|
|
// }
|
|
|
|
// private void SetSelectedButton(AntdUI.Button button)
|
|
// {
|
|
// if (selectedButton != null)
|
|
// {
|
|
// selectedButton.BackColor = Color.Transparent;
|
|
// selectedButton.Type = TTypeMini.Default;
|
|
// selectedButton.Invalidate();
|
|
// }
|
|
// selectedButton = button;
|
|
// if (button != null)
|
|
// {
|
|
// button.BackColor = AntdUI.Config.IsDark ? darkSelectedColor : lightSelectedColor;
|
|
// button.Type = TTypeMini.Primary;
|
|
// button.Invalidate();
|
|
// }
|
|
// }
|
|
|
|
|
|
|
|
// public void UpdateTheme(bool isDark)
|
|
// {
|
|
// this.SuspendLayout();
|
|
// this.BackColor = isDark ? darkBackgroundColor : lightBackgroundColor;
|
|
|
|
// foreach (Control control in this.Controls)
|
|
// {
|
|
// if (control is AntdUI.Button button)
|
|
// {
|
|
// if (button == selectedButton)
|
|
// {
|
|
// button.BackColor = isDark ? darkSelectedColor : lightSelectedColor;
|
|
// button.Type = TTypeMini.Primary;
|
|
// }
|
|
// else
|
|
// {
|
|
// button.BackColor = Color.Transparent;
|
|
// button.Type = TTypeMini.Default;
|
|
// }
|
|
// button.ForeColor = isDark ? Color.White : Color.Black;
|
|
// }
|
|
// }
|
|
|
|
// this.ResumeLayout();
|
|
// }
|
|
|
|
|
|
|
|
// public void AddItem(SidebarNavigationItem item)
|
|
// {
|
|
// this.SuspendLayout();
|
|
// items.Add(item);
|
|
// CreateNavigationButton(item);
|
|
// ReorderButtons();
|
|
// this.ResumeLayout();
|
|
// }
|
|
|
|
|
|
// protected override void OnResize(EventArgs e)
|
|
// {
|
|
// base.OnResize(e);
|
|
// foreach (Control control in this.Controls)
|
|
// {
|
|
// if (control is AntdUI.Button button)
|
|
// {
|
|
// button.Width = this.Width;
|
|
// button.Height = this.Width;
|
|
// }
|
|
// }
|
|
// }
|
|
|
|
// private void ReorderButtons()
|
|
// {
|
|
// if (isReordering) return;
|
|
|
|
// try
|
|
// {
|
|
// isReordering = true;
|
|
// this.SuspendLayout();
|
|
|
|
// // 获取当前所有可见的按钮
|
|
// var visibleTopItems = items.Where(i => i.Position == Position.Top &&
|
|
// this.Controls.Cast<Control>()
|
|
// .Any(c => c is AntdUI.Button b &&
|
|
// b.Tag is ButtonInfo bi &&
|
|
// bi.Item == i))
|
|
// .OrderBy(i => items.IndexOf(i));
|
|
|
|
// var visibleBottomItems = items.Where(i => i.Position == Position.Bottom &&
|
|
// this.Controls.Cast<Control>()
|
|
// .Any(c => c is AntdUI.Button b &&
|
|
// b.Tag is ButtonInfo bi &&
|
|
// bi.Item == i))
|
|
// .OrderBy(i => items.IndexOf(i));
|
|
|
|
// this.Controls.Clear();
|
|
|
|
// // 重新添加顶部按钮
|
|
// foreach (var item in visibleTopItems.Reverse())
|
|
// {
|
|
// CreateNavigationButton(item);
|
|
// }
|
|
|
|
// // 重新添加底部按钮
|
|
// foreach (var item in visibleBottomItems)
|
|
// {
|
|
// CreateNavigationButton(item);
|
|
// }
|
|
// }
|
|
// finally
|
|
// {
|
|
// this.ResumeLayout();
|
|
// isReordering = false;
|
|
// }
|
|
// }
|
|
|
|
// private string ResizeSvgIcon(string svgContent, Size targetSize)
|
|
// {
|
|
// try
|
|
// {
|
|
// XDocument doc = XDocument.Parse(svgContent);
|
|
// XElement svg = doc.Root;
|
|
|
|
// if (svg.Attribute("width") != null && svg.Attribute("height") != null)
|
|
// {
|
|
// svg.SetAttributeValue("width", targetSize.Width);
|
|
// svg.SetAttributeValue("height", targetSize.Height);
|
|
// }
|
|
|
|
// if (svg.Attribute("viewBox") == null)
|
|
// {
|
|
// svg.SetAttributeValue("viewBox", $"0 0 {targetSize.Width} {targetSize.Height}");
|
|
// }
|
|
|
|
// return doc.ToString();
|
|
// }
|
|
// catch (Exception)
|
|
// {
|
|
// return svgContent;
|
|
// }
|
|
// }
|
|
|
|
// private void NavigationButton_Click(object sender, EventArgs e)
|
|
// {
|
|
// if (sender is AntdUI.Button clickedButton && clickedButton.Tag is ButtonInfo info)
|
|
// {
|
|
// SetSelectedButton(clickedButton);
|
|
// ItemSelected?.Invoke(this, info.Item);
|
|
// }
|
|
// }
|
|
|
|
// // 更新按钮文本的方法
|
|
// public void UpdateButtonText(string tag, string text)
|
|
// {
|
|
// foreach (Control control in this.Controls)
|
|
// {
|
|
// if (control is AntdUI.Button button &&
|
|
// button.Tag is ButtonInfo buttonInfo &&
|
|
// buttonInfo.Item.Tag == tag)
|
|
// {
|
|
// buttonInfo.Item.Text = text;
|
|
// button.Text = showText ? text : "";
|
|
// break;
|
|
// }
|
|
// }
|
|
// }
|
|
|
|
|
|
|
|
|
|
// public void ClearItems()
|
|
// {
|
|
// this.SuspendLayout();
|
|
// try
|
|
// {
|
|
// items.Clear();
|
|
// this.Controls.Clear();
|
|
// selectedButton = null;
|
|
// }
|
|
// finally
|
|
// {
|
|
// this.ResumeLayout();
|
|
// }
|
|
// this.Invalidate();
|
|
// }
|
|
|
|
// public IEnumerable<SidebarNavigationItem> GetItems()
|
|
// {
|
|
// return items;
|
|
// }
|
|
|
|
|
|
|
|
//}
|
|
}
|
|
|
|
#region 最早期的
|
|
//public enum Position
|
|
//{
|
|
// Top,
|
|
// Bottom
|
|
//}
|
|
|
|
//public class SidebarNavigationItem
|
|
//{
|
|
// public string Text { get; set; }
|
|
// public string Tag { get; set; }
|
|
// public string IconSvg { get; set; }
|
|
// public Func<Control> CreatePageControl { get; set; }
|
|
// public bool IsSettings { get; set; }
|
|
// public Position Position { get; set; } = Position.Top;
|
|
//}
|
|
|
|
//public partial class SidebarNavigation : UserControl
|
|
//{
|
|
// private List<SidebarNavigationItem> items = new List<SidebarNavigationItem>();
|
|
// private Color lightBackgroundColor = Color.FromArgb(240, 240, 240);
|
|
// private Color darkBackgroundColor = Color.FromArgb(33, 33, 33);
|
|
// private Color lightSelectedColor = Color.FromArgb(0, 150, 136);
|
|
// private Color darkSelectedColor = Color.FromArgb(0, 200, 186);
|
|
// private int buttonHeight = 40;
|
|
// private AntdUI.Button selectedButton;
|
|
// private ToolTip toolTip;
|
|
// private bool isReordering = false;
|
|
|
|
// public Size IconSize { get; set; } = new Size(200, 200);
|
|
|
|
// public event EventHandler<SidebarNavigationItem> ItemSelected;
|
|
|
|
// public SidebarNavigation()
|
|
// {
|
|
// InitializeComponent();
|
|
|
|
// SetStyle(ControlStyles.OptimizedDoubleBuffer |
|
|
// ControlStyles.AllPaintingInWmPaint |
|
|
// ControlStyles.UserPaint, true);
|
|
|
|
// this.BackColor = AntdUI.Config.IsDark ? darkBackgroundColor : lightBackgroundColor;
|
|
// this.Width = 50;
|
|
// this.AutoScroll = true;
|
|
|
|
// toolTip = new ToolTip
|
|
// {
|
|
// AutoPopDelay = 5000,
|
|
// InitialDelay = 500,
|
|
// ReshowDelay = 500,
|
|
// ShowAlways = true
|
|
// };
|
|
// }
|
|
|
|
// public void AddItem(SidebarNavigationItem item)
|
|
// {
|
|
// this.SuspendLayout();
|
|
// items.Add(item);
|
|
// CreateNavigationButton(item);
|
|
// ReorderButtons();
|
|
// this.ResumeLayout();
|
|
// }
|
|
|
|
// public void ClearItems()
|
|
// {
|
|
// this.SuspendLayout();
|
|
// try
|
|
// {
|
|
// items.Clear();
|
|
// for (int i = this.Controls.Count - 1; i >= 0; i--)
|
|
// {
|
|
// if (this.Controls[i] is AntdUI.Button button)
|
|
// {
|
|
// button.Click -= NavigationButton_Click;
|
|
// this.Controls.RemoveAt(i);
|
|
// button.Dispose();
|
|
// }
|
|
// }
|
|
// selectedButton = null;
|
|
// ReorderButtons();
|
|
// }
|
|
// finally
|
|
// {
|
|
// this.ResumeLayout();
|
|
// }
|
|
// this.Invalidate();
|
|
// }
|
|
|
|
// private void CreateNavigationButton(SidebarNavigationItem item)
|
|
// {
|
|
// string resizedSvg = ResizeSvgIcon(item.IconSvg, IconSize);
|
|
|
|
// AntdUI.Button btn = new AntdUI.Button
|
|
// {
|
|
// Text = "",
|
|
// Tag = item,
|
|
// Dock = item.Position == Position.Bottom ? DockStyle.Bottom : DockStyle.Top,
|
|
// Height = buttonHeight,
|
|
// IconSvg = resizedSvg,
|
|
// BackColor = this.BackColor,
|
|
// ForeColor = AntdUI.Config.IsDark ? Color.White : Color.Black,
|
|
// Radius = 0,
|
|
// BorderWidth = 0
|
|
// };
|
|
// btn.Click += NavigationButton_Click;
|
|
|
|
// toolTip.SetToolTip(btn, item.Text);
|
|
|
|
// this.Controls.Add(btn);
|
|
// }
|
|
|
|
// private string ResizeSvgIcon(string svgContent, Size targetSize)
|
|
// {
|
|
// try
|
|
// {
|
|
// XDocument doc = XDocument.Parse(svgContent);
|
|
// XElement svg = doc.Root;
|
|
|
|
// if (svg.Attribute("width") != null && svg.Attribute("height") != null)
|
|
// {
|
|
// svg.SetAttributeValue("width", targetSize.Width);
|
|
// svg.SetAttributeValue("height", targetSize.Height);
|
|
// }
|
|
|
|
// if (svg.Attribute("viewBox") == null)
|
|
// {
|
|
// svg.SetAttributeValue("viewBox", $"0 0 {targetSize.Width} {targetSize.Height}");
|
|
// }
|
|
|
|
// return doc.ToString();
|
|
// }
|
|
// catch (Exception)
|
|
// {
|
|
// return svgContent;
|
|
// }
|
|
// }
|
|
|
|
|
|
// /// <summary>
|
|
// /// 图标按钮排序
|
|
// /// </summary>
|
|
// private void ReorderButtons()
|
|
// {
|
|
// if (isReordering) return;
|
|
|
|
// try
|
|
// {
|
|
// isReordering = true;
|
|
// this.SuspendLayout();
|
|
|
|
// var buttonDict = this.Controls.OfType<AntdUI.Button>()
|
|
// .Where(b => b.Tag is SidebarNavigationItem)
|
|
// .ToDictionary(b => ((SidebarNavigationItem)b.Tag).Tag);
|
|
|
|
// this.Controls.Clear();
|
|
|
|
// // 分别获取顶部和底部的项目
|
|
// var topItems = items.Where(i => i.Position == Position.Top)
|
|
// .OrderBy(i => items.IndexOf(i));
|
|
// var bottomItems = items.Where(i => i.Position == Position.Bottom)
|
|
// .OrderBy(i => items.IndexOf(i));
|
|
|
|
// // 添加顶部按钮(反序添加以保证正确顺序)
|
|
// foreach (var item in topItems.Reverse())
|
|
// {
|
|
// if (buttonDict.TryGetValue(item.Tag, out var button))
|
|
// {
|
|
// button.Dock = DockStyle.Top;
|
|
// this.Controls.Add(button);
|
|
// }
|
|
// }
|
|
|
|
// // 添加底部按钮(正序添加以保证正确顺序)
|
|
// foreach (var item in bottomItems)
|
|
// {
|
|
// if (buttonDict.TryGetValue(item.Tag, out var button))
|
|
// {
|
|
// button.Dock = DockStyle.Bottom;
|
|
// this.Controls.Add(button);
|
|
// }
|
|
// }
|
|
// }
|
|
// finally
|
|
// {
|
|
// this.ResumeLayout();
|
|
// isReordering = false;
|
|
// }
|
|
// }
|
|
|
|
|
|
// public void UpdateIconSize(Size newSize)
|
|
// {
|
|
// this.IconSize = newSize;
|
|
// foreach (Control control in this.Controls)
|
|
// {
|
|
// if (control is AntdUI.Button button && button.Tag is SidebarNavigationItem item)
|
|
// {
|
|
// string resizedSvg = ResizeSvgIcon(item.IconSvg, newSize);
|
|
// button.IconSvg = resizedSvg;
|
|
// }
|
|
// }
|
|
// this.Invalidate();
|
|
// }
|
|
|
|
// private void NavigationButton_Click(object sender, EventArgs e)
|
|
// {
|
|
// if (sender is AntdUI.Button clickedButton && clickedButton.Tag is SidebarNavigationItem item)
|
|
// {
|
|
// SetSelectedButton(clickedButton);
|
|
// ItemSelected?.Invoke(this, item);
|
|
// }
|
|
// }
|
|
|
|
// private void SetSelectedButton(AntdUI.Button button)
|
|
// {
|
|
// if (selectedButton != null)
|
|
// {
|
|
// selectedButton.BackColor = this.BackColor;
|
|
// }
|
|
// selectedButton = button;
|
|
// selectedButton.BackColor = AntdUI.Config.IsDark ? darkSelectedColor : lightSelectedColor;
|
|
// }
|
|
|
|
// public void UpdateTheme(bool isDark)
|
|
// {
|
|
// this.SuspendLayout();
|
|
// this.BackColor = isDark ? darkBackgroundColor : lightBackgroundColor;
|
|
// foreach (Control control in this.Controls)
|
|
// {
|
|
// if (control is AntdUI.Button button)
|
|
// {
|
|
// button.BackColor = this.BackColor;
|
|
// button.ForeColor = isDark ? Color.White : Color.Black;
|
|
// }
|
|
// }
|
|
// if (selectedButton != null)
|
|
// {
|
|
// selectedButton.BackColor = isDark ? darkSelectedColor : lightSelectedColor;
|
|
// }
|
|
// toolTip.BackColor = isDark ? Color.FromArgb(64, 64, 64) : Color.White;
|
|
// toolTip.ForeColor = isDark ? Color.White : Color.Black;
|
|
// this.ResumeLayout();
|
|
// }
|
|
|
|
// public void UpdateItemToolTip(AntdUI.Button button, string translatedText)
|
|
// {
|
|
// if (button.InvokeRequired)
|
|
// {
|
|
// button.Invoke(new Action(() => toolTip.SetToolTip(button, translatedText)));
|
|
// }
|
|
// else
|
|
// {
|
|
// toolTip.SetToolTip(button, translatedText);
|
|
// }
|
|
// }
|
|
|
|
// protected override void OnControlAdded(ControlEventArgs e)
|
|
// {
|
|
// base.OnControlAdded(e);
|
|
// ReorderButtons();
|
|
// }
|
|
|
|
// protected override void OnResize(EventArgs e)
|
|
// {
|
|
// base.OnResize(e);
|
|
// this.SuspendLayout();
|
|
// foreach (Control control in this.Controls)
|
|
// {
|
|
// if (control is AntdUI.Button button)
|
|
// {
|
|
// button.Width = this.Width;
|
|
// }
|
|
// }
|
|
// this.ResumeLayout();
|
|
// }
|
|
|
|
// public void SelectFirstItem()
|
|
// {
|
|
// if (this.Controls.Count > 0 && this.Controls[0] is AntdUI.Button firstButton)
|
|
// {
|
|
// SetSelectedButton(firstButton);
|
|
// if (firstButton.Tag is SidebarNavigationItem item)
|
|
// {
|
|
// ItemSelected?.Invoke(this, item);
|
|
// }
|
|
// }
|
|
// }
|
|
|
|
// public IEnumerable<SidebarNavigationItem> GetItems()
|
|
// {
|
|
// return items;
|
|
// }
|
|
|
|
// public void UpdateVisibility(Func<SidebarNavigationItem, bool> visibilityPredicate)
|
|
// {
|
|
// this.SuspendLayout();
|
|
// foreach (Control control in this.Controls)
|
|
// {
|
|
// if (control is AntdUI.Button button && button.Tag is SidebarNavigationItem item)
|
|
// {
|
|
// button.Visible = visibilityPredicate(item);
|
|
// }
|
|
// }
|
|
// this.ResumeLayout();
|
|
// }
|
|
//}
|
|
#endregion
|
|
|