Files
Terminal.Gui/Examples/UICatalog/Scenarios/Keys.cs
Tig f548059a27 Fixes #4258 - Glyphs drawn at mid-point of wide glyphs don't get drawn with clipping (#4462)
* Enhanced `View.Drawing.cs` with improved comments, a new
`DoDrawComplete` method for clip region updates, and
clarified terminology. Added detailed remarks for the
`OnDrawComplete` method and `DrawComplete` event.

Refactored `ViewDrawingClippingTests` to simplify driver
setup, use target-typed `new`, and add a new test for wide
glyph clipping with bordered subviews. Improved handling of
edge cases like empty viewports and nested clips.

Added `WideGlyphs.DrawFlow.md` and
`ViewDrawingClippingTests.DrawFlow.md` to document the draw
flow, clipping behavior, and coordinate systems for both the
scenario and the test.

Commented out redundant `Driver.Clip` initialization in
`ApplicationImpl`. Added a `BUGBUG` comment in `Border` to
highlight missing redraw logic for `LineStyle` changes.

* Uncomment Driver.Clip initialization in Screen redraw

* Fixed it!

* Fixes #4258 - Correct wide glyph and border rendering

Refactored `OutputBufferImpl.AddStr` to improve handling of wide glyphs:
- Wide glyphs now modify only the first column they occupy, leaving the second column untouched.
- Removed redundant code that set replacement characters and marked cells as not dirty.
- Synchronized cursor updates (`Col` and `Row`) with the buffer lock to prevent race conditions.
- Modularized logic with helper methods for better readability and maintainability.

Updated `WideGlyphs.cs`:
- Removed dashed `BorderStyle` and added border thickness and subview for `arrangeableViewAtEven`.
- Removed unused `superView` initialization.

Enhanced tests:
- Added unit tests to verify correct rendering of borders and content at odd columns overlapping wide glyphs.
- Updated existing tests to reflect the new behavior of wide glyph handling.
- Introduced `DriverAssert.AssertDriverOutputIs` to validate raw ANSI output.

Improved documentation:
- Expanded problem description and root cause analysis in `WideGlyphBorderBugFix.md`.
- Detailed the fix and its impact, ensuring proper layering of content at any column position.

General cleanup:
- Removed unused imports and redundant code.
- Improved code readability and maintainability.

* Code cleanup

* Update Tests/UnitTestsParallelizable/ViewBase/Draw/ViewDrawingClippingTests.cs

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

* Update Terminal.Gui/Drivers/OutputBufferImpl.cs

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

* Update Tests/UnitTestsParallelizable/ViewBase/Draw/ViewDrawingClippingTests.cs

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

* Update Tests/UnitTestsParallelizable/ViewBase/Draw/ViewDrawingClippingTests.cs

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

* Fixed test slowness problem

* Simplified

* Rmoved temp .md files

* Refactor I/O handling and improve testability

Refactored `InputProcessor` and `Output` access by replacing direct property usage with `GetInputProcessor()` and `GetOutput()` methods to enhance encapsulation. Introduced `GetLastOutput()` and `GetLastBuffer()` methods for better debugging and testability.

Centralized `StringBuilder` usage in `OutputBase` implementations to ensure consistency. Improved exception handling with clearer messages. Updated tests to align with the refactored structure and added a new test for wide glyph handling.

Enhanced ANSI sequence handling and simplified cursor visibility logic to prevent flickering. Standardized method naming for consistency. Cleaned up redundant code and improved documentation for better developer clarity.

* Refactored `NetOutput`, `FakeOutput`, `UnixOutput`, and `WindowsOutput` classes to support access to `Output` and added a `IDriver.GetOutput` to acess the `IOutput`. `IOutput` now has a `GetLastOutput` method.

Simplified `DriverAssert` logic and enhanced `DriverTests` with a new test for wide glyph clipping across drivers.

Performed general cleanup, including removal of unused code, improved formatting, and adoption of modern C# practices. Added `using System.Diagnostics` in `OutputBufferImpl` for debugging.

---------

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
2025-12-08 12:28:32 -07:00

179 lines
5.0 KiB
C#

