Files
Terminal.Gui/Examples/UICatalog/Scenarios/DimAutoDemo.cs
Copilot fb4043afbc Fixes #3829. Rename Slider to LinearRange and implement CWP (#4509)
* Initial plan

* Rename Slider to LinearRange - files and code updated

Co-authored-by: tig <585482+tig@users.noreply.github.com>

* Fix variable name mismatches in scenario files

Co-authored-by: tig <585482+tig@users.noreply.github.com>

* Fix typos in comments identified by code review

Co-authored-by: tig <585482+tig@users.noreply.github.com>

* Update HelpText for consistency with LinearRange rename

Co-authored-by: tig <585482+tig@users.noreply.github.com>

* Refactor LinearRange properties to use CWP with CWPPropertyHelper

- Added CWP events (Changing/Changed) for Type, LegendsOrientation, MinimumInnerSpacing, ShowLegends, ShowEndSpacing, and UseMinimumSize properties
- Used CWPPropertyHelper for consistent property change workflow
- Added virtual OnChanging/OnChanged methods for each property
- All existing tests pass

Co-authored-by: tig <585482+tig@users.noreply.github.com>

* Fix CWP property implementations - remove duplicate field updates in doWork

- CWPPropertyHelper updates the ref parameter, so doWork should only contain side effects
- Fixed Type, MinimumInnerSpacing, LegendsOrientation, ShowLegends, ShowEndSpacing, and UseMinimumSize
- Added comprehensive CWP tests for property changes (9 new tests, all passing)
- All 45 pre-existing LinearRange tests still pass (10 were already failing before changes)

Co-authored-by: tig <585482+tig@users.noreply.github.com>

* Add fluent integration tests for LinearRange

- Created LinearRangeFluentTests with 5 test scenarios
- Tests cover rendering, navigation, type changes with CWP events, range selection, and vertical orientation
- All tests build successfully

Co-authored-by: tig <585482+tig@users.noreply.github.com>

* Fix unit tests and address review comments

- Fixed review comments: Updated Title properties from "_Slider" to "_LinearRange" in DimAutoDemo and ViewportSettings
- Fixed review comment: Renamed filterSlider to filterLinearRange in Mouse.cs
- Fixed CWP implementation: Use local variable instead of backing field as ref parameter to CWPPropertyHelper
- This ensures doWork can update the backing field BEFORE SetContentSize() reads it
- All 55 LinearRange tests now passing (was 45/55, now 55/55)

Co-authored-by: tig <585482+tig@users.noreply.github.com>

* Update DimAutoDemo.cs

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>

* Update ViewportSettings.cs

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>

* code cleanup

* Refactor LinearRanges scenario for clarity and modularity

Refactored the LinearRanges scenario to separate UI setup, configuration, and event logic. The Main() method now handles all UI layout, configuration controls, and event wiring, while MakeSliders() is responsible only for adding LinearRange controls to the main window. Updated event handlers to use discard parameters for unused arguments and modernized code with collection initializers and LINQ. This results in a clearer separation of concerns and improved maintainability.

* un did change

* cleanup

* cleanup

* cleanup

* fixed unit test
code cleanup

---------

Co-authored-by: Tig <tig@users.noreply.github.com>
Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com>
Co-authored-by: tig <585482+tig@users.noreply.github.com>
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
2025-12-21 10:40:09 -07:00

235 lines
7.0 KiB
C#

