From 8d44217c8a7803657d321fc5a8076c16cc279fe7 Mon Sep 17 00:00:00 2001 From: BDisp Date: Mon, 14 Nov 2022 13:22:52 +0000 Subject: [PATCH] Fixes #2222. RadioGroup first upper case and ProcessColdKey is not working well. --- Terminal.Gui/Core/View.cs | 11 +++++++++ Terminal.Gui/Views/RadioGroup.cs | 39 ++++++++++++++++++++++++++++---- UnitTests/RadioGroupTests.cs | 15 ++++++++++++ UnitTests/ViewTests.cs | 22 ++++++++++++++++++ 4 files changed, 82 insertions(+), 5 deletions(-) diff --git a/Terminal.Gui/Core/View.cs b/Terminal.Gui/Core/View.cs index 274f40de9..b9094e03f 100644 --- a/Terminal.Gui/Core/View.cs +++ b/Terminal.Gui/Core/View.cs @@ -3063,6 +3063,17 @@ namespace Terminal.Gui { return Enabled ? ColorScheme.Normal : ColorScheme.Disabled; } + /// + /// Determines the current based on the value. + /// + /// if is + /// or if is . + /// If it's overridden can return other values. + public virtual Attribute GetHotNormalColor () + { + return Enabled ? ColorScheme.HotNormal : ColorScheme.Disabled; + } + /// /// Get the top superview of a given . /// diff --git a/Terminal.Gui/Views/RadioGroup.cs b/Terminal.Gui/Views/RadioGroup.cs index b97d87b60..aaa510abf 100644 --- a/Terminal.Gui/Views/RadioGroup.cs +++ b/Terminal.Gui/Views/RadioGroup.cs @@ -67,6 +67,7 @@ namespace Terminal.Gui { Frame = rect; } CanFocus = true; + HotKeySpecifier = new Rune ('_'); // Things this view knows how to do AddCommand (Command.LineUp, () => { MoveUp (); return true; }); @@ -215,9 +216,36 @@ namespace Terminal.Gui { Move (horizontal [i].pos, 0); break; } + var rl = radioLabels [i]; Driver.SetAttribute (GetNormalColor ()); Driver.AddStr (ustring.Make (new Rune [] { i == selected ? Driver.Selected : Driver.UnSelected, ' ' })); - DrawHotString (radioLabels [i], HasFocus && i == cursor, ColorScheme); + TextFormatter.FindHotKey (rl, HotKeySpecifier, true, out int hotPos, out Key hotKey); + if (hotPos != -1 && (hotKey != Key.Null || hotKey != Key.Unknown)) { + var rlRunes = rl.ToRunes (); + for (int j = 0; j < rlRunes.Length; j++) { + Rune rune = rlRunes [j]; + if (j == hotPos && i == cursor) { + Application.Driver.SetAttribute (HasFocus ? ColorScheme.HotFocus : GetHotNormalColor ()); + } else if (j == hotPos && i != cursor) { + Application.Driver.SetAttribute (GetHotNormalColor ()); + } else if (HasFocus && i == cursor) { + Application.Driver.SetAttribute (ColorScheme.Focus); + } + if (rune == HotKeySpecifier && j + 1 < rlRunes.Length) { + j++; + rune = rlRunes [j]; + if (i == cursor) { + Application.Driver.SetAttribute (HasFocus ? ColorScheme.HotFocus : GetHotNormalColor ()); + } else if (i != cursor) { + Application.Driver.SetAttribute (GetHotNormalColor ()); + } + } + Application.Driver.AddRune (rune); + Driver.SetAttribute (GetNormalColor ()); + } + } else { + DrawHotString (rl, HasFocus && i == cursor, ColorScheme); + } } } @@ -280,11 +308,12 @@ namespace Terminal.Gui { key = Char.ToUpper ((char)key); foreach (var l in radioLabels) { bool nextIsHot = false; - foreach (var c in l) { - if (c == '_') + TextFormatter.FindHotKey (l, HotKeySpecifier, true, out _, out Key hotKey); + foreach (Rune c in l) { + if (c == HotKeySpecifier) { nextIsHot = true; - else { - if (nextIsHot && c == key) { + } else { + if ((nextIsHot && Rune.ToUpper (c) == key) || (key == (uint)hotKey)) { SelectedItem = i; cursor = i; if (!HasFocus) diff --git a/UnitTests/RadioGroupTests.cs b/UnitTests/RadioGroupTests.cs index 753628802..489f80897 100644 --- a/UnitTests/RadioGroupTests.cs +++ b/UnitTests/RadioGroupTests.cs @@ -172,5 +172,20 @@ namespace Terminal.Gui.Views { Assert.True (rg.ProcessKey (new KeyEvent (Key.Space, new KeyModifiers ()))); Assert.Equal (1, rg.SelectedItem); } + + [Fact] + public void ProcessColdKey_HotKey () + { + var rg = new RadioGroup (new NStack.ustring [] { "Left", "Right", "Cen_tered", "Justified" }); + + Assert.True (rg.ProcessColdKey (new KeyEvent (Key.t, new KeyModifiers ()))); + Assert.Equal (2, rg.SelectedItem); + Assert.True (rg.ProcessColdKey (new KeyEvent (Key.L, new KeyModifiers ()))); + Assert.Equal (0, rg.SelectedItem); + Assert.True (rg.ProcessColdKey (new KeyEvent (Key.J, new KeyModifiers ()))); + Assert.Equal (3, rg.SelectedItem); + Assert.True (rg.ProcessColdKey (new KeyEvent (Key.R, new KeyModifiers ()))); + Assert.Equal (1, rg.SelectedItem); + } } } diff --git a/UnitTests/ViewTests.cs b/UnitTests/ViewTests.cs index e2a20e80b..8eda59c33 100644 --- a/UnitTests/ViewTests.cs +++ b/UnitTests/ViewTests.cs @@ -4062,5 +4062,27 @@ This is a tes Assert.False (view.IsKeyPress); Assert.True (view.IsKeyUp); } + + [Fact, AutoInitShutdown] + public void GetNormalColor_ColorScheme () + { + var view = new View { ColorScheme = Colors.Base }; + + Assert.Equal (view.ColorScheme.Normal, view.GetNormalColor ()); + + view.Enabled = false; + Assert.Equal (view.ColorScheme.Disabled, view.GetNormalColor ()); + } + + [Fact, AutoInitShutdown] + public void GetHotNormalColor_ColorScheme () + { + var view = new View { ColorScheme = Colors.Base }; + + Assert.Equal (view.ColorScheme.HotNormal, view.GetHotNormalColor ()); + + view.Enabled = false; + Assert.Equal (view.ColorScheme.Disabled, view.GetHotNormalColor ()); + } } }