| | | 1 | | // ----------------------------------------------------------------------- |
| | | 2 | | // <copyright file="ShellService.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.Services; |
| | | 11 | | |
| | | 12 | | /// <inheritdoc /> |
| | | 13 | | public sealed class ShellService(IShellHostProvider hostProvider) : IShellService |
| | | 14 | | { |
| | 15 | 15 | | private readonly IShellHostProvider _hostProvider = hostProvider ?? throw new ArgumentNullException(nameof(hostProvi |
| | | 16 | | |
| | | 17 | | /// <inheritdoc /> |
| | 3 | 18 | | public bool IsAvailable => _hostProvider.Current is not null; |
| | | 19 | | |
| | | 20 | | /// <inheritdoc /> |
| | 0 | 21 | | public bool IsFileMenuAvailable => TryGetFileMenuHost(out _); |
| | | 22 | | |
| | | 23 | | /// <inheritdoc /> |
| | 3 | 24 | | public void CloseDrawers() => RequireHost().CloseDrawers(); |
| | | 25 | | |
| | | 26 | | /// <inheritdoc /> |
| | 0 | 27 | | public void SetNotificationsDrawer(ShellDrawerAction action) => RequireHost().SetNotificationsDrawer(action); |
| | | 28 | | |
| | | 29 | | /// <inheritdoc /> |
| | 0 | 30 | | public void SetFileMenuDrawer(ShellDrawerAction action) => RequireHost().SetFileMenuDrawer(action); |
| | | 31 | | |
| | | 32 | | /// <inheritdoc /> |
| | | 33 | | public void OpenFileMenuContent<T>() |
| | | 34 | | where T : class, IWorkspaceViewModel |
| | 3 | 35 | | => RequireFileMenuHost().OpenFileMenuContent<T>(); |
| | | 36 | | |
| | | 37 | | /// <inheritdoc /> |
| | 0 | 38 | | public void OpenFileMenuContent(Type contentType) => RequireFileMenuHost().OpenFileMenuContent(contentType); |
| | | 39 | | |
| | | 40 | | /// <inheritdoc /> |
| | 0 | 41 | | public void CloseFileMenuContent() => RequireFileMenuHost().CloseFileMenuContent(); |
| | | 42 | | |
| | | 43 | | /// <inheritdoc /> |
| | | 44 | | public void SetFileMenuContentVisibility<T>(ShellDrawerAction action) |
| | | 45 | | where T : class, IWorkspaceViewModel |
| | 3 | 46 | | => RequireFileMenuHost().SetFileMenuContentVisibility<T>(action); |
| | | 47 | | |
| | | 48 | | /// <inheritdoc /> |
| | | 49 | | public void SetFileMenuContentVisibility(Type contentType, ShellDrawerAction action) |
| | 0 | 50 | | => RequireFileMenuHost().SetFileMenuContentVisibility(contentType, action); |
| | | 51 | | |
| | | 52 | | private IShellHost RequireHost() |
| | 3 | 53 | | => _hostProvider.Current |
| | 3 | 54 | | ?? throw new InvalidOperationException("No shell host is attached. Register ShellHostViewModel with IShellHos |
| | | 55 | | |
| | | 56 | | private IShellHostWithFileMenu RequireFileMenuHost() |
| | 6 | 57 | | => TryGetFileMenuHost(out var host) |
| | 6 | 58 | | ? host |
| | 6 | 59 | | : throw new InvalidOperationException( |
| | 6 | 60 | | "The attached shell host does not have a file menu. Pass FileMenuViewModel to ShellHostViewModel."); |
| | | 61 | | |
| | | 62 | | private bool TryGetFileMenuHost(out IShellHostWithFileMenu host) |
| | | 63 | | { |
| | 6 | 64 | | if (_hostProvider.Current is IShellHostWithFileMenu fileMenuHost and IShellCapabilities { HasFileMenu: true }) |
| | | 65 | | { |
| | 3 | 66 | | host = fileMenuHost; |
| | 3 | 67 | | return true; |
| | | 68 | | } |
| | | 69 | | |
| | 3 | 70 | | host = null!; |
| | 3 | 71 | | return false; |
| | | 72 | | } |
| | | 73 | | } |
| | | 74 | | |