Skip to main content

IEditorAction Interface

Definition

Assembly:Avalonia.Controls.RichTextEditor
Package:Avalonia.Controls.RichTextEditor

Represents an action that can be executed on a RichTextEditor.

public interface IEditorAction

Remarks

Editor actions provide a unified abstraction for operations like formatting, clipboard operations, and undo/redo. They can be used across multiple UI surfaces: toolbar buttons, context menus, and keyboard shortcuts.

Actions are typically singletons accessed via the Avalonia.Controls.Documents.Primitives.Actions.EditorActions class.

Methods

NameDescription
CanExecuteGets whether the action can currently execute.
ExecuteExecutes the action synchronously.
ExecuteAsyncExecutes the action asynchronously.
GetStateGets the current state of the action.

CanExecute Method

Gets whether the action can currently execute.

public bool CanExecute(Avalonia.Controls.Documents.Primitives.ITextEditorHost host)

Parameters

host Avalonia.Controls.Documents.Primitives.ITextEditorHost

The editor host to check against.

Returns

bool

True if the action can execute; otherwise, false.

Remarks

This is called frequently to update UI state (button enabled/disabled). Implementations should be fast and avoid heavy computation.

Execute Method

Executes the action synchronously.

public void Execute(Avalonia.Controls.Documents.Primitives.ITextEditorHost host)

Parameters

host Avalonia.Controls.Documents.Primitives.ITextEditorHost

The editor host to execute on.

Remarks

For actions that require async operations (like file dialogs), use Avalonia.Controls.Documents.Primitives.Actions.IEditorAction.ExecuteAsync(Avalonia.Controls.Documents.Primitives.ITextEditorHost) instead.

ExecuteAsync Method

Executes the action asynchronously.

public System.Threading.Tasks.Task ExecuteAsync(Avalonia.Controls.Documents.Primitives.ITextEditorHost host)

Parameters

host Avalonia.Controls.Documents.Primitives.ITextEditorHost

The editor host to execute on.

Returns

System.Threading.Tasks.Task

A task representing the asynchronous operation.

Remarks

The default implementation calls Avalonia.Controls.Documents.Primitives.Actions.IEditorAction.Execute(Avalonia.Controls.Documents.Primitives.ITextEditorHost) synchronously. Override this for actions that need async operations like file dialogs.

GetState Method

Gets the current state of the action.

public object GetState(Avalonia.Controls.Documents.Primitives.ITextEditorHost host)

Parameters

host Avalonia.Controls.Documents.Primitives.ITextEditorHost

The editor host to query.

Returns

object

The current state, or null if the action has no state. For toggle actions, this returns a boolean. For property actions, this returns the current property value.

Properties

NameDescription
DisplayNameGets the display name for UI purposes.
GestureGets the optional keyboard gesture for this action.
IdGets the unique identifier for this action.

DisplayName Property

Gets the display name for UI purposes.

public string DisplayName { get; set; }

Remarks

This is used for tooltips, menu items, and accessibility.

Gesture Property

Gets the optional keyboard gesture for this action.

public Avalonia.Input.KeyGesture Gesture { get; set; }

Remarks

When set, this gesture is displayed in tooltips and can be automatically registered as a keyboard shortcut.

Id Property

Gets the unique identifier for this action.

public string Id { get; set; }

Remarks

IDs follow a hierarchical naming convention:

  • Edit.Undo, Edit.Redo, Edit.Cut, etc.
  • Format.RichBold, Format.RichItalic, etc.
  • Paragraph.AlignLeft, Paragraph.BulletList, etc.