Skip to content

Commit

Permalink
Use driver_activated to determine show driverNotActivatedAlertWindow
Browse files Browse the repository at this point in the history
  • Loading branch information
tekezo committed Sep 17, 2023
1 parent 789264c commit ba77767
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 77 deletions.
52 changes: 26 additions & 26 deletions src/apps/SettingsWindow/src/AlertWindowsManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,26 +6,26 @@ public class AlertWindowsManager {
static let shared = AlertWindowsManager()

var parentWindow: NSWindow?
var driverNotLoadedAlertWindow: NSWindow?
var driverVersionNotMatchedAlertWindow: NSWindow?
var driverNotActivatedAlertWindow: NSWindow?
var driverVersionMismatchedAlertWindow: NSWindow?
var inputMonitoringPermissionsAlertWindow: NSWindow?

//
// driverNotLoadedAlertWindow
// driverNotActivatedAlertWindow
//

public func showDriverNotLoadedAlertWindow() {
public func showDriverNotActivatedAlertWindow() {
if let parentWindow = parentWindow {
if driverNotLoadedAlertWindow == nil {
let view = DriverNotLoadedAlertView(
if driverNotActivatedAlertWindow == nil {
let view = DriverNotActivatedAlertView(
parentWindow: parentWindow,
onCloseButtonPressed: { [weak self] in
guard let self = self else { return }

self.hideDriverNotLoadedAlertWindow()
self.hideDriverNotActivatedAlertWindow()
})

driverNotLoadedAlertWindow = NSPanel(
driverNotActivatedAlertWindow = NSPanel(
contentRect: .zero,
styleMask: [
.titled,
Expand All @@ -35,39 +35,39 @@ public class AlertWindowsManager {
backing: .buffered,
defer: false
)
driverNotLoadedAlertWindow!.contentView = NSHostingView(rootView: view)
driverNotActivatedAlertWindow!.contentView = NSHostingView(rootView: view)

parentWindow.beginSheet(driverNotLoadedAlertWindow!) { [weak self] _ in
parentWindow.beginSheet(driverNotActivatedAlertWindow!) { [weak self] _ in
guard let self = self else { return }

self.driverNotLoadedAlertWindow = nil
self.driverNotActivatedAlertWindow = nil
}
}
}
}

public func hideDriverNotLoadedAlertWindow() {
public func hideDriverNotActivatedAlertWindow() {
if let parentWindow = parentWindow {
if driverNotLoadedAlertWindow != nil {
parentWindow.endSheet(driverNotLoadedAlertWindow!)
if driverNotActivatedAlertWindow != nil {
parentWindow.endSheet(driverNotActivatedAlertWindow!)
}
}
}

//
// driverVersionNotMatchedAlertWindow
// driverVersionMismatchedAlertWindow
//

public func showDriverVersionNotMatchedAlertWindow() {
public func showDriverVersionMismatchedAlertWindow() {
if let parentWindow = parentWindow {
if driverVersionNotMatchedAlertWindow == nil {
let view = DriverVersionNotMatchedAlertView(onCloseButtonPressed: { [weak self] in
if driverVersionMismatchedAlertWindow == nil {
let view = DriverVersionMismatchedAlertView(onCloseButtonPressed: { [weak self] in
guard let self = self else { return }

self.hideDriverVersionNotMatchedAlertWindow()
self.hideDriverVersionMismatchedAlertWindow()
})

driverVersionNotMatchedAlertWindow = NSPanel(
driverVersionMismatchedAlertWindow = NSPanel(
contentRect: .zero,
styleMask: [
.titled,
Expand All @@ -77,21 +77,21 @@ public class AlertWindowsManager {
backing: .buffered,
defer: false
)
driverVersionNotMatchedAlertWindow!.contentView = NSHostingView(rootView: view)
driverVersionMismatchedAlertWindow!.contentView = NSHostingView(rootView: view)

parentWindow.beginSheet(driverVersionNotMatchedAlertWindow!) { [weak self] _ in
parentWindow.beginSheet(driverVersionMismatchedAlertWindow!) { [weak self] _ in
guard let self = self else { return }

self.driverVersionNotMatchedAlertWindow = nil
self.driverVersionMismatchedAlertWindow = nil
}
}
}
}

public func hideDriverVersionNotMatchedAlertWindow() {
public func hideDriverVersionMismatchedAlertWindow() {
if let parentWindow = parentWindow {
if driverVersionNotMatchedAlertWindow != nil {
parentWindow.endSheet(driverVersionNotMatchedAlertWindow!)
if driverVersionMismatchedAlertWindow != nil {
parentWindow.endSheet(driverVersionMismatchedAlertWindow!)
}
}
}
Expand Down
84 changes: 39 additions & 45 deletions src/apps/SettingsWindow/src/StateJsonMonitor.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ private func callback(
}

private struct State: Codable {
var driverLoaded: Bool?
var driverVersionMatched: Bool?
var driverActivated: Bool?
var driverVersionMismatched: Bool?
var hidDeviceOpenPermitted: Bool?
}

Expand All @@ -25,8 +25,8 @@ public class StateJsonMonitor {

private var states: [String: State] = [:]

public var needsDriverNotLoadedAlert = false
public var needsDriverVersionNotMatchedAlert = false
public var needsDriverNotActivatedAlert = false
public var needsDriverVersionMismatchedAlert = false
public var needsInputMonitoringPermissionsAlert = false

public func start() {
Expand All @@ -53,49 +53,49 @@ public class StateJsonMonitor {
// Show alerts
//

var driverNotLoaded = false
var driverVersionNotMatched = false
var driverNotActivated = false
var driverVersionMismatched = false
var inputMonitoringNotPermitted = false
for state in states {
if state.value.driverLoaded == false {
driverNotLoaded = true
if state.value.driverActivated == false {
driverNotActivated = true
}
if state.value.driverVersionMatched == false {
driverVersionNotMatched = true
if state.value.driverVersionMismatched == true {
driverVersionMismatched = true
}
if state.value.hidDeviceOpenPermitted == false {
inputMonitoringNotPermitted = true
}
}

// print("driverNotLoaded \(driverNotLoaded)")
// print("driverVersionNotMatched \(driverVersionNotMatched)")
// print("driverNotActivated \(driverNotActivated)")
// print("driverVersionMismatched \(driverVersionMismatched)")
// print("inputMonitoringNotPermitted \(inputMonitoringNotPermitted)")

//
// - DriverNotLoadedAlertWindow
// - DriverVersionNotMatchedAlertWindow
// - DriverNotActivatedAlertWindow
// - DriverVersionMismatchedAlertWindow
//

if needsDriverVersionNotMatchedAlert {
// If DriverVersionNotMatchedAlertWindow is shown,
// keep needsDriverNotLoadedAlert to prevent showing DriverNotLoadedAlertWindow after the driver is deactivated.
if needsDriverVersionMismatchedAlert {
// If DriverVersionMismatchedAlertWindow is shown,
// keep needsDriverNotActivatedAlert to prevent showing DriverNotActivatedAlertWindow after the driver is deactivated.

if !driverVersionNotMatched {
needsDriverVersionNotMatchedAlert = false
if !driverVersionMismatched {
needsDriverVersionMismatchedAlert = false
}

} else {
if driverNotLoaded {
needsDriverNotLoadedAlert = true
needsDriverVersionNotMatchedAlert = false
if driverNotActivated {
needsDriverNotActivatedAlert = true
needsDriverVersionMismatchedAlert = false
} else {
needsDriverNotLoadedAlert = false
needsDriverNotActivatedAlert = false

if driverVersionNotMatched {
needsDriverVersionNotMatchedAlert = true
if driverVersionMismatched {
needsDriverVersionMismatchedAlert = true
} else {
needsDriverVersionNotMatchedAlert = false
needsDriverVersionMismatchedAlert = false
}
}
}
Expand All @@ -110,23 +110,23 @@ public class StateJsonMonitor {
needsInputMonitoringPermissionsAlert = false
}

print("needsDriverNotLoadedAlert \(needsDriverNotLoadedAlert)")
print("needsDriverVersionNotMatchedAlert \(needsDriverVersionNotMatchedAlert)")
print("needsDriverNotActivatedAlert \(needsDriverNotActivatedAlert)")
print("needsDriverVersionMismatchedAlert \(needsDriverVersionMismatchedAlert)")
print("needsInputMonitoringPermissionsAlert \(needsInputMonitoringPermissionsAlert)")

//
// Update alert window
//

updateAlertWindow(
needsAlert: { return StateJsonMonitor.shared.needsDriverNotLoadedAlert },
show: { AlertWindowsManager.shared.showDriverNotLoadedAlertWindow() },
hide: { AlertWindowsManager.shared.hideDriverNotLoadedAlertWindow() })
needsAlert: { return StateJsonMonitor.shared.needsDriverNotActivatedAlert },
show: { AlertWindowsManager.shared.showDriverNotActivatedAlertWindow() },
hide: { AlertWindowsManager.shared.hideDriverNotActivatedAlertWindow() })

updateAlertWindow(
needsAlert: { return StateJsonMonitor.shared.needsDriverVersionNotMatchedAlert },
show: { AlertWindowsManager.shared.showDriverVersionNotMatchedAlertWindow() },
hide: { AlertWindowsManager.shared.hideDriverVersionNotMatchedAlertWindow() })
needsAlert: { return StateJsonMonitor.shared.needsDriverVersionMismatchedAlert },
show: { AlertWindowsManager.shared.showDriverVersionMismatchedAlertWindow() },
hide: { AlertWindowsManager.shared.hideDriverVersionMismatchedAlertWindow() })

updateAlertWindow(
needsAlert: { return StateJsonMonitor.shared.needsInputMonitoringPermissionsAlert },
Expand All @@ -139,18 +139,12 @@ public class StateJsonMonitor {
show: @escaping () -> Void,
hide: @escaping () -> Void
) {
// Note:
// Delay before displaying the alert to avoid the alert appearing momentarily.
// (e.g, when karabiner_grabbedr is restarted)

if needsAlert() {
DispatchQueue.main.asyncAfter(deadline: .now() + 3) {
if needsAlert() {
show()
}
Task { @MainActor in
if needsAlert() {
show()
} else {
hide()
}
} else {
hide()
}
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import SwiftUI

struct DriverNotLoadedAlertView: View {
struct DriverNotActivatedAlertView: View {
let parentWindow: NSWindow?
let onCloseButtonPressed: () -> Void

Expand Down Expand Up @@ -110,10 +110,10 @@ struct DriverNotLoadedAlertView: View {
}
}

struct DriverNotLoadedAlertView_Previews: PreviewProvider {
struct DriverNotActivatedAlertView_Previews: PreviewProvider {
static var previews: some View {
Group {
DriverNotLoadedAlertView(
DriverNotActivatedAlertView(
parentWindow: nil,
onCloseButtonPressed: {}
)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import SwiftUI

struct DriverVersionNotMatchedAlertView: View {
struct DriverVersionMismatchedAlertView: View {
let onCloseButtonPressed: () -> Void

var body: some View {
Expand Down Expand Up @@ -43,10 +43,10 @@ struct DriverVersionNotMatchedAlertView: View {
}
}

struct DriverVersionNotMatchedAlertView_Previews: PreviewProvider {
struct DriverVersionMismatchedAlertView_Previews: PreviewProvider {
static var previews: some View {
Group {
DriverVersionNotMatchedAlertView(onCloseButtonPressed: {})
DriverVersionMismatchedAlertView(onCloseButtonPressed: {})
.previewLayout(.sizeThatFits)
}
}
Expand Down

0 comments on commit ba77767

Please sign in to comment.