|
| 1 | +#nullable enable |
| 2 | + |
| 3 | +namespace UICatalog.Scenarios; |
| 4 | + |
| 5 | +/// <summary> |
| 6 | +/// Demonstrates the SchemeName fallback chain introduced in v2. |
| 7 | +/// When a view's <see cref="View.SchemeName"/> is not found in the active theme, the view no longer |
| 8 | +/// throws a <see cref="KeyNotFoundException"/>. Instead, it walks the fallback chain: |
| 9 | +/// <list type="number"> |
| 10 | +/// <item> |
| 11 | +/// <description>Named scheme (if found in current theme)</description> |
| 12 | +/// </item> |
| 13 | +/// <item> |
| 14 | +/// <description>SuperView's scheme (recursive)</description> |
| 15 | +/// </item> |
| 16 | +/// <item> |
| 17 | +/// <description>"Base" scheme from the current theme</description> |
| 18 | +/// </item> |
| 19 | +/// <item> |
| 20 | +/// <description>Hard-coded "Base" scheme (always present)</description> |
| 21 | +/// </item> |
| 22 | +/// </list> |
| 23 | +/// </summary> |
| 24 | +[ScenarioMetadata ("Theme Fallback", "Demonstrates graceful SchemeName fallback when a named scheme is missing from the active theme.")] |
| 25 | +[ScenarioCategory ("Colors")] |
| 26 | +[ScenarioCategory ("Configuration")] |
| 27 | +public sealed class ThemeFallback : Scenario |
| 28 | +{ |
| 29 | + private const string CUSTOM_SCHEME_NAME = "CustomHighlight"; |
| 30 | + private const string MISSING_SCHEME_NAME = "NonExistentScheme"; |
| 31 | + |
| 32 | + public override void Main () |
| 33 | + { |
| 34 | + ConfigurationManager.Enable (ConfigLocations.All); |
| 35 | + |
| 36 | + using IApplication app = Application.Create (); |
| 37 | + app.Init (); |
| 38 | + |
| 39 | + // Extend the Default theme with a custom scheme that has TextStyle.Blink |
| 40 | + // so it stands out visually. Other built-in themes do NOT contain this scheme, |
| 41 | + // so switching themes lets you watch the fallback chain activate in real time. |
| 42 | + SchemeManager.AddScheme (CUSTOM_SCHEME_NAME, new () { Normal = new Attribute (Color.BrightYellow, Color.Blue, TextStyle.Blink) }); |
| 43 | + |
| 44 | + using Window appWindow = new (); |
| 45 | + appWindow.Title = GetQuitKeyAndName (); |
| 46 | + |
| 47 | + // --- Theme selector --- |
| 48 | + string [] themeLabels = ThemeManager.GetThemeNames ().Select (n => "_" + n).ToArray (); |
| 49 | + |
| 50 | + OptionSelector themeSelector = new () |
| 51 | + { |
| 52 | + Title = "_Theme", |
| 53 | + BorderStyle = LineStyle.Rounded, |
| 54 | + X = 1, |
| 55 | + Y = 1, |
| 56 | + Width = Dim.Auto (), |
| 57 | + Height = Dim.Auto (), |
| 58 | + Labels = themeLabels, |
| 59 | + Value = ThemeManager.GetThemeNames ().IndexOf (ThemeManager.Theme) |
| 60 | + }; |
| 61 | + |
| 62 | + themeSelector.ValueChanged += (sender, args) => |
| 63 | + { |
| 64 | + if (sender is not OptionSelector sel) |
| 65 | + { |
| 66 | + return; |
| 67 | + } |
| 68 | + |
| 69 | + string rawLabel = sel.Labels! [(int)args.NewValue!]; |
| 70 | + |
| 71 | + // Strip the leading underscore added for keyboard shortcut. |
| 72 | + ThemeManager.Theme = rawLabel [1..]; |
| 73 | + ConfigurationManager.Apply (); |
| 74 | + |
| 75 | + // Re-add the custom scheme to the newly-active theme so the |
| 76 | + // "Default" theme always demonstrates the found case. |
| 77 | + if (ThemeManager.Theme == ThemeManager.DEFAULT_THEME_NAME) |
| 78 | + { |
| 79 | + SchemeManager.AddScheme (CUSTOM_SCHEME_NAME, |
| 80 | + new () { Normal = new Attribute (Color.BrightYellow, Color.Blue, TextStyle.Blink) }); |
| 81 | + } |
| 82 | + }; |
| 83 | + |
| 84 | + // --- Explanation --- |
| 85 | + Label intro = new () |
| 86 | + { |
| 87 | + X = Pos.Right (themeSelector) + 1, |
| 88 | + Y = 1, |
| 89 | + Width = Dim.Fill (1), |
| 90 | + Text = $"Switch to a non-Default theme to see the fallback activate.\n" |
| 91 | + + $" • \"{CUSTOM_SCHEME_NAME}\" is only in the Default theme.\n" |
| 92 | + + $" • \"{MISSING_SCHEME_NAME}\" is never in any theme.\n" |
| 93 | + + $"In both missing cases the view falls back gracefully instead of throwing." |
| 94 | + }; |
| 95 | + |
| 96 | + // --- View 1: scheme FOUND in the active theme --- |
| 97 | + FrameView foundFrame = new () |
| 98 | + { |
| 99 | + Title = $"SchemeName = \"{CUSTOM_SCHEME_NAME}\"", |
| 100 | + X = Pos.Right (themeSelector) + 1, |
| 101 | + Y = Pos.Bottom (intro) + 1, |
| 102 | + Width = Dim.Fill (1), |
| 103 | + Height = 5, |
| 104 | + SchemeName = CUSTOM_SCHEME_NAME |
| 105 | + }; |
| 106 | + |
| 107 | + Label foundLabel = new () |
| 108 | + { |
| 109 | + X = 1, |
| 110 | + Y = 1, |
| 111 | + Width = Dim.Fill (2), |
| 112 | + Text = $"On the Default theme this scheme exists (BrightYellow/Blue + Blink).\n" |
| 113 | + + $"On any other theme the scheme is missing → fallback chain activates." |
| 114 | + }; |
| 115 | + foundFrame.Add (foundLabel); |
| 116 | + |
| 117 | + // --- View 2: scheme NEVER found — fallback always activates --- |
| 118 | + FrameView missingFrame = new () |
| 119 | + { |
| 120 | + Title = $"SchemeName = \"{MISSING_SCHEME_NAME}\"", |
| 121 | + X = Pos.Right (themeSelector) + 1, |
| 122 | + Y = Pos.Bottom (foundFrame) + 1, |
| 123 | + Width = Dim.Fill (1), |
| 124 | + Height = 5, |
| 125 | + SchemeName = MISSING_SCHEME_NAME |
| 126 | + }; |
| 127 | + |
| 128 | + Label missingLabel = new () |
| 129 | + { |
| 130 | + X = 1, |
| 131 | + Y = 1, |
| 132 | + Width = Dim.Fill (2), |
| 133 | + Text = $"This scheme does not exist in any theme.\n" |
| 134 | + + $"The view silently falls back to its SuperView's scheme (no exception).\n" |
| 135 | + + $"A warning is written to the debug log." |
| 136 | + }; |
| 137 | + missingFrame.Add (missingLabel); |
| 138 | + |
| 139 | + appWindow.Add (themeSelector, intro, foundFrame, missingFrame); |
| 140 | + |
| 141 | + app.Run (appWindow); |
| 142 | + } |
| 143 | +} |
0 commit comments