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
4 changes: 3 additions & 1 deletion Sources/AskKeyAppKit/App/AppDelegate+Approval.swift
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,8 @@ extension AppDelegate {
let groupAllowance = GroupTimedAllowancePresentation(snapshot: pending?.groupAllowanceSnapshot)
let remote = pending.map {
RemoteApprovalPanelState(requestID: $0.requestID,
status: DeviceServicesHost.coordinator?.remoteApprovals[$0.requestID])
status: DeviceServicesHost.coordinator?.remoteApprovals[$0.requestID],
footer: DeviceServicesHost.coordinator?.remoteFooterHint(for: $0.requestID))
}
let panel = Self.makeFrozenApprovalPanel(
FrozenAgentApprovalPrompt(
Expand Down Expand Up @@ -313,6 +314,7 @@ extension AppDelegate {
let coordinator = DeviceServicesHost.coordinator
let step = remote.step(requestEnded: requestEnded, screenUnlocked: screenUnlocked, expired: expired,
now: Date(), status: coordinator?.remoteApprovals[remote.requestID],
footer: coordinator?.remoteFooterHint(for: remote.requestID),
lastDecision: coordinator?.lastRemoteDecision,
applying: coordinator?.isApplyingRemoteDecision(to: remote.requestID) == true)
if step == .close { finish(nil) }
Expand Down
37 changes: 29 additions & 8 deletions Sources/AskKeyAppKit/App/FrozenAgentApprovalPrompt.swift
Original file line number Diff line number Diff line change
Expand Up @@ -278,18 +278,39 @@ 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.
private var footer: some View {
HStack(spacing: Theme.Spacing.xs) {
TimelineView(.periodic(from: .now, by: 1)) { context in
Text(appLocalizedFormat("Expires in %@", FrozenCountdown.format(deadline: expiresAt, now: context.date)))
.monospacedDigit()
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
}
}
Text(verbatim: "·")
Button(appLocalized("Esc to decide later")) { finish(nil) }
.buttonStyle(.plain)
.keyboardShortcut(.cancelAction)
.accessibilityElement(children: .ignore)
.accessibilityLabel(Text(verbatim: RemoteApprovalFooter.sentence(text)))
.accessibilityAddTraits(.isButton)
.accessibilityAction { finish(nil) }
}
.font(Theme.Fonts.caption)
.foregroundStyle(Theme.textTertiary)
}

private var decideLaterButton: some View {
Button(appLocalized("Esc to decide later")) { finish(nil) }
.buttonStyle(.plain)
.keyboardShortcut(.cancelAction)
}
}
94 changes: 89 additions & 5 deletions Sources/AskKeyAppKit/App/RemoteApprovalPanelState.swift
Original file line number Diff line number Diff line change
@@ -1,10 +1,84 @@
import Foundation
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.
enum RemoteApprovalFooterHint: Equatable {
/// An eligible request waits for its 20-second send to an approver with
/// room under its limit.
case waiting(sendAt: Date)
/// A copy saved to an approver's inbox is still outstanding.
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.
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 }
// "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",
count: seconds, language: AppLanguage.current)
case .sent:
return appLocalizedFormat("Also on iPhone · Expires in %@", expiry)
case nil:
break
}
return appLocalizedFormat("Expires in %@", expiry)
}

/// 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")
}

/// 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.
static func secondsUntilSend(_ sendAt: Date, now: Date) -> Int? {
let remaining = sendAt.timeIntervalSince(now)
guard remaining > 0 else { return nil }
return min(Int(remaining.rounded(.up)), Int(DeviceCoordinator.remoteSendDelay))
}

/// The moment the footer's number counts toward.
static func anchor(_ hint: RemoteApprovalFooterHint?, expiresAt: Date?) -> Date? {
if case .waiting(let sendAt) = hint { return sendAt }
return expiresAt
}
}

/// Ticks once a second, just after each whole second before `anchor`, so
/// the footer's number changes when it should and not up to a second late.
struct RemoteApprovalFooterSchedule: TimelineSchedule {
/// How long after a whole second each tick falls, so rounding the
/// remaining time never lands on the boundary itself.
static let offset: TimeInterval = 0.01
let anchor: Date?

/// The first entry is the latest tick at or before `startDate`.
func entries(from startDate: Date, mode: TimelineScheduleMode) -> AnyIterator<Date> {
let base = (anchor ?? startDate).addingTimeInterval(Self.offset)
var step = startDate.timeIntervalSince(base).rounded(.down)
return AnyIterator {
defer { step += 1 }
return base.addingTimeInterval(step)
}
}
}

