Skip to content

v5 F1: DI composition root, plus two hotkey fixes - #696

Merged
TheJoeFin merged 12 commits into
devfrom
v5/f1-di
Nov 10, 2026
Merged

TheJoeFin merged 12 commits into
devfrom
v5/f1-di

Conversation

@TheJoeFin

Copy link
Copy Markdown
Owner

Phase F1 of the v5 plan (docs/v5/F1-DI-Composition-Root.md): one Microsoft.Extensions.DependencyInjection container built by the app. The existing Singleton<T> resolves through it without any of its 77 call sites changing, and without changing when any service is constructed. Also fixes two hotkey bugs found along the way.

What changes

  • ServiceAccess (Core): static seam through which Core and Core.Windows reach the app's container. TryGet<T> returns null when no provider is registered or the type is unknown; GetRequired<T> throws and names the type.
  • Singleton<T>.Instance asks ServiceAccess first, then falls back to the old lazy instance, now exposed internally as FallbackInstance. Behavior is unchanged when no provider is registered. Adds a class constraint, which every current use already meets.
  • AppServices (app): a lazily built ServiceProvider. A [ModuleInitializer] only stores the delegate that reads it, so test discovery and design-time builds construct nothing.
    • SettingsService, HistoryService, TtsService, LanguageService and WebSearchUrlCatalog are registered through a factory that returns Singleton<T>.FallbackInstance. The container and Singleton<T> hand out the same object whichever is asked first, and each service is still built on first use, on the calling thread.
    • ITextGrabSettings is transient over SettingsAccess.Current, so tests that swap the settings resolver don't get a cached object.
    • One shared HttpClient, which recycles pooled connections every 2 minutes.
    • AppRuntimeState.
  • AppRuntimeState now owns HotKeyIds, NumberOfRunningInstances and TextGrabIcon. App's properties forward to it, and every (App)App.Current / App.Current is App cast is replaced with a container lookup. NotifyIconUtilities.RegisterHotKeys and UnregisterHotkeys took an App only to reach HotKeyIds, so they are now parameterless.
  • TesseractGitHubFileDownloader can take the shared HttpClient. Its user-agent moved from the client's default headers onto each request, so a shared client is never modified.
  • Hotkey fixes:
    • UnregisterHotkeys now clears HotKeyIds. Before, every Settings open/close cycle appended a full set of new IDs.
    • HotKeyManager now reuses freed IDs through a HotKeyIdPool (1–0xBFFF) instead of a counter that only grows. Long sessions could otherwise run out of the Win32 application ID range.

Startup order is unchanged. The first Singleton<T>.Instance read, in App's static init, builds the container immediately before SettingsService is constructed, on the same thread and at the same moment as before.

New NuGet packages (please verify the MSIX build in Visual Studio)

  • Microsoft.Extensions.DependencyInjection 10.0.14 in Text-Grab
  • Microsoft.Extensions.DependencyInjection.Abstractions 10.0.14 in Text-Grab.Core. It was already transitive via NCalc.Core, so no resolved version changes.

Testing

Gate Result
dotnet build Tests/Tests.csproj -c Debug -p:Platform=x64 0 errors, no new warnings
Tests.Core.exe 992 passed (baseline 972)
Tests.Core.Windows.exe 208 total, 4 skipped, 0 failed; TierBoundaryTests 3/3
Tests.exe 893 total, 3 skipped, 0 failed (baseline 877)

Tests.exe exits 139 after printing its summary. This also happens on dev; TestHostInitializer.cs documents it as the ARM64 graphics-driver teardown crash.

New tests cover:

  • ServiceAccess, and Singleton<T> with and without a provider;
  • the container resolving every registration;
  • Singleton<T> and the container returning the same instance in both resolution orders, on probe types no earlier test has touched;
  • settings swaps not being cached;
  • the Tesseract request shape;
  • HotKeyIds clearing and HotKeyIdPool reuse and exhaustion.

Hand checks on the unpackaged Debug build, in isolated automation profiles:

  • UI harness safe suites: 15 pass, 9 documented skips, 0 fail.
  • --windowless and the Settings window behave the same as on dev.
  • Tray icon and hotkeys register at startup.
  • Probing from outside the process confirmed the register → unregister (Settings open) → re-register (Settings close) cycle.
  • The owner also tested by hand.

