From ddcaa9483ddb01526d2725ff3560745456426452 Mon Sep 17 00:00:00 2001 From: rabbitism Date: Wed, 2 Apr 2025 00:48:47 +0800 Subject: [PATCH] feat: use thumb in titlebar. --- .../Controls/UrsaWindow.axaml | 5 +- src/Ursa/Controls/TitleBar/TitleBar.cs | 52 ++----------------- src/Ursa/Controls/TitleBar/WindowThumb.cs | 38 ++++++++------ 3 files changed, 28 insertions(+), 67 deletions(-) diff --git a/src/Ursa.Themes.Semi/Controls/UrsaWindow.axaml b/src/Ursa.Themes.Semi/Controls/UrsaWindow.axaml index d828093..3614387 100644 --- a/src/Ursa.Themes.Semi/Controls/UrsaWindow.axaml +++ b/src/Ursa.Themes.Semi/Controls/UrsaWindow.axaml @@ -107,8 +107,7 @@ - @@ -125,7 +124,7 @@ Content="{TemplateBinding RightContent}" IsVisible="{TemplateBinding IsTitleVisible}" /> ("PART_CaptionButtons"); - this._background = e.NameScope.Get("PART_Background"); - DoubleTappedEvent.AddHandler(OnDoubleTapped, _background); - PointerPressedEvent.AddHandler(OnPointerPressed, _background); + this._captionButtons = e.NameScope.Get(PART_CaptionButtons); this._captionButtons?.Attach(_visualRoot); } @@ -78,48 +76,6 @@ public class TitleBar: ContentControl } } - private void OnPointerPressed(object? sender, PointerPressedEventArgs e) - { - if(_visualRoot is not null && _visualRoot.WindowState == WindowState.FullScreen) - { - return; - } - if (e.GetCurrentPoint(this).Properties.IsLeftButtonPressed) - { - if (e.ClickCount < 2) - { - _visualRoot?.BeginMoveDrag(e); - } - } - } - - private void OnDoubleTapped(object? sender, TappedEventArgs e) - { - if (_visualRoot is null) return; - if (!_visualRoot.CanResize) return; - if ( _visualRoot.WindowState == WindowState.FullScreen) return; - _visualRoot.WindowState = _visualRoot.WindowState == WindowState.Maximized ? WindowState.Normal : WindowState.Maximized; - } - - private void UpdateSize(Window window) - { - Thickness offScreenMargin = window.OffScreenMargin; - var left = offScreenMargin.Left; - offScreenMargin = window.OffScreenMargin; - double top = offScreenMargin.Top; - offScreenMargin = window.OffScreenMargin; - double right = offScreenMargin.Right; - offScreenMargin = window.OffScreenMargin; - double bottom = offScreenMargin.Bottom; - this.Margin = new Thickness(left, top, right, bottom); - if (window.WindowState != WindowState.FullScreen) - { - this.Height = window.WindowDecorationMargin.Top; - if (this._captionButtons != null) - this._captionButtons.Height = this.Height; - } - } - protected override void OnDetachedFromVisualTree(VisualTreeAttachmentEventArgs e) { base.OnDetachedFromVisualTree(e); diff --git a/src/Ursa/Controls/TitleBar/WindowThumb.cs b/src/Ursa/Controls/TitleBar/WindowThumb.cs index 04caee2..60a3e1c 100644 --- a/src/Ursa/Controls/TitleBar/WindowThumb.cs +++ b/src/Ursa/Controls/TitleBar/WindowThumb.cs @@ -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 BackgroundProperty = AvaloniaProperty.Register( - nameof(Background), Brushes.Transparent); + public static readonly StyledProperty BackgroundProperty = + TemplatedControl.BackgroundProperty.AddOwner(); public IBrush? Background { get => GetValue(BackgroundProperty); set => SetValue(BackgroundProperty, value); } - static WindowThumb() { IsHitTestVisibleProperty.OverrideDefaultValue(true); - HorizontalAlignmentProperty.OverrideDefaultValue(Avalonia.Layout.HorizontalAlignment.Stretch); - VerticalAlignmentProperty.OverrideDefaultValue(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)); } } } \ No newline at end of file