diff --git a/Sources/ParcelApp/AppTourIntegration.swift b/Sources/ParcelApp/AppTourIntegration.swift index b276a519..1021c6d3 100644 --- a/Sources/ParcelApp/AppTourIntegration.swift +++ b/Sources/ParcelApp/AppTourIntegration.swift @@ -57,10 +57,12 @@ enum AppTourIntegration { services.setPaused(true) } }, - useAlternatePrefix: { - try await settings.setValue(.string("alt-cmd"), for: .prefix) - try await reload(services) - }) + useAlternatePrefix: { try await useAlternatePrefix(settings: settings, services: services) }) + } + + static func useAlternatePrefix(settings: SettingsModel, services: EngineShellServices) async throws { + try await settings.setValue(.string("alt-cmd"), for: .prefix) + try await reload(services) } static func personalization( diff --git a/Sources/ParcelApp/NativeAppRuntime.swift b/Sources/ParcelApp/NativeAppRuntime.swift index 351e41da..bcdf418e 100644 --- a/Sources/ParcelApp/NativeAppRuntime.swift +++ b/Sources/ParcelApp/NativeAppRuntime.swift @@ -75,6 +75,8 @@ final class NativeAppRuntime: AppRuntime { private var hotkeyBindings: AppHotkeys? private let shortcutCapture = AppShortcutCapture() private var hotkeyMode: String? + private var voiceOverObservation: NSKeyValueObservation? + private var voiceOverNoticeShown = false private var hotkeyGate = HotkeyDispatchGate() private var effectiveShortcuts: [ConfigCommand: KeyChord]? private var registeredShortcutConfiguration: ParcelConfiguration? @@ -110,6 +112,7 @@ final class NativeAppRuntime: AppRuntime { log.failure = { [weak self] error, kind in self?.services.composition?.report(error, kind: kind) } permissions.onInputAccessRefreshed = { [weak self] in self?.retryInputInstallation($0) } log.refusal = { [weak self] in self?.overlays?.refused($0) } + services.onInputActivityChanged = { [weak self] in self?.refreshInputActivity() } services.overlayActiveAnimations = { [weak self] in self?.overlays?.activeAnimationCount ?? 0 } settings.minimums = SettingsMinimumsModel( read: { [weak self] in @@ -488,7 +491,12 @@ final class NativeAppRuntime: AppRuntime { if self.registeredShortcutConfiguration != nil { _ = self.refreshShortcutPresentation() } self.publishInputFailure() }, - isEventTapInputAllowed: { [weak self] in self?.secureInput.refresh(checkOwner: false) == false }) + isEventTapInputAllowed: { [weak self] in self?.secureInput.refresh(checkOwner: false) == false }, + isActive: { [weak self] in self?.acceptsHotkeyInput == true }, + onCarbonPress: { [weak self] in _ = self?.secureInput.refresh(checkOwner: false) }, + onRepeatedTapTimeout: { [weak self] _ in + self?.services.composition?.report(InputError.tapTimedOut, kind: .tapStalls) + }) hotkeys = center hotkeyBindings = AppHotkeys( register: { key, repeats in _ = try center.register(key, repeats: repeats) }, @@ -496,9 +504,51 @@ final class NativeAppRuntime: AppRuntime { updateRepeats: { center.updateRepeats($0, enabled: $1) }, registrationIsPresent: { center.isRegistered($0) }) } + observeVoiceOver() return refreshHotkeys(configuration, mode: services.engine?.snapshot.state.activeMode) } + private var acceptsHotkeyInput: Bool { + services.engine?.isRunning == true && services.engine?.isPaused == false + && !services.isPaused && !services.isTourUnresolved + } + + private func observeVoiceOver() { + guard voiceOverObservation == nil else { return } + voiceOverObservation = NSWorkspace.shared.observe(\.isVoiceOverEnabled, options: [.new]) { + [weak self] _, _ in + if Thread.isMainThread { + MainActor.assumeIsolated { self?.refreshInputActivity() } + } else { + DispatchQueue.main.async { [weak self] in self?.refreshInputActivity() } + } + } + } + + private func refreshInputActivity() { + hotkeyGate.invalidate() + do { try hotkeys?.refreshActivity() } catch { services.composition?.report(error, kind: .hotkeys) } + if registeredShortcutConfiguration != nil { _ = refreshShortcutPresentation() } + updateVoiceOverNotice() + } + + private func updateVoiceOverNotice() { + let configuration = services.engine?.configuration ?? registeredShortcutConfiguration ?? .defaults + let chords = + Array(configuration.keybindings.values) + Array(configuration.defaultModeEntries.values) + + configuration.modes.values.flatMap { $0.values } + let conflicts = + NSWorkspace.shared.isVoiceOverEnabled + && chords.contains { Self.hotkey($0).conflictsWithVoiceOver } + guard conflicts else { + services.composition?.clearNotice(.voiceOver) + return + } + guard !voiceOverNoticeShown, services.composition != nil else { return } + voiceOverNoticeShown = true + services.composition?.report(AppFailure(message: AppNotices.Kind.voiceOver.message), kind: .voiceOver) + } + private func permitsHotkey(_ press: HotKeyPress) -> Bool { secureInput.refresh(checkOwner: false) return hotkeys?.isAvailable(press.hotKey) == true @@ -536,6 +586,7 @@ final class NativeAppRuntime: AppRuntime { services.composition?.report(AppHotkeyFailure(chords: conflicts.map(\.chord)), kind: .hotkeys) } publishInputFailure() + updateVoiceOverNotice() return active } @@ -545,13 +596,14 @@ final class NativeAppRuntime: AppRuntime { } let routes = Dictionary(uniqueKeysWithValues: hotkeys.registrations.map { ($0.hotKey, $0.route) }) var installed = configuration - installed.keybindings = configuration.keybindings.filter { hotkeys.isRegistered(Self.hotkey($0.value)) } + installed.keybindings = configuration.keybindings.filter { hotkeys.isAvailable(Self.hotkey($0.value)) } installed.defaultModeEntries = configuration.defaultModeEntries.filter { - hotkeys.isRegistered(Self.hotkey($0.value)) + hotkeys.isAvailable(Self.hotkey($0.value)) } - installed.modes = configuration.modes.mapValues { $0.filter { hotkeys.isRegistered(Self.hotkey($0.value)) } } + installed.modes = configuration.modes.mapValues { $0.filter { hotkeys.isAvailable(Self.hotkey($0.value)) } } learningConfiguration = Self.shortcutPresentation( - installed, secureInput: secureInput.isEnabled, routes: routes) + installed, secureInput: secureInput.isEnabled, routes: routes, + isActive: acceptsHotkeyInput, voiceOverEnabled: NSWorkspace.shared.isVoiceOverEnabled) let active = learningConfiguration.keybindings effectiveShortcuts = active shell?.setEffectiveShortcuts(active) @@ -560,12 +612,13 @@ final class NativeAppRuntime: AppRuntime { } static func shortcutPresentation( - _ configuration: ParcelConfiguration, secureInput: Bool, routes: [HotKey: HotKeyRoute] = [:] + _ configuration: ParcelConfiguration, secureInput: Bool, routes: [HotKey: HotKeyRoute] = [:], + isActive: Bool = true, voiceOverEnabled: Bool = false ) -> ParcelConfiguration { - guard secureInput else { return configuration } func available(_ chord: KeyChord) -> Bool { let key = hotkey(chord) - return (routes[key] ?? HotKeyRoute.preferred(for: key)) == .carbon + return isActive && !(voiceOverEnabled && key.conflictsWithVoiceOver) + && (!secureInput || (routes[key] ?? HotKeyRoute.preferred(for: key)) == .carbon) } var presented = configuration presented.keybindings = configuration.keybindings.filter { available($0.value) } @@ -1090,6 +1143,15 @@ extension NativeAppRuntime { case .shortcutSettings, .layoutSettings: showSettings() return + case .useAlternatePrefix: + dispatch { [weak self] in + guard let self else { throw CancellationError() } + try await AppTourIntegration.useAlternatePrefix(settings: self.settings, services: self.services) + } + return + case .retryStartup: + dispatch { [weak self] in await self?.services.composition?.retryStartup() } + return } if !opened { services.composition?.report( @@ -1183,6 +1245,8 @@ extension NativeAppRuntime { } func stopShell() async { secureInput.stop() + voiceOverObservation?.invalidate() + voiceOverObservation = nil feedback?.controller.stop() settings.feedback = nil feedback = nil diff --git a/Sources/ParcelUI/NoticeAction.swift b/Sources/ParcelUI/NoticeAction.swift index 39dbc9f4..913d068f 100644 --- a/Sources/ParcelUI/NoticeAction.swift +++ b/Sources/ParcelUI/NoticeAction.swift @@ -1,7 +1,8 @@ import Foundation public enum NoticeAction: String, Hashable, Sendable { - case accessibilitySettings, inputMonitoringSettings, shortcutSettings, layoutSettings + case accessibilitySettings, inputMonitoringSettings, shortcutSettings, layoutSettings, useAlternatePrefix, + retryStartup public var title: String { let key = @@ -10,6 +11,8 @@ public enum NoticeAction: String, Hashable, Sendable { case .inputMonitoringSettings: "Open Input Monitoring Settings…" case .shortcutSettings: "Review Shortcut Settings…" case .layoutSettings: "Review Layout Settings…" + case .useAlternatePrefix: "Use ⌥⌘" + case .retryStartup: "Try again" } return Bundle.module.localizedString(forKey: key, value: nil, table: "NoticeActions") } diff --git a/Sources/ParcelUI/Resources/NoticeActions.xcstrings b/Sources/ParcelUI/Resources/NoticeActions.xcstrings index 6cebeda1..5276c9ef 100644 --- a/Sources/ParcelUI/Resources/NoticeActions.xcstrings +++ b/Sources/ParcelUI/Resources/NoticeActions.xcstrings @@ -1,6 +1,14 @@ { "sourceLanguage" : "en", "strings" : { + "Use ⌥⌘" : { + "extractionState" : "manual", + "localizations" : { "en" : { "stringUnit" : { "state" : "translated", "value" : "Use ⌥⌘" } } } + }, + "Try again" : { + "extractionState" : "manual", + "localizations" : { "en" : { "stringUnit" : { "state" : "translated", "value" : "Try again" } } } + }, "Open Accessibility Settings…" : { "extractionState" : "manual", "localizations" : { "en" : { "stringUnit" : { "state" : "translated", "value" : "Open Accessibility Settings…" } } } diff --git a/Tests/ParcelAppTests/AppFeedbackTests.swift b/Tests/ParcelAppTests/AppFeedbackTests.swift index 4af0f4c2..c617052b 100644 --- a/Tests/ParcelAppTests/AppFeedbackTests.swift +++ b/Tests/ParcelAppTests/AppFeedbackTests.swift @@ -38,6 +38,36 @@ struct AppFeedbackTests { NativeAppRuntime.shortcutPresentation(configuration, secureInput: false, routes: routes) == configuration) } + @Test(arguments: [false, true]) + func voiceOverHidesConflictingLettersArrowsAndModeEntries(secureInput: Bool) throws { + var configuration = ParcelConfiguration.defaults + let letter = try KeyChord(parsing: "ctrl-alt-a") + let arrow = try KeyChord(parsing: "ctrl-alt-shift-left") + let alternate = try KeyChord(parsing: "alt-cmd-d") + configuration.keybindings = [.focusLeft: letter, .focusRight: arrow, .retile: alternate] + configuration.defaultModeEntries = ["letter": letter, "arrow": arrow, "alternate": alternate] + configuration.modes = ["resize": [.exitMode: letter, .command(.ratioIncrease): alternate]] + let presented = NativeAppRuntime.shortcutPresentation( + configuration, secureInput: secureInput, voiceOverEnabled: true) + #expect(presented.keybindings == [.retile: alternate]) + #expect(presented.defaultModeEntries == ["alternate": alternate]) + #expect(presented.modes == ["resize": [.command(.ratioIncrease): alternate]]) + #expect(NativeAppRuntime.shortcutPresentation(configuration, secureInput: false) == configuration) + } + + @Test(arguments: [false, true]) + func pauseAndTourHoldHideEveryShortcutWithoutChangingTheConfiguration(voiceOver: Bool) { + let configuration = ParcelConfiguration.defaults + let presented = NativeAppRuntime.shortcutPresentation( + configuration, secureInput: false, isActive: false, voiceOverEnabled: voiceOver) + #expect(presented.keybindings.isEmpty) + #expect(presented.defaultModeEntries.isEmpty) + #expect(presented.modes.values.allSatisfy { $0.isEmpty }) + #expect(presented.general == configuration.general) + #expect(presented.motion == configuration.motion) + #expect(NativeAppRuntime.shortcutPresentation(configuration, secureInput: false) == configuration) + } + @Test func inactiveModeHintsUsePreferredRoutesUntilRegistered() throws { var configuration = ParcelConfiguration.defaults let carbon = try KeyChord(parsing: "escape") diff --git a/Tests/ParcelAppTests/NoticeLocalizationRegressionTests.swift b/Tests/ParcelAppTests/NoticeLocalizationRegressionTests.swift index bac0b361..e01be17e 100644 --- a/Tests/ParcelAppTests/NoticeLocalizationRegressionTests.swift +++ b/Tests/ParcelAppTests/NoticeLocalizationRegressionTests.swift @@ -18,6 +18,30 @@ struct NoticeLocalizationRegressionTests { #expect(!notices.visible.joined().contains(raw)) } + @Test func voiceOverNoticeOffersTheAlternatePrefixAndStallsDoNotClaimLostAccess() { + let notices = AppNotices() + notices.report(InputError.tapTimedOut, kind: .voiceOver) + notices.report(InputError.tapTimedOut, kind: .voiceOver) + #expect(notices.visible == [AppNotices.Kind.voiceOver.message]) + #expect(notices.actions == [.useAlternatePrefix]) + #expect(NoticeAction.useAlternatePrefix.title == "Use ⌥⌘") + notices.clear(.voiceOver) + notices.report(InputError.tapTimedOut, kind: .tapStalls) + #expect(notices.visible == [AppNotices.Kind.tapStalls.message]) + #expect(notices.actions.isEmpty) + #expect(!notices.visible.joined().contains("Accessibility")) + #expect(!notices.visible.joined().contains("stopped")) + } + + @Test func startupFailureOffersRetryWithoutClaimingAccessibilityIsMissing() { + let notices = AppNotices() + notices.report(TourSetupFailure.unavailable, kind: .startup) + #expect(notices.visible == [AppNotices.Kind.startup.message]) + #expect(notices.actions == [.retryStartup]) + #expect(NoticeAction.retryStartup.title == "Try again") + #expect(!notices.visible.joined().contains("Accessibility")) + } + @Test func ipcMessageIsNotTreatedAsHumanLocalizedCopy() { let notices = AppNotices() notices.report(IPCError(code: "space_command_failed", message: "raw AX failure -25202"), kind: .internalFailure) diff --git a/Tests/ParcelAppTests/TourLifecycleTests.swift b/Tests/ParcelAppTests/TourLifecycleTests.swift index e0f880b3..fef0e578 100644 --- a/Tests/ParcelAppTests/TourLifecycleTests.swift +++ b/Tests/ParcelAppTests/TourLifecycleTests.swift @@ -200,7 +200,8 @@ struct TourLifecycleTests { } } - @Test func alternatePrefixIsSavedAtomicallyAndInheritedByTheTourCommands() async throws { + @Test(arguments: [false, true]) + func alternatePrefixIsSavedAtomicallyForTheTourAndNotice(fromTour: Bool) async throws { let fixture = try TourLifecycleFixture() defer { fixture.remove() } let source = "# Keep this note.\n[general]\nprefix = \"ctrl-alt\"\nlaunch-at-login = false\n" @@ -216,7 +217,11 @@ struct TourLifecycleTests { do { let actions = AppTourIntegration.actions( settings: settings, services: runtime.services, permissions: permissions, loginStatus: { .disabled }) - try await actions.useAlternatePrefix() + if fromTour { + try await actions.useAlternatePrefix() + } else { + try await AppTourIntegration.useAlternatePrefix(settings: settings, services: runtime.services) + } let configuration = settings.configuration #expect(configuration.general.prefix == [.alt, .cmd]) #expect(composition.configuration.general.prefix == [.alt, .cmd])