Merge pull request #785 from irihitech/fix-784
Fix UrsaWindow memory leakage.
This commit is contained in:
@@ -33,6 +33,7 @@ public class CaptionButtons: Avalonia.Controls.Chrome.CaptionButtons
|
|||||||
/// 切换进入全屏前 窗口的状态
|
/// 切换进入全屏前 窗口的状态
|
||||||
/// </summary>
|
/// </summary>
|
||||||
private WindowState? _oldWindowState;
|
private WindowState? _oldWindowState;
|
||||||
|
|
||||||
protected override void OnApplyTemplate(TemplateAppliedEventArgs e)
|
protected override void OnApplyTemplate(TemplateAppliedEventArgs e)
|
||||||
{
|
{
|
||||||
_closeButton = e.NameScope.Get<Button>(PART_CloseButton);
|
_closeButton = e.NameScope.Get<Button>(PART_CloseButton);
|
||||||
@@ -44,23 +45,13 @@ public class CaptionButtons: Avalonia.Controls.Chrome.CaptionButtons
|
|||||||
Button.ClickEvent.AddHandler((_, _) => OnMinimize(), _minimizeButton);
|
Button.ClickEvent.AddHandler((_, _) => OnMinimize(), _minimizeButton);
|
||||||
Button.ClickEvent.AddHandler((_, _) => OnToggleFullScreen(), _fullScreenButton);
|
Button.ClickEvent.AddHandler((_, _) => OnToggleFullScreen(), _fullScreenButton);
|
||||||
|
|
||||||
Window.WindowStateProperty.Changed.AddClassHandler<Window, WindowState>(WindowStateChanged);
|
if (HostWindow is not null && !HostWindow.CanResize)
|
||||||
if (this.HostWindow is not null && !HostWindow.CanResize)
|
|
||||||
{
|
{
|
||||||
_restoreButton.IsEnabled = false;
|
_restoreButton.IsEnabled = false;
|
||||||
}
|
}
|
||||||
UpdateVisibility();
|
UpdateVisibility();
|
||||||
}
|
}
|
||||||
|
|
||||||
private void WindowStateChanged(Window window, AvaloniaPropertyChangedEventArgs<WindowState> e)
|
|
||||||
{
|
|
||||||
if (window != HostWindow) return;
|
|
||||||
if (e.NewValue.Value == WindowState.FullScreen)
|
|
||||||
{
|
|
||||||
_oldWindowState = e.OldValue.Value;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
protected override void OnToggleFullScreen()
|
protected override void OnToggleFullScreen()
|
||||||
{
|
{
|
||||||
if (HostWindow != null)
|
if (HostWindow != null)
|
||||||
@@ -71,7 +62,7 @@ public class CaptionButtons: Avalonia.Controls.Chrome.CaptionButtons
|
|||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
HostWindow.WindowState = _oldWindowState.HasValue ? _oldWindowState.Value : WindowState.Normal;
|
HostWindow.WindowState = _oldWindowState ?? WindowState.Normal;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -79,17 +70,23 @@ public class CaptionButtons: Avalonia.Controls.Chrome.CaptionButtons
|
|||||||
{
|
{
|
||||||
if (hostWindow is null) return;
|
if (hostWindow is null) return;
|
||||||
base.Attach(hostWindow);
|
base.Attach(hostWindow);
|
||||||
_windowStateSubscription = HostWindow?.GetObservable(Window.WindowStateProperty).Subscribe(_ =>
|
_windowStateSubscription = HostWindow?.GetPropertyChangedObservable(Window.WindowStateProperty).Subscribe(OnHostWindowStateChanged);
|
||||||
{
|
Action<bool> a = _ => UpdateVisibility();
|
||||||
UpdateVisibility();
|
|
||||||
});
|
|
||||||
Action<bool> a = (_) => UpdateVisibility();
|
|
||||||
_fullScreenSubscription = HostWindow?.GetObservable(UrsaWindow.IsFullScreenButtonVisibleProperty).Subscribe(a);
|
_fullScreenSubscription = HostWindow?.GetObservable(UrsaWindow.IsFullScreenButtonVisibleProperty).Subscribe(a);
|
||||||
_minimizeSubscription = HostWindow?.GetObservable(UrsaWindow.IsMinimizeButtonVisibleProperty).Subscribe(a);
|
_minimizeSubscription = HostWindow?.GetObservable(UrsaWindow.IsMinimizeButtonVisibleProperty).Subscribe(a);
|
||||||
_restoreSubscription = HostWindow?.GetObservable(UrsaWindow.IsRestoreButtonVisibleProperty).Subscribe(a);
|
_restoreSubscription = HostWindow?.GetObservable(UrsaWindow.IsRestoreButtonVisibleProperty).Subscribe(a);
|
||||||
_closeSubscription = HostWindow?.GetObservable(UrsaWindow.IsCloseButtonVisibleProperty).Subscribe(a);
|
_closeSubscription = HostWindow?.GetObservable(UrsaWindow.IsCloseButtonVisibleProperty).Subscribe(a);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void OnHostWindowStateChanged(AvaloniaPropertyChangedEventArgs e)
|
||||||
|
{
|
||||||
|
UpdateVisibility();
|
||||||
|
if (e.GetNewValue<WindowState>() == WindowState.FullScreen)
|
||||||
|
{
|
||||||
|
_oldWindowState = e.GetOldValue<WindowState>();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
private void UpdateVisibility()
|
private void UpdateVisibility()
|
||||||
{
|
{
|
||||||
if (HostWindow is not UrsaWindow u)
|
if (HostWindow is not UrsaWindow u)
|
||||||
|
|||||||
Reference in New Issue
Block a user