Skip to content

Commit

Permalink
Merge pull request #146 from wakatime/feature/status-bar-icon-disabled
Browse files Browse the repository at this point in the history
Show indicator when a11y permission disabled
  • Loading branch information
alanhamlett authored Aug 10, 2023
2 parents 96ca1e8 + b9c9ea7 commit dc9117f
Show file tree
Hide file tree
Showing 7 changed files with 69 additions and 10 deletions.
31 changes: 27 additions & 4 deletions WakaTime/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,18 @@ import AppUpdater
import Cocoa
import UserNotifications

class AppDelegate: NSObject, NSApplicationDelegate {
class AppDelegate: NSObject, NSApplicationDelegate, StatusBarDelegate {
var window: NSWindow!
var statusBarItem: NSStatusItem!
var statusBarA11yItem: NSMenuItem!
var statusBarA11yStatus: Bool = true
var settingsWindowController = SettingsWindowController()
var monitoredAppsWindowController = MonitoredAppsWindowController()
var wakaTime: WakaTime?

let updater = AppUpdater(owner: "wakatime", repo: "macos-wakatime")

func applicationDidFinishLaunching(_ aNotification: Notification) {

wakaTime = WakaTime()

// Handle deep links
let eventManager = NSAppleEventManager.shared()
eventManager.setEventHandler(
Expand All @@ -29,6 +28,12 @@ class AppDelegate: NSObject, NSApplicationDelegate {

let menu = NSMenu()

statusBarA11yItem = NSMenuItem(
title: "* A11y permission needed *",
action: #selector(AppDelegate.a11yClicked(_:)),
keyEquivalent: "")
statusBarA11yItem.isHidden = true
menu.addItem(statusBarA11yItem)
menu.addItem(withTitle: "Dashboard", action: #selector(AppDelegate.dashboardClicked(_:)), keyEquivalent: "")
menu.addItem(withTitle: "Settings", action: #selector(AppDelegate.settingsClicked(_:)), keyEquivalent: "")
menu.addItem(withTitle: "Monitored Apps", action: #selector(AppDelegate.monitoredAppsClicked(_:)), keyEquivalent: "")
Expand All @@ -38,6 +43,8 @@ class AppDelegate: NSObject, NSApplicationDelegate {
menu.addItem(withTitle: "Quit", action: #selector(AppDelegate.quitClicked(_:)), keyEquivalent: "")

statusBarItem.menu = menu

wakaTime = WakaTime(self)
}

@objc func handleGetURL(_ event: NSAppleEventDescriptor, withReplyEvent replyEvent: NSAppleEventDescriptor) {
Expand Down Expand Up @@ -93,10 +100,26 @@ class AppDelegate: NSObject, NSApplicationDelegate {
}
}

@objc func a11yClicked(_ sender: AnyObject) {
a11yStatusChanged(Accessibility.requestA11yPermission())
}

@objc func quitClicked(_ sender: AnyObject) {
NSApplication.shared.terminate(self)
}

func a11yStatusChanged(_ hasPermission: Bool) {
guard statusBarA11yStatus != hasPermission else { return }

statusBarA11yStatus = hasPermission
if hasPermission {
statusBarItem.button?.image = NSImage(named: NSImage.Name("WakaTime"))
} else {
statusBarItem.button?.image = NSImage(named: NSImage.Name("WakaTimeDisabled"))
}
statusBarA11yItem.isHidden = hasPermission
}

private func showSettings() {
NSApp.activate(ignoringOtherApps: true)
settingsWindowController.showWindow(self)
Expand Down
6 changes: 2 additions & 4 deletions WakaTime/Helpers/Accessibility.swift
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
import AppKit

class Accessibility {
public static func requestA11yPermission() {
public static func requestA11yPermission() -> Bool {
let prompt = kAXTrustedCheckOptionPrompt.takeUnretainedValue() as String
let options: NSDictionary = [prompt: true]
let appHasPermission = AXIsProcessTrustedWithOptions(options)
if appHasPermission {
// print("has a11y permission")
}
return appHasPermission
}
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
{
"images" : [
{
"idiom" : "universal",
"scale" : "1x"
},
{
"filename" : "32.png",
"idiom" : "universal",
"scale" : "2x"
},
{
"filename" : "64.png",
"idiom" : "universal",
"scale" : "3x"
}
],
"info" : {
"author" : "xcode",
"version" : 1
},
"properties" : {
"template-rendering-intent" : "template"
}
}
14 changes: 12 additions & 2 deletions WakaTime/WakaTime.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ class WakaTime {
// MARK: Watcher

let watcher = Watcher()
let delegate: StatusBarDelegate

// MARK: Watcher State

Expand All @@ -24,14 +25,19 @@ class WakaTime {

// MARK: Initialization and Setup

init() {
init(_ delegate: StatusBarDelegate) {
self.delegate = delegate

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

configureFirebase()
checkForApiKey()
watcher.eventHandler = handleEvent
watcher.statusBarDelegate = delegate
}

private func configureFirebase() {
Expand Down Expand Up @@ -115,3 +121,7 @@ class WakaTime {
}
}
}

protocol StatusBarDelegate {
func a11yStatusChanged(_ hasPermission: Bool) -> Void
}
3 changes: 3 additions & 0 deletions WakaTime/Watcher.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ class Watcher: NSObject {

var appVersions: [String: String] = [:]
var eventHandler: ((_ app: NSRunningApplication, _ path: URL, _ isWrite: Bool, _ isBuilding: Bool) -> Void)?
var statusBarDelegate: StatusBarDelegate?
var isBuilding = false
var activeApp: NSRunningApplication?
private var observer: AXObserver?
Expand Down Expand Up @@ -117,8 +118,10 @@ class Watcher: NSObject {
observeActivityText(activeWindow: activeWindow)
}
// NSLog("Watching for file changes on \(app.localizedName ?? "nil")")
self.statusBarDelegate?.a11yStatusChanged(true)
} catch {
NSLog("Failed to setup AXObserver: \(error.localizedDescription)")
self.statusBarDelegate?.a11yStatusChanged(false)
}
}

Expand Down

0 comments on commit dc9117f

Please sign in to comment.