Browse Source

Merge pull request #5555 from MarchingCube/captionbuttons-api

Allow for controlling behavior of CaptionButtons.
pull/5737/head
Max Katz 6 years ago
committed by GitHub
parent
commit
e50d4ad40f
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
  1. 73
      src/Avalonia.Controls/Chrome/CaptionButtons.cs

73
src/Avalonia.Controls/Chrome/CaptionButtons.cs

@ -14,17 +14,21 @@ namespace Avalonia.Controls.Chrome
public class CaptionButtons : TemplatedControl public class CaptionButtons : TemplatedControl
{ {
private CompositeDisposable? _disposables; private CompositeDisposable? _disposables;
private Window? _hostWindow;
public void Attach(Window hostWindow) /// <summary>
/// Currently attached window.
/// </summary>
protected Window? HostWindow { get; private set; }
public virtual void Attach(Window hostWindow)
{ {
if (_disposables == null) if (_disposables == null)
{ {
_hostWindow = hostWindow; HostWindow = hostWindow;
_disposables = new CompositeDisposable _disposables = new CompositeDisposable
{ {
_hostWindow.GetObservable(Window.WindowStateProperty) HostWindow.GetObservable(Window.WindowStateProperty)
.Subscribe(x => .Subscribe(x =>
{ {
PseudoClasses.Set(":minimized", x == WindowState.Minimized); PseudoClasses.Set(":minimized", x == WindowState.Minimized);
@ -36,14 +40,45 @@ namespace Avalonia.Controls.Chrome
} }
} }
public void Detach() public virtual void Detach()
{ {
if (_disposables != null) if (_disposables != null)
{ {
_disposables.Dispose(); _disposables.Dispose();
_disposables = null; _disposables = null;
_hostWindow = null; HostWindow = null;
}
}
protected virtual void OnClose()
{
HostWindow?.Close();
}
protected virtual void OnRestore()
{
if (HostWindow != null)
{
HostWindow.WindowState = HostWindow.WindowState == WindowState.Maximized ? WindowState.Normal : WindowState.Maximized;
}
}
protected virtual void OnMinimize()
{
if (HostWindow != null)
{
HostWindow.WindowState = WindowState.Minimized;
}
}
protected virtual void OnToggleFullScreen()
{
if (HostWindow != null)
{
HostWindow.WindowState = HostWindow.WindowState == WindowState.FullScreen
? WindowState.Normal
: WindowState.FullScreen;
} }
} }
@ -56,31 +91,13 @@ namespace Avalonia.Controls.Chrome
var minimiseButton = e.NameScope.Get<Panel>("PART_MinimiseButton"); var minimiseButton = e.NameScope.Get<Panel>("PART_MinimiseButton");
var fullScreenButton = e.NameScope.Get<Panel>("PART_FullScreenButton"); var fullScreenButton = e.NameScope.Get<Panel>("PART_FullScreenButton");
closeButton.PointerReleased += (sender, e) => _hostWindow?.Close(); closeButton.PointerReleased += (sender, e) => OnClose();
restoreButton.PointerReleased += (sender, e) => restoreButton.PointerReleased += (sender, e) => OnRestore();
{
if (_hostWindow != null)
{
_hostWindow.WindowState = _hostWindow.WindowState == WindowState.Maximized ? WindowState.Normal : WindowState.Maximized;
}
};
minimiseButton.PointerReleased += (sender, e) => minimiseButton.PointerReleased += (sender, e) => OnMinimize();
{
if (_hostWindow != null)
{
_hostWindow.WindowState = WindowState.Minimized;
}
};
fullScreenButton.PointerReleased += (sender, e) => fullScreenButton.PointerReleased += (sender, e) => OnToggleFullScreen();
{
if (_hostWindow != null)
{
_hostWindow.WindowState = _hostWindow.WindowState == WindowState.FullScreen ? WindowState.Normal : WindowState.FullScreen;
}
};
} }
} }
} }

Loading…
Cancel
Save