From e8ba3dddeefe64ab533cbe3f82433fa9669c1370 Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Thu, 20 Nov 2025 06:32:37 +0000 Subject: [PATCH] Phase 1: Add CWP support to View initialization Co-authored-by: tig <585482+tig@users.noreply.github.com> --- Terminal.Gui/ViewBase/View.cs | 95 +++++++++++++++++++++++++++++++++-- 1 file changed, 92 insertions(+), 3 deletions(-) diff --git a/Terminal.Gui/ViewBase/View.cs b/Terminal.Gui/ViewBase/View.cs index 68325dfe9..5dc0ad406 100644 --- a/Terminal.Gui/ViewBase/View.cs +++ b/Terminal.Gui/ViewBase/View.cs @@ -189,10 +189,34 @@ public partial class View : IDisposable, ISupportInitializeNotification } /// - /// Raised once when the is being initialized for the first time. Allows - /// configurations and assignments to be performed before the being shown. + /// Raised during before initialization completes. Can be canceled by setting + /// to . Follows the Cancellable Work Pattern (CWP). + /// + /// + /// + /// This event is raised after the virtual method is called and before + /// the initialization work is performed. Subscribe to this event to perform pre-initialization work + /// or to cancel initialization. + /// + /// + /// This event follows Terminal.Gui's Cancellable Work Pattern. The pattern is: + /// 1. (pre-notification, can cancel) + /// 2. event (can cancel) + /// 3. Initialization work (sets = true, layouts, etc.) + /// 4. (post-notification) + /// 5. event + /// + /// + public event EventHandler? Initializing; + + /// + /// Raised once when the has been initialized for the first time. Allows + /// configurations and assignments to be performed after the has been laid out. /// View implements to allow for more sophisticated initialization. /// + /// + /// This is the post-notification event in the Cancellable Work Pattern pair with . + /// public event EventHandler? Initialized; /// @@ -254,7 +278,18 @@ public partial class View : IDisposable, ISupportInitializeNotification /// Signals the View that initialization is ending. See . /// - /// Initializes all SubViews and Invokes the event. + /// + /// Initializes all SubViews and invokes the event. Follows the Cancellable + /// Work Pattern (CWP). + /// + /// + /// The initialization flow is: + /// 1. (pre-notification, can cancel) + /// 2. event (can cancel) + /// 3. Initialization work (sets = true, layouts, etc.) + /// 4. (post-notification) + /// 5. event + /// /// public virtual void EndInit () { @@ -263,6 +298,22 @@ public partial class View : IDisposable, ISupportInitializeNotification throw new InvalidOperationException ("The view is already initialized."); } + // CWP Phase 1: Pre-notification via virtual method (can cancel) + if (OnInitializing ()) + { + return; // Initialization canceled + } + + // CWP Phase 2: Event notification (can cancel) + var args = new CancelEventArgs (); + Initializing?.Invoke (this, args); + + if (args.Cancel) + { + return; // Initialization canceled + } + + // CWP Phase 3: Perform initialization work IsInitialized = true; EndInitAdornments (); @@ -290,9 +341,47 @@ public partial class View : IDisposable, ISupportInitializeNotification // Thus, we call SetNeedsLayout() to ensure that the layout is performed at least once. SetNeedsLayout (); + // CWP Phase 4: Post-notification via virtual method + OnInitialized (); + + // CWP Phase 5: Post-notification event Initialized?.Invoke (this, EventArgs.Empty); } + /// + /// Called before the event and before initialization work is performed. + /// Override to provide custom pre-initialization logic or to cancel initialization. + /// + /// to cancel initialization; to proceed. + /// + /// + /// This is the first phase of the Cancellable Work Pattern (CWP) for view initialization. + /// Default implementation returns (allow initialization to proceed). + /// + /// + /// If this method returns , initialization is canceled, and neither the + /// event nor event will be raised. + /// + /// + protected virtual bool OnInitializing () + { + return false; // Default: allow initialization + } + + /// + /// Called after initialization work completes and before the event is raised. + /// Override to provide custom post-initialization logic. + /// + /// + /// This is the fourth phase of the Cancellable Work Pattern (CWP) for view initialization. + /// At this point, is , layout has been performed, + /// and all subviews have been initialized. + /// + protected virtual void OnInitialized () + { + // Default: do nothing + } + #endregion Constructors and Initialization #region Visibility