Files
Terminal.Gui/Examples/UICatalog/Scenarios/WideGlyphs.cs
Tig 4c772bd5f3 Fixes #4497 - makes replacement char conifgurable (#4498)
* Add Glyphs.ReplacementChar config property

Introduced Glyphs.ReplacementChar to allow overriding the Unicode replacement character, defaulting to a space (' '). Updated both config.json and Glyphs.cs with this property, scoped to ThemeScope and documented as an override for Rune.ReplacementChar.

* Standardize to Glyphs.ReplacementChar for wide char invalidation

Replaced all uses of Rune.ReplacementChar.ToString() with Glyphs.ReplacementChar.ToString() in OutputBufferImpl and related tests. This ensures consistent use of the replacement character when invalidating or overwriting wide characters in the output buffer.

* Add configurable wide glyph replacement chars to OutputBuffer

Allows setting custom replacement characters for wide glyphs that cannot fit in the available space via IOutputBuffer.SetReplacementChars. Updated IDriver to expose GetOutputBuffer. All code paths and tests now use the configurable characters, improving testability and flexibility. Tests now use '①' and '②' for clarity instead of the default replacement character.

* Fixed warnings.

* Update IOutputBuffer.cs

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

* Add tests for wide char clipping edge cases in OutputBuffer

Added three unit tests to OutputBufferWideCharTests.cs to verify and document OutputBufferImpl's behavior when wide (double-width) characters are written at the edges of a clipping region. Tests cover cases where the first or second column of a wide character is outside the clip, as well as when both columns are inside. The tests assert correct use of replacement characters, dirty flags, and column advancement, and document that certain code paths are currently unreachable due to IsValidLocation checks.

* Clarify dead code path with explanatory comments

Added comments to mark a rarely executed code path as dead code, noting it is apparently never called. Referenced the related test scenario AddStr_WideChar_FirstColumnOutsideClip_SecondColumnInside_CurrentBehavior for context. No functional changes were made.

* Remove dead code for wide char partial clip handling

Removed unreachable code that handled the case where the first column of a wide character is outside the clipping region but the second column is inside. This logic was marked as dead code and never called. Now, only the cases where the second column is outside the clip or both columns are in bounds are handled. This simplifies the code and removes unnecessary checks.

* Replaces Glyphs.ReplacementChar with Glyphs.WideGlyphReplacement to clarify its use for clipped wide glyphs. Updates IOutputBuffer to use SetWideGlyphReplacement (single Rune) instead of SetReplacementChars (two Runes). Refactors OutputBufferImpl and all test code to use the new property and method. Removes second-column replacement logic, simplifying the API and improving consistency. Updates comments and test assertions to match the new naming and behavior.

* Update themes in config.json and add new UI Catalog props

Renamed "UI Catalog Theme" to "UI Catalog" and removed the
"Glyphs.ReplacementChar" property. Added several new properties
to the "UI Catalog" theme, including default shadow, highlight
states, button alignment, and separator line style. Also added
"Glyphs.WideGlyphReplacement" to the "Hot Dog Stand" theme.

---------

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
2025-12-15 09:26:52 -07:00

223 lines
7.0 KiB
C#

