Add resize functionality and improve dialog controls

This commit introduces the ability to resize dialogs by adding `CanResize` properties to dialog options and control classes. It also refines dialog controls' behavior and layout, ensuring consistent resizing capabilities across different dialog types. Additionally, it enhances the overlay feedback element's positioning logic and updates the resizer's appearance and visibility handling.
This commit is contained in:
rabbitism
2024-09-17 22:44:35 +08:00
parent a70f7205e9
commit c512cb6e13
11 changed files with 278 additions and 168 deletions

View File

@@ -1,10 +1,30 @@
using Avalonia;
using Avalonia.Controls;
using Avalonia.Controls.Primitives;
using Irihi.Avalonia.Shared.Helpers;
namespace Ursa.Controls;
public class DialogResizer: TemplatedControl
{
public const string PART_Top = "PART_Top";
public const string PART_Bottom = "PART_Bottom";
public const string PART_Left = "PART_Left";
public const string PART_Right = "PART_Right";
public const string PART_TopLeft = "PART_TopLeft";
public const string PART_TopRight = "PART_TopRight";
public const string PART_BottomLeft = "PART_BottomLeft";
public const string PART_BottomRight = "PART_BottomRight";
private Thumb? _top;
private Thumb? _bottom;
private Thumb? _left;
private Thumb? _right;
private Thumb? _topLeft;
private Thumb? _topRight;
private Thumb? _bottomLeft;
private Thumb? _bottomRight;
public static readonly StyledProperty<ResizeDirection> ResizeDirectionProperty = AvaloniaProperty.Register<DialogResizer, ResizeDirection>(
nameof(ResizeDirection));
@@ -16,4 +36,39 @@ public class DialogResizer: TemplatedControl
get => GetValue(ResizeDirectionProperty);
set => SetValue(ResizeDirectionProperty, value);
}
static DialogResizer()
{
ResizeDirectionProperty.Changed.AddClassHandler<DialogResizer, ResizeDirection>((resizer, e) => resizer.OnResizeDirectionChanged(e));
}
private void OnResizeDirectionChanged(AvaloniaPropertyChangedEventArgs<ResizeDirection> args)
{
UpdateThumbVisibility(args.NewValue.Value);
}
protected override void OnApplyTemplate(TemplateAppliedEventArgs e)
{
base.OnApplyTemplate(e);
_top = e.NameScope.Find<Thumb>(PART_Top);
_bottom = e.NameScope.Find<Thumb>(PART_Bottom);
_left = e.NameScope.Find<Thumb>(PART_Left);
_right = e.NameScope.Find<Thumb>(PART_Right);
_topLeft = e.NameScope.Find<Thumb>(PART_TopLeft);
_topRight = e.NameScope.Find<Thumb>(PART_TopRight);
_bottomLeft = e.NameScope.Find<Thumb>(PART_BottomLeft);
_bottomRight = e.NameScope.Find<Thumb>(PART_BottomRight);
}
private void UpdateThumbVisibility(ResizeDirection direction)
{
IsVisibleProperty.SetValue(direction.HasFlag(ResizeDirection.Top), _top);
IsVisibleProperty.SetValue(direction.HasFlag(ResizeDirection.Bottom), _bottom);
IsVisibleProperty.SetValue(direction.HasFlag(ResizeDirection.Left), _left);
IsVisibleProperty.SetValue(direction.HasFlag(ResizeDirection.Right), _right);
IsVisibleProperty.SetValue(direction.HasFlag(ResizeDirection.TopLeft), _topLeft);
IsVisibleProperty.SetValue(direction.HasFlag(ResizeDirection.TopRight), _topRight);
IsVisibleProperty.SetValue(direction.HasFlag(ResizeDirection.BottomLeft), _bottomLeft);
IsVisibleProperty.SetValue(direction.HasFlag(ResizeDirection.BottomRight), _bottomRight);
}
}

View File

@@ -3,12 +3,16 @@ namespace Ursa.Controls;
[Flags]
public enum ResizeDirection
{
Top,
Bottom,
Left,
Right,
TopLeft,
TopRight,
BottomLeft,
BottomRight,
Top = 1,
Bottom = 2,
Left = 4,
Right = 8,
TopLeft = 16,
TopRight = 32,
BottomLeft = 64,
BottomRight = 128,
Sides = Top | Bottom | Left | Right,
Corners = TopLeft | TopRight | BottomLeft | BottomRight,
All = Sides | Corners,
}