Browse Source

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
Samuel Attard 8 years ago
parent
commit
703b5738c8

+ 9 - 0
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)

+ 2 - 0
atom/browser/api/atom_api_window.h

@@ -85,6 +85,7 @@ class Window : public mate::TrackableObject<Window>,
   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<Window>,
   void SetAutoHideCursor(bool auto_hide);
 
   void SetVibrancy(mate::Arguments* args);
+  void InitTouchBar();
 
   v8::Local<v8::Value> WebContents(v8::Isolate* isolate);
 

+ 10 - 0
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) {

+ 4 - 0
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);

+ 1 - 0
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;

+ 78 - 0
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 () <NSTouchBarDelegate>
 @end
 
 @implementation AtomNSWindow
+  NSMutableArray<NSTouchBarItemIdentifier>* 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:

+ 1 - 0
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)