Skip to content

Commit 9b0f55d

Browse files
authored
Merge pull request #83 from marceloexc/feat/confirm-quit
add a "Warn before Quitting" toggle
2 parents 75d51ff + 5387d8c commit 9b0f55d

File tree

3 files changed

+56
-6
lines changed

3 files changed

+56
-6
lines changed

rm2000/AppKitWindowManagerDelegate.swift

Lines changed: 42 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,18 +10,19 @@ class WindowController: NSWindowController {
1010
}
1111
}
1212

13-
class AppKitWindowManagerDelegate: NSObject, NSApplicationDelegate,
14-
NSWindowDelegate
15-
{
16-
17-
@Published var willTerminate = false
13+
class AppKitWindowManagerDelegate: NSObject, NSApplicationDelegate, NSWindowDelegate {
14+
1815
var mainWindowController: WindowController?
1916
let recordingState = TapeRecorderState.shared
2017
let storeKitManager = StoreManager.shared
2118
let mainWindowIdentifier = NSUserInterfaceItemIdentifier("mainWindow")
2219

2320
private var onboardingWindowController: NSWindowController?
2421
private var hudHostingView: NSHostingView<AnyView>?
22+
@MainActor private var confirmOnQuit: Bool {
23+
AppState.shared.confirmOnQuit
24+
}
25+
@Published private var willTerminate = false
2526

2627
private var hudWindow: NSWindow?
2728
private var mainWindow: NSWindow?
@@ -145,6 +146,42 @@ class AppKitWindowManagerDelegate: NSObject, NSApplicationDelegate,
145146
onboardingWindowController?.showWindow(nil)
146147
window.center()
147148
}
149+
150+
func applicationShouldTerminate(_ sender: NSApplication) -> NSApplication.TerminateReply {
151+
if confirmOnQuit {
152+
self.willTerminate = true
153+
self.promptQuitConfirmation()
154+
return .terminateLater
155+
}
156+
return .terminateNow
157+
}
158+
159+
/// dont close (user canceled)
160+
func `continue`() {
161+
NSApplication.shared.reply(toApplicationShouldTerminate: false)
162+
}
163+
/// close
164+
func close() {
165+
NSApplication.shared.reply(toApplicationShouldTerminate: true)
166+
}
167+
168+
func promptQuitConfirmation() {
169+
let alert = NSAlert()
170+
alert.messageText = "Really Quit?"
171+
alert.informativeText = "You will not be able to use your Global Recording Hotkey to record."
172+
alert.alertStyle = .critical
173+
alert.addButton(withTitle: "Yes")
174+
alert.addButton(withTitle: "No")
175+
176+
DispatchQueue.main.async {
177+
let response = alert.runModal()
178+
if response == .alertFirstButtonReturn {
179+
self.close()
180+
} else {
181+
self.continue()
182+
}
183+
}
184+
}
148185

149186
/*
150187
A function like this should never exist.

rm2000/AppState.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ import SwiftUI
2828
}
2929
}
3030

31+
@AppStorage("confirm_on_quit") var confirmOnQuit: Bool = false
32+
3133
@AppStorage("sample_directory") var sampleDirectoryPath: String = ""
3234
@AppStorage("sample_directory_bookmark") private var sampleDirectoryBookmark:
3335
Data?

rm2000/Views/Settings/GeneralTabView.swift

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@ import SwiftUI
66
struct GeneralTabView: View {
77
@Environment(\.openURL) private var openURL
88
@State private var isDockHidden: Bool = AppState.shared.hideDockIcon
9-
9+
@State private var isConfirmOnQuit: Bool = AppState.shared.hideDockIcon
10+
1011
var body: some View {
1112
VStack {
1213
GroupBox {
@@ -23,6 +24,16 @@ struct GeneralTabView: View {
2324
AppState.shared.hideDockIcon = newValue
2425
}
2526
} footer: {
27+
//
28+
}
29+
30+
Section {
31+
Toggle(isOn: $isConfirmOnQuit) {
32+
Text("Warn Before Quitting (⌘Q)")
33+
}.toggleStyle(.switch)
34+
.onChange(of: isConfirmOnQuit) { _, newValue in
35+
AppState.shared.confirmOnQuit = newValue
36+
}
2637
}
2738

2839
KeyboardShortcuts.Recorder(

0 commit comments

Comments
 (0)