Index: ui/views_core/menu_runner_mac.mm |
diff --git a/ui/views_core/menu_runner_mac.mm b/ui/views_core/menu_runner_mac.mm |
new file mode 100644 |
index 0000000000000000000000000000000000000000..6a9cdb023579b7424435dac7b11a82ee25396c56 |
--- /dev/null |
+++ b/ui/views_core/menu_runner_mac.mm |
@@ -0,0 +1,142 @@ |
+// Copyright 2013 The Chromium Authors. All rights reserved. |
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+ |
+#include "ui/views/controls/menu/menu_runner.h" |
+ |
+#include <Cocoa/Cocoa.h> |
+ |
+#include "base/mac/scoped_nsobject.h" |
+#include "base/time/time.h" |
+#include "ui/base/cocoa/menu_controller.h" |
+#include "ui/base/models/menu_model.h" |
+#include "ui/events/event_utils.h" |
+#include "ui/gfx/geometry/rect.h" |
+#include "ui/gfx/mac/point_utils.h" |
+ |
+namespace views { |
+ |
+class MenuModelAdapter {}; |
+class MenuRunnerHandler {}; |
+ |
+namespace internal { |
+ |
+// Manages the menu. To destroy a MenuRunnerImpl invoke Release(). Release() |
+// deletes immediately if the menu isn't showing. If the menu is showing |
+// Release() cancels the menu and when the nested RunMenuAt() call returns |
+// deletes itself and the menu. |
+class MenuRunnerImpl { |
+ public: |
+ explicit MenuRunnerImpl(ui::MenuModel* menu_model); |
+ |
+ NSMenu* menu() { return [menu_controller_ menu]; } |
+ |
+ bool running() const { return [menu_controller_ isMenuOpen]; } |
+ |
+ void Release() { menu_controller_.reset(); } |
+ |
+ // Runs the menu. |
+ MenuRunner::RunResult RunMenuAt(Widget* parent, |
+ MenuButton* button, |
+ const gfx::Rect& bounds, |
+ ui::MenuAnchorPosition anchor, |
+ int32 types) WARN_UNUSED_RESULT; |
+ |
+ void Cancel() { [menu_controller_ cancel]; } |
+ |
+ // Returns the time from the event which closed the menu - or 0. |
+ base::TimeDelta closing_event_time() const { |
+ return closing_event_time_; |
+ } |
+ |
+ private: |
+ // The Cocoa menu controller that this instance is bridging. |
+ base::scoped_nsobject<MenuController> menu_controller_; |
+ |
+ // The timestamp of the event which closed the menu - or 0. |
+ base::TimeDelta closing_event_time_; |
+ |
+ DISALLOW_COPY_AND_ASSIGN(MenuRunnerImpl); |
+}; |
+ |
+MenuRunnerImpl::MenuRunnerImpl(ui::MenuModel* menu) |
+ : closing_event_time_(base::TimeDelta()) { |
+ menu_controller_.reset([[MenuController alloc] initWithModel:menu |
+ useWithPopUpButtonCell:NO]); |
+} |
+ |
+MenuRunner::RunResult MenuRunnerImpl::RunMenuAt( |
+ Widget* parent, |
+ MenuButton* button, |
+ const gfx::Rect& bounds, |
+ ui::MenuAnchorPosition anchor, |
+ int32 types) { |
+ closing_event_time_ = base::TimeDelta(); |
+ // TODO(tapted): Ensure this is OK with being invoked while already running(). |
+ |
+ if ((types & MenuRunner::FOR_DROP) != 0) { |
+ NOTIMPLEMENTED(); |
+ return MenuRunner::NORMAL_EXIT; |
+ } |
+ |
+ [menu() popUpMenuPositioningItem:nil |
+ atLocation:ScreenPointToNSPoint(bounds.origin()) |
+ inView:nil]; |
+ |
+ // Get the time of the event which closed this menu. |
+ closing_event_time_ = ui::EventTimeForNow(); |
+ return MenuRunner::NORMAL_EXIT; |
+} |
+ |
+} // namespace internal |
+ |
+MenuRunner::MenuRunner(ui::MenuModel* menu_model) |
+ : holder_(new internal::MenuRunnerImpl(menu_model)) { |
+} |
+ |
+MenuRunner::MenuRunner(MenuItemView* menu) { |
+ NOTIMPLEMENTED(); |
+} |
+ |
+MenuRunner::~MenuRunner() { |
+ holder_->Release(); |
+} |
+ |
+MenuItemView* MenuRunner::GetMenu() { |
+ NOTIMPLEMENTED(); |
+ return NULL; |
+} |
+ |
+template<> |
+MenuRunner::RunResult MenuRunner::RunMenuAt(Widget* parent, |
+ MenuButton* button, |
+ const gfx::Rect& bounds, |
+ ui::MenuAnchorPosition anchor, |
+ ui::MenuSourceType source_type, |
+ int32 types) { |
+ // TODO(tapted): See if we need a DisplayChangeListener. |
+ |
+ // TODO(tapted): Adjust the anchor for context menu / touch, if Cocoa can't be |
+ // trusted to do it. |
+ |
+ return holder_->RunMenuAt(parent, button, bounds, anchor, types); |
+} |
+ |
+bool MenuRunner::IsRunning() const { |
+ return holder_->running(); |
+} |
+ |
+void MenuRunner::Cancel() { |
+ holder_->Cancel(); |
+} |
+ |
+base::TimeDelta MenuRunner::closing_event_time() const { |
+ return holder_->closing_event_time(); |
+} |
+ |
+void MenuRunner::SetRunnerHandler( |
+ scoped_ptr<MenuRunnerHandler> runner_handler) { |
+ NOTIMPLEMENTED(); |
+} |
+ |
+} // namespace views |