diff --git a/Terminal.Gui/Views/Toplevel.cs b/Terminal.Gui/Views/Toplevel.cs index e3feb8ef9..09a3a9e3a 100644 --- a/Terminal.Gui/Views/Toplevel.cs +++ b/Terminal.Gui/Views/Toplevel.cs @@ -17,8 +17,15 @@ namespace Terminal.Gui.Views; /// and run (e.g. s). To run a Toplevel, create the and call /// . /// +/// +/// Phase 2: now implements as an adapter pattern for +/// backward compatibility. The lifecycle events (, , +/// , ) are bridged to the new IRunnable events +/// (, , +/// , ). +/// /// -public partial class Toplevel : View +public partial class Toplevel : View, IRunnable { /// /// Initializes a new instance of the class, @@ -199,6 +206,118 @@ public partial class Toplevel : View #endregion + #region IRunnable Implementation - Adapter Pattern for Backward Compatibility + + /// + bool IRunnable.IsRunning => App?.RunnableSessionStack?.Any (token => token.Runnable == this) ?? false; + + /// + bool IRunnable.RaiseIsRunningChanging (bool oldIsRunning, bool newIsRunning) + { + // Bridge to legacy Closing event when stopping + if (!newIsRunning && oldIsRunning) + { + ToplevelClosingEventArgs args = new (this); + + if (OnClosing (args)) + { + return true; // Canceled + } + } + + return false; + } + + /// + event EventHandler>? IRunnable.IsRunningChanging + { + add { } + remove { } + } + + /// + void IRunnable.RaiseIsRunningChangedEvent (bool newIsRunning) + { + // Update Running property to maintain backward compatibility + Running = newIsRunning; + + // Bridge to legacy events + if (newIsRunning) + { + OnLoaded (); + } + else + { + OnClosed (this); + OnUnloaded (); + } + } + + /// + event EventHandler>? IRunnable.IsRunningChanged + { + add { } + remove { } + } + + /// + bool IRunnable.IsModal + { + get + { + if (App is null) + { + return false; + } + + // Check if this toplevel is at the top of the RunnableSessionStack + if (App.RunnableSessionStack is { } && App.RunnableSessionStack.TryPeek (out RunnableSessionToken? topToken)) + { + return topToken?.Runnable == this; + } + + // Fallback: Check if this is the TopRunnable + return App.TopRunnable == this; + } + } + + /// + bool IRunnable.RaiseIsModalChanging (bool oldIsModal, bool newIsModal) + { + // No cancellation for modal changes in legacy Toplevel + return false; + } + + /// + event EventHandler>? IRunnable.IsModalChanging + { + add { } + remove { } + } + + /// + void IRunnable.RaiseIsModalChangedEvent (bool newIsModal) + { + // Bridge to legacy Activate/Deactivate events + if (newIsModal) + { + OnActivate (App?.TopRunnable as Toplevel ?? this); + } + else + { + OnDeactivate (App?.TopRunnable as Toplevel ?? this); + } + } + + /// + event EventHandler>? IRunnable.IsModalChanged + { + add { } + remove { } + } + + #endregion + #region Size / Position Management // TODO: Make cancelable?