< Summary

Information
Class: MyNet.UI.ViewModels.Shell.FileMenu.FileMenuViewModel
Assembly: MyNet.UI
File(s): https://raw.githubusercontent.com/sandre58/MyNet/85372080fe102cd9ee155ceab49ae000e7f66103/src/MyNet.UI/ViewModels/Shell/FileMenu/FileMenuViewModel.cs
Tag: 323_28699572109
Line coverage
73%
Covered lines: 19
Uncovered lines: 7
Coverable lines: 26
Total lines: 102
Line coverage: 73%
Branch coverage
62%
Covered branches: 5
Total branches: 8
Branch coverage: 62.5%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.ctor(...)100%22100%
set_Content(...)100%11100%
ShowContent()100%210%
ShowContent(...)50%22100%
HideContent()100%22100%
ToggleContent(...)0%620%
ContentIsVisible(...)100%11100%
ContentIsVisible()100%210%

File(s)

https://raw.githubusercontent.com/sandre58/MyNet/85372080fe102cd9ee155ceab49ae000e7f66103/src/MyNet.UI/ViewModels/Shell/FileMenu/FileMenuViewModel.cs

#LineLine coverage
 1// -----------------------------------------------------------------------
 2// <copyright file="FileMenuViewModel.cs" company="Stéphane ANDRE">
 3// Copyright (c) Stéphane ANDRE. All rights reserved.
 4// </copyright>
 5// -----------------------------------------------------------------------
 6
 7using System;
 8using System.Collections.Generic;
 9using System.Linq;
 10using System.Windows.Input;
 11using MyNet.UI.Commands;
 12using MyNet.UI.ViewModels.Workspace;
 13
 14namespace 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>
 20public sealed class FileMenuViewModel : ViewModelBase
 21{
 3022    private readonly HashSet<IWorkspaceViewModel> _contentViewModels = [];
 23
 24    /// <summary>
 25    /// Initializes a new instance of the <see cref="FileMenuViewModel"/> class.
 26    /// </summary>
 3027    public FileMenuViewModel(
 3028        IEnumerable<IWorkspaceViewModel> contentViewModels,
 3029        ICommandFactory? commandFactory = null)
 30    {
 3031        ArgumentNullException.ThrowIfNull(contentViewModels);
 32
 12033        foreach (var workspace in contentViewModels)
 3034            _contentViewModels.Add(workspace);
 35
 3036        var commands = commandFactory.GetOrDefault();
 3037        ToggleFileMenuContentCommand = commands.CreateRequired<Type>(ToggleContent);
 3038    }
 39
 40    /// <summary>
 41    /// Gets or sets the workspace currently displayed in the file menu.
 42    /// </summary>
 3343    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
 055        => ShowContent(typeof(T));
 56
 57    /// <summary>
 58    /// Shows the workspace registered for <paramref name="contentType"/>.
 59    /// </summary>
 60    public void ShowContent(Type contentType)
 61    {
 2162        ArgumentNullException.ThrowIfNull(contentType);
 63
 2164        if (!ContentIsVisible(contentType))
 2165            Content = _contentViewModels.FirstOrDefault(contentType.IsInstanceOfType);
 2166    }
 67
 68    /// <summary>
 69    /// Hides the current file menu content.
 70    /// </summary>
 71    public void HideContent()
 72    {
 1873        if (Content is not null)
 1274            Content = null;
 1875    }
 76
 77    /// <summary>
 78    /// Toggles visibility of the workspace registered for <paramref name="contentType"/>.
 79    /// </summary>
 80    public void ToggleContent(Type contentType)
 81    {
 082        ArgumentNullException.ThrowIfNull(contentType);
 83
 084        if (!ContentIsVisible(contentType))
 085            ShowContent(contentType);
 86        else
 087            HideContent();
 088    }
 89
 90    /// <summary>
 91    /// Returns whether <paramref name="contentType"/> matches the current <see cref="Content"/>.
 92    /// </summary>
 2793    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
 0100        => ContentIsVisible(typeof(T));
 101}
 102