2026-04-13 23:53:59 +08:00
|
|
|
using System.Windows;
|
|
|
|
|
using System.Windows.Controls;
|
|
|
|
|
using System.Windows.Input;
|
|
|
|
|
using System.Windows.Media;
|
|
|
|
|
|
|
|
|
|
namespace Ink_Canvas.Controls
|
|
|
|
|
{
|
|
|
|
|
public partial class ToolMenuButton : UserControl
|
|
|
|
|
{
|
2026-04-14 12:19:36 +08:00
|
|
|
private Geometry _pendingGeometry;
|
|
|
|
|
private Brush _pendingBrush;
|
|
|
|
|
|
2026-04-13 23:53:59 +08:00
|
|
|
public static readonly DependencyProperty LabelProperty = DependencyProperty.Register(
|
|
|
|
|
nameof(Label), typeof(string), typeof(ToolMenuButton),
|
|
|
|
|
new PropertyMetadata(string.Empty, OnLabelChanged));
|
|
|
|
|
|
|
|
|
|
private static void OnLabelChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
|
|
|
|
|
{
|
|
|
|
|
var button = (ToolMenuButton)d;
|
|
|
|
|
if (button.LabelControl != null)
|
|
|
|
|
button.LabelControl.Content = (string)e.NewValue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public string Label
|
|
|
|
|
{
|
|
|
|
|
get => (string)GetValue(LabelProperty);
|
|
|
|
|
set => SetValue(LabelProperty, value);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static readonly DependencyProperty IconGeometryProperty = DependencyProperty.Register(
|
|
|
|
|
nameof(IconGeometry), typeof(string), typeof(ToolMenuButton),
|
|
|
|
|
new PropertyMetadata(string.Empty, OnIconGeometryChanged));
|
|
|
|
|
|
|
|
|
|
private static void OnIconGeometryChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
|
|
|
|
|
{
|
|
|
|
|
var button = (ToolMenuButton)d;
|
2026-04-14 12:19:36 +08:00
|
|
|
if (e.NewValue is string geometry && !string.IsNullOrEmpty(geometry))
|
2026-04-13 23:53:59 +08:00
|
|
|
{
|
2026-04-14 12:19:36 +08:00
|
|
|
button._pendingGeometry = Geometry.Parse(geometry);
|
|
|
|
|
if (button.IconGeometryInternal != null)
|
|
|
|
|
button.IconGeometryInternal.Geometry = button._pendingGeometry;
|
2026-04-13 23:53:59 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public string IconGeometry
|
|
|
|
|
{
|
|
|
|
|
get => (string)GetValue(IconGeometryProperty);
|
|
|
|
|
set => SetValue(IconGeometryProperty, value);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static readonly DependencyProperty IconBrushProperty = DependencyProperty.Register(
|
|
|
|
|
nameof(IconBrush), typeof(Brush), typeof(ToolMenuButton),
|
|
|
|
|
new PropertyMetadata(null, OnIconBrushChanged));
|
|
|
|
|
|
|
|
|
|
private static void OnIconBrushChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
|
|
|
|
|
{
|
|
|
|
|
var button = (ToolMenuButton)d;
|
2026-04-14 12:19:36 +08:00
|
|
|
button._pendingBrush = (Brush)e.NewValue;
|
2026-04-13 23:53:59 +08:00
|
|
|
if (button.IconGeometryInternal != null)
|
2026-04-14 12:19:36 +08:00
|
|
|
button.IconGeometryInternal.Brush = button._pendingBrush;
|
2026-04-13 23:53:59 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public Brush IconBrush
|
|
|
|
|
{
|
|
|
|
|
get => (Brush)GetValue(IconBrushProperty);
|
|
|
|
|
set => SetValue(IconBrushProperty, value);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public new Brush Background
|
|
|
|
|
{
|
|
|
|
|
get => ButtonPanel.Background;
|
|
|
|
|
set => ButtonPanel.Background = value;
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-14 12:19:36 +08:00
|
|
|
public GeometryDrawing Icon
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
if (IconGeometryInternal != null)
|
|
|
|
|
return IconGeometryInternal;
|
|
|
|
|
return new GeometryDrawing(_pendingBrush, null, _pendingGeometry);
|
|
|
|
|
}
|
|
|
|
|
}
|
2026-04-13 23:53:59 +08:00
|
|
|
|
|
|
|
|
public event MouseButtonEventHandler ButtonMouseDown;
|
|
|
|
|
public event MouseEventHandler ButtonMouseLeave;
|
2026-04-17 01:20:06 +08:00
|
|
|
public event MouseButtonEventHandler ButtonMouseUp;
|
2026-04-13 23:53:59 +08:00
|
|
|
|
|
|
|
|
public ToolMenuButton()
|
|
|
|
|
{
|
|
|
|
|
InitializeComponent();
|
2026-04-14 12:19:36 +08:00
|
|
|
Loaded += ToolMenuButton_Loaded;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void ToolMenuButton_Loaded(object sender, RoutedEventArgs e)
|
|
|
|
|
{
|
|
|
|
|
if (IconGeometryInternal != null)
|
|
|
|
|
{
|
|
|
|
|
if (_pendingGeometry != null)
|
|
|
|
|
IconGeometryInternal.Geometry = _pendingGeometry;
|
|
|
|
|
if (_pendingBrush != null)
|
|
|
|
|
IconGeometryInternal.Brush = _pendingBrush;
|
|
|
|
|
}
|
2026-04-13 23:53:59 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void ButtonPanel_MouseDown(object sender, MouseButtonEventArgs e)
|
|
|
|
|
{
|
|
|
|
|
ButtonMouseDown?.Invoke(this, e);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void ButtonPanel_MouseLeave(object sender, MouseEventArgs e)
|
|
|
|
|
{
|
|
|
|
|
ButtonMouseLeave?.Invoke(this, e);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void ButtonPanel_MouseUp(object sender, MouseButtonEventArgs e)
|
|
|
|
|
{
|
2026-04-17 01:20:06 +08:00
|
|
|
ButtonMouseUp?.Invoke(this, e);
|
2026-04-13 23:53:59 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|