]> khamidou — GitHub mirrors - electron.git/commitdiff
views: Make acceleratros work.
authorCheng Zhao <zcbenz@gmail.com>
Fri, 4 Jul 2014 08:54:10 +0000 (16:54 +0800)
committerCheng Zhao <zcbenz@gmail.com>
Fri, 4 Jul 2014 08:54:10 +0000 (16:54 +0800)
atom/browser/api/atom_api_menu.cc
atom/browser/api/atom_api_menu.h
atom/browser/api/atom_api_menu_gtk.cc
atom/browser/api/atom_api_menu_gtk.h
atom/browser/native_window.cc
atom/browser/native_window.h
atom/browser/native_window_views.cc
atom/browser/native_window_views.h
vendor/brightray

index 946ffd6a080464f7067ae5ebef266439e7a862a7..2c8c0868793ccc09dd007ed40bd28ba81c22e133 100644 (file)
@@ -4,7 +4,7 @@
 
 #include "atom/browser/api/atom_api_menu.h"
 
-#include "atom/browser/api/atom_api_window.h"
+#include "atom/browser/native_window.h"
 #include "atom/browser/ui/accelerator_util.h"
 #include "atom/common/native_mate_converters/string16_converter.h"
 #include "native_mate/constructor.h"
@@ -157,6 +157,10 @@ void Menu::MenuWillShow(ui::SimpleMenuModel* source) {
                "menuWillShow", -1);
 }
 
