using System.Collections.ObjectModel; using System.Windows.Input; 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; /// State of the main window: rail, current section, job bar, status strip and first run (BRIEF §12.1). public sealed class ShellViewModel : LocalizedViewModel { private readonly AppSettings _settings; private SectionViewModel _current; private bool _isRailCollapsed; private string? _jobText; 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 WelcomeViewModel? _welcome; private Guid? _currentSequenceId; 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]; _current = Library; Library.IsSelected = true; 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(name, out var key)) Navigate(key); }); ToggleRailCommand = new RelayCommand(() => IsRailCollapsed = !IsRailCollapsed); 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 TopSections { get; } public IReadOnlyList BottomSections { get; } public IEnumerable AllSections => TopSections.Concat(BottomSections); public ICommand NavigateCommand { get; } public ICommand ToggleRailCommand { get; } /// In-window notifications; the window binds its here. public ObservableCollection PendingToasts { get; } = []; /// First-run choice (Welcome.html); null once made. public WelcomeViewModel? Welcome { get => _welcome; private set { if (Set(ref _welcome, value)) OnPropertyChanged(nameof(IsWelcomeOpen)); } } public bool IsWelcomeOpen => _welcome is not null; public SectionViewModel Current { get => _current; private set { if (Set(ref _current, value)) OnPropertyChanged(nameof(WindowTitle)); } } public string WindowTitle => Current == Library ? "Titano" : "Titano — " + Current.Title; public bool IsRailCollapsed { get => _isRailCollapsed; set { if (!Set(ref _isRailCollapsed, value)) return; _settings.RailCollapsed = value; OnPropertyChanged(nameof(RailToggleToolTip)); } } public string RailToggleToolTip => Loc.L(IsRailCollapsed ? "Tooltips.Shell.ExpandRail" : "Tooltips.Shell.CollapseRail"); /// The sequence the editor opens (set by the Library and the Jobs screen). public Guid? CurrentSequenceId { get => _currentSequenceId; private set => Set(ref _currentSequenceId, value); } /// Loads the data of every screen once the index is open (or failed to open). public async Task StartAsync() { if (Services.OpenError is { } error) ReportError(error); Jobs.Attach(); await Library.AttachAsync(); Settings.Attach(); About.Attach(); await Server.AttachAsync(); } /// Opens a sequence in the editor. public void OpenSequence(Guid sequenceId) { CurrentSequenceId = sequenceId; Navigate(SectionKey.Sequence); } /// Closes the first-run overlay for good. public void CompleteWelcome() { _settings.FirstRunCompleted = true; Services.SaveSettings(); Welcome?.Dispose(); Welcome = null; } /// Shows the first-run overlay again (capture, tests). public void ShowWelcome() => Welcome ??= new WelcomeViewModel(this); // ---- Job bar ---- public string JobText => _jobActive && _jobText is not null ? _jobText : _queuedCount > 0 ? Loc.L("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 => Set(ref _jobActive, value); } public bool IsJobIndeterminate { get => _jobIndeterminate; private set => Set(ref _jobIndeterminate, value); } /// Pauses the job the bar shows. public ICommand? StopJobCommand => Jobs.PauseCurrentCommand; /// Updates the global job bar (and the taskbar progress through the window). public void SetJob(string? text, double progress, bool indeterminate = false) { _jobText = text; IsJobActive = text is not null; JobProgress = progress; IsJobIndeterminate = indeterminate; OnPropertyChanged(nameof(JobText)); } // ---- 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)); } /// Active jobs (running + queued) for the status strip, queued ones for the idle job bar. public void SetJobCount(int active, int queued = 0) { _jobCount = active; _queuedCount = queued; OnPropertyChanged(nameof(JobsText)); OnPropertyChanged(nameof(JobText)); } 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); /// A one-line notice with one action (job done, update available). public void Notify(string title, string? message = null, string? actionText = null, string? actionToolTip = null, Action? action = null) => ShowToast(new Toast { Title = title, Message = message, ActionText = actionText, ActionToolTip = actionToolTip, ActionCommand = action is null ? null : new RelayCommand(action), }); /// Errors are one sentence saying what happened plus one action (BRIEF §0.2 rule 6). public void ReportError(Exception ex) { Log.Error("ui", "Operation failed", ex); ShowToast(new Toast { Title = Loc.L("Errors.Unexpected"), Message = ex.Message, ActionText = Loc.L("Common.OpenLogs"), ActionToolTip = Loc.L("Tooltips.Common.OpenLogs"), ActionCommand = new RelayCommand(() => ShellActions.Reveal(Services.Environment.LogDirectory)), }); } public override void Dispose() { Jobs.Dispose(); Library.Dispose(); Server.Dispose(); Settings.Dispose(); About.Dispose(); Welcome?.Dispose(); base.Dispose(); } }