Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1153)

Side by Side Diff: ui/views/examples/menu_example.cc

Issue 1904753002: MenuButton: support Mac look & feel (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Add basic tests Created 4 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 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 #include "ui/views/examples/menu_example.h" 5 #include "ui/views/examples/menu_example.h"
6 6
7 #include <set> 7 #include <set>
8 8
9 #include "base/macros.h" 9 #include "base/macros.h"
10 #include "base/strings/utf_string_conversions.h" 10 #include "base/strings/utf_string_conversions.h"
11 #include "ui/base/models/simple_menu_model.h" 11 #include "ui/base/models/simple_menu_model.h"
12 #include "ui/views/controls/button/menu_button.h" 12 #include "ui/views/controls/button/menu_button.h"
13 #include "ui/views/controls/button/menu_button_listener.h" 13 #include "ui/views/controls/button/menu_button_listener.h"
14 #include "ui/views/controls/menu/menu_runner.h" 14 #include "ui/views/controls/menu/menu_runner.h"
15 #include "ui/views/layout/fill_layout.h" 15 #include "ui/views/layout/box_layout.h"
16 #include "ui/views/view.h" 16 #include "ui/views/view.h"
17 #include "ui/views/widget/widget.h" 17 #include "ui/views/widget/widget.h"
18 18
19 using base::ASCIIToUTF16; 19 using base::ASCIIToUTF16;
20 20
21 namespace views { 21 namespace views {
22 namespace examples { 22 namespace examples {
23 23
24 namespace { 24 namespace {
25 25
(...skipping 27 matching lines...) Expand all
53 53
54 std::unique_ptr<ui::SimpleMenuModel> submenu_; 54 std::unique_ptr<ui::SimpleMenuModel> submenu_;
55 std::set<int> checked_fruits_; 55 std::set<int> checked_fruits_;
56 int current_encoding_command_id_; 56 int current_encoding_command_id_;
57 57
58 DISALLOW_COPY_AND_ASSIGN(ExampleMenuModel); 58 DISALLOW_COPY_AND_ASSIGN(ExampleMenuModel);
59 }; 59 };
60 60
61 class ExampleMenuButton : public MenuButton, public MenuButtonListener { 61 class ExampleMenuButton : public MenuButton, public MenuButtonListener {
62 public: 62 public:
63 explicit ExampleMenuButton(const base::string16& test); 63 ExampleMenuButton(const base::string16& test,
64 bool use_border,
65 bool use_background);
64 ~ExampleMenuButton() override; 66 ~ExampleMenuButton() override;
65 67
68 protected:
69 bool ShouldUsePlatformStyleBackground() const override;
70 bool ShouldUsePlatformStyleBorder() const override;
71
66 private: 72 private:
67 // MenuButtonListener: 73 // MenuButtonListener:
68 void OnMenuButtonClicked(MenuButton* source, 74 void OnMenuButtonClicked(MenuButton* source,
69 const gfx::Point& point, 75 const gfx::Point& point,
70 const ui::Event* event) override; 76 const ui::Event* event) override;
71 77
72 ui::SimpleMenuModel* GetMenuModel(); 78 ui::SimpleMenuModel* GetMenuModel();
73 79
74 std::unique_ptr<ExampleMenuModel> menu_model_; 80 std::unique_ptr<ExampleMenuModel> menu_model_;
75 std::unique_ptr<MenuRunner> menu_runner_; 81 std::unique_ptr<MenuRunner> menu_runner_;
76 82
83 bool use_background_;
84 bool use_border_;
85
77 DISALLOW_COPY_AND_ASSIGN(ExampleMenuButton); 86 DISALLOW_COPY_AND_ASSIGN(ExampleMenuButton);
78 }; 87 };
79 88
80 // ExampleMenuModel --------------------------------------------------------- 89 // ExampleMenuModel ---------------------------------------------------------
81 90
82 ExampleMenuModel::ExampleMenuModel() 91 ExampleMenuModel::ExampleMenuModel()
83 : ui::SimpleMenuModel(this), 92 : ui::SimpleMenuModel(this),
84 current_encoding_command_id_(COMMAND_SELECT_ASCII) { 93 current_encoding_command_id_(COMMAND_SELECT_ASCII) {
85 AddItem(COMMAND_DO_SOMETHING, ASCIIToUTF16("Do Something")); 94 AddItem(COMMAND_DO_SOMETHING, ASCIIToUTF16("Do Something"));
86 AddSeparator(ui::NORMAL_SEPARATOR); 95 AddSeparator(ui::NORMAL_SEPARATOR);
(...skipping 85 matching lines...) Expand 10 before | Expand all | Expand 10 after
172 DVLOG(1) << "Unchecked " << checked_fruit; 181 DVLOG(1) << "Unchecked " << checked_fruit;
173 checked_fruits_.erase(iter); 182 checked_fruits_.erase(iter);
174 } 183 }
175 break; 184 break;
176 } 185 }
177 } 186 }
178 } 187 }
179 188
180 // ExampleMenuButton ----------------------------------------------------------- 189 // ExampleMenuButton -----------------------------------------------------------
181 190
182 ExampleMenuButton::ExampleMenuButton(const base::string16& test) 191 ExampleMenuButton::ExampleMenuButton(const base::string16& test,
183 : MenuButton(test, this, true) {} 192 bool use_background,
193 bool use_border)
194 : MenuButton(test, this, true),
195 use_background_(use_background),
196 use_border_(use_border) {}
184 197
185 ExampleMenuButton::~ExampleMenuButton() { 198 ExampleMenuButton::~ExampleMenuButton() {
186 } 199 }
187 200
188 void ExampleMenuButton::OnMenuButtonClicked(MenuButton* source, 201 void ExampleMenuButton::OnMenuButtonClicked(MenuButton* source,
189 const gfx::Point& point, 202 const gfx::Point& point,
190 const ui::Event* event) { 203 const ui::Event* event) {
191 menu_runner_.reset(new MenuRunner(GetMenuModel(), MenuRunner::HAS_MNEMONICS)); 204 menu_runner_.reset(new MenuRunner(GetMenuModel(), MenuRunner::HAS_MNEMONICS));
192 205
193 if (menu_runner_->RunMenuAt(source->GetWidget()->GetTopLevelWidget(), 206 if (menu_runner_->RunMenuAt(source->GetWidget()->GetTopLevelWidget(),
194 this, 207 this,
195 gfx::Rect(point, gfx::Size()), 208 gfx::Rect(point, gfx::Size()),
196 MENU_ANCHOR_TOPRIGHT, 209 MENU_ANCHOR_TOPRIGHT,
197 ui::MENU_SOURCE_NONE) == 210 ui::MENU_SOURCE_NONE) ==
198 MenuRunner::MENU_DELETED) { 211 MenuRunner::MENU_DELETED) {
199 return; 212 return;
200 } 213 }
201 } 214 }
202 215
203 ui::SimpleMenuModel* ExampleMenuButton::GetMenuModel() { 216 ui::SimpleMenuModel* ExampleMenuButton::GetMenuModel() {
204 if (!menu_model_.get()) 217 if (!menu_model_.get())
205 menu_model_.reset(new ExampleMenuModel); 218 menu_model_.reset(new ExampleMenuModel);
206 return menu_model_.get(); 219 return menu_model_.get();
207 } 220 }
208 221
222 bool ExampleMenuButton::ShouldUsePlatformStyleBackground() const {
223 return use_background_;
224 }
225
226 bool ExampleMenuButton::ShouldUsePlatformStyleBorder() const {
227 return use_border_;
228 }
229
209 } // namespace 230 } // namespace
210 231
211 MenuExample::MenuExample() : ExampleBase("Menu") { 232 MenuExample::MenuExample() : ExampleBase("Menu") {
212 } 233 }
213 234
214 MenuExample::~MenuExample() { 235 MenuExample::~MenuExample() {
215 } 236 }
216 237
217 void MenuExample::CreateExampleView(View* container) { 238 void MenuExample::CreateExampleView(View* container) {
218 // We add a button to open a menu. 239 // We add a button to open a menu.
219 ExampleMenuButton* menu_button = new ExampleMenuButton( 240 ExampleMenuButton* menu_button = new ExampleMenuButton(
220 ASCIIToUTF16("Open a menu")); 241 ASCIIToUTF16("Open a menu (no decoration)"), false, false);
221 container->SetLayoutManager(new FillLayout); 242 ExampleMenuButton* background_button = new ExampleMenuButton(
243 ASCIIToUTF16("Open a menu (background only)"), true, false);
244 ExampleMenuButton* border_button = new ExampleMenuButton(
245 ASCIIToUTF16("Open a menu (border only)"), false, true);
246 ExampleMenuButton* both_button = new ExampleMenuButton(
247 ASCIIToUTF16("Open a menu (both decorations)"), true, true);
248 container->SetLayoutManager(new BoxLayout(BoxLayout::kVertical, 1, 1, 1));
222 container->AddChildView(menu_button); 249 container->AddChildView(menu_button);
250 container->AddChildView(background_button);
251 container->AddChildView(border_button);
252 container->AddChildView(both_button);
223 } 253 }
224 254
225 } // namespace examples 255 } // namespace examples
226 } // namespace views 256 } // namespace views
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698