|
| 1 | +using System.ComponentModel; |
| 2 | +using System.Windows.Input; |
| 3 | +using SquidStd.Tui.Internal; |
| 4 | + |
| 5 | +namespace SquidStd.Tui.Binding; |
| 6 | + |
| 7 | +/// <summary> |
| 8 | +/// Wires ViewModel (<see cref="INotifyPropertyChanged" />) properties and commands to view targets, and |
| 9 | +/// owns the lifetime of every subscription it creates. Dispose to release them all. |
| 10 | +/// </summary> |
| 11 | +public sealed partial class ViewBinder : IDisposable |
| 12 | +{ |
| 13 | + private readonly List<IDisposable> _subscriptions = new(); |
| 14 | + private readonly Action<Action> _marshal; |
| 15 | + |
| 16 | + /// <summary>Initialises the binder with an optional marshal action for UI-thread dispatch.</summary> |
| 17 | + /// <param name="marshal"> |
| 18 | + /// Runs an update on the UI thread. Defaults to running inline; the host passes a delegate over |
| 19 | + /// <c>Application.Invoke</c> so updates raised from background threads reach the Terminal.Gui loop. |
| 20 | + /// </param> |
| 21 | + public ViewBinder(Action<Action>? marshal = null) |
| 22 | + { |
| 23 | + _marshal = marshal ?? (action => action()); |
| 24 | + } |
| 25 | + |
| 26 | + /// <summary>Applies <paramref name="apply" /> now and whenever <paramref name="propertyName" /> changes.</summary> |
| 27 | + public void OneWay(INotifyPropertyChanged source, string propertyName, Action apply) |
| 28 | + { |
| 29 | + apply(); |
| 30 | + |
| 31 | + void Handler(object? sender, PropertyChangedEventArgs e) |
| 32 | + { |
| 33 | + if (e.PropertyName is null || string.Equals(e.PropertyName, propertyName, StringComparison.Ordinal)) |
| 34 | + { |
| 35 | + _marshal(apply); |
| 36 | + } |
| 37 | + } |
| 38 | + |
| 39 | + source.PropertyChanged += Handler; |
| 40 | + _subscriptions.Add(new Unsubscriber(() => source.PropertyChanged -= Handler)); |
| 41 | + } |
| 42 | + |
| 43 | + /// <summary> |
| 44 | + /// Binds a source property both ways. <paramref name="applyToTarget" /> runs on source changes; |
| 45 | + /// <paramref name="writeToSource" /> runs when the target raises a change. A reentrancy guard stops |
| 46 | + /// the source→target→source feedback loop. |
| 47 | + /// </summary> |
| 48 | + public void TwoWay( |
| 49 | + INotifyPropertyChanged source, |
| 50 | + string propertyName, |
| 51 | + Action applyToTarget, |
| 52 | + Action<Action> subscribeTargetChanged, |
| 53 | + Action writeToSource |
| 54 | + ) |
| 55 | + { |
| 56 | + var guard = new ReentryGuard(); |
| 57 | + |
| 58 | + applyToTarget(); |
| 59 | + |
| 60 | + void SourceHandler(object? sender, PropertyChangedEventArgs e) |
| 61 | + { |
| 62 | + if (e.PropertyName is not null && !string.Equals(e.PropertyName, propertyName, StringComparison.Ordinal)) |
| 63 | + { |
| 64 | + return; |
| 65 | + } |
| 66 | + |
| 67 | + if (guard.IsBusy) |
| 68 | + { |
| 69 | + return; |
| 70 | + } |
| 71 | + |
| 72 | + _marshal(() => |
| 73 | + { |
| 74 | + using (guard.Enter()) |
| 75 | + { |
| 76 | + applyToTarget(); |
| 77 | + } |
| 78 | + }); |
| 79 | + } |
| 80 | + |
| 81 | + source.PropertyChanged += SourceHandler; |
| 82 | + _subscriptions.Add(new Unsubscriber(() => source.PropertyChanged -= SourceHandler)); |
| 83 | + |
| 84 | + subscribeTargetChanged(() => |
| 85 | + { |
| 86 | + if (guard.IsBusy) |
| 87 | + { |
| 88 | + return; |
| 89 | + } |
| 90 | + |
| 91 | + using (guard.Enter()) |
| 92 | + { |
| 93 | + writeToSource(); |
| 94 | + } |
| 95 | + }); |
| 96 | + } |
| 97 | + |
| 98 | + /// <summary> |
| 99 | + /// Binds a command to a control: <paramref name="subscribeTrigger" /> activates the command (when it |
| 100 | + /// can execute), and <paramref name="setEnabled" /> tracks <see cref="ICommand.CanExecute" />. |
| 101 | + /// </summary> |
| 102 | + public void Command(ICommand command, Action<bool> setEnabled, Action<Action> subscribeTrigger) |
| 103 | + { |
| 104 | + setEnabled(command.CanExecute(null)); |
| 105 | + |
| 106 | + void CanHandler(object? sender, EventArgs e) |
| 107 | + { |
| 108 | + _marshal(() => setEnabled(command.CanExecute(null))); |
| 109 | + } |
| 110 | + |
| 111 | + command.CanExecuteChanged += CanHandler; |
| 112 | + _subscriptions.Add(new Unsubscriber(() => command.CanExecuteChanged -= CanHandler)); |
| 113 | + |
| 114 | + subscribeTrigger(() => |
| 115 | + { |
| 116 | + if (command.CanExecute(null)) |
| 117 | + { |
| 118 | + command.Execute(null); |
| 119 | + } |
| 120 | + }); |
| 121 | + } |
| 122 | + |
| 123 | + /// <summary>Disposes all active subscriptions created by this binder.</summary> |
| 124 | + public void Dispose() |
| 125 | + { |
| 126 | + for (var i = 0; i < _subscriptions.Count; i++) |
| 127 | + { |
| 128 | + _subscriptions[i].Dispose(); |
| 129 | + } |
| 130 | + |
| 131 | + _subscriptions.Clear(); |
| 132 | + } |
| 133 | +} |
0 commit comments