From 9d7f100f57dded88fa998a86715a919ca471c3db Mon Sep 17 00:00:00 2001 From: Joe Wreschnig Date: Sat, 9 Mar 2013 20:48:40 +0100 Subject: [PATCH] Add support for horizontal directions to scrolling. Rework UI to make the relationship between scrolling type and speed more obvious. --- Classes/NJOutputController.h | 2 + Classes/NJOutputController.m | 43 ++++++---- Classes/NJOutputMouseScroll.h | 1 + Classes/NJOutputMouseScroll.m | 26 ++++-- Info.plist | 2 +- Resources/English.lproj/MainMenu.xib | 120 ++++++++++++++++++++++----- 6 files changed, 145 insertions(+), 49 deletions(-) diff --git a/Classes/NJOutputController.h b/Classes/NJOutputController.h index f8052e7..6f4c866 100644 --- a/Classes/NJOutputController.h +++ b/Classes/NJOutputController.h @@ -25,6 +25,7 @@ IBOutlet NSPopUpButton *mappingPopup; IBOutlet NJMappingsController *mappingsController; IBOutlet NJDeviceController *inputController; + IBOutlet NSButton *smoothCheck; } @property (assign) BOOL enabled; @@ -36,6 +37,7 @@ - (IBAction)sdirChanged:(id)sender; - (IBAction)mouseSpeedChanged:(id)sender; - (IBAction)scrollSpeedChanged:(id)sender; +- (IBAction)scrollTypeChanged:(id)sender; - (void)focusKey; diff --git a/Classes/NJOutputController.m b/Classes/NJOutputController.m index d022406..cbb48f7 100644 --- a/Classes/NJOutputController.m +++ b/Classes/NJOutputController.m @@ -71,16 +71,13 @@ if (row != 5) { scrollDirSelect.selectedSegment = -1; scrollSpeedSlider.floatValue = scrollSpeedSlider.minValue; + smoothCheck.state = NSOffState; [scrollDirSelect resignIfFirstResponder]; + [scrollSpeedSlider resignIfFirstResponder]; + [smoothCheck resignIfFirstResponder]; } else { if (scrollDirSelect.selectedSegment == -1) scrollDirSelect.selectedSegment = 0; - if (scrollDirSelect.selectedSegment < 2 - && !scrollSpeedSlider.floatValue) - scrollSpeedSlider.floatValue = 15; - else if (scrollDirSelect.selectedSegment >= 2 - && scrollSpeedSlider.floatValue) - scrollSpeedSlider.floatValue = scrollSpeedSlider.minValue; } } @@ -129,8 +126,6 @@ - (void)sdirChanged:(NSView *)sender { [radioButtons selectCellAtRow:5 column:0]; - if (scrollDirSelect.selectedSegment >= 2) - scrollSpeedSlider.floatValue = 0; [sender.window makeFirstResponder:sender]; [self commit]; } @@ -138,10 +133,19 @@ - (void)scrollSpeedChanged:(NSSlider *)sender { [radioButtons selectCellAtRow:5 column:0]; [sender.window makeFirstResponder:sender]; - if (!sender.floatValue && scrollDirSelect.selectedSegment < 2) - scrollDirSelect.selectedSegment += 2; - else if (sender.floatValue && scrollDirSelect.selectedSegment >= 2) - scrollDirSelect.selectedSegment -= 2; + [self commit]; +} + +- (IBAction)scrollTypeChanged:(NSButton *)sender { + [radioButtons selectCellAtRow:5 column:0]; + [sender.window makeFirstResponder:sender]; + if (sender.state == NSOnState) { + scrollSpeedSlider.floatValue = (scrollSpeedSlider.maxValue - scrollSpeedSlider.minValue) / 2; + [scrollSpeedSlider setEnabled:YES]; + } else { + scrollSpeedSlider.floatValue = scrollSpeedSlider.minValue; + [scrollSpeedSlider setEnabled:NO]; + } [self commit]; } @@ -180,10 +184,9 @@ } case 5: { NJOutputMouseScroll *ms = [[NJOutputMouseScroll alloc] init]; - ms.direction = (scrollDirSelect.selectedSegment & 1) ? 1 : -1; - ms.speed = scrollDirSelect.selectedSegment < 2 - ? scrollSpeedSlider.floatValue - : 0.f; + ms.direction = [scrollDirSelect.cell tagForSegment:scrollDirSelect.selectedSegment]; + ms.speed = scrollSpeedSlider.floatValue; + ms.smooth = smoothCheck.state == NSOnState; return ms; } default: @@ -209,7 +212,8 @@ [mouseSpeedSlider setEnabled:enabled]; [mouseBtnSelect setEnabled:enabled]; [scrollDirSelect setEnabled:enabled]; - [scrollSpeedSlider setEnabled:enabled]; + [smoothCheck setEnabled:enabled]; + [scrollSpeedSlider setEnabled:enabled && smoothCheck.isEnabled]; } - (void)loadOutput:(NJOutput *)output forInput:(NJInput *)input { @@ -248,8 +252,11 @@ [radioButtons selectCellAtRow:5 column:0]; int direction = [(NJOutputMouseScroll *)output direction]; float speed = [(NJOutputMouseScroll *)output speed]; - scrollDirSelect.selectedSegment = (direction > 0) + !speed * 2; + BOOL smooth = [(NJOutputMouseScroll *)output smooth]; + [scrollDirSelect selectSegmentWithTag:direction]; scrollSpeedSlider.floatValue = speed; + smoothCheck.state = smooth ? NSOnState : NSOffState; + [scrollSpeedSlider setEnabled:smooth]; } else { [radioButtons selectCellAtRow:self.enabled ? 0 : -1 column:0]; } diff --git a/Classes/NJOutputMouseScroll.h b/Classes/NJOutputMouseScroll.h index dd0444a..dac9144 100644 --- a/Classes/NJOutputMouseScroll.h +++ b/Classes/NJOutputMouseScroll.h @@ -11,5 +11,6 @@ @property (nonatomic, assign) int direction; @property (nonatomic, assign) float speed; +@property (nonatomic, assign) BOOL smooth; @end diff --git a/Classes/NJOutputMouseScroll.m b/Classes/NJOutputMouseScroll.m index 99cec4a..b97a3e7 100644 --- a/Classes/NJOutputMouseScroll.m +++ b/Classes/NJOutputMouseScroll.m @@ -16,7 +16,8 @@ - (NSDictionary *)serialize { return @{ @"type": self.class.serializationCode, @"direction": @(_direction), - @"speed": @(_speed) + @"speed": @(_speed), + @"smooth": @(_smooth), }; } @@ -25,19 +26,28 @@ NJOutputMouseScroll *output = [[NJOutputMouseScroll alloc] init]; output.direction = [serialization[@"direction"] intValue]; output.speed = [serialization[@"speed"] floatValue]; + output.smooth = [serialization[@"smooth"] boolValue]; return output; } - (BOOL)isContinuous { - return !!self.speed; + return _smooth; +} + +- (int)wheel:(int)n { + int amount = abs(_direction) == n ? _direction / n : 0; + if (self.smooth) + amount *= _speed * self.magnitude; + return amount; } - (void)trigger { - if (!self.speed) { + if (!_smooth) { CGEventRef scroll = CGEventCreateScrollWheelEvent(NULL, kCGScrollEventUnitLine, - 1, - _direction); + 2, + [self wheel:1], + [self wheel:2]); CGEventPost(kCGHIDEventTap, scroll); CFRelease(scroll); } @@ -47,11 +57,11 @@ if (self.magnitude < 0.05f) return NO; // dead zone - int amount = (int)(_speed * self.magnitude * _direction); CGEventRef scroll = CGEventCreateScrollWheelEvent(NULL, kCGScrollEventUnitPixel, - 1, - amount); + 2, + [self wheel:1], + [self wheel:2]); CGEventPost(kCGHIDEventTap, scroll); CFRelease(scroll); diff --git a/Info.plist b/Info.plist index 4ad3f41..c382613 100644 --- a/Info.plist +++ b/Info.plist @@ -46,7 +46,7 @@ CFBundleSignature ???? CFBundleVersion - 69 + 77 LSApplicationCategoryType public.app-category.utilities NSHumanReadableCopyright diff --git a/Resources/English.lproj/MainMenu.xib b/Resources/English.lproj/MainMenu.xib index 772132e..d6f1307 100644 --- a/Resources/English.lproj/MainMenu.xib +++ b/Resources/English.lproj/MainMenu.xib @@ -718,10 +718,42 @@ aW5nLg NO + + + 268 + {{343, 31}, {70, 18}} + + + + _NS:9 + YES + + 67108864 + 268435456 + Smooth + + _NS:9 + + 1211912448 + 2 + + NSImage + NSSwitch + + + NSSwitch + + + + 200 + 25 + + NO + 265 - {{189, 33}, {224, 20}} + {{189, 33}, {150, 20}} @@ -735,26 +767,27 @@ aW5nLg - 86 - ↑ - Scroll up continuously + 36 + ← + -2 0 - 85 - ↓ - Scroll down continuously - 1 + 35 + → + 2 0 - ⤒ - Scroll up one step + 35 + ↑ + -1 0 - ⤓ - Scroll down one step + 35 + ↓ + 1 0 @@ -765,10 +798,10 @@ aW5nLg 265 - {{191, 24}, {176, 16}} + {{191, 24}, {146, 16}} - + _NS:9 YES @@ -777,11 +810,11 @@ aW5nLg _NS:9 - 30 - 0.0 + 29 + 1 15 0.0 - 11 + 15 1 YES NO @@ -858,10 +891,10 @@ aW5nLg _NS:9 20 - 0.0 + 1 10 0.0 - 11 + 20 1 YES NO @@ -1934,6 +1967,22 @@ aW5nLg 709 + + + scrollTypeChanged: + + + + 948 + + + + smoothCheck + + + + 949 + keyDelegate @@ -2485,6 +2534,7 @@ aW5nLg + @@ -2765,6 +2815,19 @@ aW5nLg + + 946 + + + + + + + + 947 + + + @@ -2851,10 +2914,10 @@ aW5nLg com.apple.InterfaceBuilder.CocoaPlugin com.apple.InterfaceBuilder.CocoaPlugin - + com.apple.InterfaceBuilder.CocoaPlugin com.apple.InterfaceBuilder.CocoaPlugin - + com.apple.InterfaceBuilder.CocoaPlugin com.apple.InterfaceBuilder.CocoaPlugin com.apple.InterfaceBuilder.CocoaPlugin @@ -2949,12 +3012,14 @@ aW5nLg com.apple.InterfaceBuilder.CocoaPlugin com.apple.InterfaceBuilder.CocoaPlugin com.apple.InterfaceBuilder.CocoaPlugin + com.apple.InterfaceBuilder.CocoaPlugin + com.apple.InterfaceBuilder.CocoaPlugin - 945 + 949 @@ -3178,6 +3243,7 @@ aW5nLg id id id + id id @@ -3201,6 +3267,10 @@ aW5nLg scrollSpeedChanged: id + + scrollTypeChanged: + id + sdirChanged: id @@ -3217,6 +3287,7 @@ aW5nLg NSMatrix NSSegmentedControl NSSlider + NSButton NSTextField @@ -3260,6 +3331,10 @@ aW5nLg scrollSpeedSlider NSSlider + + smoothCheck + NSButton + title NSTextField @@ -3284,6 +3359,7 @@ aW5nLg {16, 15} {8, 8} {9, 9} + {15, 15} -- 2.30.2