using System.Collections.Specialized; using System.Windows; using System.Windows.Controls; using System.Windows.Input; using System.Windows.Media; using System.Windows.Threading; using Titano.App.Infrastructure; using Titano.Ui.Controls; namespace Titano.App.Shell; /// /// The client's window (BRIEF §12.1): rail, job bar, section, status strip, the first-run dialog and the toasts, which /// also become Windows notifications while nobody sees the window (). /// public partial class MainWindow : TitanoWindow { private readonly ShellViewModel _shell; private readonly OsNotifications _notifications; private WindowState? _restoreTo; public MainWindow(ShellViewModel shell) { _shell = shell; DataContext = shell; InitializeComponent(); UpdateConnection(); shell.PropertyChanged += (_, e) => { if (e.PropertyName is nameof(ShellViewModel.IsConnected) or "") UpdateConnection(); if (e.PropertyName is nameof(ShellViewModel.IsWelcomeOpen) or "") FocusWelcome(); }; // Screenshots and tests run off screen and never activated: they must not reach the notification area. _notifications = new OsNotifications(this, () => shell.Services.Settings.OsNotifications && !shell.Services.Environment.IsCapture, t => Toasts.Dismiss(t)); shell.PendingToasts.CollectionChanged += OnToastRequested; Loaded += (_, _) => FocusWelcome(); RestorePlacement(); Closing += (_, _) => SavePlacement(); Closed += (_, _) => _notifications.Dispose(); } /// Started at sign-in (Settings › General › Startup): the window waits minimised on the taskbar. internal static bool StartsMinimized(IReadOnlyList arguments) => arguments.Any(a => a.Equals(RunKeyStartWithWindows.MinimizedOption, StringComparison.OrdinalIgnoreCase)); /// Minimised until the user opens it; that first restore brings back the saved state (maximised or not). internal void WaitMinimized(bool maximized) { WindowState = WindowState.Minimized; _restoreTo = maximized ? WindowState.Maximized : WindowState.Normal; } /// The maximised flag to save: a window started minimised and never opened keeps the state it would come back to. internal static bool SavesMaximized(WindowState state, WindowState? restoreTo) => state == WindowState.Maximized || (state == WindowState.Minimized && restoreTo == WindowState.Maximized); /// The first restore of a window started minimised brings back the saved maximised state. protected override void OnStateChanged(EventArgs e) { base.OnStateChanged(e); if (_restoreTo is not { } state || WindowState == WindowState.Minimized) return; _restoreTo = null; if (WindowState != state) WindowState = state; } /// F6 moves the keyboard to the newest notification (as in Windows apps), Esc there dismisses it. protected override void OnKeyDown(KeyEventArgs e) { base.OnKeyDown(e); if (!e.Handled && e.Key == Key.F6 && Keyboard.Modifiers == ModifierKeys.None) e.Handled = Toasts.FocusNewest(); } private void UpdateConnection() => Connection.SetResourceReference(TextBlock.ForegroundProperty, _shell.IsConnected ? "Brush.Accent" : "Brush.TextMuted"); /// The first-run dialog takes the keyboard on its chosen option once it is on screen. private void FocusWelcome() { if (!_shell.IsWelcomeOpen) return; Dispatcher.BeginInvoke(DispatcherPriority.Loaded, () => { if (!_shell.IsWelcomeOpen || !IsLoaded) return; if (FindChecked(WelcomeOverlay) is { } choice) choice.Focus(); else WelcomeOverlay.MoveFocus(new TraversalRequest(FocusNavigationDirection.First)); }); } private static RadioButton? FindChecked(DependencyObject root) { for (var i = 0; i < VisualTreeHelper.GetChildrenCount(root); i++) { var child = VisualTreeHelper.GetChild(root, i); if (child is RadioButton { IsChecked: true } radio) return radio; if (FindChecked(child) is { } nested) return nested; } return null; } private void OnToastRequested(object? sender, NotifyCollectionChangedEventArgs e) { if (e.Action != NotifyCollectionChangedAction.Add || e.NewItems is null) return; foreach (Toast t in e.NewItems) { Toasts.Show(t); _notifications.Show(t); } Dispatcher.BeginInvoke(() => _shell.PendingToasts.Clear()); } private void RestorePlacement() { if (_shell.Services.Environment.IsCapture) return; var p = _shell.Services.Settings.Window; var maximized = p?.Maximized == true; if (StartsMinimized(Environment.GetCommandLineArgs())) WaitMinimized(maximized); if (p is null || p.Width < MinWidth || p.Height < MinHeight) return; var virtualScreen = new Rect(SystemParameters.VirtualScreenLeft, SystemParameters.VirtualScreenTop, SystemParameters.VirtualScreenWidth, SystemParameters.VirtualScreenHeight); var rect = new Rect(p.Left, p.Top, p.Width, p.Height); if (!virtualScreen.IntersectsWith(rect)) return; WindowStartupLocation = WindowStartupLocation.Manual; Left = p.Left; Top = p.Top; Width = p.Width; Height = p.Height; if (maximized && _restoreTo is null) WindowState = WindowState.Maximized; } private void SavePlacement() { if (_shell.Services.Environment.IsCapture) return; var b = RestoreBounds; _shell.Services.Settings.Window = new AppSettings.WindowPlacement { Left = b.Left, Top = b.Top, Width = b.Width, Height = b.Height, Maximized = SavesMaximized(WindowState, _restoreTo), }; } }