Skip to content

A full-featured download manager.

fix(windows): detect UserChoiceLatest defaults

Dr_rOotc16bd0f3389d
diff --git a/src/main/platform/windows-default-apps.test.ts b/src/main/platform/windows-default-apps.test.ts
index 634113a4..0dc60bec 100644
--- a/src/main/platform/windows-default-apps.test.ts
+++ b/src/main/platform/windows-default-apps.test.ts
@@ -2,6 +2,7 @@ import { describe, expect, it, vi } from 'vitest'
import {
buildWindowsDefaultAppsSettingsUrl,
getWindowsDefaultAssociations,
+ readWindowsUserChoice,
resolveWindowsDefaultAppsSettingsUrl,
supportsRegisteredAppDefaultAppsQuery,
WINDOWS_DEFAULT_APPS_SETTINGS_URL,
@@ -108,6 +109,35 @@ describe('Windows Default Apps settings', () => {
expect(readUserChoice).not.toHaveBeenCalled()
})
+ it('uses Windows 11 UserChoiceLatest instead of a stale legacy value', async () => {
+ const queryProgId = vi.fn(async (key: string) => {
+ if (key.endsWith('UserChoiceLatest\\ProgId')) {
+ return key.includes('FileExts')
+ ? 'Motrix.File.Torrent'
+ : 'Motrix.Url.Magnet'
+ }
+ return 'Other.Stale.Handler'
+ })
+ const readUserChoice = (association: 'torrent' | 'magnet') =>
+ readWindowsUserChoice(association, { queryProgId })
+
+ await expect(
+ getWindowsDefaultAssociations({
+ platform: 'win32',
+ hasRegistration: vi.fn().mockResolvedValue(true),
+ readUserChoice,
+ })
+ ).resolves.toMatchObject({ torrent: true, magnet: true })
+ expect(queryProgId.mock.calls).toEqual([
+ [
+ 'Software\\Microsoft\\Windows\\CurrentVersion\\Explorer\\FileExts\\.torrent\\UserChoiceLatest\\ProgId',
+ ],
+ [
+ 'Software\\Microsoft\\Windows\\Shell\\Associations\\UrlAssociations\\magnet\\UserChoiceLatest\\ProgId',
+ ],
+ ])
+ })
+
it('reports current defaults for an all-users installation', async () => {
const hasRegistration = vi
.fn()
diff --git a/src/main/platform/windows-default-apps.ts b/src/main/platform/windows-default-apps.ts
index 6d843254..8fc75bb3 100644
--- a/src/main/platform/windows-default-apps.ts
+++ b/src/main/platform/windows-default-apps.ts
@@ -11,12 +11,28 @@ export const WINDOWS_DEFAULT_APPS_SETTINGS_URL = 'ms-settings:defaultapps'
const WINDOWS_CAPABILITIES_REGISTRY_PATH = 'Software\\Motrix\\Capabilities'
const WINDOWS_TORRENT_PROGID = 'Motrix.File.Torrent'
const WINDOWS_MAGNET_PROGID = 'Motrix.Url.Magnet'
-const WINDOWS_USER_CHOICE_KEYS = {
+const WINDOWS_ASSOCIATION_KEYS = {
torrent:
- 'Software\\Microsoft\\Windows\\CurrentVersion\\Explorer\\FileExts\\.torrent\\UserChoice',
+ 'Software\\Microsoft\\Windows\\CurrentVersion\\Explorer\\FileExts\\.torrent',
magnet:
- 'Software\\Microsoft\\Windows\\Shell\\Associations\\UrlAssociations\\magnet\\UserChoice',
+ 'Software\\Microsoft\\Windows\\Shell\\Associations\\UrlAssociations\\magnet',
} as const
+const WINDOWS_USER_CHOICE_SUFFIXES = [
+ 'UserChoiceLatest\\ProgId',
+ 'UserChoiceLatest',
+ 'UserChoice',
+] as const
+
+type WindowsAssociation = keyof typeof WINDOWS_ASSOCIATION_KEYS
+
+interface WindowsUserChoiceResult {
+ ok: boolean
+ progId: string | null
+}
+
+interface ReadWindowsUserChoiceDeps {
+ queryProgId?: (key: string) => Promise<string | null>
+}
export type WindowsRegistrationScope = 'user' | 'machine'
@@ -29,8 +45,8 @@ interface GetWindowsDefaultAssociationsDeps {
platform?: NodeJS.Platform
hasRegistration?: (scope: WindowsRegistrationScope) => Promise<boolean | null>
readUserChoice?: (
- association: 'torrent' | 'magnet'
- ) => Promise<{ ok: boolean; progId: string | null }>
+ association: WindowsAssociation
+ ) => Promise<WindowsUserChoiceResult>
}
/**
@@ -85,32 +101,46 @@ async function hasWindowsRegistration(
}
}
-async function readWindowsUserChoice(
- association: 'torrent' | 'magnet'
-): Promise<{ ok: boolean; progId: string | null }> {
+async function queryWindowsRegistryProgId(key: string): Promise<string | null> {
const systemRoot = process.env.SystemRoot ?? 'C:\\Windows'
const regExe = path.win32.join(systemRoot, 'System32', 'reg.exe')
try {
const { stdout } = await execFileAsync(
regExe,
- [
- 'query',
- `HKCU\\${WINDOWS_USER_CHOICE_KEYS[association]}`,
- '/v',
- 'ProgId',
- '/reg:64',
- ],
+ ['query', `HKCU\\${key}`, '/v', 'ProgId', '/reg:64'],
{ timeout: 2_000, windowsHide: true }
)
- const progId =
+ return (
/^\s*ProgId\s+REG_\w+\s+(.+?)\s*$/imu.exec(stdout.toString())?.[1] ?? null
- return { ok: progId !== null, progId }
+ )
} catch {
- return { ok: false, progId: null }
+ return null
}
}
+/**
+ * Windows 11 24H2+ records the effective choice in UserChoiceLatest and may
+ * leave the legacy UserChoice stale. Current builds nest ProgId one level
+ * deeper, while earlier builds used a direct value. Probe both Latest layouts
+ * before falling back to the legacy key so the verdict follows the handler
+ * Windows actually resolves.
+ */
+export async function readWindowsUserChoice(
+ association: WindowsAssociation,
+ deps: ReadWindowsUserChoiceDeps = {}
+): Promise<WindowsUserChoiceResult> {
+ const queryProgId = deps.queryProgId ?? queryWindowsRegistryProgId
+ const baseKey = WINDOWS_ASSOCIATION_KEYS[association]
+
+ for (const suffix of WINDOWS_USER_CHOICE_SUFFIXES) {
+ const progId = await queryProgId(`${baseKey}\\${suffix}`)
+ if (progId !== null) return { ok: true, progId }
+ }
+
+ return { ok: false, progId: null }
+}
+
export async function getWindowsDefaultAssociations(
deps: GetWindowsDefaultAssociationsDeps = {}
): Promise<WindowsDefaultAssociations> {