Still to check: the packaged (MSIX) build and first-run migration, in Visual Studio.

🤖 Generated with Claude Code

TheJoeFin and others added 12 commits November 9, 2026 19:36
Core and Core.Windows cannot reference the app, so the app will register a
resolver delegate here from a module initializer, the same pattern as
SettingsAccess. TryGet returns null when no provider is set or the type is not
registered; GetRequired throws with the missing type named.

Adds a direct reference to Microsoft.Extensions.DependencyInjection.Abstractions
10.0.14, which Core already pulled in transitively via NCalc.Core.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Instance now asks ServiceAccess first and falls back to the existing lazy
GetOrAdd instance, exposed internally as FallbackInstance. With no provider
registered, behavior is unchanged. The app will register each Singleton-backed
type with a factory returning FallbackInstance, so code that runs before and
after the container exists sees the same object.

Adds the class constraint, which every current use already satisfies.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
AppServices builds one Microsoft.Extensions.DependencyInjection container,
lazily, on first resolve. A module initializer only stores the delegate that
reads it, so test discovery and design-time builds construct nothing.

Registers SettingsService, HistoryService, TtsService, LanguageService and
WebSearchUrlCatalog through a factory returning Singleton<T>.FallbackInstance,
so every service is still constructed on first use, on the same thread, and
Singleton<T>.Instance and the container return the same object in either
order. ITextGrabSettings is transient over SettingsAccess.Current so swapping
the settings resolver in tests is not cached. Adds one shared HttpClient.

Adds the Microsoft.Extensions.DependencyInjection 10.0.14 package to the app.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
HotKeyIds, NumberOfRunningInstances and TextGrabIcon now live on an
AppRuntimeState singleton registered in AppServices. App's properties of the
same names forward to it, so no caller changes in this commit.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Replaces the three (App)App.Current casts in NotifyIconUtilities with the
container's AppRuntimeState. RegisterHotKeys and UnregisterHotkeys took an App
only to reach HotKeyIds, so they are now parameterless, and their callers in
SettingsWindow, KeysSettings and App drop the App.Current casts that existed
only to pass it in.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Replaces the (App)App.Current cast used to close the tray icon when
run-in-the-background is turned off.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Replaces the App.Current cast in WindowUtilities.ShouldShutDown. The old
guard only skipped the check when Current was not App, which never happens
in the app; with default state the result is the same either way.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Adds a constructor taking an HttpClient and moves the user-agent from the
client's DefaultRequestHeaders onto each request, so a shared client is never
modified. The Language settings page passes the container's HttpClient instead
of creating a new client per download. The parameterless constructor still
creates its own client.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Records that DI.Abstractions was already transitive in Core, that Tests adds
four Singleton<LanguageService> uses, and why removing the SettingsWindow and
KeysSettings casts meant changing the hotkey method signatures. Fixes the
Tests.Core.Windows executable path in the v5 README.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
UnregisterHotkeys unregistered every ID but kept them in the list, and
RegisterHotKeys always appends fresh IDs (HotKeyManager never reuses them).
Each Settings open/close cycle therefore grew the list by a full set, and every
later unregister re-sent the stale IDs to the message window. The list now
holds only the IDs that are currently registered.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
HotKeyManager took every ID from an ever-increasing counter, including for
registrations that failed. RegisterHotKey only accepts application IDs up to
0xBFFF, so a long enough session of Settings open/close cycles would exhaust
the range and every later registration would fail.

A HotKeyIdPool now hands out the lowest free ID in 1..0xBFFF and takes IDs
back on unregister and on failed registration. Hotkey presses are matched by
key and modifiers, never by ID, so reuse does not change dispatch.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
F1 now ends with the HotKeyIds and hotkey ID reuse fixes, so the row
records 790bc4e instead of 0d53f5a.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@TheJoeFin
TheJoeFin merged commit a688fd3 into dev Nov 10, 2026
1 check passed
@TheJoeFin
TheJoeFin deleted the v5/f1-di branch November 10, 2026 03:06
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant