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. A toggle action returns a boolean, a property action the current value.

Remarks​

Read Avalonia.Controls.Documents.Primitives.Actions.IToggleAction.IsChecked(Avalonia.Controls.Documents.Primitives.ITextEditorHost) or Avalonia.Controls.Documents.Primitives.Actions.IPropertyAction.GetValueAsObject(Avalonia.Controls.Documents.Primitives.ITextEditorHost) instead. Both say which family the value came from, which an untyped object cannot.

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​

The gesture is what tooltips and menu items display. It registers nothing: the shortcut that actually fires is handled by the editor's keyboard component, so an action whose gesture differs from the one the editor handles advertises a shortcut that does nothing.

Id Property​

Gets the unique identifier for this action.

public string Id { get; set; }

Remarks​

IDs follow a hierarchical naming convention: