Skip to content

Commit aa39a09

Browse files
committed
showing alerts only on main thread
1 parent 82d67d7 commit aa39a09

4 files changed

Lines changed: 111 additions & 75 deletions

File tree

native/app/Source/Application.swift

Lines changed: 32 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -109,24 +109,26 @@ class Application {
109109

110110
private static func installDriver (_ completion: @escaping() -> Void) {
111111
if !Driver.isInstalled || Driver.isOutdated {
112-
if Alert.confirm(
113-
title: "Audio Driver Installation",
114-
message: "eqMac needs to install an Audio Driver. \nIn order to do that we will ask for your System Password. \nPlease close any apps playing audio (Spotify, YouTube etc.) otherwise installation might fail.",
115-
cancelText: "Quit eqMac"
116-
) {
117-
Driver.install(started: {
118-
UI.showLoadingWindow("Installing eqMac audio driver")
119-
}) { success in
120-
if (success) {
121-
UI.hideLoadingWindow()
122-
completion()
123-
} else {
124-
driverFailedToInstallPrompt()
112+
Alert.confirm(
113+
title: "Audio Driver Installation",
114+
message: "eqMac needs to install an Audio Driver. \nIn order to do that we will ask for your System Password. \nPlease close any apps playing audio (Spotify, YouTube etc.) otherwise installation might fail.",
115+
cancelText: "Quit eqMac"
116+
) { install in
117+
if install {
118+
Driver.install(started: {
119+
UI.showLoadingWindow("Installing eqMac audio driver")
120+
}) { success in
121+
if (success) {
122+
UI.hideLoadingWindow()
123+
completion()
124+
} else {
125+
driverFailedToInstallPrompt()
126+
}
125127
}
128+
129+
} else {
130+
quit()
126131
}
127-
128-
} else {
129-
quit()
130132
}
131133
} else {
132134
completion()
@@ -135,11 +137,13 @@ class Application {
135137

136138
private static func driverFailedToInstallPrompt () {
137139
UI.hideLoadingWindow()
138-
if Alert.confirm(
139-
title: "Driver failed to install", message: "Unfortunately the audio driver has failed to install. You can restart eqMac and try again or quit.", okText: "Try again", cancelText: "Quit") {
140-
return restart()
141-
} else {
142-
return quit()
140+
Alert.confirm(
141+
title: "Driver failed to install", message: "Unfortunately the audio driver has failed to install. You can restart eqMac and try again or quit.", okText: "Try again", cancelText: "Quit") { restart in
142+
if restart {
143+
return self.restart()
144+
} else {
145+
return self.quit()
146+
}
143147
}
144148
}
145149

@@ -186,11 +190,13 @@ class Application {
186190
}
187191

188192
private static func passthroughDeviceFailedToActivatePrompt () {
189-
if Alert.confirm(
190-
title: "Driver failed to activate", message: "Unfortunately the audio driver has failed to active. You can restart eqMac and try again or quit.", okText: "Try again", cancelText: "Quit") {
191-
return restart()
192-
} else {
193-
return quit()
193+
Alert.confirm(
194+
title: "Driver failed to activate", message: "Unfortunately the audio driver has failed to active. You can restart eqMac and try again or quit.", okText: "Try again", cancelText: "Quit") { restart in
195+
if restart {
196+
return self.restart()
197+
} else {
198+
return self.quit()
199+
}
194200
}
195201
}
196202

native/app/Source/Audio/Sources/Sources.swift

Lines changed: 30 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ public enum SourceType : String {
2121
]
2222
}
2323

24-
class Sources {
24+
class Sources: NSObject {
2525
// typealias StoreSubscriberStateType = SourceState
2626
var source: SourceType! = .System {
2727
didSet {
@@ -30,12 +30,13 @@ class Sources {
3030
}
3131
let sourceChanged = Event<SourceType>()
3232
var isReady = EmitterKit.Event<Void>()
33-
33+
3434
// var file: File!
3535
// var input: Input!
3636
var system: SystemAudioSource!
3737

3838
init (_ callback: @escaping (Sources) -> Void) {
39+
super.init()
3940
Console.log("Creating Sources")
4041
// source = state.source
4142
setup(callback)
@@ -69,34 +70,43 @@ class Sources {
6970
func getInputPermission (_ callback: @escaping () -> Void) {
7071
if !InputSource.hasPermission {
7172
let title = "Microphone Usage Permission"
72-
if Alert.confirm(
73+
Alert.confirm(
7374
title: title,
7475
message: "eqMac needs access to Microphone to Route and Process System Audio. \nPlease click the \"Proceed\" button and allow access.",
7576
okText: "Proceed",
76-
cancelText: "No, quit eqMac")
77-
{
78-
InputSource.requestPermission() { allowed in
79-
if !InputSource.hasPermission {
80-
Script.apple("open_privacy_microphone_settings")
81-
if Alert.confirm(
82-
title: title,
83-
message: "You have not allowed access to your Microphone. \neqMac needs access to Microphone to Route and Process System Audio. \nWe will try to open your Security & Privacy settings. \n\n If it didn't open please follow these steps, otherwise skip to step 5: \n\n1. Open your \"System Preferences.app\" \n2. Click on \"Security & Privacy\" \n3. Click on \"Privacy\" tab \n4. Scroll down to \"Microphone\" \n5. Check the box against \"eqMac.app\" \n\nAfter that we will need to restart the Application",
84-
okText: "Ok I did that. Restart eqMac",
85-
cancelText: "No, quit eqMac")
86-
{
87-
Application.restart()
88-
} else {
89-
Application.quit()
77+
cancelText: "No, quit eqMac") { proceed in
78+
if proceed {
79+
InputSource.requestPermission() { allowed in
80+
if !InputSource.hasPermission {
81+
Script.apple("open_privacy_microphone_settings")
82+
Utilities.delay(1000) {
83+
84+
Alert.confirm(
85+
title: "Microphone Usage Permission",
86+
message: "You have not allowed access to your Microphone. \neqMac needs access to Microphone to Route and Process System Audio. \neqMac will try to open your Security & Privacy settings. \n\n If it didn't open please follow these steps, otherwise skip to step 5: \n\n1. Open your \"System Preferences.app\" \n2. Click on \"Security & Privacy\" \n3. Click on \"Privacy\" tab \n4. Scroll down to \"Microphone\" section \n5. Check the box against \"eqMac.app\" \n\nAfter that we will need to restart the Application",
87+
okText: "Ok I did that. Restart eqMac",
88+
cancelText: "No, quit eqMac") { restart in
89+
if restart {
90+
Application.restart()
91+
} else {
92+
Application.quit()
93+
}
94+
}
95+
}
96+
}
97+
9098
}
99+
} else {
100+
Application.quit()
91101
}
92-
}
93-
} else {
94-
Application.quit()
95102
}
103+
return
96104
}
97105
callback()
98106
}
99107

108+
109+
100110
func reset () {
101111
system = nil
102112
}

native/app/Source/Helpers/Alert.swift

Lines changed: 49 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -10,37 +10,57 @@ import Foundation
1010
import AppKit
1111

1212
class Alert {
13-
static func info (title: String, message: String) {
14-
let alert = getAlert(title, message)
15-
alert.addButton(withTitle: "Ok")
16-
alert.runModal()
13+
static func info (title: String, message: String) {
14+
DispatchQueue.main.async {
15+
let alert = getAlert(title, message)
16+
alert.addButton(withTitle: "Ok")
17+
alert.runModal()
1718
}
18-
19-
static func confirm (title: String, message: String, okText: String = "Ok", cancelText: String = "Cancel") -> Bool {
20-
let alert = getAlert(title, message)
21-
alert.addButton(withTitle: okText)
22-
alert.addButton(withTitle: cancelText)
23-
return alert.runModal() == .alertFirstButtonReturn
19+
}
20+
21+
static func confirm (
22+
title: String,
23+
message: String,
24+
okText: String = "Ok",
25+
cancelText: String = "Cancel",
26+
callback: @escaping (Bool) -> Void
27+
) {
28+
DispatchQueue.main.async {
29+
let alert = getAlert(title, message)
30+
alert.addButton(withTitle: okText)
31+
alert.addButton(withTitle: cancelText)
32+
let result = alert.runModal() == .alertFirstButtonReturn
33+
callback(result)
2434
}
25-
26-
static func prompt (title: String, message: String, okText: String = "Save", cancelText: String = "Cancel") -> String? {
27-
let alert = getAlert(title, message)
28-
alert.addButton(withTitle: okText)
29-
alert.addButton(withTitle: cancelText)
30-
let input = NSTextField(frame: NSMakeRect(0, 0, 200, 24))
31-
input.stringValue = ""
32-
alert.accessoryView = input
33-
if (alert.runModal() == .alertFirstButtonReturn) {
34-
input.validateEditing()
35-
return input.stringValue == "" ? nil : input.stringValue
36-
}
37-
return nil
35+
}
36+
37+
static func prompt (
38+
title: String,
39+
message: String,
40+
okText: String = "Save",
41+
cancelText: String = "Cancel",
42+
callback: @escaping (String?) -> Void
43+
) {
44+
DispatchQueue.main.async {
45+
let alert = getAlert(title, message)
46+
alert.addButton(withTitle: okText)
47+
alert.addButton(withTitle: cancelText)
48+
let input = NSTextField(frame: NSMakeRect(0, 0, 200, 24))
49+
input.stringValue = ""
50+
alert.accessoryView = input
51+
if (alert.runModal() == .alertFirstButtonReturn) {
52+
input.validateEditing()
53+
callback(input.stringValue == "" ? nil : input.stringValue)
54+
}
55+
callback(nil)
3856
}
3957

40-
static private func getAlert (_ title: String, _ message: String) -> NSAlert {
41-
let alert = NSAlert()
42-
alert.messageText = title
43-
alert.informativeText = message
44-
return alert
45-
}
58+
}
59+
60+
static private func getAlert (_ title: String, _ message: String) -> NSAlert {
61+
let alert = NSAlert()
62+
alert.messageText = title
63+
alert.informativeText = message
64+
return alert
65+
}
4666
}
786 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)