From 40730195547c41fcae7c6630b27bb1da927e0858 Mon Sep 17 00:00:00 2001 From: BDisp Date: Mon, 19 Oct 2020 04:12:21 +0100 Subject: [PATCH 1/6] Fixes #959. PositionCursor with hotkeys. --- Terminal.Gui/Core/TextFormatter.cs | 13 +++++++++++-- Terminal.Gui/Core/View.cs | 2 +- Terminal.Gui/Views/Button.cs | 8 +++++++- Terminal.Gui/Views/RadioGroup.cs | 5 +++++ UICatalog/Scenarios/Buttons.cs | 2 ++ 5 files changed, 26 insertions(+), 4 deletions(-) diff --git a/Terminal.Gui/Core/TextFormatter.cs b/Terminal.Gui/Core/TextFormatter.cs index 84625e2b3..8cd34eb7f 100644 --- a/Terminal.Gui/Core/TextFormatter.cs +++ b/Terminal.Gui/Core/TextFormatter.cs @@ -102,6 +102,11 @@ namespace Terminal.Gui { /// HotKeyTagMask public uint HotKeyTagMask { get; set; } = 0x100000; + /// + /// Gets the position cursor from . If the is defined, the cursor will be positioned over it. + /// + public int PositionCursor { get; set; } + /// /// Gets the formatted lines. /// @@ -567,16 +572,17 @@ namespace Terminal.Gui { int x; switch (textAlignment) { case TextAlignment.Left: - x = bounds.Left; - break; case TextAlignment.Justified: x = bounds.Left; + PositionCursor = hotKeyPos; break; case TextAlignment.Right: x = bounds.Right - runes.Length; + PositionCursor = bounds.Width - runes.Length + hotKeyPos; break; case TextAlignment.Centered: x = bounds.Left + (bounds.Width - runes.Length) / 2; + PositionCursor = (bounds.Width - runes.Length) / 2 + hotKeyPos; break; default: throw new ArgumentOutOfRangeException (); @@ -588,6 +594,9 @@ namespace Terminal.Gui { rune = runes [col - x]; } if ((rune & HotKeyTagMask) == HotKeyTagMask) { + if (textAlignment == TextAlignment.Justified) { + PositionCursor = col - bounds.Left; + } Application.Driver?.SetAttribute (hotColor); Application.Driver?.AddRune ((Rune)((uint)rune & ~HotKeyTagMask)); Application.Driver?.SetAttribute (normalColor); diff --git a/Terminal.Gui/Core/View.cs b/Terminal.Gui/Core/View.cs index 94ceace08..0cc0883ed 100644 --- a/Terminal.Gui/Core/View.cs +++ b/Terminal.Gui/Core/View.cs @@ -1086,7 +1086,7 @@ namespace Terminal.Gui { focused.PositionCursor (); } else { if (CanFocus && HasFocus && Visible) { - Move (textFormatter.HotKeyPos == -1 ? 0 : textFormatter.HotKeyPos, 0); + Move (textFormatter.HotKeyPos == -1 ? 0 : textFormatter.PositionCursor, 0); } else { Move (frame.X, frame.Y); } diff --git a/Terminal.Gui/Views/Button.cs b/Terminal.Gui/Views/Button.cs index 9492ae68e..a4c241e31 100644 --- a/Terminal.Gui/Views/Button.cs +++ b/Terminal.Gui/Views/Button.cs @@ -145,7 +145,13 @@ namespace Terminal.Gui { base.Text = ustring.Make (_leftBracket) + " " + text + " " + ustring.Make (_rightBracket); int w = base.Text.RuneCount - (base.Text.Contains (HotKeySpecifier) ? 1 : 0); - Width = w; + try { + Width = w; +#pragma warning disable RCS1075 // Avoid empty catch clause that catches System.Exception. + } catch (Exception) { +#pragma warning restore RCS1075 // Avoid empty catch clause that catches System.Exception. + // It's a Dim.DimCombine and so can't be assigned. Let it have it's own anchor. + } Height = 1; Frame = new Rect (Frame.Location, new Size (w, 1)); SetNeedsDisplay (); diff --git a/Terminal.Gui/Views/RadioGroup.cs b/Terminal.Gui/Views/RadioGroup.cs index 8e896f1ae..0972a08f3 100644 --- a/Terminal.Gui/Views/RadioGroup.cs +++ b/Terminal.Gui/Views/RadioGroup.cs @@ -259,6 +259,11 @@ namespace Terminal.Gui { } } + public void Refresh () + { + OnSelectedItemChanged (selected, -1); + } + /// /// Called whenever the current selected item changes. Invokes the event. /// diff --git a/UICatalog/Scenarios/Buttons.cs b/UICatalog/Scenarios/Buttons.cs index 43974d741..e0a5d120d 100644 --- a/UICatalog/Scenarios/Buttons.cs +++ b/UICatalog/Scenarios/Buttons.cs @@ -266,6 +266,8 @@ namespace UICatalog { break; } }; + + Top.Ready += () => radioGroup.Refresh (); } } } \ No newline at end of file From d4e7a8c6b3fe07b3b2d4d229c2e13d75bb437599 Mon Sep 17 00:00:00 2001 From: BDisp Date: Mon, 19 Oct 2020 04:19:46 +0100 Subject: [PATCH 2/6] Added XML Comment. --- Terminal.Gui/Views/RadioGroup.cs | 3 +++ 1 file changed, 3 insertions(+) diff --git a/Terminal.Gui/Views/RadioGroup.cs b/Terminal.Gui/Views/RadioGroup.cs index 0972a08f3..40e2b417a 100644 --- a/Terminal.Gui/Views/RadioGroup.cs +++ b/Terminal.Gui/Views/RadioGroup.cs @@ -259,6 +259,9 @@ namespace Terminal.Gui { } } + /// + /// Allow to invoke the after their creation. + /// public void Refresh () { OnSelectedItemChanged (selected, -1); From 9e9de9c1210df35031dd96373652621478e1a0e5 Mon Sep 17 00:00:00 2001 From: BDisp Date: Tue, 20 Oct 2020 20:28:15 +0100 Subject: [PATCH 3/6] Changed to CursorPosition and improving Button LayoutStyle.Computed. --- Terminal.Gui/Core/TextFormatter.cs | 12 ++++++------ Terminal.Gui/Core/View.cs | 2 +- Terminal.Gui/Views/Button.cs | 10 +++++++--- 3 files changed, 14 insertions(+), 10 deletions(-) diff --git a/Terminal.Gui/Core/TextFormatter.cs b/Terminal.Gui/Core/TextFormatter.cs index 8cd34eb7f..f2e3ba4b8 100644 --- a/Terminal.Gui/Core/TextFormatter.cs +++ b/Terminal.Gui/Core/TextFormatter.cs @@ -103,9 +103,9 @@ namespace Terminal.Gui { public uint HotKeyTagMask { get; set; } = 0x100000; /// - /// Gets the position cursor from . If the is defined, the cursor will be positioned over it. + /// Gets the cursor position from . If the is defined, the cursor will be positioned over it. /// - public int PositionCursor { get; set; } + public int CursorPosition { get; set; } /// /// Gets the formatted lines. @@ -574,15 +574,15 @@ namespace Terminal.Gui { case TextAlignment.Left: case TextAlignment.Justified: x = bounds.Left; - PositionCursor = hotKeyPos; + CursorPosition = hotKeyPos; break; case TextAlignment.Right: x = bounds.Right - runes.Length; - PositionCursor = bounds.Width - runes.Length + hotKeyPos; + CursorPosition = bounds.Width - runes.Length + hotKeyPos; break; case TextAlignment.Centered: x = bounds.Left + (bounds.Width - runes.Length) / 2; - PositionCursor = (bounds.Width - runes.Length) / 2 + hotKeyPos; + CursorPosition = (bounds.Width - runes.Length) / 2 + hotKeyPos; break; default: throw new ArgumentOutOfRangeException (); @@ -595,7 +595,7 @@ namespace Terminal.Gui { } if ((rune & HotKeyTagMask) == HotKeyTagMask) { if (textAlignment == TextAlignment.Justified) { - PositionCursor = col - bounds.Left; + CursorPosition = col - bounds.Left; } Application.Driver?.SetAttribute (hotColor); Application.Driver?.AddRune ((Rune)((uint)rune & ~HotKeyTagMask)); diff --git a/Terminal.Gui/Core/View.cs b/Terminal.Gui/Core/View.cs index 0cc0883ed..21b5036fd 100644 --- a/Terminal.Gui/Core/View.cs +++ b/Terminal.Gui/Core/View.cs @@ -1086,7 +1086,7 @@ namespace Terminal.Gui { focused.PositionCursor (); } else { if (CanFocus && HasFocus && Visible) { - Move (textFormatter.HotKeyPos == -1 ? 0 : textFormatter.PositionCursor, 0); + Move (textFormatter.HotKeyPos == -1 ? 0 : textFormatter.CursorPosition, 0); } else { Move (frame.X, frame.Y); } diff --git a/Terminal.Gui/Views/Button.cs b/Terminal.Gui/Views/Button.cs index a4c241e31..9d558bf6e 100644 --- a/Terminal.Gui/Views/Button.cs +++ b/Terminal.Gui/Views/Button.cs @@ -147,10 +147,14 @@ namespace Terminal.Gui { int w = base.Text.RuneCount - (base.Text.Contains (HotKeySpecifier) ? 1 : 0); try { Width = w; -#pragma warning disable RCS1075 // Avoid empty catch clause that catches System.Exception. } catch (Exception) { -#pragma warning restore RCS1075 // Avoid empty catch clause that catches System.Exception. - // It's a Dim.DimCombine and so can't be assigned. Let it have it's own anchor. + // It's a Dim.DimCombine and so can't be assigned. Let it have it's own anchor. + var width = Width; + LayoutStyle = LayoutStyle.Absolute; + Width = w; + LayoutStyle = LayoutStyle.Computed; + Width = width; + w = width.Anchor (w); } Height = 1; Frame = new Rect (Frame.Location, new Size (w, 1)); From bb67b5ad07312ac6e2d13b716b5c1227dae65aae Mon Sep 17 00:00:00 2001 From: BDisp Date: Tue, 20 Oct 2020 20:49:29 +0100 Subject: [PATCH 4/6] Removed unnecessary code. --- Terminal.Gui/Views/Button.cs | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/Terminal.Gui/Views/Button.cs b/Terminal.Gui/Views/Button.cs index 9d558bf6e..af26ab937 100644 --- a/Terminal.Gui/Views/Button.cs +++ b/Terminal.Gui/Views/Button.cs @@ -149,12 +149,7 @@ namespace Terminal.Gui { Width = w; } catch (Exception) { // It's a Dim.DimCombine and so can't be assigned. Let it have it's own anchor. - var width = Width; - LayoutStyle = LayoutStyle.Absolute; - Width = w; - LayoutStyle = LayoutStyle.Computed; - Width = width; - w = width.Anchor (w); + w = Width.Anchor (w); } Height = 1; Frame = new Rect (Frame.Location, new Size (w, 1)); From be840f31cca31f01505dbb491e9e86442033142b Mon Sep 17 00:00:00 2001 From: BDisp Date: Tue, 20 Oct 2020 21:11:11 +0100 Subject: [PATCH 5/6] Changing Dim.DimCombine class as public. --- Terminal.Gui/Core/PosDim.cs | 2 +- Terminal.Gui/Views/Button.cs | 4 +--- 2 files changed, 2 insertions(+), 4 deletions(-) diff --git a/Terminal.Gui/Core/PosDim.cs b/Terminal.Gui/Core/PosDim.cs index efacd12c8..bf13d944c 100644 --- a/Terminal.Gui/Core/PosDim.cs +++ b/Terminal.Gui/Core/PosDim.cs @@ -499,7 +499,7 @@ namespace Terminal.Gui { return new DimAbsolute (n); } - class DimCombine : Dim { + public class DimCombine : Dim { Dim left, right; bool add; public DimCombine (bool add, Dim left, Dim right) diff --git a/Terminal.Gui/Views/Button.cs b/Terminal.Gui/Views/Button.cs index af26ab937..73beba18d 100644 --- a/Terminal.Gui/Views/Button.cs +++ b/Terminal.Gui/Views/Button.cs @@ -145,9 +145,7 @@ namespace Terminal.Gui { base.Text = ustring.Make (_leftBracket) + " " + text + " " + ustring.Make (_rightBracket); int w = base.Text.RuneCount - (base.Text.Contains (HotKeySpecifier) ? 1 : 0); - try { - Width = w; - } catch (Exception) { + if (Width is Dim.DimCombine) { // It's a Dim.DimCombine and so can't be assigned. Let it have it's own anchor. w = Width.Anchor (w); } From 5fe4e8be3319831a404b800bd84c0e389ba5ee82 Mon Sep 17 00:00:00 2001 From: BDisp Date: Wed, 21 Oct 2020 09:19:33 +0100 Subject: [PATCH 6/6] Changed DimCombine class to internal. --- Terminal.Gui/Core/PosDim.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Terminal.Gui/Core/PosDim.cs b/Terminal.Gui/Core/PosDim.cs index bf13d944c..59ca006f1 100644 --- a/Terminal.Gui/Core/PosDim.cs +++ b/Terminal.Gui/Core/PosDim.cs @@ -499,7 +499,7 @@ namespace Terminal.Gui { return new DimAbsolute (n); } - public class DimCombine : Dim { + internal class DimCombine : Dim { Dim left, right; bool add; public DimCombine (bool add, Dim left, Dim right)