Skip to content

Commit

Permalink
Merge pull request #3416 from tig/v2_2432-DimAuto
Browse files Browse the repository at this point in the history
Partially Fixes #2432 - `Dim.Auto` automatically sizes views based on `Text` - Replaces `View.AutoSize`
  • Loading branch information
tig authored May 7, 2024
2 parents 37f59d2 + d305445 commit 4105680
Show file tree
Hide file tree
Showing 141 changed files with 12,082 additions and 8,151 deletions.
5 changes: 5 additions & 0 deletions Directory.Build.targets
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<Project>
<PropertyGroup>
<DefineConstants>$(DefineConstants);DIMAUTO</DefineConstants>
</PropertyGroup>
</Project>
2 changes: 1 addition & 1 deletion ReactiveExample/LoginView.cs
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ private Label LoginProgressLabel (View previous)

var loginProgressLabel = new Label
{
AutoSize = false, X = Pos.Left (previous), Y = Pos.Top (previous) + 1, Width = 40, Height = 1, Text = idle
X = Pos.Left (previous), Y = Pos.Top (previous) + 1, Width = 40, Height = 1, Text = idle
};

ViewModel
Expand Down
47 changes: 16 additions & 31 deletions Terminal.Gui/Application.cs
Original file line number Diff line number Diff line change
Expand Up @@ -557,8 +557,6 @@ public static RunState Begin (Toplevel toplevel)
return rs;
}

private static CursorVisibility _cachedCursorVisibility;

/// <summary>
/// Calls <see cref="View.PositionCursor"/> on the most focused view in the view starting with <paramref name="view"/>.
/// </summary>
Expand All @@ -571,29 +569,21 @@ public static RunState Begin (Toplevel toplevel)
/// <returns><see langword="true"/> if a view positioned the cursor and the position is visible.</returns>
internal static bool PositionCursor (View view)
{
if (view is null)
{
return false;
}

// Find the most focused view and position the cursor there.
View mostFocused = view.MostFocused;
View mostFocused = view?.MostFocused;

if (mostFocused is null)
{
return false;
}

CursorVisibility cachedCursorVisibility;

// If the view is not visible or enabled, don't position the cursor
if (!mostFocused.Visible || !mostFocused.Enabled)
{
Driver.GetCursorVisibility (out cachedCursorVisibility);

if (cachedCursorVisibility != CursorVisibility.Invisible)
Driver.GetCursorVisibility (out CursorVisibility current);
if (current != CursorVisibility.Invisible)
{
_cachedCursorVisibility = cachedCursorVisibility;
Driver.SetCursorVisibility (CursorVisibility.Invisible);
}

Expand All @@ -611,40 +601,35 @@ internal static bool PositionCursor (View view)
Point? prevCursor = new (Driver.Row, Driver.Col);
Point? cursor = mostFocused.PositionCursor ();

// If the cursor is not in a visible location in the SuperView, hide it
Driver.GetCursorVisibility (out CursorVisibility currentCursorVisibility);

if (cursor is { })
{
// Convert cursor to screen coords
cursor = mostFocused.ViewportToScreen (mostFocused.Viewport with { Location = cursor.Value }).Location;

// If the cursor is not in a visible location in the SuperView, hide it
if (!superViewViewport.Contains (cursor.Value))
{
Driver.GetCursorVisibility (out cachedCursorVisibility);

if (cachedCursorVisibility != CursorVisibility.Invisible)
if (currentCursorVisibility != CursorVisibility.Invisible)
{
_cachedCursorVisibility = cachedCursorVisibility;
Driver.SetCursorVisibility (CursorVisibility.Invisible);
}

Driver.SetCursorVisibility (CursorVisibility.Invisible);

return false;
}

Driver.GetCursorVisibility (out cachedCursorVisibility);

if (cachedCursorVisibility == CursorVisibility.Invisible)
// Show it
if (currentCursorVisibility == CursorVisibility.Invisible)
{
Driver.SetCursorVisibility (_cachedCursorVisibility);
Driver.SetCursorVisibility (mostFocused.CursorVisibility);
}

return prevCursor != cursor;
return true;
}

Driver.GetCursorVisibility (out cachedCursorVisibility);

if (cachedCursorVisibility != CursorVisibility.Invisible)
if (currentCursorVisibility != CursorVisibility.Invisible)
{
_cachedCursorVisibility = cachedCursorVisibility;
Driver.SetCursorVisibility (CursorVisibility.Invisible);
}

Expand Down Expand Up @@ -1413,14 +1398,14 @@ public static bool OnSizeChanging (SizeChangedEventArgs args)
{
SizeChanging?.Invoke (null, args);

if (args.Cancel)
if (args.Cancel || args.Size is null)
{
return false;
}

foreach (Toplevel t in _topLevels)
{
t.SetRelativeLayout (args.Size);
t.SetRelativeLayout (args.Size.Value);
t.LayoutSubviews ();
t.PositionToplevels ();
t.OnSizeChanging (new (args.Size));
Expand Down
10 changes: 10 additions & 0 deletions Terminal.Gui/ConsoleDrivers/CursesDriver/CursesDriver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -214,9 +214,13 @@ public override void UpdateCursor ()
if (!RunningUnitTests && Col >= 0 && Col < Cols && Row >= 0 && Row < Rows)
{
Curses.move (Row, Col);
Curses.raw ();
Curses.noecho ();
Curses.refresh ();
}
}


public override void UpdateScreen ()
{
for (var row = 0; row < Rows; row++)
Expand Down Expand Up @@ -606,6 +610,12 @@ internal void ProcessInput ()
k = KeyCode.Enter;
}

// Strip the KeyCode.Space flag off if it's set
if (k != KeyCode.Space && k.HasFlag (KeyCode.Space))
{
k &= ~KeyCode.Space;
}

OnKeyDown (new Key (k));
OnKeyUp (new Key (k));
}
Expand Down
9 changes: 3 additions & 6 deletions Terminal.Gui/ConsoleDrivers/NetDriver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1137,7 +1137,7 @@ private void ProcessInput (InputResult inputEvent)
break;
case EventType.Mouse:
MouseEvent me = ToDriverMouse (inputEvent.MouseEvent);
Debug.WriteLine ($"NetDriver: ({me.X},{me.Y}) - {me.Flags}");
//Debug.WriteLine ($"NetDriver: ({me.X},{me.Y}) - {me.Flags}");
OnMouseEvent (me);

break;
Expand Down Expand Up @@ -1591,10 +1591,7 @@ private KeyCode MapKey (ConsoleKeyInfo keyInfo)
return KeyCode.Tab;
}

if (keyInfo.Key == ConsoleKey.Tab)
{
return MapToKeyCodeModifiers (keyInfo.Modifiers, (KeyCode)((uint)keyInfo.Key));
}
return MapToKeyCodeModifiers (keyInfo.Modifiers, (KeyCode)((uint)keyInfo.Key));
}

