Files
community/InkCanvas.Controls/ToolbarImageButton.xaml.cs
T

111 lines
4.0 KiB
C#
Raw Normal View History

2026-04-11 12:06:53 +08:00
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media;
namespace Ink_Canvas.Controls
{
public partial class ToolbarImageButton : UserControl
{
private static ToolbarImageButton _lastPressedButton;
public static readonly DependencyProperty LabelProperty = DependencyProperty.Register(
nameof(Label), typeof(string), typeof(ToolbarImageButton),
new PropertyMetadata(string.Empty, (d, e) => ((ToolbarImageButton)d).LabelTextBlock.Text = (string)e.NewValue));
public string Label
{
get => (string)GetValue(LabelProperty);
set => SetValue(LabelProperty, value);
}
public static readonly DependencyProperty IconGeometryDrawingProperty = DependencyProperty.Register(
nameof(IconGeometryDrawing), typeof(GeometryDrawing), typeof(ToolbarImageButton),
new PropertyMetadata(null, OnIconGeometryDrawingChanged));
private static void OnIconGeometryDrawingChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
var button = (ToolbarImageButton)d;
if (e.NewValue is GeometryDrawing newDrawing)
{
button.IconGeometryInternal.Geometry = newDrawing.Geometry;
button.IconGeometryInternal.Brush = newDrawing.Brush;
}
}
public GeometryDrawing IconGeometryDrawing
{
get => (GeometryDrawing)GetValue(IconGeometryDrawingProperty);
set => SetValue(IconGeometryDrawingProperty, value);
}
public GeometryDrawing Icon => IconGeometryInternal;
public GeometryDrawing GeometryDrawing => IconGeometryInternal;
2026-04-11 17:18:51 +08:00
public static readonly DependencyProperty IconBrushProperty = DependencyProperty.Register(
nameof(IconBrush), typeof(Brush), typeof(ToolbarImageButton),
new PropertyMetadata(null, (d, e) => ((ToolbarImageButton)d).IconGeometryInternal.Brush = (Brush)e.NewValue));
public Brush IconBrush
{
get => (Brush)GetValue(IconBrushProperty);
set => SetValue(IconBrushProperty, value);
}
2026-04-11 12:06:53 +08:00
public new Brush Background
{
get => ButtonPanel.Background;
set => ButtonPanel.Background = value;
}
public event MouseButtonEventHandler ButtonMouseDown;
public event MouseEventHandler ButtonMouseLeave;
2026-04-17 01:20:06 +08:00
public event MouseButtonEventHandler ButtonMouseUp;
2026-04-11 12:06:53 +08:00
public ToolbarImageButton()
{
InitializeComponent();
ButtonPanel.Background = Brushes.Transparent;
2026-04-11 13:42:33 +08:00
IsEnabledChanged += ToolbarImageButton_IsEnabledChanged;
}
private void ToolbarImageButton_IsEnabledChanged(object sender, DependencyPropertyChangedEventArgs e)
{
var isEnabled = (bool)e.NewValue;
ButtonImage.Opacity = isEnabled ? 1.0 : 0.5;
LabelTextBlock.Opacity = isEnabled ? 1.0 : 0.5;
ButtonPanel.IsEnabled = isEnabled;
2026-04-11 12:06:53 +08:00
}
private void ButtonPanel_MouseDown(object sender, MouseButtonEventArgs e)
{
2026-04-11 13:42:33 +08:00
if (!IsEnabled) return;
2026-04-11 12:06:53 +08:00
if (_lastPressedButton != null && _lastPressedButton != this)
{
_lastPressedButton.Background = Brushes.Transparent;
}
_lastPressedButton = this;
ButtonPanel.Background = new SolidColorBrush(Color.FromArgb(28, 24, 24, 27));
ButtonMouseDown?.Invoke(this, e);
}
private void ButtonPanel_MouseLeave(object sender, MouseEventArgs e)
{
2026-04-11 13:42:33 +08:00
if (!IsEnabled) return;
2026-04-11 12:06:53 +08:00
ButtonMouseLeave?.Invoke(this, e);
}
private void ButtonPanel_MouseUp(object sender, MouseButtonEventArgs e)
{
2026-04-11 13:42:33 +08:00
if (!IsEnabled) return;
2026-04-11 12:06:53 +08:00
if (_lastPressedButton == this)
{
ButtonPanel.Background = Brushes.Transparent;
_lastPressedButton = null;
}
2026-04-17 01:20:06 +08:00
ButtonMouseUp?.Invoke(this, e);
2026-04-11 12:06:53 +08:00
}
}
2026-04-13 13:01:14 +08:00
}