Skip to content

Commit

Permalink
Merge pull request #264 from wakatime/main
Browse files Browse the repository at this point in the history
Release v5.15.0
  • Loading branch information
alanhamlett committed May 7, 2024
2 parents 4299aa0 + b9440bd commit 2152acc
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 3 deletions.
2 changes: 1 addition & 1 deletion README.md
Expand Up @@ -55,4 +55,4 @@ Made with :heart: by the WakaTime Team.
[dashboard]: https://wakatime.com/
[contributing]: CONTRIBUTING.md
[authors]: AUTHORS
[supported apps]: https://github.com/wakatime/macos-wakatime/blob/main/WakaTime/Extensions/NSRunningApplicationExtension.swift#L3
[supported apps]: https://github.com/wakatime/macos-wakatime/blob/main/WakaTime/Watchers/MonitoredApp.swift#L3
16 changes: 16 additions & 0 deletions WakaTime/Helpers/PropertiesManager.swift
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
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
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
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 2152acc

Please sign in to comment.