| | | 1 | | // ----------------------------------------------------------------------- |
| | | 2 | | // <copyright file="ShellFileMenuHost.cs" company="Stéphane ANDRE"> |
| | | 3 | | // Copyright (c) Stéphane ANDRE. All rights reserved. |
| | | 4 | | // </copyright> |
| | | 5 | | // ----------------------------------------------------------------------- |
| | | 6 | | |
| | | 7 | | using System; |
| | | 8 | | using MyNet.UI.ViewModels.Workspace; |
| | | 9 | | |
| | | 10 | | namespace MyNet.UI.ViewModels.Shell.FileMenu; |
| | | 11 | | |
| | | 12 | | /// <summary> |
| | | 13 | | /// Coordinates file menu content navigation and drawer visibility for the shell host. |
| | | 14 | | /// </summary> |
| | | 15 | | /// <remarks> |
| | | 16 | | /// Initializes a new instance of the <see cref="ShellFileMenuHost"/> class. |
| | | 17 | | /// </remarks> |
| | | 18 | | public sealed class ShellFileMenuHost( |
| | | 19 | | FileMenuViewModel fileMenuViewModel, |
| | | 20 | | IShellFileMenuDrawer drawer, |
| | | 21 | | Action? onDrawerOpening = null) |
| | | 22 | | { |
| | | 23 | | /// <summary> |
| | | 24 | | /// Gets the file menu content view model. |
| | | 25 | | /// </summary> |
| | 30 | 26 | | public FileMenuViewModel FileMenuViewModel { get; } = fileMenuViewModel ?? throw new ArgumentNullException(nameof(fi |
| | | 27 | | |
| | | 28 | | /// <summary> |
| | | 29 | | /// Gets the shell drawer state surface. |
| | | 30 | | /// </summary> |
| | 30 | 31 | | public IShellFileMenuDrawer Drawer { get; } = drawer ?? throw new ArgumentNullException(nameof(drawer)); |
| | | 32 | | |
| | | 33 | | /// <summary> |
| | | 34 | | /// Hides file menu content when the shell closes all drawers. |
| | | 35 | | /// </summary> |
| | 6 | 36 | | public void OnCloseAllDrawers() => FileMenuViewModel.HideContent(); |
| | | 37 | | |
| | | 38 | | /// <summary> |
| | | 39 | | /// Opens the file menu drawer and shows the workspace registered for <typeparamref name="T"/>. |
| | | 40 | | /// </summary> |
| | | 41 | | public void OpenFileMenuContent<T>() |
| | | 42 | | where T : class, IWorkspaceViewModel |
| | 21 | 43 | | => OpenFileMenuContent(typeof(T)); |
| | | 44 | | |
| | | 45 | | /// <summary> |
| | | 46 | | /// Opens the file menu drawer and shows the workspace registered for <paramref name="contentType"/>. |
| | | 47 | | /// </summary> |
| | | 48 | | public void OpenFileMenuContent(Type contentType) |
| | | 49 | | { |
| | 21 | 50 | | ArgumentNullException.ThrowIfNull(contentType); |
| | 21 | 51 | | NotifyDrawerOpening(); |
| | 21 | 52 | | Drawer.IsFileMenuOpen = true; |
| | 21 | 53 | | FileMenuViewModel.ShowContent(contentType); |
| | 21 | 54 | | } |
| | | 55 | | |
| | | 56 | | /// <summary> |
| | | 57 | | /// Hides file menu content and closes the file menu drawer. |
| | | 58 | | /// </summary> |
| | | 59 | | public void CloseFileMenuContent() |
| | | 60 | | { |
| | 12 | 61 | | FileMenuViewModel.HideContent(); |
| | 12 | 62 | | Drawer.IsFileMenuOpen = false; |
| | 12 | 63 | | } |
| | | 64 | | |
| | | 65 | | /// <summary> |
| | | 66 | | /// Applies an action to the file menu drawer and its content for the given workspace type. |
| | | 67 | | /// </summary> |
| | | 68 | | public void SetFileMenuContentVisibility<T>(ShellDrawerAction action) |
| | | 69 | | where T : class, IWorkspaceViewModel |
| | 6 | 70 | | => SetFileMenuContentVisibility(typeof(T), action); |
| | | 71 | | |
| | | 72 | | /// <summary> |
| | | 73 | | /// Applies an action to the file menu drawer and its content. |
| | | 74 | | /// </summary> |
| | | 75 | | public void SetFileMenuContentVisibility(Type contentType, ShellDrawerAction action) |
| | | 76 | | { |
| | 6 | 77 | | ArgumentNullException.ThrowIfNull(contentType); |
| | | 78 | | |
| | | 79 | | switch (action) |
| | | 80 | | { |
| | | 81 | | case ShellDrawerAction.Show: |
| | 0 | 82 | | NotifyDrawerOpening(); |
| | 0 | 83 | | Drawer.IsFileMenuOpen = true; |
| | 0 | 84 | | FileMenuViewModel.ShowContent(contentType); |
| | 0 | 85 | | break; |
| | | 86 | | |
| | | 87 | | case ShellDrawerAction.Hide: |
| | 0 | 88 | | FileMenuViewModel.HideContent(); |
| | 0 | 89 | | Drawer.IsFileMenuOpen = false; |
| | 0 | 90 | | break; |
| | | 91 | | |
| | | 92 | | case ShellDrawerAction.Toggle: |
| | 6 | 93 | | if (Drawer.IsFileMenuOpen) |
| | | 94 | | { |
| | 6 | 95 | | if (!FileMenuViewModel.ContentIsVisible(contentType)) |
| | | 96 | | { |
| | 0 | 97 | | NotifyDrawerOpening(); |
| | 0 | 98 | | FileMenuViewModel.ShowContent(contentType); |
| | | 99 | | } |
| | | 100 | | else |
| | | 101 | | { |
| | 6 | 102 | | CloseFileMenuContent(); |
| | | 103 | | } |
| | | 104 | | } |
| | | 105 | | else |
| | | 106 | | { |
| | 0 | 107 | | OpenFileMenuContent(contentType); |
| | | 108 | | } |
| | | 109 | | |
| | | 110 | | break; |
| | | 111 | | } |
| | 0 | 112 | | } |
| | | 113 | | |
| | | 114 | | /// <summary> |
| | | 115 | | /// Applies a drawer visibility action without changing content. |
| | | 116 | | /// </summary> |
| | | 117 | | public void SetDrawer(ShellDrawerAction action) |
| | | 118 | | { |
| | 3 | 119 | | if (action.WillOpen(Drawer.IsFileMenuOpen)) |
| | 3 | 120 | | NotifyDrawerOpening(); |
| | | 121 | | |
| | 3 | 122 | | Drawer.IsFileMenuOpen = action.ApplyToOpenState(Drawer.IsFileMenuOpen); |
| | 3 | 123 | | } |
| | | 124 | | |
| | | 125 | | private void NotifyDrawerOpening() |
| | | 126 | | { |
| | 24 | 127 | | if (!Drawer.IsFileMenuOpen) |
| | 24 | 128 | | onDrawerOpening?.Invoke(); |
| | 12 | 129 | | } |
| | | 130 | | } |
| | | 131 | | |