Skip to content

Commit

Permalink
Fixes gui-cs#3127 - View Layout improvements - Redefines how `LayoutS…
Browse files Browse the repository at this point in the history
…tyle` works. Fixes `AutoSize` etc... (gui-cs#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 gui-cs#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 gui-cs#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
  • Loading branch information
tig authored Jan 13, 2024
1 parent 4cc6339 commit d2ad112
Show file tree
Hide file tree
Showing 78 changed files with 21,373 additions and 20,399 deletions.
25 changes: 16 additions & 9 deletions Terminal.Gui/Application.cs
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ static List<CultureInfo> GetSupportedCultures ()
/// </para>
/// <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>
/// <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>
public static void Init (ConsoleDriver driver = null, string driverName = null) => InternalInit (Toplevel.Create, driver, driverName);
public static void Init (ConsoleDriver driver = null, string driverName = null) => InternalInit (() => new Toplevel (), driver, driverName);

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

Top = topLevelFactory ();
Current = Top;

// Ensure Top's layout is up to date.
Current.SetRelativeLayout (Driver.Bounds);

_cachedSupportedCultures = GetSupportedCultures ();
_mainThreadId = Thread.CurrentThread.ManagedThreadId;
_initialized = true;
Expand Down Expand Up @@ -397,9 +401,9 @@ public static RunState Begin (Toplevel Toplevel)
MoveCurrent (Current);
}

if (Toplevel.LayoutStyle == LayoutStyle.Computed) {
Toplevel.SetRelativeLayout (new Rect (0, 0, Driver.Cols, Driver.Rows));
}
//if (Toplevel.LayoutStyle == LayoutStyle.Computed) {
Toplevel.SetRelativeLayout (Driver.Bounds);
//}
Toplevel.LayoutSubviews ();
Toplevel.PositionToplevels ();
Toplevel.FocusFirst ();
Expand Down Expand Up @@ -443,7 +447,7 @@ public static RunState Begin (Toplevel Toplevel)
/// platform will be used (<see cref="WindowsDriver"/>, <see cref="CursesDriver"/>, or <see cref="NetDriver"/>).
/// Must be <see langword="null"/> if <see cref="Init"/> has already been called.
/// </param>
public static void Run<T> (Func<Exception, bool> errorHandler = null, ConsoleDriver driver = null) where T : Toplevel, new ()
public static void Run<T> (Func<Exception, bool> errorHandler = null, ConsoleDriver driver = null) where T : Toplevel, new()
{
if (_initialized) {
if (Driver != null) {
Expand Down Expand Up @@ -710,7 +714,7 @@ public static void RunIteration (ref RunState state, ref bool firstIteration)
&& (Driver.Cols != state.Toplevel.Frame.Width || Driver.Rows != state.Toplevel.Frame.Height)
&& (state.Toplevel.NeedsDisplay || state.Toplevel.SubViewNeedsDisplay || state.Toplevel.LayoutNeeded)) {

state.Toplevel.Clear (new Rect (Point.Empty, new Size (Driver.Cols, Driver.Rows)));
state.Toplevel.Clear (Driver.Bounds);
}

if (state.Toplevel.NeedsDisplay ||
Expand Down Expand Up @@ -1334,7 +1338,8 @@ bool FrameHandledMouseEvent (Frame frame)
/// <summary>
/// Alternative key to navigate forwards through views. Ctrl+Tab is the primary key.
/// </summary>
[SerializableConfigurationProperty (Scope = typeof (SettingsScope))] [JsonConverter (typeof (KeyJsonConverter))]
[SerializableConfigurationProperty (Scope = typeof (SettingsScope))]
[JsonConverter (typeof (KeyJsonConverter))]
public static Key AlternateForwardKey {
get => _alternateForwardKey;
set {
Expand All @@ -1358,7 +1363,8 @@ static void OnAlternateForwardKeyChanged (KeyChangedEventArgs e)
/// <summary>
/// Alternative key to navigate backwards through views. Shift+Ctrl+Tab is the primary key.
/// </summary>
[SerializableConfigurationProperty (Scope = typeof (SettingsScope))] [JsonConverter (typeof (KeyJsonConverter))]
[SerializableConfigurationProperty (Scope = typeof (SettingsScope))]
[JsonConverter (typeof (KeyJsonConverter))]
public static Key AlternateBackwardKey {
get => _alternateBackwardKey;
set {
Expand All @@ -1382,7 +1388,8 @@ static void OnAlternateBackwardKeyChanged (KeyChangedEventArgs oldKey)
/// <summary>
/// Gets or sets the key to quit the application.
/// </summary>
[SerializableConfigurationProperty (Scope = typeof (SettingsScope))] [JsonConverter (typeof (KeyJsonConverter))]
[SerializableConfigurationProperty (Scope = typeof (SettingsScope))]
[JsonConverter (typeof (KeyJsonConverter))]
public static Key QuitKey {
get => _quitKey;
set {
Expand Down
9 changes: 7 additions & 2 deletions Terminal.Gui/ConsoleDrivers/ConsoleDriver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public virtual int Cols {
get => _cols;
internal set {
_cols = value;
ClearContents();
ClearContents ();
}
}

Expand All @@ -73,7 +73,7 @@ public virtual int Rows {
get => _rows;
internal set {
_rows = value;
ClearContents();
ClearContents ();
}
}

Expand Down Expand Up @@ -552,6 +552,11 @@ public enum DiagnosticFlags : uint {
/// </summary>
public static DiagnosticFlags Diagnostics { get; set; }

/// <summary>
/// Gets the dimensions of the terminal.
/// </summary>
public Rect Bounds => new Rect (0, 0, Cols, Rows);

/// <summary>
/// Suspends the application (e.g. on Linux via SIGTSTP) and upon resume, resets the console driver.
/// </summary>
Expand Down
1 change: 1 addition & 0 deletions Terminal.Gui/Text/Autocomplete/AppendAutocomplete.cs
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ public override bool ProcessKey (Key a)
public override void GenerateSuggestions (AutocompleteContext context)
{
if (_suspendSuggestions) {
_suspendSuggestions = false;
return;
}
base.GenerateSuggestions (context);
Expand Down
11 changes: 0 additions & 11 deletions Terminal.Gui/Text/Autocomplete/PopupAutocomplete.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,17 +23,6 @@ public Popup (PopupAutocomplete autocomplete)
WantMousePositionReports = true;
}

public override Rect Frame {
get => base.Frame;
set {
base.Frame = value;
X = value.X;
Y = value.Y;
Width = value.Width;
Height = value.Height;
}
}

public override void OnDrawContent (Rect contentArea)
{
if (autocomplete.LastPopupPos == null) {
Expand Down
109 changes: 74 additions & 35 deletions Terminal.Gui/Text/TextFormatter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,23 +50,50 @@ public enum VerticalTextAlignment {
Justified
}

/// TextDirection [H] = Horizontal [V] = Vertical
/// =============
/// LeftRight_TopBottom [H] Normal
/// TopBottom_LeftRight [V] Normal
///
/// RightLeft_TopBottom [H] Invert Text
/// TopBottom_RightLeft [V] Invert Lines
///
/// LeftRight_BottomTop [H] Invert Lines
/// BottomTop_LeftRight [V] Invert Text
///
/// RightLeft_BottomTop [H] Invert Text + Invert Lines
/// BottomTop_RightLeft [V] Invert Text + Invert Lines
///
/// <summary>
/// Text direction enumeration, controls how text is displayed.
/// </summary>
/// <remarks>
/// <para>TextDirection [H] = Horizontal [V] = Vertical</para>
/// <table>
/// <tr>
/// <th>TextDirection</th>
/// <th>Description</th>
/// </tr>
/// <tr>
/// <td>LeftRight_TopBottom [H]</td>
/// <td>Normal</td>
/// </tr>
/// <tr>
/// <td>TopBottom_LeftRight [V]</td>
/// <td>Normal</td>
/// </tr>
/// <tr>
/// <td>RightLeft_TopBottom [H]</td>
/// <td>Invert Text</td>
/// </tr>
/// <tr>
/// <td>TopBottom_RightLeft [V]</td>
/// <td>Invert Lines</td>
/// </tr>
/// <tr>
/// <td>LeftRight_BottomTop [H]</td>
/// <td>Invert Lines</td>
/// </tr>
/// <tr>
/// <td>BottomTop_LeftRight [V]</td>
/// <td>Invert Text</td>
/// </tr>
/// <tr>
/// <td>RightLeft_BottomTop [H]</td>
/// <td>Invert Text + Invert Lines</td>
/// </tr>
/// <tr>
/// <td>BottomTop_RightLeft [V]</td>
/// <td>Invert Text + Invert Lines</td>
/// </tr>
/// </table>
/// </remarks>
public enum TextDirection {
/// <summary>
/// Normal horizontal direction.
Expand Down Expand Up @@ -1075,7 +1102,7 @@ public virtual string Text {
_text = EnableNeedsFormat (value);

if ((AutoSize && Alignment != TextAlignment.Justified && VerticalAlignment != VerticalTextAlignment.Justified) || (textWasNull && Size.IsEmpty)) {
Size = CalcRect (0, 0, _text, _textDirection, TabWidth).Size;
Size = CalcRect (0, 0, _text, Direction, TabWidth).Size;
}

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

/// <summary>
/// Used by <see cref="Text"/> to resize the view's <see cref="View.Bounds"/> with the <see cref="Size"/>.
/// Setting <see cref="AutoSize"/> to true only work if the <see cref="View.Width"/> and <see cref="View.Height"/> are null or
/// <see cref="LayoutStyle.Absolute"/> values and doesn't work with <see cref="LayoutStyle.Computed"/> layout,
/// to avoid breaking the <see cref="Pos"/> and <see cref="Dim"/> settings.
/// Gets or sets whether the <see cref="Size"/> should be automatically changed to fit the <see cref="Text"/>.
/// </summary>
/// <remarks>
/// Auto size is ignored if the <see cref="TextAlignment.Justified"/> and <see cref="VerticalTextAlignment.Justified"/> are used.
/// <para>
/// Used by <see cref="View.AutoSize"/> to resize the view's <see cref="View.Bounds"/> to fit <see cref="Size"/>.
/// </para>
/// <para>
/// AutoSize is ignored if <see cref="TextAlignment.Justified"/> and <see cref="VerticalTextAlignment.Justified"/> are used.
/// </para>
/// </remarks>
public bool AutoSize {
get => _autoSize;
set {
_autoSize = EnableNeedsFormat (value);
if (_autoSize && Alignment != TextAlignment.Justified && VerticalAlignment != VerticalTextAlignment.Justified) {
Size = CalcRect (0, 0, Text, _textDirection, TabWidth).Size;
Size = CalcRect (0, 0, _text, Direction, TabWidth).Size;
}
}
}
Expand Down Expand Up @@ -1140,7 +1169,12 @@ public VerticalTextAlignment VerticalAlignment {
/// <value>The text vertical alignment.</value>
public TextDirection Direction {
get => _textDirection;
set => _textDirection = EnableNeedsFormat (value);
set {
_textDirection = EnableNeedsFormat (value);
if (AutoSize && Alignment != TextAlignment.Justified && VerticalAlignment != VerticalTextAlignment.Justified) {
Size = CalcRect (0, 0, Text, Direction, TabWidth).Size;
}
}
}

/// <summary>
Expand Down Expand Up @@ -1204,24 +1238,24 @@ public static bool IsTopToBottom (TextDirection textDirection)
}

/// <summary>
/// Allows word wrap the to fit the available container width.
/// Gets or sets whether word wrap will be used to fit <see cref="Text"/> to <see cref="Size"/>.
/// </summary>
public bool WordWrap {
get => _wordWrap;
set => _wordWrap = EnableNeedsFormat (value);
}

/// <summary>
/// Gets or sets the size of the area the text will be constrained to when formatted.
/// Gets or sets the size <see cref="Text"/> will be constrained to when formatted.
/// </summary>
/// <remarks>
/// Does not return the size the formatted text; just the value that was set.
/// Does not return the size of the formatted text but the size that will be used to constrain the text when formatted.
/// </remarks>
public Size Size {
get => _size;
set {
if (AutoSize && Alignment != TextAlignment.Justified && VerticalAlignment != VerticalTextAlignment.Justified) {
_size = EnableNeedsFormat (CalcRect (0, 0, Text, _textDirection, TabWidth).Size);
_size = EnableNeedsFormat (CalcRect (0, 0, Text, Direction, TabWidth).Size);
} else {
_size = EnableNeedsFormat (value);
}
Expand Down Expand Up @@ -1291,7 +1325,7 @@ public List<string> Lines {
NeedsFormat = false;
return _lines;
}

if (NeedsFormat) {
var shown_text = _text;
if (FindHotKey (_text, HotKeySpecifier, true, out _hotKeyPos, out var newHotKey)) {
Expand All @@ -1300,7 +1334,7 @@ public List<string> Lines {
shown_text = ReplaceHotKeyWithTag (shown_text, _hotKeyPos);
}

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

/// <summary>
/// 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.
/// If it is <c>false</c> when Draw is called, the Draw call will be faster.
/// Gets or sets whether the <see cref="TextFormatter"/> needs to format the text.
/// </summary>
/// <remarks>
/// <para>
/// If <c>false</c> when Draw is called, the Draw call will be faster.
/// </para>
/// <para>
/// Used by <see cref="Draw(Rect, Attribute, Attribute, Rect, bool, ConsoleDriver)"/>
/// </para>
/// <para>
/// This is set to true when the properties of <see cref="TextFormatter"/> are set.
/// </para>
/// </remarks>
Expand Down Expand Up @@ -1400,7 +1439,7 @@ public void Draw (Rect bounds, Attribute normalColor, Attribute hotColor, Rect c
// Use "Lines" to ensure a Format (don't use "lines"))

var linesFormated = Lines;
switch (_textDirection) {
switch (Direction) {
case TextDirection.TopBottom_RightLeft:
case TextDirection.LeftRight_BottomTop:
case TextDirection.RightLeft_BottomTop:
Expand All @@ -1409,7 +1448,7 @@ public void Draw (Rect bounds, Attribute normalColor, Attribute hotColor, Rect c
break;
}

var isVertical = IsVerticalDirection (_textDirection);
var isVertical = IsVerticalDirection (Direction);
var maxBounds = bounds;
if (driver != null) {
maxBounds = containerBounds == default
Expand Down Expand Up @@ -1441,7 +1480,7 @@ public void Draw (Rect bounds, Attribute normalColor, Attribute hotColor, Rect c

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

switch (_textDirection) {
switch (Direction) {
case TextDirection.RightLeft_BottomTop:
case TextDirection.RightLeft_TopBottom:
case TextDirection.BottomTop_LeftRight:
Expand All @@ -1454,7 +1493,7 @@ public void Draw (Rect bounds, Attribute normalColor, Attribute hotColor, Rect c

int x, y;
// Horizontal Alignment
if (_textAlignment == TextAlignment.Right || (_textAlignment == TextAlignment.Justified && !IsLeftToRight (_textDirection))) {
if (_textAlignment == TextAlignment.Right || (_textAlignment == TextAlignment.Justified && !IsLeftToRight (Direction))) {
if (isVertical) {
var runesWidth = GetSumMaxCharWidth (Lines, line, TabWidth);
x = bounds.Right - runesWidth;
Expand Down Expand Up @@ -1487,7 +1526,7 @@ public void Draw (Rect bounds, Attribute normalColor, Attribute hotColor, Rect c
}

// Vertical Alignment
if (_textVerticalAlignment == VerticalTextAlignment.Bottom || (_textVerticalAlignment == VerticalTextAlignment.Justified && !IsTopToBottom (_textDirection))) {
if (_textVerticalAlignment == VerticalTextAlignment.Bottom || (_textVerticalAlignment == VerticalTextAlignment.Justified && !IsTopToBottom (Direction))) {
if (isVertical) {
y = bounds.Bottom - runes.Length;
} else {
Expand Down
2 changes: 1 addition & 1 deletion Terminal.Gui/View/Frame.cs
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ public override void BoundsToScreen (int col, int row, out int rcol, out int rro
/// <inheritdoc/>
public override Rect FrameToScreen ()
{
// Frames are *Children* of a View, not SubViews. Thus View.FramToScreen will not work.
// Frames are *Children* of a View, not SubViews. Thus View.FrameToScreen will not work.
// To get the screen-relative coordinates of a Frame, we need to know who
// the Parent is
var ret = Parent?.Frame ?? Frame;
Expand Down
Loading

0 comments on commit d2ad112

Please sign in to comment.