#nullable enable
using System.Text;
namespace UICatalog.Scenarios;
[ScenarioMetadata ("WideGlyphs", "Demonstrates wide glyphs with overlapped views & clipping")]
[ScenarioCategory ("Unicode")]
[ScenarioCategory ("Drawing")]
public sealed class WideGlyphs : Scenario
{
private Rune [,]? _codepoints;
public override void Main ()
{
// Init
Application.Init ();
// Setup - Create a top-level application window and configure it.
Window appWindow = new ()
{
Title = GetQuitKeyAndName (),
BorderStyle = LineStyle.None
};
// Add Editors
AdornmentsEditor adornmentsEditor = new ()
{
BorderStyle = LineStyle.Single,
X = Pos.AnchorEnd (),
AutoSelectViewToEdit = true,
AutoSelectAdornments = false,
ShowViewIdentifier = true
};
appWindow.Add (adornmentsEditor);
ViewportSettingsEditor viewportSettingsEditor = new ()
{
BorderStyle = LineStyle.Single,
Y = Pos.AnchorEnd (),
X = Pos.AnchorEnd (),
AutoSelectViewToEdit = true,
};
appWindow.Add (viewportSettingsEditor);
// Build the array of codepoints once when subviews are laid out
appWindow.SubViewsLaidOut += (s, _) =>
{
View? view = s as View;
if (view is null)
{
return;
}
// Only rebuild if size changed or array is null
if (_codepoints is null ||
_codepoints.GetLength (0) != view.Viewport.Height ||
_codepoints.GetLength (1) != view.Viewport.Width)
{
_codepoints = new Rune [view.Viewport.Height, view.Viewport.Width];
for (int r = 0; r < view.Viewport.Height; r++)
{
for (int c = 0; c < view.Viewport.Width; c += 2)
{
_codepoints [r, c] = GetRandomWideCodepoint ();
}
}
}
};
// Fill the window with the pre-built codepoints array
// For detailed documentation on the draw code flow from Application.Run to this event,
// see WideGlyphs.DrawFlow.md in this directory
appWindow.DrawingContent += (s, e) =>
{
View? view = s as View;
if (view is null || _codepoints is null)
{
return;
}
// Traverse the Viewport, using the pre-built array
for (int r = 0; r < view.Viewport.Height && r < _codepoints.GetLength (0); r++)
{
for (int c = 0; c < view.Viewport.Width && c < _codepoints.GetLength (1); c += 2)
{
Rune codepoint = _codepoints [r, c];
if (codepoint != default (Rune))
{
view.AddRune (c, r, codepoint);
}
}
}
e.DrawContext?.AddDrawnRectangle (view.Viewport);
};
Line verticalLineAtEven = new ()
{
X = 10,
Orientation = Orientation.Vertical,
Length = Dim.Fill ()
};
appWindow.Add (verticalLineAtEven);
Line verticalLineAtOdd = new ()
{
X = 25,
Orientation = Orientation.Vertical,
Length = Dim.Fill ()
};
appWindow.Add (verticalLineAtOdd);
View arrangeableViewAtEven = new ()
{
CanFocus = true,
Arrangement = ViewArrangement.Movable | ViewArrangement.Resizable,
X = 30,
Y = 5,
Width = 15,
Height = 5,
//BorderStyle = LineStyle.Dashed
};
// Proves it's not LineCanvas related
arrangeableViewAtEven!.Border!.Thickness = new (1);
arrangeableViewAtEven.Border.Add (new View () { Height = Dim.Auto (), Width = Dim.Auto (), Text = "Even" });
appWindow.Add (arrangeableViewAtEven);
View arrangeableViewAtOdd = new ()
{
CanFocus = true,
Arrangement = ViewArrangement.Movable | ViewArrangement.Resizable,
X = 31,
Y = 11,
Width = 15,
Height = 5,
BorderStyle = LineStyle.Dashed,
};
appWindow.Add (arrangeableViewAtOdd);
var superView = new View
{
CanFocus = true,
X = 30, // on an even column to start
Y = Pos.Center (),
Width = Dim.Auto (),
Height = Dim.Auto (),
BorderStyle = LineStyle.Single,
Arrangement = ViewArrangement.Movable | ViewArrangement.Resizable
};
Rune codepoint = Glyphs.Apple;
superView.DrawingContent += (s, e) =>
{
var view = s as View;
for (var r = 0; r < view!.Viewport.Height; r++)
{
for (var c = 0; c < view.Viewport.Width; c += 2)
{
if (codepoint != default (Rune))
{
view.AddRune (c, r, codepoint);
}
}
}
e.DrawContext?.AddDrawnRectangle (view.Viewport);
e.Cancel = true;
};
appWindow.Add (superView);
var viewWithBorderAtX0 = new View
{
Text = "viewWithBorderAtX0",
BorderStyle = LineStyle.Dashed,
X = 0,
Y = 1,
Width = Dim.Auto (),
Height = 3
};
var viewWithBorderAtX1 = new View
{
Text = "viewWithBorderAtX1",
BorderStyle = LineStyle.Dashed,
X = 1,
Y = Pos.Bottom (viewWithBorderAtX0) + 1,
Width = Dim.Auto (),
Height = 3
};
var viewWithBorderAtX2 = new View
{
Text = "viewWithBorderAtX2",
BorderStyle = LineStyle.Dashed,
X = 2,
Y = Pos.Bottom (viewWithBorderAtX1) + 1,
Width = Dim.Auto (),
Height = 3
};
superView.Add (viewWithBorderAtX0, viewWithBorderAtX1, viewWithBorderAtX2);
// Run - Start the application.
Application.Run (appWindow);
appWindow.Dispose ();
// Shutdown - Calling Application.Shutdown is required.
Application.Shutdown ();
}
private Rune GetRandomWideCodepoint ()
{
Random random = new ();
int codepoint = random.Next (0x4E00, 0x9FFF);
return new (codepoint);
}
}