using Avalonia.Controls; using Avalonia.Input; using Avalonia.Interactivity; namespace DodoSSH.Client.App.Views; /// /// The window's own titlebar, drawn because the design draws one. /// /// /// /// Everything here is a window operation, which is why it is code-behind and not a command on a view model: /// dragging, maximising and closing are properties of the this control happens to be /// inside, and a view model that knew about them would be a view model that could not be tested without one. /// /// /// The window is found by walking up rather than injected, so this control drops into any window — including /// the bare one the layout harness hosts it in, where is simply a different window and /// every handler still has something to act on. /// /// internal sealed partial class TitleBar : UserControl { public TitleBar() => InitializeComponent(); private Window? Host => TopLevel.GetTopLevel(this) as Window; /// /// Left button only, and only on a press that has not already been handled by something inside the bar — /// otherwise dragging would start from the close button and swallow the click that was meant to close /// the window. /// private void OnDrag(object? sender, PointerPressedEventArgs e) { if (e.Handled || !e.GetCurrentPoint(this).Properties.IsLeftButtonPressed) { return; } Host?.BeginMoveDrag(e); } private void OnMinimise(object? sender, RoutedEventArgs e) { if (Host is { } window) { window.WindowState = WindowState.Minimized; } } /// /// Both the button and a double-click on the bar arrive here, which is the convention Windows sets and /// the one people reach for without thinking about it. /// private void OnToggleMaximised(object? sender, RoutedEventArgs e) { if (Host is not { } window) { return; } window.WindowState = window.WindowState == WindowState.Maximized ? WindowState.Normal : WindowState.Maximized; } private void OnClose(object? sender, RoutedEventArgs e) => Host?.Close(); }