Files
Terminal.Gui/Examples/UICatalog/Scenarios/ViewportSettings.cs
Tig 3e2eebfd2c Fixes #4057 - MASSIVE! Fully implements ColorScheme->Scheme + VisualRole + Colors.->SchemeManager. (#4062)
* touching publish.yml

* ColorScheme->Scheme

* ColorScheme->Scheme 2

* Prototype of GetAttributeForRole

* Badly broke CM

* Further Badly broke CM

* Refactored CM big-time. View still broken

* All unit test pass again. Tons added. CM is still WIP, but Schemes is not mostly refactored and working.

* Actually:
All unit test pass again.
Tons added.
CM is still WIP, but Schemes is not mostly refactored and working.

* Bug fixes.
DeepMemberWiseClone cleanup

* Further cleanup of Scope<T>, ConfigProperty, etc.

* Made ConfigManager thread safe.

* WIP: Broken

* WIP: new deep clone impl

* WIP: new deep clone impl is done. Now fixing CM

* WIP:
- config.md
- Working on AOT clean up
- Core CM is broken; but known.

* WIP

* Merged.
Removed CM from Application.Init

* WIP

* More WIP; Less broke

* All CM unit tests pass... Not sure if it actually works though

* All unit tests pass... Themes are broken though in UI Cat

* CM Ready for review?

* Fixed failures due to TextStyles PR

* Working on Scheme/Attribute

* Working on Scheme/Attribute 2

* Working on Scheme/Attribute 3

* Working on Scheme/Attribute 4

* Working on Scheme/Attribute 5

* Working on Scheme/Attribute 6

* Added test to show how awful memory usage is

* Improved schema. Updated config.json

* Nade Scope<T> concurrentdictionary and added test to prove

* Made Themes ConcrurrentDictionary. Added bunches of tests

* Code cleanup

* Code cleanup 2

* Code cleanup 3

* Tweaking Scheme

* ClearJsonErrors

* ClearJsonErrors2

* Updated Attribute API

* It all (mostly) works!

* Skip odd unit test

* Messed with Themes

* Theme tweaks

* Code reorg. New .md stuff

* Fixed Enabled. Added mock driver

* Fixed a bunch of View.Enabled related issues

* Scheme -> Get/SetScheme()

* Cleanup

* Cleanup2

* Broke something

* Fixed everything

* Made CM.Enable better

* Text Style Scenario

* Added comments

* Fixed UI Catalog Theme Changing

* Fixed more dynamic CM update stuff

* Warning cleanup

* New Default Theme

* fixed unit test

* Refactoring Scheme and Attribute to fix inheritance

* more unit tests

* ConfigProperty is not updating schemes correctly

* All unit tests pass.
Code cleanup

* All unit tests pass.
Code cleanup2

* Fixed unit tests

* Upgraded TextField and TextView

* Fixed TextView !Enabled bug

* More updates to TextView. More unit tests for SchemeManager

* Upgraded CharMap

* API docs

* Fixe HexView API

* upgrade HexView

* Fixed shortcut KeyView

* Fixed more bugs. Added new themes

* updated themes

* upgraded Border

* Fixed themes memory usage...mostly

* Fixed themes memory usage...mostly2

* Fixed themes memory usage...2

* Fixed themes memory usage...3

* Added new colors

* Fixed GetHardCodedConfig bug

* Added Themes Scenario - WIP

* Added Themes Scenario

* Tweaked Themes Scenario

* Code cleanup

* Fixed json schmea

* updated deepdives

* updated deepdives

* Tweaked Themes Scenario

* Made Schemes a concurrent dict

* Test cleanup

* Thread safe ConfigProperty tests

* trying to make things more thread safe

* more trying to make things more thread safe

* Fixing bugs in shadowview

* Fixing bugs in shadowview 2

* Refactored GetViewsUnderMouse to GetViewsUnderLocation etc...

* Fixed dupe unit tests?

* Added better description of layout and coordiantes to deep dive

* Added better description of layout and coordiantes to deep dive

* Modified tests that call v2.AddTimeout; they were returning true which means restart the timer!
This was causing mac/linux unit test failures.
I think

* Fixed auto scheme.
Broke TextView/TextField selection

* Realized Attribute.IsExplicitlySet is stupid; just use nullable

* Fixed Attribute. Simplified. MOre theme testing

* Updated themes again

* GetViewsUnderMouse to GetViewsUnderLocation broke TransparentMouse.

* Fixing mouseunder bugs

* rewriting...

* All working again.
Shadows are now slick as snot.
GetViewsUnderLocation is rewritten to actually work and be readable.
Tons more low-level unit tests.
Margin is now actually ViewportSettings.Transparent.

