Skip to content

CommandManager

0x5BFA edited this page Jan 19, 2025 · 12 revisions

Overview

Usage

Initialization

CommandManager.Initialize(CommandExecutionContexts.Browser);

CommandManager.Initialize(CommandExecutionContexts.Sidebar);

CommandManager.Initialize(CommandExecutionContexts.Home); // Temporary

CommandManager.Initialize(CommandExecutionContexts.Tabs);

Execute a command

// Sync
if (CommandManager.NavigateBack.CanExecute())
    CommandManager.NavigateBack.Execute();

// Async
if (async CommandManager.NavigateBack.CanExecuteAsync())
    async CommandManager.NavigateBack.ExecuteAsync();

// Sync with parameter
if (CommandManager.NavigateBack.CanExecute(parameter))
    CommandManager.NavigateBack.Execute(parameter);

Cresting a new command

[RichCommand]
internal class DeleteAction : IAction
{
    // Only can executes with the Browser context
    public static CommandExecutionContexts ExecutableContexts = CommandExecutionContexts.Browser;

    public string Title => "";

    public string Description => "";

    public HotKey DefaultHotKey => new(...);

    public Task<bool> CanExecuteAsync()
    {
        // Verify the executability

        return Task.FromResult(...);
    }

    public Task ExecuteAsync(CommandExecutionContexts context, object? parameter)
    {
        if (!ExecutableContexts.HasFlag(context)
            return throw new InvalidOperarionException($"This command cannot be executed with the given context: {context}");

        // Execute...
    }
}

Source generator

RichCommandAttribute

[AttributeUsage(AttributeTargets.Class, AllowMultiple = false, Inherited = false)]
public sealed class RichCommandAttribute : Attribute {}
// CommandManager.g.cs
namespace global::Files.App.Data
{
    internal partial class CommandManager
    {
        private readonly FrozenDictionary<CommandCodes, IRichCommand> commands;
        private readonly FrozenDictionary<CommandCodes, IRichCommand> modifiableCommands;

        public IRichCommand NavigateBack { get; } = new ActionCommand(CommandCodes.NavigateBack, new NavigateBackAction());
...

        public bool Initialize(CommandExecutionContexts context)
        {
..

            commands =
            [
            [CommandCodes.NavigateBack] = new NavigateBackAction(),
...
            ];

...
        }
    }

    internal enum CommandCodes
    {
        NavigateBack,
...
    }
}

Modifiable command

public void AppendModifiableCommands()
{
    modifiableCommands = [ ... ];
}
```c#
Controls
  • FolderBrowser
  • DetailsFolderView
  • GridFolderView
  • ListFolderView
  • TilesFolderView
  • ContentFolderView
  • ColumnsFolderView
  • TreeFolderView
  • GalleryFolderView
  • HomeFolderView
  • RectanglurSelectionVisual
  • DataGrid
  • SidebarView
  • Omnibar
  • Toolbar
  • FilePreviewPresenter
  • ColorTags
  • RichTokens
  • TerminalView
API
  • WindowsStorable
  • ArchiveStorable
  • HomeStorable
  • FtpStorable
  • SftpStorable
  • WebDAVStorable
Infrastructure
  • CommandManager
  • MultitaskingManager
  • DialogManager
  • AppSettings
  • OperationServer

Copyright © 2025 0x5BFA. All rights reserved. Do not copy or redistribute modified versions.

Clone this wiki locally