| | | 1 | | // ----------------------------------------------------------------------- |
| | | 2 | | // <copyright file="ShellHostViewModel.cs" company="Stéphane ANDRE"> |
| | | 3 | | // Copyright (c) Stéphane ANDRE. All rights reserved. |
| | | 4 | | // </copyright> |
| | | 5 | | // ----------------------------------------------------------------------- |
| | | 6 | | |
| | | 7 | | using System; |
| | | 8 | | using System.Diagnostics.CodeAnalysis; |
| | | 9 | | using System.Reactive.Disposables; |
| | | 10 | | using System.Windows.Input; |
| | | 11 | | using MyNet.UI.Commands; |
| | | 12 | | using MyNet.UI.Dialogs; |
| | | 13 | | using MyNet.UI.Loading; |
| | | 14 | | using MyNet.UI.Services; |
| | | 15 | | using MyNet.UI.ViewModels.Shell.Chrome; |
| | | 16 | | using MyNet.UI.ViewModels.Shell.Drawers; |
| | | 17 | | using MyNet.UI.ViewModels.Shell.FileMenu; |
| | | 18 | | using MyNet.UI.ViewModels.Shell.Notifications; |
| | | 19 | | using MyNet.UI.ViewModels.Shell.Taskbar; |
| | | 20 | | using MyNet.UI.ViewModels.Workspace; |
| | | 21 | | |
| | | 22 | | namespace MyNet.UI.ViewModels.Shell.Host; |
| | | 23 | | |
| | | 24 | | /// <summary> |
| | | 25 | | /// Shell host view model: drawers, composition of panel view models, and optional file menu navigation. |
| | | 26 | | /// </summary> |
| | | 27 | | [SuppressMessage("Usage", "CA2213:Disposable fields should be disposed", Justification = "Disposed through the shared di |
| | | 28 | | public class ShellHostViewModel : ViewModelBase, IShellCapabilities, IShellHostWithFileMenu, IShellNotificationsDrawerHo |
| | | 29 | | { |
| | | 30 | | private readonly ShellOptions _options; |
| | | 31 | | private readonly IDialogService _dialogService; |
| | | 32 | | private readonly ShellNotificationsHost _notificationsHost; |
| | | 33 | | |
| | | 34 | | /// <summary> |
| | | 35 | | /// Initializes a new instance of the <see cref="ShellHostViewModel"/> class. |
| | | 36 | | /// </summary> |
| | 24 | 37 | | public ShellHostViewModel( |
| | 24 | 38 | | IApplicationInfo applicationInfo, |
| | 24 | 39 | | ShellNotificationsViewModel notificationsViewModel, |
| | 24 | 40 | | ShellCultureViewModel cultureChrome, |
| | 24 | 41 | | ShellThemeViewModel themeChrome, |
| | 24 | 42 | | IAppCommandsService appCommandsService, |
| | 24 | 43 | | IBusyService applicationBusy, |
| | 24 | 44 | | ITaskbarProgressSource taskbarProgress, |
| | 24 | 45 | | IDialogService dialogService, |
| | 24 | 46 | | ShellNotificationsDrawerCoordinator notificationsDrawerCoordinator, |
| | 24 | 47 | | FileMenuViewModel? fileMenuViewModel = null, |
| | 24 | 48 | | IShellHostProvider? shellHostProvider = null, |
| | 24 | 49 | | ShellOptions? options = null, |
| | 24 | 50 | | ICommandFactory? commandFactory = null) |
| | | 51 | | { |
| | 24 | 52 | | ArgumentNullException.ThrowIfNull(applicationInfo); |
| | 24 | 53 | | ArgumentNullException.ThrowIfNull(notificationsViewModel); |
| | 24 | 54 | | ArgumentNullException.ThrowIfNull(cultureChrome); |
| | 24 | 55 | | ArgumentNullException.ThrowIfNull(themeChrome); |
| | 24 | 56 | | ArgumentNullException.ThrowIfNull(appCommandsService); |
| | 24 | 57 | | ArgumentNullException.ThrowIfNull(applicationBusy); |
| | 24 | 58 | | ArgumentNullException.ThrowIfNull(taskbarProgress); |
| | 24 | 59 | | ArgumentNullException.ThrowIfNull(dialogService); |
| | 24 | 60 | | ArgumentNullException.ThrowIfNull(notificationsDrawerCoordinator); |
| | | 61 | | |
| | 24 | 62 | | _options = options ?? ShellOptions.Default; |
| | 24 | 63 | | _dialogService = dialogService; |
| | | 64 | | |
| | 24 | 65 | | ProductName = applicationInfo.ProductName; |
| | | 66 | | NotificationsViewModel = notificationsViewModel; |
| | | 67 | | CultureChrome = cultureChrome; |
| | | 68 | | ThemeChrome = themeChrome; |
| | | 69 | | ApplicationBusy = applicationBusy; |
| | | 70 | | TaskbarProgress = taskbarProgress; |
| | | 71 | | |
| | 24 | 72 | | if (fileMenuViewModel is not null) |
| | 18 | 73 | | FileMenuHost = new(fileMenuViewModel, this, OnFileMenuDrawerOpening); |
| | | 74 | | |
| | 24 | 75 | | var commands = commandFactory.GetOrDefault(); |
| | | 76 | | |
| | 24 | 77 | | _notificationsHost = new( |
| | 24 | 78 | | this, |
| | 24 | 79 | | notificationsViewModel, |
| | 24 | 80 | | notificationsDrawerCoordinator, |
| | 24 | 81 | | CanUseShellChrome, |
| | 24 | 82 | | OnNotificationsDrawerOpening); |
| | | 83 | | |
| | 24 | 84 | | ToggleNotificationsCommand = _notificationsHost.CreateToggleCommand(commands); |
| | 24 | 85 | | ToggleFileMenuCommand = commands.Create(ToggleFileMenu, CanToggleFileMenu); |
| | 24 | 86 | | CloseDrawersCommand = commands.Create(CloseDrawers, CanUseShellChrome); |
| | 24 | 87 | | ExitCommand = commands.Create(appCommandsService.Exit, CanUseShellChrome); |
| | | 88 | | |
| | 24 | 89 | | if (shellHostProvider is not null) |
| | | 90 | | { |
| | 3 | 91 | | shellHostProvider.Attach(this); |
| | 3 | 92 | | Disposables.Add(Disposable.Create(() => shellHostProvider.Detach(this))); |
| | | 93 | | } |
| | | 94 | | |
| | 24 | 95 | | Disposables.Add(_notificationsHost); |
| | 24 | 96 | | } |
| | | 97 | | |
| | | 98 | | #if DEBUG |
| | | 99 | | /// <summary> |
| | | 100 | | /// Gets a value indicating whether the application runs in debug configuration. |
| | | 101 | | /// </summary> |
| | | 102 | | public bool IsDebug { get; } = true; |
| | | 103 | | #else |
| | | 104 | | /// <summary> |
| | | 105 | | /// Gets a value indicating whether the application runs in debug configuration. |
| | | 106 | | /// </summary> |
| | | 107 | | public bool IsDebug { get; } |
| | | 108 | | #endif |
| | | 109 | | |
| | | 110 | | /// <inheritdoc /> |
| | 3 | 111 | | public bool HasFileMenu => FileMenuHost is not null; |
| | | 112 | | |
| | | 113 | | /// <summary> |
| | | 114 | | /// Gets the optional file menu host. |
| | | 115 | | /// </summary> |
| | | 116 | | public ShellFileMenuHost? FileMenuHost { get; } |
| | | 117 | | |
| | | 118 | | /// <summary> |
| | | 119 | | /// Gets the notifications panel view model. |
| | | 120 | | /// </summary> |
| | | 121 | | public ShellNotificationsViewModel NotificationsViewModel { get; } |
| | | 122 | | |
| | | 123 | | /// <summary> |
| | | 124 | | /// Gets the shell culture selector. |
| | | 125 | | /// </summary> |
| | | 126 | | public ShellCultureViewModel CultureChrome { get; } |
| | | 127 | | |
| | | 128 | | /// <summary> |
| | | 129 | | /// Gets the shell quick theme toggle. |
| | | 130 | | /// </summary> |
| | | 131 | | public ShellThemeViewModel ThemeChrome { get; } |
| | | 132 | | |
| | | 133 | | /// <summary> |
| | | 134 | | /// Gets the application-wide busy service (overlay). |
| | | 135 | | /// </summary> |
| | | 136 | | public IBusyService ApplicationBusy { get; } |
| | | 137 | | |
| | | 138 | | /// <summary> |
| | | 139 | | /// Gets the taskbar progress source synchronized with <see cref="ApplicationBusy"/> by <see cref="BusyTaskbarCoordi |
| | | 140 | | /// </summary> |
| | | 141 | | public ITaskbarProgressSource TaskbarProgress { get; } |
| | | 142 | | |
| | | 143 | | /// <summary> |
| | | 144 | | /// Gets or sets a value indicating whether the notifications drawer is open. |
| | | 145 | | /// </summary> |
| | 45 | 146 | | public bool IsNotificationsOpen { get; set => SetProperty(ref field, value); } |
| | | 147 | | |
| | | 148 | | /// <inheritdoc /> |
| | 24 | 149 | | public bool IsFileMenuOpen { get; set => SetProperty(ref field, value); } |
| | | 150 | | |
| | | 151 | | /// <inheritdoc /> |
| | | 152 | | public ICommand ToggleNotificationsCommand { get; } |
| | | 153 | | |
| | | 154 | | /// <summary> |
| | | 155 | | /// Gets the command that toggles the file menu drawer. |
| | | 156 | | /// </summary> |
| | | 157 | | public ICommand ToggleFileMenuCommand { get; } |
| | | 158 | | |
| | | 159 | | /// <summary> |
| | | 160 | | /// Gets the command that closes all shell drawers. |
| | | 161 | | /// </summary> |
| | | 162 | | public ICommand CloseDrawersCommand { get; } |
| | | 163 | | |
| | | 164 | | /// <summary> |
| | | 165 | | /// Gets the command that exits the application. |
| | | 166 | | /// </summary> |
| | | 167 | | public ICommand ExitCommand { get; } |
| | | 168 | | |
| | | 169 | | /// <inheritdoc /> |
| | 9 | 170 | | public FileMenuViewModel FileMenuViewModel => RequireFileMenuHost().FileMenuViewModel; |
| | | 171 | | |
| | | 172 | | /// <summary> |
| | | 173 | | /// Gets the product name displayed in the window title. |
| | | 174 | | /// </summary> |
| | | 175 | | public string ProductName { get; } |
| | | 176 | | |
| | | 177 | | /// <summary> |
| | | 178 | | /// Gets the window title. |
| | | 179 | | /// </summary> |
| | 0 | 180 | | public virtual string Title => IsDebug ? $"{ProductName} [Debug]" : ProductName; |
| | | 181 | | |
| | | 182 | | /// <summary> |
| | | 183 | | /// Closes all shell drawers. |
| | | 184 | | /// </summary> |
| | | 185 | | public virtual void CloseDrawers() |
| | | 186 | | { |
| | 0 | 187 | | _notificationsHost.Close(); |
| | 0 | 188 | | IsFileMenuOpen = false; |
| | 0 | 189 | | FileMenuHost?.OnCloseAllDrawers(); |
| | 0 | 190 | | } |
| | | 191 | | |
| | | 192 | | /// <summary> |
| | | 193 | | /// Applies an action to the notifications drawer. |
| | | 194 | | /// </summary> |
| | 9 | 195 | | public void SetNotificationsDrawer(ShellDrawerAction action) => _notificationsHost.SetDrawer(action); |
| | | 196 | | |
| | | 197 | | /// <inheritdoc /> |
| | | 198 | | public void SetFileMenuDrawer(ShellDrawerAction action) |
| | | 199 | | { |
| | 0 | 200 | | if (FileMenuHost is not null) |
| | 0 | 201 | | FileMenuHost.SetDrawer(action); |
| | | 202 | | else |
| | 0 | 203 | | IsFileMenuOpen = action.ApplyToOpenState(IsFileMenuOpen); |
| | 0 | 204 | | } |
| | | 205 | | |
| | | 206 | | /// <inheritdoc /> |
| | | 207 | | public void OpenFileMenuContent<T>() |
| | | 208 | | where T : class, IWorkspaceViewModel |
| | 15 | 209 | | => RequireFileMenuHost().OpenFileMenuContent<T>(); |
| | | 210 | | |
| | | 211 | | /// <inheritdoc /> |
| | 0 | 212 | | public void OpenFileMenuContent(Type contentType) => RequireFileMenuHost().OpenFileMenuContent(contentType); |
| | | 213 | | |
| | | 214 | | /// <inheritdoc /> |
| | 3 | 215 | | public void CloseFileMenuContent() => RequireFileMenuHost().CloseFileMenuContent(); |
| | | 216 | | |
| | | 217 | | /// <inheritdoc /> |
| | | 218 | | public void SetFileMenuContentVisibility<T>(ShellDrawerAction action) |
| | | 219 | | where T : class, IWorkspaceViewModel |
| | 3 | 220 | | => RequireFileMenuHost().SetFileMenuContentVisibility<T>(action); |
| | | 221 | | |
| | | 222 | | /// <inheritdoc /> |
| | | 223 | | public void SetFileMenuContentVisibility(Type contentType, ShellDrawerAction action) |
| | 0 | 224 | | => RequireFileMenuHost().SetFileMenuContentVisibility(contentType, action); |
| | | 225 | | |
| | | 226 | | private ShellFileMenuHost RequireFileMenuHost() |
| | 30 | 227 | | => FileMenuHost |
| | 30 | 228 | | ?? throw new InvalidOperationException("File menu is not configured. Pass a FileMenuViewModel to ShellHostVie |
| | | 229 | | |
| | 0 | 230 | | private void ToggleFileMenu() => SetFileMenuDrawer(ShellDrawerAction.Toggle); |
| | | 231 | | |
| | 0 | 232 | | private bool CanToggleFileMenu() => HasFileMenu && CanUseShellChrome(); |
| | | 233 | | |
| | 0 | 234 | | private bool CanUseShellChrome() => !_dialogService.HasOpenedDialogs; |
| | | 235 | | |
| | | 236 | | private void OnNotificationsDrawerOpening() |
| | | 237 | | { |
| | 6 | 238 | | if (!_options.MutuallyExclusiveDrawers) |
| | 0 | 239 | | return; |
| | | 240 | | |
| | 6 | 241 | | IsFileMenuOpen = false; |
| | 6 | 242 | | FileMenuHost?.OnCloseAllDrawers(); |
| | 6 | 243 | | } |
| | | 244 | | |
| | | 245 | | private void OnFileMenuDrawerOpening() |
| | | 246 | | { |
| | 12 | 247 | | if (!_options.MutuallyExclusiveDrawers) |
| | 0 | 248 | | return; |
| | | 249 | | |
| | 12 | 250 | | _notificationsHost.Close(); |
| | 12 | 251 | | } |
| | | 252 | | } |
| | | 253 | | |