Skip to content

Commit

Permalink
feat: add active pseudo class. dispose caption buttons on detach.
Browse files Browse the repository at this point in the history
  • Loading branch information
rabbitism committed Dec 2, 2024
1 parent 9001322 commit 017ac58
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 8 deletions.
3 changes: 2 additions & 1 deletion src/Ursa/Controls/TitleBar/CaptionButtons.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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(_ =>
{
Expand Down
33 changes: 26 additions & 7 deletions src/Ursa/Controls/TitleBar/TitleBar.cs
Original file line number Diff line number Diff line change
@@ -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<object?> LeftContentProperty = AvaloniaProperty.Register<TitleBar, object?>(
nameof(LeftContent));
Expand Down Expand Up @@ -45,19 +49,27 @@ protected override void OnApplyTemplate(TemplateAppliedEventArgs e)
this._captionButtons?.Detach();
this._captionButtons = e.NameScope.Get<CaptionButtons>("PART_CaptionButtons");
this._background = e.NameScope.Get<InputElement>("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;
}
Expand Down Expand Up @@ -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();
}
}

0 comments on commit 017ac58

Please sign in to comment.