Skip to content

Commit d2ad112

Browse files
authored
Fixes #3127 - View Layout improvements - Redefines how LayoutStyle works. Fixes AutoSize etc... (#3130)
* Removes CheckAbsoulte and updates unit tests to match * Fixed code that was dependent on ToString behavior vs. direct test for null * Dim/Pos != null WIP * Moved AutoSize specific tests out of Pos/Dim tests * Broke out AutoSize = false tests to new file * Commented test TODOs * New test * Removed unused API and cleaned up code * Removed unused API and cleaned up code * Cleaned up code * Cleaned up code * reorg'd Toplevel tests * Fixed Create and related unit tests * Added test from #3136 * Removed TopLevel.Create * Fixed SetCurrentOverlappedAsTop * Updated pull request template * Updated pull request template * Revert "Updated pull request template" This reverts commit d807190. * reverting * re-reverting * Fixed every thing but autosize scenarios?? * Fixed hexview * Fixed contextmenu * Fixed more minor issues in tests * Fixed more minor issues in tests * Debugging Dialog test failure * Fixed bad Dialog test. Was cleary invalid * Fixed OnResizeNeeded bug * Fixed OnResizeNeeded bug * Fixed UICatalog to not eat exceptions * Fixed TextView * Removed Frame overrides * Made Frame non-virtual * Fixed radioGroup * Fixed TabView * Hcked ScrolLBarView unit tests to pass * All AutoSize tests pass! * All tests pass!!!!!!! * Updated API docs. Cleaned up code. * Fixed ColorPicker * Added 'Bounds =' unit tests * Refactored TextFormatter.Size setting logic * Cleaned up OnResizeNeeded (api docs and usages) * Merges in #3019 changes. Makes OnResizeNeeded non-virtual. If we find a use-case where someone wants to override it we can change this back. * Fixed FileDialog bounds warning * Removed resharper settings from editorconfig * Added Pos.Center test to AllViewsTests.cs. Modernized RadioGroup. Fixed ProgressBar. * Reverted formatting * Reverted formatting * Reverted formatting * Reverted formatting * Reverted formatting * Reverted formatting * Reverted formatting * Code cleanup * Code cleanup * Code cleanup * Code cleanup * Code cleanup * Code cleanup * Code cleanup * Code cleanup * Reverted formatting
1 parent 4cc6339 commit d2ad112

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

78 files changed

+21373
-20399
lines changed

Terminal.Gui/Application.cs

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ static List<CultureInfo> GetSupportedCultures ()
109109
/// </para>
110110
/// <param name="driver">The <see cref="ConsoleDriver"/> to use. If neither <paramref name="driver"/> or <paramref name="driverName"/> are specified the default driver for the platform will be used.</param>
111111
/// <param name="driverName">The short name (e.g. "net", "windows", "ansi", "fake", or "curses") of the <see cref="ConsoleDriver"/> to use. If neither <paramref name="driver"/> or <paramref name="driverName"/> are specified the default driver for the platform will be used.</param>
112-
public static void Init (ConsoleDriver driver = null, string driverName = null) => InternalInit (Toplevel.Create, driver, driverName);
112+
public static void Init (ConsoleDriver driver = null, string driverName = null) => InternalInit (() => new Toplevel (), driver, driverName);
113113

114114
internal static bool _initialized = false;
115115
internal static int _mainThreadId = -1;
@@ -194,6 +194,10 @@ internal static void InternalInit (Func<Toplevel> topLevelFactory, ConsoleDriver
194194

195195
Top = topLevelFactory ();
196196
Current = Top;
197+
198+
// Ensure Top's layout is up to date.
199+
Current.SetRelativeLayout (Driver.Bounds);
200+
197201
_cachedSupportedCultures = GetSupportedCultures ();
198202
_mainThreadId = Thread.CurrentThread.ManagedThreadId;
199203
_initialized = true;
@@ -397,9 +401,9 @@ public static RunState Begin (Toplevel Toplevel)
397401
MoveCurrent (Current);
398402
}
399403

