- Toast.RunsFromNotification: a click on a Windows notification restores the window and runs the toast's
action only when the toast opts in (card detected: Import; interrupted import: Resume; job finished: Open;
navigation to Import or Jobs) and it is the only notification shown since the icon appeared. Cancel, Undo
and Retry stay in the window, and the notification text no longer offers them.
- Job endings are coalesced: finished jobs of one kind wait until no job of that kind the user waits for is
queued or running, then one toast says how many finished ("12 jobs finished", action Jobs); failures of one
update share one toast. Server > Upload all no longer raises one toast and one notification per sequence.
- Every failure that is not upkeep (scan, thumbnails, proxies, light analysis) is announced, including move,
adopt and user downloads; finished moves and adoptions are announced too.
- Rail badges: Library counts the sequences with warnings (the Warnings filter), refreshed after analyses;
Archive counts the index health lines that need attention.
- Tests: the balloon click path, the --minimized restore to a saved maximised window, batch and failure
coalescing, and the new badges.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_012iLMBNwBE2GnphAsu4cjaC
142 lines
6.0 KiB
C#
142 lines
6.0 KiB
C#
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;
|
||
|
||
/// <summary>
|
||
/// 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 (<see cref="OsNotifications"/>).
|
||
/// </summary>
|
||
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();
|
||
}
|
||
|
||
/// <summary>Started at sign-in (Settings › General › Startup): the window waits minimised on the taskbar.</summary>
|
||
internal static bool StartsMinimized(IReadOnlyList<string> arguments) =>
|
||
arguments.Any(a => a.Equals(RunKeyStartWithWindows.MinimizedOption, StringComparison.OrdinalIgnoreCase));
|
||
|
||
/// <summary>Minimised until the user opens it; that first restore brings back the saved state (maximised or not).</summary>
|
||
internal void WaitMinimized(bool maximized)
|
||
{
|
||
WindowState = WindowState.Minimized;
|
||
_restoreTo = maximized ? WindowState.Maximized : WindowState.Normal;
|
||
}
|
||
|
||
/// <summary>The maximised flag to save: a window started minimised and never opened keeps the state it would come back to.</summary>
|
||
internal static bool SavesMaximized(WindowState state, WindowState? restoreTo) =>
|
||
state == WindowState.Maximized || (state == WindowState.Minimized && restoreTo == WindowState.Maximized);
|
||
|
||
/// <summary>The first restore of a window started minimised brings back the saved maximised state.</summary>
|
||
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;
|
||
}
|
||
|
||
/// <summary>F6 moves the keyboard to the newest notification (as in Windows apps), Esc there dismisses it.</summary>
|
||
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");
|
||
|
||
/// <summary>The first-run dialog takes the keyboard on its chosen option once it is on screen.</summary>
|
||
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),
|
||
};
|
||
}
|
||
}
|