Skip to content

Commit

Permalink
Ability to disable a11y permission
Browse files Browse the repository at this point in the history
  • Loading branch information
alanhamlett committed May 7, 2024
1 parent e7bbb79 commit 2b340b7
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 2 deletions.
16 changes: 16 additions & 0 deletions WakaTime/Helpers/PropertiesManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ class PropertiesManager {
enum Keys: String {
case shouldLaunchOnLogin = "launch_on_login"
case shouldLogToFile = "log_to_file"
case shouldRequestA11y = "request_a11y"
case shouldAutomaticallyDownloadUpdates = "should_automatically_download_updates"
case hasLaunchedBefore = "has_launched_before"
case domainPreference = "domain_preference"
Expand Down Expand Up @@ -72,6 +73,21 @@ class PropertiesManager {
}
}

static var shouldRequestA11yPermission: Bool {
get {
guard UserDefaults.standard.string(forKey: Keys.shouldRequestA11y.rawValue) != nil else {
UserDefaults.standard.set(true, forKey: Keys.shouldRequestA11y.rawValue)
return true
}

return UserDefaults.standard.bool(forKey: Keys.shouldRequestA11y.rawValue)
}
set {
UserDefaults.standard.set(newValue, forKey: Keys.shouldRequestA11y.rawValue)
UserDefaults.standard.synchronize()
}
}

static var hasLaunchedBefore: Bool {
get {
guard UserDefaults.standard.string(forKey: Keys.hasLaunchedBefore.rawValue) != nil else {
Expand Down
21 changes: 20 additions & 1 deletion WakaTime/Views/SettingsView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,18 @@ class SettingsView: NSView, NSTextFieldDelegate, NSTextViewDelegate {
return checkbox
}()

lazy var requestA11yCheckbox: NSButton = {
let checkbox = NSButton(
checkboxWithTitle: "Enable stats from Xcode by requesting accessibility permission",
target: self,
action: #selector(enableA11yCheckboxClicked)
)
checkbox.state = PropertiesManager.shouldRequestA11yPermission ? .on : .off
return checkbox
}()

lazy var checkboxesStackView: NSStackView = {
let stack = NSStackView(views: [launchAtLoginCheckbox, enableLoggingCheckbox])
let stack = NSStackView(views: [launchAtLoginCheckbox, requestA11yCheckbox, enableLoggingCheckbox])
stack.alignment = .leading
stack.orientation = .vertical
stack.spacing = 10
Expand Down Expand Up @@ -233,6 +243,15 @@ class SettingsView: NSView, NSTextFieldDelegate, NSTextViewDelegate {
}
}

@objc func enableA11yCheckboxClicked() {
PropertiesManager.shouldRequestA11yPermission = requestA11yCheckbox.state == .on
if requestA11yCheckbox.state == .on {
PropertiesManager.shouldRequestA11yPermission = true
} else {
PropertiesManager.shouldRequestA11yPermission = false
}
}

@objc func domainPreferenceDidChange(_ sender: NSSegmentedControl) {
PropertiesManager.domainPreference = sender.selectedSegment == 0 ? .domain : .url
updateDomainPreference(animate: true)
Expand Down
2 changes: 1 addition & 1 deletion WakaTime/WakaTime.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ class WakaTime: HeartbeatEventHandler {

Dependencies.installDependencies()
if SettingsManager.shouldRegisterAsLoginItem() { SettingsManager.registerAsLoginItem() }
if !Accessibility.requestA11yPermission() {
if PropertiesManager.shouldRequestA11yPermission && !Accessibility.requestA11yPermission() {
delegate.a11yStatusChanged(false)
}

Expand Down
4 changes: 4 additions & 0 deletions WakaTime/Watchers/Watcher.swift
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,10 @@ class Watcher: NSObject {
} catch {
Logging.default.log("Failed to setup AXObserver: \(error.localizedDescription)")

guard PropertiesManager.shouldRequestA11yPermission else {
return
}

// TODO: App could be still launching, retry setting AXObserver in 20 seconds for this app

if lastCheckedA11y.timeIntervalSinceNow > 60 {
Expand Down

0 comments on commit 2b340b7

Please sign in to comment.