static void BindStringValue( this NSCustomCaretColorTextField control, IProperty <string> value, IObservable <bool> isRooted) { var setByUser = false; var hasUnsavedChanges = false; value = value .ConnectWhile(isRooted) .DistinctUntilChangedOrSet(); DataBinding .ObservableFromNativeEvent <EventArgs>(control, "Changed") .Subscribe(_ => { if (!setByUser) { return; } hasUnsavedChanges = true; value.Write(control.StringValue, save: false); }); value.Subscribe(v => Fusion.Application.MainThread.Schedule(() => { setByUser = false; try { control.StringValue = v; } finally { setByUser = true; } })); ((IObservableResponder)control).LostFocus.Subscribe(_ => { if (!hasUnsavedChanges) { return; } value.Write(control.StringValue, save: true); hasUnsavedChanges = false; }); }
public static void Initialize(IScheduler dispatcher) { TextBox.Implementation.Factory = (value, isMultiLine, doWrap, onFocused, foregroundColor) => { value = value.PreventFeedback(); return(Control .Create(self => { var ctrl = new NSCustomCaretColorTextField() { Editable = true, Bordered = false, Cell = doWrap ? new NSTextFieldCell() { Font = NSFont.SystemFontOfSize(11), Wraps = doWrap, Editable = true, } : new VerticallyCenteredTextFieldCell() { Font = NSFont.SystemFontOfSize(11), Wraps = doWrap, Editable = true, }, FocusRingType = NSFocusRingType.None }; onFocused.Do(handler => ((IObservableResponder)ctrl).GotFocus .WithLatestFromBuffered(handler.Execute.ConnectWhile(self.IsRooted), (_, h) => h) .Subscribe(c => c())); self.BindNativeDefaults(ctrl, dispatcher); self.BindNativeProperty(dispatcher, "Enabled", value.IsReadOnly, isReadOnly => ctrl.Enabled = !isReadOnly); ctrl.BindStringValue(value, self.IsRooted); self.BindNativeProperty(dispatcher, "foreground", foregroundColor, fg => { ctrl.TextColor = ctrl.CaretColor = fg.ToNSColor(); }); return ctrl; }) .WithHeight(20)); }; }