/// What the open approval panel shows about a paired iPhone (ADR 0014 → Mac
/// App): where its request went, for Details, and the approver's decision
/// once it applied. The panel's privacy check feeds it the coordinator's
/// state on every tick and closes the panel when `step` says so.
/// App): where its request went, for Details, what the footer says about
/// the approvers, and the approver's decision once it applied. The panel's
/// privacy check feeds it the coordinator's state on every tick and closes
/// the panel when `step` says so.
@Observable
final class RemoteApprovalPanelState {
/// How long "Approved on" or "Denied on" stays on an unlocked screen.
Expand All @@ -22,31 +96,41 @@ final class RemoteApprovalPanelState {
/// The approvers the request went to; kept after the request left
/// pending, when the coordinator drops it.
private(set) var status: RemoteApprovalStatus?
/// What the footer says about the approvers; nil once the request left
/// pending or an approver's decision was recorded.
private(set) var footer: RemoteApprovalFooterHint?
/// The approver's decision that moved this request out of pending.
private(set) var outcome: RemoteAppliedDecision?
@ObservationIgnored private var outcomeShownAt: Date?
@ObservationIgnored private var waitingSince: Date?

init(requestID: String, status: RemoteApprovalStatus? = nil) {
init(requestID: String, status: RemoteApprovalStatus? = nil, footer: RemoteApprovalFooterHint? = nil) {
self.requestID = requestID
self.status = status
self.footer = footer
}

/// 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.
func record(_ decision: RemoteAppliedDecision?, now: Date) {
guard outcome == nil, let decision, decision.requestID == requestID else { return }
outcome = decision
outcomeShownAt = now
footer = nil
}

/// One privacy check. A locked screen closes the panel at once; an
/// approver's decision stays for `outcomeDuration`; a request that ended
/// any other way closes the panel, after at most `applyingGrace` while a
/// decision for it is being applied.
func step(requestEnded: Bool, screenUnlocked: Bool, expired: Bool, now: Date,
status: RemoteApprovalStatus?, lastDecision: RemoteAppliedDecision?, applying: Bool) -> Step {
status: RemoteApprovalStatus?, footer: RemoteApprovalFooterHint? = nil,
lastDecision: RemoteAppliedDecision?, applying: Bool) -> Step {
if !requestEnded, status != self.status { self.status = status }
record(lastDecision, now: now)
let footer = requestEnded || outcome != nil ? nil : footer
if footer != self.footer { self.footer = footer }
guard screenUnlocked else { return .close }
if let shownAt = outcomeShownAt {
return now.timeIntervalSince(shownAt) >= Self.outcomeDuration ? .close : .keepOpen
Expand Down
24 changes: 24 additions & 0 deletions Sources/AskKeyAppKit/Devices/DeviceCoordinator+Presentation.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,28 @@ extension DeviceCoordinator {
func wasSentToApprover(_ requestID: String) -> Bool {
remoteApprovals[requestID]?.approvers.contains { $0.sentAt != nil } == true
}

/// What the approval panel's footer says about the approvers (ADR 0014 →
/// Mac App, amended by #345), from the same state the sending follows:
/// `.waiting` while the request waits for its 20-second send with the
/// send time ahead, before its deadline, still eligible and with an
/// active approver below its outstanding limit; `.sent` while a copy
/// saved to an approver's inbox (Details' "Sent to") is undecided and
/// open; nil otherwise, including while sealing, while a first send
/// retries, when every approver is full and once every copy closed.
func remoteFooterHint(for requestID: String) -> RemoteApprovalFooterHint? {
guard let track = remoteRequests[requestID], track.ended == nil else { return nil }
switch track.phase {
case .waiting(let sendAt):
guard sendAt > now, let deadline = track.deadline, deadline > sendAt, remoteSendingAllowed,
activeApprovers(in: listedDevices).contains(where: {
outstandingCount(for: $0.device.deviceID) < Self.maximumOutstandingPerApprover
}) else { return nil }
return .waiting(sendAt: sendAt)
case .sent:
return track.entries.contains { $0.sentAt != nil && !$0.decided } ? .sent : nil
case .sending, .stayingOnMac:
return nil
}
}
}
46 changes: 46 additions & 0 deletions Sources/AskKeyAppKit/Resources/Localizable.xcstrings
Original file line number Diff line number Diff line change
Expand Up @@ -16041,6 +16041,52 @@
}
}
},
"iPhone can decide in %lld s": {
"extractionState": "manual",
"localizations": {
"en": {
"stringUnit": {
"state": "translated",
"value": "iPhone can decide in %lld s"
}
},
"zh-Hans": {
"stringUnit": {
"state": "translated",
"value": "%lld 秒后 iPhone 也能处理"
}
},
"qps-ploc": {
"stringUnit": {
"state": "translated",
"value": "[!!iPhone can decide in %lld s!!]"
}
}
}
},
"Also on iPhone · Expires in %@": {
"extractionState": "manual",
"localizations": {
"en": {
"stringUnit": {
"state": "translated",
"value": "Also on iPhone · Expires in %@"
}
},
"zh-Hans": {
"stringUnit": {
"state": "translated",
"value": "iPhone 也能处理 · %@ 后失效"
}
},
"qps-ploc": {
"stringUnit": {
"state": "translated",
"value": "[!!Also on iPhone · Expires in %@!!]"
}
}
}
},
"If verification fails, Ask Key restores the original settings.": {
"extractionState": "extracted_with_value",
"localizations": {
Expand Down
Loading
Loading