Centralize Terminal.Gui static Application access behind helpers#180
Conversation
Route all UI-layer access to the deprecated static Application object through a small helper surface in Utilities/, alongside the existing UiThread.Run wrapper: - TerminalUi.AddTimeout/RemoveTimeout for main-loop timers - TerminalUi.RunModal/RequestStop for modal dialogs - TerminalUi.Query/ErrorQuery so call sites stop passing Application.Instance to MessageBox - TerminalUi.TrySetClipboardData for the static Clipboard class - TerminalUi.TopRunnableView/IsTopRunnable for top-level view queries All call sites in App/ now use the helpers; behavior is unchanged. The remaining direct uses of the deprecated statics are confined to TerminalUi/UiThread plus the entry points that need the application lifecycle itself (Program.cs Init/Run/Shutdown, MainWindow Application.KeyDown, ThemeManager Application.Driver), which the next commit migrates to the instance-based IApplication API. CS0618 count: 101 -> 27, now concentrated in 5 files. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01Cb6n3buYyULFMq4AursDkF
Switch from the deprecated legacy static Application object to the instance-based model introduced in Terminal.Gui 2.4: - Program.cs creates the application with Application.Create(), runs app.Init()/app.Run(mainWindow), and disposes it on exit (Dispose is the instance-based replacement for Application.Shutdown). The IL2026 pragma is dropped: the instance lifecycle carries no trim attributes. - TerminalUi holds the IApplication instance (TerminalUi.App, assigned once at startup) and all helpers now call the non-deprecated instance members: Invoke, AddTimeout/RemoveTimeout, Run/RequestStop, MessageBox overloads taking IApplication, App.Clipboard, TopRunnable. - UiThread.Run marshals via TerminalUi, preserving the existing threading discipline for OPC UA background-thread callbacks. - MainWindow subscribes to keyboard events via App.Keyboard.KeyDown (TerminalUi.Add/RemoveKeyDownHandler) instead of Application.KeyDown. - ThemeManager reads the driver from the instance (TerminalUi.Driver). When no application is running (headless unit tests), fire-and-forget helpers (Invoke, timers, clipboard) are no-ops and interactive ones (modal dialogs, message boxes) throw, matching the effective legacy behavior where those paths were never reachable without a session. CS0618 count: 27 -> 1. The single remaining warning is the unrelated TextView deprecation (superseded by the external gui-cs/Editor package), handled in the next commit. Verified: 696/696 tests pass; TUI smoke-tested under a pseudo-tty (full render of menus/panes, Ctrl+Q quits, exit code 0; --help exits 0). Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01Cb6n3buYyULFMq4AursDkF
With the Terminal.Gui static-API migration complete the build is warning-free, so enable TreatWarningsAsErrors in Opcilloscope.csproj to stop deprecations from accumulating silently until a package bump turns them into a wall of errors. The one genuinely unfixable warning is suppressed at the call site with a targeted #pragma: HelpDialog's TextView is obsolete in Terminal.Gui 2.4.5, but its designated successor (EditorView) ships in the separate gui-cs/Editor package, and a read-only help pane does not justify a new dependency. Verified clean under warnings-as-errors: Debug and Release builds, dotnet publish (single-file, as used by ci.yml/release.yml), and the full test suite (696/696); TUI pseudo-tty smoke test still exits 0. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01Cb6n3buYyULFMq4AursDkF
The Terminal.Gui v2 API notes still told contributors to use the now- deprecated static Application.Invoke()/AddTimeout(). Replace that guidance with the instance-based model the codebase migrated to: route all UI-thread marshalling, timers, modal dialogs, message boxes, and clipboard access through the UiThread/TerminalUi helpers, and note that TreatWarningsAsErrors makes a static-Application call a build error. Also list Utilities/TerminalUi.cs in the project structure and fix the thread-safety and common-issues examples. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01Cb6n3buYyULFMq4AursDkF
ReviewReviewed the full diff plus the resulting repo state (this PR's commits are already merged into the checked-out tree, so I could grep/cross-check every call site rather than just read the patch). SummaryClean, well-scoped mechanical migration off the deprecated Terminal.Gui static Things that look right
Suggestions (non-blocking)
Bugs / correctness / security / performanceNone found. This is a pure delegation/indirection refactor (call-site renames behind a thin static wrapper); no behavior, threading, or data-flow changes, and no security-sensitive surface is touched. Performance impact is a single extra static-method hop, negligible for a TUI app. Nice, focused PR — the "why" in the commit messages (CS0618 counts going 101 → 27 → 1, confining the remainder to 5 files) makes the intent and progress easy to follow. |
Cover the TerminalUi contract the migration introduced: fire-and-forget members (Invoke, AddTimeout/RemoveTimeout, TrySetClipboardData, TopRunnableView, IsTopRunnable, Driver, key-down handlers) degrade to no-ops when no application is running, while the interactive members (RunModal, RequestStop, Query, ErrorQuery) throw InvalidOperationException rather than silently skip. Also verify delegation to the running IApplication instance via a mock, and that UiThread.Run marshals through IApplication.Invoke. Closes the coverage gap flagged in PR review; both helpers previously had no dedicated tests. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01Cb6n3buYyULFMq4AursDkF
Review: Centralize Terminal.Gui static
|
Route all UI-layer access to the deprecated static Application object
through a small helper surface in Utilities/, alongside the existing
UiThread.Run wrapper:
Application.Instance to MessageBox
All call sites in App/ now use the helpers; behavior is unchanged.
The remaining direct uses of the deprecated statics are confined to
TerminalUi/UiThread plus the entry points that need the application
lifecycle itself (Program.cs Init/Run/Shutdown, MainWindow
Application.KeyDown, ThemeManager Application.Driver), which the next
commit migrates to the instance-based IApplication API.
CS0618 count: 101 -> 27, now concentrated in 5 files.
Co-Authored-By: Claude Fable 5 noreply@anthropic.com
Claude-Session: https://claude.ai/code/session_01Cb6n3buYyULFMq4AursDkF