using System.Collections.ObjectModel;
namespace UICatalog.Scenarios;
[ScenarioMetadata ("Keys", "Shows keyboard input handling.")]
[ScenarioCategory ("Mouse and Keyboard")]
public class Keys : Scenario
{
public override void Main ()
{
Application.Init ();
ObservableCollection<string> keyDownList = [];
ObservableCollection<string> keyDownNotHandledList = new ();
ObservableCollection<string> swallowedList = new ();
var win = new Window { Title = GetQuitKeyAndName () };
var label = new Label
{
X = 0,
Y = 0,
Text = "_Type text here:"
};
win.Add (label);
var edit = new TextField
{
X = Pos.Right (label) + 1,
Y = Pos.Top (label),
Width = Dim.Fill (2),
Height = 1,
};
win.Add (edit);
label = new Label
{
X = 0,
Y = Pos.Bottom (label),
Text = "Last _Application.KeyDown:"
};
win.Add (label);
var labelAppKeypress = new Label
{
X = Pos.Right (label) + 1,
Y = Pos.Top (label)
};
win.Add (labelAppKeypress);
Application.KeyDown += (s, e) => labelAppKeypress.Text = e.ToString ();
label = new ()
{
X = 0,
Y = Pos.Bottom (label),
Text = "_Last TextField.KeyDown:"
};
win.Add (label);
var lastTextFieldKeyDownLabel = new Label
{
X = Pos.Right (label) + 1,
Y = Pos.Top (label),
Height = 1,
};
win.Add (lastTextFieldKeyDownLabel);
edit.KeyDown += (s, e) => lastTextFieldKeyDownLabel.Text = e.ToString ();
// Application key event log:
label = new Label
{
X = 0,
Y = Pos.Bottom (label) + 1,
Text = "Application Key Events:"
};
win.Add (label);
int maxKeyString = Key.CursorRight.WithAlt.WithCtrl.WithShift.ToString ().Length;
ObservableCollection<string> keyList = new ();
var appKeyListView = new ListView
{
X = 0,
Y = Pos.Bottom (label),
Width = "KeyDown:".Length + maxKeyString,
Height = Dim.Fill (),
Source = new ListWrapper<string> (keyList)
};
appKeyListView.SchemeName = "Runnable";
win.Add (appKeyListView);
// View key events...
edit.KeyDown += (s, a) => { keyDownList.Add (a.ToString ()); };
edit.KeyDownNotHandled += (s, a) =>
{
keyDownNotHandledList.Add ($"{a}");
};
// KeyDown
label = new Label
{
X = Pos.Right (appKeyListView) + 1,
Y = Pos.Top (label),
Text = "TextView Key Down:"
};
win.Add (label);
var onKeyDownListView = new ListView
{
X = Pos.Left (label),
Y = Pos.Bottom (label),
Width = maxKeyString,
Height = Dim.Fill (),
Source = new ListWrapper<string> (keyDownList)
};
appKeyListView.SchemeName = "Runnable";
win.Add (onKeyDownListView);
// KeyDownNotHandled
label = new Label
{
X = Pos.Right (onKeyDownListView) + 1,
Y = Pos.Top (label),
Text = "TextView KeyDownNotHandled:"
};
win.Add (label);
var onKeyDownNotHandledListView = new ListView
{
X = Pos.Left (label),
Y = Pos.Bottom (label),
Width = maxKeyString,
Height = Dim.Fill (),
Source = new ListWrapper<string> (keyDownNotHandledList)
};
appKeyListView.SchemeName = "Runnable";
win.Add (onKeyDownNotHandledListView);
// Swallowed
label = new Label
{
X = Pos.Right (onKeyDownNotHandledListView) + 1,
Y = Pos.Top (label),
Text = "Swallowed:"
};
win.Add (label);
var onSwallowedListView = new ListView
{
X = Pos.Left (label),
Y = Pos.Bottom (label),
Width = maxKeyString,
Height = Dim.Fill (),
Source = new ListWrapper<string> (swallowedList)
};
appKeyListView.SchemeName = "Runnable";
win.Add (onSwallowedListView);
Application.Driver!.GetInputProcessor ().AnsiSequenceSwallowed += (s, e) => { swallowedList.Add (e.Replace ("\x1b", "Esc")); };
Application.KeyDown += (s, a) => KeyDownPressUp (a, "Down");
Application.KeyUp += (s, a) => KeyDownPressUp (a, "Up");
void KeyDownPressUp (Key args, string updown)
{
var msg = $"Key{updown,-7}: {args}";
keyList.Add (msg);
appKeyListView.MoveDown ();
onKeyDownNotHandledListView.MoveDown ();
}
Application.Run (win);
win.Dispose ();
Application.Shutdown ();
}
}