From 49160819d28a2e353d66303b9e7b84c37dda730a Mon Sep 17 00:00:00 2001 From: Cheng Zhao Date: Thu, 10 Jul 2014 12:07:01 +0800 Subject: [PATCH] linux: Implement context menu with views. --- atom.gyp | 4 +- atom/browser/api/atom_api_menu_gtk.cc | 48 ------------------- atom/browser/api/atom_api_menu_views.cc | 40 ++++++++++++++++ ...m_api_menu_gtk.h => atom_api_menu_views.h} | 20 ++++---- atom/browser/native_window_views.h | 1 + 5 files changed, 54 insertions(+), 59 deletions(-) delete mode 100644 atom/browser/api/atom_api_menu_gtk.cc create mode 100644 atom/browser/api/atom_api_menu_views.cc rename atom/browser/api/{atom_api_menu_gtk.h => atom_api_menu_views.h} (51%) diff --git a/atom.gyp b/atom.gyp index 85dacd9c..a4f38b6f 100644 --- a/atom.gyp +++ b/atom.gyp @@ -54,8 +54,8 @@ 'atom/browser/api/atom_api_dialog.cc', '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/api/atom_api_menu_views.cc', + 'atom/browser/api/atom_api_menu_views.h', 'atom/browser/api/atom_api_menu_mac.h', 'atom/browser/api/atom_api_menu_mac.mm', 'atom/browser/api/atom_api_menu_win.cc', diff --git a/atom/browser/api/atom_api_menu_gtk.cc b/atom/browser/api/atom_api_menu_gtk.cc deleted file mode 100644 index a21ce53d..00000000 --- a/atom/browser/api/atom_api_menu_gtk.cc +++ /dev/null @@ -1,48 +0,0 @@ -// Copyright (c) 2014 GitHub, Inc. All rights reserved. -// Use of this source code is governed by the MIT license that can be -// found in the LICENSE file. - -#include "atom/browser/api/atom_api_menu_gtk.h" - -#include "content/public/browser/render_widget_host_view.h" -#include "ui/gfx/point.h" -#include "ui/gfx/screen.h" - -#include "atom/common/node_includes.h" - -namespace atom { - -namespace api { - -MenuGtk::MenuGtk() { -} - -void MenuGtk::Popup(Window* window) { - /* - uint32_t triggering_event_time; - gfx::Point point; - - NativeWindow* native_window = window->window(); - GdkEventButton* event = native_window->GetWebContents()-> - GetRenderWidgetHostView()->GetLastMouseDown(); - if (event) { - triggering_event_time = event->time; - point = gfx::Point(event->x_root, event->y_root); - } else { - triggering_event_time = GDK_CURRENT_TIME; - point = gfx::Screen::GetNativeScreen()->GetCursorScreenPoint(); - } - - menu_gtk_.reset(new ::MenuGtk(this, model_.get())); - menu_gtk_->PopupAsContext(point, triggering_event_time); - */ -} - -// static -mate::Wrappable* Menu::Create() { - return new MenuGtk(); -} - -} // namespace api - -} // namespace atom diff --git a/atom/browser/api/atom_api_menu_views.cc b/atom/browser/api/atom_api_menu_views.cc new file mode 100644 index 00000000..723b6259 --- /dev/null +++ b/atom/browser/api/atom_api_menu_views.cc @@ -0,0 +1,40 @@ +// Copyright (c) 2014 GitHub, Inc. All rights reserved. +// Use of this source code is governed by the MIT license that can be +// found in the LICENSE file. + +#include "atom/browser/api/atom_api_menu_views.h" + +#include "atom/browser/native_window_views.h" +#include "ui/gfx/screen.h" +#include "ui/views/controls/menu/menu_runner.h" + +namespace atom { + +namespace api { + +MenuViews::MenuViews() { +} + +void MenuViews::Popup(Window* window) { + gfx::Point cursor = gfx::Screen::GetNativeScreen()->GetCursorScreenPoint(); + + menu_runner_.reset(new views::MenuRunner(model())); + views::MenuRunner::RunResult result = menu_runner_->RunMenuAt( + static_cast(window->window())->widget(), + NULL, + gfx::Rect(cursor, gfx::Size()), + views::MenuItemView::TOPLEFT, + ui::MENU_SOURCE_MOUSE, + views::MenuRunner::HAS_MNEMONICS | views::MenuRunner::CONTEXT_MENU); + if (result == views::MenuRunner::MENU_DELETED) + LOG(ERROR) << "Menu deleted when running"; +} + +// static +mate::Wrappable* Menu::Create() { + return new MenuViews(); +} + +} // namespace api + +} // namespace atom diff --git a/atom/browser/api/atom_api_menu_gtk.h b/atom/browser/api/atom_api_menu_views.h similarity index 51% rename from atom/browser/api/atom_api_menu_gtk.h rename to atom/browser/api/atom_api_menu_views.h index 3c39fb73..13e5a905 100644 --- a/atom/browser/api/atom_api_menu_gtk.h +++ b/atom/browser/api/atom_api_menu_views.h @@ -2,32 +2,34 @@ // Use of this source code is governed by the MIT license that can be // found in the LICENSE file. -#ifndef ATOM_BROWSER_API_ATOM_API_MENU_GTK_H_ -#define ATOM_BROWSER_API_ATOM_API_MENU_GTK_H_ +#ifndef ATOM_BROWSER_API_ATOM_API_MENU_VIEWS_H_ +#define ATOM_BROWSER_API_ATOM_API_MENU_VIEWS_H_ #include "atom/browser/api/atom_api_menu.h" -#include "chrome/browser/ui/gtk/menu_gtk.h" + +namespace views { +class MenuRunner; +} namespace atom { namespace api { -class MenuGtk : public Menu, - public ::MenuGtk::Delegate { +class MenuViews : public Menu { public: - MenuGtk(); + MenuViews(); protected: virtual void Popup(Window* window) OVERRIDE; private: - scoped_ptr<::MenuGtk> menu_gtk_; + scoped_ptr menu_runner_; - DISALLOW_COPY_AND_ASSIGN(MenuGtk); + DISALLOW_COPY_AND_ASSIGN(MenuViews); }; } // namespace api } // namespace atom -#endif // ATOM_BROWSER_API_ATOM_API_MENU_GTK_H_ +#endif // ATOM_BROWSER_API_ATOM_API_MENU_VIEWS_H_ diff --git a/atom/browser/native_window_views.h b/atom/browser/native_window_views.h index 6fdb691d..63f69d61 100644 --- a/atom/browser/native_window_views.h +++ b/atom/browser/native_window_views.h @@ -70,6 +70,7 @@ class NativeWindowViews : public NativeWindow, virtual gfx::NativeWindow GetNativeWindow() OVERRIDE; SkRegion* draggable_region() const { return draggable_region_.get(); } + views::Widget* widget() const { return window_.get(); } private: // NativeWindow: -- 2.43.0