Skip to content

Commit

Permalink
show accessibility permission status in app menu (#29)
Browse files Browse the repository at this point in the history
  • Loading branch information
artginzburg committed Sep 1, 2022
1 parent acdb42f commit d44200e
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 0 deletions.
2 changes: 2 additions & 0 deletions MiddleClick/TrayMenu.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
@private
NSStatusItem* _statusItem;
Controller* myController;
NSMenuItem* accessibilityPermissionStatusItem;
NSMenuItem* accessibilityPermissionActionItem;
NSMenuItem* tapItem;
NSMenuItem* clickItem;
}
Expand Down
43 changes: 43 additions & 0 deletions MiddleClick/TrayMenu.m
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down Expand Up @@ -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:)
Expand Down Expand Up @@ -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];
Expand All @@ -110,6 +151,8 @@ - (void)applicationDidFinishLaunching:(NSNotification*)notification
_statusItem.menu = menu;
_statusItem.button.toolTip = @"MiddleClick";
_statusItem.button.image = icon;

[self initAccessibilityPermissionStatus:menu];

[menu release];
}
Expand Down

0 comments on commit d44200e

Please sign in to comment.