浏览代码

Fixes #2222. RadioGroup first upper case and ProcessColdKey is not working well.

BDisp 2 年之前
父节点
当前提交
8d44217c8a
共有 4 个文件被更改,包括 82 次插入5 次删除
  1. 11 0
      Terminal.Gui/Core/View.cs
  2. 34 5
      Terminal.Gui/Views/RadioGroup.cs
  3. 15 0
      UnitTests/RadioGroupTests.cs
  4. 22 0
      UnitTests/ViewTests.cs

+ 11 - 0
Terminal.Gui/Core/View.cs

@@ -3063,6 +3063,17 @@ namespace Terminal.Gui {
 			return Enabled ? ColorScheme.Normal : ColorScheme.Disabled;
 		}
 
+		/// <summary>
+		/// Determines the current <see cref="ColorScheme"/> based on the <see cref="Enabled"/> value.
+		/// </summary>
+		/// <returns><see cref="Terminal.Gui.ColorScheme.HotNormal"/> if <see cref="Enabled"/> is <see langword="true"/>
+		/// or <see cref="Terminal.Gui.ColorScheme.Disabled"/> if <see cref="Enabled"/> is <see langword="false"/>.
+		/// If it's overridden can return other values.</returns>
+		public virtual Attribute GetHotNormalColor ()
+		{
+			return Enabled ? ColorScheme.HotNormal : ColorScheme.Disabled;
+		}
+
 		/// <summary>
 		/// Get the top superview of a given <see cref="View"/>.
 		/// </summary>

+ 34 - 5
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)

+ 15 - 0
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);
+		}
 	}
 }

+ 22 - 0
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 ());
+		}
 	}
 }