Enhance TitleBar and CaptionButtons to respect latest Window.CanMaximize and Window.CanMinimize (#815)
* fix: enhance window resizing logic to include maximization check * fix: update CaptionButtons to control button visibility based on window properties * fix: update obsolescence messages for minimize and restore button properties * chore: update comment.
This commit is contained in:
@@ -6,12 +6,12 @@ using Irihi.Avalonia.Shared.Helpers;
|
||||
|
||||
namespace Ursa.Controls;
|
||||
|
||||
[TemplatePart(PART_CloseButton, typeof (Button))]
|
||||
[TemplatePart(PART_RestoreButton, typeof (Button))]
|
||||
[TemplatePart(PART_MinimizeButton, typeof (Button))]
|
||||
[TemplatePart(PART_FullScreenButton, typeof (Button))]
|
||||
[TemplatePart(PART_CloseButton, typeof(Button))]
|
||||
[TemplatePart(PART_RestoreButton, typeof(Button))]
|
||||
[TemplatePart(PART_MinimizeButton, typeof(Button))]
|
||||
[TemplatePart(PART_FullScreenButton, typeof(Button))]
|
||||
[PseudoClasses(":minimized", ":normal", ":maximized", ":fullscreen")]
|
||||
public class CaptionButtons: Avalonia.Controls.Chrome.CaptionButtons
|
||||
public class CaptionButtons : Avalonia.Controls.Chrome.CaptionButtons
|
||||
{
|
||||
private const string PART_CloseButton = "PART_CloseButton";
|
||||
private const string PART_RestoreButton = "PART_RestoreButton";
|
||||
@@ -19,21 +19,15 @@ public class CaptionButtons: Avalonia.Controls.Chrome.CaptionButtons
|
||||
private const string PART_FullScreenButton = "PART_FullScreenButton";
|
||||
|
||||
private Button? _closeButton;
|
||||
private Button? _restoreButton;
|
||||
private Button? _minimizeButton;
|
||||
private Button? _fullScreenButton;
|
||||
|
||||
private IDisposable? _windowStateSubscription;
|
||||
private IDisposable? _fullScreenSubscription;
|
||||
private IDisposable? _minimizeSubscription;
|
||||
private IDisposable? _restoreSubscription;
|
||||
private IDisposable? _closeSubscription;
|
||||
private Button? _minimizeButton;
|
||||
private Button? _restoreButton;
|
||||
|
||||
/// <summary>
|
||||
/// 切换进入全屏前 窗口的状态
|
||||
/// Stores the previous window state before entering full-screen mode.
|
||||
/// </summary>
|
||||
private WindowState? _oldWindowState;
|
||||
|
||||
|
||||
protected override void OnApplyTemplate(TemplateAppliedEventArgs e)
|
||||
{
|
||||
_closeButton = e.NameScope.Get<Button>(PART_CloseButton);
|
||||
@@ -44,11 +38,9 @@ public class CaptionButtons: Avalonia.Controls.Chrome.CaptionButtons
|
||||
Button.ClickEvent.AddHandler((_, _) => OnRestore(), _restoreButton);
|
||||
Button.ClickEvent.AddHandler((_, _) => OnMinimize(), _minimizeButton);
|
||||
Button.ClickEvent.AddHandler((_, _) => OnToggleFullScreen(), _fullScreenButton);
|
||||
|
||||
if (HostWindow is not null && !HostWindow.CanResize)
|
||||
{
|
||||
|
||||
if (HostWindow is not null && (!HostWindow.CanResize || !HostWindow.CanMaximize))
|
||||
_restoreButton.IsEnabled = false;
|
||||
}
|
||||
UpdateVisibility();
|
||||
}
|
||||
|
||||
@@ -57,58 +49,59 @@ public class CaptionButtons: Avalonia.Controls.Chrome.CaptionButtons
|
||||
if (HostWindow != null)
|
||||
{
|
||||
if (HostWindow.WindowState != WindowState.FullScreen)
|
||||
{
|
||||
HostWindow.WindowState = WindowState.FullScreen;
|
||||
}
|
||||
else
|
||||
{
|
||||
HostWindow.WindowState = _oldWindowState ?? WindowState.Normal;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public override void Attach(Window? hostWindow)
|
||||
{
|
||||
if (hostWindow is null) return;
|
||||
base.Attach(hostWindow);
|
||||
_windowStateSubscription = HostWindow?.GetPropertyChangedObservable(Window.WindowStateProperty).Subscribe(OnHostWindowStateChanged);
|
||||
Action<bool> a = _ => UpdateVisibility();
|
||||
_fullScreenSubscription = HostWindow?.GetObservable(UrsaWindow.IsFullScreenButtonVisibleProperty).Subscribe(a);
|
||||
_minimizeSubscription = HostWindow?.GetObservable(UrsaWindow.IsMinimizeButtonVisibleProperty).Subscribe(a);
|
||||
_restoreSubscription = HostWindow?.GetObservable(UrsaWindow.IsRestoreButtonVisibleProperty).Subscribe(a);
|
||||
_closeSubscription = HostWindow?.GetObservable(UrsaWindow.IsCloseButtonVisibleProperty).Subscribe(a);
|
||||
if (HostWindow is not null) HostWindow.PropertyChanged += OnWindowPropertyChanged;
|
||||
}
|
||||
|
||||
private void OnHostWindowStateChanged(AvaloniaPropertyChangedEventArgs e)
|
||||
private void OnWindowPropertyChanged(object? sender, AvaloniaPropertyChangedEventArgs e)
|
||||
{
|
||||
UpdateVisibility();
|
||||
if (e.GetNewValue<WindowState>() == WindowState.FullScreen)
|
||||
if (e.Property == Window.WindowStateProperty)
|
||||
{
|
||||
_oldWindowState = e.GetOldValue<WindowState>();
|
||||
UpdateVisibility();
|
||||
if (e.GetNewValue<WindowState>() == WindowState.FullScreen) _oldWindowState = e.GetOldValue<WindowState>();
|
||||
}
|
||||
|
||||
if (e.Property == UrsaWindow.IsFullScreenButtonVisibleProperty
|
||||
|| e.Property == UrsaWindow.IsMinimizeButtonVisibleProperty
|
||||
|| e.Property == UrsaWindow.IsRestoreButtonVisibleProperty
|
||||
|| e.Property == UrsaWindow.IsCloseButtonVisibleProperty
|
||||
|| e.Property == Window.CanMaximizeProperty
|
||||
|| e.Property == Window.CanMinimizeProperty)
|
||||
UpdateVisibility();
|
||||
}
|
||||
|
||||
|
||||
private void UpdateVisibility()
|
||||
{
|
||||
if (HostWindow is not UrsaWindow u)
|
||||
if (HostWindow is UrsaWindow u)
|
||||
{
|
||||
return;
|
||||
IsVisibleProperty.SetValue(u.IsCloseButtonVisible, _closeButton);
|
||||
IsVisibleProperty.SetValue(
|
||||
u.CanMaximize && u.WindowState != WindowState.FullScreen && u.IsRestoreButtonVisible,
|
||||
_restoreButton);
|
||||
IsVisibleProperty.SetValue(
|
||||
u.CanMinimize && u.WindowState != WindowState.FullScreen && u.IsMinimizeButtonVisible,
|
||||
_minimizeButton);
|
||||
IsVisibleProperty.SetValue(u.IsFullScreenButtonVisible, _fullScreenButton);
|
||||
}
|
||||
else if (HostWindow is { } s)
|
||||
{
|
||||
IsVisibleProperty.SetValue(s.CanMaximize && s.WindowState != WindowState.FullScreen, _restoreButton);
|
||||
IsVisibleProperty.SetValue(s.CanMinimize && s.WindowState != WindowState.FullScreen, _minimizeButton);
|
||||
}
|
||||
|
||||
IsVisibleProperty.SetValue(u.IsCloseButtonVisible, _closeButton);
|
||||
IsVisibleProperty.SetValue(u.WindowState != WindowState.FullScreen && u.IsRestoreButtonVisible,
|
||||
_restoreButton);
|
||||
IsVisibleProperty.SetValue(u.WindowState != WindowState.FullScreen && u.IsMinimizeButtonVisible,
|
||||
_minimizeButton);
|
||||
IsVisibleProperty.SetValue(u.IsFullScreenButtonVisible, _fullScreenButton);
|
||||
}
|
||||
|
||||
public override void Detach()
|
||||
{
|
||||
if (HostWindow is not null) HostWindow.PropertyChanged -= OnWindowPropertyChanged;
|
||||
base.Detach();
|
||||
_windowStateSubscription?.Dispose();
|
||||
_fullScreenSubscription?.Dispose();
|
||||
_minimizeSubscription?.Dispose();
|
||||
_restoreSubscription?.Dispose();
|
||||
_closeSubscription?.Dispose();
|
||||
}
|
||||
}
|
||||
@@ -29,8 +29,8 @@ public class WindowThumb: Control
|
||||
|
||||
private void OnTapped(object? sender, TappedEventArgs e)
|
||||
{
|
||||
if (this.VisualRoot is not Window window) return;
|
||||
if (!window.CanResize) return;
|
||||
if (VisualRoot is not Window window) return;
|
||||
if (!window.CanResize || !window.CanMaximize) return;
|
||||
if ( window.WindowState == WindowState.FullScreen) return;
|
||||
window.WindowState = window.WindowState == WindowState.Maximized ? WindowState.Normal : WindowState.Maximized;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user