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

Side by Side Diff: chrome/browser/ui/views/toolbar/app_menu.cc

Issue 1988983004: Fix app menu borders for RTL (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Address review 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
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2013 The Chromium Authors. All rights reserved. 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 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 "chrome/browser/ui/views/toolbar/app_menu.h" 5 #include "chrome/browser/ui/views/toolbar/app_menu.h"
6 6
7 #include <stdint.h> 7 #include <stdint.h>
8 8
9 #include <algorithm> 9 #include <algorithm>
10 #include <cmath> 10 #include <cmath>
(...skipping 120 matching lines...) Expand 10 before | Expand all | Expand 10 after
131 private: 131 private:
132 DISALLOW_COPY_AND_ASSIGN(FullscreenButton); 132 DISALLOW_COPY_AND_ASSIGN(FullscreenButton);
133 }; 133 };
134 134
135 // Combination border/background for the buttons contained in the menu. The 135 // Combination border/background for the buttons contained in the menu. The
136 // painting of the border/background is done here as LabelButton does not always 136 // painting of the border/background is done here as LabelButton does not always
137 // paint the border. 137 // paint the border.
138 class InMenuButtonBackground : public views::Background { 138 class InMenuButtonBackground : public views::Background {
139 public: 139 public:
140 enum ButtonType { 140 enum ButtonType {
141 // A rectangular button with no neighbor on the left. 141 // A rectangular button with no drawn border.
142 LEFT_BUTTON, 142 NO_BORDER,
143 143
144 // A rectangular button with neighbors on both sides. 144 // A rectangular button with a border drawn along the leading (left) side.
145 CENTER_BUTTON, 145 LEADING_BORDER,
146 146
147 // A rectangular button with no neighbor on the right. 147 // A button with no drawn border and a rounded background.
148 RIGHT_BUTTON,
149
150 // A rectangular button that is not a member in a group.
151 SINGLE_BUTTON,
152
153 // A button with no group neighbors and a rounded background.
154 ROUNDED_BUTTON, 148 ROUNDED_BUTTON,
155 }; 149 };
156 150
157 explicit InMenuButtonBackground(ButtonType type) 151 explicit InMenuButtonBackground(ButtonType type) : type_(type) {}
158 : type_(type), left_button_(NULL), right_button_(NULL) {}
159
160 // Used when the type is CENTER_BUTTON to determine if the left/right edge
161 // needs to be rendered selected.
162 void SetOtherButtons(const CustomButton* left_button,
163 const CustomButton* right_button) {
164 if (base::i18n::IsRTL()) {
165 left_button_ = right_button;
166 right_button_ = left_button;
167 } else {
168 left_button_ = left_button;
169 right_button_ = right_button;
170 }
171 }
172 152
173 // Overridden from views::Background. 153 // Overridden from views::Background.
174 void Paint(gfx::Canvas* canvas, View* view) const override { 154 void Paint(gfx::Canvas* canvas, View* view) const override {
175 CustomButton* button = CustomButton::AsCustomButton(view); 155 CustomButton* button = CustomButton::AsCustomButton(view);
176 views::Button::ButtonState state = 156 views::Button::ButtonState state =
177 button ? button->state() : views::Button::STATE_NORMAL; 157 button ? button->state() : views::Button::STATE_NORMAL;
178 int h = view->height(); 158 int h = view->height();
179 159
180 // Normal buttons get a border drawn on the right side and the rest gets 160 // Draw leading border where needed. This is along the left edge unless the
181 // filled in. The left or rounded buttons however do not get a line to 161 // layout is RTL and the button isn't mirroring itself.
182 // combine buttons.
183 gfx::Rect bounds(view->GetLocalBounds()); 162 gfx::Rect bounds(view->GetLocalBounds());
184 if (type_ != RIGHT_BUTTON && type_ != ROUNDED_BUTTON) { 163 if (type_ == LEADING_BORDER) {
185 canvas->FillRect(gfx::Rect(0, 0, 1, h), 164 gfx::Rect rect = view->FlipCanvasOnPaintForRTLUI()
186 BorderColor(view, views::Button::STATE_NORMAL)); 165 ? gfx::Rect(0, 0, 1, h)
166 : gfx::Rect(view->GetMirroredXWithWidthInView(0, 1), 0, 1, h);
167 canvas->FillRect(rect, BorderColor(view, views::Button::STATE_NORMAL));
187 bounds.Inset(gfx::Insets(0, 1, 0, 0)); 168 bounds.Inset(gfx::Insets(0, 1, 0, 0));
188 } 169 }
189 170
171 // Fill in background for state.
190 bounds.set_x(view->GetMirroredXForRect(bounds)); 172 bounds.set_x(view->GetMirroredXForRect(bounds));
191 DrawBackground(canvas, view, bounds, state); 173 DrawBackground(canvas, view, bounds, state);
192 } 174 }
193 175
194 private: 176 private:
195 static SkColor BorderColor(View* view, views::Button::ButtonState state) { 177 static SkColor BorderColor(View* view, views::Button::ButtonState state) {
196 ui::NativeTheme* theme = view->GetNativeTheme(); 178 ui::NativeTheme* theme = view->GetNativeTheme();
197 switch (state) { 179 switch (state) {
198 case views::Button::STATE_HOVERED: 180 case views::Button::STATE_HOVERED:
199 return theme->GetSystemColor( 181 return theme->GetSystemColor(
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after
236 // Consistent with a hover corner radius (kInkDropSmallCornerRadius). 218 // Consistent with a hover corner radius (kInkDropSmallCornerRadius).
237 const int kBackgroundCornerRadius = 2; 219 const int kBackgroundCornerRadius = 2;
238 params.menu_item.corner_radius = kBackgroundCornerRadius; 220 params.menu_item.corner_radius = kBackgroundCornerRadius;
239 } 221 }
240 view->GetNativeTheme()->Paint(canvas->sk_canvas(), 222 view->GetNativeTheme()->Paint(canvas->sk_canvas(),
241 ui::NativeTheme::kMenuItemBackground, 223 ui::NativeTheme::kMenuItemBackground,
242 ui::NativeTheme::kHovered, bounds, params); 224 ui::NativeTheme::kHovered, bounds, params);
243 } 225 }
244 } 226 }
245 227
246 ButtonType TypeAdjustedForRTL() const {
247 if (!base::i18n::IsRTL())
248 return type_;
249
250 switch (type_) {
251 case LEFT_BUTTON: return RIGHT_BUTTON;
252 case RIGHT_BUTTON: return LEFT_BUTTON;
253 default: break;
254 }
255 return type_;
256 }
257
258 const ButtonType type_; 228 const ButtonType type_;
259 229
260 // See description above setter for details.
261 const CustomButton* left_button_;
262 const CustomButton* right_button_;
263
264 DISALLOW_COPY_AND_ASSIGN(InMenuButtonBackground); 230 DISALLOW_COPY_AND_ASSIGN(InMenuButtonBackground);
265 }; 231 };
266 232
267 base::string16 GetAccessibleNameForAppMenuItem(ButtonMenuItemModel* model, 233 base::string16 GetAccessibleNameForAppMenuItem(ButtonMenuItemModel* model,
268 int item_index, 234 int item_index,
269 int accessible_string_id) { 235 int accessible_string_id) {
270 base::string16 accessible_name = 236 base::string16 accessible_name =
271 l10n_util::GetStringUTF16(accessible_string_id); 237 l10n_util::GetStringUTF16(accessible_string_id);
272 base::string16 accelerator_text; 238 base::string16 accelerator_text;
273 239
(...skipping 21 matching lines...) Expand all
295 SetFocusBehavior(FocusBehavior::ALWAYS); 261 SetFocusBehavior(FocusBehavior::ALWAYS);
296 SetHorizontalAlignment(gfx::ALIGN_CENTER); 262 SetHorizontalAlignment(gfx::ALIGN_CENTER);
297 263
298 in_menu_background_ = new InMenuButtonBackground(type); 264 in_menu_background_ = new InMenuButtonBackground(type);
299 set_background(in_menu_background_); 265 set_background(in_menu_background_);
300 SetBorder(views::Border::CreateEmptyBorder(0, kHorizontalPadding, 0, 266 SetBorder(views::Border::CreateEmptyBorder(0, kHorizontalPadding, 0,
301 kHorizontalPadding)); 267 kHorizontalPadding));
302 SetFontList(MenuConfig::instance().font_list); 268 SetFontList(MenuConfig::instance().font_list);
303 } 269 }
304 270
305 void SetOtherButtons(const InMenuButton* left, const InMenuButton* right) {
306 in_menu_background_->SetOtherButtons(left, right);
307 }
308
309 void GetAccessibleState(ui::AXViewState* state) override { 271 void GetAccessibleState(ui::AXViewState* state) override {
310 LabelButton::GetAccessibleState(state); 272 LabelButton::GetAccessibleState(state);
311 state->role = ui::AX_ROLE_MENU_ITEM; 273 state->role = ui::AX_ROLE_MENU_ITEM;
312 } 274 }
313 275
314 // views::LabelButton 276 // views::LabelButton
315 void OnNativeThemeChanged(const ui::NativeTheme* theme) override { 277 void OnNativeThemeChanged(const ui::NativeTheme* theme) override {
316 if (theme) { 278 if (theme) {
317 SetTextColor( 279 SetTextColor(
318 views::Button::STATE_DISABLED, 280 views::Button::STATE_DISABLED,
(...skipping 140 matching lines...) Expand 10 before | Expand all | Expand 10 after
459 421
460 // CutCopyPasteView is the view containing the cut/copy/paste buttons. 422 // CutCopyPasteView is the view containing the cut/copy/paste buttons.
461 class AppMenu::CutCopyPasteView : public AppMenuView { 423 class AppMenu::CutCopyPasteView : public AppMenuView {
462 public: 424 public:
463 CutCopyPasteView(AppMenu* menu, 425 CutCopyPasteView(AppMenu* menu,
464 ButtonMenuItemModel* menu_model, 426 ButtonMenuItemModel* menu_model,
465 int cut_index, 427 int cut_index,
466 int copy_index, 428 int copy_index,
467 int paste_index) 429 int paste_index)
468 : AppMenuView(menu, menu_model) { 430 : AppMenuView(menu, menu_model) {
469 InMenuButton* cut = CreateAndConfigureButton( 431 CreateAndConfigureButton(IDS_CUT, InMenuButtonBackground::LEADING_BORDER,
470 IDS_CUT, InMenuButtonBackground::LEFT_BUTTON, cut_index); 432 cut_index);
471 InMenuButton* copy = CreateAndConfigureButton( 433 CreateAndConfigureButton(IDS_COPY, InMenuButtonBackground::LEADING_BORDER,
472 IDS_COPY, InMenuButtonBackground::CENTER_BUTTON, copy_index); 434 copy_index);
473 InMenuButton* paste = CreateAndConfigureButton( 435 CreateAndConfigureButton(IDS_PASTE, InMenuButtonBackground::LEADING_BORDER,
474 IDS_PASTE, InMenuButtonBackground::CENTER_BUTTON, paste_index); 436 paste_index);
475 copy->SetOtherButtons(cut, paste);
476 } 437 }
477 438
478 // Overridden from View. 439 // Overridden from View.
479 gfx::Size GetPreferredSize() const override { 440 gfx::Size GetPreferredSize() const override {
480 // Returned height doesn't matter as MenuItemView forces everything to the 441 // Returned height doesn't matter as MenuItemView forces everything to the
481 // height of the menuitemview. 442 // height of the menuitemview.
482 return gfx::Size(GetMaxChildViewPreferredWidth() * child_count(), 0); 443 return gfx::Size(GetMaxChildViewPreferredWidth() * child_count(), 0);
483 } 444 }
484 445
485 void Layout() override { 446 void Layout() override {
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after
528 zoom_label_max_width_(0), 489 zoom_label_max_width_(0),
529 zoom_label_max_width_valid_(false) { 490 zoom_label_max_width_valid_(false) {
530 browser_zoom_subscription_ = 491 browser_zoom_subscription_ =
531 ui_zoom::ZoomEventManager::GetForBrowserContext( 492 ui_zoom::ZoomEventManager::GetForBrowserContext(
532 menu->browser_->profile()) 493 menu->browser_->profile())
533 ->AddZoomLevelChangedCallback( 494 ->AddZoomLevelChangedCallback(
534 base::Bind(&AppMenu::ZoomView::OnZoomLevelChanged, 495 base::Bind(&AppMenu::ZoomView::OnZoomLevelChanged,
535 base::Unretained(this))); 496 base::Unretained(this)));
536 497
537 decrement_button_ = CreateButtonWithAccName( 498 decrement_button_ = CreateButtonWithAccName(
538 IDS_ZOOM_MINUS2, InMenuButtonBackground::LEFT_BUTTON, 499 IDS_ZOOM_MINUS2, InMenuButtonBackground::LEADING_BORDER,
539 decrement_index, IDS_ACCNAME_ZOOM_MINUS2); 500 decrement_index, IDS_ACCNAME_ZOOM_MINUS2);
540 501
541 zoom_label_ = new Label( 502 zoom_label_ = new Label(
542 l10n_util::GetStringFUTF16Int(IDS_ZOOM_PERCENT, 100)); 503 l10n_util::GetStringFUTF16Int(IDS_ZOOM_PERCENT, 100));
543 zoom_label_->SetAutoColorReadabilityEnabled(false); 504 zoom_label_->SetAutoColorReadabilityEnabled(false);
544 zoom_label_->SetHorizontalAlignment(gfx::ALIGN_RIGHT); 505 zoom_label_->SetHorizontalAlignment(gfx::ALIGN_RIGHT);
545 506
546 InMenuButtonBackground* center_bg = 507 InMenuButtonBackground* center_bg =
547 new InMenuButtonBackground(InMenuButtonBackground::RIGHT_BUTTON); 508 new InMenuButtonBackground(InMenuButtonBackground::NO_BORDER);
548 zoom_label_->set_background(center_bg); 509 zoom_label_->set_background(center_bg);
549 510
550 AddChildView(zoom_label_); 511 AddChildView(zoom_label_);
551 zoom_label_max_width_valid_ = false; 512 zoom_label_max_width_valid_ = false;
552 513
553 increment_button_ = CreateButtonWithAccName( 514 increment_button_ = CreateButtonWithAccName(
554 IDS_ZOOM_PLUS2, InMenuButtonBackground::RIGHT_BUTTON, 515 IDS_ZOOM_PLUS2, InMenuButtonBackground::NO_BORDER, increment_index,
555 increment_index, IDS_ACCNAME_ZOOM_PLUS2); 516 IDS_ACCNAME_ZOOM_PLUS2);
556
557 center_bg->SetOtherButtons(decrement_button_, increment_button_);
558 517
559 fullscreen_button_ = new FullscreenButton(this); 518 fullscreen_button_ = new FullscreenButton(this);
560 // all buttons on menu should must be a custom button in order for 519 // all buttons on menu should must be a custom button in order for
561 // the keyboard nativigation work. 520 // the keyboard navigation work.
562 DCHECK(CustomButton::AsCustomButton(fullscreen_button_)); 521 DCHECK(CustomButton::AsCustomButton(fullscreen_button_));
563 gfx::ImageSkia* full_screen_image = 522 gfx::ImageSkia* full_screen_image =
564 ui::ResourceBundle::GetSharedInstance().GetImageSkiaNamed( 523 ui::ResourceBundle::GetSharedInstance().GetImageSkiaNamed(
565 IDR_FULLSCREEN_MENU_BUTTON); 524 IDR_FULLSCREEN_MENU_BUTTON);
566 fullscreen_button_->SetImage(ImageButton::STATE_NORMAL, full_screen_image); 525 fullscreen_button_->SetImage(ImageButton::STATE_NORMAL, full_screen_image);
567 526
568 // Since |fullscreen_button_| will reside in a menu, make it ALWAYS 527 // Since |fullscreen_button_| will reside in a menu, make it ALWAYS
569 // focusable regardless of the platform. 528 // focusable regardless of the platform.
570 fullscreen_button_->SetFocusBehavior(FocusBehavior::ALWAYS); 529 fullscreen_button_->SetFocusBehavior(FocusBehavior::ALWAYS);
571 fullscreen_button_->set_tag(fullscreen_index); 530 fullscreen_button_->set_tag(fullscreen_index);
572 fullscreen_button_->SetImageAlignment( 531 fullscreen_button_->SetImageAlignment(
573 ImageButton::ALIGN_CENTER, ImageButton::ALIGN_MIDDLE); 532 ImageButton::ALIGN_CENTER, ImageButton::ALIGN_MIDDLE);
574 fullscreen_button_->set_background( 533 fullscreen_button_->set_background(
575 new InMenuButtonBackground(InMenuButtonBackground::SINGLE_BUTTON)); 534 new InMenuButtonBackground(InMenuButtonBackground::LEADING_BORDER));
576 fullscreen_button_->SetAccessibleName(GetAccessibleNameForAppMenuItem( 535 fullscreen_button_->SetAccessibleName(GetAccessibleNameForAppMenuItem(
577 menu_model, fullscreen_index, IDS_ACCNAME_FULLSCREEN)); 536 menu_model, fullscreen_index, IDS_ACCNAME_FULLSCREEN));
578 AddChildView(fullscreen_button_); 537 AddChildView(fullscreen_button_);
579 538
580 // Need to set a font list for the zoom label width calculations. 539 // Need to set a font list for the zoom label width calculations.
581 OnNativeThemeChanged(NULL); 540 OnNativeThemeChanged(NULL);
582 UpdateZoomControls(); 541 UpdateZoomControls();
583 } 542 }
584 543
585 ~ZoomView() override {} 544 ~ZoomView() override {}
(...skipping 716 matching lines...) Expand 10 before | Expand all | Expand 10 after
1302 0, 1261 0,
1303 BookmarkMenuDelegate::SHOW_PERMANENT_FOLDERS, 1262 BookmarkMenuDelegate::SHOW_PERMANENT_FOLDERS,
1304 BOOKMARK_LAUNCH_LOCATION_APP_MENU); 1263 BOOKMARK_LAUNCH_LOCATION_APP_MENU);
1305 } 1264 }
1306 1265
1307 int AppMenu::ModelIndexFromCommandId(int command_id) const { 1266 int AppMenu::ModelIndexFromCommandId(int command_id) const {
1308 CommandIDToEntry::const_iterator ix = command_id_to_entry_.find(command_id); 1267 CommandIDToEntry::const_iterator ix = command_id_to_entry_.find(command_id);
1309 DCHECK(ix != command_id_to_entry_.end()); 1268 DCHECK(ix != command_id_to_entry_.end());
1310 return ix->second.second; 1269 return ix->second.second;
1311 } 1270 }
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698