Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ability to disable a11y permission #263

Merged
merged 1 commit into from
May 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading