Chromium Code Reviews| Index: ui/views/controls/combobox/combobox.cc |
| diff --git a/ui/views/controls/combobox/combobox.cc b/ui/views/controls/combobox/combobox.cc |
| index 278b4c3bada9a75727e048a8048ef91c1d21e97d..1ae342435582aa359a3ec83c984f47abf6c21bb0 100644 |
| --- a/ui/views/controls/combobox/combobox.cc |
| +++ b/ui/views/controls/combobox/combobox.cc |
| @@ -46,6 +46,10 @@ namespace views { |
| namespace { |
| +// Action style shoulder padding widths |
| +const int kActionLeftPadding = 12; |
| +const int kActionRightPadding = 11; |
| + |
| // Menu border widths |
| const int kMenuBorderWidthLeft = 1; |
| const int kMenuBorderWidthTop = 1; |
| @@ -54,12 +58,6 @@ const int kMenuBorderWidthRight = 1; |
| // Limit how small a combobox can be. |
| const int kMinComboboxWidth = 25; |
| -// Size of the combobox arrow margins |
| -const int kDisclosureArrowLeftPadding = 7; |
| -const int kDisclosureArrowRightPadding = 7; |
| -const int kDisclosureArrowButtonLeftPadding = 11; |
| -const int kDisclosureArrowButtonRightPadding = 12; |
| - |
| // Define the id of the first item in the menu (since it needs to be > 0) |
| const int kFirstMenuItemId = 1000; |
| @@ -89,6 +87,22 @@ const int kFocusedPressedMenuButtonImages[] = |
| #undef MENU_IMAGE_GRID |
| +gfx::Rect PositionArrowWithinShoulder(const gfx::Rect& shoulder_bounds, |
|
sky
2016/05/17 16:49:21
similar comment about naming.
Elly Fong-Jones
2016/05/17 17:35:05
-> PositionArrowWithinContainer
|
| + const gfx::Size& arrow_size, |
| + Combobox::Style style) { |
| + gfx::Rect bounds(shoulder_bounds); |
| + if (style == Combobox::STYLE_ACTION) { |
| + // This positions the arrow horizontally. The later call to |
| + // ClampToCenteredSize will position it vertically without touching the |
| + // horizontal position. |
| + bounds.Inset(kActionLeftPadding, 0, kActionRightPadding, 0); |
| + DCHECK_EQ(bounds.width(), arrow_size.width()); |
| + } |
| + |
| + bounds.ClampToCenteredSize(arrow_size); |
| + return bounds; |
| +} |
| + |
| // The transparent button which holds a button state but is not rendered. |
| class TransparentButton : public CustomButton { |
| public: |
| @@ -343,9 +357,9 @@ class Combobox::ComboboxMenuModelAdapter : public ui::MenuModel, |
| //////////////////////////////////////////////////////////////////////////////// |
| // Combobox, public: |
| -Combobox::Combobox(ui::ComboboxModel* model) |
| +Combobox::Combobox(ui::ComboboxModel* model, Style style) |
| : model_(model), |
| - style_(STYLE_NORMAL), |
| + style_(style), |
| listener_(NULL), |
| selected_index_(model_->GetDefaultIndex()), |
| invalid_(false), |
| @@ -353,6 +367,9 @@ Combobox::Combobox(ui::ComboboxModel* model) |
| text_button_(new TransparentButton(this)), |
| arrow_button_(new TransparentButton(this)), |
| weak_ptr_factory_(this) { |
| + if (style_ == STYLE_ACTION) |
|
sky
2016/05/17 16:49:21
nit: move to initializer on 364.
Elly Fong-Jones
2016/05/17 17:35:05
Done.
|
| + selected_index_ = 0; |
| + |
| ModelChanged(); |
| #if defined(OS_MACOSX) |
| SetFocusBehavior(FocusBehavior::ACCESSIBLE_ONLY); |
| @@ -361,8 +378,10 @@ Combobox::Combobox(ui::ComboboxModel* model) |
| #endif |
| UpdateBorder(); |
| + arrow_image_ = PlatformStyle::CreateComboboxArrow(enabled(), style); |
| // set_background() takes ownership but takes a raw pointer. |
| - std::unique_ptr<Background> b = PlatformStyle::CreateComboboxBackground(); |
| + std::unique_ptr<Background> b = |
| + PlatformStyle::CreateComboboxBackground(GetShoulderWidth()); |
| set_background(b.release()); |
| // Initialize the button images. |
| @@ -404,19 +423,6 @@ const gfx::FontList& Combobox::GetFontList() { |
| return rb.GetFontListWithDelta(ui::kLabelFontSizeDelta); |
| } |
| -void Combobox::SetStyle(Style style) { |
| - if (style_ == style) |
| - return; |
| - |
| - style_ = style; |
| - if (style_ == STYLE_ACTION) |
| - selected_index_ = 0; |
| - |
| - UpdateBorder(); |
| - content_size_ = GetContentSize(); |
| - PreferredSizeChanged(); |
| -} |
| - |
| void Combobox::ModelChanged() { |
| // If the selection is no longer valid (or the model is empty), restore the |
| // default index. |
| @@ -465,12 +471,6 @@ void Combobox::SetInvalid(bool invalid) { |
| SchedulePaint(); |
| } |
| -int Combobox::GetArrowButtonWidth() const { |
| - return GetDisclosureArrowLeftPadding() + |
| - ArrowSize().width() + |
| - GetDisclosureArrowRightPadding(); |
| -} |
| - |
| void Combobox::Layout() { |
| PrefixDelegate::Layout(); |
| @@ -484,9 +484,7 @@ void Combobox::Layout() { |
| break; |
| } |
| case STYLE_ACTION: { |
| - arrow_button_width = GetDisclosureArrowLeftPadding() + |
| - ArrowSize().width() + |
| - GetDisclosureArrowRightPadding(); |
| + arrow_button_width = GetShoulderWidth(); |
| text_button_width = width() - arrow_button_width; |
| break; |
| } |
| @@ -497,6 +495,11 @@ void Combobox::Layout() { |
| arrow_button_->SetBounds(arrow_button_x, 0, arrow_button_width, height()); |
| } |
| +void Combobox::OnEnabledChanged() { |
| + PrefixDelegate::OnEnabledChanged(); |
| + arrow_image_ = PlatformStyle::CreateComboboxArrow(enabled(), style_); |
| +} |
| + |
| int Combobox::GetRowCount() { |
| return model()->GetItemCount(); |
| } |
| @@ -529,8 +532,7 @@ gfx::Size Combobox::GetPreferredSize() const { |
| Textfield::kTextPadding, |
| Textfield::kTextPadding); |
| int total_width = std::max(kMinComboboxWidth, content_size_.width()) + |
| - insets.width() + GetDisclosureArrowLeftPadding() + |
| - ArrowSize().width() + GetDisclosureArrowRightPadding(); |
| + insets.width() + GetShoulderWidth(); |
| return gfx::Size(total_width, content_size_.height() + insets.height()); |
| } |
| @@ -738,8 +740,7 @@ void Combobox::PaintText(gfx::Canvas* canvas) { |
| base::string16 text = model()->GetItemAt(selected_index_); |
| gfx::Size arrow_size = ArrowSize(); |
| - int disclosure_arrow_offset = width() - arrow_size.width() - |
| - GetDisclosureArrowLeftPadding() - GetDisclosureArrowRightPadding(); |
| + int disclosure_arrow_offset = width() - GetShoulderWidth(); |
| const gfx::FontList& font_list = Combobox::GetFontList(); |
| int text_width = gfx::GetStringWidth(text, font_list); |
| @@ -750,26 +751,18 @@ void Combobox::PaintText(gfx::Canvas* canvas) { |
| AdjustBoundsForRTLUI(&text_bounds); |
| canvas->DrawStringRect(text, font_list, text_color, text_bounds); |
| - int arrow_x = disclosure_arrow_offset + GetDisclosureArrowLeftPadding(); |
| - gfx::Rect arrow_bounds(arrow_x, |
| - height() / 2 - arrow_size.height() / 2, |
| - arrow_size.width(), |
| - arrow_size.height()); |
| + gfx::Rect arrow_bounds(disclosure_arrow_offset, 0, GetShoulderWidth(), |
| + height()); |
| + arrow_bounds = PositionArrowWithinShoulder(arrow_bounds, ArrowSize(), style_); |
| AdjustBoundsForRTLUI(&arrow_bounds); |
| - gfx::ImageSkia arrow_image = PlatformStyle::CreateComboboxArrow( |
| - enabled(), style_); |
| - canvas->DrawImageInt(arrow_image, arrow_bounds.x(), arrow_bounds.y()); |
| + canvas->DrawImageInt(arrow_image_, arrow_bounds.x(), arrow_bounds.y()); |
| } |
| void Combobox::PaintButtons(gfx::Canvas* canvas) { |
| DCHECK(style_ == STYLE_ACTION); |
| - gfx::ScopedCanvas scoped_canvas(canvas); |
| - if (base::i18n::IsRTL()) { |
| - canvas->Translate(gfx::Vector2d(width(), 0)); |
| - canvas->Scale(-1, 1); |
| - } |
| + gfx::ScopedRTLFlipCanvas scoped_canvas(canvas, bounds()); |
| bool focused = HasFocus(); |
| const std::vector<const gfx::ImageSkia*>& arrow_button_images = |
| @@ -877,30 +870,8 @@ void Combobox::OnPerformAction() { |
| selected_index_ = 0; |
| } |
| -int Combobox::GetDisclosureArrowLeftPadding() const { |
| - switch (style_) { |
| - case STYLE_NORMAL: |
| - return kDisclosureArrowLeftPadding; |
| - case STYLE_ACTION: |
| - return kDisclosureArrowButtonLeftPadding; |
| - } |
| - NOTREACHED(); |
| - return 0; |
| -} |
| - |
| -int Combobox::GetDisclosureArrowRightPadding() const { |
| - switch (style_) { |
| - case STYLE_NORMAL: |
| - return kDisclosureArrowRightPadding; |
| - case STYLE_ACTION: |
| - return kDisclosureArrowButtonRightPadding; |
| - } |
| - NOTREACHED(); |
| - return 0; |
| -} |
| - |
| gfx::Size Combobox::ArrowSize() const { |
| - return PlatformStyle::CreateComboboxArrow(enabled(), style_).size(); |
| + return arrow_image_.size(); |
| } |
| gfx::Size Combobox::GetContentSize() const { |
| @@ -926,4 +897,12 @@ PrefixSelector* Combobox::GetPrefixSelector() { |
| return selector_.get(); |
| } |
| +int Combobox::GetShoulderWidth() const { |
| + const int kNormalPadding = 7; |
| + int padding = style_ == STYLE_NORMAL ? |
| + kNormalPadding * 2 : |
| + kActionLeftPadding + kActionRightPadding; |
| + return ArrowSize().width() + padding; |
| +} |
| + |
| } // namespace views |