Add ICU message format support
[chromium-blink-merge.git] / ui / views / controls / menu / menu_runner_impl_cocoa.mm
blob849558fddb788ee84573ce3f003a4949f3d43f40
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"
12 namespace views {
13 namespace internal {
15 // static
16 MenuRunnerImplInterface* MenuRunnerImplInterface::Create(
17     ui::MenuModel* menu_model,
18     int32 run_types) {
19   if ((run_types & MenuRunner::CONTEXT_MENU) != 0 &&
20       (run_types & MenuRunner::IS_NESTED) == 0) {
21     return new MenuRunnerImplCocoa(menu_model);
22   }
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() {
38   if (IsRunning()) {
39     if (delete_after_run_)
40       return;  // We already canceled.
42     delete_after_run_ = true;
43     [menu_controller_ cancel];
44   } else {
45     delete this;
46   }
49 MenuRunner::RunResult MenuRunnerImplCocoa::RunMenuAt(Widget* parent,
50                                                      MenuButton* button,
51                                                      const gfx::Rect& bounds,
52                                                      MenuAnchorPosition anchor,
53                                                      int32 run_types) {
54   DCHECK(run_types & MenuRunner::CONTEXT_MENU);
55   DCHECK(!IsRunning());
56   closing_event_time_ = base::TimeDelta();
57   [NSMenu popUpContextMenu:[menu_controller_ menu]
58                  withEvent:[NSApp currentEvent]
59                    forView:nil];
60   closing_event_time_ = ui::EventTimeForNow();
62   if (delete_after_run_) {
63     delete this;
64     return MenuRunner::MENU_DELETED;
65   }
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
82 }  // namespace views