| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #import "ui/views/controls/menu/menu_runner_impl_cocoa.h" | 5 #import "ui/views/controls/menu/menu_runner_impl_cocoa.h" |
| 6 | 6 |
| 7 #include "base/mac/sdk_forward_declarations.h" | 7 #include "base/mac/sdk_forward_declarations.h" |
| 8 #import "ui/base/cocoa/menu_controller.h" | 8 #import "ui/base/cocoa/menu_controller.h" |
| 9 #include "ui/base/models/menu_model.h" | 9 #include "ui/base/models/menu_model.h" |
| 10 #include "ui/events/event_utils.h" | 10 #include "ui/events/event_utils.h" |
| (...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 86 int32_t run_types) { | 86 int32_t run_types) { |
| 87 if ((run_types & kNativeRunTypes) != 0 && | 87 if ((run_types & kNativeRunTypes) != 0 && |
| 88 (run_types & MenuRunner::IS_NESTED) == 0) { | 88 (run_types & MenuRunner::IS_NESTED) == 0) { |
| 89 return new MenuRunnerImplCocoa(menu_model); | 89 return new MenuRunnerImplCocoa(menu_model); |
| 90 } | 90 } |
| 91 | 91 |
| 92 return new MenuRunnerImplAdapter(menu_model); | 92 return new MenuRunnerImplAdapter(menu_model); |
| 93 } | 93 } |
| 94 | 94 |
| 95 MenuRunnerImplCocoa::MenuRunnerImplCocoa(ui::MenuModel* menu) | 95 MenuRunnerImplCocoa::MenuRunnerImplCocoa(ui::MenuModel* menu) |
| 96 : delete_after_run_(false), closing_event_time_(base::TimeDelta()) { | 96 : running_(false), |
| 97 delete_after_run_(false), |
| 98 closing_event_time_(base::TimeDelta()) { |
| 97 menu_controller_.reset( | 99 menu_controller_.reset( |
| 98 [[MenuController alloc] initWithModel:menu useWithPopUpButtonCell:NO]); | 100 [[MenuController alloc] initWithModel:menu useWithPopUpButtonCell:NO]); |
| 99 } | 101 } |
| 100 | 102 |
| 101 bool MenuRunnerImplCocoa::IsRunning() const { | 103 bool MenuRunnerImplCocoa::IsRunning() const { |
| 102 return [menu_controller_ isMenuOpen]; | 104 return running_; |
| 103 } | 105 } |
| 104 | 106 |
| 105 void MenuRunnerImplCocoa::Release() { | 107 void MenuRunnerImplCocoa::Release() { |
| 106 if (IsRunning()) { | 108 if (IsRunning()) { |
| 107 if (delete_after_run_) | 109 if (delete_after_run_) |
| 108 return; // We already canceled. | 110 return; // We already canceled. |
| 109 | 111 |
| 110 delete_after_run_ = true; | 112 delete_after_run_ = true; |
| 111 [menu_controller_ cancel]; | 113 [menu_controller_ cancel]; |
| 112 } else { | 114 } else { |
| 113 delete this; | 115 delete this; |
| 114 } | 116 } |
| 115 } | 117 } |
| 116 | 118 |
| 117 MenuRunner::RunResult MenuRunnerImplCocoa::RunMenuAt(Widget* parent, | 119 MenuRunner::RunResult MenuRunnerImplCocoa::RunMenuAt(Widget* parent, |
| 118 MenuButton* button, | 120 MenuButton* button, |
| 119 const gfx::Rect& bounds, | 121 const gfx::Rect& bounds, |
| 120 MenuAnchorPosition anchor, | 122 MenuAnchorPosition anchor, |
| 121 int32_t run_types) { | 123 int32_t run_types) { |
| 122 DCHECK(run_types & kNativeRunTypes); | 124 DCHECK(run_types & kNativeRunTypes); |
| 123 DCHECK(!IsRunning()); | 125 DCHECK(!IsRunning()); |
| 124 DCHECK(parent); | 126 DCHECK(parent); |
| 125 closing_event_time_ = base::TimeDelta(); | 127 closing_event_time_ = base::TimeDelta(); |
| 128 running_ = true; |
| 126 | 129 |
| 127 if (run_types & MenuRunner::CONTEXT_MENU) { | 130 if (run_types & MenuRunner::CONTEXT_MENU) { |
| 128 [NSMenu popUpContextMenu:[menu_controller_ menu] | 131 [NSMenu popUpContextMenu:[menu_controller_ menu] |
| 129 withEvent:[NSApp currentEvent] | 132 withEvent:[NSApp currentEvent] |
| 130 forView:parent->GetNativeView()]; | 133 forView:parent->GetNativeView()]; |
| 131 } else if (run_types & MenuRunner::COMBOBOX) { | 134 } else if (run_types & MenuRunner::COMBOBOX) { |
| 132 NSMenuItem* checked_item = FirstCheckedItem(menu_controller_); | 135 NSMenuItem* checked_item = FirstCheckedItem(menu_controller_); |
| 133 base::scoped_nsobject<NSView> anchor_view( | 136 base::scoped_nsobject<NSView> anchor_view( |
| 134 CreateMenuAnchorView(parent->GetNativeWindow(), bounds, checked_item)); | 137 CreateMenuAnchorView(parent->GetNativeWindow(), bounds, checked_item)); |
| 135 NSMenu* menu = [menu_controller_ menu]; | 138 NSMenu* menu = [menu_controller_ menu]; |
| 136 [menu setMinimumWidth:bounds.width() + kNativeCheckmarkWidth]; | 139 [menu setMinimumWidth:bounds.width() + kNativeCheckmarkWidth]; |
| 137 [menu popUpMenuPositioningItem:checked_item | 140 [menu popUpMenuPositioningItem:checked_item |
| 138 atLocation:NSZeroPoint | 141 atLocation:NSZeroPoint |
| 139 inView:anchor_view]; | 142 inView:anchor_view]; |
| 140 [anchor_view removeFromSuperview]; | 143 [anchor_view removeFromSuperview]; |
| 141 } else { | 144 } else { |
| 142 NOTREACHED(); | 145 NOTREACHED(); |
| 143 } | 146 } |
| 144 | 147 |
| 145 closing_event_time_ = ui::EventTimeForNow(); | 148 closing_event_time_ = ui::EventTimeForNow(); |
| 149 running_ = false; |
| 146 | 150 |
| 147 if (delete_after_run_) { | 151 if (delete_after_run_) { |
| 148 delete this; | 152 delete this; |
| 149 return MenuRunner::MENU_DELETED; | 153 return MenuRunner::MENU_DELETED; |
| 150 } | 154 } |
| 151 | 155 |
| 152 return MenuRunner::NORMAL_EXIT; | 156 return MenuRunner::NORMAL_EXIT; |
| 153 } | 157 } |
| 154 | 158 |
| 155 void MenuRunnerImplCocoa::Cancel() { | 159 void MenuRunnerImplCocoa::Cancel() { |
| 156 [menu_controller_ cancel]; | 160 [menu_controller_ cancel]; |
| 157 } | 161 } |
| 158 | 162 |
| 159 base::TimeDelta MenuRunnerImplCocoa::GetClosingEventTime() const { | 163 base::TimeDelta MenuRunnerImplCocoa::GetClosingEventTime() const { |
| 160 return closing_event_time_; | 164 return closing_event_time_; |
| 161 } | 165 } |
| 162 | 166 |
| 163 MenuRunnerImplCocoa::~MenuRunnerImplCocoa() { | 167 MenuRunnerImplCocoa::~MenuRunnerImplCocoa() { |
| 164 } | 168 } |
| 165 | 169 |
| 166 } // namespace internal | 170 } // namespace internal |
| 167 } // namespace views | 171 } // namespace views |
| OLD | NEW |