diff --git a/src/Ursa/Controls/TitleBar/CaptionButtons.cs b/src/Ursa/Controls/TitleBar/CaptionButtons.cs index 1ab7f738..3d8f19d4 100644 --- a/src/Ursa/Controls/TitleBar/CaptionButtons.cs +++ b/src/Ursa/Controls/TitleBar/CaptionButtons.cs @@ -75,8 +75,9 @@ protected override void OnToggleFullScreen() } } } - public override void Attach(Window hostWindow) + public override void Attach(Window? hostWindow) { + if (hostWindow is null) return; base.Attach(hostWindow); _windowStateSubscription = HostWindow?.GetObservable(Window.WindowStateProperty).Subscribe(_ => { diff --git a/src/Ursa/Controls/TitleBar/TitleBar.cs b/src/Ursa/Controls/TitleBar/TitleBar.cs index 24b6674f..8e0165c5 100644 --- a/src/Ursa/Controls/TitleBar/TitleBar.cs +++ b/src/Ursa/Controls/TitleBar/TitleBar.cs @@ -1,16 +1,20 @@ using Avalonia; using Avalonia.Controls; +using Avalonia.Controls.Metadata; using Avalonia.Controls.Primitives; using Avalonia.Input; +using Irihi.Avalonia.Shared.Common; using Irihi.Avalonia.Shared.Helpers; namespace Ursa.Controls; +[PseudoClasses(PseudoClassName.PC_Active)] public class TitleBar: ContentControl { private CaptionButtons? _captionButtons; private InputElement? _background; private Window? _visualRoot; + private IDisposable? _activeSubscription; public static readonly StyledProperty LeftContentProperty = AvaloniaProperty.Register( nameof(LeftContent)); @@ -45,19 +49,27 @@ protected override void OnApplyTemplate(TemplateAppliedEventArgs e) this._captionButtons?.Detach(); this._captionButtons = e.NameScope.Get("PART_CaptionButtons"); this._background = e.NameScope.Get("PART_Background"); - if (!(this.VisualRoot is Window visualRoot)) - return; - _visualRoot = visualRoot; DoubleTappedEvent.AddHandler(OnDoubleTapped, _background); PointerPressedEvent.AddHandler(OnPointerPressed, _background); - this._captionButtons?.Attach(visualRoot); - // this.UpdateSize(visualRoot); + this._captionButtons?.Attach(_visualRoot); + } + + protected override void OnAttachedToVisualTree(VisualTreeAttachmentEventArgs e) + { + base.OnAttachedToVisualTree(e); + _visualRoot = this.VisualRoot as Window; + if (_visualRoot is not null) + { + _activeSubscription = _visualRoot.GetObservable(WindowBase.IsActiveProperty).Subscribe(isActive => + { + PseudoClasses.Set(PseudoClassName.PC_Active, isActive); + }); + } } private void OnPointerPressed(object? sender, PointerPressedEventArgs e) { - if(_visualRoot is not null - && _visualRoot.WindowState == WindowState.FullScreen) + if(_visualRoot is not null && _visualRoot.WindowState == WindowState.FullScreen) { return; } @@ -96,4 +108,11 @@ private void UpdateSize(Window window) this._captionButtons.Height = this.Height; } } + + protected override void OnDetachedFromVisualTree(VisualTreeAttachmentEventArgs e) + { + base.OnDetachedFromVisualTree(e); + _captionButtons?.Detach(); + _activeSubscription?.Dispose(); + } } \ No newline at end of file