- Implement tests for picker behavior, including loading sequences, searching, and handling empty libraries. - Validate sequence opening via Enter key and double-click actions. - Ensure proper handling of unsaved edits and sequence deletion scenarios. - Verify accessibility features for picker rows across different languages. - Introduce a dialog counting mechanism to manage unsaved edit confirmations.
364 lines
15 KiB
C#
364 lines
15 KiB
C#
using System.Collections.ObjectModel;
|
|
using System.Windows.Input;
|
|
using System.Windows.Shell;
|
|
using Titano.App.Infrastructure;
|
|
using Titano.App.Sections;
|
|
using Titano.Core.Diagnostics;
|
|
using Titano.Core.Localization;
|
|
using Titano.Ui.Controls;
|
|
using Titano.Ui.Mvvm;
|
|
|
|
namespace Titano.App.Shell;
|
|
|
|
/// <summary>State of the main window: rail, current section, job bar, status strip and first run (BRIEF §12.1).</summary>
|
|
public sealed class ShellViewModel : LocalizedViewModel
|
|
{
|
|
private readonly AppSettings _settings;
|
|
private SectionViewModel _current;
|
|
private bool _isRailCollapsed;
|
|
private string? _jobText;
|
|
private string? _jobName;
|
|
private double _jobProgress;
|
|
private bool _jobActive;
|
|
private bool _jobIndeterminate;
|
|
private long _sequenceCount;
|
|
private long _photoCount;
|
|
private long _totalBytes;
|
|
private string? _serverName;
|
|
private int _jobCount;
|
|
private int _queuedCount;
|
|
private int _unseenFailures;
|
|
private WelcomeViewModel? _welcome;
|
|
private Guid? _currentSequenceId;
|
|
private int _sequenceRequests;
|
|
|
|
public ShellViewModel(AppServices services)
|
|
{
|
|
Services = services;
|
|
_settings = services.Settings;
|
|
_isRailCollapsed = _settings.RailCollapsed;
|
|
Library = new LibraryViewModel(this);
|
|
Import = new ImportViewModel(this);
|
|
Sequence = new SequenceViewModel(this);
|
|
Archive = new ArchiveViewModel(this);
|
|
Jobs = new JobsViewModel(this);
|
|
Server = new ServerViewModel(this);
|
|
Settings = new SettingsViewModel(this);
|
|
About = new AboutViewModel(this);
|
|
TopSections = [Library, Import, Sequence, Archive, Jobs, Server];
|
|
BottomSections = [Settings, About];
|
|
foreach (var section in AllSections) section.InCollapsedRail = _isRailCollapsed;
|
|
_current = Library;
|
|
Library.IsSelected = true;
|
|
Library.PropertyChanged += OnCurrentChanged;
|
|
|
|
// The rail and its shortcuts (Ctrl+1…6, Ctrl+,) navigate only while no first-run dialog covers the shell.
|
|
NavigateCommand = new RelayCommand(p =>
|
|
{
|
|
if (p is SectionViewModel s) Navigate(s.Key);
|
|
else if (p is SectionKey k) Navigate(k);
|
|
else if (p is string name && Enum.TryParse<SectionKey>(name, out var key)) Navigate(key);
|
|
}, _ => IsShellEnabled);
|
|
// Like the shortcuts above, Ctrl+B leaves the shell behind the first-run dialog alone.
|
|
ToggleRailCommand = new RelayCommand(() => IsRailCollapsed = !IsRailCollapsed, () => IsShellEnabled);
|
|
services.Server.PropertyChanged += (_, _) => SetConnection(services.Server.ServerName);
|
|
if (!_settings.FirstRunCompleted) _welcome = new WelcomeViewModel(this);
|
|
}
|
|
|
|
public AppServices Services { get; }
|
|
|
|
public LibraryViewModel Library { get; }
|
|
public ImportViewModel Import { get; }
|
|
public SequenceViewModel Sequence { get; }
|
|
public ArchiveViewModel Archive { get; }
|
|
public JobsViewModel Jobs { get; }
|
|
public ServerViewModel Server { get; }
|
|
public SettingsViewModel Settings { get; }
|
|
public AboutViewModel About { get; }
|
|
|
|
public IReadOnlyList<SectionViewModel> TopSections { get; }
|
|
public IReadOnlyList<SectionViewModel> BottomSections { get; }
|
|
public IEnumerable<SectionViewModel> AllSections => TopSections.Concat(BottomSections);
|
|
|
|
public ICommand NavigateCommand { get; }
|
|
public ICommand ToggleRailCommand { get; }
|
|
|
|
/// <summary>In-window notifications; the window binds its <see cref="ToastHost"/> here.</summary>
|
|
public ObservableCollection<Toast> PendingToasts { get; } = [];
|
|
|
|
/// <summary>First-run choice (Welcome.html); null once made.</summary>
|
|
public WelcomeViewModel? Welcome
|
|
{
|
|
get => _welcome;
|
|
private set
|
|
{
|
|
if (!Set(ref _welcome, value)) return;
|
|
OnPropertyChanged(nameof(IsWelcomeOpen));
|
|
OnPropertyChanged(nameof(IsShellEnabled));
|
|
CommandManager.InvalidateRequerySuggested();
|
|
}
|
|
}
|
|
|
|
public bool IsWelcomeOpen => _welcome is not null;
|
|
|
|
/// <summary>The rail and the sections take input only while no first-run dialog covers them.</summary>
|
|
public bool IsShellEnabled => _welcome is null;
|
|
|
|
public SectionViewModel Current
|
|
{
|
|
get => _current;
|
|
private set
|
|
{
|
|
var previous = _current;
|
|
if (!Set(ref _current, value)) return;
|
|
previous.PropertyChanged -= OnCurrentChanged;
|
|
value.PropertyChanged += OnCurrentChanged;
|
|
OnPropertyChanged(nameof(WindowTitle));
|
|
OnPropertyChanged(nameof(IsJobBarVisible));
|
|
}
|
|
}
|
|
|
|
public string WindowTitle => Current == Library ? Loc.L("Shell.AppName") : Loc.L("Shell.WindowTitle", Loc.L("Shell.AppName"), Current.Title);
|
|
|
|
/// <summary>The rail shows icons alone (≡ or Ctrl+B); kept at once in the settings (owner review 1).</summary>
|
|
public bool IsRailCollapsed
|
|
{
|
|
get => _isRailCollapsed;
|
|
set
|
|
{
|
|
if (!Set(ref _isRailCollapsed, value)) return;
|
|
_settings.RailCollapsed = value;
|
|
foreach (var section in AllSections) section.InCollapsedRail = value;
|
|
Services.SavePreferences();
|
|
OnPropertyChanged(nameof(RailToggleToolTip));
|
|
OnPropertyChanged(nameof(RailToggleName));
|
|
}
|
|
}
|
|
|
|
public string RailToggleToolTip => Loc.L(IsRailCollapsed ? "Tooltips.Shell.ExpandRail" : "Tooltips.Shell.CollapseRail");
|
|
|
|
/// <summary>The rail button's accessible name: two words, the sentence is its tooltip.</summary>
|
|
public string RailToggleName => Loc.L(IsRailCollapsed ? "Shell.Nav.Expand" : "Shell.Nav.Collapse");
|
|
|
|
/// <summary>The sequence the editor opens (set by the Library and the Jobs screen).</summary>
|
|
public Guid? CurrentSequenceId
|
|
{
|
|
get => _currentSequenceId;
|
|
private set => Set(ref _currentSequenceId, value);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Counts <see cref="OpenSequence"/> calls: asking for the open sequence again (the Library, Jobs, a project file) ends a
|
|
/// choice of another in Edit, while coming back to Edit alone does not (owner review 1, R7).
|
|
/// </summary>
|
|
public int SequenceRequests
|
|
{
|
|
get => _sequenceRequests;
|
|
private set => Set(ref _sequenceRequests, value);
|
|
}
|
|
|
|
/// <summary>Loads the data of every screen once the index is open (or failed to open).</summary>
|
|
public async Task StartAsync()
|
|
{
|
|
if (Services.OpenError is { } error) ReportError(error);
|
|
Jobs.Attach();
|
|
await Library.AttachAsync();
|
|
// Edit opened before the index did shows its picker empty until now (neither "no sequences" nor Import).
|
|
if (Sequence.ShowsPicker && Sequence.IsSelected) _ = Sequence.Picker.ReloadAsync();
|
|
Settings.Attach();
|
|
About.Attach();
|
|
await Server.AttachAsync();
|
|
}
|
|
|
|
/// <summary>Opens a sequence in Edit.</summary>
|
|
public void OpenSequence(Guid sequenceId)
|
|
{
|
|
CurrentSequenceId = sequenceId;
|
|
SequenceRequests++;
|
|
Navigate(SectionKey.Edit);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Edit's open sequence (or none) is again the sequence asked for: after Keep editing, a choice cancelled, an open that
|
|
/// failed, or the sequence asked for deleted. Coming back to Edit then neither asks once more nor tries again; asking
|
|
/// for a sequence again (<see cref="OpenSequence"/>) does.
|
|
/// </summary>
|
|
internal void KeepSequence(Guid? sequenceId) => CurrentSequenceId = sequenceId;
|
|
|
|
/// <summary>Closes the first-run overlay for good.</summary>
|
|
public void CompleteWelcome()
|
|
{
|
|
_settings.FirstRunCompleted = true;
|
|
Services.SaveSettings();
|
|
Welcome?.Dispose();
|
|
Welcome = null;
|
|
}
|
|
|
|
/// <summary>Shows the first-run overlay again (capture, tests).</summary>
|
|
public void ShowWelcome() => Welcome ??= new WelcomeViewModel(this);
|
|
|
|
// ---- Job bar ----
|
|
public string JobText => _jobActive && _jobText is not null
|
|
? _jobText
|
|
: _queuedCount > 0 ? Loc.Plural("Shell.JobBar.Ready", _queuedCount) : Loc.L("Shell.JobBar.Idle");
|
|
|
|
public double JobProgress { get => _jobProgress; private set => Set(ref _jobProgress, value); }
|
|
public bool IsJobActive
|
|
{
|
|
get => _jobActive;
|
|
private set
|
|
{
|
|
if (Set(ref _jobActive, value)) OnPropertyChanged(nameof(IsJobBarVisible));
|
|
}
|
|
}
|
|
|
|
/// <summary>The job bar shows "Ready" when idle, except in the editor where it gives its 40 px to the preview (DESIGN §4).</summary>
|
|
public bool IsJobBarVisible => _jobActive || _current != Sequence;
|
|
public bool IsJobIndeterminate { get => _jobIndeterminate; private set => Set(ref _jobIndeterminate, value); }
|
|
|
|
/// <summary>Pauses the job the bar shows.</summary>
|
|
public ICommand? StopJobCommand => Jobs.PauseCurrentCommand;
|
|
|
|
/// <summary>
|
|
/// What screen readers hear from the job bar: the job when one starts, the idle line when the last one ends; not its
|
|
/// progress, speed or time left, which change every second.
|
|
/// </summary>
|
|
public string JobAnnouncement => _jobActive && _jobName is not null ? _jobName : JobText;
|
|
|
|
/// <summary>
|
|
/// The taskbar button mirrors the job bar (BRIEF §11.5): the running job's progress, sliding while its total is
|
|
/// unknown; red while no job runs and a failure waits on the Jobs screen; nothing otherwise.
|
|
/// </summary>
|
|
public TaskbarItemProgressState TaskbarState =>
|
|
_jobActive ? _jobIndeterminate ? TaskbarItemProgressState.Indeterminate : TaskbarItemProgressState.Normal
|
|
: _unseenFailures > 0 ? TaskbarItemProgressState.Error
|
|
: TaskbarItemProgressState.None;
|
|
|
|
/// <summary>0…1 for <see cref="TaskbarState"/>; a waiting failure fills the button.</summary>
|
|
public double TaskbarProgress => _jobActive ? _jobProgress : _unseenFailures > 0 ? 1 : 0;
|
|
|
|
/// <summary>
|
|
/// Updates the global job bar and the taskbar button. <paramref name="name"/> says which job it is without its
|
|
/// progress ("Render: Harbour sunset"); null reads <paramref name="text"/>.
|
|
/// </summary>
|
|
public void SetJob(string? text, double progress, bool indeterminate = false, string? name = null)
|
|
{
|
|
_jobText = text;
|
|
_jobName = text is null ? null : name ?? text;
|
|
IsJobActive = text is not null;
|
|
JobProgress = progress;
|
|
IsJobIndeterminate = indeterminate;
|
|
OnPropertyChanged(nameof(JobText));
|
|
OnPropertyChanged(nameof(JobAnnouncement));
|
|
OnPropertyChanged(nameof(TaskbarState));
|
|
OnPropertyChanged(nameof(TaskbarProgress));
|
|
}
|
|
|
|
/// <summary>Failed jobs not seen on the Jobs screen yet (the taskbar button turns red while no job runs).</summary>
|
|
public void SetUnseenFailures(int count)
|
|
{
|
|
if (_unseenFailures == count) return;
|
|
_unseenFailures = count;
|
|
OnPropertyChanged(nameof(TaskbarState));
|
|
OnPropertyChanged(nameof(TaskbarProgress));
|
|
}
|
|
|
|
// ---- Status strip ----
|
|
public string SequencesText => Loc.Plural("Shell.Status.Sequences", _sequenceCount);
|
|
public string PhotosText => Loc.Plural("Shell.Status.Photos", _photoCount);
|
|
public string StorageText => _sequenceCount == 0 && _settings.LastArchivePath is null ? Loc.L("Shell.Status.NoArchive") : Formatting.Bytes(_totalBytes);
|
|
public string ConnectionText => _serverName is null ? Loc.L("Shell.Status.NotConnected") : Loc.L("Shell.Status.Connected", _serverName);
|
|
public bool IsConnected => _serverName is not null;
|
|
public string JobsText => Loc.Plural("Shell.Status.Jobs", _jobCount);
|
|
|
|
public void SetLibraryTotals(long sequences, long photos, long bytes)
|
|
{
|
|
_sequenceCount = sequences;
|
|
_photoCount = photos;
|
|
_totalBytes = bytes;
|
|
OnPropertyChanged(nameof(SequencesText));
|
|
OnPropertyChanged(nameof(PhotosText));
|
|
OnPropertyChanged(nameof(StorageText));
|
|
}
|
|
|
|
public void SetConnection(string? serverName)
|
|
{
|
|
if (_serverName == serverName) return;
|
|
_serverName = serverName;
|
|
OnPropertyChanged(nameof(ConnectionText));
|
|
OnPropertyChanged(nameof(IsConnected));
|
|
}
|
|
|
|
/// <summary>Active jobs (running + queued) for the status strip, queued ones for the idle job bar.</summary>
|
|
public void SetJobCount(int active, int queued = 0)
|
|
{
|
|
_jobCount = active;
|
|
_queuedCount = queued;
|
|
OnPropertyChanged(nameof(JobsText));
|
|
OnPropertyChanged(nameof(JobText));
|
|
OnPropertyChanged(nameof(JobAnnouncement));
|
|
}
|
|
|
|
public void Navigate(SectionKey key)
|
|
{
|
|
var target = AllSections.First(s => s.Key == key);
|
|
foreach (var s in AllSections) s.IsSelected = ReferenceEquals(s, target);
|
|
Current = target;
|
|
}
|
|
|
|
public void ShowToast(Toast toast) => PendingToasts.Add(toast);
|
|
|
|
/// <summary>The section on screen renamed itself (the editor opened a sequence): the window title follows.</summary>
|
|
private void OnCurrentChanged(object? sender, System.ComponentModel.PropertyChangedEventArgs e)
|
|
{
|
|
if (e.PropertyName is nameof(SectionViewModel.Title) or "" or null) OnPropertyChanged(nameof(WindowTitle));
|
|
}
|
|
|
|
/// <summary>
|
|
/// A one-line notice with one action (job done, update available). <paramref name="isError"/> marks something that
|
|
/// did not happen or failed: screen readers then say it at once instead of after what they are reading.
|
|
/// <paramref name="runsFromNotification"/>: a click on its Windows notification also runs the action (<see cref="Toast.RunsFromNotification"/>).
|
|
/// </summary>
|
|
public void Notify(string title, string? message = null, string? actionText = null, string? actionToolTip = null, Action? action = null, bool isError = false,
|
|
bool runsFromNotification = false) =>
|
|
ShowToast(new Toast
|
|
{
|
|
Title = title,
|
|
Message = message,
|
|
ActionText = actionText,
|
|
ActionToolTip = actionToolTip,
|
|
ActionCommand = action is null ? null : new RelayCommand(action),
|
|
IsError = isError,
|
|
RunsFromNotification = runsFromNotification,
|
|
});
|
|
|
|
/// <summary>Errors are one sentence saying what happened plus one action (BRIEF §0.2 rule 6); the details go to the log.</summary>
|
|
public void ReportError(Exception ex)
|
|
{
|
|
Log.Error("ui", "Operation failed", ex);
|
|
ShowToast(new Toast
|
|
{
|
|
Title = ErrorText.For(ex),
|
|
IsError = true,
|
|
ActionText = Loc.L("Common.OpenLogs"),
|
|
ActionToolTip = Loc.L("Tooltips.Common.OpenLogs"),
|
|
ActionCommand = new RelayCommand(() => ShellActions.Reveal(Services.Environment.LogDirectory)),
|
|
});
|
|
}
|
|
|
|
public override void Dispose()
|
|
{
|
|
// The editor first: it stops its runners and closes its source before the engine and the index under it go.
|
|
Sequence.Dispose();
|
|
Jobs.Dispose();
|
|
Library.Dispose();
|
|
Import.Dispose();
|
|
Archive.Dispose();
|
|
Server.Dispose();
|
|
Settings.Dispose();
|
|
About.Dispose();
|
|
Welcome?.Dispose();
|
|
base.Dispose();
|
|
}
|
|
}
|