Skip to content

Commit

Permalink
Merge branch 'v2_develop' into v2_3847_tabview-focus-fix
Browse files Browse the repository at this point in the history
  • Loading branch information
tig authored Nov 26, 2024
2 parents 8104ef3 + 92c546e commit 37bb43a
Show file tree
Hide file tree
Showing 70 changed files with 7,580 additions and 6,038 deletions.
1 change: 1 addition & 0 deletions CommunityToolkitExample/LoginView.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ public void Receive (Message<LoginActions> message)
}
}
SetText();
// BUGBUG: This should not be needed:
Application.LayoutAndDraw ();
}

Expand Down
4 changes: 2 additions & 2 deletions NativeAot/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ namespace NativeAot;

public static class Program
{
[RequiresUnreferencedCode ("Calls Terminal.Gui.Application.Init(ConsoleDriver, String)")]
[RequiresDynamicCode ("Calls Terminal.Gui.Application.Init(ConsoleDriver, String)")]
[RequiresUnreferencedCode ("Calls Terminal.Gui.Application.Init(IConsoleDriver, String)")]
[RequiresDynamicCode ("Calls Terminal.Gui.Application.Init(IConsoleDriver, String)")]
private static void Main (string [] args)
{
Application.Init ();
Expand Down
2 changes: 1 addition & 1 deletion SelfContained/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ namespace SelfContained;

public static class Program
{
[RequiresUnreferencedCode ("Calls Terminal.Gui.Application.Run<T>(Func<Exception, Boolean>, ConsoleDriver)")]
[RequiresUnreferencedCode ("Calls Terminal.Gui.Application.Run<T>(Func<Exception, Boolean>, IConsoleDriver)")]
private static void Main (string [] args)
{
Application.Init ();
Expand Down
8 changes: 4 additions & 4 deletions Terminal.Gui/Application/Application.Driver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@ public static partial class Application // Driver abstractions
{
internal static bool _forceFakeConsole;

/// <summary>Gets the <see cref="ConsoleDriver"/> that has been selected. See also <see cref="ForceDriver"/>.</summary>
public static ConsoleDriver? Driver { get; internal set; }
/// <summary>Gets the <see cref="IConsoleDriver"/> that has been selected. See also <see cref="ForceDriver"/>.</summary>
public static IConsoleDriver? Driver { get; internal set; }

// BUGBUG: Force16Colors should be nullable.
/// <summary>
/// Gets or sets whether <see cref="Application.Driver"/> will be forced to output only the 16 colors defined in
/// <see cref="ColorName16"/>. The default is <see langword="false"/>, meaning 24-bit (TrueColor) colors will be output
/// as long as the selected <see cref="ConsoleDriver"/> supports TrueColor.
/// as long as the selected <see cref="IConsoleDriver"/> supports TrueColor.
/// </summary>
[SerializableConfigurationProperty (Scope = typeof (SettingsScope))]
public static bool Force16Colors { get; set; }
Expand All @@ -23,7 +23,7 @@ public static partial class Application // Driver abstractions
/// specified, the driver is selected based on the platform.
/// </summary>
/// <remarks>
/// Note, <see cref="Application.Init(ConsoleDriver, string)"/> will override this configuration setting if called
/// Note, <see cref="Application.Init(IConsoleDriver, string)"/> will override this configuration setting if called
/// with either `driver` or `driverName` specified.
/// </remarks>
[SerializableConfigurationProperty (Scope = typeof (SettingsScope))]
Expand Down
20 changes: 10 additions & 10 deletions Terminal.Gui/Application/Application.Initialization.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ public static partial class Application // Initialization (Init/Shutdown)
/// <summary>Initializes a new instance of <see cref="Terminal.Gui"/> Application.</summary>
/// <para>Call this method once per instance (or after <see cref="Shutdown"/> has been called).</para>
/// <para>
/// This function loads the right <see cref="ConsoleDriver"/> for the platform, Creates a <see cref="Toplevel"/>. and
/// This function loads the right <see cref="IConsoleDriver"/> for the platform, Creates a <see cref="Toplevel"/>. and
/// assigns it to <see cref="Top"/>
/// </para>
/// <para>
Expand All @@ -21,23 +21,23 @@ public static partial class Application // Initialization (Init/Shutdown)
/// </para>
/// <para>
/// The <see cref="Run{T}"/> function combines
/// <see cref="Init(Terminal.Gui.ConsoleDriver,string)"/> and <see cref="Run(Toplevel, Func{Exception, bool})"/>
/// <see cref="Init(Terminal.Gui.IConsoleDriver,string)"/> and <see cref="Run(Toplevel, Func{Exception, bool})"/>
/// into a single
/// call. An application cam use <see cref="Run{T}"/> without explicitly calling
/// <see cref="Init(Terminal.Gui.ConsoleDriver,string)"/>.
/// <see cref="Init(Terminal.Gui.IConsoleDriver,string)"/>.
/// </para>
/// <param name="driver">
/// The <see cref="ConsoleDriver"/> to use. If neither <paramref name="driver"/> or
/// The <see cref="IConsoleDriver"/> 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
/// <see cref="IConsoleDriver"/> to use. If neither <paramref name="driver"/> or <paramref name="driverName"/> are
/// specified the default driver for the platform will be used.
/// </param>
[RequiresUnreferencedCode ("AOT")]
[RequiresDynamicCode ("AOT")]
public static void Init (ConsoleDriver? driver = null, string? driverName = null) { InternalInit (driver, driverName); }
public static void Init (IConsoleDriver? driver = null, string? driverName = null) { InternalInit (driver, driverName); }

internal static int MainThreadId { get; set; } = -1;

Expand All @@ -53,7 +53,7 @@ public static partial class Application // Initialization (Init/Shutdown)
[RequiresUnreferencedCode ("AOT")]
[RequiresDynamicCode ("AOT")]
internal static void InternalInit (
ConsoleDriver? driver = null,
IConsoleDriver? driver = null,
string? driverName = null,
bool calledViaRunT = false
)
Expand Down Expand Up @@ -136,7 +136,7 @@ internal static void InternalInit (

if (driverType is { })
{
Driver = (ConsoleDriver)Activator.CreateInstance (driverType)!;
Driver = (IConsoleDriver)Activator.CreateInstance (driverType)!;
}
else
{
Expand Down Expand Up @@ -181,7 +181,7 @@ internal static void InternalInit (
private static void Driver_KeyUp (object? sender, Key e) { RaiseKeyUpEvent (e); }
private static void Driver_MouseEvent (object? sender, MouseEventArgs e) { RaiseMouseEvent (e); }

/// <summary>Gets of list of <see cref="ConsoleDriver"/> types that are available.</summary>
/// <summary>Gets of list of <see cref="IConsoleDriver"/> types that are available.</summary>
/// <returns></returns>
[RequiresUnreferencedCode ("AOT")]
public static List<Type?> GetDriverTypes ()
Expand All @@ -193,7 +193,7 @@ internal static void InternalInit (
{
foreach (Type? type in asm.GetTypes ())
{
if (type.IsSubclassOf (typeof (ConsoleDriver)) && !type.IsAbstract)
if (typeof (IConsoleDriver).IsAssignableFrom (type) && !type.IsAbstract && type.IsClass)
{
driverTypes.Add (type);
}
Expand Down
6 changes: 3 additions & 3 deletions Terminal.Gui/Application/Application.Keyboard.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ namespace Terminal.Gui;
public static partial class Application // Keyboard handling
{
/// <summary>
/// Called when the user presses a key (by the <see cref="ConsoleDriver"/>). Raises the cancelable
/// Called when the user presses a key (by the <see cref="IConsoleDriver"/>). Raises the cancelable
/// <see cref="KeyDown"/> event, then calls <see cref="View.NewKeyDownEvent"/> on all top level views, and finally
/// if the key was not handled, invokes any Application-scoped <see cref="KeyBindings"/>.
/// </summary>
Expand Down Expand Up @@ -116,7 +116,7 @@ public static bool RaiseKeyDownEvent (Key key)
public static event EventHandler<Key>? KeyDown;

/// <summary>
/// Called when the user releases a key (by the <see cref="ConsoleDriver"/>). Raises the cancelable <see cref="KeyUp"/>
/// Called when the user releases a key (by the <see cref="IConsoleDriver"/>). Raises the cancelable <see cref="KeyUp"/>
/// event
/// then calls <see cref="View.NewKeyUpEvent"/> on all top level views. Called after <see cref="RaiseKeyDownEvent"/>.
/// </summary>
Expand Down Expand Up @@ -205,7 +205,7 @@ internal static void AddApplicationKeyBindings ()
Command.Refresh,
static () =>
{
LayoutAndDraw ();
LayoutAndDraw (true);

return true;
}
Expand Down
15 changes: 10 additions & 5 deletions Terminal.Gui/Application/Application.Run.cs
Original file line number Diff line number Diff line change
Expand Up @@ -305,7 +305,7 @@ internal static bool PositionCursor ()
/// <returns>The created <see cref="Toplevel"/> object. The caller is responsible for disposing this object.</returns>
[RequiresUnreferencedCode ("AOT")]
[RequiresDynamicCode ("AOT")]
public static Toplevel Run (Func<Exception, bool>? errorHandler = null, ConsoleDriver? driver = null) { return Run<Toplevel> (errorHandler, driver); }
public static Toplevel Run (Func<Exception, bool>? errorHandler = null, IConsoleDriver? driver = null) { return Run<Toplevel> (errorHandler, driver); }

/// <summary>
/// Runs the application by creating a <see cref="Toplevel"/>-derived object of type <c>T</c> and calling
Expand All @@ -323,14 +323,14 @@ internal static bool PositionCursor ()
/// </remarks>
/// <param name="errorHandler"></param>
/// <param name="driver">
/// The <see cref="ConsoleDriver"/> to use. If not specified the default driver for the platform will
/// The <see cref="IConsoleDriver"/> to use. If not specified the default driver for the 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>
/// <returns>The created T object. The caller is responsible for disposing this object.</returns>
[RequiresUnreferencedCode ("AOT")]
[RequiresDynamicCode ("AOT")]
public static T Run<T> (Func<Exception, bool>? errorHandler = null, ConsoleDriver? driver = null)
public static T Run<T> (Func<Exception, bool>? errorHandler = null, IConsoleDriver? driver = null)
where T : Toplevel, new()
{
if (!Initialized)
Expand Down Expand Up @@ -369,7 +369,7 @@ public static T Run<T> (Func<Exception, bool>? errorHandler = null, ConsoleDrive
/// return control immediately.
/// </para>
/// <para>When using <see cref="Run{T}"/> or
/// <see cref="Run(System.Func{System.Exception,bool},Terminal.Gui.ConsoleDriver)"/>
/// <see cref="Run(System.Func{System.Exception,bool},Terminal.Gui.IConsoleDriver)"/>
/// <see cref="Init"/> will be called automatically.
/// </para>
/// <para>
Expand Down Expand Up @@ -505,6 +505,11 @@ public static void LayoutAndDraw (bool forceDraw = false)
{
bool neededLayout = View.Layout (TopLevels.Reverse (), Screen.Size);

if (ClearScreenNextIteration)
{
forceDraw = true;
ClearScreenNextIteration = false;
}
if (forceDraw)
{
Driver?.ClearContents ();
Expand Down Expand Up @@ -688,6 +693,6 @@ public static void End (RunState runState)
runState.Toplevel = null;
runState.Dispose ();

LayoutAndDraw ();
LayoutAndDraw (true);
}
}
15 changes: 12 additions & 3 deletions Terminal.Gui/Application/Application.Screen.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@ public static partial class Application // Screen related stuff
private static Rectangle? _screen;

/// <summary>
/// Gets or sets the size of the screen. By default, this is the size of the screen as reported by the <see cref="ConsoleDriver"/>.
/// Gets or sets the size of the screen. By default, this is the size of the screen as reported by the <see cref="IConsoleDriver"/>.
/// </summary>
/// <remarks>
/// <para>
/// If the <see cref="ConsoleDriver"/> has not been initialized, this will return a default size of 2048x2048; useful for unit tests.
/// If the <see cref="IConsoleDriver"/> has not been initialized, this will return a default size of 2048x2048; useful for unit tests.
/// </para>
/// </remarks>
public static Rectangle Screen
Expand Down Expand Up @@ -63,8 +63,17 @@ public static bool OnSizeChanging (SizeChangedEventArgs args)
t.SetNeedsLayout ();
}

LayoutAndDraw ();
LayoutAndDraw (true);

return true;
}

/// <summary>
/// Gets or sets whether the screen will be cleared, and all Views redrawn, during the next Application iteration.
/// </summary>
/// <remarks>
/// This is typicall set to true when a View's <see cref="View.Frame"/> changes and that view has no
/// SuperView (e.g. when <see cref="Application.Top"/> is moved or resized.
/// </remarks>
public static bool ClearScreenNextIteration { get; set; }
}
8 changes: 5 additions & 3 deletions Terminal.Gui/Application/Application.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public static partial class Application
/// <returns>A string representation of the Application </returns>
public new static string ToString ()
{
ConsoleDriver? driver = Driver;
IConsoleDriver? driver = Driver;

if (driver is null)
{
Expand All @@ -43,11 +43,11 @@ public static partial class Application
}

/// <summary>
/// Gets a string representation of the Application rendered by the provided <see cref="ConsoleDriver"/>.
/// Gets a string representation of the Application rendered by the provided <see cref="IConsoleDriver"/>.
/// </summary>
/// <param name="driver">The driver to use to render the contents.</param>
/// <returns>A string representation of the Application </returns>
public static string ToString (ConsoleDriver? driver)
public static string ToString (IConsoleDriver? driver)
{
if (driver is null)
{
Expand Down Expand Up @@ -215,6 +215,8 @@ internal static void ResetState (bool ignoreDisposed = false)

Navigation = null;

ClearScreenNextIteration = false;

AddApplicationKeyBindings ();

// Reset synchronization context to allow the user to run async/await,
Expand Down
31 changes: 14 additions & 17 deletions Terminal.Gui/Application/MainLoop.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
//
#nullable enable
//
// MainLoop.cs: IMainLoopDriver and MainLoop for Terminal.Gui
//
// Authors:
Expand Down Expand Up @@ -36,7 +37,7 @@ internal interface IMainLoopDriver
/// Monitoring of file descriptors is only available on Unix, there does not seem to be a way of supporting this
/// on Windows.
/// </remarks>
internal class MainLoop : IDisposable
public class MainLoop : IDisposable
{
internal List<Func<bool>> _idleHandlers = new ();
internal SortedList<long, Timeout> _timeouts = new ();
Expand All @@ -49,7 +50,7 @@ internal class MainLoop : IDisposable
/// <summary>Creates a new MainLoop.</summary>
/// <remarks>Use <see cref="Dispose"/> to release resources.</remarks>
/// <param name="driver">
/// The <see cref="ConsoleDriver"/> instance (one of the implementations FakeMainLoop, UnixMainLoop,
/// The <see cref="IConsoleDriver"/> instance (one of the implementations FakeMainLoop, UnixMainLoop,
/// NetMainLoop or WindowsMainLoop).
/// </param>
internal MainLoop (IMainLoopDriver driver)
Expand All @@ -72,7 +73,7 @@ internal ReadOnlyCollection<Func<bool>> IdleHandlers

/// <summary>The current <see cref="IMainLoopDriver"/> in use.</summary>
/// <value>The main loop driver.</value>
internal IMainLoopDriver MainLoopDriver { get; private set; }
internal IMainLoopDriver? MainLoopDriver { get; private set; }

/// <summary>Used for unit tests.</summary>
internal bool Running { get; set; }
Expand Down Expand Up @@ -117,7 +118,7 @@ internal Func<bool> AddIdle (Func<bool> idleHandler)
_idleHandlers.Add (idleHandler);
}

MainLoopDriver.Wakeup ();
MainLoopDriver?.Wakeup ();

return idleHandler;
}
Expand All @@ -130,10 +131,7 @@ internal Func<bool> AddIdle (Func<bool> idleHandler)
/// </remarks>
internal object AddTimeout (TimeSpan time, Func<bool> callback)
{
if (callback is null)
{
throw new ArgumentNullException (nameof (callback));
}
ArgumentNullException.ThrowIfNull (callback);

var timeout = new Timeout { Span = time, Callback = callback };
AddTimeout (time, timeout);
Expand All @@ -156,7 +154,7 @@ internal bool CheckTimersAndIdleHandlers (out int waitTimeout)

waitTimeout = 0;

lock (_timeouts)
lock (_timeoutsLockToken)
{
if (_timeouts.Count > 0)
{
Expand Down Expand Up @@ -191,7 +189,7 @@ internal bool CheckTimersAndIdleHandlers (out int waitTimeout)
/// You can use this method if you want to probe if events are pending. Typically used if you need to flush the
/// input queue while still running some of your own code in your main thread.
/// </remarks>
internal bool EventsPending () { return MainLoopDriver.EventsPending (); }
internal bool EventsPending () { return MainLoopDriver!.EventsPending (); }

/// <summary>Removes an idle handler added with <see cref="AddIdle(Func{bool})"/> from processing.</summary>
/// <param name="token">A token returned by <see cref="AddIdle(Func{bool})"/></param>
Expand Down Expand Up @@ -225,7 +223,7 @@ internal bool RemoveTimeout (object token)
{
lock (_timeoutsLockToken)
{
int idx = _timeouts.IndexOfValue (token as Timeout);
int idx = _timeouts.IndexOfValue ((token as Timeout)!);

if (idx == -1)
{
Expand Down Expand Up @@ -262,17 +260,17 @@ internal void Run ()
/// </remarks>
internal void RunIteration ()
{
lock (_timeouts)
lock (_timeoutsLockToken)
{
if (_timeouts.Count > 0)
{
RunTimers ();
}
}

MainLoopDriver.Iteration ();
MainLoopDriver?.Iteration ();

var runIdle = false;
bool runIdle;

lock (_idleHandlersLock)
{
Expand All @@ -296,8 +294,7 @@ internal void Stop ()
/// Invoked when a new timeout is added. To be used in the case when
/// <see cref="Application.EndAfterFirstIteration"/> is <see langword="true"/>.
/// </summary>
[CanBeNull]
internal event EventHandler<TimeoutEventArgs> TimeoutAdded;
internal event EventHandler<TimeoutEventArgs>? TimeoutAdded;

/// <summary>Wakes up the <see cref="MainLoop"/> that might be waiting on input.</summary>
internal void Wakeup () { MainLoopDriver?.Wakeup (); }
Expand Down
2 changes: 1 addition & 1 deletion Terminal.Gui/Clipboard/Clipboard.cs
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ public static bool TrySetClipboardData (string text)

/// <summary>
/// Helper class for console drivers to invoke shell commands to interact with the clipboard. Used primarily by
/// CursesDriver, but also used in Unit tests which is why it is in ConsoleDriver.cs.
/// CursesDriver, but also used in Unit tests which is why it is in IConsoleDriver.cs.
/// </summary>
internal static class ClipboardProcessRunner
{
Expand Down
Loading

0 comments on commit 37bb43a

Please sign in to comment.