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

Side by Side Diff: chrome/browser/ui/views/wrench_menu.cc

Issue 10843060: views: Fix gesture event propagation in menus. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: self-nit Created 8 years, 4 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 | Annotate | Revision Log
« no previous file with comments | « no previous file | ui/views/controls/menu/menu_host_root_view.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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 "chrome/browser/ui/views/wrench_menu.h" 5 #include "chrome/browser/ui/views/wrench_menu.h"
6 6
7 #include <algorithm> 7 #include <algorithm>
8 #include <cmath> 8 #include <cmath>
9 #include <set> 9 #include <set>
10 10
(...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after
86 86
87 // For touch menu items we want to have this height in pixels. 87 // For touch menu items we want to have this height in pixels.
88 const int kTouchItemHeight = 47; 88 const int kTouchItemHeight = 47;
89 89
90 // Subclass of ImageButton whose preferred size includes the size of the border. 90 // Subclass of ImageButton whose preferred size includes the size of the border.
91 class FullscreenButton : public ImageButton { 91 class FullscreenButton : public ImageButton {
92 public: 92 public:
93 explicit FullscreenButton(views::ButtonListener* listener) 93 explicit FullscreenButton(views::ButtonListener* listener)
94 : ImageButton(listener) { } 94 : ImageButton(listener) { }
95 95
96 virtual gfx::Size GetPreferredSize() { 96 // Overridden from ImageButton.
97 virtual gfx::Size GetPreferredSize() OVERRIDE {
97 gfx::Size pref = ImageButton::GetPreferredSize(); 98 gfx::Size pref = ImageButton::GetPreferredSize();
98 gfx::Insets insets; 99 gfx::Insets insets;
99 if (border()) 100 if (border())
100 border()->GetInsets(&insets); 101 border()->GetInsets(&insets);
101 pref.Enlarge(insets.width(), insets.height()); 102 pref.Enlarge(insets.width(), insets.height());
102 return pref; 103 return pref;
103 } 104 }
104 105
105 private: 106 private:
106 DISALLOW_COPY_AND_ASSIGN(FullscreenButton); 107 DISALLOW_COPY_AND_ASSIGN(FullscreenButton);
107 }; 108 };
108 109
109 // Border for buttons contained in the menu. This is only used for getting the 110 // Border for buttons contained in the menu. This is only used for getting the
110 // insets, the actual painting is done in MenuButtonBackground. 111 // insets, the actual painting is done in MenuButtonBackground.
111 class MenuButtonBorder : public views::Border { 112 class MenuButtonBorder : public views::Border {
112 public: 113 public:
113 MenuButtonBorder() 114 MenuButtonBorder()
114 : horizontal_padding_(ui::GetDisplayLayout() == ui::LAYOUT_TOUCH ? 115 : horizontal_padding_(ui::GetDisplayLayout() == ui::LAYOUT_TOUCH ?
115 kHorizontalTouchPadding : 116 kHorizontalTouchPadding :
116 kHorizontalPadding) {} 117 kHorizontalPadding) {}
117 118
118 virtual void Paint(const View& view, gfx::Canvas* canvas) const { 119 // Overridden from views::Border.
120 virtual void Paint(const View& view, gfx::Canvas* canvas) const OVERRIDE {
119 // Painting of border is done in MenuButtonBackground. 121 // Painting of border is done in MenuButtonBackground.
120 } 122 }
121 123
122 virtual void GetInsets(gfx::Insets* insets) const { 124 virtual void GetInsets(gfx::Insets* insets) const OVERRIDE {
123 insets->Set(MenuConfig::instance().item_top_margin, 125 insets->Set(MenuConfig::instance().item_top_margin,
124 horizontal_padding_, 126 horizontal_padding_,
125 MenuConfig::instance().item_bottom_margin, 127 MenuConfig::instance().item_bottom_margin,
126 horizontal_padding_); 128 horizontal_padding_);
127 } 129 }
128 130
129 private: 131 private:
130 // The horizontal padding dependent on the layout. 132 // The horizontal padding dependent on the layout.
131 const int horizontal_padding_; 133 const int horizontal_padding_;
132 134
(...skipping 22 matching lines...) Expand all
155 void SetOtherButtons(CustomButton* left_button, CustomButton* right_button) { 157 void SetOtherButtons(CustomButton* left_button, CustomButton* right_button) {
156 if (base::i18n::IsRTL()) { 158 if (base::i18n::IsRTL()) {
157 left_button_ = right_button; 159 left_button_ = right_button;
158 right_button_ = left_button; 160 right_button_ = left_button;
159 } else { 161 } else {
160 left_button_ = left_button; 162 left_button_ = left_button;
161 right_button_ = right_button; 163 right_button_ = right_button;
162 } 164 }
163 } 165 }
164 166
165 virtual void Paint(gfx::Canvas* canvas, View* view) const { 167 // Overridden from views::Background.
168 virtual void Paint(gfx::Canvas* canvas, View* view) const OVERRIDE {
166 CustomButton::ButtonState state = 169 CustomButton::ButtonState state =
167 (view->GetClassName() == views::Label::kViewClassName) ? 170 (view->GetClassName() == views::Label::kViewClassName) ?
168 CustomButton::BS_NORMAL : static_cast<CustomButton*>(view)->state(); 171 CustomButton::BS_NORMAL : static_cast<CustomButton*>(view)->state();
169 int w = view->width(); 172 int w = view->width();
170 int h = view->height(); 173 int h = view->height();
171 // Windows is drawing its own separators and we cannot use the touch button 174 // Windows is drawing its own separators and we cannot use the touch button
172 // for that. 175 // for that.
173 #if !defined(OS_WIN) 176 #if !defined(OS_WIN)
174 if (ui::GetDisplayLayout() == ui::LAYOUT_TOUCH) { 177 if (ui::GetDisplayLayout() == ui::LAYOUT_TOUCH) {
175 // Normal buttons get a border drawn on the right side and the rest gets 178 // Normal buttons get a border drawn on the right side and the rest gets
(...skipping 111 matching lines...) Expand 10 before | Expand all | Expand 10 after
287 290
288 // A View subclass that forces SchedulePaint to paint all. Normally when the 291 // A View subclass that forces SchedulePaint to paint all. Normally when the
289 // mouse enters/exits a button the buttons invokes SchedulePaint. As part of the 292 // mouse enters/exits a button the buttons invokes SchedulePaint. As part of the
290 // button border (MenuButtonBackground) is rendered by the button to the 293 // button border (MenuButtonBackground) is rendered by the button to the
291 // left/right of it SchedulePaint on the the button may not be enough, so this 294 // left/right of it SchedulePaint on the the button may not be enough, so this
292 // forces a paint all. 295 // forces a paint all.
293 class ScheduleAllView : public views::View { 296 class ScheduleAllView : public views::View {
294 public: 297 public:
295 ScheduleAllView() {} 298 ScheduleAllView() {}
296 299
297 virtual void SchedulePaintInRect(const gfx::Rect& r) { 300 // Overridden from views::View.
301 virtual void SchedulePaintInRect(const gfx::Rect& r) OVERRIDE {
298 View::SchedulePaintInRect(gfx::Rect(0, 0, width(), height())); 302 View::SchedulePaintInRect(gfx::Rect(0, 0, width(), height()));
299 } 303 }
300 304
301 private: 305 private:
302 DISALLOW_COPY_AND_ASSIGN(ScheduleAllView); 306 DISALLOW_COPY_AND_ASSIGN(ScheduleAllView);
303 }; 307 };
304 308
305 string16 GetAccessibleNameForWrenchMenuItem( 309 string16 GetAccessibleNameForWrenchMenuItem(
306 MenuModel* model, int item_index, int accessible_string_id) { 310 MenuModel* model, int item_index, int accessible_string_id) {
307 string16 accessible_name = l10n_util::GetStringUTF16(accessible_string_id); 311 string16 accessible_name = l10n_util::GetStringUTF16(accessible_string_id);
(...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after
367 MenuModel* menu_model_; 371 MenuModel* menu_model_;
368 372
369 private: 373 private:
370 DISALLOW_COPY_AND_ASSIGN(WrenchMenuView); 374 DISALLOW_COPY_AND_ASSIGN(WrenchMenuView);
371 }; 375 };
372 376
373 class ButtonContainerMenuItemView : public MenuItemView { 377 class ButtonContainerMenuItemView : public MenuItemView {
374 public: 378 public:
375 // Constructor for use with button containing menu items which have a 379 // Constructor for use with button containing menu items which have a
376 // different height then . 380 // different height then .
377 explicit ButtonContainerMenuItemView(MenuItemView* parent, 381 ButtonContainerMenuItemView(MenuItemView* parent, int id, int height)
378 int id,
379 int height)
380 : MenuItemView(parent, id, MenuItemView::NORMAL), 382 : MenuItemView(parent, id, MenuItemView::NORMAL),
381 height_(height) { 383 height_(height) {
382 }; 384 };
383 385
384 gfx::Size GetChildPreferredSize() OVERRIDE { 386 // Overridden from MenuItemView.
387 virtual gfx::Size GetChildPreferredSize() OVERRIDE {
385 gfx::Size size = MenuItemView::GetChildPreferredSize(); 388 gfx::Size size = MenuItemView::GetChildPreferredSize();
386 // When there is a height override given, we need to deduct our spacing 389 // When there is a height override given, we need to deduct our spacing
387 // above and below to get to the correct height to return here for the 390 // above and below to get to the correct height to return here for the
388 // child item. 391 // child item.
389 int height = height_ - GetTopMargin() - GetBottomMargin(); 392 int height = height_ - GetTopMargin() - GetBottomMargin();
390 if (height > size.height()) 393 if (height > size.height())
391 size.set_height(height); 394 size.set_height(height);
392 return size; 395 return size;
393 } 396 }
394 397
395 private: 398 private:
396 int height_; 399 int height_;
397 400
398 DISALLOW_COPY_AND_ASSIGN(ButtonContainerMenuItemView); 401 DISALLOW_COPY_AND_ASSIGN(ButtonContainerMenuItemView);
399 }; 402 };
400 403
401 class TintedImageSource: public gfx::CanvasImageSource { 404 class TintedImageSource: public gfx::CanvasImageSource {
402 public: 405 public:
403 TintedImageSource(gfx::ImageSkia& image, SkColor tint_value) 406 TintedImageSource(gfx::ImageSkia& image, SkColor tint_value)
404 : CanvasImageSource(image.size(), false), 407 : CanvasImageSource(image.size(), false),
405 image_(image), 408 image_(image),
406 tint_value_(tint_value) { 409 tint_value_(tint_value) {
407 } 410 }
408 411
409 virtual ~TintedImageSource() { 412 virtual ~TintedImageSource() {
410 } 413 }
411 414
412 void Draw(gfx::Canvas* canvas) OVERRIDE { 415 // Overridden from gfx::CanvasImageSource.
416 virtual void Draw(gfx::Canvas* canvas) OVERRIDE {
413 canvas->DrawImageInt(image_, 0, 0); 417 canvas->DrawImageInt(image_, 0, 0);
414 SkPaint paint; 418 SkPaint paint;
415 // We leave the old alpha alone and add the new color multiplied 419 // We leave the old alpha alone and add the new color multiplied
416 // with the source alpha to the existing alpha. Thus: We brighten 420 // with the source alpha to the existing alpha. Thus: We brighten
417 // the image up - but only the non transparent pixels. 421 // the image up - but only the non transparent pixels.
418 paint.setXfermodeMode(SkXfermode::kDstATop_Mode); 422 paint.setXfermodeMode(SkXfermode::kDstATop_Mode);
419 paint.setColor(tint_value_); 423 paint.setColor(tint_value_);
420 canvas->sk_canvas()->drawPaint(paint); 424 canvas->sk_canvas()->drawPaint(paint);
421 } 425 }
422 426
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after
459 paste_index, 463 paste_index,
460 NULL); 464 NULL);
461 if (is_touch) { 465 if (is_touch) {
462 cut->SetEnabledColor(kTouchButtonText); 466 cut->SetEnabledColor(kTouchButtonText);
463 copy->SetEnabledColor(kTouchButtonText); 467 copy->SetEnabledColor(kTouchButtonText);
464 paste->SetEnabledColor(kTouchButtonText); 468 paste->SetEnabledColor(kTouchButtonText);
465 } 469 }
466 copy_background->SetOtherButtons(cut, paste); 470 copy_background->SetOtherButtons(cut, paste);
467 } 471 }
468 472
469 gfx::Size GetPreferredSize() { 473 // Overridden from View.
474 virtual gfx::Size GetPreferredSize() OVERRIDE {
470 // Returned height doesn't matter as MenuItemView forces everything to the 475 // Returned height doesn't matter as MenuItemView forces everything to the
471 // height of the menuitemview. 476 // height of the menuitemview.
472 return gfx::Size(GetMaxChildViewPreferredWidth() * child_count(), 0); 477 return gfx::Size(GetMaxChildViewPreferredWidth() * child_count(), 0);
473 } 478 }
474 479
475 void Layout() { 480 virtual void Layout() OVERRIDE {
476 // All buttons are given the same width. 481 // All buttons are given the same width.
477 int width = GetMaxChildViewPreferredWidth(); 482 int width = GetMaxChildViewPreferredWidth();
478 for (int i = 0; i < child_count(); ++i) 483 for (int i = 0; i < child_count(); ++i)
479 child_at(i)->SetBounds(i * width, 0, width, height()); 484 child_at(i)->SetBounds(i * width, 0, width, height());
480 } 485 }
481 486
482 // ButtonListener 487 // Overridden from ButtonListener.
483 virtual void ButtonPressed(views::Button* sender, const views::Event& event) { 488 virtual void ButtonPressed(views::Button* sender,
489 const views::Event& event) OVERRIDE {
484 menu_->CancelAndEvaluate(menu_model_, sender->tag()); 490 menu_->CancelAndEvaluate(menu_model_, sender->tag());
485 } 491 }
486 492
487 private: 493 private:
488 // Returns the max preferred width of all the children. 494 // Returns the max preferred width of all the children.
489 int GetMaxChildViewPreferredWidth() { 495 int GetMaxChildViewPreferredWidth() {
490 int width = 0; 496 int width = 0;
491 for (int i = 0; i < child_count(); ++i) 497 for (int i = 0; i < child_count(); ++i)
492 width = std::max(width, child_at(i)->GetPreferredSize().width()); 498 width = std::max(width, child_at(i)->GetPreferredSize().width());
493 return width; 499 return width;
(...skipping 95 matching lines...) Expand 10 before | Expand all | Expand 10 after
589 AddChildView(fullscreen_button_); 595 AddChildView(fullscreen_button_);
590 596
591 UpdateZoomControls(); 597 UpdateZoomControls();
592 598
593 registrar_.Add( 599 registrar_.Add(
594 this, content::NOTIFICATION_ZOOM_LEVEL_CHANGED, 600 this, content::NOTIFICATION_ZOOM_LEVEL_CHANGED,
595 content::Source<HostZoomMap>( 601 content::Source<HostZoomMap>(
596 HostZoomMap::GetForBrowserContext(menu->browser_->profile()))); 602 HostZoomMap::GetForBrowserContext(menu->browser_->profile())));
597 } 603 }
598 604
599 gfx::Size GetPreferredSize() { 605 // Overridden from View.
606 virtual gfx::Size GetPreferredSize() OVERRIDE {
600 // The increment/decrement button are forced to the same width. 607 // The increment/decrement button are forced to the same width.
601 int button_width = std::max(increment_button_->GetPreferredSize().width(), 608 int button_width = std::max(increment_button_->GetPreferredSize().width(),
602 decrement_button_->GetPreferredSize().width()); 609 decrement_button_->GetPreferredSize().width());
603 int zoom_padding = ui::GetDisplayLayout() == ui::LAYOUT_TOUCH ? 610 int zoom_padding = ui::GetDisplayLayout() == ui::LAYOUT_TOUCH ?
604 kTouchZoomPadding : kZoomPadding; 611 kTouchZoomPadding : kZoomPadding;
605 int fullscreen_width = fullscreen_button_->GetPreferredSize().width() + 612 int fullscreen_width = fullscreen_button_->GetPreferredSize().width() +
606 zoom_padding; 613 zoom_padding;
607 // Returned height doesn't matter as MenuItemView forces everything to the 614 // Returned height doesn't matter as MenuItemView forces everything to the
608 // height of the menuitemview. Note that we have overridden the height when 615 // height of the menuitemview. Note that we have overridden the height when
609 // constructing the menu. 616 // constructing the menu.
610 return gfx::Size(button_width + zoom_label_width_ + button_width + 617 return gfx::Size(button_width + zoom_label_width_ + button_width +
611 fullscreen_width, 0); 618 fullscreen_width, 0);
612 } 619 }
613 620
614 void Layout() { 621 virtual void Layout() OVERRIDE {
615 int x = 0; 622 int x = 0;
616 int button_width = std::max(increment_button_->GetPreferredSize().width(), 623 int button_width = std::max(increment_button_->GetPreferredSize().width(),
617 decrement_button_->GetPreferredSize().width()); 624 decrement_button_->GetPreferredSize().width());
618 gfx::Rect bounds(0, 0, button_width, height()); 625 gfx::Rect bounds(0, 0, button_width, height());
619 626
620 decrement_button_->SetBoundsRect(bounds); 627 decrement_button_->SetBoundsRect(bounds);
621 628
622 x += bounds.width(); 629 x += bounds.width();
623 bounds.set_x(x); 630 bounds.set_x(x);
624 bounds.set_width(zoom_label_width_); 631 bounds.set_width(zoom_label_width_);
625 zoom_label_->SetBoundsRect(bounds); 632 zoom_label_->SetBoundsRect(bounds);
626 633
627 x += bounds.width(); 634 x += bounds.width();
628 bounds.set_x(x); 635 bounds.set_x(x);
629 bounds.set_width(button_width); 636 bounds.set_width(button_width);
630 increment_button_->SetBoundsRect(bounds); 637 increment_button_->SetBoundsRect(bounds);
631 638
632 bool is_touch = ui::GetDisplayLayout() == ui::LAYOUT_TOUCH; 639 bool is_touch = ui::GetDisplayLayout() == ui::LAYOUT_TOUCH;
633 x += bounds.width() + (is_touch ? 0 : kZoomPadding); 640 x += bounds.width() + (is_touch ? 0 : kZoomPadding);
634 bounds.set_x(x); 641 bounds.set_x(x);
635 bounds.set_width(fullscreen_button_->GetPreferredSize().width() + 642 bounds.set_width(fullscreen_button_->GetPreferredSize().width() +
636 (is_touch ? kTouchZoomPadding : 0)); 643 (is_touch ? kTouchZoomPadding : 0));
637 fullscreen_button_->SetBoundsRect(bounds); 644 fullscreen_button_->SetBoundsRect(bounds);
638 } 645 }
639 646
640 // ButtonListener: 647 // Overridden from ButtonListener.
641 virtual void ButtonPressed(views::Button* sender, const views::Event& event) { 648 virtual void ButtonPressed(views::Button* sender,
649 const views::Event& event) OVERRIDE {
642 if (sender->tag() == fullscreen_index_) { 650 if (sender->tag() == fullscreen_index_) {
643 menu_->CancelAndEvaluate(menu_model_, sender->tag()); 651 menu_->CancelAndEvaluate(menu_model_, sender->tag());
644 } else { 652 } else {
645 // Zoom buttons don't close the menu. 653 // Zoom buttons don't close the menu.
646 menu_model_->ActivatedAt(sender->tag()); 654 menu_model_->ActivatedAt(sender->tag());
647 } 655 }
648 } 656 }
649 657
650 // content::NotificationObserver: 658 // Overridden from content::NotificationObserver.
651 virtual void Observe(int type, 659 virtual void Observe(int type,
652 const content::NotificationSource& source, 660 const content::NotificationSource& source,
653 const content::NotificationDetails& details) { 661 const content::NotificationDetails& details) OVERRIDE {
654 DCHECK_EQ(content::NOTIFICATION_ZOOM_LEVEL_CHANGED, type); 662 DCHECK_EQ(content::NOTIFICATION_ZOOM_LEVEL_CHANGED, type);
655 UpdateZoomControls(); 663 UpdateZoomControls();
656 } 664 }
657 665
658 private: 666 private:
659 void UpdateZoomControls() { 667 void UpdateZoomControls() {
660 int zoom = 100; 668 int zoom = 100;
661 // Don't override initial states of increment and decrement buttons when 669 // Don't override initial states of increment and decrement buttons when
662 // instant extended API is enabled and mode is NTP; they are properly 670 // instant extended API is enabled and mode is NTP; they are properly
663 // updated in ToolbarView::ModeChanged() via CommandUpdater, and queried 671 // updated in ToolbarView::ModeChanged() via CommandUpdater, and queried
(...skipping 441 matching lines...) Expand 10 before | Expand all | Expand 10 after
1105 bookmark_menu_delegate_.reset( 1113 bookmark_menu_delegate_.reset(
1106 new BookmarkMenuDelegate(browser_, 1114 new BookmarkMenuDelegate(browser_,
1107 browser_, 1115 browser_,
1108 parent, 1116 parent,
1109 first_bookmark_command_id_)); 1117 first_bookmark_command_id_));
1110 bookmark_menu_delegate_->Init( 1118 bookmark_menu_delegate_->Init(
1111 this, bookmark_menu_, model->bookmark_bar_node(), 0, 1119 this, bookmark_menu_, model->bookmark_bar_node(), 0,
1112 BookmarkMenuDelegate::SHOW_PERMANENT_FOLDERS, 1120 BookmarkMenuDelegate::SHOW_PERMANENT_FOLDERS,
1113 bookmark_utils::LAUNCH_WRENCH_MENU); 1121 bookmark_utils::LAUNCH_WRENCH_MENU);
1114 } 1122 }
OLDNEW
« no previous file with comments | « no previous file | ui/views/controls/menu/menu_host_root_view.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698