// Handle control keys (e.g. CursorUp)
Expand Down Expand Up @@ -1648,7 +1645,7 @@ private KeyCode MapKey (ConsoleKeyInfo keyInfo)
}


return MapToKeyCodeModifiers (keyInfo.Modifiers, (KeyCode)((uint)keyInfo.Key));
return MapToKeyCodeModifiers (keyInfo.Modifiers, (KeyCode)((uint)keyInfo.KeyChar));
}

#endregion Keyboard Handling
Expand Down
32 changes: 21 additions & 11 deletions Terminal.Gui/ConsoleDrivers/WindowsDriver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
using System.Runtime.InteropServices;
using System.Text;
using static Terminal.Gui.ConsoleDrivers.ConsoleKeyMapping;
using static Terminal.Gui.SpinnerStyle;

namespace Terminal.Gui;

Expand Down Expand Up @@ -156,10 +157,7 @@ public void ReadFromConsoleOutput (Size size, Coord coords, ref SmallRect window
}
}

if (!_initialCursorVisibility.HasValue && GetCursorVisibility (out CursorVisibility visibility))
{
_initialCursorVisibility = visibility;
}
SetInitialCursorVisibility();

if (!SetConsoleActiveScreenBuffer (_screenBuffer))
{
Expand Down Expand Up @@ -216,11 +214,11 @@ public bool GetCursorVisibility (out CursorVisibility visibility)
}
else if (info.dwSize > 50)
{
visibility = CursorVisibility.Box;
visibility = CursorVisibility.Default;
}
else
{
visibility = CursorVisibility.Underline;
visibility = CursorVisibility.Default;
}

return true;
Expand Down Expand Up @@ -815,6 +813,11 @@ object lpReserved
[StructLayout (LayoutKind.Sequential)]
public struct ConsoleCursorInfo
{
/// <summary>
/// The percentage of the character cell that is filled by the cursor.This value is between 1 and 100.
/// The cursor appearance varies, ranging from completely filling the cell to showing up as a horizontal
/// line at the bottom of the cell.
/// </summary>
public uint dwSize;
public bool bVisible;
}
Expand Down Expand Up @@ -1436,6 +1439,8 @@ internal override MainLoop Init ()
#if HACK_CHECK_WINCHANGED
_mainLoopDriver.WinChanged = ChangeWin;
#endif

WinConsole?.SetInitialCursorVisibility ();
return new MainLoop (_mainLoopDriver);
}

Expand Down Expand Up @@ -1517,23 +1522,28 @@ internal void ProcessInput (WindowsConsole.InputRecord inputEvent)
#if HACK_CHECK_WINCHANGED
private void ChangeWin (object s, SizeChangedEventArgs e)
{
int w = e.Size.Width;
if (e.Size is null)
{
return;
}

int w = e.Size.Value.Width;

if (w == Cols - 3 && e.Size.Height < Rows)
if (w == Cols - 3 && e.Size.Value.Height < Rows)
{
w += 3;
}

Left = 0;
Top = 0;
Cols = e.Size.Width;
Rows = e.Size.Height;
Cols = e.Size.Value.Width;
Rows = e.Size.Value.Height;

if (!RunningUnitTests)
{
Size newSize = WinConsole.SetConsoleWindow (
(short)Math.Max (w, 16),
(short)Math.Max (e.Size.Height, 0));
(short)Math.Max (e.Size.Value.Height, 0));

Cols = newSize.Width;
Rows = newSize.Height;
Expand Down
1 change: 0 additions & 1 deletion Terminal.Gui/Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,4 @@
-->
<Authors>Miguel de Icaza, Charlie Kindel (@tig), @BDisp</Authors>
</PropertyGroup>

</Project>
Loading

0 comments on commit 4105680

Please sign in to comment.