From 703b5738c8df36decee7891be3b9158b9283d095 Mon Sep 17 00:00:00 2001 From: Samuel Attard Date: Sun, 27 Nov 2016 16:57:01 +1100 Subject: [PATCH] Initial TouchBar Magic * Make the AtomNSWindow also a NSTouchbarDelegate * Implement basic makeTouchBar and makeItemForIdentifier methods * Initial sending of touch / update events through IPC to BrowserWindowObjects TODO: * JS API * JS Object Converters * Generalize methods so that popovers can work --- atom/browser/api/atom_api_window.cc | 9 ++++ atom/browser/api/atom_api_window.h | 2 + atom/browser/native_window.cc | 10 ++++ atom/browser/native_window.h | 4 ++ atom/browser/native_window_mac.h | 1 + atom/browser/native_window_mac.mm | 78 +++++++++++++++++++++++++++ atom/browser/native_window_observer.h | 1 + 7 files changed, 105 insertions(+) diff --git a/atom/browser/api/atom_api_window.cc b/atom/browser/api/atom_api_window.cc index 2f97a88faa..1107a628a9 100644 --- a/atom/browser/api/atom_api_window.cc +++ b/atom/browser/api/atom_api_window.cc @@ -282,6 +282,10 @@ void Window::OnExecuteWindowsCommand(const std::string& command_name) { Emit("app-command", command_name); } +void Window::OnTouchBarItemResult(const std::string& item_type, const std::string& item_id) { + Emit("_touch-bar-interaction", item_type, item_id); +} + #if defined(OS_WIN) void Window::OnWindowMessage(UINT message, WPARAM w_param, LPARAM l_param) { if (IsWindowMessageHooked(message)) { @@ -840,6 +844,10 @@ void Window::SetVibrancy(mate::Arguments* args) { window_->SetVibrancy(type); } +void Window::InitTouchBar() { + window_->InitTouchBar(); +} + int32_t Window::ID() const { return weak_map_id(); } @@ -960,6 +968,7 @@ void Window::BuildPrototype(v8::Isolate* isolate, .SetMethod("setAutoHideCursor", &Window::SetAutoHideCursor) #endif .SetMethod("setVibrancy", &Window::SetVibrancy) + .SetMethod("initTouchBar", &Window::InitTouchBar) #if defined(OS_WIN) .SetMethod("hookWindowMessage", &Window::HookWindowMessage) .SetMethod("isWindowMessageHooked", &Window::IsWindowMessageHooked) diff --git a/atom/browser/api/atom_api_window.h b/atom/browser/api/atom_api_window.h index 80af78a5b0..3325c87fa9 100644 --- a/atom/browser/api/atom_api_window.h +++ b/atom/browser/api/atom_api_window.h @@ -85,6 +85,7 @@ class Window : public mate::TrackableObject, void OnRendererUnresponsive() override; void OnRendererResponsive() override; void OnExecuteWindowsCommand(const std::string& command_name) override; + void OnTouchBarItemResult(const std::string& item_type, const std::string& item_id) override; #if defined(OS_WIN) void OnWindowMessage(UINT message, WPARAM w_param, LPARAM l_param) override; @@ -203,6 +204,7 @@ class Window : public mate::TrackableObject, void SetAutoHideCursor(bool auto_hide); void SetVibrancy(mate::Arguments* args); + void InitTouchBar(); v8::Local WebContents(v8::Isolate* isolate); diff --git a/atom/browser/native_window.cc b/atom/browser/native_window.cc index eae68c37f5..2219a4d595 100644 --- a/atom/browser/native_window.cc +++ b/atom/browser/native_window.cc @@ -340,6 +340,9 @@ void NativeWindow::SetAutoHideCursor(bool auto_hide) { void NativeWindow::SetVibrancy(const std::string& filename) { } +void NativeWindow::InitTouchBar() { +} + void NativeWindow::FocusOnWebView() { web_contents()->GetRenderViewHost()->GetWidget()->Focus(); } @@ -565,6 +568,13 @@ void NativeWindow::NotifyWindowExecuteWindowsCommand( observer.OnExecuteWindowsCommand(command); } +void NativeWindow::NotifyTouchBarItemInteraction( + const std::string& type, + const std::string& item_id) { + FOR_EACH_OBSERVER(NativeWindowObserver, observers_, + OnTouchBarItemResult(type, item_id)); + } + #if defined(OS_WIN) void NativeWindow::NotifyWindowMessage( UINT message, WPARAM w_param, LPARAM l_param) { diff --git a/atom/browser/native_window.h b/atom/browser/native_window.h index aa5e7e0c71..3338396146 100644 --- a/atom/browser/native_window.h +++ b/atom/browser/native_window.h @@ -169,6 +169,9 @@ class NativeWindow : public base::SupportsUserData, // Vibrancy API virtual void SetVibrancy(const std::string& type); + // Touchbar API + virtual void InitTouchBar(); + // Webview APIs. virtual void FocusOnWebView(); virtual void BlurWebView(); @@ -228,6 +231,7 @@ class NativeWindow : public base::SupportsUserData, void NotifyWindowEnterHtmlFullScreen(); void NotifyWindowLeaveHtmlFullScreen(); void NotifyWindowExecuteWindowsCommand(const std::string& command); + void NotifyTouchBarItemInteraction(const std::string& item_type, const std::string& item_id); #if defined(OS_WIN) void NotifyWindowMessage(UINT message, WPARAM w_param, LPARAM l_param); diff --git a/atom/browser/native_window_mac.h b/atom/browser/native_window_mac.h index 2beb55c029..fec5eacb3a 100644 --- a/atom/browser/native_window_mac.h +++ b/atom/browser/native_window_mac.h @@ -100,6 +100,7 @@ class NativeWindowMac : public NativeWindow, void SetAutoHideCursor(bool auto_hide) override; void SetVibrancy(const std::string& type) override; + void InitTouchBar() override; // content::RenderWidgetHost::InputEventObserver: void OnInputEvent(const blink::WebInputEvent& event) override; diff --git a/atom/browser/native_window_mac.mm b/atom/browser/native_window_mac.mm index acbef9906a..029cf6002d 100644 --- a/atom/browser/native_window_mac.mm +++ b/atom/browser/native_window_mac.mm @@ -351,9 +351,14 @@ bool ScopedDisableResize::disable_resize_ = false; - (void)setShell:(atom::NativeWindowMac*)shell; - (void)setEnableLargerThanScreen:(bool)enable; - (void)enableWindowButtonsOffset; +- (void)reloadTouchBar; +@end + +@interface AtomNSWindow () @end @implementation AtomNSWindow + NSMutableArray* bar_items_ = [[NSMutableArray alloc] init]; - (void)setShell:(atom::NativeWindowMac*)shell { shell_ = shell; @@ -363,6 +368,75 @@ bool ScopedDisableResize::disable_resize_ = false; enable_larger_than_screen_ = enable; } +- (void)reloadTouchBar { + bar_items_ = [[NSMutableArray alloc] init]; + [bar_items_ addObject:@"com.electron.tb.button.1"]; + [bar_items_ addObject:@"com.electron.tb.button.2"]; + [bar_items_ addObject:NSTouchBarItemIdentifierOtherItemsProxy]; + NSLog(@"Reloading Touch Bar --> '%@'", bar_items_[1]); + self.touchBar = nil; +} + +- (NSTouchBar *)makeTouchBar { + NSLog(@"Making Touch Bar"); + NSTouchBar* bar = [[NSTouchBar alloc] init]; + bar.delegate = self; + + // Set the default ordering of items. + + // NSLog(@"%@", bar_items_[1]); + bar.defaultItemIdentifiers = [bar_items_ copy]; + + return bar; +} + +- (void)buttonAction:(id)sender { + NSString* item_id = [NSString stringWithFormat:@"com.electron.tb.button.%d", (int)((NSButton *)sender).tag]; + NSLog(@"Button with ID: '%@' was pressed", item_id); + shell_->NotifyTouchBarItemInteraction("button", std::string([item_id UTF8String])); +} + +- (void)colorPickerAction:(id)sender { + NSString* item_id = ((NSColorPickerTouchBarItem *)sender).identifier; + NSLog(@"ColorPicker with ID: '%@' was updated with color '%@'", item_id, ((NSColorPickerTouchBarItem *)sender).color); + shell_->NotifyTouchBarItemInteraction("color_picker", std::string([item_id UTF8String])); +} + +static NSTouchBarItemIdentifier ButtonIdentifier = @"com.electron.tb.button."; +static NSTouchBarItemIdentifier ColorPickerIdentifier = @"com.electron.tb.colorpicker."; +// static NSTouchBarItemIdentifier ListIdentifier = @"com.electron.tb.list."; +static NSTouchBarItemIdentifier LabelIdentifier = @"com.electron.tb.label."; +// static NSTouchBarItemIdentifier SliderIdentifier = @"com.electron.tb.slider."; + +- (nullable NSTouchBarItem *)touchBar:(NSTouchBar *)touchBar makeItemForIdentifier:(NSTouchBarItemIdentifier)identifier { + if ([identifier hasPrefix:ButtonIdentifier]) { + NSString *idCopy = [identifier copy]; + idCopy = [identifier substringFromIndex:[ButtonIdentifier length]]; + NSButton *theButton = [NSButton buttonWithTitle:@"Electron Button" target:self action:@selector(buttonAction:)]; + theButton.tag = [idCopy floatValue]; + + NSCustomTouchBarItem *customItem = [[NSCustomTouchBarItem alloc] initWithIdentifier:identifier]; + customItem.view = theButton; + + return customItem; + } else if ([identifier hasPrefix:LabelIdentifier]) { + NSTextField *theLabel = [NSTextField labelWithString:@"Hello From Electron"]; + + NSCustomTouchBarItem *customItem = [[NSCustomTouchBarItem alloc] initWithIdentifier:identifier]; + customItem.view = theLabel; + + return customItem; + } else if ([identifier hasPrefix:ColorPickerIdentifier]) { + NSColorPickerTouchBarItem *colorPickerItem = [[NSColorPickerTouchBarItem alloc] initWithIdentifier:identifier]; + colorPickerItem.target = self; + colorPickerItem.action = @selector(colorPickerAction:); + return colorPickerItem; + } + + return nil; +} + + // NSWindow overrides. - (void)swipeWithEvent:(NSEvent *)event { @@ -1346,6 +1420,10 @@ void NativeWindowMac::SetVibrancy(const std::string& type) { [effect_view setMaterial:vibrancyType]; } +void NativeWindowMac::InitTouchBar() { + [window_ reloadTouchBar]; +} + void NativeWindowMac::OnInputEvent(const blink::WebInputEvent& event) { switch (event.type) { case blink::WebInputEvent::GestureScrollBegin: diff --git a/atom/browser/native_window_observer.h b/atom/browser/native_window_observer.h index 42d6b0287f..e3d9deaed2 100644 --- a/atom/browser/native_window_observer.h +++ b/atom/browser/native_window_observer.h @@ -70,6 +70,7 @@ class NativeWindowObserver { virtual void OnWindowLeaveFullScreen() {} virtual void OnWindowEnterHtmlFullScreen() {} virtual void OnWindowLeaveHtmlFullScreen() {} + virtual void OnTouchBarItemResult(const std::string& item_type, const std::string& item_id) {} // Called when window message received #if defined(OS_WIN)