|
| 1 | +#nullable enable |
| 2 | + |
| 3 | +using System.Text; |
| 4 | + |
| 5 | +namespace UICatalog.Scenarios; |
| 6 | + |
| 7 | +[ScenarioMetadata ("WideGlyphs", "Demonstrates wide glyphs with overlapped views & clipping")] |
| 8 | +[ScenarioCategory ("Unicode")] |
| 9 | +[ScenarioCategory ("Drawing")] |
| 10 | + |
| 11 | +public sealed class WideGlyphs : Scenario |
| 12 | +{ |
| 13 | + private Rune [,]? _codepoints; |
| 14 | + |
| 15 | + public override void Main () |
| 16 | + { |
| 17 | + // Init |
| 18 | + Application.Init (); |
| 19 | + |
| 20 | + // Setup - Create a top-level application window and configure it. |
| 21 | + Window appWindow = new () |
| 22 | + { |
| 23 | + Title = GetQuitKeyAndName (), |
| 24 | + BorderStyle = LineStyle.None |
| 25 | + }; |
| 26 | + |
| 27 | + // Build the array of codepoints once when subviews are laid out |
| 28 | + appWindow.SubViewsLaidOut += (s, e) => |
| 29 | + { |
| 30 | + View? view = s as View; |
| 31 | + if (view is null) |
| 32 | + { |
| 33 | + return; |
| 34 | + } |
| 35 | + |
| 36 | + // Only rebuild if size changed or array is null |
| 37 | + if (_codepoints is null || |
| 38 | + _codepoints.GetLength (0) != view.Viewport.Height || |
| 39 | + _codepoints.GetLength (1) != view.Viewport.Width) |
| 40 | + { |
| 41 | + _codepoints = new Rune [view.Viewport.Height, view.Viewport.Width]; |
| 42 | + |
| 43 | + for (int r = 0; r < view.Viewport.Height; r++) |
| 44 | + { |
| 45 | + for (int c = 0; c < view.Viewport.Width; c += 2) |
| 46 | + { |
| 47 | + _codepoints [r, c] = GetRandomWideCodepoint (); |
| 48 | + } |
| 49 | + } |
| 50 | + } |
| 51 | + }; |
| 52 | + |
| 53 | + // Fill the window with the pre-built codepoints array |
| 54 | + appWindow.DrawingContent += (s, e) => |
| 55 | + { |
| 56 | + View? view = s as View; |
| 57 | + if (view is null || _codepoints is null) |
| 58 | + { |
| 59 | + return; |
| 60 | + } |
| 61 | + |
| 62 | + // Traverse the Viewport, using the pre-built array |
| 63 | + for (int r = 0; r < view.Viewport.Height && r < _codepoints.GetLength (0); r++) |
| 64 | + { |
| 65 | + for (int c = 0; c < view.Viewport.Width && c < _codepoints.GetLength (1); c += 2) |
| 66 | + { |
| 67 | + Rune codepoint = _codepoints [r, c]; |
| 68 | + if (codepoint != default (Rune)) |
| 69 | + { |
| 70 | + view.AddRune (c, r, codepoint); |
| 71 | + } |
| 72 | + } |
| 73 | + } |
| 74 | + }; |
| 75 | + |
| 76 | + Line verticalLineAtEven = new Line () |
| 77 | + { |
| 78 | + X = 10, |
| 79 | + Orientation = Orientation.Vertical, |
| 80 | + Length = Dim.Fill () |
| 81 | + }; |
| 82 | + appWindow.Add (verticalLineAtEven); |
| 83 | + |
| 84 | + Line verticalLineAtOdd = new Line () |
| 85 | + { |
| 86 | + X = 25, |
| 87 | + Orientation = Orientation.Vertical, |
| 88 | + Length = Dim.Fill () |
| 89 | + }; |
| 90 | + appWindow.Add (verticalLineAtOdd); |
| 91 | + |
| 92 | + View arrangeableViewAtEven = new () |
| 93 | + { |
| 94 | + CanFocus = true, |
| 95 | + Arrangement = ViewArrangement.Movable | ViewArrangement.Resizable, |
| 96 | + X = 30, |
| 97 | + Y = 5, |
| 98 | + Width = 15, |
| 99 | + Height = 5, |
| 100 | + BorderStyle = LineStyle.Dashed, |
| 101 | + }; |
| 102 | + appWindow.Add (arrangeableViewAtEven); |
| 103 | + |
| 104 | + View arrangeableViewAtOdd = new () |
| 105 | + { |
| 106 | + CanFocus = true, |
| 107 | + Arrangement = ViewArrangement.Movable | ViewArrangement.Resizable, |
| 108 | + X = 31, |
| 109 | + Y = 11, |
| 110 | + Width = 15, |
| 111 | + Height = 5, |
| 112 | + BorderStyle = LineStyle.Dashed, |
| 113 | + }; |
| 114 | + appWindow.Add (arrangeableViewAtOdd); |
| 115 | + // Run - Start the application. |
| 116 | + Application.Run (appWindow); |
| 117 | + appWindow.Dispose (); |
| 118 | + |
| 119 | + // Shutdown - Calling Application.Shutdown is required. |
| 120 | + Application.Shutdown (); |
| 121 | + } |
| 122 | + |
| 123 | + private Rune GetRandomWideCodepoint () |
| 124 | + { |
| 125 | + Random random = new (); |
| 126 | + int codepoint = random.Next (0x4E00, 0x9FFF); |
| 127 | + return new Rune (codepoint); |
| 128 | + } |
| 129 | +} |
0 commit comments