Skip to content

Commit

Permalink
Merge pull request #56 from artginzburg/17-an-option-for-either-both-…
Browse files Browse the repository at this point in the history
…tap-and-click

Better UX of the click mode setting
  • Loading branch information
artginzburg authored Jan 4, 2023
2 parents 3bf4287 + 87bcc75 commit b2e45a2
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 28 deletions.
1 change: 1 addition & 0 deletions MiddleClick/Controller.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,6 @@
- (void)start;
- (void)setMode:(BOOL)click;
- (BOOL)getClickMode;
- (void)resetClickMode;

@end
28 changes: 26 additions & 2 deletions MiddleClick/Controller.m
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,8 @@ - (void)start

fingersQua = [[NSUserDefaults standardUserDefaults] integerForKey:@"fingers"];

needToClick =
[[NSUserDefaults standardUserDefaults] boolForKey:@"needClick"];
NSString* needToClickNullable = [[NSUserDefaults standardUserDefaults] valueForKey:@"needClick"];
needToClick = needToClickNullable ? [[NSUserDefaults standardUserDefaults] boolForKey:@"needClick"] : [self getIsSystemTapToClickDisabled];

NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init];
[NSApplication sharedApplication];
Expand Down Expand Up @@ -230,6 +230,11 @@ - (void)setMode:(BOOL)click
[[NSUserDefaults standardUserDefaults] setBool:click forKey:@"needClick"];
needToClick = click;
}
- (void)resetClickMode
{
[[NSUserDefaults standardUserDefaults] removeObjectForKey:@"needClick"];
needToClick = [self getIsSystemTapToClickDisabled];
}

// listening to mouse clicks to replace them with middle clicks if there are 3
// fingers down at the time of clicking this is done by replacing the left click
Expand Down Expand Up @@ -391,4 +396,23 @@ static void unregisterMTDeviceCallback(MTDeviceRef device, MTContactCallbackFunc
MTDeviceRelease(device);
}

- (BOOL)getIsSystemTapToClickDisabled {
NSString* isSystemTapToClickEnabled = [self runCommand:(@"defaults read com.apple.driver.AppleBluetoothMultitouch.trackpad Clicking")];
return [isSystemTapToClickEnabled isEqualToString:@"0\n"];
}

- (NSString *)runCommand:(NSString *)commandToRun {
NSPipe* pipe = [NSPipe pipe];

NSTask* task = [[NSTask alloc] init];
[task setLaunchPath: @"/bin/sh"];
[task setArguments:@[@"-c", [NSString stringWithFormat:@"%@", commandToRun]]];
[task setStandardOutput:pipe];

NSFileHandle* file = [pipe fileHandleForReading];
[task launch];

return [[NSString alloc] initWithData:[file readDataToEndOfFile] encoding:NSUTF8StringEncoding];
}

@end
8 changes: 4 additions & 4 deletions MiddleClick/TrayMenu.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@
Controller* myController;
NSMenuItem* accessibilityPermissionStatusItem;
NSMenuItem* accessibilityPermissionActionItem;
NSMenuItem* tapItem;
NSMenuItem* clickItem;
NSMenuItem* infoItem;
NSMenuItem* tapToClickItem;
}
- (id)initWithController:(Controller*)ctrl;
- (void)setChecks;
- (void)setClick:(id)sender;
- (void)setTap:(id)sender;
- (void)toggleTapToClick:(id)sender;
- (void)resetTapToClick:(id)sender;
@end
49 changes: 27 additions & 22 deletions MiddleClick/TrayMenu.m
Original file line number Diff line number Diff line change
Expand Up @@ -57,27 +57,27 @@ - (void)openAccessibilitySettings:(id)sender
}
}

- (void)setClick:(id)sender
- (void)toggleTapToClick:(id)sender
{
[myController setMode:YES];
[myController setMode:[sender state] == NSControlStateValueOn];
[self setChecks];
}

- (void)setTap:(id)sender
- (void)resetTapToClick:(id)sender
{
[myController setMode:NO];
[myController resetClickMode];
[self setChecks];
}

- (void)setChecks
{
if ([myController getClickMode]) {
[clickItem setState:NSControlStateValueOn];
[tapItem setState:NSControlStateValueOff];
} else {
[clickItem setState:NSControlStateValueOff];
[tapItem setState:NSControlStateValueOn];
}
bool clickMode = [myController getClickMode];
NSString* clickModeInfo = clickMode ? @"Click" : @"Click or Tap";

int fingersQua = (int)[[NSUserDefaults standardUserDefaults] integerForKey:@"fingers"];

[infoItem setTitle:[clickModeInfo stringByAppendingFormat: @" with %d Fingers", fingersQua]];
[tapToClickItem setState:clickMode ? NSControlStateValueOff : NSControlStateValueOn];
}

- (void)actionQuit:(id)sender
Expand All @@ -92,8 +92,6 @@ - (NSMenu*)createMenu



int fingersQua = (int)[[NSUserDefaults standardUserDefaults] integerForKey:@"fingers"];

[self createMenuAccessibilityPermissionItems:menu];

// Add About
Expand All @@ -104,15 +102,22 @@ - (NSMenu*)createMenu

[menu addItem:[NSMenuItem separatorItem]];

clickItem = [menu addItemWithTitle:[NSString stringWithFormat: @"%d Finger Click", fingersQua]
action:@selector(setClick:)
keyEquivalent:@""];
[clickItem setTarget:self];

tapItem = [menu addItemWithTitle:[NSString stringWithFormat: @"%d Finger Tap", fingersQua]
action:@selector(setTap:)
keyEquivalent:@""];
[tapItem setTarget:self];
infoItem = [menu addItemWithTitle:@""
action:nil
keyEquivalent:@""];
[infoItem setTarget:self];

tapToClickItem = [menu addItemWithTitle:@"Tap to click"
action:@selector(toggleTapToClick:)
keyEquivalent:@""];
[tapToClickItem setTarget:self];

NSMenuItem* resetItem = [menu addItemWithTitle:@"Reset to System Settings"
action:@selector(resetTapToClick:)
keyEquivalent:@""];
resetItem.alternate = YES;
resetItem.keyEquivalentModifierMask = NSEventModifierFlagOption;
[resetItem setTarget:self];

[self setChecks];

Expand Down

0 comments on commit b2e45a2

Please sign in to comment.