123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243 |
- #nullable enable
- using System.Collections.Concurrent;
- using System.Diagnostics.CodeAnalysis;
- using Microsoft.Extensions.Logging;
- namespace Terminal.Gui;
- /// <summary>
- /// Implementation of <see cref="IApplication"/> that boots the new 'v2'
- /// main loop architecture.
- /// </summary>
- public class ApplicationV2 : ApplicationImpl
- {
- private readonly Func<INetInput> _netInputFactory;
- private readonly Func<IConsoleOutput> _netOutputFactory;
- private readonly Func<IWindowsInput> _winInputFactory;
- private readonly Func<IConsoleOutput> _winOutputFactory;
- private IMainLoopCoordinator? _coordinator;
- private string? _driverName;
- private readonly ITimedEvents _timedEvents = new TimedEvents ();
- /// <summary>
- /// Creates anew instance of the Application backend. The provided
- /// factory methods will be used on Init calls to get things booted.
- /// </summary>
- public ApplicationV2 () : this (
- () => new NetInput (),
- () => new NetOutput (),
- () => new WindowsInput (),
- () => new WindowsOutput ()
- )
- { }
- internal ApplicationV2 (
- Func<INetInput> netInputFactory,
- Func<IConsoleOutput> netOutputFactory,
- Func<IWindowsInput> winInputFactory,
- Func<IConsoleOutput> winOutputFactory
- )
- {
- _netInputFactory = netInputFactory;
- _netOutputFactory = netOutputFactory;
- _winInputFactory = winInputFactory;
- _winOutputFactory = winOutputFactory;
- IsLegacy = false;
- }
- /// <inheritdoc/>
- [RequiresUnreferencedCode ("AOT")]
- [RequiresDynamicCode ("AOT")]
- public override void Init (IConsoleDriver? driver = null, string? driverName = null)
- {
- if (Application.Initialized)
- {
- Logging.Logger.LogError ("Init called multiple times without shutdown, ignoring.");
- return;
- }
- if (!string.IsNullOrWhiteSpace (driverName))
- {
- _driverName = driverName;
- }
- Application.Navigation = new ();
- Application.AddKeyBindings ();
- // This is consistent with Application.ForceDriver which magnetically picks up driverName
- // making it use custom driver in future shutdown/init calls where no driver is specified
- CreateDriver (driverName ?? _driverName);
- Application.InitializeConfigurationManagement ();
- Application.Initialized = true;
- Application.OnInitializedChanged (this, new (true));
- Application.SubscribeDriverEvents ();
- }
- private void CreateDriver (string? driverName)
- {
- PlatformID p = Environment.OSVersion.Platform;
- bool definetlyWin = driverName?.Contains ("win") ?? false;
- bool definetlyNet = driverName?.Contains ("net") ?? false;
- if (definetlyWin)
- {
- _coordinator = CreateWindowsSubcomponents ();
- }
- else if (definetlyNet)
- {
- _coordinator = CreateNetSubcomponents ();
- }
- else if (p == PlatformID.Win32NT || p == PlatformID.Win32S || p == PlatformID.Win32Windows)
- {
- _coordinator = CreateWindowsSubcomponents ();
- }
- else
- {
- _coordinator = CreateNetSubcomponents ();
- }
- _coordinator.StartAsync ().Wait ();
- if (Application.Driver == null)
- {
- throw new ("Application.Driver was null even after booting MainLoopCoordinator");
- }
- }
- private IMainLoopCoordinator CreateWindowsSubcomponents ()
- {
- ConcurrentQueue<WindowsConsole.InputRecord> inputBuffer = new ();
- MainLoop<WindowsConsole.InputRecord> loop = new ();
- return new MainLoopCoordinator<WindowsConsole.InputRecord> (
- _timedEvents,
- _winInputFactory,
- inputBuffer,
- new WindowsInputProcessor (inputBuffer),
- _winOutputFactory,
- loop);
- }
- private IMainLoopCoordinator CreateNetSubcomponents ()
- {
- ConcurrentQueue<ConsoleKeyInfo> inputBuffer = new ();
- MainLoop<ConsoleKeyInfo> loop = new ();
- return new MainLoopCoordinator<ConsoleKeyInfo> (
- _timedEvents,
- _netInputFactory,
- inputBuffer,
- new NetInputProcessor (inputBuffer),
- _netOutputFactory,
- loop);
- }
- /// <inheritdoc/>
- [RequiresUnreferencedCode ("AOT")]
- [RequiresDynamicCode ("AOT")]
- public override T Run<T> (Func<Exception, bool>? errorHandler = null, IConsoleDriver? driver = null)
- {
- var top = new T ();
- Run (top, errorHandler);
- return top;
- }
- /// <inheritdoc/>
- public override void Run (Toplevel view, Func<Exception, bool>? errorHandler = null)
- {
- Logging.Logger.LogInformation ($"Run '{view}'");
- ArgumentNullException.ThrowIfNull (view);
- if (!Application.Initialized)
- {
- throw new NotInitializedException (nameof (Run));
- }
- Application.Top = view;
- Application.Begin (view);
- // TODO : how to know when we are done?
- while (Application.TopLevels.TryPeek (out Toplevel? found) && found == view)
- {
- if (_coordinator is null)
- {
- throw new ($"{nameof (IMainLoopCoordinator)}inexplicably became null during Run");
- }
- _coordinator.RunIteration ();
- }
- }
- /// <inheritdoc/>
- public override void Shutdown ()
- {
- _coordinator?.Stop ();
- base.Shutdown ();
- Application.Driver = null;
- }
- /// <inheritdoc/>
- public override void RequestStop (Toplevel? top)
- {
- Logging.Logger.LogInformation ($"RequestStop '{top}'");
- // TODO: This definition of stop seems sketchy
- Application.TopLevels.TryPop (out _);
- if (Application.TopLevels.Count > 0)
- {
- Application.Top = Application.TopLevels.Peek ();
- }
- else
- {
- Application.Top = null;
- }
- }
- /// <inheritdoc/>
- public override void Invoke (Action action)
- {
- _timedEvents.AddIdle (
- () =>
- {
- action ();
- return false;
- }
- );
- }
- /// <inheritdoc/>
- public override void AddIdle (Func<bool> func) { _timedEvents.AddIdle (func); }
- /// <summary>
- /// Removes an idle function added by <see cref="AddIdle"/>
- /// </summary>
- /// <param name="fnTrue">Function to remove</param>
- /// <returns>True if it was found and removed</returns>
- public bool RemoveIdle (Func<bool> fnTrue) { return _timedEvents.RemoveIdle (fnTrue); }
- /// <inheritdoc/>
- public override object AddTimeout (TimeSpan time, Func<bool> callback) { return _timedEvents.AddTimeout (time, callback); }
- /// <inheritdoc/>
- public override bool RemoveTimeout (object token) { return _timedEvents.RemoveTimeout (token); }
- /// <inheritdoc />
- public override void LayoutAndDraw (bool forceDraw)
- {
- // No more ad-hoc drawing, you must wait for iteration to do it
- Application.Top?.SetNeedsDraw();
- Application.Top?.SetNeedsLayout ();
- }
- }
|