* Code cleanup

* Code cleanup

* Code cleanup of color apis

* Fixed Hover/Highlight

* Update Examples/UICatalog/Scenarios/AllViewsTester.cs

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

* Update Examples/UICatalog/Scenarios/CharacterMap/CharacterMap.cs

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

* Update Examples/UICatalog/Scenarios/Clipping.cs

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

* Fixed race condition?

* reverted

* Simplified Attribute API by removing events from SetAttributeForRole

* Removed recursion from GetViewsAtLocation

* Removed unneeded code

* Code clean up.
Fixed Scheme bug.

* reverted temporary disable

* Adjusted scheme algo

* Upgraded TextValidateField

* Fixed TextValidate bugs

* Tweaks

* Frameview rounded border by default

* API doc cleanup

* Readme fix

* Addressed tznind feeback

* Fixed more unit test issues by protecting Application statics from being set if Application.Initialized is not true

* Fixed more unit test issues by protecting Application statics from being set if Application.Initialized is not true 2

* cleanup

---------

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
2025-05-29 14:08:48 -06:00

262 lines
8.9 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
using Terminal.Gui;
namespace UICatalog.Scenarios;
[ScenarioMetadata ("ViewportSettings", "Demonstrates manipulating Viewport, ViewportSettings, and ContentSize to scroll content.")]
[ScenarioCategory ("Layout")]
[ScenarioCategory ("Drawing")]
[ScenarioCategory ("Scrolling")]
[ScenarioCategory ("Adornments")]
public class ViewportSettings : Scenario
{
public class ViewportSettingsDemoView : FrameView
{
public ViewportSettingsDemoView ()
{
Id = "ViewportSettingsDemoView";
Width = Dim.Fill ();
Height = Dim.Fill ();
SchemeName = "base";
base.Text =
"Text (ViewportSettingsDemoView.Text). This is long text.\nThe second line.\n3\n4\n5th line\nLine 6. This is a longer line that should wrap automatically.";
CanFocus = true;
BorderStyle = LineStyle.Rounded;
Arrangement = ViewArrangement.Resizable;
SetContentSize (new (60, 40));
ViewportSettings |= Terminal.Gui.ViewportSettings.ClearContentOnly;
ViewportSettings |= Terminal.Gui.ViewportSettings.ClipContentOnly;
VerticalScrollBar.Visible = true;
// Things this view knows how to do
AddCommand (Command.ScrollDown, () => ScrollVertical (1));
AddCommand (Command.ScrollUp, () => ScrollVertical (-1));
AddCommand (Command.ScrollRight, () => ScrollHorizontal (1));
AddCommand (Command.ScrollLeft, () => ScrollHorizontal (-1));
// Default keybindings for all ListViews
KeyBindings.Add (Key.CursorUp, Command.ScrollUp);
KeyBindings.Add (Key.CursorDown, Command.ScrollDown);
KeyBindings.Add (Key.CursorLeft, Command.ScrollLeft);
KeyBindings.Add (Key.CursorRight, Command.ScrollRight);
// Add a status label to the border that shows Viewport and ContentSize values. Bit of a hack.
// TODO: Move to Padding with controls
Border?.Add (new Label { X = 20 });
ViewportChanged += VirtualDemoView_LayoutComplete;
MouseEvent += VirtualDemoView_MouseEvent;
}
private void VirtualDemoView_MouseEvent (object sender, MouseEventArgs e)
{
if (e.Flags == MouseFlags.WheeledDown)
{
ScrollVertical (1);
return;
}
if (e.Flags == MouseFlags.WheeledUp)
{
ScrollVertical (-1);
return;
}
if (e.Flags == MouseFlags.WheeledRight)
{
ScrollHorizontal (1);
return;
}
if (e.Flags == MouseFlags.WheeledLeft)
{
ScrollHorizontal (-1);
}
}
private void VirtualDemoView_LayoutComplete (object sender, DrawEventArgs drawEventArgs)
{
Label frameLabel = Padding?.SubViews.OfType<Label> ().FirstOrDefault ();
if (frameLabel is { })
{
frameLabel.Text = $"Viewport: {Viewport}\nFrame: {Frame}";
}
}
}
public override void Main ()
{
Application.Init ();
Window app = new ()
{
Title = GetQuitKeyAndName (),
// Use a different colorscheme so ViewSettings.ClearContentOnly is obvious
SchemeName = "Toplevel",
BorderStyle = LineStyle.None
};
var adornmentsEditor = new AdornmentsEditor
{
X = Pos.AnchorEnd (),
AutoSelectViewToEdit = true,
ShowViewIdentifier = true
};
app.Add (adornmentsEditor);
ViewportSettingsEditor viewportSettingsEditor = new ViewportSettingsEditor ()
{
Y = Pos.AnchorEnd (),
//X = Pos.Right (adornmentsEditor),
};
app.Add (viewportSettingsEditor);
var view = new ViewportSettingsDemoView
{
Title = "ViewportSettings Demo View",
Width = Dim.Fill (Dim.Func (() => app.IsInitialized ? adornmentsEditor.Frame.Width + 1 : 1)),
Height = Dim.Fill (Dim.Func (() => app.IsInitialized ? viewportSettingsEditor.Frame.Height : 1))
};
app.Add (view);
// Add demo views to show that things work correctly
var textField = new TextField { X = 20, Y = 7, Width = 15, Text = "Test Te_xtField" };
var colorPicker = new ColorPicker16 { Title = "_BG", BoxHeight = 1, BoxWidth = 1, X = Pos.AnchorEnd (), Y = 10 };
colorPicker.BorderStyle = LineStyle.RoundedDotted;
colorPicker.ColorChanged += (s, e) =>
{
colorPicker.SuperView!.SetScheme (
new (colorPicker.SuperView.GetScheme ())
{
Normal = new (
colorPicker.SuperView.GetAttributeForRole (VisualRole.Normal).Foreground,
e.CurrentValue
)
});
};
var textView = new TextView
{
X = Pos.Center (),
Y = 10,
Title = "TextVie_w",
Text = "I have a 3 row top border.\nMy border inherits from the SuperView.\nI have 3 lines of text with room for 2.",
AllowsTab = false,
Width = 30,
Height = 6 // TODO: Use Dim.Auto
};
textView.Border!.Thickness = new (1, 3, 1, 1);
var charMap = new CharMap
{
X = Pos.Center (),
Y = Pos.Bottom (textView) + 1,
Width = Dim.Auto (DimAutoStyle.Content, maximumContentDim: Dim.Func (() => view.GetContentSize ().Width)),
Height = Dim.Auto (DimAutoStyle.Content, maximumContentDim: Dim.Percent (20)),
};
charMap.Accepting += (s, e) =>
MessageBox.Query (20, 7, "Hi", $"Am I a {view.GetType ().Name}?", "Yes", "No");
var buttonAnchored = new Button
{
X = Pos.AnchorEnd () - 10, Y = Pos.AnchorEnd () - 4, Text = "Bottom Rig_ht"
};
buttonAnchored.Accepting += (sender, args) => MessageBox.Query ("Hi", $"You pressed {((Button)sender)?.Text}", "_Ok");
view.Margin!.Data = "Margin";
view.Margin.Thickness = new (0);
view.Border!.Data = "Border";
view.Border.Thickness = new (3);
view.Padding.Data = "Padding";
view.Add (buttonAnchored, textField, colorPicker, charMap, textView);
var longLabel = new Label
{
Id = "label2",
X = 0,
Y = 30,
Text =
"This label is long. It should clip to the ContentArea if ClipContentOnly is set. This is a virtual scrolling demo. Use the arrow keys and/or mouse wheel to scroll the content."
};
longLabel.TextFormatter.WordWrap = true;
view.Add (longLabel);
List<object> options = new () { "Option 1", "Option 2", "Option 3" };
Slider slider = new (options)
{
X = 0,
Y = Pos.Bottom (textField) + 1,
Orientation = Orientation.Vertical,
Type = SliderType.Multiple,
AllowEmpty = false,
BorderStyle = LineStyle.Double,
Title = "_Slider"
};
view.Add (slider);
adornmentsEditor.Initialized += (s, e) =>
{
adornmentsEditor.ViewToEdit = view;
};
adornmentsEditor.AutoSelectViewToEdit = true;
adornmentsEditor.AutoSelectSuperView = view;
adornmentsEditor.AutoSelectAdornments = false;
view.Initialized += (s, e) =>
{
viewportSettingsEditor.ViewToEdit = view;
};
view.SetFocus ();
Application.Run (app);
app.Dispose ();
Application.Shutdown ();
}
public override List<Key> GetDemoKeyStrokes ()
{
var keys = new List<Key> ();
for (int i = 0; i < 50; i++)
{
keys.Add (Key.CursorRight);
}
for (int i = 0; i < 25; i++)
{
keys.Add (Key.CursorLeft);
}
for (int i = 0; i < 50; i++)
{
keys.Add (Key.CursorDown);
}
for (int i = 0; i < 25; i++)
{
keys.Add (Key.CursorUp);
}
return keys;
}
}