400-
if (Toplevel.LayoutStyle == LayoutStyle.Computed) {
401-
Toplevel.SetRelativeLayout (new Rect (0, 0, Driver.Cols, Driver.Rows));
402-
}
404+
//if (Toplevel.LayoutStyle == LayoutStyle.Computed) {
405+
Toplevel.SetRelativeLayout (Driver.Bounds);
406+
//}
403407
Toplevel.LayoutSubviews ();
404408
Toplevel.PositionToplevels ();
405409
Toplevel.FocusFirst ();
@@ -443,7 +447,7 @@ public static RunState Begin (Toplevel Toplevel)
443447
/// platform will be used (<see cref="WindowsDriver"/>, <see cref="CursesDriver"/>, or <see cref="NetDriver"/>).
444448
/// Must be <see langword="null"/> if <see cref="Init"/> has already been called.
445449
/// </param>
446-
public static void Run<T> (Func<Exception, bool> errorHandler = null, ConsoleDriver driver = null) where T : Toplevel, new ()
450+
public static void Run<T> (Func<Exception, bool> errorHandler = null, ConsoleDriver driver = null) where T : Toplevel, new()
447451
{
448452
if (_initialized) {
449453
if (Driver != null) {
@@ -710,7 +714,7 @@ public static void RunIteration (ref RunState state, ref bool firstIteration)
710714
&& (Driver.Cols != state.Toplevel.Frame.Width || Driver.Rows != state.Toplevel.Frame.Height)
711715
&& (state.Toplevel.NeedsDisplay || state.Toplevel.SubViewNeedsDisplay || state.Toplevel.LayoutNeeded)) {
712716

713-
state.Toplevel.Clear (new Rect (Point.Empty, new Size (Driver.Cols, Driver.Rows)));
717+
state.Toplevel.Clear (Driver.Bounds);
714718
}
715719

716720
if (state.Toplevel.NeedsDisplay ||
@@ -1334,7 +1338,8 @@ bool FrameHandledMouseEvent (Frame frame)
13341338
/// <summary>
13351339
/// Alternative key to navigate forwards through views. Ctrl+Tab is the primary key.
13361340
/// </summary>
1337-
[SerializableConfigurationProperty (Scope = typeof (SettingsScope))] [JsonConverter (typeof (KeyJsonConverter))]
1341+
[SerializableConfigurationProperty (Scope = typeof (SettingsScope))]
1342+
[JsonConverter (typeof (KeyJsonConverter))]
13381343
public static Key AlternateForwardKey {
13391344
get => _alternateForwardKey;
13401345
set {
@@ -1358,7 +1363,8 @@ static void OnAlternateForwardKeyChanged (KeyChangedEventArgs e)
13581363
/// <summary>
13591364
/// Alternative key to navigate backwards through views. Shift+Ctrl+Tab is the primary key.
13601365
/// </summary>
1361-
[SerializableConfigurationProperty (Scope = typeof (SettingsScope))] [JsonConverter (typeof (KeyJsonConverter))]
1366+
[SerializableConfigurationProperty (Scope = typeof (SettingsScope))]
1367+
[JsonConverter (typeof (KeyJsonConverter))]
13621368
public static Key AlternateBackwardKey {
13631369
get => _alternateBackwardKey;
13641370
set {
@@ -1382,7 +1388,8 @@ static void OnAlternateBackwardKeyChanged (KeyChangedEventArgs oldKey)
13821388
/// <summary>
13831389
/// Gets or sets the key to quit the application.
13841390
/// </summary>
1385-
[SerializableConfigurationProperty (Scope = typeof (SettingsScope))] [JsonConverter (typeof (KeyJsonConverter))]
1391+
[SerializableConfigurationProperty (Scope = typeof (SettingsScope))]
1392+
[JsonConverter (typeof (KeyJsonConverter))]
13861393
public static Key QuitKey {
13871394
get => _quitKey;
13881395
set {

Terminal.Gui/ConsoleDrivers/ConsoleDriver.cs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ public virtual int Cols {
6262
get => _cols;
6363
internal set {
6464
_cols = value;
65-
ClearContents();
65+
ClearContents ();
6666
}
6767
}
6868

@@ -73,7 +73,7 @@ public virtual int Rows {
7373
get => _rows;
7474
internal set {
7575
_rows = value;
76-
ClearContents();
76+
ClearContents ();
7777
}
7878
}
7979

@@ -552,6 +552,11 @@ public enum DiagnosticFlags : uint {
552552
/// </summary>
553553
public static DiagnosticFlags Diagnostics { get; set; }
554554

555+
/// <summary>
556+
/// Gets the dimensions of the terminal.
557+
/// </summary>
558+
public Rect Bounds => new Rect (0, 0, Cols, Rows);
559+
555560
/// <summary>
556561
/// Suspends the application (e.g. on Linux via SIGTSTP) and upon resume, resets the console driver.
557562
/// </summary>

Terminal.Gui/Text/Autocomplete/AppendAutocomplete.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ public override bool ProcessKey (Key a)
8383
public override void GenerateSuggestions (AutocompleteContext context)
8484
{
8585
if (_suspendSuggestions) {
86+
_suspendSuggestions = false;
8687
return;
8788
}
8889
base.GenerateSuggestions (context);

Terminal.Gui/Text/Autocomplete/PopupAutocomplete.cs

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -23,17 +23,6 @@ public Popup (PopupAutocomplete autocomplete)
2323
WantMousePositionReports = true;
2424
}
2525

26-
public override Rect Frame {
27-
get => base.Frame;
28-
set {
29-
base.Frame = value;
30-
X = value.X;
31-
Y = value.Y;
32-
Width = value.Width;
33-
Height = value.Height;
34-
}
35-
}
36-
3726
public override void OnDrawContent (Rect contentArea)
3827
{
3928
if (autocomplete.LastPopupPos == null) {

Terminal.Gui/Text/TextFormatter.cs

Lines changed: 74 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -50,23 +50,50 @@ public enum VerticalTextAlignment {
5050
Justified
5151
}
5252

53-
/// TextDirection [H] = Horizontal [V] = Vertical
54-
/// =============
55-
/// LeftRight_TopBottom [H] Normal
56-
/// TopBottom_LeftRight [V] Normal
57-
///
58-
/// RightLeft_TopBottom [H] Invert Text
59-
/// TopBottom_RightLeft [V] Invert Lines
60-
///
61-
/// LeftRight_BottomTop [H] Invert Lines
62-
/// BottomTop_LeftRight [V] Invert Text
63-
///
64-
/// RightLeft_BottomTop [H] Invert Text + Invert Lines
65-
/// BottomTop_RightLeft [V] Invert Text + Invert Lines
66-
///
6753
/// <summary>
6854
/// Text direction enumeration, controls how text is displayed.
6955
/// </summary>
56+
/// <remarks>
57+
/// <para>TextDirection [H] = Horizontal [V] = Vertical</para>
58+
/// <table>
59+
/// <tr>
60+
/// <th>TextDirection</th>
61+
/// <th>Description</th>
62+
/// </tr>
63+
/// <tr>
64+
/// <td>LeftRight_TopBottom [H]</td>
65+
/// <td>Normal</td>
66+
/// </tr>
67+
/// <tr>
68+
/// <td>TopBottom_LeftRight [V]</td>
69+
/// <td>Normal</td>
70+
/// </tr>
71+
/// <tr>
72+
/// <td>RightLeft_TopBottom [H]</td>
73+
/// <td>Invert Text</td>
74+
/// </tr>
75+
/// <tr>
76+
/// <td>TopBottom_RightLeft [V]</td>
77+
/// <td>Invert Lines</td>
78+
/// </tr>
79+
/// <tr>
80+
/// <td>LeftRight_BottomTop [H]</td>
81+
/// <td>Invert Lines</td>
82+
/// </tr>
83+
/// <tr>
84+
/// <td>BottomTop_LeftRight [V]</td>
85+
/// <td>Invert Text</td>
86+
/// </tr>
87+
/// <tr>
88+
/// <td>RightLeft_BottomTop [H]</td>
89+
/// <td>Invert Text + Invert Lines</td>
90+
/// </tr>
91+
/// <tr>
92+
/// <td>BottomTop_RightLeft [V]</td>
93+
/// <td>Invert Text + Invert Lines</td>
94+
/// </tr>
95+
/// </table>
96+
/// </remarks>
7097
public enum TextDirection {
7198
/// <summary>
7299
/// Normal horizontal direction.
@@ -1075,7 +1102,7 @@ public virtual string Text {
10751102
_text = EnableNeedsFormat (value);
10761103

10771104
if ((AutoSize && Alignment != TextAlignment.Justified && VerticalAlignment != VerticalTextAlignment.Justified) || (textWasNull && Size.IsEmpty)) {
1078-
Size = CalcRect (0, 0, _text, _textDirection, TabWidth).Size;
1105+
Size = CalcRect (0, 0, _text, Direction, TabWidth).Size;
10791106
}
10801107

10811108
//if (_text != null && _text.GetRuneCount () > 0 && (Size.Width == 0 || Size.Height == 0 || Size.Width != _text.GetColumns ())) {
@@ -1087,20 +1114,22 @@ public virtual string Text {
10871114
}
10881115

10891116
/// <summary>
1090-
/// Used by <see cref="Text"/> to resize the view's <see cref="View.Bounds"/> with the <see cref="Size"/>.
1091-
/// Setting <see cref="AutoSize"/> to true only work if the <see cref="View.Width"/> and <see cref="View.Height"/> are null or
1092-
/// <see cref="LayoutStyle.Absolute"/> values and doesn't work with <see cref="LayoutStyle.Computed"/> layout,
1093-
/// to avoid breaking the <see cref="Pos"/> and <see cref="Dim"/> settings.
1117+
/// Gets or sets whether the <see cref="Size"/> should be automatically changed to fit the <see cref="Text"/>.
10941118
/// </summary>
10951119
/// <remarks>
1096-
/// Auto size is ignored if the <see cref="TextAlignment.Justified"/> and <see cref="VerticalTextAlignment.Justified"/> are used.
1120+
/// <para>
1121+
/// Used by <see cref="View.AutoSize"/> to resize the view's <see cref="View.Bounds"/> to fit <see cref="Size"/>.
1122+
/// </para>
1123+
/// <para>
1124+
/// AutoSize is ignored if <see cref="TextAlignment.Justified"/> and <see cref="VerticalTextAlignment.Justified"/> are used.
1125+
/// </para>
10971126
/// </remarks>
10981127
public bool AutoSize {
10991128
get => _autoSize;
11001129
set {
11011130
_autoSize = EnableNeedsFormat (value);
11021131
if (_autoSize && Alignment != TextAlignment.Justified && VerticalAlignment != VerticalTextAlignment.Justified) {
1103-
Size = CalcRect (0, 0, Text, _textDirection, TabWidth).Size;
1132+
Size = CalcRect (0, 0, _text, Direction, TabWidth).Size;
11041133
}
11051134
}
11061135
}
@@ -1140,7 +1169,12 @@ public VerticalTextAlignment VerticalAlignment {
11401169
/// <value>The text vertical alignment.</value>
11411170
public TextDirection Direction {
11421171
get => _textDirection;
1143-
set => _textDirection = EnableNeedsFormat (value);
1172+
set {
1173+
_textDirection = EnableNeedsFormat (value);
1174+
if (AutoSize && Alignment != TextAlignment.Justified && VerticalAlignment != VerticalTextAlignment.Justified) {
1175+
Size = CalcRect (0, 0, Text, Direction, TabWidth).Size;
1176+
}
1177+
}
11441178
}
11451179

11461180
/// <summary>
@@ -1204,24 +1238,24 @@ public static bool IsTopToBottom (TextDirection textDirection)
12041238
}
12051239

12061240
/// <summary>
1207-
/// Allows word wrap the to fit the available container width.
1241+
/// Gets or sets whether word wrap will be used to fit <see cref="Text"/> to <see cref="Size"/>.
12081242
/// </summary>
12091243
public bool WordWrap {
12101244
get => _wordWrap;
12111245
set => _wordWrap = EnableNeedsFormat (value);
12121246
}
12131247

12141248
/// <summary>
1215-
/// Gets or sets the size of the area the text will be constrained to when formatted.
1249+
/// Gets or sets the size <see cref="Text"/> will be constrained to when formatted.
12161250
/// </summary>
12171251
/// <remarks>
1218-
/// Does not return the size the formatted text; just the value that was set.
1252+
/// Does not return the size of the formatted text but the size that will be used to constrain the text when formatted.
12191253
/// </remarks>
12201254
public Size Size {
12211255
get => _size;
12221256
set {
12231257
if (AutoSize && Alignment != TextAlignment.Justified && VerticalAlignment != VerticalTextAlignment.Justified) {
1224-
_size = EnableNeedsFormat (CalcRect (0, 0, Text, _textDirection, TabWidth).Size);
1258+
_size = EnableNeedsFormat (CalcRect (0, 0, Text, Direction, TabWidth).Size);
12251259
} else {
12261260
_size = EnableNeedsFormat (value);
12271261
}
@@ -1291,7 +1325,7 @@ public List<string> Lines {
12911325
NeedsFormat = false;
12921326
return _lines;
12931327
}
1294-
1328+
12951329
if (NeedsFormat) {
12961330
var shown_text = _text;
12971331
if (FindHotKey (_text, HotKeySpecifier, true, out _hotKeyPos, out var newHotKey)) {
@@ -1300,7 +1334,7 @@ public List<string> Lines {
13001334
shown_text = ReplaceHotKeyWithTag (shown_text, _hotKeyPos);
13011335
}
13021336

1303-
if (IsVerticalDirection (_textDirection)) {
1337+
if (IsVerticalDirection (Direction)) {
13041338
var colsWidth = GetSumMaxCharWidth (shown_text, 0, 1, TabWidth);
13051339
_lines = Format (shown_text, Size.Height, VerticalAlignment == VerticalTextAlignment.Justified, Size.Width > colsWidth && WordWrap,
13061340
PreserveTrailingSpaces, TabWidth, Direction, MultiLine);
@@ -1325,11 +1359,16 @@ public List<string> Lines {
13251359
}
13261360

13271361
/// <summary>
1328-
/// Gets or sets whether the <see cref="TextFormatter"/> needs to format the text when <see cref="Draw(Rect, Attribute, Attribute, Rect, bool, ConsoleDriver)"/> is called.
1329-
/// If it is <c>false</c> when Draw is called, the Draw call will be faster.
1362+
/// Gets or sets whether the <see cref="TextFormatter"/> needs to format the text.
13301363
/// </summary>
13311364
/// <remarks>
13321365
/// <para>
1366+
/// If <c>false</c> when Draw is called, the Draw call will be faster.
1367+
/// </para>
1368+
/// <para>
1369+
/// Used by <see cref="Draw(Rect, Attribute, Attribute, Rect, bool, ConsoleDriver)"/>
1370+
/// </para>
1371+
/// <para>
13331372
/// This is set to true when the properties of <see cref="TextFormatter"/> are set.
13341373
/// </para>
13351374
/// </remarks>
@@ -1400,7 +1439,7 @@ public void Draw (Rect bounds, Attribute normalColor, Attribute hotColor, Rect c
14001439
// Use "Lines" to ensure a Format (don't use "lines"))
14011440

14021441
var linesFormated = Lines;
1403-
switch (_textDirection) {
1442+
switch (Direction) {
14041443
case TextDirection.TopBottom_RightLeft:
14051444
case TextDirection.LeftRight_BottomTop:
14061445
case TextDirection.RightLeft_BottomTop:
@@ -1409,7 +1448,7 @@ public void Draw (Rect bounds, Attribute normalColor, Attribute hotColor, Rect c
14091448
break;
14101449
}
14111450

1412-
var isVertical = IsVerticalDirection (_textDirection);
1451+
var isVertical = IsVerticalDirection (Direction);
14131452
var maxBounds = bounds;
14141453
if (driver != null) {
14151454
maxBounds = containerBounds == default
@@ -1441,7 +1480,7 @@ public void Draw (Rect bounds, Attribute normalColor, Attribute hotColor, Rect c
14411480

14421481
var runes = _lines [line].ToRunes ();
14431482

1444-
switch (_textDirection) {
1483+
switch (Direction) {
14451484
case TextDirection.RightLeft_BottomTop:
14461485
case TextDirection.RightLeft_TopBottom:
14471486
case TextDirection.BottomTop_LeftRight:
@@ -1454,7 +1493,7 @@ public void Draw (Rect bounds, Attribute normalColor, Attribute hotColor, Rect c
14541493

14551494
int x, y;
14561495
// Horizontal Alignment
1457-
if (_textAlignment == TextAlignment.Right || (_textAlignment == TextAlignment.Justified && !IsLeftToRight (_textDirection))) {
1496+
if (_textAlignment == TextAlignment.Right || (_textAlignment == TextAlignment.Justified && !IsLeftToRight (Direction))) {
14581497
if (isVertical) {
14591498
var runesWidth = GetSumMaxCharWidth (Lines, line, TabWidth);
14601499
x = bounds.Right - runesWidth;
@@ -1487,7 +1526,7 @@ public void Draw (Rect bounds, Attribute normalColor, Attribute hotColor, Rect c
14871526
}
14881527

14891528
// Vertical Alignment
1490-
if (_textVerticalAlignment == VerticalTextAlignment.Bottom || (_textVerticalAlignment == VerticalTextAlignment.Justified && !IsTopToBottom (_textDirection))) {
1529+
if (_textVerticalAlignment == VerticalTextAlignment.Bottom || (_textVerticalAlignment == VerticalTextAlignment.Justified && !IsTopToBottom (Direction))) {
14911530
if (isVertical) {
14921531
y = bounds.Bottom - runes.Length;
14931532
} else {

Terminal.Gui/View/Frame.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ public override void BoundsToScreen (int col, int row, out int rcol, out int rro
5858
/// <inheritdoc/>
5959
public override Rect FrameToScreen ()
6060
{
61-
// Frames are *Children* of a View, not SubViews. Thus View.FramToScreen will not work.
61+
// Frames are *Children* of a View, not SubViews. Thus View.FrameToScreen will not work.
6262
// To get the screen-relative coordinates of a Frame, we need to know who
6363
// the Parent is
6464
var ret = Parent?.Frame ?? Frame;

0 commit comments

Comments
 (0)