Gum tool viewmodels

From vchelaru/Gum · View interactive version

Gum tool viewmodels is an AI agent skill from the GitHub repository vchelaru/Gum. Gum tool ViewModel conventions. Triggers: ViewModels, XAML views, data binding, DependsOn, visibility properties.

How to use this skill

Skill file

Gum Tool ViewModel Conventions

Base Classes

Property Patterns

Stored properties use Get<T>()/Set():

public string Name
{
    get => Get<string>() ?? string.Empty;
    set => Set(value);
}

A backing field with Set(ref _field, value) is the faster alternative for hot properties (no dictionary lookup; a field initializer counts as already set, so assigning the initial value raises nothing). Tool view models keep Get<T>()/Set(value) by default.

Derived properties must use [DependsOn] so changes to the source property automatically raise PropertyChanged for the derived property. Without this, the UI will not update.

[DependsOn(nameof(SourceType))]
public bool IsLocalFile => SourceType == SourceType.LocalFile;

Multiple dependencies are expressed with multiple attributes:

[DependsOn(nameof(IsPreviewLoaded))]
[DependsOn(nameof(IsLoading))]
public bool CanImport => IsPreviewLoaded && !IsLoading;

View Logic on the VM, Not in XAML

The boundary is logic vs. theming:

Do not use IValueConverter or DataTrigger for logic. The one exception is themed brushes (see below).

Visibility example — expose a bool, not WPF Visibility (ADR-0004), so the VM can move into the headless assembly:

[DependsOn(nameof(ErrorMessage))]
public bool IsErrorMessageVisible => !string.IsNullOrEmpty(ErrorMessage);

XAML binds through the stock BooleanToVisibilityConverter (a pure type-adapter, not logic):

<TextBlock Visibility="{Binding IsErrorMessageVisible, Converter={StaticResource BoolToVisibilityConverter}}" />

Never expose System.Windows types from a VM — Visibility, Color, Brush, WriteableBitmap. They pin the VM to the WPF assembly and defeat the compiler-enforced logic↔view boundary. Resolve display state in neutral types instead: bool for visibility, Gum's color type (RenderingLibrary has one) for colors, a color in place of a Brush, byte[]/a Gum image for pixels. Framework types live only in the view/converter layer. See Direction/decisions/0004-viewmodels-expose-neutral-presentation-state.md.

VMs in GumCommon or runtime projects must stay UI-agnostic — this rule is for tool code only.

Exception: themed brushes

Light/dark theming uses brushes defined in Gum/Themes/Frb.Brushes.{Light,Dark}.xaml (e.g. Frb.Brushes.Error). These must be resolved with {DynamicResource ...} so a runtime theme switch repaints — that only works from a FrameworkElement, not from a VM-side Brush property. The right pattern:

This is the only situation where DataTrigger is preferred over a VM-side property.

A clickable text link in the Avalonia head is a HyperlinkButton, never a transparent Button: the head's Button style forces white Primary.Contrast text, while HyperlinkButton takes Frb.Brushes.Link per theme through FrbThemeResources.ControlBrushAliases.

Common Pitfalls

Missing [DependsOn]: If a getter computes from another property but lacks the attribute, the UI will show stale values. The ViewModel constructor scans for [DependsOn] via reflection at construction time — it only works if the attribute is present.

Two-way derived properties: Properties like IsLocalFile that both read from and write to a backing property need [DependsOn] for the read direction. The write direction (setter updating SourceType) works normally through Set().

Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.