OLD | NEW |
(Empty) | |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "ui/views/controls/menu/menu_runner.h" |
| 6 |
| 7 #include <Cocoa/Cocoa.h> |
| 8 |
| 9 #include "base/mac/scoped_nsobject.h" |
| 10 #include "base/time/time.h" |
| 11 #include "ui/base/cocoa/menu_controller.h" |
| 12 #include "ui/base/models/menu_model.h" |
| 13 #include "ui/events/event_utils.h" |
| 14 #include "ui/gfx/geometry/rect.h" |
| 15 #include "ui/gfx/mac/point_utils.h" |
| 16 |
| 17 namespace views { |
| 18 |
| 19 class MenuModelAdapter {}; |
| 20 class MenuRunnerHandler {}; |
| 21 |
| 22 namespace internal { |
| 23 |
| 24 // Manages the menu. To destroy a MenuRunnerImpl invoke Release(). Release() |
| 25 // deletes immediately if the menu isn't showing. If the menu is showing |
| 26 // Release() cancels the menu and when the nested RunMenuAt() call returns |
| 27 // deletes itself and the menu. |
| 28 class MenuRunnerImpl { |
| 29 public: |
| 30 explicit MenuRunnerImpl(ui::MenuModel* menu_model); |
| 31 |
| 32 NSMenu* menu() { return [menu_controller_ menu]; } |
| 33 |
| 34 bool running() const { return [menu_controller_ isMenuOpen]; } |
| 35 |
| 36 void Release() { menu_controller_.reset(); } |
| 37 |
| 38 // Runs the menu. |
| 39 MenuRunner::RunResult RunMenuAt(Widget* parent, |
| 40 MenuButton* button, |
| 41 const gfx::Rect& bounds, |
| 42 ui::MenuAnchorPosition anchor, |
| 43 int32 types) WARN_UNUSED_RESULT; |
| 44 |
| 45 void Cancel() { [menu_controller_ cancel]; } |
| 46 |
| 47 // Returns the time from the event which closed the menu - or 0. |
| 48 base::TimeDelta closing_event_time() const { |
| 49 return closing_event_time_; |
| 50 } |
| 51 |
| 52 private: |
| 53 // The Cocoa menu controller that this instance is bridging. |
| 54 base::scoped_nsobject<MenuController> menu_controller_; |
| 55 |
| 56 // The timestamp of the event which closed the menu - or 0. |
| 57 base::TimeDelta closing_event_time_; |
| 58 |
| 59 DISALLOW_COPY_AND_ASSIGN(MenuRunnerImpl); |
| 60 }; |
| 61 |
| 62 MenuRunnerImpl::MenuRunnerImpl(ui::MenuModel* menu) |
| 63 : closing_event_time_(base::TimeDelta()) { |
| 64 menu_controller_.reset([[MenuController alloc] initWithModel:menu |
| 65 useWithPopUpButtonCell:NO]); |
| 66 } |
| 67 |
| 68 MenuRunner::RunResult MenuRunnerImpl::RunMenuAt( |
| 69 Widget* parent, |
| 70 MenuButton* button, |
| 71 const gfx::Rect& bounds, |
| 72 ui::MenuAnchorPosition anchor, |
| 73 int32 types) { |
| 74 closing_event_time_ = base::TimeDelta(); |
| 75 // TODO(tapted): Ensure this is OK with being invoked while already running(). |
| 76 |
| 77 if ((types & MenuRunner::FOR_DROP) != 0) { |
| 78 NOTIMPLEMENTED(); |
| 79 return MenuRunner::NORMAL_EXIT; |
| 80 } |
| 81 |
| 82 [menu() popUpMenuPositioningItem:nil |
| 83 atLocation:ScreenPointToNSPoint(bounds.origin()) |
| 84 inView:nil]; |
| 85 |
| 86 // Get the time of the event which closed this menu. |
| 87 closing_event_time_ = ui::EventTimeForNow(); |
| 88 return MenuRunner::NORMAL_EXIT; |
| 89 } |
| 90 |
| 91 } // namespace internal |
| 92 |
| 93 MenuRunner::MenuRunner(ui::MenuModel* menu_model) |
| 94 : holder_(new internal::MenuRunnerImpl(menu_model)) { |
| 95 } |
| 96 |
| 97 MenuRunner::MenuRunner(MenuItemView* menu) { |
| 98 NOTIMPLEMENTED(); |
| 99 } |
| 100 |
| 101 MenuRunner::~MenuRunner() { |
| 102 holder_->Release(); |
| 103 } |
| 104 |
| 105 MenuItemView* MenuRunner::GetMenu() { |
| 106 NOTIMPLEMENTED(); |
| 107 return NULL; |
| 108 } |
| 109 |
| 110 template<> |
| 111 MenuRunner::RunResult MenuRunner::RunMenuAt(Widget* parent, |
| 112 MenuButton* button, |
| 113 const gfx::Rect& bounds, |
| 114 ui::MenuAnchorPosition anchor, |
| 115 ui::MenuSourceType source_type, |
| 116 int32 types) { |
| 117 // TODO(tapted): See if we need a DisplayChangeListener. |
| 118 |
| 119 // TODO(tapted): Adjust the anchor for context menu / touch, if Cocoa can't be |
| 120 // trusted to do it. |
| 121 |
| 122 return holder_->RunMenuAt(parent, button, bounds, anchor, types); |
| 123 } |
| 124 |
| 125 bool MenuRunner::IsRunning() const { |
| 126 return holder_->running(); |
| 127 } |
| 128 |
| 129 void MenuRunner::Cancel() { |
| 130 holder_->Cancel(); |
| 131 } |
| 132 |
| 133 base::TimeDelta MenuRunner::closing_event_time() const { |
| 134 return holder_->closing_event_time(); |
| 135 } |
| 136 |
| 137 void MenuRunner::SetRunnerHandler( |
| 138 scoped_ptr<MenuRunnerHandler> runner_handler) { |
| 139 NOTIMPLEMENTED(); |
| 140 } |
| 141 |
| 142 } // namespace views |
OLD | NEW |