From 22ac4c61bdf79e6bc2b6738b9470cfb4f734cfab Mon Sep 17 00:00:00 2001 From: Jaakko Keränen Date: Wed, 16 Sep 2020 07:59:26 +0300 Subject: macOS: Added basic touch bar buttons --- src/macos.m | 129 +++++++++++++++++++++++++----------------------------------- 1 file changed, 54 insertions(+), 75 deletions(-) diff --git a/src/macos.m b/src/macos.m index bbbaaa19..6f833e12 100644 --- a/src/macos.m +++ b/src/macos.m @@ -28,38 +28,18 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ #import -#if 0 -static NSTouchBarItemIdentifier play_TouchId_ = @"fi.skyjake.BitwiseHarmony.play"; -static NSTouchBarItemIdentifier restart_TouchId_ = @"fi.skyjake.BitwiseHarmony.restart"; - -static NSTouchBarItemIdentifier seqMoveUp_TouchId_ = @"fi.skyjake.BitwiseHarmony.sequence.move.up"; -static NSTouchBarItemIdentifier seqMoveDown_TouchId_ = @"fi.skyjake.BitwiseHarmony.sequence.move.down"; - -static NSTouchBarItemIdentifier goto_TouchId_ = @"fi.skyjake.BitwiseHarmony.goto"; -static NSTouchBarItemIdentifier mute_TouchId_ = @"fi.skyjake.BitwiseHarmony.mute"; -static NSTouchBarItemIdentifier solo_TouchId_ = @"fi.skyjake.BitwiseHarmony.solo"; -static NSTouchBarItemIdentifier color_TouchId_ = @"fi.skyjake.BitwiseHarmony.color"; -static NSTouchBarItemIdentifier event_TouchId_ = @"fi.skyjake.BitwiseHarmony.event"; - -static NSTouchBarItemIdentifier eventList_TouchId_ = @"fi.skyjake.BitwiseHarmony.eventlist"; -static NSTouchBarItemIdentifier masterGainEvent_TouchId_ = @"fi.skyjake.BitwiseHarmony.event.mastergain"; -static NSTouchBarItemIdentifier resetEvent_TouchId_ = @"fi.skyjake.BitwiseHarmony.event.reset"; -static NSTouchBarItemIdentifier voiceEvent_TouchId_ = @"fi.skyjake.BitwiseHarmony.event.voice"; -static NSTouchBarItemIdentifier panEvent_TouchId_ = @"fi.skyjake.BitwiseHarmony.event.pan"; -static NSTouchBarItemIdentifier gainEvent_TouchId_ = @"fi.skyjake.BitwiseHarmony.event.gain"; -static NSTouchBarItemIdentifier fadeEvent_TouchId_ = @"fi.skyjake.BitwiseHarmony.event.fade"; -static NSTouchBarItemIdentifier pitchSpeedEvent_TouchId_ = @"fi.skyjake.BitwiseHarmony.event.pitchspeed"; -static NSTouchBarItemIdentifier pitchBendUpEvent_TouchId_ = @"fi.skyjake.BitwiseHarmony.event.pitchbendup"; -static NSTouchBarItemIdentifier pitchBendDownEvent_TouchId_ = @"fi.skyjake.BitwiseHarmony.event.pitchbenddown"; -static NSTouchBarItemIdentifier tremoloEvent_TouchId_ = @"fi.skyjake.BitwiseHarmony.event.tremolo"; -#endif +static NSTouchBarItemIdentifier goBack_TouchId_ = @"fi.skyjake.Lagrange.back"; +static NSTouchBarItemIdentifier goForward_TouchId_ = @"fi.skyjake.Lagrange.forward"; +static NSTouchBarItemIdentifier find_TouchId_ = @"fi.skyjake.Lagrange.find"; +static NSTouchBarItemIdentifier newTab_TouchId_ = @"fi.skyjake.Lagrange.tabs.new"; enum iTouchBarVariant { default_TouchBarVariant, }; -#if 0 -@interface CommandButton : NSButtonTouchBarItem { +/*----------------------------------------------------------------------------------------------*/ + +@interface CommandButton : NSCustomTouchBarItem { NSString *command; iWidget *widget; } @@ -70,6 +50,10 @@ enum iTouchBarVariant { title:(NSString *)title widget:(iWidget *)widget command:(NSString *)cmd; +- (id)initWithIdentifier:(NSTouchBarItemIdentifier)identifier + image:(NSImage *)image + widget:(iWidget *)widget + command:(NSString *)cmd; - (void)dealloc; @end @@ -79,9 +63,17 @@ enum iTouchBarVariant { title:(NSString *)title command:(NSString *)cmd { [super initWithIdentifier:identifier]; - self.title = title; - self.target = self; - self.action = @selector(buttonPressed); + self.view = [NSButton buttonWithTitle:title target:self action:@selector(buttonPressed)]; + command = cmd; + return self; +} + +- (id)initWithIdentifier:(NSTouchBarItemIdentifier)identifier + image:(NSImage *)image + widget:(iWidget *)widget + command:(NSString *)cmd { + [super initWithIdentifier:identifier]; + self.view = [NSButton buttonWithImage:image target:self action:@selector(buttonPressed)]; command = cmd; return self; } @@ -111,7 +103,8 @@ enum iTouchBarVariant { } @end -#endif + +/*----------------------------------------------------------------------------------------------*/ @interface MyDelegate : NSResponder { enum iTouchBarVariant touchBarVariant; @@ -120,7 +113,7 @@ enum iTouchBarVariant { NSMutableDictionary *menuCommands; } - (id)initWithSDLDelegate:(NSObject *)sdl; -//- (NSTouchBar *)makeTouchBar; +- (NSTouchBar *)makeTouchBar; - (BOOL)application:(NSApplication *)app openFile:(NSString *)filename; - (void)application:(NSApplication *)app openFiles:(NSArray *)filenames; - (void)application:(NSApplication *)app openURLs:(NSArray *)urls; @@ -153,8 +146,6 @@ static void appearanceChanged_MacOS_(NSString *name) { const iBool isDark = [name containsString:@"Dark"]; const iBool isHighContrast = [name containsString:@"HighContrast"]; postCommandf_App("~os.theme.changed dark:%d contrast:%d", isDark ? 1 : 0, isHighContrast ? 1 : 0); -// printf("Effective appearance changed: %s\n", [name cStringUsingEncoding:NSUTF8StringEncoding]); -// fflush(stdout); } - (void)setAppearance:(NSString *)name { @@ -204,42 +195,17 @@ static void appearanceChanged_MacOS_(NSString *name) { } } -#if 0 +#if 1 - (NSTouchBar *)makeTouchBar { NSTouchBar *bar = [[NSTouchBar alloc] init]; bar.delegate = self; switch (touchBarVariant) { case default_TouchBarVariant: - bar.defaultItemIdentifiers = @[ play_TouchId_, restart_TouchId_, + bar.defaultItemIdentifiers = @[ goBack_TouchId_, goForward_TouchId_, NSTouchBarItemIdentifierFixedSpaceSmall, - NSTouchBarItemIdentifierOtherItemsProxy ]; - break; - case sequence_TouchBarVariant: - bar.defaultItemIdentifiers = @[ play_TouchId_, restart_TouchId_, - NSTouchBarItemIdentifierFlexibleSpace, - seqMoveUp_TouchId_, seqMoveDown_TouchId_, - NSTouchBarItemIdentifierFlexibleSpace, - NSTouchBarItemIdentifierOtherItemsProxy]; - break; - case tracker_TouchBarVariant: - bar.defaultItemIdentifiers = @[ play_TouchId_, restart_TouchId_, - NSTouchBarItemIdentifierFlexibleSpace, - goto_TouchId_, - event_TouchId_, - NSTouchBarItemIdentifierFlexibleSpace, - solo_TouchId_, mute_TouchId_, color_TouchId_, - NSTouchBarItemIdentifierFlexibleSpace, - NSTouchBarItemIdentifierOtherItemsProxy ]; - break; - case wide_TouchBarVariant: - bar.defaultItemIdentifiers = @[ play_TouchId_, restart_TouchId_, - NSTouchBarItemIdentifierFlexibleSpace, - event_TouchId_, - NSTouchBarItemIdentifierFlexibleSpace, - solo_TouchId_, mute_TouchId_, color_TouchId_, - NSTouchBarItemIdentifierFlexibleSpace, - seqMoveUp_TouchId_, seqMoveDown_TouchId_, + find_TouchId_, NSTouchBarItemIdentifierFlexibleSpace, + newTab_TouchId_, NSTouchBarItemIdentifierOtherItemsProxy ]; break; } @@ -265,6 +231,30 @@ static void appearanceChanged_MacOS_(NSString *name) { - (nullable NSTouchBarItem *)touchBar:(NSTouchBar *)touchBar makeItemForIdentifier:(NSTouchBarItemIdentifier)identifier { iUnused(touchBar); + if ([identifier isEqualToString:goBack_TouchId_]) { + return [[CommandButton alloc] initWithIdentifier:identifier + image:[NSImage imageNamed:NSImageNameTouchBarGoBackTemplate] + widget:nil + command:@"navigate.back"]; + } + else if ([identifier isEqualToString:goForward_TouchId_]) { + return [[CommandButton alloc] initWithIdentifier:identifier + image:[NSImage imageNamed:NSImageNameTouchBarGoForwardTemplate] + widget:nil + command:@"navigate.forward"]; + } + else if ([identifier isEqualToString:find_TouchId_]) { + return [[CommandButton alloc] initWithIdentifier:identifier + image:[NSImage imageNamed:NSImageNameTouchBarSearchTemplate] + widget:nil + command:@"focus.set id:find.input"]; + } + else if ([identifier isEqualToString:newTab_TouchId_]) { + return [[CommandButton alloc] initWithIdentifier:identifier + image:[NSImage imageNamed:NSImageNameTouchBarAddTemplate] + widget:nil + command:@"tabs.new"]; + } #if 0 if ([identifier isEqualToString:play_TouchId_]) { return [NSButtonTouchBarItem @@ -524,18 +514,7 @@ void handleCommand_MacOS(const char *cmd) { if (equal_Command(cmd, "tabs.changed")) { MyDelegate *myDel = (MyDelegate *) [[NSApplication sharedApplication] delegate]; const char *tabId = suffixPtr_Command(cmd, "id"); - if (equal_CStr(tabId, "tracker")) { - [myDel setTouchBarVariant:tracker_TouchBarVariant]; - } - else if (equal_CStr(tabId, "sequence")) { - [myDel setTouchBarVariant:sequence_TouchBarVariant]; - } - else if (equal_CStr(tabId, "trackertab")) { - [myDel setTouchBarVariant:wide_TouchBarVariant]; - } - else { - [myDel setTouchBarVariant:default_TouchBarVariant]; - } + [myDel setTouchBarVariant:default_TouchBarVariant]; } #endif } -- cgit v1.2.3