1 // Copyright 2014 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.
5 #import "ui/views/controls/menu/menu_runner_impl_cocoa.h"
7 #import "ui/base/cocoa/menu_controller.h"
8 #include "ui/events/event_utils.h"
9 #include "ui/gfx/geometry/rect.h"
10 #include "ui/views/controls/menu/menu_runner_impl_adapter.h"
16 MenuRunnerImplInterface* MenuRunnerImplInterface::Create(
17 ui::MenuModel* menu_model,
19 if ((run_types & MenuRunner::CONTEXT_MENU) != 0 &&
20 (run_types & MenuRunner::IS_NESTED) == 0) {
21 return new MenuRunnerImplCocoa(menu_model);
24 return new MenuRunnerImplAdapter(menu_model);
27 MenuRunnerImplCocoa::MenuRunnerImplCocoa(ui::MenuModel* menu)
28 : delete_after_run_(false), closing_event_time_(base::TimeDelta()) {
29 menu_controller_.reset(
30 [[MenuController alloc] initWithModel:menu useWithPopUpButtonCell:NO]);
33 bool MenuRunnerImplCocoa::IsRunning() const {
34 return [menu_controller_ isMenuOpen];
37 void MenuRunnerImplCocoa::Release() {
39 if (delete_after_run_)
40 return; // We already canceled.
42 delete_after_run_ = true;
43 [menu_controller_ cancel];
49 MenuRunner::RunResult MenuRunnerImplCocoa::RunMenuAt(Widget* parent,
51 const gfx::Rect& bounds,
52 MenuAnchorPosition anchor,
54 DCHECK(run_types & MenuRunner::CONTEXT_MENU);
56 closing_event_time_ = base::TimeDelta();
57 [NSMenu popUpContextMenu:[menu_controller_ menu]
58 withEvent:[NSApp currentEvent]
60 closing_event_time_ = ui::EventTimeForNow();
62 if (delete_after_run_) {
64 return MenuRunner::MENU_DELETED;
67 return MenuRunner::NORMAL_EXIT;
70 void MenuRunnerImplCocoa::Cancel() {
71 [menu_controller_ cancel];
74 base::TimeDelta MenuRunnerImplCocoa::GetClosingEventTime() const {
75 return closing_event_time_;
78 MenuRunnerImplCocoa::~MenuRunnerImplCocoa() {
81 } // namespace internal