Skip to content

Commit 4b9a74b

Browse files
committed
Inform regarding background activity when quitting
1 parent f157b3e commit 4b9a74b

File tree

4 files changed

+64
-4
lines changed

4 files changed

+64
-4
lines changed

BatteryToolkit/BTLocalization.swift

+21-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ internal enum BTLocalization {
99
enum Prompts {
1010
static let ok = NSLocalizedString(
1111
"OK",
12-
comment: "Prompt button to acknowledge an situation"
12+
comment: "Prompt button to acknowledge a situation"
1313
)
1414

1515
static let approve = NSLocalizedString(
@@ -37,6 +37,26 @@ internal enum BTLocalization {
3737
comment: "Prompt button to disable a core function and quit the app"
3838
)
3939

40+
static let openSystemSettings = NSLocalizedString(
41+
"Open System Settings",
42+
comment: "Prompt button to open System Settings"
43+
)
44+
45+
static let quitMessage = NSLocalizedString(
46+
"Quit Battery Toolkit?",
47+
comment: "Prompt caption asking whether to quit the app"
48+
)
49+
50+
static let quitInfo = NSLocalizedString(
51+
"Battery Toolkit will continue to run in the background. To permanently suspend it, disable the background activity from the Battery Toolkit menu.",
52+
comment: "Prompt caption asking whether to quit the app"
53+
)
54+
55+
static let quitInfoMacOS13 = NSLocalizedString(
56+
"To temporarily suspend it, disable the background activity in System Settings.",
57+
comment: "Prompt caption asking whether to quit the app"
58+
)
59+
4060
static let unexpectedErrorMessage = NSLocalizedString(
4161
"An unexpected error has occured.",
4262
comment: "Prompt caption informing the user of an unexpected error"

BatteryToolkit/Views/BTAppPrompts.swift

+37-1
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,50 @@
11
//
2-
// Copyright (C) 2022 Marvin Häuser. All rights reserved.
2+
// Copyright (C) 2022 - 2024 Marvin Häuser. All rights reserved.
33
// SPDX-License-Identifier: BSD-3-Clause
44
//
55

66
import Cocoa
7+
import ServiceManagement
78

89
@MainActor
910
internal enum BTAppPrompts {
1011
private(set) static var open: UInt8 = 0
1112

13+
static func promptQuit() {
14+
let alert = NSAlert()
15+
alert.messageText = BTLocalization.Prompts.quitMessage
16+
if #available(macOS 13.0, *) {
17+
alert.informativeText = BTLocalization.Prompts.quitInfo + " " + BTLocalization.Prompts.quitInfoMacOS13
18+
} else {
19+
alert.informativeText = BTLocalization.Prompts.quitInfo
20+
}
21+
alert.alertStyle = NSAlert.Style.informational
22+
_ = alert.addButton(withTitle: BTLocalization.Prompts.quit)
23+
_ = alert.addButton(withTitle: BTLocalization.Prompts.cancel)
24+
if #available(macOS 13.0, *) {
25+
_ = alert.addButton(withTitle: BTLocalization.Prompts.openSystemSettings)
26+
}
27+
let response = self.runPromptStandalone(alert: alert)
28+
switch response {
29+
case NSApplication.ModalResponse.alertFirstButtonReturn:
30+
NSApp.terminate(self)
31+
32+
case NSApplication.ModalResponse.alertSecondButtonReturn:
33+
break
34+
35+
case NSApplication.ModalResponse.alertThirdButtonReturn:
36+
if #available(macOS 13.0, *) {
37+
SMAppService.openSystemSettingsLoginItems()
38+
} else {
39+
assertionFailure()
40+
}
41+
break
42+
43+
default:
44+
assertionFailure()
45+
}
46+
}
47+
1248
static func promptApproveDaemon(timeout: UInt8) async throws {
1349
let alert = NSAlert()
1450
alert.messageText = BTLocalization.Prompts.Daemon.allowMessage

BatteryToolkit/Views/Main/BTCommandsMenuDelegate.swift

+4
Original file line numberDiff line numberDiff line change
@@ -240,6 +240,10 @@ internal final class BTCommandsMenuDelegate: NSObject, NSMenuDelegate {
240240
self.refreshTimer = nil
241241
}
242242

243+
@IBAction private func quitHandler(sender _: NSMenuItem) {
244+
BTAppPrompts.promptQuit()
245+
}
246+
243247
@IBAction private func disablePowerAdapterHandler(sender _: NSMenuItem) {
244248
Task {
245249
do {

BatteryToolkit/Views/Main/Base.lproj/Main.storyboard

+2-2
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@
5959
<menuItem isSeparatorItem="YES" id="kCx-OE-vgT"/>
6060
<menuItem title="Quit Battery Toolkit" keyEquivalent="q" id="4sb-4s-VLi">
6161
<connections>
62-
<action selector="terminate:" target="Ady-hI-5gd" id="Te7-pn-YzF"/>
62+
<action selector="quitHandlerWithSender:" target="Yoz-Et-jXW" id="XwH-9q-S0c"/>
6363
</connections>
6464
</menuItem>
6565
</items>
@@ -373,7 +373,7 @@
373373
<menuItem isSeparatorItem="YES" id="9fM-fx-Sxj"/>
374374
<menuItem title="Quit Battery Toolkit" keyEquivalent="q" id="Smu-3d-BCn">
375375
<connections>
376-
<action selector="terminate:" target="Ady-hI-5gd" id="lXW-g6-h5X"/>
376+
<action selector="quitHandlerWithSender:" target="Yoz-Et-jXW" id="WbX-gg-kmB"/>
377377
</connections>
378378
</menuItem>
379379
</items>

0 commit comments

Comments
 (0)