This commit is contained in:
PrefacedCorg
2026-04-15 14:15:05 +08:00
parent 8298f7d5bb
commit 4b8d89854e
4 changed files with 256 additions and 163 deletions
@@ -0,0 +1,40 @@
<UserControl x:Class="Ink_Canvas.Controls.BoardToolbarButton"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:ikw="http://schemas.inkore.net/lib/ui/wpf"
mc:Ignorable="d"
d:DesignHeight="50" d:DesignWidth="60">
<Border x:Name="ButtonBorder"
Width="60" Height="50"
MouseDown="ButtonBorder_MouseDown"
MouseUp="ButtonBorder_MouseUp"
Background="{DynamicResource BoardFloatBarBackground}"
Opacity="1"
BorderThickness="1"
BorderBrush="{DynamicResource BoardFloatBarBorderBrush}">
<Grid Margin="6,6,6,4">
<Image x:Name="ButtonImage"
VerticalAlignment="Top"
RenderOptions.BitmapScalingMode="HighQuality"
Height="20" Width="20">
<Image.Source>
<DrawingImage>
<DrawingImage.Drawing>
<DrawingGroup ClipGeometry="M0,0 V24 H24 V0 H0 Z">
<GeometryDrawing x:Name="IconGeometryInternal"
Brush="{DynamicResource IconForeground}" />
</DrawingGroup>
</DrawingImage.Drawing>
</DrawingImage>
</Image.Source>
</Image>
<TextBlock x:Name="LabelTextBlock"
Foreground="{DynamicResource FloatBarForeground}"
VerticalAlignment="Bottom"
HorizontalAlignment="Center"
FontSize="12" />
</Grid>
</Border>
</UserControl>
@@ -0,0 +1,156 @@
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media;
namespace Ink_Canvas.Controls
{
public enum ButtonPosition
{
First,
Middle,
Last,
Single
}
public partial class BoardToolbarButton : UserControl
{
public static readonly DependencyProperty LabelProperty = DependencyProperty.Register(
nameof(Label), typeof(string), typeof(BoardToolbarButton),
new PropertyMetadata(string.Empty, OnLabelChanged));
private static void OnLabelChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
var button = (BoardToolbarButton)d;
button.LabelTextBlock.Text = (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(BoardToolbarButton),
new PropertyMetadata(string.Empty, OnIconGeometryChanged));
private static void OnIconGeometryChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
var button = (BoardToolbarButton)d;
if (e.NewValue is string geometry && !string.IsNullOrEmpty(geometry))
{
button.IconGeometryInternal.Geometry = Geometry.Parse(geometry);
}
}
public string IconGeometry
{
get => (string)GetValue(IconGeometryProperty);
set => SetValue(IconGeometryProperty, value);
}
public static readonly DependencyProperty PositionProperty = DependencyProperty.Register(
nameof(Position), typeof(ButtonPosition), typeof(BoardToolbarButton),
new PropertyMetadata(ButtonPosition.Middle, OnPositionChanged));
private static void OnPositionChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
var button = (BoardToolbarButton)d;
button.UpdateCornerRadius((ButtonPosition)e.NewValue);
}
public ButtonPosition Position
{
get => (ButtonPosition)GetValue(PositionProperty);
set => SetValue(PositionProperty, value);
}
public static readonly DependencyProperty IconBrushProperty = DependencyProperty.Register(
nameof(IconBrush), typeof(Brush), typeof(BoardToolbarButton),
new PropertyMetadata(null, OnIconBrushChanged));
private static void OnIconBrushChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
var button = (BoardToolbarButton)d;
if (e.NewValue is Brush brush)
{
button.IconGeometryInternal.Brush = brush;
}
}
public Brush IconBrush
{
get => (Brush)GetValue(IconBrushProperty);
set => SetValue(IconBrushProperty, value);
}
public event MouseButtonEventHandler ButtonMouseDown;
public event MouseButtonEventHandler ButtonMouseUp;
public GeometryDrawing IconGeometryDrawing => IconGeometryInternal;
public Border ButtonBorderControl => ButtonBorder;
public TextBlock LabelTextBlockControl => LabelTextBlock;
public new Brush Background
{
get => ButtonBorder.Background;
set => ButtonBorder.Background = value;
}
public new Brush BorderBrush
{
get => ButtonBorder.BorderBrush;
set => ButtonBorder.BorderBrush = value;
}
public new Brush Foreground
{
get => LabelTextBlock.Foreground;
set => LabelTextBlock.Foreground = value;
}
public BoardToolbarButton()
{
InitializeComponent();
UpdateCornerRadius(Position);
}
private void UpdateCornerRadius(ButtonPosition position)
{
if (ButtonBorder == null) return;
switch (position)
{
case ButtonPosition.First:
ButtonBorder.CornerRadius = new CornerRadius(5, 0, 0, 5);
ButtonBorder.BorderThickness = new Thickness(1, 1, 0, 1);
break;
case ButtonPosition.Middle:
ButtonBorder.CornerRadius = new CornerRadius(0);
ButtonBorder.BorderThickness = new Thickness(0, 1, 0, 1);
break;
case ButtonPosition.Last:
ButtonBorder.CornerRadius = new CornerRadius(0, 5, 5, 0);
ButtonBorder.BorderThickness = new Thickness(0, 1, 1, 1);
break;
case ButtonPosition.Single:
ButtonBorder.CornerRadius = new CornerRadius(5);
ButtonBorder.BorderThickness = new Thickness(1);
break;
}
}
private void ButtonBorder_MouseDown(object sender, MouseButtonEventArgs e)
{
ButtonMouseDown?.Invoke(this, e);
}
private void ButtonBorder_MouseUp(object sender, MouseButtonEventArgs e)
{
ButtonMouseUp?.Invoke(this, e);
}
}
}