using System;
using System.Collections.Generic;
namespace UICatalog.Scenarios;
[ScenarioMetadata ("DimAuto", "Demonstrates Dim.Auto")]
[ScenarioCategory ("Layout")]
public class DimAutoDemo : Scenario
{
public override void Main ()
{
Application.Init ();
// Setup - Create a top-level application window and configure it.
Window appWindow = new ()
{
Title = GetQuitKeyAndName (),
};
// For diagnostics
appWindow.Padding.Thickness = new Thickness (1);
FrameView dimAutoFrameView = CreateDimAutoContentFrameView ();
//FrameView sliderFrameView = CreateSliderFrameView ();
//sliderFrameView.X = Pos.Right(dimAutoFrameView) + 1;
//sliderFrameView.Width = Dim.Fill ();
//sliderFrameView.Height = Dim.Fill ();
////var dlgButton = new Button
////{
//// Text = "Open Test _Dialog",
//// X = Pos.Right (dimAutoFrameView),
//// Y = Pos.Top (dimAutoFrameView)
////};
////dlgButton.Accept += DlgButton_Clicked;
appWindow.Add (dimAutoFrameView/*, sliderFrameView dlgButton*/);
// Run - Start the application.
Application.Run (appWindow);
appWindow.Dispose ();
// Shutdown - Calling Application.Shutdown is required.
Application.Shutdown ();
}
private static FrameView CreateDimAutoContentFrameView ()
{
var dimAutoFrameView = new FrameView
{
Title = "Type to make View grow",
X = 0,
Y = 0,
Width = Dim.Auto (DimAutoStyle.Content, minimumContentDim: Dim.Percent (25)),
Height = Dim.Auto (DimAutoStyle.Content, minimumContentDim: 10)
};
dimAutoFrameView.Margin!.Thickness = new Thickness (1);
dimAutoFrameView.ValidatePosDim = true;
var textEdit = new TextView
{
Text = "",
X = 0, Y = 0, Width = 20, Height = 4
};
dimAutoFrameView.Add (textEdit);
var vlabel = new Label
{
Text = textEdit.Text,
X = Pos.Left (textEdit),
Y = Pos.Bottom (textEdit) + 1,
Width = Dim.Auto (DimAutoStyle.Text, 1),
Height = Dim.Auto (DimAutoStyle.Text, 8),
SchemeName = "Error",
TextDirection = TextDirection.TopBottom_LeftRight
};
vlabel.Id = "vlabel";
dimAutoFrameView.Add (vlabel);
var hlabel = new Label
{
Text = textEdit.Text,
X = Pos.Right (vlabel) + 1,
Y = Pos.Bottom (textEdit),
Width = Dim.Auto (DimAutoStyle.Text, 20),
Height = Dim.Auto (DimAutoStyle.Text, 1),
SchemeName = "Error"
};
hlabel.Id = "hlabel";
dimAutoFrameView.Add (hlabel);
var heightAuto = new View
{
X = Pos.Right (vlabel) + 1,
Y = Pos.Bottom (hlabel) + 1,
Width = 20,
Height = Dim.Auto (),
SchemeName = "Error",
Title = "W: 20, H: Auto",
BorderStyle = LineStyle.Rounded
};
heightAuto.Id = "heightAuto";
dimAutoFrameView.Add (heightAuto);
var widthAuto = new View
{
X = Pos.Right (heightAuto) + 1,
Y = Pos.Bottom (hlabel) + 1,
Width = Dim.Auto (),
Height = 5,
SchemeName = "Error",
Title = "W: Auto, H: 5",
BorderStyle = LineStyle.Rounded
};
widthAuto.Id = "widthAuto";
dimAutoFrameView.Add (widthAuto);
var bothAuto = new View
{
X = Pos.Right (widthAuto) + 1,
Y = Pos.Bottom (hlabel) + 1,
Width = Dim.Auto (),
Height = Dim.Auto (),
SchemeName = "Error",
Title = "W: Auto, H: Auto",
BorderStyle = LineStyle.Rounded
};
bothAuto.Id = "bothAuto";
dimAutoFrameView.Add (bothAuto);
textEdit.ContentsChanged += (s, e) =>
{
hlabel.Text = textEdit.Text;
vlabel.Text = textEdit.Text;
heightAuto.Text = textEdit.Text;
widthAuto.Text = textEdit.Text;
bothAuto.Text = textEdit.Text;
};
//var movingButton = new Button
//{
// Text = "_Click\nTo Move\nDown",
// X = Pos.Right (vlabel),
// Y = Pos.Bottom (vlabel)
//};
//movingButton.Accept += (s, e) => { movingButton.Y = movingButton.Frame.Y + 1; };
//dimAutoFrameView.Add (movingButton);
var resetButton = new Button
{
Text = "_Reset Button (AnchorEnd)",
X = Pos.AnchorEnd (),
Y = Pos.AnchorEnd ()
};
resetButton.Accepting += (s, e) =>
{
//movingButton.Y = Pos.Bottom (hlabel);
//movingButton.X = 0;
};
dimAutoFrameView.Add (resetButton);
var optionSelector = new OptionSelector ()
{
Labels = ["One", "Two", "Three"],
X = 0,
Y = Pos.AnchorEnd (),
Title = "Options",
BorderStyle = LineStyle.Dotted
};
dimAutoFrameView.Add (optionSelector);
return dimAutoFrameView;
}
private static FrameView CreateSliderFrameView ()
{
var sliderFrameView = new FrameView
{
Title = "LinearRange - Example of a DimAuto View",
};
List<object> options = new () { "One", "Two", "Three", "Four" };
LinearRange linearRange = new (options)
{
X = 0,
Y = 0,
Type = LinearRangeType.Multiple,
AllowEmpty = false,
BorderStyle = LineStyle.Double,
Title = "_LinearRange"
};
sliderFrameView.Add (linearRange);
return sliderFrameView;
}
private void DlgButton_Clicked (object sender, EventArgs e)
{
var dlg = new Dialog
{
Title = "Test Dialog",
Width = Dim.Auto (minimumContentDim: Dim.Percent (10))
//Height = Dim.Auto (min: Dim.Percent (50))
};
var text = new TextField
{
ValidatePosDim = true,
Text = "TextField: X=1; Y=Pos.Bottom (label)+1, Width=Dim.Fill (0); Height=1",
TextFormatter = new () { WordWrap = true },
X = 0,
Y = 0, //Pos.Bottom (label) + 1,
Width = Dim.Fill (10),
Height = 1
};
//var btn = new Button
//{
// Text = "AnchorEnd", Y = Pos.AnchorEnd (1)
//};
//// TODO: We should really fix AnchorEnd to do this automatically.
//btn.X = Pos.AnchorEnd () - (Pos.Right (btn) - Pos.Left (btn));
//dlg.Add (label);
dlg.Add (text);
//dlg.Add (btn);
Application.Run (dlg);
dlg.Dispose ();
}
}