fix: permission checks might freeze or restart the app (closes #5112)
This commit is contained in:
1 parent
3c04a4ce15
commit
7906d630be
10 files changed
+131
-128
No files matched your search
@@ -12,7 +12,7 @@ class IOKitPrototype {
|
|||||||
kIOHIDDeviceUsageKey: kHIDUsage_GD_Keyboard
|
kIOHIDDeviceUsageKey: kHIDUsage_GD_Keyboard
|
||||||
] as CFDictionary)
|
] as CFDictionary)
|
||||||
IOHIDManagerRegisterInputValueCallback(manager, keyboardEventHandler, nil)
|
IOHIDManagerRegisterInputValueCallback(manager, keyboardEventHandler, nil)
|
||||||
IOHIDManagerScheduleWithRunLoop(manager, BackgroundWork.keyboardAndTrackpadEventsThread.runLoop!, CFRunLoopMode.commonModes.rawValue)
|
IOHIDManagerScheduleWithRunLoop(manager, BackgroundWork.keyboardAndMouseAndTrackpadEventsThread.runLoop!, CFRunLoopMode.commonModes.rawValue)
|
||||||
IOHIDManagerOpen(manager, IOOptionBits(kIOHIDOptionsTypeNone))
|
IOHIDManagerOpen(manager, IOOptionBits(kIOHIDOptionsTypeNone))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,24 +2,36 @@ import Foundation
|
|||||||
|
|
||||||
// queues and dedicated threads to observe background events such as keyboard inputs, or accessibility events
|
// queues and dedicated threads to observe background events such as keyboard inputs, or accessibility events
|
||||||
class BackgroundWork {
|
class BackgroundWork {
|
||||||
// we use an OperationQueue tied to the global DispatchQueue to call blocking APIs in parallel
|
// we use Threads when the APIs we observe require a RunLoop (e.g. CGEvent.tapCreate, AXObserverGetRunLoopSource, CFMessagePortCreateRunLoopSource)
|
||||||
|
// when possible, we prefer OperationQueues
|
||||||
|
static var accessibilityEventsThread: BackgroundThreadWithRunLoop!
|
||||||
|
static var keyboardAndMouseAndTrackpadEventsThread: BackgroundThreadWithRunLoop!
|
||||||
|
static var missionControlThread: BackgroundThreadWithRunLoop!
|
||||||
|
static var cliEventsThread: BackgroundThreadWithRunLoop!
|
||||||
|
|
||||||
|
// TODO: use OperationQueue instead of thread
|
||||||
|
static var repeatingKeyThread: BackgroundThreadWithRunLoop!
|
||||||
|
|
||||||
|
// we use an OperationQueue for most tasks, especially when we need to call blocking APIs in parallel
|
||||||
static var screenshotsQueue: LabeledOperationQueue!
|
static var screenshotsQueue: LabeledOperationQueue!
|
||||||
static var accessibilityCommandsQueue: LabeledOperationQueue!
|
static var accessibilityCommandsQueue: LabeledOperationQueue!
|
||||||
static var axCallsFirstAttemptQueue: LabeledOperationQueue!
|
static var axCallsFirstAttemptQueue: LabeledOperationQueue!
|
||||||
static var axCallsRetriesQueue: LabeledOperationQueue!
|
static var axCallsRetriesQueue: LabeledOperationQueue!
|
||||||
static var axCallsManualDiscoveryQueue: LabeledOperationQueue!
|
static var axCallsManualDiscoveryQueue: LabeledOperationQueue!
|
||||||
static var crashReportsQueue: LabeledOperationQueue!
|
static var crashReportsQueue: LabeledOperationQueue!
|
||||||
// we use Threads to observe events in sequence
|
static var systemPermissionsCheckOnTimerQueue: LabeledOperationQueue!
|
||||||
static var accessibilityEventsThread: BackgroundThreadWithRunLoop!
|
static var systemPermissionsSystemCallsQueue: LabeledOperationQueue!
|
||||||
static var keyboardAndTrackpadEventsThread: BackgroundThreadWithRunLoop!
|
|
||||||
static var systemPermissionsThread: BackgroundThreadWithRunLoop!
|
|
||||||
static var repeatingKeyThread: BackgroundThreadWithRunLoop!
|
|
||||||
static var missionControlThread: BackgroundThreadWithRunLoop!
|
|
||||||
static var cliEventsThread: BackgroundThreadWithRunLoop!
|
|
||||||
static var debugMenu: DebugMenu!
|
|
||||||
|
|
||||||
|
private static var debugMenu: DebugMenu!
|
||||||
private static var totalPotentialThreadCount = 0
|
private static var totalPotentialThreadCount = 0
|
||||||
|
|
||||||
|
static func preStart() {
|
||||||
|
// we make calls to the system permissions API to know if permissions are granted. We do this on a timer
|
||||||
|
systemPermissionsCheckOnTimerQueue = LabeledOperationQueue("systemPermissionsCheckOnTimerQueue", .userInteractive, 1)
|
||||||
|
// if macOS is overwhelmed, let's reduce the pressure on it by calling permission APIs one at a time
|
||||||
|
systemPermissionsSystemCallsQueue = LabeledOperationQueue("systemPermissionsSystemCallsQueue", .userInteractive, 1)
|
||||||
|
}
|
||||||
|
|
||||||
static func start() {
|
static func start() {
|
||||||
screenshotsQueue = LabeledOperationQueue("screenshotsQueue", .userInteractive, 8)
|
screenshotsQueue = LabeledOperationQueue("screenshotsQueue", .userInteractive, 8)
|
||||||
// calls to focus/close/minimize/etc windows
|
// calls to focus/close/minimize/etc windows
|
||||||
@@ -34,7 +46,7 @@ class BackgroundWork {
|
|||||||
// we observe app and windows notifications. They arrive on this thread, and are handled off the main thread initially
|
// we observe app and windows notifications. They arrive on this thread, and are handled off the main thread initially
|
||||||
accessibilityEventsThread = BackgroundThreadWithRunLoop("accessibilityEventsThread", .userInteractive)
|
accessibilityEventsThread = BackgroundThreadWithRunLoop("accessibilityEventsThread", .userInteractive)
|
||||||
// we listen to as any keyboard events as possible on a background thread, as it's more available/reliable than the main thread
|
// we listen to as any keyboard events as possible on a background thread, as it's more available/reliable than the main thread
|
||||||
keyboardAndTrackpadEventsThread = BackgroundThreadWithRunLoop("keyboardAndTrackpadEventsThread", .userInteractive)
|
keyboardAndMouseAndTrackpadEventsThread = BackgroundThreadWithRunLoop("keyboardAndMouseAndTrackpadEventsThread", .userInteractive)
|
||||||
// we time key repeat on a background thread for precision. We handle their consequence on the main-thread
|
// we time key repeat on a background thread for precision. We handle their consequence on the main-thread
|
||||||
repeatingKeyThread = BackgroundThreadWithRunLoop("repeatingKeyThread", .userInteractive)
|
repeatingKeyThread = BackgroundThreadWithRunLoop("repeatingKeyThread", .userInteractive)
|
||||||
// we main Mission Control state on a background thread. We protect reads from main-thread with an NSLock
|
// we main Mission Control state on a background thread. We protect reads from main-thread with an NSLock
|
||||||
@@ -51,15 +63,10 @@ class BackgroundWork {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static func startSystemPermissionThread() {
|
|
||||||
// not 100% sure this shouldn't be on the main-thread; it doesn't do anything except dispatch to main.async
|
|
||||||
systemPermissionsThread = BackgroundThreadWithRunLoop("systemPermissionsThread", .utility)
|
|
||||||
}
|
|
||||||
|
|
||||||
static func addPotentialThreadCount(_ additionalCount: Int) {
|
static func addPotentialThreadCount(_ additionalCount: Int) {
|
||||||
totalPotentialThreadCount += additionalCount
|
totalPotentialThreadCount += additionalCount
|
||||||
// a macos process has a soft limit of 64 threads. We need to be careful to don't spawn too many threads through DispatchQueues
|
// a macos process has a soft limit of 64 threads. We need to be careful to don't spawn too many threads through DispatchQueues
|
||||||
assert(totalPotentialThreadCount <= 43)
|
assert(totalPotentialThreadCount <= 45)
|
||||||
}
|
}
|
||||||
|
|
||||||
// useful during development to inspect how many threads are used by AltTab
|
// useful during development to inspect how many threads are used by AltTab
|
||||||
|
|||||||
@@ -4,58 +4,56 @@ import ScreenCaptureKit.SCShareableContent
|
|||||||
// macOS has some privacy restrictions. The user needs to grant certain permissions, app by app, in System Preferences > Security & Privacy
|
// macOS has some privacy restrictions. The user needs to grant certain permissions, app by app, in System Preferences > Security & Privacy
|
||||||
class SystemPermissions {
|
class SystemPermissions {
|
||||||
static var preStartupPermissionsPassed = false
|
static var preStartupPermissionsPassed = false
|
||||||
static var timerPermissionsToUpdatePermissionsWindow: Timer?
|
private static var timer: DispatchSourceTimer!
|
||||||
static var timerPermissionsRemovedWhileAltTabIsRunning: Timer?
|
|
||||||
|
|
||||||
static func ensurePermissionsAreGranted(_ continueAppStartup: @escaping () -> Void) {
|
static func ensurePermissionsAreGranted() {
|
||||||
let startupBlock = {
|
timer = DispatchSource.makeTimerSource(queue: BackgroundWork.systemPermissionsCheckOnTimerQueue.strongUnderlyingQueue)
|
||||||
pollPermissionsRemovedWhileAltTabIsRunning()
|
timer.setEventHandler(handler: checkPermissionsOnTimer)
|
||||||
continueAppStartup()
|
setImmediateTimer()
|
||||||
}
|
timer.resume()
|
||||||
if AccessibilityPermission.update() != .notGranted && ScreenRecordingPermission.update() != .notGranted {
|
}
|
||||||
preStartupPermissionsPassed = true
|
|
||||||
startupBlock()
|
private static func checkPermissionsOnTimer() {
|
||||||
|
AccessibilityPermission.update()
|
||||||
|
ScreenRecordingPermission.update()
|
||||||
|
if !preStartupPermissionsPassed {
|
||||||
|
checkPermissionsPreStartup()
|
||||||
} else {
|
} else {
|
||||||
App.app.permissionsWindow.show(startupBlock)
|
checkPermissionsPostStartup()
|
||||||
|
}
|
||||||
|
DispatchQueue.main.async {
|
||||||
|
Menubar.togglePermissionCallout(ScreenRecordingPermission.status == .skipped)
|
||||||
|
App.app.permissionsWindow.updatePermissionViews()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static func pollPermissionsToUpdatePermissionsWindow(_ startupBlock: @escaping () -> Void) {
|
private static func checkPermissionsPreStartup() {
|
||||||
timerPermissionsToUpdatePermissionsWindow = Timer(timeInterval: 0.1, repeats: true) { _ in
|
Logger.info("accessibility:\(AccessibilityPermission.status), screen-recording:\(ScreenRecordingPermission.status)")
|
||||||
checkPermissionsToUpdatePermissionsWindow(startupBlock)
|
if AccessibilityPermission.status != .notGranted && ScreenRecordingPermission.status != .notGranted {
|
||||||
}
|
|
||||||
timerPermissionsToUpdatePermissionsWindow!.tolerance = 0.1
|
|
||||||
CFRunLoopAddTimer(BackgroundWork.systemPermissionsThread.runLoop, timerPermissionsToUpdatePermissionsWindow!, .commonModes)
|
|
||||||
}
|
|
||||||
|
|
||||||
private static func pollPermissionsRemovedWhileAltTabIsRunning() {
|
|
||||||
timerPermissionsRemovedWhileAltTabIsRunning = Timer(timeInterval: 5, repeats: true) { _ in
|
|
||||||
DispatchQueue.main.async {
|
DispatchQueue.main.async {
|
||||||
checkPermissionsWhileAltTabIsRunning()
|
preStartupPermissionsPassed = true
|
||||||
|
App.app.permissionsWindow?.close()
|
||||||
|
setInfrequentTimer()
|
||||||
|
App.app.continueAppLaunchAfterPermissionsAreGranted()
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
DispatchQueue.main.async {
|
||||||
|
App.app.permissionsWindow.show()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
timerPermissionsRemovedWhileAltTabIsRunning!.tolerance = 1
|
|
||||||
CFRunLoopAddTimer(BackgroundWork.systemPermissionsThread.runLoop, timerPermissionsRemovedWhileAltTabIsRunning!, .commonModes)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private static func checkPermissionsWhileAltTabIsRunning() {
|
private static func checkPermissionsPostStartup() {
|
||||||
Logger.debug(AccessibilityPermission.status, ScreenRecordingPermission.status, preStartupPermissionsPassed, ScreenRecordingPermission.status, Appearance.hideThumbnails, Preferences.previewFocusedWindow)
|
Logger.debug("accessibility:\(AccessibilityPermission.status), screen-recording:\(ScreenRecordingPermission.status)")
|
||||||
AccessibilityPermission.update()
|
|
||||||
Logger.debug(AccessibilityPermission.status)
|
|
||||||
if AccessibilityPermission.status == .notGranted {
|
if AccessibilityPermission.status == .notGranted {
|
||||||
Logger.info("accessibilityPermission not granted; restarting")
|
Logger.error("accessibility permission revoked while AltTab was running; restarting")
|
||||||
App.app.restart()
|
DispatchQueue.main.async { App.app.restart() }
|
||||||
}
|
}
|
||||||
if ScreenRecordingPermission.status == .skipped || (Appearance.hideThumbnails && !Preferences.previewFocusedWindow) { return }
|
|
||||||
ScreenRecordingPermission.update()
|
|
||||||
Logger.debug(ScreenRecordingPermission.status)
|
|
||||||
Menubar.togglePermissionCallout(ScreenRecordingPermission.status == .skipped)
|
|
||||||
if ScreenRecordingPermission.status == .notGranted {
|
if ScreenRecordingPermission.status == .notGranted {
|
||||||
// permission check may yield a false negative during wake-up
|
// permission check may yield a false negative during wake-up; we restart after 2 negative checks
|
||||||
// we restart after 2 negative checks
|
|
||||||
if ScreenRecordingPermission.flakyCounter >= 2 {
|
if ScreenRecordingPermission.flakyCounter >= 2 {
|
||||||
Logger.info("screenRecordingPermission not granted 3 times; restarting")
|
Logger.error("screen-recording permission revoked while AltTab was running (checked 2 times); restarting")
|
||||||
App.app.restart()
|
DispatchQueue.main.async { App.app.restart() }
|
||||||
} else {
|
} else {
|
||||||
ScreenRecordingPermission.flakyCounter += 1
|
ScreenRecordingPermission.flakyCounter += 1
|
||||||
}
|
}
|
||||||
@@ -64,30 +62,16 @@ class SystemPermissions {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private static func checkPermissionsToUpdatePermissionsWindow(_ startupBlock: @escaping () -> Void) {
|
static func setInfrequentTimer() {
|
||||||
AccessibilityPermission.update()
|
timer.schedule(deadline: .now() + 5, repeating: 5, leeway: .seconds(1))
|
||||||
ScreenRecordingPermission.update()
|
}
|
||||||
Logger.debug(AccessibilityPermission.status, ScreenRecordingPermission.status, preStartupPermissionsPassed)
|
|
||||||
DispatchQueue.main.async {
|
static func setFrequentTimer() {
|
||||||
Menubar.togglePermissionCallout(ScreenRecordingPermission.status == .skipped)
|
timer.schedule(deadline: .now() + 0.5, repeating: 0.5, leeway: .milliseconds(500))
|
||||||
if AccessibilityPermission.status != App.app.permissionsWindow?.accessibilityView?.permissionStatus {
|
}
|
||||||
App.app.permissionsWindow?.accessibilityView.updatePermissionStatus(AccessibilityPermission.status)
|
|
||||||
}
|
private static func setImmediateTimer() {
|
||||||
if #available(macOS 10.15, *), ScreenRecordingPermission.status != App.app.permissionsWindow?.screenRecordingView?.permissionStatus {
|
timer.schedule(deadline: .now(), repeating: .never, leeway: .never)
|
||||||
App.app.permissionsWindow?.screenRecordingView?.updatePermissionStatus(ScreenRecordingPermission.status)
|
|
||||||
}
|
|
||||||
if !preStartupPermissionsPassed {
|
|
||||||
if AccessibilityPermission.status != .notGranted && ScreenRecordingPermission.status != .notGranted {
|
|
||||||
preStartupPermissionsPassed = true
|
|
||||||
App.app.permissionsWindow?.close()
|
|
||||||
startupBlock()
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
if AccessibilityPermission.status == .notGranted || ScreenRecordingPermission.status == .notGranted {
|
|
||||||
App.app.restart()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -176,12 +160,15 @@ class ScreenRecordingPermission {
|
|||||||
private static func runWithTimeout(_ block: @escaping (@escaping (Bool) -> Void) -> Void) -> Bool {
|
private static func runWithTimeout(_ block: @escaping (@escaping (Bool) -> Void) -> Void) -> Bool {
|
||||||
let semaphore = DispatchSemaphore(value: 0)
|
let semaphore = DispatchSemaphore(value: 0)
|
||||||
var result = false
|
var result = false
|
||||||
block { r in
|
BackgroundWork.systemPermissionsSystemCallsQueue.addOperation {
|
||||||
result = r
|
block { r in
|
||||||
semaphore.signal()
|
result = r
|
||||||
|
semaphore.signal()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
let timeoutResult = semaphore.wait(timeout: .now() + 1)
|
let timeoutResult = semaphore.wait(timeout: .now() + 6)
|
||||||
if timeoutResult == .timedOut {
|
if timeoutResult == .timedOut {
|
||||||
|
Logger.error("screen-recording permission call timed out after 6s")
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
return result
|
return result
|
||||||
|
|||||||
@@ -97,7 +97,7 @@ class KeyboardEvents {
|
|||||||
userInfo: nil)
|
userInfo: nil)
|
||||||
if let eventTap {
|
if let eventTap {
|
||||||
let runLoopSource = CFMachPortCreateRunLoopSource(nil, eventTap, 0)
|
let runLoopSource = CFMachPortCreateRunLoopSource(nil, eventTap, 0)
|
||||||
CFRunLoopAddSource(BackgroundWork.keyboardAndTrackpadEventsThread.runLoop, runLoopSource, .commonModes)
|
CFRunLoopAddSource(BackgroundWork.keyboardAndMouseAndTrackpadEventsThread.runLoop, runLoopSource, .commonModes)
|
||||||
} else {
|
} else {
|
||||||
App.app.restart()
|
App.app.restart()
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -28,7 +28,7 @@ class ScrollwheelEvents {
|
|||||||
userInfo: nil)
|
userInfo: nil)
|
||||||
if let eventTap {
|
if let eventTap {
|
||||||
let runLoopSource = CFMachPortCreateRunLoopSource(nil, eventTap, 0)
|
let runLoopSource = CFMachPortCreateRunLoopSource(nil, eventTap, 0)
|
||||||
CFRunLoopAddSource(BackgroundWork.keyboardAndTrackpadEventsThread.runLoop, runLoopSource, .commonModes)
|
CFRunLoopAddSource(BackgroundWork.keyboardAndMouseAndTrackpadEventsThread.runLoop, runLoopSource, .commonModes)
|
||||||
} else {
|
} else {
|
||||||
App.app.restart()
|
App.app.restart()
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -30,7 +30,7 @@ class TrackpadEvents {
|
|||||||
userInfo: nil)
|
userInfo: nil)
|
||||||
if let eventTap {
|
if let eventTap {
|
||||||
let runLoopSource = CFMachPortCreateRunLoopSource(nil, eventTap, 0)
|
let runLoopSource = CFMachPortCreateRunLoopSource(nil, eventTap, 0)
|
||||||
CFRunLoopAddSource(BackgroundWork.keyboardAndTrackpadEventsThread.runLoop, runLoopSource, .commonModes)
|
CFRunLoopAddSource(BackgroundWork.keyboardAndMouseAndTrackpadEventsThread.runLoop, runLoopSource, .commonModes)
|
||||||
} else {
|
} else {
|
||||||
App.app.restart()
|
App.app.restart()
|
||||||
}
|
}
|
||||||
|
|||||||
+29
-28
@@ -124,7 +124,7 @@ class App: AppCenterApplication {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@objc func checkPermissions(_ sender: NSMenuItem) {
|
@objc func checkPermissions(_ sender: NSMenuItem) {
|
||||||
permissionsWindow.show({})
|
permissionsWindow.show()
|
||||||
}
|
}
|
||||||
|
|
||||||
@objc func supportProject() {
|
@objc func supportProject() {
|
||||||
@@ -287,35 +287,36 @@ extension App: NSApplicationDelegate {
|
|||||||
#endif
|
#endif
|
||||||
AXUIElement.setGlobalTimeout()
|
AXUIElement.setGlobalTimeout()
|
||||||
Preferences.initialize()
|
Preferences.initialize()
|
||||||
BackgroundWork.startSystemPermissionThread()
|
|
||||||
permissionsWindow = PermissionsWindow()
|
permissionsWindow = PermissionsWindow()
|
||||||
SystemPermissions.ensurePermissionsAreGranted { [weak self] in
|
BackgroundWork.preStart()
|
||||||
guard let self else { return }
|
SystemPermissions.ensurePermissionsAreGranted()
|
||||||
Logger.info("System Permissions are granted")
|
}
|
||||||
BackgroundWork.start()
|
|
||||||
NSScreen.updatePreferred()
|
func continueAppLaunchAfterPermissionsAreGranted() {
|
||||||
Appearance.update()
|
Logger.info("system permissions are granted")
|
||||||
Menubar.initialize()
|
BackgroundWork.start()
|
||||||
MainMenu.loadFromXib()
|
NSScreen.updatePreferred()
|
||||||
self.thumbnailsPanel = ThumbnailsPanel()
|
Appearance.update()
|
||||||
self.previewPanel = PreviewPanel()
|
Menubar.initialize()
|
||||||
Spaces.refresh()
|
MainMenu.loadFromXib()
|
||||||
SpacesEvents.observe()
|
self.thumbnailsPanel = ThumbnailsPanel()
|
||||||
ScreensEvents.observe()
|
self.previewPanel = PreviewPanel()
|
||||||
SystemAppearanceEvents.observe()
|
Spaces.refresh()
|
||||||
SystemScrollerStyleEvents.observe()
|
SpacesEvents.observe()
|
||||||
Applications.initialDiscovery()
|
ScreensEvents.observe()
|
||||||
self.preferencesWindow = PreferencesWindow()
|
SystemAppearanceEvents.observe()
|
||||||
self.feedbackWindow = FeedbackWindow()
|
SystemScrollerStyleEvents.observe()
|
||||||
KeyboardEvents.addEventHandlers()
|
Applications.initialDiscovery()
|
||||||
MouseEvents.observe()
|
self.preferencesWindow = PreferencesWindow()
|
||||||
TrackpadEvents.observe()
|
self.feedbackWindow = FeedbackWindow()
|
||||||
CliEvents.observe()
|
KeyboardEvents.addEventHandlers()
|
||||||
Logger.info("AltTab finished launching")
|
MouseEvents.observe()
|
||||||
#if DEBUG
|
TrackpadEvents.observe()
|
||||||
|
CliEvents.observe()
|
||||||
|
Logger.info("AltTab finished launching")
|
||||||
|
#if DEBUG
|
||||||
// self.showPreferencesWindow()
|
// self.showPreferencesWindow()
|
||||||
#endif
|
#endif
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func applicationShouldHandleReopen(_ sender: NSApplication, hasVisibleWindows flag: Bool) -> Bool {
|
func applicationShouldHandleReopen(_ sender: NSApplication, hasVisibleWindows flag: Bool) -> Bool {
|
||||||
|
|||||||
@@ -4,6 +4,7 @@ class Menubar {
|
|||||||
static var statusItem: NSStatusItem!
|
static var statusItem: NSStatusItem!
|
||||||
static var menu: NSMenu!
|
static var menu: NSMenu!
|
||||||
static var permissionCalloutMenuItems: [NSMenuItem]?
|
static var permissionCalloutMenuItems: [NSMenuItem]?
|
||||||
|
static var isShowingPermissionCallout = false
|
||||||
|
|
||||||
static func addMenuItem(_ title: String, _ action: Selector, _ keyEquivalent: String, _ symbolName: String?, _ color: NSColor? = nil) {
|
static func addMenuItem(_ title: String, _ action: Selector, _ keyEquivalent: String, _ symbolName: String?, _ color: NSColor? = nil) {
|
||||||
let item = menu.addItem(withTitle: title, action: action, keyEquivalent: keyEquivalent)
|
let item = menu.addItem(withTitle: title, action: action, keyEquivalent: keyEquivalent)
|
||||||
@@ -41,6 +42,8 @@ class Menubar {
|
|||||||
|
|
||||||
// NSMenuItem.isHidden isn't reliable with custom views. We add/remove to hide/show these items
|
// NSMenuItem.isHidden isn't reliable with custom views. We add/remove to hide/show these items
|
||||||
static func togglePermissionCallout(_ show: Bool) {
|
static func togglePermissionCallout(_ show: Bool) {
|
||||||
|
guard isShowingPermissionCallout != show else { return }
|
||||||
|
isShowingPermissionCallout = show
|
||||||
permissionCalloutMenuItems?.enumerated().forEach { offset, element in
|
permissionCalloutMenuItems?.enumerated().forEach { offset, element in
|
||||||
if show && !menu.items.contains(element) {
|
if show && !menu.items.contains(element) {
|
||||||
menu.insertItem(element, at: offset)
|
menu.insertItem(element, at: offset)
|
||||||
|
|||||||
@@ -43,6 +43,7 @@ class PermissionView: StackView {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func updatePermissionStatus(_ permissionStatus: PermissionStatus) {
|
func updatePermissionStatus(_ permissionStatus: PermissionStatus) {
|
||||||
|
guard status.stringValue.isEmpty || permissionStatus != self.permissionStatus else { return }
|
||||||
self.permissionStatus = permissionStatus
|
self.permissionStatus = permissionStatus
|
||||||
var color: NSColor
|
var color: NSColor
|
||||||
var label: String
|
var label: String
|
||||||
|
|||||||
@@ -13,19 +13,22 @@ class PermissionsWindow: NSWindow {
|
|||||||
setupView()
|
setupView()
|
||||||
}
|
}
|
||||||
|
|
||||||
func show(_ startupBlock: @escaping () -> Void) {
|
func show() {
|
||||||
accessibilityView.updatePermissionStatus(AccessibilityPermission.update())
|
guard !isVisible else { return }
|
||||||
if #available(macOS 10.15, *) {
|
Logger.debug()
|
||||||
screenRecordingView.updatePermissionStatus(ScreenRecordingPermission.update())
|
SystemPermissions.setFrequentTimer()
|
||||||
}
|
updatePermissionViews()
|
||||||
center()
|
center()
|
||||||
App.shared.activate(ignoringOtherApps: true)
|
App.shared.activate(ignoringOtherApps: true)
|
||||||
makeKeyAndOrderFront(nil)
|
makeKeyAndOrderFront(nil)
|
||||||
if #available(macOS 10.15, *), !SystemPermissions.preStartupPermissionsPassed {
|
}
|
||||||
// this call triggers the permission prompt, however it's the only way to force the app to be listed with a checkbox
|
|
||||||
SLSRequestScreenCaptureAccess()
|
func updatePermissionViews() {
|
||||||
|
guard isVisible else { return }
|
||||||
|
accessibilityView.updatePermissionStatus(AccessibilityPermission.status)
|
||||||
|
if #available(macOS 10.15, *) {
|
||||||
|
screenRecordingView.updatePermissionStatus(ScreenRecordingPermission.status)
|
||||||
}
|
}
|
||||||
SystemPermissions.pollPermissionsToUpdatePermissionsWindow(startupBlock)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private func setupWindow() {
|
private func setupWindow() {
|
||||||
@@ -84,17 +87,18 @@ class PermissionsWindow: NSWindow {
|
|||||||
}
|
}
|
||||||
|
|
||||||
extension PermissionsWindow: NSWindowDelegate {
|
extension PermissionsWindow: NSWindowDelegate {
|
||||||
func windowWillClose(_ notification: Notification) {
|
func windowShouldClose(_ sender: NSWindow) -> Bool {
|
||||||
Logger.debug(SystemPermissions.preStartupPermissionsPassed)
|
Logger.debug("preStartupPermissionsPassed:\(SystemPermissions.preStartupPermissionsPassed), accessibility:\(AccessibilityPermission.status), screen-recording:\(ScreenRecordingPermission.status)")
|
||||||
if !SystemPermissions.preStartupPermissionsPassed {
|
if !SystemPermissions.preStartupPermissionsPassed {
|
||||||
if AccessibilityPermission.update() == .notGranted || ScreenRecordingPermission.update() == .notGranted {
|
if AccessibilityPermission.status == .notGranted || ScreenRecordingPermission.status == .notGranted {
|
||||||
Logger.error("Before using this app, you need to give permission in System Settings > Privacy & Security > Accessibility.",
|
Logger.error("Before using this app, you need to give permission in System Settings > Privacy & Security > Accessibility.",
|
||||||
"Please authorize and re-launch.",
|
"Please authorize and re-launch.",
|
||||||
"See https://help.rescuetime.com/article/59-how-do-i-enable-accessibility-permissions-on-mac-osx")
|
"See https://help.rescuetime.com/article/59-how-do-i-enable-accessibility-permissions-on-mac-osx")
|
||||||
App.shared.terminate(self)
|
App.shared.terminate(self)
|
||||||
|
return false // prevent the close; termination will close everything once
|
||||||
}
|
}
|
||||||
} else {
|
|
||||||
SystemPermissions.timerPermissionsToUpdatePermissionsWindow?.invalidate()
|
|
||||||
}
|
}
|
||||||
|
SystemPermissions.setInfrequentTimer()
|
||||||
|
return true
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Reference in new issue
Block a user