Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 33 additions & 2 deletions Sources/AskKeyAppKit/AgentApprovalPanelFactory.swift
Original file line number Diff line number Diff line change
@@ -1,9 +1,40 @@
import AppKit

/// The approval panel's window. Esc decides later with no control on the
/// card (#356): every key event AppKit delivers to the panel passes through
/// `sendEvent(_:)`, whichever view is first responder and whether or not
/// the app is active, so the panel takes Esc there and hands it to `cancel`.
/// Holding Esc decides once: whichever approval panel is key swallows the
/// repeats, so they never decide the next request. A cancel that reaches the
/// panel through the
/// responder chain (AppKit sends one for Command-period or Esc with a
/// modifier while the app is active) ends in `cancelOperation(_:)` and
/// decides later too.
final class AgentApprovalPanel: NSPanel {
var cancel: (@MainActor () -> Void)?

override func sendEvent(_ event: NSEvent) {
guard let cancel, Self.isEscape(event) else { return super.sendEvent(event) }
if !event.isARepeat { cancel() }
}

override func cancelOperation(_ sender: Any?) {
guard let cancel else { return super.cancelOperation(sender) }
cancel()
}

/// Esc without Command, Control, Option or Shift, as the card's former
/// `.cancelAction` shortcut matched it.
static func isEscape(_ event: NSEvent) -> Bool {
event.type == .keyDown && event.keyCode == 53 // kVK_Escape
&& event.modifierFlags.intersection([.command, .control, .option, .shift]).isEmpty
}
}

