feat: use thumb in titlebar.

This commit is contained in:
rabbitism
2025-04-02 00:48:47 +08:00
parent 13891062c7
commit ddcaa9483d
3 changed files with 28 additions and 67 deletions

View File

@@ -1,5 +1,6 @@
using Avalonia;
using Avalonia.Controls;
using Avalonia.Controls.Primitives;
using Avalonia.Input;
using Avalonia.Interactivity;
using Avalonia.Media;
@@ -8,44 +9,49 @@ namespace Ursa.Controls;
public class WindowThumb: Control
{
public static readonly StyledProperty<IBrush?> BackgroundProperty = AvaloniaProperty.Register<WindowThumb, IBrush?>(
nameof(Background), Brushes.Transparent);
public static readonly StyledProperty<IBrush?> BackgroundProperty =
TemplatedControl.BackgroundProperty.AddOwner<WindowThumb>();
public IBrush? Background
{
get => GetValue(BackgroundProperty);
set => SetValue(BackgroundProperty, value);
}
static WindowThumb()
{
IsHitTestVisibleProperty.OverrideDefaultValue<WindowThumb>(true);
HorizontalAlignmentProperty.OverrideDefaultValue<WindowThumb>(Avalonia.Layout.HorizontalAlignment.Stretch);
VerticalAlignmentProperty.OverrideDefaultValue<WindowThumb>(Avalonia.Layout.VerticalAlignment.Stretch);
}
public WindowThumb()
{
this.AddHandler(PointerPressedEvent, OnPressed, RoutingStrategies.Direct);
this.AddHandler(DoubleTappedEvent, OnTapped, RoutingStrategies.Direct);
this.AddHandler(PointerPressedEvent, OnPressed);
this.AddHandler(DoubleTappedEvent, OnTapped);
}
private void OnTapped(object sender, TappedEventArgs e)
{
if (this.VisualRoot is not Window visualRoot) return;
if (!visualRoot.CanResize) return;
if ( visualRoot.WindowState == WindowState.FullScreen) return;
visualRoot.WindowState = visualRoot.WindowState == WindowState.Maximized ? WindowState.Normal : WindowState.Maximized;
if (this.VisualRoot is not Window window) return;
if (!window.CanResize) return;
if ( window.WindowState == WindowState.FullScreen) return;
window.WindowState = window.WindowState == WindowState.Maximized ? WindowState.Normal : WindowState.Maximized;
}
private void OnPressed(object sender, PointerPressedEventArgs e)
{
if (e.GetCurrentPoint(this).Properties.IsLeftButtonPressed)
if (!e.GetCurrentPoint(this).Properties.IsLeftButtonPressed) return;
if (e.ClickCount > 1) return;
if (VisualRoot is Window window)
{
if (VisualRoot is Window window)
{
window.BeginMoveDrag(e);
}
window.BeginMoveDrag(e);
}
}
public override void Render(DrawingContext context)
{
base.Render(context);
if(Background is not null)
{
context.FillRectangle(Background, new Rect(Bounds.Size));
}
}
}