From d44200e5091931a0228e313235782c376284ac58 Mon Sep 17 00:00:00 2001 From: Art Ginzburg Date: Fri, 2 Sep 2022 00:06:51 +0300 Subject: [PATCH] show accessibility permission status in app menu (#29) --- MiddleClick/TrayMenu.h | 2 ++ MiddleClick/TrayMenu.m | 43 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 45 insertions(+) diff --git a/MiddleClick/TrayMenu.h b/MiddleClick/TrayMenu.h index 56f2bd6..cfa7173 100644 --- a/MiddleClick/TrayMenu.h +++ b/MiddleClick/TrayMenu.h @@ -5,6 +5,8 @@ @private NSStatusItem* _statusItem; Controller* myController; + NSMenuItem* accessibilityPermissionStatusItem; + NSMenuItem* accessibilityPermissionActionItem; NSMenuItem* tapItem; NSMenuItem* clickItem; } diff --git a/MiddleClick/TrayMenu.m b/MiddleClick/TrayMenu.m index 8b77aeb..828094f 100644 --- a/MiddleClick/TrayMenu.m +++ b/MiddleClick/TrayMenu.m @@ -12,12 +12,43 @@ - (id)initWithController:(Controller*)ctrl return self; } +- (void)initAccessibilityPermissionStatus:(NSMenu*)menu +{ + BOOL hasAccessibilityPermission = AXIsProcessTrusted(); + + [self updateAccessibilityPermissionStatus:menu + hasAccessibilityPermission:hasAccessibilityPermission]; + + if (!hasAccessibilityPermission) { + [NSTimer scheduledTimerWithTimeInterval:0.3 repeats:NO block:^(NSTimer* timer) { + [self initAccessibilityPermissionStatus:menu]; + }]; + } +} +- (void)updateAccessibilityPermissionStatus:(NSMenu*)menu + hasAccessibilityPermission:(BOOL)isTrusted +{ + _statusItem.button.appearsDisabled = !isTrusted; + accessibilityPermissionStatusItem.hidden = isTrusted; + accessibilityPermissionActionItem.hidden = isTrusted; +} + - (void)openWebsite:(id)sender { NSURL* url = [NSURL URLWithString:@"https://github.com/artginzburg/MiddleClick-BigSur"]; [[NSWorkspace sharedWorkspace] openURL:url]; } +- (void)openAccessibilitySettings:(id)sender +{ + NSAppleScript *a = [[NSAppleScript alloc] initWithSource: + @"tell application \"System Preferences\"\n" + "activate\n" + "reveal anchor \"Privacy_Accessibility\" of pane \"com.apple.preference.security\"\n" + "end tell"]; + [a executeAndReturnError:nil]; + [a release]; +} - (void)setClick:(id)sender { @@ -56,6 +87,8 @@ - (NSMenu*)createMenu int fingersQua = (int)[[NSUserDefaults standardUserDefaults] integerForKey:@"fingers"]; + [self createMenuAccessibilityPermissionItems:menu]; + // Add About menuItem = [menu addItemWithTitle:@"About MiddleClick..." action:@selector(openWebsite:) @@ -88,6 +121,14 @@ - (NSMenu*)createMenu return menu; } +- (void)createMenuAccessibilityPermissionItems:(NSMenu *)menu +{ + accessibilityPermissionStatusItem = [menu addItemWithTitle:@"Missing Accessibility permission" action:nil keyEquivalent:@""]; + accessibilityPermissionActionItem = [menu addItemWithTitle:@"Open Privacy Preferences" action:@selector(openAccessibilitySettings:) keyEquivalent:@","]; + + [menu addItem:[NSMenuItem separatorItem]]; +} + - (void)applicationDidFinishLaunching:(NSNotification*)notification { NSMenu* menu = [self createMenu]; @@ -110,6 +151,8 @@ - (void)applicationDidFinishLaunching:(NSNotification*)notification _statusItem.menu = menu; _statusItem.button.toolTip = @"MiddleClick"; _statusItem.button.image = icon; + + [self initAccessibilityPermissionStatus:menu]; [menu release]; }