@MainActor
enum AgentApprovalPanelFactory {
static func make(contentSize: NSSize) -> NSPanel {
let panel = NSPanel(
static func make(contentSize: NSSize) -> AgentApprovalPanel {
let panel = AgentApprovalPanel(
contentRect: NSRect(origin: .zero, size: contentSize),
// Non-activating: the panel takes keyboard focus without bringing
// Ask Key, and any open management window, to the front.
Expand Down
3 changes: 3 additions & 0 deletions Sources/AskKeyAppKit/App/AppDelegate+Approval.swift
Original file line number Diff line number Diff line change
Expand Up @@ -377,6 +377,9 @@ extension AppDelegate {
let hosting = NSHostingController(rootView: AgentApprovalPanelContent(prompt: prompt, layout: layout))
hosting.sizingOptions = [.preferredContentSize]
panel.contentViewController = hosting
// Esc decides later (#356). The panel reaches the card through its
// content, so it never keeps the card's decision closure alive.
panel.cancel = { [weak hosting] in hosting?.rootView.prompt.finish(nil) }
layout.start()
return panel
}
Expand Down
34 changes: 9 additions & 25 deletions Sources/AskKeyAppKit/App/FrozenAgentApprovalPrompt.swift
Original file line number Diff line number Diff line change
Expand Up @@ -280,37 +280,21 @@ struct FrozenAgentApprovalPrompt: View {

/// One moving number at a time: the send countdown while the request
/// waits for a paired iPhone, otherwise the expiry (ADR 0014 → Mac App).
/// A footer too long for one line, as the English "Also on iPhone" form
/// is, puts the text and "Esc to decide later" on a line each, so
/// neither breaks. VoiceOver reads it as one sentence and decides later
/// when it is activated; mouse and Esc work as before.
/// The footer shows only that sentence (#356); Esc decides later through
/// the panel (`AgentApprovalPanel`), and VoiceOver offers it as a named
/// action on the footer. A sentence too long for one line wraps.
private var footer: some View {
let hint = remote?.footer
return TimelineView(RemoteApprovalFooterSchedule(anchor: RemoteApprovalFooter.anchor(hint, expiresAt: expiresAt))) { context in
let text = RemoteApprovalFooter.text(hint, expiresAt: expiresAt, now: context.date)
ViewThatFits(in: .horizontal) {
HStack(spacing: Theme.Spacing.xs) {
Text(text).monospacedDigit()
Text(verbatim: "·")
decideLaterButton
}
VStack(spacing: Theme.Spacing.xs) {
Text(text).monospacedDigit()
decideLaterButton
}
}
.accessibilityElement(children: .ignore)
.accessibilityLabel(Text(verbatim: RemoteApprovalFooter.sentence(text)))
.accessibilityAddTraits(.isButton)
.accessibilityAction { finish(nil) }
Text(verbatim: text)
.monospacedDigit()
.multilineTextAlignment(.center)
.fixedSize(horizontal: false, vertical: true)
.accessibilityAction(named: Text(verbatim: RemoteApprovalFooter.decideLaterAction)) { finish(nil) }
.accessibilityIdentifier("approval-footer")
}
.font(Theme.Fonts.caption)
.foregroundStyle(Theme.textTertiary)
}

private var decideLaterButton: some View {
Button(appLocalized("Esc to decide later")) { finish(nil) }
.buttonStyle(.plain)
.keyboardShortcut(.cancelAction)
}
}
37 changes: 15 additions & 22 deletions Sources/AskKeyAppKit/App/RemoteApprovalPanelState.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import Observation
import SwiftUI

/// What the approval panel's footer says about the approvers (ADR 0014 →
/// Mac App, amended by #345). Without a hint the footer is unchanged.
/// Mac App, amended by #345). Without a hint the footer shows the expiry.
enum RemoteApprovalFooterHint: Equatable {
/// An eligible request waits for its 20-second send to an approver with
/// room under its limit.
Expand All @@ -12,36 +12,29 @@ enum RemoteApprovalFooterHint: Equatable {
case sent
}

/// The footer's text before "Esc to decide later": one moving number at a
/// time (#344, decision 6). The send countdown takes the expiry's place
/// while the request waits; the expiry returns once a copy was saved.
/// The footer's text: one moving number at a time, in the maintainer's
/// wording (#356, replacing #344 decision 6's). The send countdown takes the
/// expiry's place while the request waits; the expiry returns once the
/// send is due, worded the same whether or not a copy was saved.
enum RemoteApprovalFooter {
static func text(_ hint: RemoteApprovalFooterHint?, expiresAt: Date?, now: Date) -> String {
let expiry = FrozenCountdown.format(deadline: expiresAt, now: now)
switch hint {
case .waiting(let sendAt):
guard let seconds = secondsUntilSend(sendAt, now: now) else { break }
if case .waiting(let sendAt) = hint, let seconds = secondsUntilSend(sendAt, now: now) {
// "s" inflects in neither catalog language; the count still goes
// through the plural lookup, so a language that inflects can add
// plural variations to this key.
return AppLanguage.localizedCount("iPhone can decide in %lld s", oneKey: "iPhone can decide in %lld s",
return AppLanguage.localizedCount("This request will be sent to iPhone in %lld s",
oneKey: "This request will be sent to iPhone in %lld s",
count: seconds, language: AppLanguage.current)
case .sent:
return appLocalizedFormat("Also on iPhone · Expires in %@", expiry)
case nil:
break
}
return appLocalizedFormat("Expires in %@", expiry)
return appLocalizedFormat("This request expires in %@", FrozenCountdown.format(deadline: expiresAt, now: now))
}

/// The whole footer as one sentence, for VoiceOver: the text, then
/// "Esc to decide later".
static func sentence(_ text: String) -> String {
text + " · " + appLocalized("Esc to decide later")
}
/// The name of the footer's VoiceOver action, which decides later as
/// Esc does.
static var decideLaterAction: String { appLocalized("Decide Later") }

/// Whole seconds left before the send, rounded up, from 20 to 1; nil
/// once the send is due, when the footer is unchanged until it is saved.
/// once the send is due, when the footer shows the expiry.
static func secondsUntilSend(_ sendAt: Date, now: Date) -> Int? {
let remaining = sendAt.timeIntervalSince(now)
guard remaining > 0 else { return nil }
Expand Down Expand Up @@ -111,8 +104,8 @@ final class RemoteApprovalPanelState {
}

/// Records the coordinator's latest decision when it decided this request.
/// The footer's hint goes with it, so "Also on iPhone" never shows next
/// to the approver's decision.
/// The footer's hint goes with it, so a hint about the approvers never
/// shows next to the approver's decision.
func record(_ decision: RemoteAppliedDecision?, now: Date) {
guard outcome == nil, let decision, decision.requestID == requestID else { return }
outcome = decision
Expand Down
24 changes: 12 additions & 12 deletions Sources/AskKeyAppKit/Resources/Localizable.xcstrings
Original file line number Diff line number Diff line change
Expand Up @@ -715,12 +715,12 @@
"qps-ploc": { "stringUnit": { "state": "translated", "value": "[!!Create!!]" } }
}
},
"Esc to decide later": {
"Decide Later": {
"extractionState": "manual",
"localizations": {
"en": { "stringUnit": { "state": "translated", "value": "Esc to decide later" } },
"zh-Hans": { "stringUnit": { "state": "translated", "value": "Esc 稍后处理" } },
"qps-ploc": { "stringUnit": { "state": "translated", "value": "[!!Esc to decide later!!]" } }
"en": { "stringUnit": { "state": "translated", "value": "Decide Later" } },
"zh-Hans": { "stringUnit": { "state": "translated", "value": "稍后处理" } },
"qps-ploc": { "stringUnit": { "state": "translated", "value": "[!!Decide Later!!]" } }
}
},
"Hands over": {
Expand Down Expand Up @@ -16041,48 +16041,48 @@
}
}
},
"iPhone can decide in %lld s": {
"This request will be sent to iPhone in %lld s": {
"extractionState": "manual",
"localizations": {
"en": {
"stringUnit": {
"state": "translated",
"value": "iPhone can decide in %lld s"
"value": "This request will be sent to iPhone in %lld s"
}
},
"zh-Hans": {
"stringUnit": {
"state": "translated",
"value": "%lld 秒后 iPhone 也能处理"
"value": "%lld 秒后将把审批请求发到 iPhone"
}
},
"qps-ploc": {
"stringUnit": {
"state": "translated",
"value": "[!!iPhone can decide in %lld s!!]"
"value": "[!!This request will be sent to iPhone in %lld s!!]"
}
}
}
},
"Also on iPhone · Expires in %@": {
"This request expires in %@": {
"extractionState": "manual",
"localizations": {
"en": {
"stringUnit": {
"state": "translated",
"value": "Also on iPhone · Expires in %@"
"value": "This request expires in %@"
}
},
"zh-Hans": {
"stringUnit": {
"state": "translated",
"value": "iPhone 也能处理 · %@ 后失效"
"value": "审批请求将于 %@ 后失效"
}
},
"qps-ploc": {
"stringUnit": {
"state": "translated",
"value": "[!!Also on iPhone · Expires in %@!!]"
"value": "[!!This request expires in %@!!]"
}
}
}
Expand Down
5 changes: 3 additions & 2 deletions Tests/AskKeyAppTests/ApprovalPromptContentTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -153,8 +153,9 @@ final class ApprovalPromptContentTests: AskKeyAppTestCase {

func testFooterInBothLanguages() {
Fixtures.withLanguages { language in
let footer = appLocalizedFormat("Expires in %@", "4:11") + " · " + appLocalized("Esc to decide later")
XCTAssertEqual(footer, language == "en" ? "Expires in 4:11 · Esc to decide later" : "4:11 后失效 · Esc 稍后处理") // i18n-literal: Chinese footer.
let now = Date(timeIntervalSince1970: 2_000_000_000)
let footer = RemoteApprovalFooter.text(nil, expiresAt: now.addingTimeInterval(251), now: now)
XCTAssertEqual(footer, language == "en" ? "This request expires in 4:11" : "审批请求将于 4:11 后失效") // i18n-literal: Chinese footer.
XCTAssertEqual(appLocalized("Details"), language == "en" ? "Details" : "详细信息") // i18n-literal: Chinese Details link.
}
}
Expand Down
20 changes: 10 additions & 10 deletions Tests/AskKeyAppTests/RemoteApprovalFooterHintTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ import XCTest

/// What the coordinator tells the approval panel's footer (ADR 0014 → Mac
/// App, amended by #345): the countdown only while an eligible request waits
/// for an approver with room, "Also on iPhone" only while a saved copy is
/// outstanding, and the unchanged footer in every other case. The footer
/// for an approver with room, the sent hint only while a saved copy is
/// outstanding, and no hint in every other case. The footer
/// only reads; none of these steps changes what is sent.
@MainActor
final class RemoteApprovalFooterHintTests: AskKeyAppTestCase {
Expand Down Expand Up @@ -43,7 +43,7 @@ final class RemoteApprovalFooterHintTests: AskKeyAppTestCase {
await harness.coordinator.remoteApprovalTick()
}

func testTheCountdownRunsUntilTheSendThenTheSavedCopySaysAlsoOnIPhone() async throws {
func testTheCountdownRunsUntilTheSendThenTheSavedCopyGivesTheSentHint() async throws {
let (pending, sendAt) = try await waitingRead()
for _ in 0..<19 {
XCTAssertEqual(hint(pending), .waiting(sendAt: sendAt))
Expand All @@ -54,14 +54,14 @@ final class RemoteApprovalFooterHintTests: AskKeyAppTestCase {
harness.clock.advance(by: 1)
harness.coordinator.reconcileRemoteRequests()
XCTAssertEqual(harness.coordinator.remoteRequests[pending.requestID]?.phase, .sending)
XCTAssertNil(hint(pending), "unchanged while the request is being sealed")
XCTAssertNil(hint(pending), "no hint while the request is being sealed")
try await harness.waitForRequests(1)
try await harness.waitUntil { harness.entries(pending).first?.sentAt != nil }
XCTAssertEqual(hint(pending), .sent)
XCTAssertTrue(harness.coordinator.wasSentToApprover(pending.requestID), "the same saved state as Details' Sent to")
}

func testARequestDecidedOnTheMacWithinTwentySecondsNeverSaysAlsoOnIPhone() async throws {
func testARequestDecidedOnTheMacWithinTwentySecondsNeverGetsTheSentHint() async throws {
let (pending, sendAt) = try await waitingRead()
harness.clock.advance(by: 10)
await harness.coordinator.remoteApprovalTick()
Expand All @@ -78,7 +78,7 @@ final class RemoteApprovalFooterHintTests: AskKeyAppTestCase {
XCTAssertTrue(harness.macSent(.approvalRequest).isEmpty)
}

func testWithoutAnApproverTheFooterIsAlwaysUnchanged() async throws {
func testWithoutAnApproverTheFooterNeverHasAHint() async throws {
try await harness.pairPhone()
await harness.coordinator.removeDevice(harness.phone.deviceID)
try await harness.addUnflaggedEntry(for: harness.phone, name: "Synthetic Phone")
Expand Down Expand Up @@ -126,7 +126,7 @@ final class RemoteApprovalFooterHintTests: AskKeyAppTestCase {
XCTAssertNil(hint(pending), "a request that will not be sealed never promises the iPhone")
}

func testWhenEveryApproverIsFullTheFooterIsUnchangedUntilOneHasRoom() async throws {
func testWhenEveryApproverIsFullTheFooterHasNoHintUntilOneHasRoom() async throws {
try await harness.pairApprover()
var sent: [BrokerPendingApproval] = []
for _ in 0..<DeviceCoordinator.maximumOutstandingPerApprover {
Expand All @@ -146,7 +146,7 @@ final class RemoteApprovalFooterHintTests: AskKeyAppTestCase {
XCTAssertEqual(hint(waiting), .waiting(sendAt: sendAt), "a closed copy makes room")
}

func testAFirstSendThatIsRetriedKeepsTheFooterUnchangedUntilItIsSaved() async throws {
func testAFirstSendThatIsRetriedHasNoHintUntilItIsSaved() async throws {
let (pending, _) = try await waitingRead()
harness.transport.failNextSends(with: [.networkUnavailable])
try await passTheTwentySecondMark()
Expand All @@ -163,7 +163,7 @@ final class RemoteApprovalFooterHintTests: AskKeyAppTestCase {
XCTAssertEqual(hint(pending), .sent)
}

func testOnceEveryCopyClosedTheFooterIsUnchanged() async throws {
func testOnceEveryCopyClosedTheFooterHasNoHint() async throws {
let (pending, _) = try await waitingRead()
let second = SimulatedPairingPhone(transport: harness.transport, clock: harness.clock)
try await harness.pair(second, name: "Synthetic Phone Two")
Expand All @@ -181,7 +181,7 @@ final class RemoteApprovalFooterHintTests: AskKeyAppTestCase {
XCTAssertEqual(try harness.state(pending), .pending, "the request stays on the Mac")
}

func testARequestTheIPhoneDecidedNoLongerSaysAlsoOnIPhone() async throws {
func testARequestTheIPhoneDecidedNoLongerHasTheSentHint() async throws {
let (pending, _) = try await waitingRead()
try await passTheTwentySecondMark()
try await harness.waitForRequests(1)
Expand Down
Loading
Loading