Skip to content

Commit d6856c8

Browse files
fix: keep FileMint in the background at login
1 parent 569715b commit d6856c8

9 files changed

Lines changed: 55 additions & 12 deletions

File tree

‎App/FileMint/AppDelegate.swift‎

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,16 @@ import FileMintCore
44
@MainActor
55
final class AppDelegate: NSObject, NSApplicationDelegate {
66
func applicationDidFinishLaunching(_ notification: Notification) {
7+
let launchEvent = NSAppleEventManager.shared().currentAppleEvent
78
PreferencesModel.shared.startGlobalShortcut()
89
UpdateModel.shared.startAutomaticChecks()
910
BatchRenameOperations.shared.refreshStatus()
1011
if #available(macOS 13.3, *) {
1112
FileMintLauncherShortcuts.updateAppShortcutParameters()
1213
}
13-
let activationExpected = NSAppleEventManager.shared().currentAppleEvent?
14-
.paramDescriptor(forKeyword: AEKeyword(kAEApplicationActivationExpected))?.booleanValue == true
1514
PreferencesModel.shared.externalInvocations.handleOrdinaryLaunch(
1615
isDefault: notification.userInfo?[NSApplication.launchIsDefaultUserInfoKey] as? Bool == true,
17-
activationExpected: activationExpected)
16+
event: launchEvent)
1817
Task { await PreferencesModel.shared.prepareLoginItemIfNeeded() }
1918
}
2019