+void Menu::AttachToWindow(Window* window) {
+  window->window()->SetMenu(model_.get());
+}
+
 void Menu::InsertItemAt(
     int index, int command_id, const base::string16& label) {
   model_->InsertItemAt(index, command_id, label);
@@ -246,9 +250,7 @@ void Menu::BuildPrototype(v8::Isolate* isolate,
       .SetMethod("isItemCheckedAt", &Menu::IsItemCheckedAt)
       .SetMethod("isEnabledAt", &Menu::IsEnabledAt)
       .SetMethod("isVisibleAt", &Menu::IsVisibleAt)
-#if defined(OS_WIN) || defined(OS_LINUX)
       .SetMethod("_attachToWindow", &Menu::AttachToWindow)
-#endif
 #if defined(OS_WIN)
       .SetMethod("_updateStates", &Menu::UpdateStates)
 #endif
index 122c8bc15568f8446e8fa60a33572687f54a344c..62060dcff69fb22517a85fa96e41313bb1f72cfa 100644 (file)
@@ -53,6 +53,7 @@ class Menu : public mate::Wrappable,
   virtual void ExecuteCommand(int command_id, int event_flags) OVERRIDE;
   virtual void MenuWillShow(ui::SimpleMenuModel* source) OVERRIDE;
 
+  virtual void AttachToWindow(Window* window);
   virtual void Popup(Window* window) = 0;
 
   scoped_ptr<ui::SimpleMenuModel> model_;
@@ -87,10 +88,6 @@ class Menu : public mate::Wrappable,
   virtual void UpdateStates() = 0;
 #endif
 
-#if defined(OS_WIN) || defined(OS_LINUX)
-  virtual void AttachToWindow(Window* window) = 0;
-#endif
-
   DISALLOW_COPY_AND_ASSIGN(Menu);
 };
 
index 5daa60132a32bf33767f9bd7346b08aff2cf5f62..a21ce53d614a61b25275cac7e8dfcee5bc48b237 100644 (file)
@@ -38,10 +38,6 @@ void MenuGtk::Popup(Window* window) {
   */
 }
 
-void MenuGtk::AttachToWindow(Window* window) {
-  // static_cast<NativeWindowGtk*>(window->window())->SetMenu(model_.get());
-}
-
 // static
 mate::Wrappable* Menu::Create() {
   return new MenuGtk();
index eb6c94c9932bf620fcce65664b001b0288aa306d..3c39fb730b7bd7939d6f0a7af834eb6d8fe8fe5c 100644 (file)
@@ -19,7 +19,6 @@ class MenuGtk : public Menu,
 
  protected:
   virtual void Popup(Window* window) OVERRIDE;
-  virtual void AttachToWindow(Window* window) OVERRIDE;
 
  private:
   scoped_ptr<::MenuGtk> menu_gtk_;
index 74bc52b42210e3093327d8bf4a099913d7fe719f..d43e29c5677a3de9d36acd9903c1a8db77542a42 100644 (file)
@@ -197,6 +197,9 @@ void NativeWindow::SetRepresentedFilename(const std::string& filename) {
 void NativeWindow::SetDocumentEdited(bool edited) {
 }
 
+void NativeWindow::SetMenu(ui::MenuModel* menu) {
+}
+
 bool NativeWindow::HasModalDialog() {
   return has_dialog_attached_;
 }
index 6929b8d2f9dc5422eb1a20d6cdf4766a6af7278e..3bf8c9b57ad725521bcc4e41ffb1ee7d65355692 100644 (file)
@@ -16,6 +16,7 @@
 #include "base/memory/weak_ptr.h"
 #include "base/observer_list.h"
 #include "atom/browser/native_window_observer.h"
+#include "atom/browser/ui/accelerator_util.h"
 #include "content/public/browser/notification_registrar.h"
 #include "content/public/browser/notification_observer.h"
 #include "vendor/brightray/browser/default_web_contents_delegate.h"
@@ -44,6 +45,10 @@ namespace mate {
 class Dictionary;
 }
 
+namespace ui {
+class MenuModel;
+}
+
 namespace atom {
 
 class AtomJavaScriptDialogManager;
@@ -129,6 +134,7 @@ class NativeWindow : public brightray::DefaultWebContentsDelegate,
   virtual bool IsKiosk() = 0;
   virtual void SetRepresentedFilename(const std::string& filename);
   virtual void SetDocumentEdited(bool edited);
+  virtual void SetMenu(ui::MenuModel* menu);
   virtual bool HasModalDialog();
   virtual gfx::NativeWindow GetNativeWindow() = 0;
 
index ced2513fac746de5e3555b360114552e28107295..2be87a9b7ee4a56ba9a380cd858798bfeaa60b4c 100644 (file)
@@ -9,6 +9,8 @@
 
 #include "atom/common/options_switches.h"
 #include "base/strings/utf_string_conversions.h"
+#include "browser/inspectable_web_contents_view.h"
+#include "content/public/browser/native_web_keyboard_event.h"
 #include "content/public/browser/web_contents_view.h"
 #include "native_mate/dictionary.h"
 #include "ui/gfx/image/image.h"
@@ -47,7 +49,7 @@ NativeWindowViews::NativeWindowViews(content::WebContents* web_contents,
                                      const mate::Dictionary& options)
     : NativeWindow(web_contents, options),
       window_(new views::Widget),
-      web_view_(new views::WebView(web_contents->GetBrowserContext())),
+      web_view_(inspectable_web_contents()->GetView()->GetView()),
       resizable_(true) {
   options.Get(switches::kResizable, &resizable_);
   options.Get(switches::kTitle, &title_);
@@ -63,7 +65,6 @@ NativeWindowViews::NativeWindowViews(content::WebContents* web_contents,
   // Add web view.
   SetLayoutManager(new views::FillLayout);
   set_background(views::Background::CreateStandardPanelBackground());
-  web_view_->SetWebContents(web_contents);
   AddChildView(web_view_);
 
   int width = 800, height = 600;
@@ -239,6 +240,11 @@ bool NativeWindowViews::IsKiosk() {
   return IsFullscreen();
 }
 
+void NativeWindowViews::SetMenu(ui::MenuModel* menu_model) {
+  // FIXME
+  RegisterAccelerators(menu_model);
+}
+
 gfx::NativeWindow NativeWindowViews::GetNativeWindow() {
   return window_->GetNativeWindow();
 }
@@ -253,7 +259,7 @@ void NativeWindowViews::DeleteDelegate() {
 }
 
 views::View* NativeWindowViews::GetInitiallyFocusedView() {
-  return web_view_;
+  return inspectable_web_contents()->GetView()->GetWebView();
 }
 
 bool NativeWindowViews::CanResize() const {
@@ -306,6 +312,42 @@ views::NonClientFrameView* NativeWindowViews::CreateNonClientFrameView(
   return frame_view;
 }
 
+void NativeWindowViews::HandleKeyboardEvent(
+    content::WebContents*,
+    const content::NativeWebKeyboardEvent& event) {
+  if (event.type == blink::WebInputEvent::RawKeyDown) {
+    ui::Accelerator accelerator(
+        static_cast<ui::KeyboardCode>(event.windowsKeyCode),
+        content::GetModifiersFromNativeWebKeyboardEvent(event));
+
+    if (GetFocusManager()->ProcessAccelerator(accelerator)) {
+      return;
+    }
+  }
+}
+
+bool NativeWindowViews::AcceleratorPressed(const ui::Accelerator& accelerator) {
+  return accelerator_util::TriggerAcceleratorTableCommand(
+      &accelerator_table_, accelerator);
+}
+
+void NativeWindowViews::RegisterAccelerators(ui::MenuModel* menu_model) {
+  // Clear previous accelerators.
+  views::FocusManager* focus_manager = GetFocusManager();
+  accelerator_table_.clear();
+  focus_manager->UnregisterAccelerators(this);
+
+  // Register accelerators with focus manager.
+  accelerator_util::GenerateAcceleratorTable(&accelerator_table_, menu_model);
+  accelerator_util::AcceleratorTable::const_iterator iter;
+  for (iter = accelerator_table_.begin();
+       iter != accelerator_table_.end();
+       ++iter) {
+    focus_manager->RegisterAccelerator(
+        iter->first, ui::AcceleratorManager::kNormalPriority, this);
+  }
+}
+
 // static
 NativeWindow* NativeWindow::Create(content::WebContents* web_contents,
                                    const mate::Dictionary& options) {
index 2b03972885034e21ba864608cf8accc9dcb0c6ab..a8433593a0efdde8b5ea0cb7e730c447026a6bf4 100644 (file)
 #include <string>
 #include <vector>
 
+#include "atom/browser/ui/accelerator_util.h"
 #include "ui/views/widget/widget_delegate.h"
 
 namespace views {
-class WebView;
 class Widget;
 }
 
@@ -62,6 +62,7 @@ class NativeWindowViews : public NativeWindow,
   virtual void SetSkipTaskbar(bool skip) OVERRIDE;
   virtual void SetKiosk(bool kiosk) OVERRIDE;
   virtual bool IsKiosk() OVERRIDE;
+  virtual void SetMenu(ui::MenuModel* menu_model) OVERRIDE;
   virtual gfx::NativeWindow GetNativeWindow() OVERRIDE;
 
  private:
@@ -85,8 +86,22 @@ class NativeWindowViews : public NativeWindow,
   virtual views::NonClientFrameView* CreateNonClientFrameView(
       views::Widget* widget) OVERRIDE;
 
+  // content::WebContentsDelegate:
+  virtual void HandleKeyboardEvent(
+      content::WebContents*,
+      const content::NativeWebKeyboardEvent& event) OVERRIDE;
+
+  // views::View:
+  virtual bool AcceleratorPressed(const ui::Accelerator& accelerator) OVERRIDE;
+
+  // Register accelerators supported by the menu model.
+  void RegisterAccelerators(ui::MenuModel* menu_model);
+
   scoped_ptr<views::Widget> window_;
-  views::WebView* web_view_;  // managed by window_.
+  views::View* web_view_;  // Managed by inspectable_web_contents_.
+
+  // Map from accelerator to menu item's command id.
+  accelerator_util::AcceleratorTable accelerator_table_;
 
   bool resizable_;
   std::string title_;
index b2e79c9803b61a9b90aa45af32686e8ef9d4c571..c4426492f5d0321e4c824a0443e6a261fc985ac7 160000 (submodule)
@@ -1 +1 @@
-Subproject commit b2e79c9803b61a9b90aa45af32686e8ef9d4c571
+Subproject commit c4426492f5d0321e4c824a0443e6a261fc985ac7