From 49e71b45af6061d7ad7d9e0d5ea903f68cd31e68 Mon Sep 17 00:00:00 2001 From: BDisp Date: Mon, 19 Jul 2021 14:21:05 +0100 Subject: [PATCH] Fixes #1380. Setting AllowsTab in TextView keeps Multiline. --- Terminal.Gui/Views/TextView.cs | 21 +++++++++------------ UICatalog/Scenarios/Editor.cs | 28 +++++++++++++++++++++++----- UnitTests/TextViewTests.cs | 30 ++++++++++++++++++++++-------- 3 files changed, 54 insertions(+), 25 deletions(-) diff --git a/Terminal.Gui/Views/TextView.cs b/Terminal.Gui/Views/TextView.cs index 1849406a4..74b1b3a83 100644 --- a/Terminal.Gui/Views/TextView.cs +++ b/Terminal.Gui/Views/TextView.cs @@ -206,7 +206,7 @@ namespace Terminal.Gui { last = last < lines.Count ? last : lines.Count; for (int i = first; i < last; i++) { var line = GetLine (i); - var tabSum = line.Sum (r => r == '\t' ? tabWidth - 1 : 0); + var tabSum = line.Sum (r => r == '\t' ? Math.Max (tabWidth - 1, 0) : 0); var l = line.Count + tabSum; if (l > maxLength) { maxLength = l; @@ -236,7 +236,7 @@ namespace Terminal.Gui { for (int i = start; i < t.Count; i++) { var r = t [i]; size += Rune.ColumnWidth (r); - if (r == '\t' && tabWidth > 0) { + if (r == '\t') { size += tabWidth + 1; } if (i == pX || (size > pX)) { @@ -261,7 +261,7 @@ namespace Terminal.Gui { var rune = t [i]; size += Rune.ColumnWidth (rune); len += Rune.RuneLen (rune); - if (rune == '\t' && tabWidth > 0) { + if (rune == '\t') { size += tabWidth + 1; len += tabWidth - 1; } @@ -276,7 +276,7 @@ namespace Terminal.Gui { { s = Rune.ColumnWidth (r); l = Rune.RuneLen (r); - if (r == '\t' && tWidth > 0) { + if (r == '\t') { s += tWidth + 1; l += tWidth - 1; } @@ -1132,6 +1132,7 @@ namespace Terminal.Gui { Multiline = false; AllowsTab = false; } + SetNeedsDisplay (); } } @@ -1149,12 +1150,10 @@ namespace Terminal.Gui { if (allowsTab && !multiline) { Multiline = true; } - if (!allowsTab && multiline) { - Multiline = false; - } if (!allowsTab && tabWidth > 0) { tabWidth = 0; } + SetNeedsDisplay (); } } @@ -1165,12 +1164,10 @@ namespace Terminal.Gui { get => tabWidth; set { tabWidth = Math.Max (value, 0); - if (tabWidth == 0 && AllowsTab) { - AllowsTab = false; - } if (tabWidth > 0 && !AllowsTab) { AllowsTab = true; } + SetNeedsDisplay (); } } @@ -1306,7 +1303,7 @@ namespace Terminal.Gui { if (idx >= currentColumn) break; var cols = Rune.ColumnWidth (line [idx]); - if (line [idx] == '\t' && TabWidth > 0) { + if (line [idx] == '\t') { cols += TabWidth + 1; } TextModel.SetCol (ref col, Frame.Width, cols); @@ -1701,7 +1698,7 @@ namespace Terminal.Gui { ColorNormal (line, idxCol); } - if (rune == '\t' && TabWidth > 0) { + if (rune == '\t') { cols += TabWidth + 1; if (col + cols > right) { cols = right - col; diff --git a/UICatalog/Scenarios/Editor.cs b/UICatalog/Scenarios/Editor.cs index 43ab6de42..ae32eaf19 100644 --- a/UICatalog/Scenarios/Editor.cs +++ b/UICatalog/Scenarios/Editor.cs @@ -68,7 +68,10 @@ namespace UICatalog { new MenuItem (" B_ox Fix", "", () => SetCursor(CursorVisibility.BoxFix)), new MenuItem (" U_nderline Fix","", () => SetCursor(CursorVisibility.UnderlineFix)) }), - new MenuBarItem ("Forma_t", CreateWrapChecked ()) + new MenuBarItem ("Forma_t", new MenuItem [] { + CreateWrapChecked (), + CreateAllowsTabChecked () + }) }); Top.Add (menu); @@ -423,10 +426,11 @@ namespace UICatalog { return new MenuItem [] { item }; } - private MenuItem [] CreateWrapChecked () + private MenuItem CreateWrapChecked () { - var item = new MenuItem (); - item.Title = "Word Wrap"; + var item = new MenuItem { + Title = "Word Wrap" + }; item.CheckType |= MenuItemCheckStyle.Checked; item.Checked = false; item.Action += () => { @@ -441,7 +445,21 @@ namespace UICatalog { } }; - return new MenuItem [] { item }; + return item; + } + + private MenuItem CreateAllowsTabChecked () + { + var item = new MenuItem { + Title = "Allows Tab" + }; + item.CheckType |= MenuItemCheckStyle.Checked; + item.Checked = true; + item.Action += () => { + _textView.AllowsTab = item.Checked = !item.Checked; + }; + + return item; } private void CreateFindReplace (bool isFind = true) diff --git a/UnitTests/TextViewTests.cs b/UnitTests/TextViewTests.cs index 5bbc14e2e..2aa6b97f6 100644 --- a/UnitTests/TextViewTests.cs +++ b/UnitTests/TextViewTests.cs @@ -1418,7 +1418,7 @@ namespace Terminal.Gui.Views { [Fact] [InitShutdown] - public void TabWidth_Setting_To_Zero_Changes_AllowsTab_To_False_If_True () + public void TabWidth_Setting_To_Zero_Keeps_AllowsTab () { Assert.Equal (4, _textView.TabWidth); Assert.True (_textView.AllowsTab); @@ -1426,11 +1426,11 @@ namespace Terminal.Gui.Views { Assert.True (_textView.Multiline); _textView.TabWidth = -1; Assert.Equal (0, _textView.TabWidth); - Assert.False (_textView.AllowsTab); - Assert.False (_textView.AllowsReturn); - Assert.False (_textView.Multiline); + Assert.True (_textView.AllowsTab); + Assert.True (_textView.AllowsReturn); + Assert.True (_textView.Multiline); _textView.ProcessKey (new KeyEvent (Key.Tab, new KeyModifiers ())); - Assert.Equal ("TAB to jump between text fields.", _textView.Text); + Assert.Equal ("\tTAB to jump between text fields.", _textView.Text); _textView.ProcessKey (new KeyEvent (Key.BackTab, new KeyModifiers ())); Assert.Equal ("TAB to jump between text fields.", _textView.Text); } @@ -1441,9 +1441,9 @@ namespace Terminal.Gui.Views { { _textView.TabWidth = 0; Assert.Equal (0, _textView.TabWidth); - Assert.False (_textView.AllowsTab); - Assert.False (_textView.AllowsReturn); - Assert.False (_textView.Multiline); + Assert.True (_textView.AllowsTab); + Assert.True (_textView.AllowsReturn); + Assert.True (_textView.Multiline); _textView.AllowsTab = true; Assert.True (_textView.AllowsTab); Assert.Equal (4, _textView.TabWidth); @@ -1761,6 +1761,20 @@ namespace Terminal.Gui.Views { Application.Run (); } + [Fact] + public void TextView_MultiLine_But_Without_Tabs () + { + var view = new TextView (); + + // the default for TextView + Assert.True (view.Multiline); + + view.AllowsTab = false; + Assert.False (view.AllowsTab); + + Assert.True (view.Multiline); + } + private int GetLeftCol (int start) { var lines = _textView.Text.Split (Environment.NewLine);