‎App/FileMint/DesignSystem.swift‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -263,7 +263,7 @@ enum InterfaceText {
263263
case parameters, keepOriginal, previewMissing, allImages, outputSize, start, more, chooseImages
264264
case resourceSubtitle, generalSubtitle, creationSubtitle, typesSubtitle, foldersSubtitle, filesSubtitle
265265
case interface, enabledFinder, disabledFinder, selectedImages, textResult, reduceWithoutUpscale
266-
case busy, pixelWidth, pixelHeight, fitting, inFinder, launchHint, menuBarHint, menuEnabled, prepared
266+
case busy, pixelWidth, pixelHeight, fitting, inFinder, menuBarHint, menuEnabled, prepared
267267
func text(_ language: AppLanguage) -> String {
268268
let pair: (String, String) = switch self {
269269
case .fileCreation: ("File Creation", "文件创建")
@@ -299,7 +299,6 @@ enum InterfaceText {
299299
case .pixelHeight: ("Common height (px)", "统一高度(像素)")
300300
case .fitting: ("Centered on a transparent square", "居中适配透明方形")
301301
case .inFinder: ("Show in Finder", "在 Finder 中显示")
302-
case .launchHint: ("Ready for Finder actions after login.", "登录后即可使用 Finder 右键功能。")
303302
case .menuBarHint: ("Quick access to creation and settings.", "随时新建文件,快速打开设置。")
304303
case .menuEnabled: ("Show Resource Tools in Finder", "在 Finder 中显示资源工具")
305304
case .prepared: ("Ready when you are.", "准备好了,随手新建。")

‎App/FileMint/ExternalInvocationCoordinator.swift‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -272,8 +272,8 @@ final class ExternalInvocationCoordinator: ObservableObject {
272272
}
273273
}
274274

275-
func handleOrdinaryLaunch(isDefault: Bool, activationExpected: Bool) {
276-
guard isDefault, activationExpected else { return }
275+
func handleOrdinaryLaunch(isDefault: Bool, event: NSAppleEventDescriptor?) {
276+
guard LoginItemPolicy.shouldShowSettingsOnLaunch(isDefault: isDefault, event: event) else { return }
277277
handleReopen()
278278
}
279279
func handleReopen() {

‎App/FileMint/SettingsSections.swift‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ struct GeneralPane: View {
6969
}
7070
}
7171
SettingsSection(title: model.text(.startupAndAccess)) {
72-
PreferenceRow(title: model.text(.launchAtLogin), detail: InterfaceText.launchHint.text(model.preferences.language)) {
72+
PreferenceRow(title: model.text(.launchAtLogin), detail: model.text(.launchAtLoginHint)) {
7373
Toggle(model.text(.launchAtLogin), isOn: Binding(
7474
get: { model.preferences.launchAtLogin },
7575
set: { value in Task { await model.setLaunchAtLogin(value) } }

‎CorePackage/Sources/FileMintCore/Localization.swift‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ public enum FileMintTextKey: String, CaseIterable, Sendable {
4242
case tealAccent, indigoAccent, mossAccent, coralAccent, roseAccent, slateAccent
4343
case customAccent, chooseAccentColor, accentHexCode, applyAccentColor
4444
case finderMenuIconStyle, coloredMenuIcons, systemMonochromeMenuIcons, finderMenuIconStyleHint
45-
case launchAtLogin
45+
case launchAtLogin, launchAtLoginHint
4646
case showMenuBar
4747
case automaticallyCheckForUpdates
4848
case automaticUpdateHint
@@ -468,6 +468,10 @@ public enum FileMintStrings {
468468

469469
.followSystem: ("Follow System", "跟随系统"),
470470
.launchAtLogin: ("Launch at login", "开机自动启动"),
471+
.launchAtLoginHint: (
472+
"Start FileMint in the background at login. If turned off, open FileMint manually to use the global shortcuts for File Drawer and Quick Search.",
473+
"登录 macOS 时在后台启动 FileMint。关闭此项后,需先手动打开 FileMint,才能用全局快捷键唤起文件抽屉和快捷搜索。"
474+
),
471475
.showMenuBar: ("Show in menu bar", "显示在菜单栏"),
472476
.automaticallyCheckForUpdates: ("Automatically check for updates", "自动检查更新"),
473477
.automaticUpdateHint: ("While FileMint is running, check for a new version at most once every 7 days. Download and install when you choose.", "FileMint 运行时,每 7 天最多检查一次新版本。下载和安装由你决定。"),

‎CorePackage/Sources/FileMintCore/LoginItemPolicy.swift‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,16 @@ public enum LoginItemState: Sendable {
55
}
66

77
public enum LoginItemPolicy {
8+
public static func shouldShowSettingsOnLaunch(isDefault: Bool, event: NSAppleEventDescriptor?) -> Bool {
9+
guard isDefault, let event,
10+
event.paramDescriptor(forKeyword: AEKeyword(kAEApplicationActivationExpected))?.booleanValue == true
11+
else { return false }
12+
13+
let launchedAtLogin = event.eventID == kAEOpenApplication &&
14+
event.paramDescriptor(forKeyword: keyAEPropData)?.enumCodeValue == keyAELaunchedAsLogInItem
15+
return !launchedAtLogin
16+
}
17+
818
public static func shouldRegisterInitially(wantsEnabled: Bool, attempted: Bool, status: LoginItemState, installed: Bool) -> Bool {
919
installed && wantsEnabled && !attempted && (status == .notRegistered || status == .notFound)
1020
}

‎CorePackage/Tests/FileMintCoreTests/StartupPreferencesTests.swift‎

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -297,6 +297,21 @@ struct StartupPreferencesTests {
297297
#expect(!LoginItemPolicy.shouldRegisterInitially(wantsEnabled: true, attempted: false, status: .notRegistered, installed: false))
298298
}
299299

300+
@Test("login and non-activating launches leave settings closed")
301+
func launchWindowPolicy() {
302+
let event = NSAppleEventDescriptor(eventClass: kCoreEventClass, eventID: kAEOpenApplication,
303+
targetDescriptor: nil, returnID: AEReturnID(kAutoGenerateReturnID), transactionID: AETransactionID(kAnyTransactionID))
304+
#expect(!LoginItemPolicy.shouldShowSettingsOnLaunch(isDefault: true, event: nil))
305+
#expect(!LoginItemPolicy.shouldShowSettingsOnLaunch(isDefault: true, event: event))
306+
307+
event.setParam(NSAppleEventDescriptor(boolean: true), forKeyword: AEKeyword(kAEApplicationActivationExpected))
308+
#expect(LoginItemPolicy.shouldShowSettingsOnLaunch(isDefault: true, event: event))
309+
#expect(!LoginItemPolicy.shouldShowSettingsOnLaunch(isDefault: false, event: event))
310+
311+
event.setParam(NSAppleEventDescriptor(enumCode: keyAELaunchedAsLogInItem), forKeyword: keyAEPropData)
312+
#expect(!LoginItemPolicy.shouldShowSettingsOnLaunch(isDefault: true, event: event))
313+
}
314+
300315
@Test("development and disk-image copies never become automatic login items")
301316
func installLocations() {
302317
let home = URL(fileURLWithPath: "/Users/example")
@@ -342,6 +357,16 @@ struct StartupPreferencesTests {
342357
#expect(FileMintStrings.text(.quickCollisionHint, language: .chinese).contains("替换已有文件前询问"))
343358
}
344359

360+
@Test("launch at login guidance describes background startup and manual launch for both global shortcuts",
361+
arguments: [AppLanguage.english, .chinese])
362+
func launchAtLoginGuidance(language: AppLanguage) {
363+
let hint = FileMintStrings.text(.launchAtLoginHint, language: language)
364+
let expectedPhrases = language == .chinese
365+
? ["登录 macOS 时在后台启动 FileMint", "关闭此项后", "手动打开 FileMint", "文件抽屉", "快捷搜索"]
366+
: ["background at login", "If turned off", "open FileMint manually", "File Drawer", "Quick Search"]
367+
for phrase in expectedPhrases { #expect(hint.contains(phrase)) }
368+
}
369+
345370
@Test("folder authorization guidance explains descendant coverage and removal in both languages",
346371
arguments: [AppLanguage.english, .chinese])
347372
func permissionGuidance(language: AppLanguage) {

‎scripts/launcher_product/FixtureApp.swift‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,15 +26,15 @@ final class LauncherProductFixtureDelegate: NSObject, NSApplicationDelegate {
2626
_ = LauncherProductRuntime.shared
2727
}
2828
func applicationDidFinishLaunching(_ notification: Notification) {
29+
let launchEvent = NSAppleEventManager.shared().currentAppleEvent
2930
if #available(macOS 13.3, *) {
3031
FileMintLauncherShortcuts.updateAppShortcutParameters()
3132
}
32-
let expected = NSAppleEventManager.shared().currentAppleEvent?
33-
.paramDescriptor(forKeyword: AEKeyword(kAEApplicationActivationExpected))?.booleanValue == true
33+
let expected = launchEvent?.paramDescriptor(forKeyword: AEKeyword(kAEApplicationActivationExpected))?.booleanValue == true
3434
LauncherProductRuntime.shared.record("launch", ["activationExpected": expected])
3535
LauncherProductRuntime.shared.coordinator.handleOrdinaryLaunch(
3636
isDefault: notification.userInfo?[NSApplication.launchIsDefaultUserInfoKey] as? Bool == true,
37-
activationExpected: expected)
37+
event: launchEvent)
3838
}
3939
func application(_ sender: NSApplication, open urls: [URL]) {
4040
for url in urls {

‎specs/domains/startup.md‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,12 @@ Part of the [FileMint SPEC](../SPEC.md). This file owns the behavior below; othe
1313
opens settings or adds a Dock icon; the creation panel alone stays accessory.
1414
- Launch at login and Show in menu bar default to enabled. Both have persistent
1515
switches in General. A saved off value must survive upgrades and relaunches.
16+
- A login-item launch runs FileMint in the background with enabled global
17+
shortcuts and leaves its windows closed. The login-item reason in the macOS
18+
launch event takes priority over a foreground activation request.
19+
- The Launch at login row explains background startup in both supported
20+
languages. With automatic launch off, users need to open FileMint after
21+
signing in before the File Drawer and Quick Search global shortcuts can respond.
1622
- Use macOS 13+ SMAppService.mainApp for login registration. Request the default
1723
once after the app has been placed in /Applications or ~/Applications and
1824
launched. Do not register development builds or a mounted DMG as login items.

0 commit comments

Comments
 (0)