|
| 1 | +# Terminal UI (MVVM) |
| 2 | + |
| 3 | +Build a Counter terminal app with an observable ViewModel, then display it two ways — the |
| 4 | +imperative `TuiView<T>` and the declarative `TuiComposedView<T>` DSL. |
| 5 | + |
| 6 | +## What you'll build |
| 7 | + |
| 8 | +A small Counter app where a `CounterViewModel` holds a `Value` property and an `IncrementCommand`. |
| 9 | +You will see both ways to build the view: |
| 10 | + |
| 11 | +1. **Imperative** — `TuiView<T>`: override `BuildLayout` to add Terminal.Gui widgets, then |
| 12 | + override `Bind` to wire them through `ViewBinder`. |
| 13 | +2. **Declarative** — `TuiComposedView<T>`: override `Compose` and return a `TuiNode` tree built |
| 14 | + with the `Ui.*` DSL; bindings and layout are inferred from the node types. |
| 15 | + |
| 16 | +## Prerequisites |
| 17 | + |
| 18 | +- .NET 10 SDK |
| 19 | +- `dotnet add package SquidStd.Tui` |
| 20 | + |
| 21 | +## Steps |
| 22 | + |
| 23 | +### 1. Define the ViewModel |
| 24 | + |
| 25 | +Derive from `TuiViewModel` (which extends `ObservableObject` from CommunityToolkit.Mvvm) and mark |
| 26 | +observable properties and relay commands with the standard source-generator attributes. |
| 27 | + |
| 28 | +```csharp |
| 29 | +using CommunityToolkit.Mvvm.ComponentModel; |
| 30 | +using CommunityToolkit.Mvvm.Input; |
| 31 | +using SquidStd.Tui.ViewModels; |
| 32 | + |
| 33 | +public sealed partial class CounterViewModel : TuiViewModel |
| 34 | +{ |
| 35 | + [ObservableProperty] |
| 36 | + private string _title = "Counter"; |
| 37 | + |
| 38 | + [ObservableProperty] |
| 39 | + private string _value = "0"; |
| 40 | + |
| 41 | + [RelayCommand] |
| 42 | + private void Increment() => Value = (int.Parse(Value) + 1).ToString(); |
| 43 | +} |
| 44 | +``` |
| 45 | + |
| 46 | +### 2. Imperative view — `TuiView<T>` |
| 47 | + |
| 48 | +Override `BuildLayout` to create and add Terminal.Gui widgets, then override `Bind` to wire them |
| 49 | +through `ViewBinder`. The binder tracks property-changed notifications and updates widgets without |
| 50 | +polling. |
| 51 | + |
| 52 | +```csharp |
| 53 | +using Terminal.Gui; |
| 54 | +using SquidStd.Tui.Views; |
| 55 | +using SquidStd.Tui.Binders; |
| 56 | + |
| 57 | +public sealed class CounterView : TuiView<CounterViewModel> |
| 58 | +{ |
| 59 | + private Label _value = null!; |
| 60 | + private Button _inc = null!; |
| 61 | + |
| 62 | + protected override void BuildLayout() |
| 63 | + { |
| 64 | + _value = new Label { X = 1, Y = 1 }; |
| 65 | + _inc = new Button { X = 1, Y = 3, Text = "+1" }; |
| 66 | + Add(_value, _inc); |
| 67 | + } |
| 68 | + |
| 69 | + protected override void Bind(ViewBinder b) |
| 70 | + { |
| 71 | + b.OneWayTitle(ViewModel, x => x.Title, this); |
| 72 | + b.OneWayText(ViewModel, x => x.Value, _value); |
| 73 | + b.Command(_inc, ViewModel.IncrementCommand); |
| 74 | + } |
| 75 | +} |
| 76 | +``` |
| 77 | + |
| 78 | +### 3. Declarative view — `TuiComposedView<T>` |
| 79 | + |
| 80 | +Instead of `BuildLayout` + `Bind`, derive from `TuiComposedView<T>` and return a node tree from |
| 81 | +`Compose`. The framework infers binding direction from the node type (Label → one-way, TextField → |
| 82 | +two-way, Button → command) and applies layout automatically. |
| 83 | + |
| 84 | +```csharp |
| 85 | +using SquidStd.Tui.Views; |
| 86 | +using SquidStd.Tui.Dsl; |
| 87 | + |
| 88 | +public sealed class CounterView : TuiComposedView<CounterViewModel> |
| 89 | +{ |
| 90 | + protected override TuiNode<CounterViewModel> Compose() => |
| 91 | + Ui.VStack( |
| 92 | + Ui.Label(x => x.Title), |
| 93 | + Ui.Label(x => x.Value), |
| 94 | + Ui.Button("+1", x => x.IncrementCommand)); |
| 95 | +} |
| 96 | +``` |
| 97 | + |
| 98 | +`Ui.VStack` / `Ui.HStack` stack children vertically or horizontally and auto-assign `Y`/`X` |
| 99 | +offsets. `Ui.TextField(x => x.Name)` is two-way by default; pass `BindMode.OneWay` to override. |
| 100 | + |
| 101 | +### 4. Register and run |
| 102 | + |
| 103 | +`RegisterTui()` adds the core TUI services and `TuiApplicationHost` to the DryIoc container. |
| 104 | +`RegisterView<TView, TViewModel>()` registers the view for ViewModel-first resolution. |
| 105 | +`TuiApplicationHost.RunAsync<TViewModel>()` resolves the root ViewModel, shows its view, and starts |
| 106 | +the Terminal.Gui event loop. |
| 107 | + |
| 108 | +```csharp |
| 109 | +using DryIoc; |
| 110 | +using SquidStd.Tui.Extensions; |
| 111 | +using SquidStd.Tui.Hosts; |
| 112 | + |
| 113 | +var container = new Container().RegisterTui(); |
| 114 | +container.RegisterView<CounterView, CounterViewModel>(); |
| 115 | + |
| 116 | +await container.Resolve<TuiApplicationHost>().RunAsync<CounterViewModel>(); |
| 117 | +``` |
| 118 | + |
| 119 | +### 5. ViewModel-first navigation |
| 120 | + |
| 121 | +Push a new ViewModel onto the navigation stack from inside an existing ViewModel via the injected |
| 122 | +`ITuiNavigator`: |
| 123 | + |
| 124 | +```csharp |
| 125 | +// inside CounterViewModel |
| 126 | +await Navigator.NavigateToAsync<DetailViewModel>(); |
| 127 | +await Navigator.BackAsync(); |
| 128 | +``` |
| 129 | + |
| 130 | +The navigator resolves the view that was registered for `DetailViewModel` and shows it; `BackAsync` |
| 131 | +pops the stack and restores the previous view. |
| 132 | + |
| 133 | +## Next steps |
| 134 | + |
| 135 | +- [SquidStd.Tui reference](../articles/tui.md) |
0 commit comments