From bc0634cf447c1ad57681e8399186a88593c91188 Mon Sep 17 00:00:00 2001 From: Tig Date: Sun, 23 Nov 2025 07:13:47 -0700 Subject: [PATCH] Fixed all modelusage bugs? Replaced static `Application` references with instance-based `App` context across the codebase. Updated calls to `Application.RequestStop()` and `Application.Screen` to use `App?.RequestStop()` and `App?.Screen` for better encapsulation and flexibility. Refactored test infrastructure to align with the new context, including reintroducing `FakeApplicationFactory` and `FakeApplicationLifecycle` for testing purposes. Improved logging, error handling, and test clarity by adding `logWriter` support and simplifying test setup. Removed redundant or obsolete code, such as `NetSequences` and the old `FakeApplicationFactory` implementation. Updated documentation to reflect the new `IApplication.RequestStop()` usage. --- Examples/UICatalog/Scenarios/Dialogs.cs | 2 +- .../UICatalog/Scenarios/DynamicStatusBar.cs | 2 +- .../Views/Color/ColorPicker.Prompt.cs | 4 +- Terminal.Gui/Views/Dialog.cs | 2 +- .../FileDialogs/DefaultFileOperations.cs | 4 +- Terminal.Gui/Views/FileDialogs/FileDialog.cs | 8 +-- Terminal.Gui/Views/MessageBox.cs | 4 +- Terminal.Gui/Views/TableView/TableView.cs | 4 +- Terminal.Gui/Views/Wizard/Wizard.cs | 2 +- .../FluentTests/FileDialogFluentTests.cs | 23 ++++---- .../FluentTests/GuiTestContextTests.cs | 2 +- .../GuiTestContext.Input.cs | 5 -- .../GuiTestContext.cs | 52 +++++++++++------- .../TerminalGuiFluentTesting/NetSequences.cs | 53 ------------------- Tests/TerminalGuiFluentTesting/With.cs | 5 +- .../FakeDriver/FakeApplicationFactory.cs | 0 .../FakeDriver/FakeApplicationLifecycle.cs | 0 Tests/UnitTests/Views/TableViewTests.cs | 22 ++++---- 18 files changed, 75 insertions(+), 119 deletions(-) delete mode 100644 Tests/TerminalGuiFluentTesting/NetSequences.cs rename Tests/{TerminalGuiFluentTesting => UnitTests}/FakeDriver/FakeApplicationFactory.cs (100%) rename Tests/{TerminalGuiFluentTesting => UnitTests}/FakeDriver/FakeApplicationLifecycle.cs (100%) diff --git a/Examples/UICatalog/Scenarios/Dialogs.cs b/Examples/UICatalog/Scenarios/Dialogs.cs index e7fd1ac77..8e8a6ec99 100644 --- a/Examples/UICatalog/Scenarios/Dialogs.cs +++ b/Examples/UICatalog/Scenarios/Dialogs.cs @@ -266,7 +266,7 @@ public class Dialogs : Scenario { Title = titleEdit.Text, Text = "Dialog Text", - ButtonAlignment = (Alignment)Enum.Parse (typeof (Alignment), alignmentGroup.Labels! [(int)alignmentGroup.Value!.Value] [1..]), + ButtonAlignment = (Alignment)Enum.Parse (typeof (Alignment), alignmentGroup.Labels! [(int)alignmentGroup.Value!.Value] [0..]), Buttons = buttons.ToArray () }; diff --git a/Examples/UICatalog/Scenarios/DynamicStatusBar.cs b/Examples/UICatalog/Scenarios/DynamicStatusBar.cs index 73dd3b802..8e9a23fd1 100644 --- a/Examples/UICatalog/Scenarios/DynamicStatusBar.cs +++ b/Examples/UICatalog/Scenarios/DynamicStatusBar.cs @@ -200,7 +200,7 @@ public class DynamicStatusBar : Scenario TextTitle.Text = string.Empty; Application.RequestStop (); }; - var dialog = new Dialog { Title = "Enter the menu details.", Buttons = [btnOk, btnCancel], Height = Dim.Auto (DimAutoStyle.Content, 17, Application.Screen.Height) }; + var dialog = new Dialog { Title = "Enter the menu details.", Buttons = [btnOk, btnCancel], Height = Dim.Auto (DimAutoStyle.Content, 17, App?.Screen.Height) }; Width = Dim.Fill (); Height = Dim.Fill () - 2; diff --git a/Terminal.Gui/Views/Color/ColorPicker.Prompt.cs b/Terminal.Gui/Views/Color/ColorPicker.Prompt.cs index 400040e63..11609be67 100644 --- a/Terminal.Gui/Views/Color/ColorPicker.Prompt.cs +++ b/Terminal.Gui/Views/Color/ColorPicker.Prompt.cs @@ -37,7 +37,7 @@ public partial class ColorPicker { accept = true; e.Handled = true; - Application.RequestStop (); + (s as View)?.App?.RequestStop (); }; var btnCancel = new Button @@ -51,7 +51,7 @@ public partial class ColorPicker btnCancel.Accepting += (s, e) => { e.Handled = true; - Application.RequestStop (); + (s as View)?.App ?.RequestStop (); }; d.Add (btnOk); diff --git a/Terminal.Gui/Views/Dialog.cs b/Terminal.Gui/Views/Dialog.cs index 1a5b4b362..5396520b0 100644 --- a/Terminal.Gui/Views/Dialog.cs +++ b/Terminal.Gui/Views/Dialog.cs @@ -11,7 +11,7 @@ namespace Terminal.Gui.Views; /// . This will execute the dialog until /// it terminates via the (`Esc` by default), /// or when one of the views or buttons added to the dialog calls -/// . +/// . /// public class Dialog : Window { diff --git a/Terminal.Gui/Views/FileDialogs/DefaultFileOperations.cs b/Terminal.Gui/Views/FileDialogs/DefaultFileOperations.cs index 467e8d74c..8aad9ede1 100644 --- a/Terminal.Gui/Views/FileDialogs/DefaultFileOperations.cs +++ b/Terminal.Gui/Views/FileDialogs/DefaultFileOperations.cs @@ -138,7 +138,7 @@ public class DefaultFileOperations : IFileOperations btnOk.Accepting += (s, e) => { confirm = true; - Application.RequestStop (); + (s as View)?.App?.RequestStop (); // When Accepting is handled, set e.Handled to true to prevent further processing. e.Handled = true; }; @@ -147,7 +147,7 @@ public class DefaultFileOperations : IFileOperations btnCancel.Accepting += (s, e) => { confirm = false; - Application.RequestStop (); + (s as View)?.App?.RequestStop (); // When Accepting is handled, set e.Handled to true to prevent further processing. e.Handled = true; }; diff --git a/Terminal.Gui/Views/FileDialogs/FileDialog.cs b/Terminal.Gui/Views/FileDialogs/FileDialog.cs index a24c82f0a..f8e333fff 100644 --- a/Terminal.Gui/Views/FileDialogs/FileDialog.cs +++ b/Terminal.Gui/Views/FileDialogs/FileDialog.cs @@ -108,7 +108,7 @@ public class FileDialog : Dialog, IDesignable if (Modal) { - Application.RequestStop (); + (s as View)?.App?.RequestStop (); } }; @@ -468,7 +468,6 @@ public class FileDialog : Dialog, IDesignable Style.IconProvider.IsOpenGetter = _treeView.IsExpanded; _treeView.AddObjects (_treeRoots.Keys); -#if MENU_V1 // if filtering on file type is configured then create the ComboBox and establish // initial filtering by extension(s) @@ -479,6 +478,7 @@ public class FileDialog : Dialog, IDesignable // Fiddle factor int width = AllowedTypes.Max (a => a.ToString ()!.Length) + 6; +#if MENU_V1 _allowedTypeMenu = new ( "", _allowedTypeMenuItems = AllowedTypes.Select ( @@ -512,8 +512,8 @@ public class FileDialog : Dialog, IDesignable }; Add (_allowedTypeMenuBar); - } #endif + } // if no path has been provided if (_tbPath.Text.Length <= 0) @@ -879,7 +879,7 @@ public class FileDialog : Dialog, IDesignable if (Modal) { - Application.RequestStop (); + App?.RequestStop (); } } diff --git a/Terminal.Gui/Views/MessageBox.cs b/Terminal.Gui/Views/MessageBox.cs index bdbf323a0..a913286c4 100644 --- a/Terminal.Gui/Views/MessageBox.cs +++ b/Terminal.Gui/Views/MessageBox.cs @@ -360,7 +360,7 @@ public static class MessageBox if (count == defaultButton) { b.IsDefault = true; - b.Accepting += (_, e) => + b.Accepting += (s, e) => { if (e?.Context?.Source is Button button) { @@ -376,7 +376,7 @@ public static class MessageBox e.Handled = true; } - Application.RequestStop (); + (s as View)?.App?.RequestStop (); }; } diff --git a/Terminal.Gui/Views/TableView/TableView.cs b/Terminal.Gui/Views/TableView/TableView.cs index 1c5f298e0..5d1e79f7f 100644 --- a/Terminal.Gui/Views/TableView/TableView.cs +++ b/Terminal.Gui/Views/TableView/TableView.cs @@ -1534,7 +1534,7 @@ public class TableView : View, IDesignable /// private void ClearLine (int row, int width) { - if (Application.Screen.Height == 0) + if (App?.Screen.Height == 0) { return; } @@ -1810,7 +1810,7 @@ public class TableView : View, IDesignable } } - if (Application.Screen.Height > 0) + if (App?.Screen.Height > 0) { AddRuneAt (c, row, rune); } diff --git a/Terminal.Gui/Views/Wizard/Wizard.cs b/Terminal.Gui/Views/Wizard/Wizard.cs index 50b21c7ed..3415c572a 100644 --- a/Terminal.Gui/Views/Wizard/Wizard.cs +++ b/Terminal.Gui/Views/Wizard/Wizard.cs @@ -458,7 +458,7 @@ public class Wizard : Dialog if (IsCurrentTop) { - Application.RequestStop (this); + (sender as View)?.App?.RequestStop (this); e.Handled = true; } diff --git a/Tests/IntegrationTests/FluentTests/FileDialogFluentTests.cs b/Tests/IntegrationTests/FluentTests/FileDialogFluentTests.cs index 6cc94ec45..50aba981c 100644 --- a/Tests/IntegrationTests/FluentTests/FileDialogFluentTests.cs +++ b/Tests/IntegrationTests/FluentTests/FileDialogFluentTests.cs @@ -60,10 +60,10 @@ public class FileDialogFluentTests public void CancelFileDialog_QuitKey_Quits (TestDriver d) { SaveDialog? sd = null; - using var c = With.A (() => NewSaveDialog (out sd), 100, 20, d) - .ScreenShot ("Save dialog", _out) - .EnqueueKeyEvent (Application.QuitKey) - .AssertTrue (sd!.Canceled); + using GuiTestContext c = With.A (() => NewSaveDialog (out sd), 100, 20, d, logWriter: _out) + .ScreenShot ("Save dialog", _out) + .EnqueueKeyEvent (Application.QuitKey) + .AssertTrue (sd!.Canceled); } [Theory] @@ -93,7 +93,7 @@ public class FileDialogFluentTests public void CancelFileDialog_UsingCancelButton_AltC (TestDriver d) { SaveDialog? sd = null; - using var c = With.A (() => NewSaveDialog (out sd), 100, 20, d) + using var c = With.A (() => NewSaveDialog (out sd), 100, 20, d, _out) .ScreenShot ("Save dialog", _out) .EnqueueKeyEvent (Key.C.WithAlt) .AssertTrue (sd!.Canceled); @@ -132,12 +132,13 @@ public class FileDialogFluentTests { SaveDialog? sd = null; MockFileSystem? fs = null; - using var c = With.A (() => NewSaveDialog (out sd, out fs, modal: false), 100, 20, d) - .ScreenShot ("Save dialog", _out) - .Focus