| | | 1 | | // ----------------------------------------------------------------------- |
| | | 2 | | // <copyright file="FileMenuViewModel.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.Linq; |
| | | 10 | | using System.Windows.Input; |
| | | 11 | | using MyNet.UI.Commands; |
| | | 12 | | using MyNet.UI.ViewModels.Workspace; |
| | | 13 | | |
| | | 14 | | namespace MyNet.UI.ViewModels.Shell.FileMenu; |
| | | 15 | | |
| | | 16 | | /// <summary> |
| | | 17 | | /// View model for the file menu drawer content. |
| | | 18 | | /// Drawer visibility is controlled by the shell host via <see cref="IShellFileMenuDrawer"/>. |
| | | 19 | | /// </summary> |
| | | 20 | | public sealed class FileMenuViewModel : ViewModelBase |
| | | 21 | | { |
| | 30 | 22 | | private readonly HashSet<IWorkspaceViewModel> _contentViewModels = []; |
| | | 23 | | |
| | | 24 | | /// <summary> |
| | | 25 | | /// Initializes a new instance of the <see cref="FileMenuViewModel"/> class. |
| | | 26 | | /// </summary> |
| | 30 | 27 | | public FileMenuViewModel( |
| | 30 | 28 | | IEnumerable<IWorkspaceViewModel> contentViewModels, |
| | 30 | 29 | | ICommandFactory? commandFactory = null) |
| | | 30 | | { |
| | 30 | 31 | | ArgumentNullException.ThrowIfNull(contentViewModels); |
| | | 32 | | |
| | 120 | 33 | | foreach (var workspace in contentViewModels) |
| | 30 | 34 | | _contentViewModels.Add(workspace); |
| | | 35 | | |
| | 30 | 36 | | var commands = commandFactory.GetOrDefault(); |
| | 30 | 37 | | ToggleFileMenuContentCommand = commands.CreateRequired<Type>(ToggleContent); |
| | 30 | 38 | | } |
| | | 39 | | |
| | | 40 | | /// <summary> |
| | | 41 | | /// Gets or sets the workspace currently displayed in the file menu. |
| | | 42 | | /// </summary> |
| | 33 | 43 | | public IWorkspaceViewModel? Content { get; set => SetProperty(ref field, value); } |
| | | 44 | | |
| | | 45 | | /// <summary> |
| | | 46 | | /// Gets the command that toggles file menu content by workspace type. |
| | | 47 | | /// </summary> |
| | | 48 | | public ICommand ToggleFileMenuContentCommand { get; } |
| | | 49 | | |
| | | 50 | | /// <summary> |
| | | 51 | | /// Shows the workspace registered for <typeparamref name="T"/>. |
| | | 52 | | /// </summary> |
| | | 53 | | public void ShowContent<T>() |
| | | 54 | | where T : IWorkspaceViewModel |
| | 0 | 55 | | => ShowContent(typeof(T)); |
| | | 56 | | |
| | | 57 | | /// <summary> |
| | | 58 | | /// Shows the workspace registered for <paramref name="contentType"/>. |
| | | 59 | | /// </summary> |
| | | 60 | | public void ShowContent(Type contentType) |
| | | 61 | | { |
| | 21 | 62 | | ArgumentNullException.ThrowIfNull(contentType); |
| | | 63 | | |
| | 21 | 64 | | if (!ContentIsVisible(contentType)) |
| | 21 | 65 | | Content = _contentViewModels.FirstOrDefault(contentType.IsInstanceOfType); |
| | 21 | 66 | | } |
| | | 67 | | |
| | | 68 | | /// <summary> |
| | | 69 | | /// Hides the current file menu content. |
| | | 70 | | /// </summary> |
| | | 71 | | public void HideContent() |
| | | 72 | | { |
| | 18 | 73 | | if (Content is not null) |
| | 12 | 74 | | Content = null; |
| | 18 | 75 | | } |
| | | 76 | | |
| | | 77 | | /// <summary> |
| | | 78 | | /// Toggles visibility of the workspace registered for <paramref name="contentType"/>. |
| | | 79 | | /// </summary> |
| | | 80 | | public void ToggleContent(Type contentType) |
| | | 81 | | { |
| | 0 | 82 | | ArgumentNullException.ThrowIfNull(contentType); |
| | | 83 | | |
| | 0 | 84 | | if (!ContentIsVisible(contentType)) |
| | 0 | 85 | | ShowContent(contentType); |
| | | 86 | | else |
| | 0 | 87 | | HideContent(); |
| | 0 | 88 | | } |
| | | 89 | | |
| | | 90 | | /// <summary> |
| | | 91 | | /// Returns whether <paramref name="contentType"/> matches the current <see cref="Content"/>. |
| | | 92 | | /// </summary> |
| | 27 | 93 | | public bool ContentIsVisible(Type contentType) => contentType.IsInstanceOfType(Content); |
| | | 94 | | |
| | | 95 | | /// <summary> |
| | | 96 | | /// Returns whether <typeparamref name="T"/> matches the current <see cref="Content"/>. |
| | | 97 | | /// </summary> |
| | | 98 | | public bool ContentIsVisible<T>() |
| | | 99 | | where T : IWorkspaceViewModel |
| | 0 | 100 | | => ContentIsVisible(typeof(T)); |
| | | 101 | | } |
| | | 102 | | |