mirror of
https://github.com/gui-cs/Terminal.Gui.git
synced 2025-12-26 15:57:56 +01:00
Fixes #3192. Improve correctness / clarity of existing View.AutoSize functionality/unit tests (#3202)
* Removed resharper settings from editorconfig * Remove constructors with frame parameters from Button class. * Remove constructors with frame parameters from CheckBox class. * Cleanup code. * Remove constructors with frame parameters from ComboBox class. * @BDisp Remove constructors with frame parameters from FrameView class. * Remove constructors with frame parameters from Label class. * Remove constructors with frame parameters from ListView class. * Remove constructors with frame parameters from ScrollBarView class. * Remove constructors with frame parameters from ScrollView class. * Remove namespace braces. * Cleanup code. * Cleanup code. * Cleanup code. * Remove constructors with frame parameters from TextField class. * Remove constructors with frame parameters from TimeField class. * Fixes #3182. OnResizeNeeded returns int.MaxValue and int.MaxValue when Application.Top is null, should return Size.Empty. * Remove constructors with frame parameters from Toplevel class. * Remove constructors with frame parameters from Window class. * Fix merge errors. * Revert "Fixes #3182. OnResizeNeeded returns int.MaxValue and int.MaxValue when Application.Top is null, should return Size.Empty." This reverts commitcf9c24b846. * Revert unit test. * Fixes #2882. TabView: 'Frame.DrawFrame(Rect, bool)' is obsolete: 'This method is obsolete in v2. Use use LineCanvas or Frame (#2980) * Fixes #2882. TabView: 'Frame.DrawFrame(Rect, bool)' is obsolete: 'This method is obsolete in v2. Use use LineCanvas or Frame * Trying fix this unit test that sometimes fail. * Fixes #2983. View need a alternative DrawFrame for the v2. * Use new DrawFrame method. * Change _lines field to Lines property. * Add TabWindow unit test. * Add DrawIncompleteFrame method and unit tests. * Add more unit tests to LineCanvas. * Fix newline conflict errors. * Revert "Change _lines field to Lines property." This reverts commitab6c5f3094. * Add DrawIncompleteFrame method and unit tests. * Add more unit tests to LineCanvas. * Fix newline conflict errors. * Force render immediately instead of join. * I will never rely on zero-location-based unit test again. * Fix TestTreeViewColor unit test fail. * Using location of 3 to avoid be divisible by 2 and so avoiding bugs. * Revert "Using location of 3 to avoid be divisible by 2 and so avoiding bugs." This reverts commitdd3df135d8. * Revert "I will never rely on zero-location-based unit test again." This reverts commit62adf6f285. * Revert "Fix newline conflict errors." This reverts commit4acf72612d. * Revert "Add more unit tests to LineCanvas." This reverts commit66bc6f514e. * Revert "Add DrawIncompleteFrame method and unit tests." This reverts commit680ba264e1. * Resolving merge conflicts. * Revert "Use new DrawFrame method." This reverts commit69a7f17f19. * Revert "Fixes #2983. View need a alternative DrawFrame for the v2." This reverts commitdade9fd767. * Reverting this changes to start a new one. * Add horizontal and vertical support for combining glyphs. * Fix text and auto size behavior. * Add TabWidth property. * Add unit test for WordWrap. * Fixes #3017. View TextDirection returns incorrect size on a vertical direction instance with AutoSize as false. * Using Frame to force read from the get method. * Fix some issues with AutoSize and ForceValidatePosDim. * Fixing broken unit tests. * Restoring code I've broken. * Removing forgotten code. * Only LayoutStyle.Computed can change the Frame. * DateField and TimeField depends on LayoutStyle.Computed. * Fix unit tests related with LayoutStyle. * Implements tabs, left and right arrows as View. * Draws a minimum full border. * Adds missing XML parameter. * Adds assert tests for Frame. * Removes duplicates InlineData. * Adds more unit tests for minimum full border without Left and Right thickness. * Trying to fix the TestTreeViewColor unit test fail. * Prevents a user to set TextDirection to -1. * Prevents any invalid TextDirection value. * Removes (TextDirection)(-1). * Removes unnecessary TextDirection initialization. * Removes LayoutStyle. * Fixing unit tests with border. * Trying to fix TestTreeViewColor again. * Revert "Trying to fix TestTreeViewColor again." This reverts commitc2efa8e42e. * Trying to fix TestTreeViewColor again. * Fix merge errors. * Fix merge errors. * Restoring unit test. * Restores the right XML comment. * Fix Disposing unit tests that sometimes throws because some instances aren't cleared on others unit tests classes. * Fix Disposing unit tests that sometimes throws because some instances aren't cleared on others unit tests classes. * Only call OnResizeNeeded if it's LayoutStyle.Computed. * Fix merge errors. * Fix merge errors. * Fix unit tests fail. * Reformat. * Again. * Rename to OnDrawAdornments. * Fix failing unit tests. * Reduces indentation and cleanup code. * Cleanup code. * Fix bug done when cleanup. * Replace FrameHandledMouseEvent to AdornmentHandledMouseEvent. * Removes Tab constructor parameters. --------- Co-authored-by: Tig <tig@users.noreply.github.com> * Fix merge errors. * Remove constructors with parameters from Button. * Remove parenthesis on objects initializers from Button. * Remove constructors with parameters from CheckBox. * Remove parenthesis on objects initializers from CheckBox. * Remove constructors with parameters from ComboBox. * Remove constructors with parameters from FrameView. * Remove parenthesis on objects initializers from FrameView. * Initial commit * Renamed Direction enum for clarity in refactoring unit tests * Moved nav tests to NavigationTests * Moved view tests around * Cleaning up TextFormatter and View.AutoSize code * Fixed latent TextFormatter bug with \n * removed Application dependency on some autosize unit tests * Fixed Label tests to deal with auotsize overriding height/width * Fixed more label tests. WIP * Fixed all places where AutoSize = happend after setting Dims * Started adding new primitive View.Text tests * Code comments * WIP: Enforce that it makes no sense to set Width/Height if AutoSize = true. Update Unit tests to match. * WIP: Enforce that it makes no sense to set Width/Height if AutoSize = true. Update Unit tests to match. * Remove frame set from the View constructor and prevent SetRelativeLayout running if not yet initialized. * Changes needed for unit tests pass on remove parameters constructors from the Label class. * Remove constructors with parameters from Label. * Remove parenthesis on objects initializers from Label. * Prefix private fields with underscore. * Renamed to MaxLength. * Remove constructors with parameters from ListView. * MakeWrapper not needed anymore. * Remove parenthesis on objects initializers from ListView. * WIP: Enforce that it makes no sense to set Width/Height if AutoSize = true. Update Unit tests to match. * Massive code cleanup - use parameterless constructors and ensure AutoSize is set properly. Code reformat. * Massive code cleanup - use parameterless constructors and ensure AutoSize is set properly. Code reformat. * Fixed messagebox * Remove constructors with parameters from ScrollBarView and ScrollView. * Remove parenthesis on objects initializers from ScrollBarView and ScrollView. * Cleanup code. * Fix merge errors. * Add empty dotsettings for solution and projects. * Set ReSharper language analysis level for projects to C#12 * Make ReSharper consider itself the boss for style * Add rule to enforce property backing fields above the property * Disable auto-detection of naming rules so ReSharper doesn't change them by itself * Don't let someone's VS settings override the indent settings * Explicitly set tab width to 4 spaces and force spaces. * Rules to keep various multi-line constructs aligned within themselves * Curly brace rules (Using K&R style, per current project spec) * Blank line rules Mostly to add breaks in various situations, and also to enforce max of 1 blank line. * Increase auto-wrap to 160 from default of 120 * Line break at end of all files, to make Unixy systems happy * Keep attributes on their own lines except for methods and records that are themselves single-line * Increase attribute auto-wrap to 60 from default of 38 * Wrap/chop rules for long method signatures and record declarations Chop if either already multi-line or if over 8 parameters. Chop AFTER the left paren and BEFORE the first parameter. * Chop rules for generics For generics with multi-line type parameters or multiple type parameter constraints, chop in a way that has consistent alignment. * Always enforce enum members on their own lines * One-line functions completely on one line They're usually expression-bodied anyway, here, so this is almost irrelevant * Keep control flow statements on their own lines * Follow same chop rules for method invocation as method declaration * Chop long or multi-line method chains or patterns * Wrap rules for binary operators Operator at beginning of new lines Auto-chop if long or already multi-line * Spaces between keywords and their opening parentheses * Add file layout rules for organization of reorderable items like fields, properties, etc. Ugly XML, so load it up in the UI to look at it It's MOSTLY the ReSharper defaults, but with more aggressive sorting, generally by access modifier and name, within each grouping. * Deconstructors use per-member types * Use keywords for built-in types and increase severity of inspection for violations Also apply to IntPtr and similar, which should now be nint and similar * Apply syntax style on completion. * Use var when evident for built-in and simple types, but not elsewhere * Increase severity for some minor redundancy and clarity inspections * Enforce braces always required for blocks, and treat as error * Warn if a local function is not statement-bodied * Increase various inspection severities IF Condition => Severity: Attributes not wrapped property => Suggestion Constructors expression-bodied => Error default doesn't have type when it isn't clear => suggestion Namespaces not file-scoped => error Methods not statement-bodied => hint Null check pattern not the object property pattern => Error * Adjust preference order of null check patterns to make object pattern highest * XmlDoc rules to keep tags and contents aligned and wrapped * Add a few custom profiles for code cleanup and make the full profile default * Remove parts for languages not used in this solution * copied in v2_develop changes * Merged v2_develop * Added Begin/Init unit tests. Removed Application dependencey from AutoSizeFalse tests * TextFormatter.Lines -> GetLines () * Let ReSharper know we intend to localize things * TextFormatter code cleanup * copied in v2_develop changes * Merged v2_develop * Spaces aren't wanted here either. * Fix merge errors. * Fixes ContentBottomRightCorner related with https://github.com/gui-cs/Terminal.Gui/issues/3211#issue-2098878820 * Remove constructors with parameters from ContextMenu. * Remove commented code. * Remove constructors with parameters from OpenDialog. * Remove constructors with parameters from SaveDialog. * Remove constructors with parameters from TextField. * Remove constructors with parameters from TimeField. * Fix unit test. * Remove unnecessary SetInitialProperties method. * Remove unnecessary SetInitialProperties method. * Remove parenthesis on objects initializers from Toplevel and Window. * Remove constructors with parameters from RadioGroup. * Remove constructors with parameters from TextView. * Remove constructors with parameters from MenuBar. * TEMPORARY: Turn everything that was set to error down to warning or lower * Fixes #3219. MenuBar is opened by call OpenMenu even it's disabled. * Remove constructors with parameters from Menu. * Remove constructors with parameters from View. * Change constructor to internal because is mainly useful for testing. * ReSharper Cleanup Code. * Added format only r# config * Fixes #3224. TextFormatter.Lines should return a single string.Empty list even with Width or Height equal to zero. * Remove constructors with parameters from Dialog. * Remove constructors with parameters from TextValidateField. * Fixes https://github.com/gui-cs/Terminal.Gui/issues/3224#issuecomment-1924096038 * Fixes #3225. Press CursorDown on TabView doesn't move to the next view. * Fixes #3229. TextFormatter should have a FillRemaining property. * Testing formatting merge in bdisp * Testing formatting merge * Testing formatting merge bdisp * Testing formatting merge 2 * xmldoc format * R# Full Code Cleanup * R# Full Code Cleanup2 * R# Full Code Cleanup2 * Merged! But broke tests * Refixing... * Refixed DrawTests * Refixed ViewTests * Refixed Text Tests * Refixed more unit tests * Refixed scenarios * Refixed rest of scenarios * Refixed ViewsTests * Refixed rest of tests. All unit tests pass again! * Fixed warnings * Updated R# version. Added new code cleanup settings * Applied latest code cleanup to solution * Another code cleanup pass * Tweaked r# settings. .editorconfig now matches. * r# else on separate line * r# - update * r# - full solution * test commit * test commit * test commit * Removed extra profiles * Full cleanup following cleaning up profiles * Null checking pattern rules/inspections * Tabs and extra whitespace are evil * Attributes on their own lines * Code layout rules to put fields first and to put backing fields with their properties * Merged v2_develop * Full cleanup following dodexahedron's PR --------- Co-authored-by: BDisp <bd.bdisp@gmail.com> Co-authored-by: Brandon Thetford <github@snapsinazfs.com>
This commit is contained in:
@@ -1,185 +1,205 @@
|
||||
using System.Reactive.Disposables;
|
||||
using System.Reactive.Linq;
|
||||
using System.Text;
|
||||
using ReactiveMarbles.ObservableEvents;
|
||||
using ReactiveUI;
|
||||
using Terminal.Gui;
|
||||
using ReactiveMarbles.ObservableEvents;
|
||||
|
||||
namespace ReactiveExample {
|
||||
public class LoginView : Window, IViewFor<LoginViewModel> {
|
||||
readonly CompositeDisposable _disposable = new CompositeDisposable();
|
||||
|
||||
public LoginView (LoginViewModel viewModel) : base() {
|
||||
Title = "Reactive Extensions Example";
|
||||
ViewModel = viewModel;
|
||||
var usernameLengthLabel = UsernameLengthLabel (TitleLabel ());
|
||||
var usernameInput = UsernameInput (usernameLengthLabel);
|
||||
var passwordLengthLabel = PasswordLengthLabel (usernameInput);
|
||||
var passwordInput = PasswordInput (passwordLengthLabel);
|
||||
var validationLabel = ValidationLabel (passwordInput);
|
||||
var loginButton = LoginButton (validationLabel);
|
||||
var clearButton = ClearButton (loginButton);
|
||||
LoginProgressLabel (clearButton);
|
||||
}
|
||||
|
||||
public LoginViewModel ViewModel { get; set; }
|
||||
namespace ReactiveExample;
|
||||
|
||||
protected override void Dispose (bool disposing) {
|
||||
_disposable.Dispose ();
|
||||
base.Dispose (disposing);
|
||||
}
|
||||
public class LoginView : Window, IViewFor<LoginViewModel>
|
||||
{
|
||||
private readonly CompositeDisposable _disposable = new ();
|
||||
|
||||
Label TitleLabel () {
|
||||
var label = new Label("Login Form");
|
||||
Add (label);
|
||||
return label;
|
||||
}
|
||||
public LoginView (LoginViewModel viewModel)
|
||||
{
|
||||
Title = "Reactive Extensions Example";
|
||||
ViewModel = viewModel;
|
||||
Label usernameLengthLabel = UsernameLengthLabel (TitleLabel ());
|
||||
TextField usernameInput = UsernameInput (usernameLengthLabel);
|
||||
Label passwordLengthLabel = PasswordLengthLabel (usernameInput);
|
||||
TextField passwordInput = PasswordInput (passwordLengthLabel);
|
||||
Label validationLabel = ValidationLabel (passwordInput);
|
||||
Button loginButton = LoginButton (validationLabel);
|
||||
Button clearButton = ClearButton (loginButton);
|
||||
LoginProgressLabel (clearButton);
|
||||
}
|
||||
|
||||
TextField UsernameInput (View previous) {
|
||||
var usernameInput = new TextField (ViewModel.Username) {
|
||||
X = Pos.Left(previous),
|
||||
Y = Pos.Top(previous) + 1,
|
||||
Width = 40
|
||||
};
|
||||
ViewModel
|
||||
.WhenAnyValue (x => x.Username)
|
||||
.BindTo (usernameInput, x => x.Text)
|
||||
.DisposeWith (_disposable);
|
||||
usernameInput
|
||||
.Events ()
|
||||
.TextChanged
|
||||
.Select (old => usernameInput.Text)
|
||||
.DistinctUntilChanged ()
|
||||
.BindTo (ViewModel, x => x.Username)
|
||||
.DisposeWith (_disposable);
|
||||
Add (usernameInput);
|
||||
return usernameInput;
|
||||
}
|
||||
public LoginViewModel ViewModel { get; set; }
|
||||
|
||||
Label UsernameLengthLabel (View previous) {
|
||||
var usernameLengthLabel = new Label {
|
||||
X = Pos.Left(previous),
|
||||
Y = Pos.Top(previous) + 1,
|
||||
Width = 40
|
||||
};
|
||||
ViewModel
|
||||
.WhenAnyValue (x => x.UsernameLength)
|
||||
.Select (length => $"Username ({length} characters)")
|
||||
.BindTo (usernameLengthLabel, x => x.Text)
|
||||
.DisposeWith (_disposable);
|
||||
Add (usernameLengthLabel);
|
||||
return usernameLengthLabel;
|
||||
}
|
||||
object IViewFor.ViewModel
|
||||
{
|
||||
get => ViewModel;
|
||||
set => ViewModel = (LoginViewModel)value;
|
||||
}
|
||||
|
||||
TextField PasswordInput (View previous) {
|
||||
var passwordInput = new TextField (ViewModel.Password) {
|
||||
X = Pos.Left(previous),
|
||||
Y = Pos.Top(previous) + 1,
|
||||
Width = 40
|
||||
};
|
||||
ViewModel
|
||||
.WhenAnyValue (x => x.Password)
|
||||
.BindTo (passwordInput, x => x.Text)
|
||||
.DisposeWith (_disposable);
|
||||
passwordInput
|
||||
.Events ()
|
||||
.TextChanged
|
||||
.Select (old => passwordInput.Text)
|
||||
.DistinctUntilChanged ()
|
||||
.BindTo (ViewModel, x => x.Password)
|
||||
.DisposeWith (_disposable);
|
||||
Add (passwordInput);
|
||||
return passwordInput;
|
||||
}
|
||||
protected override void Dispose (bool disposing)
|
||||
{
|
||||
_disposable.Dispose ();
|
||||
base.Dispose (disposing);
|
||||
}
|
||||
|
||||
Label PasswordLengthLabel (View previous) {
|
||||
var passwordLengthLabel = new Label {
|
||||
X = Pos.Left(previous),
|
||||
Y = Pos.Top(previous) + 1,
|
||||
Width = 40
|
||||
};
|
||||
ViewModel
|
||||
.WhenAnyValue (x => x.PasswordLength)
|
||||
.Select (length => $"Password ({length} characters)")
|
||||
.BindTo (passwordLengthLabel, x => x.Text)
|
||||
.DisposeWith (_disposable);
|
||||
Add (passwordLengthLabel);
|
||||
return passwordLengthLabel;
|
||||
}
|
||||
private Button ClearButton (View previous)
|
||||
{
|
||||
var clearButton = new Button
|
||||
{
|
||||
X = Pos.Left (previous), Y = Pos.Top (previous) + 1, Width = 40, Text = "Clear"
|
||||
};
|
||||
|
||||
Label ValidationLabel (View previous) {
|
||||
var error = "Please, enter user name and password.";
|
||||
var success = "The input is valid!";
|
||||
var validationLabel = new Label(error) {
|
||||
X = Pos.Left(previous),
|
||||
Y = Pos.Top(previous) + 1,
|
||||
Width = 40
|
||||
};
|
||||
ViewModel
|
||||
.WhenAnyValue (x => x.IsValid)
|
||||
.Select (valid => valid ? success : error)
|
||||
.BindTo (validationLabel, x => x.Text)
|
||||
.DisposeWith (_disposable);
|
||||
ViewModel
|
||||
.WhenAnyValue (x => x.IsValid)
|
||||
.Select (valid => valid ? Colors.ColorSchemes ["Base"] : Colors.ColorSchemes ["Error"])
|
||||
.BindTo (validationLabel, x => x.ColorScheme)
|
||||
.DisposeWith (_disposable);
|
||||
Add (validationLabel);
|
||||
return validationLabel;
|
||||
}
|
||||
clearButton
|
||||
.Events ()
|
||||
.Clicked
|
||||
.InvokeCommand (ViewModel, x => x.Clear)
|
||||
.DisposeWith (_disposable);
|
||||
Add (clearButton);
|
||||
|
||||
Label LoginProgressLabel (View previous) {
|
||||
var progress = "Logging in...";
|
||||
var idle = "Press 'Login' to log in.";
|
||||
var loginProgressLabel = new Label(idle) {
|
||||
X = Pos.Left(previous),
|
||||
Y = Pos.Top(previous) + 1,
|
||||
Width = 40
|
||||
};
|
||||
ViewModel
|
||||
.WhenAnyObservable (x => x.Login.IsExecuting)
|
||||
.Select (executing => executing ? progress : idle)
|
||||
.ObserveOn (RxApp.MainThreadScheduler)
|
||||
.BindTo (loginProgressLabel, x => x.Text)
|
||||
.DisposeWith (_disposable);
|
||||
Add (loginProgressLabel);
|
||||
return loginProgressLabel;
|
||||
}
|
||||
return clearButton;
|
||||
}
|
||||
|
||||
Button LoginButton (View previous) {
|
||||
var loginButton = new Button ("Login") {
|
||||
X = Pos.Left(previous),
|
||||
Y = Pos.Top(previous) + 1,
|
||||
Width = 40
|
||||
};
|
||||
loginButton
|
||||
.Events ()
|
||||
.Clicked
|
||||
.InvokeCommand (ViewModel, x => x.Login)
|
||||
.DisposeWith (_disposable);
|
||||
Add (loginButton);
|
||||
return loginButton;
|
||||
}
|
||||
private Button LoginButton (View previous)
|
||||
{
|
||||
var loginButton = new Button
|
||||
{
|
||||
X = Pos.Left (previous), Y = Pos.Top (previous) + 1, Width = 40, Text = "Login"
|
||||
};
|
||||
|
||||
Button ClearButton (View previous) {
|
||||
var clearButton = new Button("Clear") {
|
||||
X = Pos.Left(previous),
|
||||
Y = Pos.Top(previous) + 1,
|
||||
Width = 40
|
||||
};
|
||||
clearButton
|
||||
.Events ()
|
||||
.Clicked
|
||||
.InvokeCommand (ViewModel, x => x.Clear)
|
||||
.DisposeWith (_disposable);
|
||||
Add (clearButton);
|
||||
return clearButton;
|
||||
}
|
||||
|
||||
object IViewFor.ViewModel {
|
||||
get => ViewModel;
|
||||
set => ViewModel = (LoginViewModel) value;
|
||||
}
|
||||
}
|
||||
}
|
||||
loginButton
|
||||
.Events ()
|
||||
.Clicked
|
||||
.InvokeCommand (ViewModel, x => x.Login)
|
||||
.DisposeWith (_disposable);
|
||||
Add (loginButton);
|
||||
|
||||
return loginButton;
|
||||
}
|
||||
|
||||
private Label LoginProgressLabel (View previous)
|
||||
{
|
||||
var progress = "Logging in...";
|
||||
var idle = "Press 'Login' to log in.";
|
||||
|
||||
var loginProgressLabel = new Label
|
||||
{
|
||||
X = Pos.Left (previous), Y = Pos.Top (previous) + 1, Width = 40, Text = idle
|
||||
};
|
||||
|
||||
ViewModel
|
||||
.WhenAnyObservable (x => x.Login.IsExecuting)
|
||||
.Select (executing => executing ? progress : idle)
|
||||
.ObserveOn (RxApp.MainThreadScheduler)
|
||||
.BindTo (loginProgressLabel, x => x.Text)
|
||||
.DisposeWith (_disposable);
|
||||
Add (loginProgressLabel);
|
||||
|
||||
return loginProgressLabel;
|
||||
}
|
||||
|
||||
private TextField PasswordInput (View previous)
|
||||
{
|
||||
var passwordInput = new TextField
|
||||
{
|
||||
X = Pos.Left (previous), Y = Pos.Top (previous) + 1, Width = 40, Text = ViewModel.Password
|
||||
};
|
||||
|
||||
ViewModel
|
||||
.WhenAnyValue (x => x.Password)
|
||||
.BindTo (passwordInput, x => x.Text)
|
||||
.DisposeWith (_disposable);
|
||||
|
||||
passwordInput
|
||||
.Events ()
|
||||
.TextChanged
|
||||
.Select (old => passwordInput.Text)
|
||||
.DistinctUntilChanged ()
|
||||
.BindTo (ViewModel, x => x.Password)
|
||||
.DisposeWith (_disposable);
|
||||
Add (passwordInput);
|
||||
|
||||
return passwordInput;
|
||||
}
|
||||
|
||||
private Label PasswordLengthLabel (View previous)
|
||||
{
|
||||
var passwordLengthLabel = new Label { X = Pos.Left (previous), Y = Pos.Top (previous) + 1, Width = 40 };
|
||||
|
||||
ViewModel
|
||||
.WhenAnyValue (x => x.PasswordLength)
|
||||
.Select (length => $"Password ({length} characters)")
|
||||
.BindTo (passwordLengthLabel, x => x.Text)
|
||||
.DisposeWith (_disposable);
|
||||
Add (passwordLengthLabel);
|
||||
|
||||
return passwordLengthLabel;
|
||||
}
|
||||
|
||||
private Label TitleLabel ()
|
||||
{
|
||||
var label = new Label { Text = "Login Form" };
|
||||
Add (label);
|
||||
|
||||
return label;
|
||||
}
|
||||
|
||||
private TextField UsernameInput (View previous)
|
||||
{
|
||||
var usernameInput = new TextField
|
||||
{
|
||||
X = Pos.Left (previous), Y = Pos.Top (previous) + 1, Width = 40, Text = ViewModel.Username
|
||||
};
|
||||
|
||||
ViewModel
|
||||
.WhenAnyValue (x => x.Username)
|
||||
.BindTo (usernameInput, x => x.Text)
|
||||
.DisposeWith (_disposable);
|
||||
|
||||
usernameInput
|
||||
.Events ()
|
||||
.TextChanged
|
||||
.Select (old => usernameInput.Text)
|
||||
.DistinctUntilChanged ()
|
||||
.BindTo (ViewModel, x => x.Username)
|
||||
.DisposeWith (_disposable);
|
||||
Add (usernameInput);
|
||||
|
||||
return usernameInput;
|
||||
}
|
||||
|
||||
private Label UsernameLengthLabel (View previous)
|
||||
{
|
||||
var usernameLengthLabel = new Label { X = Pos.Left (previous), Y = Pos.Top (previous) + 1, Width = 40 };
|
||||
|
||||
ViewModel
|
||||
.WhenAnyValue (x => x.UsernameLength)
|
||||
.Select (length => $"Username ({length} characters)")
|
||||
.BindTo (usernameLengthLabel, x => x.Text)
|
||||
.DisposeWith (_disposable);
|
||||
Add (usernameLengthLabel);
|
||||
|
||||
return usernameLengthLabel;
|
||||
}
|
||||
|
||||
private Label ValidationLabel (View previous)
|
||||
{
|
||||
var error = "Please, enter user name and password.";
|
||||
var success = "The input is valid!";
|
||||
|
||||
var validationLabel = new Label
|
||||
{
|
||||
X = Pos.Left (previous), Y = Pos.Top (previous) + 1, Width = 40, Text = error
|
||||
};
|
||||
|
||||
ViewModel
|
||||
.WhenAnyValue (x => x.IsValid)
|
||||
.Select (valid => valid ? success : error)
|
||||
.BindTo (validationLabel, x => x.Text)
|
||||
.DisposeWith (_disposable);
|
||||
|
||||
ViewModel
|
||||
.WhenAnyValue (x => x.IsValid)
|
||||
.Select (valid => valid ? Colors.ColorSchemes ["Base"] : Colors.ColorSchemes ["Error"])
|
||||
.BindTo (validationLabel, x => x.ColorScheme)
|
||||
.DisposeWith (_disposable);
|
||||
Add (validationLabel);
|
||||
|
||||
return validationLabel;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -6,72 +6,83 @@ using System.Threading.Tasks;
|
||||
using ReactiveUI;
|
||||
using ReactiveUI.Fody.Helpers;
|
||||
|
||||
namespace ReactiveExample {
|
||||
//
|
||||
// This view model can be easily shared across different UI frameworks.
|
||||
// For example, if you have a WPF or XF app with view models written
|
||||
// this way, you can easily port your app to Terminal.Gui by implementing
|
||||
// the views with Terminal.Gui classes and ReactiveUI bindings.
|
||||
//
|
||||
// We mark the view model with the [DataContract] attributes and this
|
||||
// allows you to save the view model class to the disk, and then to read
|
||||
// the view model from the disk, making your app state persistent.
|
||||
// See also: https://www.reactiveui.net../docs/handbook/data-persistence/
|
||||
//
|
||||
[DataContract]
|
||||
public class LoginViewModel : ReactiveObject {
|
||||
readonly ObservableAsPropertyHelper<int> _usernameLength;
|
||||
readonly ObservableAsPropertyHelper<int> _passwordLength;
|
||||
readonly ObservableAsPropertyHelper<bool> _isValid;
|
||||
|
||||
public LoginViewModel () {
|
||||
var canLogin = this.WhenAnyValue (
|
||||
x => x.Username,
|
||||
x => x.Password,
|
||||
(username, password) =>
|
||||
!string.IsNullOrEmpty (username) &&
|
||||
!string.IsNullOrEmpty (password));
|
||||
|
||||
_isValid = canLogin.ToProperty (this, x => x.IsValid);
|
||||
Login = ReactiveCommand.CreateFromTask<EventArgs> (
|
||||
e => Task.Delay (TimeSpan.FromSeconds (1)),
|
||||
canLogin);
|
||||
|
||||
_usernameLength = this
|
||||
.WhenAnyValue (x => x.Username)
|
||||
.Select (name => name.Length)
|
||||
.ToProperty (this, x => x.UsernameLength);
|
||||
_passwordLength = this
|
||||
.WhenAnyValue (x => x.Password)
|
||||
.Select (password => password.Length)
|
||||
.ToProperty (this, x => x.PasswordLength);
|
||||
|
||||
Clear = ReactiveCommand.Create<EventArgs> (e => { });
|
||||
Clear.Subscribe (unit => {
|
||||
Username = string.Empty;
|
||||
Password = string.Empty;
|
||||
});
|
||||
}
|
||||
|
||||
[Reactive, DataMember]
|
||||
public string Username { get; set; } = string.Empty;
|
||||
|
||||
[Reactive, DataMember]
|
||||
public string Password { get; set; } = string.Empty;
|
||||
|
||||
[IgnoreDataMember]
|
||||
public int UsernameLength => _usernameLength.Value;
|
||||
|
||||
[IgnoreDataMember]
|
||||
public int PasswordLength => _passwordLength.Value;
|
||||
namespace ReactiveExample;
|
||||
|
||||
[IgnoreDataMember]
|
||||
public ReactiveCommand<EventArgs, Unit> Login { get; }
|
||||
|
||||
[IgnoreDataMember]
|
||||
public ReactiveCommand<EventArgs, Unit> Clear { get; }
|
||||
|
||||
[IgnoreDataMember]
|
||||
public bool IsValid => _isValid.Value;
|
||||
}
|
||||
}
|
||||
//
|
||||
// This view model can be easily shared across different UI frameworks.
|
||||
// For example, if you have a WPF or XF app with view models written
|
||||
// this way, you can easily port your app to Terminal.Gui by implementing
|
||||
// the views with Terminal.Gui classes and ReactiveUI bindings.
|
||||
//
|
||||
// We mark the view model with the [DataContract] attributes and this
|
||||
// allows you to save the view model class to the disk, and then to read
|
||||
// the view model from the disk, making your app state persistent.
|
||||
// See also: https://www.reactiveui.net../docs/handbook/data-persistence/
|
||||
//
|
||||
[DataContract]
|
||||
public class LoginViewModel : ReactiveObject
|
||||
{
|
||||
private readonly ObservableAsPropertyHelper<bool> _isValid;
|
||||
private readonly ObservableAsPropertyHelper<int> _passwordLength;
|
||||
private readonly ObservableAsPropertyHelper<int> _usernameLength;
|
||||
|
||||
public LoginViewModel ()
|
||||
{
|
||||
IObservable<bool> canLogin = this.WhenAnyValue (
|
||||
x => x.Username,
|
||||
x => x.Password,
|
||||
(username, password) =>
|
||||
!string.IsNullOrEmpty (username) && !string.IsNullOrEmpty (password)
|
||||
);
|
||||
|
||||
_isValid = canLogin.ToProperty (this, x => x.IsValid);
|
||||
|
||||
Login = ReactiveCommand.CreateFromTask<EventArgs> (
|
||||
e => Task.Delay (TimeSpan.FromSeconds (1)),
|
||||
canLogin
|
||||
);
|
||||
|
||||
_usernameLength = this
|
||||
.WhenAnyValue (x => x.Username)
|
||||
.Select (name => name.Length)
|
||||
.ToProperty (this, x => x.UsernameLength);
|
||||
|
||||
_passwordLength = this
|
||||
.WhenAnyValue (x => x.Password)
|
||||
.Select (password => password.Length)
|
||||
.ToProperty (this, x => x.PasswordLength);
|
||||
|
||||
Clear = ReactiveCommand.Create<EventArgs> (e => { });
|
||||
|
||||
Clear.Subscribe (
|
||||
unit =>
|
||||
{
|
||||
Username = string.Empty;
|
||||
Password = string.Empty;
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
[IgnoreDataMember]
|
||||
public ReactiveCommand<EventArgs, Unit> Clear { get; }
|
||||
|
||||
[IgnoreDataMember]
|
||||
public bool IsValid => _isValid.Value;
|
||||
|
||||
[IgnoreDataMember]
|
||||
public ReactiveCommand<EventArgs, Unit> Login { get; }
|
||||
|
||||
[Reactive]
|
||||
[DataMember]
|
||||
public string Password { get; set; } = string.Empty;
|
||||
|
||||
[IgnoreDataMember]
|
||||
public int PasswordLength => _passwordLength.Value;
|
||||
|
||||
[Reactive]
|
||||
[DataMember]
|
||||
public string Username { get; set; } = string.Empty;
|
||||
|
||||
[IgnoreDataMember]
|
||||
public int UsernameLength => _usernameLength.Value;
|
||||
}
|
||||
|
||||
@@ -2,14 +2,16 @@
|
||||
using ReactiveUI;
|
||||
using Terminal.Gui;
|
||||
|
||||
namespace ReactiveExample {
|
||||
public static class Program {
|
||||
static void Main (string [] args) {
|
||||
Application.Init ();
|
||||
RxApp.MainThreadScheduler = TerminalScheduler.Default;
|
||||
RxApp.TaskpoolScheduler = TaskPoolScheduler.Default;
|
||||
Application.Run (new LoginView (new LoginViewModel ()));
|
||||
Application.Shutdown ();
|
||||
}
|
||||
}
|
||||
}
|
||||
namespace ReactiveExample;
|
||||
|
||||
public static class Program
|
||||
{
|
||||
private static void Main (string [] args)
|
||||
{
|
||||
Application.Init ();
|
||||
RxApp.MainThreadScheduler = TerminalScheduler.Default;
|
||||
RxApp.TaskpoolScheduler = TaskPoolScheduler.Default;
|
||||
Application.Run (new LoginView (new LoginViewModel ()));
|
||||
Application.Shutdown ();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,39 +3,58 @@ using System.Reactive.Concurrency;
|
||||
using System.Reactive.Disposables;
|
||||
using Terminal.Gui;
|
||||
|
||||
namespace ReactiveExample {
|
||||
public class TerminalScheduler : LocalScheduler {
|
||||
public static readonly TerminalScheduler Default = new TerminalScheduler();
|
||||
TerminalScheduler () { }
|
||||
namespace ReactiveExample;
|
||||
|
||||
public override IDisposable Schedule<TState> (
|
||||
TState state, TimeSpan dueTime,
|
||||
Func<IScheduler, TState, IDisposable> action) {
|
||||
|
||||
IDisposable PostOnMainLoop() {
|
||||
var composite = new CompositeDisposable(2);
|
||||
var cancellation = new CancellationDisposable();
|
||||
Application.Invoke (() => {
|
||||
if (!cancellation.Token.IsCancellationRequested)
|
||||
composite.Add(action(this, state));
|
||||
});
|
||||
composite.Add(cancellation);
|
||||
return composite;
|
||||
}
|
||||
public class TerminalScheduler : LocalScheduler
|
||||
{
|
||||
public static readonly TerminalScheduler Default = new ();
|
||||
private TerminalScheduler () { }
|
||||
|
||||
IDisposable PostOnMainLoopAsTimeout () {
|
||||
var composite = new CompositeDisposable (2);
|
||||
var timeout = Application.AddTimeout (dueTime, () => {
|
||||
composite.Add(action (this, state));
|
||||
return false;
|
||||
});
|
||||
composite.Add (Disposable.Create (() => Application.RemoveTimeout (timeout)));
|
||||
return composite;
|
||||
}
|
||||
public override IDisposable Schedule<TState> (
|
||||
TState state,
|
||||
TimeSpan dueTime,
|
||||
Func<IScheduler, TState, IDisposable> action
|
||||
)
|
||||
{
|
||||
IDisposable PostOnMainLoop ()
|
||||
{
|
||||
var composite = new CompositeDisposable (2);
|
||||
var cancellation = new CancellationDisposable ();
|
||||
|
||||
return dueTime == TimeSpan.Zero
|
||||
? PostOnMainLoop ()
|
||||
: PostOnMainLoopAsTimeout ();
|
||||
}
|
||||
}
|
||||
}
|
||||
Application.Invoke (
|
||||
() =>
|
||||
{
|
||||
if (!cancellation.Token.IsCancellationRequested)
|
||||
{
|
||||
composite.Add (action (this, state));
|
||||
}
|
||||
}
|
||||
);
|
||||
composite.Add (cancellation);
|
||||
|
||||
return composite;
|
||||
}
|
||||
|
||||
IDisposable PostOnMainLoopAsTimeout ()
|
||||
{
|
||||
var composite = new CompositeDisposable (2);
|
||||
|
||||
object timeout = Application.AddTimeout (
|
||||
dueTime,
|
||||
() =>
|
||||
{
|
||||
composite.Add (action (this, state));
|
||||
|
||||
return false;
|
||||
}
|
||||
);
|
||||
composite.Add (Disposable.Create (() => Application.RemoveTimeout (timeout)));
|
||||
|
||||
return composite;
|
||||
}
|
||||
|
||||
return dueTime == TimeSpan.Zero
|
||||
? PostOnMainLoop ()
|
||||
: PostOnMainLoopAsTimeout ();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user