| | | 1 | | // ----------------------------------------------------------------------- |
| | | 2 | | // <copyright file="TabWorkspaceViewModel.cs" company="Stéphane ANDRE"> |
| | | 3 | | // Copyright (c) Stéphane ANDRE. All rights reserved. |
| | | 4 | | // </copyright> |
| | | 5 | | // ----------------------------------------------------------------------- |
| | | 6 | | |
| | | 7 | | using System; |
| | | 8 | | using System.Collections.Generic; |
| | | 9 | | using System.Collections.ObjectModel; |
| | | 10 | | using System.Linq; |
| | | 11 | | using System.Windows.Input; |
| | | 12 | | using MyNet.Globalization.Culture; |
| | | 13 | | using MyNet.UI.Commands; |
| | | 14 | | |
| | | 15 | | namespace MyNet.UI.ViewModels.Workspace; |
| | | 16 | | |
| | | 17 | | /// <summary> |
| | | 18 | | /// Provides a reusable base implementation for tabbed workspace view models. |
| | | 19 | | /// </summary> |
| | | 20 | | public abstract class TabWorkspaceViewModel : WorkspaceViewModel, ITabWorkspaceViewModel |
| | | 21 | | { |
| | 15 | 22 | | private readonly ObservableCollection<IWorkspaceViewModel> _workspaces = []; |
| | | 23 | | |
| | | 24 | | /// <summary> |
| | | 25 | | /// Initializes a new instance of the <see cref="TabWorkspaceViewModel"/> class. |
| | | 26 | | /// </summary> |
| | | 27 | | /// <param name="commandFactory">Optional command factory used to create commands.</param> |
| | | 28 | | /// <param name="cultureService">Optional culture service used to manage culture changes.</param> |
| | | 29 | | protected TabWorkspaceViewModel( |
| | | 30 | | ICommandFactory? commandFactory = null, |
| | | 31 | | ICultureService? cultureService = null) |
| | 15 | 32 | | : base(commandFactory, cultureService) |
| | | 33 | | { |
| | 15 | 34 | | var commands = commandFactory.GetOrDefault(); |
| | | 35 | | |
| | 15 | 36 | | Workspaces = new(_workspaces); |
| | | 37 | | |
| | 15 | 38 | | GoToTabCommand = commands.CreateRequired<object>(GoToTab, CanGoToTab); |
| | 15 | 39 | | GoToNextTabCommand = commands.Create(GoToNextTab, CanGoToNextTab); |
| | 15 | 40 | | GoToPreviousTabCommand = commands.Create(GoToPreviousTab, CanGoToPreviousTab); |
| | 15 | 41 | | } |
| | | 42 | | |
| | | 43 | | /// <summary> |
| | | 44 | | /// Gets the command to navigate to a specific tab by index or workspace reference. |
| | | 45 | | /// </summary> |
| | | 46 | | public ICommand GoToTabCommand { get; } |
| | | 47 | | |
| | | 48 | | /// <summary> |
| | | 49 | | /// Gets the command to navigate to the next tab. |
| | | 50 | | /// </summary> |
| | | 51 | | public ICommand GoToNextTabCommand { get; } |
| | | 52 | | |
| | | 53 | | /// <summary> |
| | | 54 | | /// Gets the command to navigate to the previous tab. |
| | | 55 | | /// </summary> |
| | | 56 | | public ICommand GoToPreviousTabCommand { get; } |
| | | 57 | | |
| | | 58 | | /// <summary> |
| | | 59 | | /// Gets the hosted workspaces. |
| | | 60 | | /// </summary> |
| | | 61 | | public ReadOnlyObservableCollection<IWorkspaceViewModel> Workspaces { get; } |
| | | 62 | | |
| | | 63 | | /// <summary> |
| | | 64 | | /// Gets the currently selected workspace. |
| | | 65 | | /// </summary> |
| | | 66 | | public IWorkspaceViewModel? SelectedWorkspace |
| | | 67 | | { |
| | | 68 | | get; |
| | | 69 | | private set |
| | | 70 | | { |
| | 15 | 71 | | if (!SetProperty(ref field, value)) |
| | 0 | 72 | | return; |
| | | 73 | | |
| | 15 | 74 | | NotifyPropertyChanged(nameof(SelectedIndex)); |
| | 15 | 75 | | RaiseTabNavigationCanExecuteChanged(); |
| | 15 | 76 | | } |
| | | 77 | | } |
| | | 78 | | |
| | | 79 | | /// <summary> |
| | | 80 | | /// Gets the selected workspace index. |
| | | 81 | | /// </summary> |
| | 0 | 82 | | public int SelectedIndex => SelectedWorkspace is null ? -1 : _workspaces.IndexOf(SelectedWorkspace); |
| | | 83 | | |
| | | 84 | | /// <summary> |
| | | 85 | | /// Selects the specified workspace. |
| | | 86 | | /// </summary> |
| | | 87 | | /// <param name="workspace">The workspace to select.</param> |
| | | 88 | | public virtual void Select(IWorkspaceViewModel workspace) |
| | | 89 | | { |
| | 0 | 90 | | ArgumentNullException.ThrowIfNull(workspace); |
| | | 91 | | |
| | 0 | 92 | | if (!_workspaces.Contains(workspace)) |
| | 0 | 93 | | throw new ArgumentException(null, nameof(workspace)); |
| | | 94 | | |
| | 0 | 95 | | SelectedWorkspace = workspace; |
| | 0 | 96 | | } |
| | | 97 | | |
| | | 98 | | /// <summary> |
| | | 99 | | /// Selects the workspace at the specified index. |
| | | 100 | | /// </summary> |
| | | 101 | | /// <param name="index">The zero-based index to select.</param> |
| | | 102 | | public virtual void Select(int index) |
| | | 103 | | { |
| | 0 | 104 | | if (index < 0 || index >= _workspaces.Count) |
| | 0 | 105 | | throw new ArgumentOutOfRangeException(nameof(index)); |
| | | 106 | | |
| | 0 | 107 | | SelectedWorkspace = _workspaces[index]; |
| | 0 | 108 | | } |
| | | 109 | | |
| | | 110 | | /// <summary> |
| | | 111 | | /// Adds a hosted workspace. |
| | | 112 | | /// </summary> |
| | | 113 | | /// <param name="workspace">The workspace to add.</param> |
| | | 114 | | protected virtual void AddWorkspace(IWorkspaceViewModel workspace) |
| | | 115 | | { |
| | 15 | 116 | | ArgumentNullException.ThrowIfNull(workspace); |
| | | 117 | | |
| | 15 | 118 | | _workspaces.Add(workspace); |
| | | 119 | | |
| | 15 | 120 | | if (workspace.State != LoadState.Loaded) |
| | 15 | 121 | | _ = ExecuteSafeAsync(workspace.LoadAsync); |
| | | 122 | | |
| | 15 | 123 | | SelectedWorkspace ??= workspace; |
| | 15 | 124 | | RaiseTabNavigationCanExecuteChanged(); |
| | 15 | 125 | | } |
| | | 126 | | |
| | | 127 | | /// <summary> |
| | | 128 | | /// Adds several hosted workspaces. |
| | | 129 | | /// </summary> |
| | | 130 | | /// <param name="workspaces">The workspaces to add.</param> |
| | | 131 | | protected void AddWorkspaces(IEnumerable<IWorkspaceViewModel> workspaces) |
| | | 132 | | { |
| | 15 | 133 | | ArgumentNullException.ThrowIfNull(workspaces); |
| | | 134 | | |
| | 60 | 135 | | foreach (var workspace in workspaces) |
| | 15 | 136 | | AddWorkspace(workspace); |
| | 15 | 137 | | } |
| | | 138 | | |
| | | 139 | | /// <summary> |
| | | 140 | | /// Removes a hosted workspace. |
| | | 141 | | /// </summary> |
| | | 142 | | /// <param name="workspace">The workspace to remove.</param> |
| | | 143 | | /// <returns><see langword="true"/> when the workspace was removed; otherwise <see langword="false"/>.</returns> |
| | | 144 | | protected virtual bool RemoveWorkspace(IWorkspaceViewModel workspace) |
| | | 145 | | { |
| | 0 | 146 | | ArgumentNullException.ThrowIfNull(workspace); |
| | | 147 | | |
| | 0 | 148 | | var removed = _workspaces.Remove(workspace); |
| | | 149 | | |
| | 0 | 150 | | if (!removed) |
| | 0 | 151 | | return false; |
| | | 152 | | |
| | 0 | 153 | | if (ReferenceEquals(SelectedWorkspace, workspace)) |
| | 0 | 154 | | SelectedWorkspace = _workspaces.FirstOrDefault(); |
| | | 155 | | |
| | 0 | 156 | | RaiseTabNavigationCanExecuteChanged(); |
| | 0 | 157 | | return true; |
| | | 158 | | } |
| | | 159 | | |
| | | 160 | | /// <summary> |
| | | 161 | | /// Clears all hosted workspaces. |
| | | 162 | | /// </summary> |
| | | 163 | | protected void ClearWorkspaces() |
| | | 164 | | { |
| | 0 | 165 | | _workspaces.Clear(); |
| | 0 | 166 | | SelectedWorkspace = null; |
| | 0 | 167 | | RaiseTabNavigationCanExecuteChanged(); |
| | 0 | 168 | | } |
| | | 169 | | |
| | | 170 | | /// <summary> |
| | | 171 | | /// Navigates to the specified tab by index or workspace reference. |
| | | 172 | | /// </summary> |
| | | 173 | | /// <param name="indexOrWorkspace">An <see cref="int"/> index or an <see cref="IWorkspaceViewModel"/> reference.</pa |
| | | 174 | | protected virtual void GoToTab(object indexOrWorkspace) |
| | | 175 | | { |
| | | 176 | | switch (indexOrWorkspace) |
| | | 177 | | { |
| | | 178 | | case int index: |
| | 0 | 179 | | Select(index); |
| | 0 | 180 | | break; |
| | | 181 | | case IWorkspaceViewModel workspace: |
| | 0 | 182 | | Select(workspace); |
| | | 183 | | break; |
| | | 184 | | } |
| | 0 | 185 | | } |
| | | 186 | | |
| | | 187 | | /// <summary> |
| | | 188 | | /// Determines whether navigation to the specified tab is allowed. |
| | | 189 | | /// </summary> |
| | 0 | 190 | | protected virtual bool CanGoToTab(object target) => _workspaces.Count > 0; |
| | | 191 | | |
| | | 192 | | /// <summary> |
| | | 193 | | /// Navigates to the next tab. |
| | | 194 | | /// </summary> |
| | | 195 | | protected virtual void GoToNextTab() |
| | | 196 | | { |
| | 0 | 197 | | if (SelectedIndex < _workspaces.Count - 1) |
| | 0 | 198 | | Select(SelectedIndex + 1); |
| | 0 | 199 | | } |
| | | 200 | | |
| | | 201 | | /// <summary> |
| | | 202 | | /// Determines whether navigation to the next tab is allowed. |
| | | 203 | | /// </summary> |
| | 0 | 204 | | protected virtual bool CanGoToNextTab() => SelectedWorkspace is not null && SelectedIndex < _workspaces.Count - 1; |
| | | 205 | | |
| | | 206 | | /// <summary> |
| | | 207 | | /// Navigates to the previous tab. |
| | | 208 | | /// </summary> |
| | | 209 | | protected virtual void GoToPreviousTab() |
| | | 210 | | { |
| | 0 | 211 | | if (SelectedIndex > 0) |
| | 0 | 212 | | Select(SelectedIndex - 1); |
| | 0 | 213 | | } |
| | | 214 | | |
| | | 215 | | /// <summary> |
| | | 216 | | /// Determines whether navigation to the previous tab is allowed. |
| | | 217 | | /// </summary> |
| | 0 | 218 | | protected virtual bool CanGoToPreviousTab() => SelectedWorkspace is not null && SelectedIndex > 0; |
| | | 219 | | |
| | | 220 | | private void RaiseTabNavigationCanExecuteChanged() |
| | | 221 | | { |
| | 30 | 222 | | (GoToTabCommand as IRaiseCanExecuteChanged)?.RaiseCanExecuteChanged(); |
| | 30 | 223 | | (GoToNextTabCommand as IRaiseCanExecuteChanged)?.RaiseCanExecuteChanged(); |
| | 30 | 224 | | (GoToPreviousTabCommand as IRaiseCanExecuteChanged)?.RaiseCanExecuteChanged(); |
| | 30 | 225 | | } |
| | | 226 | | } |
| | | 227 | | |