mirror of
https://github.com/shadowsocks/ShadowsocksX-NG.git
synced 2026-11-23 06:14:06 +00:00
fix(window): stop policy bouncing during launch activation
Keep regular policy while windows remain open to avoid repeated Dock registration. Share the activation retry budget across visibility events and stop permanently after exhaustion.
This commit is contained in:
@@ -123,6 +123,7 @@ struct ShadowsocksXNG2App: App {
|
||||
@MainActor
|
||||
private final class NSAppWindowActivationApplier: WindowActivationPolicyApplying {
|
||||
func apply(_ policy: NSApplication.ActivationPolicy) {
|
||||
guard NSApp.activationPolicy() != policy else { return }
|
||||
NSApp.setActivationPolicy(policy)
|
||||
if policy == .regular {
|
||||
// Window scene 会呈现窗口,但 LSUIElement 进程仍需显式激活才能置前。
|
||||
|
||||
@@ -49,13 +49,11 @@ final class WindowActivationPolicyCoordinator: ObservableObject {
|
||||
private let applier: any WindowActivationPolicyApplying
|
||||
private let activationRetryInterval: TimeInterval
|
||||
private let activationRetryLimit: Int
|
||||
private let policySettleInterval: TimeInterval
|
||||
private let activationSettleInterval: TimeInterval
|
||||
private var activationAttemptsLeft: Int
|
||||
private let launchRescanInterval: TimeInterval
|
||||
private let tickScheduler: any ActivationTickScheduling
|
||||
/// 激活已真正收敛(双信号齐备)后的永久闩:此后任何事件不再重启链,
|
||||
/// 避免会话中抢焦点。
|
||||
private var activationSettled = false
|
||||
/// 激活收敛或预算用尽后永久停止,避免窗口事件重启重试并抢焦点。
|
||||
private var activationFinished = false
|
||||
/// 重试链是否在调度中。链可能在窗口尚未可见时静默终结(key 先于可见的
|
||||
/// 呈现次序),未收敛前由参与事件重新拉起(re-arm)。
|
||||
private var isRetryChainActive = false
|
||||
@@ -66,16 +64,13 @@ final class WindowActivationPolicyCoordinator: ObservableObject {
|
||||
applying applier: any WindowActivationPolicyApplying,
|
||||
activationRetryInterval: TimeInterval = 0.2,
|
||||
activationRetryLimit: Int = 5,
|
||||
policySettleInterval: TimeInterval = 0.1,
|
||||
activationSettleInterval: TimeInterval = 0.15,
|
||||
launchRescanInterval: TimeInterval = 0.5,
|
||||
tickScheduler: (any ActivationTickScheduling)? = nil
|
||||
) {
|
||||
self.applier = applier
|
||||
self.activationRetryInterval = activationRetryInterval
|
||||
self.activationRetryLimit = activationRetryLimit
|
||||
self.policySettleInterval = policySettleInterval
|
||||
self.activationSettleInterval = activationSettleInterval
|
||||
self.activationAttemptsLeft = activationRetryLimit
|
||||
self.launchRescanInterval = launchRescanInterval
|
||||
self.tickScheduler = tickScheduler ?? TaskActivationTickScheduler()
|
||||
let center = NotificationCenter.default
|
||||
@@ -121,7 +116,7 @@ final class WindowActivationPolicyCoordinator: ObservableObject {
|
||||
/// 自然不触发。
|
||||
private func scheduleLaunchVisibilityRescan() {
|
||||
tickScheduler.scheduleTick(after: launchRescanInterval) { [weak self] in
|
||||
guard let self, !self.activationSettled, !self.isRetryChainActive else { return }
|
||||
guard let self, !self.activationFinished, !self.isRetryChainActive else { return }
|
||||
guard let window = self.anchoredWindows.allObjects.first(where: { $0.isVisible })
|
||||
else { return }
|
||||
if self.openWindows.insert(ObjectIdentifier(window)).inserted {
|
||||
@@ -165,29 +160,27 @@ final class WindowActivationPolicyCoordinator: ObservableObject {
|
||||
startLaunchActivationRetry(for: window)
|
||||
}
|
||||
|
||||
/// 启动激活重试(未收敛前可 re-arm):首拍 activate 是否被焦点抢占保护
|
||||
/// 忽略随当时前台应用而变;短窗口内轮询双信号(激活 + 菜单栏归属),
|
||||
/// 不齐则重试 activate 并置前,收敛后永久闩死、次数用尽或窗口关闭则
|
||||
/// 结束本链(后续参与事件可再拉起,直到收敛)。双击启动首拍即收敛。
|
||||
/// 预算在进程内共享;窗口尚未可见时允许后续事件续排,但不重置预算。
|
||||
/// 重试仅激活并置前,不切换策略,避免反复向 Dock 注册应用。
|
||||
private func startLaunchActivationRetry(for window: NSWindow) {
|
||||
guard !activationSettled, !isRetryChainActive else { return }
|
||||
guard !activationFinished, !isRetryChainActive else { return }
|
||||
isRetryChainActive = true
|
||||
scheduleActivationRetryTick(for: window, attemptsLeft: activationRetryLimit)
|
||||
scheduleActivationRetryTick(for: window)
|
||||
}
|
||||
|
||||
private func endRetryChain() {
|
||||
isRetryChainActive = false
|
||||
}
|
||||
|
||||
private func scheduleActivationRetryTick(for window: NSWindow, attemptsLeft: Int) {
|
||||
private func scheduleActivationRetryTick(for window: NSWindow) {
|
||||
tickScheduler.scheduleTick(after: activationRetryInterval) { [weak self] in
|
||||
self?.runActivationRetryTick(for: window, attemptsLeft: attemptsLeft)
|
||||
self?.runActivationRetryTick(for: window)
|
||||
}
|
||||
}
|
||||
|
||||
private func runActivationRetryTick(for window: NSWindow, attemptsLeft: Int) {
|
||||
private func runActivationRetryTick(for window: NSWindow) {
|
||||
let visible = window.isVisible
|
||||
let attempt = activationRetryLimit - attemptsLeft + 1
|
||||
let attempt = activationRetryLimit - activationAttemptsLeft + 1
|
||||
let menuBarOwner = NSWorkspace.shared.menuBarOwningApplication?.bundleIdentifier ?? "-"
|
||||
let active = applier.isAppActive
|
||||
RuntimeLog.emit(
|
||||
@@ -197,61 +190,19 @@ final class WindowActivationPolicyCoordinator: ObservableObject {
|
||||
menuBarOwner: menuBarOwner, onActiveSpace: window.isOnActiveSpace,
|
||||
windowVisible: visible))
|
||||
guard visible else {
|
||||
// 窗口尚未可见(key 先行的呈现次序)或已关闭:结束本链留待可见化
|
||||
// 事件或兜底重扫 re-arm。
|
||||
endRetryChain()
|
||||
return
|
||||
}
|
||||
// 双信号收敛判据:isActive 可能为真而菜单栏实际仍归此前台应用
|
||||
// (注册滞后的真相),此时继续重试——重置窗口置前本身就能盖回
|
||||
// 可见层,后续弹跳再补 Dock 图标注册。
|
||||
if applier.isAppActive && applier.ownsMenuBar {
|
||||
activationSettled = true
|
||||
if (applier.isAppActive && applier.ownsMenuBar) || activationAttemptsLeft == 0 {
|
||||
activationFinished = true
|
||||
endRetryChain()
|
||||
return
|
||||
}
|
||||
guard attemptsLeft > 0 else {
|
||||
endRetryChain()
|
||||
return
|
||||
}
|
||||
if attempt == 1 {
|
||||
// 首拍仍用普通 activate(重开等点击上下文里足以生效,避免无谓的
|
||||
// Dock 图标闪烁)。
|
||||
applier.activateApp()
|
||||
window.makeKeyAndOrderFront(nil)
|
||||
window.orderFrontRegardless()
|
||||
scheduleActivationRetryTick(for: window, attemptsLeft: attemptsLeft - 1)
|
||||
} else {
|
||||
scheduleForcedActivationSequence(for: window, attemptsLeft: attemptsLeft - 1)
|
||||
}
|
||||
}
|
||||
|
||||
/// 强制激活序列(跨拍沉淀):失败场景实测 Dock 图标从未出现、进调度
|
||||
/// 中心才刷新——启动早期的 regular 策略切换未注册到 WindowServer/Dock,
|
||||
/// activate 因此全程被拒。同拍 accessory→regular 弹跳会被系统合并吞掉,
|
||||
/// 必须拆成有真实间隔的三跳:降 accessory → 沉淀 → 回 regular(迫使
|
||||
/// 重新注册)→ 沉淀 → 激活置前。末跳续排主链保持单线调度。
|
||||
private func scheduleForcedActivationSequence(for window: NSWindow, attemptsLeft: Int) {
|
||||
applier.apply(.accessory)
|
||||
tickScheduler.scheduleTick(after: policySettleInterval) { [weak self] in
|
||||
guard let self, window.isVisible else {
|
||||
self?.endRetryChain()
|
||||
return
|
||||
}
|
||||
self.applier.apply(.regular)
|
||||
self.tickScheduler.scheduleTick(after: self.activationSettleInterval) { [weak self] in
|
||||
guard let self, window.isVisible else {
|
||||
self?.endRetryChain()
|
||||
return
|
||||
}
|
||||
if !(self.applier.isAppActive && self.applier.ownsMenuBar) {
|
||||
self.applier.activateApp()
|
||||
window.makeKeyAndOrderFront(nil)
|
||||
window.orderFrontRegardless()
|
||||
}
|
||||
self.scheduleActivationRetryTick(for: window, attemptsLeft: attemptsLeft)
|
||||
}
|
||||
}
|
||||
activationAttemptsLeft -= 1
|
||||
applier.activateApp()
|
||||
window.makeKeyAndOrderFront(nil)
|
||||
window.orderFrontRegardless()
|
||||
scheduleActivationRetryTick(for: window)
|
||||
}
|
||||
|
||||
private func trackedWindow(in notification: Notification) -> NSWindow? {
|
||||
|
||||
@@ -306,6 +306,26 @@ private final class VisibilityWindow: NSWindow {
|
||||
}
|
||||
|
||||
extension WindowActivationPolicyTests {
|
||||
func testMenuBarMismatchNeverBouncesPolicyOrRestartsExhaustedRetries() {
|
||||
spy.isActive = true
|
||||
spy.ownsMenuBar = false
|
||||
let visible = VisibilityWindow(
|
||||
contentRect: .zero, styleMask: [.titled], backing: .buffered, defer: true)
|
||||
visible.pretendVisible = true
|
||||
coordinator.anchorDidAttach(to: visible)
|
||||
|
||||
while tickScheduler.drainOne() {}
|
||||
|
||||
XCTAssertFalse(spy.applied.contains(.accessory), "窗口仍打开时不得通过策略弹跳反复注册 Dock 图标")
|
||||
let activationCalls = spy.activationCalls
|
||||
for _ in 0..<3 {
|
||||
post(NSWindow.didBecomeKeyNotification, on: visible)
|
||||
post(NSWindow.didChangeOcclusionStateNotification, on: visible)
|
||||
while tickScheduler.drainOne() {}
|
||||
}
|
||||
XCTAssertEqual(spy.activationCalls, activationCalls, "重试用尽后窗口事件不得重置预算并抢焦点")
|
||||
}
|
||||
|
||||
func testLaunchRetryReactivatesWhileInactiveAndStopsAtLimit() {
|
||||
spy.isActive = false
|
||||
let visible = VisibilityWindow(
|
||||
@@ -316,16 +336,9 @@ extension WindowActivationPolicyTests {
|
||||
XCTAssertEqual(tickScheduler.pending.count, 1)
|
||||
tickScheduler.drainOne()
|
||||
XCTAssertEqual(spy.activationCalls, 1)
|
||||
XCTAssertEqual(
|
||||
spy.applied.filter { $0 == .accessory }, [], "首拍必须先走普通 activate,不弹跳")
|
||||
while tickScheduler.drainOne() {}
|
||||
XCTAssertEqual(spy.activationCalls, 5, "默认上限 5 次激活(1 普通 + 4 次弹跳末跳)")
|
||||
XCTAssertEqual(
|
||||
spy.applied.filter { $0 == .accessory }.count, 4,
|
||||
"第二拍起每次强制激活都要降一次 accessory")
|
||||
XCTAssertEqual(
|
||||
spy.applied.filter { $0 == .regular }.count, 5,
|
||||
"attach 一次 regular + 每次弹跳回一次 regular")
|
||||
XCTAssertEqual(spy.activationCalls, 5, "默认上限 5 次激活")
|
||||
XCTAssertEqual(spy.applied, [.regular], "重试只激活,不改变随窗策略")
|
||||
XCTAssertEqual(visible.orderFrontCalls, 6, "attach 置前一次,每次激活后再置前")
|
||||
XCTAssertEqual(visible.orderRegardlessCalls, 5)
|
||||
XCTAssertTrue(tickScheduler.pending.isEmpty, "终态检查拍后不得再排")
|
||||
@@ -342,7 +355,7 @@ extension WindowActivationPolicyTests {
|
||||
while tickScheduler.drainOne() {}
|
||||
|
||||
XCTAssertEqual(spy.activationCalls, 2, "已激活后的检查不得再激活")
|
||||
XCTAssertEqual(spy.applied.suffix(2), [.accessory, .regular], "弹跳序列在激活成功处收敛")
|
||||
XCTAssertEqual(spy.applied, [.regular], "激活重试不得弹跳策略")
|
||||
XCTAssertTrue(tickScheduler.pending.isEmpty, "已激活后不得再排下一拍")
|
||||
}
|
||||
|
||||
@@ -360,7 +373,7 @@ extension WindowActivationPolicyTests {
|
||||
XCTAssertTrue(tickScheduler.pending.isEmpty)
|
||||
}
|
||||
|
||||
func testForcedSequenceAbortsWhenWindowClosesMidBounce() {
|
||||
func testLaunchRetryPreservesBudgetAcrossVisibilityChanges() {
|
||||
spy.isActive = false
|
||||
let visible = VisibilityWindow(
|
||||
contentRect: .zero, styleMask: [.titled], backing: .buffered, defer: true)
|
||||
@@ -368,13 +381,19 @@ extension WindowActivationPolicyTests {
|
||||
coordinator.anchorDidAttach(to: visible)
|
||||
tickScheduler.drainOne()
|
||||
tickScheduler.drainOne()
|
||||
XCTAssertEqual(spy.applied, [.regular, .accessory], "第二拍已降级 accessory 待弹跳")
|
||||
XCTAssertEqual(spy.activationCalls, 2)
|
||||
visible.pretendVisible = false
|
||||
|
||||
while tickScheduler.drainOne() {}
|
||||
|
||||
XCTAssertEqual(spy.activationCalls, 1, "窗口关闭后弹跳末跳不得再激活")
|
||||
XCTAssertEqual(
|
||||
spy.applied, [.regular, .accessory], "中途关闭后不得再回 regular")
|
||||
XCTAssertEqual(spy.activationCalls, 2, "窗口不可见时不得再激活")
|
||||
XCTAssertEqual(spy.applied, [.regular])
|
||||
|
||||
visible.pretendVisible = true
|
||||
post(NSWindow.didChangeOcclusionStateNotification, on: visible)
|
||||
while tickScheduler.drainOne() {}
|
||||
|
||||
XCTAssertEqual(spy.activationCalls, 5, "恢复可见后仅消耗剩余 3 次预算")
|
||||
XCTAssertEqual(spy.applied, [.regular])
|
||||
}
|
||||
}
|
||||
|
||||
@@ -54,3 +54,9 @@ GUI 日志的时序定位到启动路径的真正缺陷:重试链从未在启
|
||||
## 2026-10-24 补充:应用菜单诊断入口
|
||||
|
||||
regular 形态的默认菜单栏在应用名称菜单(`CommandGroup(after: .appInfo)`)加入“诊断…”命令,与状态菜单入口同开唯一诊断窗口;应用处于 accessory 形态(无受管窗口)时不出现该菜单栏,入口自然缺席。诊断命令实现为独立视图文件(`App/UI/Diagnostics/DiagnosticsCommands.swift`)。同日移除按源码扫描 openWindow 调用点的守卫(原为文件豁免登记制):豁免清单的维护成本高于其拦截价值。开窗纪律改由事实保证——全部 `Window` scene 集中在组合根(MainApp)一处声明,窗口激活协调只经 `.modifier(WindowActivationPolicy(...))` 挂在这些受管 scene 上。
|
||||
|
||||
## 2026-10-25 补充:取消启动策略弹跳并限制重试预算
|
||||
|
||||
用户报告启动重试期间 Dock 出现多个重复应用图标,日志显示菜单栏仍归 Xcode、重试多轮从 #1 重新开始。此前通过 accessory → regular 弹跳补注册的方案撤回:窗口打开期间保持 regular,重试只调用 activate、makeKeyAndOrderFront 和 orderFrontRegardless,不再反复触发 Dock 注册。真实 AppKit 落点仅在策略值变化时调用 setActivationPolicy。
|
||||
|
||||
默认 5 次激活预算由协调器在进程内共享。窗口尚未可见或暂时不可见时结束当前链,后续窗口事件可续排剩余预算;双信号收敛或预算用尽后永久停止重试,避免开诊断窗口、切换焦点或可见化事件重新启动整轮。0.5 秒启动可见性重扫、双信号收敛检查及末拍诊断日志保留。系统拒绝协作式激活时,仍通过 orderFrontRegardless 抬窗,用户点击窗口完成激活;不再以反复改变应用形态强制争取焦点。
|
||||
|
||||
Reference in New Issue
Block a user