Chromium Code Reviews| OLD | NEW |
|---|---|
| 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/find_bar_view.h" | 5 #include "chrome/browser/ui/views/find_bar_view.h" |
| 6 | 6 |
| 7 #include <algorithm> | 7 #include <algorithm> |
| 8 | 8 |
| 9 #include "base/i18n/number_formatting.h" | 9 #include "base/i18n/number_formatting.h" |
| 10 #include "base/macros.h" | 10 #include "base/macros.h" |
| 11 #include "base/memory/ptr_util.h" | 11 #include "base/memory/ptr_util.h" |
| 12 #include "base/strings/string_util.h" | 12 #include "base/strings/string_util.h" |
| 13 #include "base/strings/utf_string_conversions.h" | |
| 14 #include "chrome/browser/profiles/profile.h" | 13 #include "chrome/browser/profiles/profile.h" |
| 15 #include "chrome/browser/themes/theme_properties.h" | 14 #include "chrome/browser/themes/theme_properties.h" |
| 16 #include "chrome/browser/ui/find_bar/find_bar_controller.h" | 15 #include "chrome/browser/ui/find_bar/find_bar_controller.h" |
| 17 #include "chrome/browser/ui/find_bar/find_bar_state.h" | 16 #include "chrome/browser/ui/find_bar/find_bar_state.h" |
| 18 #include "chrome/browser/ui/find_bar/find_bar_state_factory.h" | 17 #include "chrome/browser/ui/find_bar/find_bar_state_factory.h" |
| 19 #include "chrome/browser/ui/find_bar/find_notification_details.h" | 18 #include "chrome/browser/ui/find_bar/find_notification_details.h" |
| 20 #include "chrome/browser/ui/find_bar/find_tab_helper.h" | 19 #include "chrome/browser/ui/find_bar/find_tab_helper.h" |
| 21 #include "chrome/browser/ui/view_ids.h" | 20 #include "chrome/browser/ui/view_ids.h" |
| 22 #include "chrome/browser/ui/views/find_bar_host.h" | 21 #include "chrome/browser/ui/views/find_bar_host.h" |
| 23 #include "chrome/browser/ui/views/frame/browser_view.h" | 22 #include "chrome/browser/ui/views/frame/browser_view.h" |
| 24 #include "chrome/grit/generated_resources.h" | 23 #include "chrome/grit/generated_resources.h" |
| 25 #include "chrome/grit/theme_resources.h" | |
| 26 #include "components/strings/grit/components_strings.h" | 24 #include "components/strings/grit/components_strings.h" |
| 27 #include "third_party/skia/include/core/SkPaint.h" | |
| 28 #include "ui/base/ime/input_method.h" | 25 #include "ui/base/ime/input_method.h" |
| 29 #include "ui/base/ime/text_input_flags.h" | 26 #include "ui/base/ime/text_input_flags.h" |
| 30 #include "ui/base/l10n/l10n_util.h" | 27 #include "ui/base/l10n/l10n_util.h" |
| 31 #include "ui/base/material_design/material_design_controller.h" | |
| 32 #include "ui/base/resource/resource_bundle.h" | |
| 33 #include "ui/base/theme_provider.h" | 28 #include "ui/base/theme_provider.h" |
| 34 #include "ui/events/event.h" | 29 #include "ui/events/event.h" |
| 35 #include "ui/gfx/canvas.h" | 30 #include "ui/gfx/canvas.h" |
| 36 #include "ui/gfx/color_palette.h" | 31 #include "ui/gfx/color_palette.h" |
| 37 #include "ui/gfx/paint_vector_icon.h" | 32 #include "ui/gfx/paint_vector_icon.h" |
| 38 #include "ui/gfx/vector_icons_public.h" | 33 #include "ui/gfx/vector_icons_public.h" |
| 39 #include "ui/native_theme/common_theme.h" | |
| 40 #include "ui/native_theme/native_theme.h" | 34 #include "ui/native_theme/native_theme.h" |
| 41 #include "ui/resources/grit/ui_resources.h" | |
| 42 #include "ui/views/background.h" | 35 #include "ui/views/background.h" |
| 43 #include "ui/views/border.h" | 36 #include "ui/views/border.h" |
| 44 #include "ui/views/bubble/bubble_border.h" | 37 #include "ui/views/bubble/bubble_border.h" |
| 45 #include "ui/views/controls/button/image_button.h" | 38 #include "ui/views/controls/button/image_button.h" |
| 46 #include "ui/views/controls/button/vector_icon_button.h" | 39 #include "ui/views/controls/button/vector_icon_button.h" |
| 47 #include "ui/views/controls/label.h" | 40 #include "ui/views/controls/label.h" |
| 48 #include "ui/views/controls/separator.h" | 41 #include "ui/views/controls/separator.h" |
| 49 #include "ui/views/layout/box_layout.h" | 42 #include "ui/views/layout/box_layout.h" |
| 50 #include "ui/views/painter.h" | 43 #include "ui/views/painter.h" |
| 51 #include "ui/views/view_targeter.h" | 44 #include "ui/views/view_targeter.h" |
| 52 #include "ui/views/widget/widget.h" | 45 #include "ui/views/widget/widget.h" |
| 53 | 46 |
| 54 namespace { | 47 namespace { |
| 55 | 48 |
| 56 // The margins around the UI controls, derived from assets and design specs. | 49 // These layout constants are all in dp. |
| 57 const int kMarginLeftOfCloseButton = 3; | |
| 58 const int kMarginRightOfCloseButton = 7; | |
| 59 const int kMarginLeftOfMatchCountLabel = 3; | |
| 60 const int kMarginRightOfMatchCountLabel = 1; | |
| 61 const int kMarginLeftOfFindTextfield = 12; | |
| 62 const int kMarginVerticalFindTextfield = 6; | |
| 63 | |
| 64 // Constants for the MD layout, all in dp. | |
| 65 // The horizontal and vertical insets for the bar. | 50 // The horizontal and vertical insets for the bar. |
| 66 const int kInteriorPadding = 8; | 51 const int kInteriorPadding = 8; |
| 67 // Default spacing between child views. | 52 // Default spacing between child views. |
| 68 const int kInterChildSpacing = 4; | 53 const int kInterChildSpacing = 4; |
| 69 // Additional spacing around the separator. | 54 // Additional spacing around the separator. |
| 70 const int kSeparatorLeftSpacing = 12 - kInterChildSpacing; | 55 const int kSeparatorLeftSpacing = 12 - kInterChildSpacing; |
| 71 const int kSeparatorRightSpacing = 8 - kInterChildSpacing; | 56 const int kSeparatorRightSpacing = 8 - kInterChildSpacing; |
| 72 | 57 |
| 73 // The margins around the match count label (We add extra space so that the | |
| 74 // background highlight extends beyond just the text). | |
| 75 const int kMatchCountExtraWidth = 9; | |
| 76 | |
| 77 // Minimum width for the match count label. | |
| 78 const int kMatchCountMinWidth = 30; | |
| 79 | |
| 80 // The text color for the match count label. | |
| 81 const SkColor kTextColorMatchCount = SkColorSetRGB(178, 178, 178); | |
| 82 | |
| 83 // The text color for the match count label when no matches are found. | |
| 84 const SkColor kTextColorNoMatch = SK_ColorBLACK; | |
| 85 | |
| 86 // The background color of the match count label when results are found. | |
| 87 const SkColor kBackgroundColorMatch = SkColorSetARGB(0, 255, 255, 255); | |
| 88 | |
| 89 // The background color of the match count label when no results are found. | |
| 90 const SkColor kBackgroundColorNoMatch = SkColorSetRGB(255, 102, 102); | |
| 91 | |
| 92 // The default number of average characters that the text box will be. | 58 // The default number of average characters that the text box will be. |
| 93 const int kDefaultCharWidth = 43; | 59 const int kDefaultCharWidth = 30; |
| 94 const int kDefaultCharWidthMd = 30; | |
| 95 | 60 |
| 96 // The match count label is like a normal label, but can process events (which | 61 // The match count label is like a normal label, but can process events (which |
| 97 // makes it easier to forward events to the text input --- see | 62 // makes it easier to forward events to the text input --- see |
| 98 // FindBarView::TargetForRect). | 63 // FindBarView::TargetForRect). |
| 99 class MatchCountLabel : public views::Label { | 64 class MatchCountLabel : public views::Label { |
| 100 public: | 65 public: |
| 101 MatchCountLabel() {} | 66 MatchCountLabel() {} |
| 102 ~MatchCountLabel() override {} | 67 ~MatchCountLabel() override {} |
| 103 | 68 |
| 104 // views::Label overrides: | 69 // views::Label overrides: |
| 105 bool CanProcessEventsWithinSubtree() const override { return true; } | 70 bool CanProcessEventsWithinSubtree() const override { return true; } |
| 106 | 71 |
| 107 gfx::Size GetPreferredSize() const override { | 72 gfx::Size GetPreferredSize() const override { |
| 108 // We need to return at least 1dip so that box layout adds padding on either | 73 // We need to return at least 1dip so that box layout adds padding on either |
| 109 // side (otherwise there will be a jump when our size changes between empty | 74 // side (otherwise there will be a jump when our size changes between empty |
| 110 // and non-empty). | 75 // and non-empty). |
| 111 gfx::Size size = views::Label::GetPreferredSize(); | 76 gfx::Size size = views::Label::GetPreferredSize(); |
| 112 size.set_width(std::max(1, size.width())); | 77 size.set_width(std::max(1, size.width())); |
| 113 return size; | 78 return size; |
| 114 } | 79 } |
| 115 | 80 |
| 116 private: | 81 private: |
| 117 DISALLOW_COPY_AND_ASSIGN(MatchCountLabel); | 82 DISALLOW_COPY_AND_ASSIGN(MatchCountLabel); |
| 118 }; | 83 }; |
| 119 | 84 |
| 85 // We use a hidden view to grab mouse clicks and bring focus to the find | |
| 86 // text box. This is because although the find text box may look like it | |
| 87 // extends all the way to the find button, it only goes as far as to the | |
| 88 // match_count label. The user, however, expects being able to click anywhere | |
| 89 // inside what looks like the find text box (including on or around the | |
| 90 // match_count label) and have focus brought to the find box. | |
| 91 class FocusForwarderView : public views::View { | |
| 92 public: | |
| 93 explicit FocusForwarderView( | |
| 94 views::Textfield* view_to_focus_on_mousedown) | |
| 95 : view_to_focus_on_mousedown_(view_to_focus_on_mousedown) {} | |
| 96 | |
| 97 private: | |
| 98 bool OnMousePressed(const ui::MouseEvent& event) override { | |
| 99 if (view_to_focus_on_mousedown_) | |
| 100 view_to_focus_on_mousedown_->RequestFocus(); | |
| 101 return true; | |
| 102 } | |
| 103 | |
| 104 views::Textfield* view_to_focus_on_mousedown_; | |
| 105 | |
| 106 DISALLOW_COPY_AND_ASSIGN(FocusForwarderView); | |
| 107 }; | |
| 108 | |
| 120 } // namespace | 109 } // namespace |
| 121 | 110 |
| 122 //////////////////////////////////////////////////////////////////////////////// | 111 //////////////////////////////////////////////////////////////////////////////// |
| 123 // FindBarView, public: | 112 // FindBarView, public: |
| 124 | 113 |
| 125 FindBarView::FindBarView(FindBarHost* host) | 114 FindBarView::FindBarView(FindBarHost* host) |
| 126 : DropdownBarView(host), | 115 : DropdownBarView(host), |
| 127 find_text_(nullptr), | 116 find_text_(new views::Textfield), |
| 128 match_count_text_(nullptr), | 117 match_count_text_(new MatchCountLabel()), |
| 129 focus_forwarder_view_(nullptr), | 118 focus_forwarder_view_(new FocusForwarderView(find_text_)), |
| 130 separator_(nullptr), | 119 separator_(new views::Separator(views::Separator::VERTICAL)), |
| 131 find_previous_button_(nullptr), | 120 find_previous_button_(new views::VectorIconButton(this)), |
| 132 find_next_button_(nullptr), | 121 find_next_button_(new views::VectorIconButton(this)), |
| 133 close_button_(nullptr) { | 122 close_button_(new views::VectorIconButton(this)) { |
| 134 find_text_ = new views::Textfield; | |
| 135 find_text_->set_id(VIEW_ID_FIND_IN_PAGE_TEXT_FIELD); | 123 find_text_->set_id(VIEW_ID_FIND_IN_PAGE_TEXT_FIELD); |
| 136 find_text_->set_default_width_in_chars( | 124 find_text_->set_default_width_in_chars(kDefaultCharWidth); |
| 137 ui::MaterialDesignController::IsModeMaterial() ? kDefaultCharWidthMd | |
| 138 : kDefaultCharWidth); | |
| 139 find_text_->set_controller(this); | 125 find_text_->set_controller(this); |
| 140 find_text_->SetAccessibleName(l10n_util::GetStringUTF16(IDS_ACCNAME_FIND)); | 126 find_text_->SetAccessibleName(l10n_util::GetStringUTF16(IDS_ACCNAME_FIND)); |
| 141 find_text_->SetTextInputFlags(ui::TEXT_INPUT_FLAG_AUTOCORRECT_OFF); | 127 find_text_->SetTextInputFlags(ui::TEXT_INPUT_FLAG_AUTOCORRECT_OFF); |
| 142 AddChildView(find_text_); | 128 AddChildView(find_text_); |
| 143 | 129 |
| 144 if (ui::MaterialDesignController::IsModeMaterial()) { | 130 find_previous_button_->SetIcon(gfx::VectorIconId::FIND_PREV); |
| 145 views::VectorIconButton* find_previous = new views::VectorIconButton(this); | 131 find_next_button_->SetIcon(gfx::VectorIconId::FIND_NEXT); |
| 146 find_previous->SetIcon(gfx::VectorIconId::FIND_PREV); | 132 close_button_->SetIcon(gfx::VectorIconId::BAR_CLOSE); |
| 147 views::VectorIconButton* find_next = new views::VectorIconButton(this); | |
| 148 find_next->SetIcon(gfx::VectorIconId::FIND_NEXT); | |
| 149 views::VectorIconButton* close = new views::VectorIconButton(this); | |
| 150 close->SetIcon(gfx::VectorIconId::BAR_CLOSE); | |
| 151 | |
| 152 find_previous_button_ = find_previous; | |
| 153 find_next_button_ = find_next; | |
| 154 close_button_ = close; | |
| 155 } else { | |
| 156 find_previous_button_ = new views::ImageButton(this); | |
| 157 find_next_button_ = new views::ImageButton(this); | |
| 158 close_button_ = new views::ImageButton(this); | |
| 159 } | |
| 160 | 133 |
| 161 find_previous_button_->set_id(VIEW_ID_FIND_IN_PAGE_PREVIOUS_BUTTON); | 134 find_previous_button_->set_id(VIEW_ID_FIND_IN_PAGE_PREVIOUS_BUTTON); |
| 162 find_previous_button_->SetFocusForPlatform(); | 135 find_previous_button_->SetFocusForPlatform(); |
| 163 find_previous_button_->SetTooltipText( | 136 find_previous_button_->SetTooltipText( |
| 164 l10n_util::GetStringUTF16(IDS_FIND_IN_PAGE_PREVIOUS_TOOLTIP)); | 137 l10n_util::GetStringUTF16(IDS_FIND_IN_PAGE_PREVIOUS_TOOLTIP)); |
| 165 find_previous_button_->SetAccessibleName( | 138 find_previous_button_->SetAccessibleName( |
| 166 l10n_util::GetStringUTF16(IDS_ACCNAME_PREVIOUS)); | 139 l10n_util::GetStringUTF16(IDS_ACCNAME_PREVIOUS)); |
| 167 AddChildView(find_previous_button_); | 140 AddChildView(find_previous_button_); |
| 168 | 141 |
| 169 find_next_button_->set_id(VIEW_ID_FIND_IN_PAGE_NEXT_BUTTON); | 142 find_next_button_->set_id(VIEW_ID_FIND_IN_PAGE_NEXT_BUTTON); |
| 170 find_next_button_->SetFocusForPlatform(); | 143 find_next_button_->SetFocusForPlatform(); |
| 171 find_next_button_->SetTooltipText( | 144 find_next_button_->SetTooltipText( |
| 172 l10n_util::GetStringUTF16(IDS_FIND_IN_PAGE_NEXT_TOOLTIP)); | 145 l10n_util::GetStringUTF16(IDS_FIND_IN_PAGE_NEXT_TOOLTIP)); |
| 173 find_next_button_->SetAccessibleName( | 146 find_next_button_->SetAccessibleName( |
| 174 l10n_util::GetStringUTF16(IDS_ACCNAME_NEXT)); | 147 l10n_util::GetStringUTF16(IDS_ACCNAME_NEXT)); |
| 175 AddChildView(find_next_button_); | 148 AddChildView(find_next_button_); |
| 176 | 149 |
| 177 close_button_->set_id(VIEW_ID_FIND_IN_PAGE_CLOSE_BUTTON); | 150 close_button_->set_id(VIEW_ID_FIND_IN_PAGE_CLOSE_BUTTON); |
| 178 close_button_->SetFocusForPlatform(); | 151 close_button_->SetFocusForPlatform(); |
| 179 close_button_->SetTooltipText( | 152 close_button_->SetTooltipText( |
| 180 l10n_util::GetStringUTF16(IDS_FIND_IN_PAGE_CLOSE_TOOLTIP)); | 153 l10n_util::GetStringUTF16(IDS_FIND_IN_PAGE_CLOSE_TOOLTIP)); |
| 181 close_button_->SetAccessibleName( | 154 close_button_->SetAccessibleName( |
| 182 l10n_util::GetStringUTF16(IDS_ACCNAME_CLOSE)); | 155 l10n_util::GetStringUTF16(IDS_ACCNAME_CLOSE)); |
| 183 close_button_->SetAnimationDuration(0); | 156 close_button_->SetAnimationDuration(0); |
| 184 AddChildView(close_button_); | 157 AddChildView(close_button_); |
| 185 | 158 |
| 186 // Create a focus forwarder view which sends focus to find_text_. | 159 // Create a focus forwarder view which sends focus to find_text_. |
|
Peter Kasting
2016/09/20 20:34:02
Nit: Since this no longer creates the view, the co
Evan Stade
2016/09/21 17:06:36
Done.
| |
| 187 focus_forwarder_view_ = new FocusForwarderView(find_text_); | |
| 188 AddChildView(focus_forwarder_view_); | 160 AddChildView(focus_forwarder_view_); |
| 189 | 161 |
| 190 EnableCanvasFlippingForRTLUI(true); | 162 EnableCanvasFlippingForRTLUI(true); |
| 191 | 163 |
| 192 if (ui::MaterialDesignController::IsModeMaterial()) | 164 // The background color is not used since there's no arrow. |
| 193 InitViewsForMaterial(); | 165 SetBorder(base::MakeUnique<views::BubbleBorder>( |
| 194 else | 166 views::BubbleBorder::NONE, views::BubbleBorder::SMALL_SHADOW, |
| 195 InitViewsForNonMaterial(); | 167 gfx::kPlaceholderColor)); |
| 168 | |
| 169 match_count_text_->SetEventTargeter( | |
| 170 base::MakeUnique<views::ViewTargeter>(this)); | |
| 171 AddChildViewAt(match_count_text_, 1); | |
| 172 | |
| 173 separator_->SetBorder(views::Border::CreateEmptyBorder( | |
| 174 0, kSeparatorLeftSpacing, 0, kSeparatorRightSpacing)); | |
| 175 AddChildViewAt(separator_, 2); | |
| 176 | |
| 177 find_text_->SetBorder(views::Border::NullBorder()); | |
| 178 | |
| 179 views::BoxLayout* manager = | |
| 180 new views::BoxLayout(views::BoxLayout::kHorizontal, kInteriorPadding, | |
| 181 kInteriorPadding, kInterChildSpacing); | |
| 182 SetLayoutManager(manager); | |
| 183 manager->SetFlexForView(find_text_, 1); | |
| 196 } | 184 } |
| 197 | 185 |
| 198 FindBarView::~FindBarView() { | 186 FindBarView::~FindBarView() { |
| 199 } | 187 } |
| 200 | 188 |
| 201 void FindBarView::SetFindTextAndSelectedRange( | 189 void FindBarView::SetFindTextAndSelectedRange( |
| 202 const base::string16& find_text, | 190 const base::string16& find_text, |
| 203 const gfx::Range& selected_range) { | 191 const gfx::Range& selected_range) { |
| 204 find_text_->SetText(find_text); | 192 find_text_->SetText(find_text); |
| 205 find_text_->SelectRange(selected_range); | 193 find_text_->SelectRange(selected_range); |
| (...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 265 void FindBarView::SetFocusAndSelection(bool select_all) { | 253 void FindBarView::SetFocusAndSelection(bool select_all) { |
| 266 find_text_->RequestFocus(); | 254 find_text_->RequestFocus(); |
| 267 GetWidget()->GetInputMethod()->ShowImeIfNeeded(); | 255 GetWidget()->GetInputMethod()->ShowImeIfNeeded(); |
| 268 if (select_all && !find_text_->text().empty()) | 256 if (select_all && !find_text_->text().empty()) |
| 269 find_text_->SelectAll(true); | 257 find_text_->SelectAll(true); |
| 270 } | 258 } |
| 271 | 259 |
| 272 /////////////////////////////////////////////////////////////////////////////// | 260 /////////////////////////////////////////////////////////////////////////////// |
| 273 // FindBarView, views::View overrides: | 261 // FindBarView, views::View overrides: |
| 274 | 262 |
| 275 void FindBarView::OnPaint(gfx::Canvas* canvas) { | |
| 276 if (ui::MaterialDesignController::IsModeMaterial()) | |
| 277 return views::View::OnPaint(canvas); | |
| 278 | |
| 279 // Paint drop down bar border and background. | |
| 280 DropdownBarView::OnPaint(canvas); | |
| 281 | |
| 282 // Paint the background and border for the textfield. | |
| 283 const int find_text_x = kMarginLeftOfFindTextfield / 2; | |
| 284 const gfx::Rect text_bounds(find_text_x, find_next_button_->y(), | |
| 285 find_next_button_->bounds().right() - find_text_x, | |
| 286 find_next_button_->height()); | |
| 287 const int kBorderCornerRadius = 2; | |
| 288 gfx::Rect background_bounds = text_bounds; | |
| 289 background_bounds.Inset(kBorderCornerRadius, kBorderCornerRadius); | |
| 290 SkPaint paint; | |
| 291 paint.setStyle(SkPaint::kFill_Style); | |
| 292 paint.setColor(find_text_->GetBackgroundColor()); | |
| 293 canvas->DrawRoundRect(background_bounds, kBorderCornerRadius, paint); | |
| 294 canvas->Save(); | |
| 295 canvas->ClipRect(gfx::Rect(0, 0, find_previous_button_->x(), height())); | |
| 296 views::Painter::PaintPainterAt(canvas, find_text_border_.get(), text_bounds); | |
| 297 canvas->Restore(); | |
| 298 | |
| 299 // Draw the background of the match text. We want to make sure the red | |
| 300 // "no-match" background almost completely fills up the amount of vertical | |
| 301 // space within the text box. We therefore fix the size relative to the button | |
| 302 // heights. We use the FindPrev button, which has a 1px outer whitespace | |
| 303 // margin, 1px border and we want to appear 1px below the border line so we | |
| 304 // subtract 3 for top and 3 for bottom. | |
| 305 gfx::Rect match_count_background_bounds(match_count_text_->bounds()); | |
| 306 match_count_background_bounds.set_height( | |
| 307 find_previous_button_->height() - 6); // Subtract 3px x 2. | |
| 308 match_count_background_bounds.set_y( | |
| 309 (height() - match_count_background_bounds.height()) / 2); | |
| 310 canvas->FillRect(match_count_background_bounds, | |
| 311 match_count_text_->background_color()); | |
| 312 } | |
| 313 | |
| 314 void FindBarView::OnPaintBackground(gfx::Canvas* canvas) { | 263 void FindBarView::OnPaintBackground(gfx::Canvas* canvas) { |
| 315 if (!ui::MaterialDesignController::IsModeMaterial()) | |
| 316 return views::View::OnPaintBackground(canvas); | |
| 317 | |
| 318 // Draw within the lines. | 264 // Draw within the lines. |
| 319 canvas->Save(); | 265 canvas->Save(); |
| 320 gfx::Rect bounds = GetLocalBounds(); | 266 gfx::Rect bounds = GetLocalBounds(); |
| 321 bounds.Inset(border()->GetInsets()); | 267 bounds.Inset(border()->GetInsets()); |
| 322 canvas->ClipRect(bounds); | 268 canvas->ClipRect(bounds); |
| 323 views::View::OnPaintBackground(canvas); | 269 views::View::OnPaintBackground(canvas); |
| 324 canvas->Restore(); | 270 canvas->Restore(); |
| 325 } | 271 } |
| 326 | 272 |
| 327 void FindBarView::Layout() { | 273 void FindBarView::Layout() { |
| 328 if (ui::MaterialDesignController::IsModeMaterial()) { | 274 views::View::Layout(); |
| 329 views::View::Layout(); | |
| 330 } else { | |
| 331 int panel_width = GetPreferredSize().width(); | |
| 332 | |
| 333 // Stay within view bounds. | |
| 334 int view_width = width(); | |
| 335 if (view_width && view_width < panel_width) | |
| 336 panel_width = view_width; | |
| 337 | |
| 338 // Set the color. | |
| 339 OnThemeChanged(); | |
| 340 | |
| 341 // First we position the close button on the far right. | |
| 342 close_button_->SizeToPreferredSize(); | |
| 343 close_button_->SetPosition(gfx::Point( | |
| 344 panel_width - close_button_->width() - kMarginRightOfCloseButton, | |
| 345 (height() - close_button_->height()) / 2)); | |
| 346 | |
| 347 // Then, the next button to the left of the close button. | |
| 348 find_next_button_->SizeToPreferredSize(); | |
| 349 find_next_button_->SetPosition( | |
| 350 gfx::Point(close_button_->x() - find_next_button_->width() - | |
| 351 kMarginLeftOfCloseButton, | |
| 352 (height() - find_next_button_->height()) / 2)); | |
| 353 | |
| 354 // Then, the previous button to the left of the next button. | |
| 355 find_previous_button_->SizeToPreferredSize(); | |
| 356 find_previous_button_->SetPosition(gfx::Point( | |
| 357 find_next_button_->x() - find_previous_button_->width(), | |
| 358 (height() - find_previous_button_->height()) / 2)); | |
| 359 | |
| 360 // Then the label showing the match count number. | |
| 361 gfx::Size sz = match_count_text_->GetPreferredSize(); | |
| 362 // We extend the label bounds a bit to give the background highlighting a | |
| 363 // bit of breathing room (margins around the text). | |
| 364 sz.Enlarge(kMatchCountExtraWidth, 0); | |
| 365 sz.SetToMax(gfx::Size(kMatchCountMinWidth, 0)); | |
| 366 const int match_count_x = | |
| 367 find_previous_button_->x() - kMarginRightOfMatchCountLabel - sz.width(); | |
| 368 const int find_text_y = kMarginVerticalFindTextfield; | |
| 369 const gfx::Insets find_text_insets(find_text_->GetInsets()); | |
| 370 match_count_text_->SetBounds(match_count_x, | |
| 371 find_text_y - find_text_insets.top() + | |
| 372 find_text_->GetBaseline() - | |
| 373 match_count_text_->GetBaseline(), | |
| 374 sz.width(), sz.height()); | |
| 375 | |
| 376 // Fill the remaining width and available height with the textfield. | |
| 377 const int left_margin = | |
| 378 kMarginLeftOfFindTextfield - find_text_insets.left(); | |
| 379 const int find_text_width = std::max(0, match_count_x - left_margin - | |
| 380 kMarginLeftOfMatchCountLabel + find_text_insets.right()); | |
| 381 find_text_->SetBounds(left_margin, find_text_y, find_text_width, | |
| 382 height() - 2 * kMarginVerticalFindTextfield); | |
| 383 } | |
| 384 | 275 |
| 385 // The focus forwarder view is a hidden view that should cover the area | 276 // The focus forwarder view is a hidden view that should cover the area |
| 386 // between the find text box and the find button so that when the user clicks | 277 // between the find text box and the find button so that when the user clicks |
| 387 // in that area we focus on the find text box. | 278 // in that area we focus on the find text box. |
| 388 const int find_text_edge = find_text_->x() + find_text_->width(); | 279 const int find_text_edge = find_text_->x() + find_text_->width(); |
| 389 focus_forwarder_view_->SetBounds( | 280 focus_forwarder_view_->SetBounds( |
| 390 find_text_edge, find_previous_button_->y(), | 281 find_text_edge, find_previous_button_->y(), |
| 391 find_previous_button_->x() - find_text_edge, | 282 find_previous_button_->x() - find_text_edge, |
| 392 find_previous_button_->height()); | 283 find_previous_button_->height()); |
| 393 } | 284 } |
| 394 | 285 |
| 395 gfx::Size FindBarView::GetPreferredSize() const { | 286 gfx::Size FindBarView::GetPreferredSize() const { |
| 396 if (ui::MaterialDesignController::IsModeMaterial()) { | 287 gfx::Size size = views::View::GetPreferredSize(); |
| 397 gfx::Size size = views::View::GetPreferredSize(); | 288 // Ignore the preferred size for the match count label, and just let it take |
| 398 // Ignore the preferred size for the match count label, and just let it take | 289 // up part of the space for the input textfield. This prevents the overall |
| 399 // up part of the space for the input textfield. This prevents the overall | 290 // width from changing every time the match count text changes. |
| 400 // width from changing every time the match count text changes. | 291 size.set_width(size.width() - match_count_text_->GetPreferredSize().width()); |
| 401 size.set_width(size.width() - | 292 return size; |
| 402 match_count_text_->GetPreferredSize().width()); | |
| 403 return size; | |
| 404 } | |
| 405 | |
| 406 gfx::Size prefsize = find_text_->GetPreferredSize(); | |
| 407 prefsize.set_height(preferred_height_); | |
| 408 | |
| 409 // Add up all the preferred sizes and margins of the rest of the controls. | |
| 410 prefsize.Enlarge(kMarginLeftOfCloseButton + kMarginRightOfCloseButton + | |
| 411 kMarginLeftOfFindTextfield - | |
| 412 find_text_->GetInsets().width(), | |
| 413 0); | |
| 414 prefsize.Enlarge(find_previous_button_->GetPreferredSize().width(), 0); | |
| 415 prefsize.Enlarge(find_next_button_->GetPreferredSize().width(), 0); | |
| 416 prefsize.Enlarge(close_button_->GetPreferredSize().width(), 0); | |
| 417 return prefsize; | |
| 418 } | 293 } |
| 419 | 294 |
| 420 //////////////////////////////////////////////////////////////////////////////// | 295 //////////////////////////////////////////////////////////////////////////////// |
| 421 // FindBarView, views::VectorIconButtonDelegate implementation: | 296 // FindBarView, views::VectorIconButtonDelegate implementation: |
| 422 | 297 |
| 423 void FindBarView::ButtonPressed( | 298 void FindBarView::ButtonPressed( |
| 424 views::Button* sender, const ui::Event& event) { | 299 views::Button* sender, const ui::Event& event) { |
| 425 switch (sender->id()) { | 300 switch (sender->id()) { |
| 426 case VIEW_ID_FIND_IN_PAGE_PREVIOUS_BUTTON: | 301 case VIEW_ID_FIND_IN_PAGE_PREVIOUS_BUTTON: |
| 427 case VIEW_ID_FIND_IN_PAGE_NEXT_BUTTON: | 302 case VIEW_ID_FIND_IN_PAGE_NEXT_BUTTON: |
| (...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 493 // a paste operation, even if the pasted text is the same as before. | 368 // a paste operation, even if the pasted text is the same as before. |
| 494 // See http://crbug.com/79002 | 369 // See http://crbug.com/79002 |
| 495 last_searched_text_.clear(); | 370 last_searched_text_.clear(); |
| 496 } | 371 } |
| 497 | 372 |
| 498 views::View* FindBarView::TargetForRect(View* root, const gfx::Rect& rect) { | 373 views::View* FindBarView::TargetForRect(View* root, const gfx::Rect& rect) { |
| 499 DCHECK_EQ(match_count_text_, root); | 374 DCHECK_EQ(match_count_text_, root); |
| 500 return find_text_; | 375 return find_text_; |
| 501 } | 376 } |
| 502 | 377 |
| 503 void FindBarView::InitViewsForNonMaterial() { | |
| 504 match_count_text_ = new views::Label(); | |
| 505 AddChildView(match_count_text_); | |
| 506 | |
| 507 // The find bar textfield has a background image instead of a border. | |
| 508 find_text_->SetBorder(views::Border::NullBorder()); | |
| 509 | |
| 510 ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance(); | |
| 511 find_previous_button_->SetImage(views::CustomButton::STATE_NORMAL, | |
| 512 rb.GetImageSkiaNamed(IDR_FINDINPAGE_PREV)); | |
| 513 find_previous_button_->SetImage(views::CustomButton::STATE_HOVERED, | |
| 514 rb.GetImageSkiaNamed(IDR_FINDINPAGE_PREV_H)); | |
| 515 find_previous_button_->SetImage(views::CustomButton::STATE_PRESSED, | |
| 516 rb.GetImageSkiaNamed(IDR_FINDINPAGE_PREV_P)); | |
| 517 find_previous_button_->SetImage(views::CustomButton::STATE_DISABLED, | |
| 518 rb.GetImageSkiaNamed(IDR_FINDINPAGE_PREV_D)); | |
| 519 | |
| 520 find_next_button_->SetImage(views::CustomButton::STATE_NORMAL, | |
| 521 rb.GetImageSkiaNamed(IDR_FINDINPAGE_NEXT)); | |
| 522 find_next_button_->SetImage(views::CustomButton::STATE_HOVERED, | |
| 523 rb.GetImageSkiaNamed(IDR_FINDINPAGE_NEXT_H)); | |
| 524 find_next_button_->SetImage(views::CustomButton::STATE_PRESSED, | |
| 525 rb.GetImageSkiaNamed(IDR_FINDINPAGE_NEXT_P)); | |
| 526 find_next_button_->SetImage(views::CustomButton::STATE_DISABLED, | |
| 527 rb.GetImageSkiaNamed(IDR_FINDINPAGE_NEXT_D)); | |
| 528 | |
| 529 close_button_->SetImage(views::CustomButton::STATE_NORMAL, | |
| 530 rb.GetImageSkiaNamed(IDR_CLOSE_1)); | |
| 531 close_button_->SetImage(views::CustomButton::STATE_HOVERED, | |
| 532 rb.GetImageSkiaNamed(IDR_CLOSE_1_H)); | |
| 533 close_button_->SetImage(views::CustomButton::STATE_PRESSED, | |
| 534 rb.GetImageSkiaNamed(IDR_CLOSE_1_P)); | |
| 535 | |
| 536 SetBackground(rb.GetImageSkiaNamed(IDR_FIND_DLG_LEFT_BACKGROUND), | |
| 537 rb.GetImageSkiaNamed(IDR_FIND_DLG_RIGHT_BACKGROUND)); | |
| 538 SetBorderFromIds(IDR_FIND_DIALOG_LEFT, IDR_FIND_DIALOG_MIDDLE, | |
| 539 IDR_FIND_DIALOG_RIGHT); | |
| 540 | |
| 541 preferred_height_ = rb.GetImageSkiaNamed(IDR_FIND_DIALOG_MIDDLE)->height(); | |
| 542 | |
| 543 static const int kImages[] = IMAGE_GRID(IDR_TEXTFIELD); | |
| 544 find_text_border_.reset(views::Painter::CreateImageGridPainter(kImages)); | |
| 545 } | |
| 546 | |
| 547 void FindBarView::InitViewsForMaterial() { | |
| 548 // The background color is not used since there's no arrow. | |
| 549 SetBorder(base::MakeUnique<views::BubbleBorder>( | |
| 550 views::BubbleBorder::NONE, views::BubbleBorder::SMALL_SHADOW, | |
| 551 SK_ColorGREEN)); | |
| 552 | |
| 553 match_count_text_ = new MatchCountLabel(); | |
| 554 match_count_text_->SetEventTargeter( | |
| 555 base::MakeUnique<views::ViewTargeter>(this)); | |
| 556 AddChildViewAt(match_count_text_, 1); | |
| 557 | |
| 558 separator_ = new views::Separator(views::Separator::VERTICAL); | |
| 559 separator_->SetBorder(views::Border::CreateEmptyBorder( | |
| 560 0, kSeparatorLeftSpacing, 0, kSeparatorRightSpacing)); | |
| 561 AddChildViewAt(separator_, 2); | |
| 562 | |
| 563 find_text_->SetBorder(views::Border::NullBorder()); | |
| 564 | |
| 565 views::BoxLayout* manager = | |
| 566 new views::BoxLayout(views::BoxLayout::kHorizontal, kInteriorPadding, | |
| 567 kInteriorPadding, kInterChildSpacing); | |
| 568 SetLayoutManager(manager); | |
| 569 manager->SetFlexForView(find_text_, 1); | |
| 570 } | |
| 571 | |
| 572 void FindBarView::Find(const base::string16& search_text) { | 378 void FindBarView::Find(const base::string16& search_text) { |
| 573 FindBarController* controller = find_bar_host()->GetFindBarController(); | 379 FindBarController* controller = find_bar_host()->GetFindBarController(); |
| 574 DCHECK(controller); | 380 DCHECK(controller); |
| 575 content::WebContents* web_contents = controller->web_contents(); | 381 content::WebContents* web_contents = controller->web_contents(); |
| 576 // We must guard against a NULL web_contents, which can happen if the text | 382 // We must guard against a NULL web_contents, which can happen if the text |
| 577 // in the Find box is changed right after the tab is destroyed. Otherwise, it | 383 // in the Find box is changed right after the tab is destroyed. Otherwise, it |
| 578 // can lead to crashes, as exposed by automation testing in issue 8048. | 384 // can lead to crashes, as exposed by automation testing in issue 8048. |
| 579 if (!web_contents) | 385 if (!web_contents) |
| 580 return; | 386 return; |
| 581 FindTabHelper* find_tab_helper = FindTabHelper::FromWebContents(web_contents); | 387 FindTabHelper* find_tab_helper = FindTabHelper::FromWebContents(web_contents); |
| (...skipping 20 matching lines...) Expand all Loading... | |
| 602 Profile::FromBrowserContext(web_contents->GetBrowserContext()); | 408 Profile::FromBrowserContext(web_contents->GetBrowserContext()); |
| 603 FindBarState* find_bar_state = FindBarStateFactory::GetForProfile(profile); | 409 FindBarState* find_bar_state = FindBarStateFactory::GetForProfile(profile); |
| 604 find_bar_state->set_last_prepopulate_text(base::string16()); | 410 find_bar_state->set_last_prepopulate_text(base::string16()); |
| 605 } | 411 } |
| 606 } | 412 } |
| 607 | 413 |
| 608 void FindBarView::UpdateMatchCountAppearance(bool no_match) { | 414 void FindBarView::UpdateMatchCountAppearance(bool no_match) { |
| 609 bool enable_buttons = !find_text_->text().empty() && !no_match; | 415 bool enable_buttons = !find_text_->text().empty() && !no_match; |
| 610 find_previous_button_->SetEnabled(enable_buttons); | 416 find_previous_button_->SetEnabled(enable_buttons); |
| 611 find_next_button_->SetEnabled(enable_buttons); | 417 find_next_button_->SetEnabled(enable_buttons); |
| 612 | |
| 613 if (ui::MaterialDesignController::IsModeMaterial()) | |
| 614 return; | |
| 615 | |
| 616 if (no_match) { | |
| 617 match_count_text_->SetBackgroundColor(kBackgroundColorNoMatch); | |
| 618 match_count_text_->SetEnabledColor(kTextColorNoMatch); | |
| 619 } else { | |
| 620 match_count_text_->SetBackgroundColor(kBackgroundColorMatch); | |
| 621 match_count_text_->SetEnabledColor(kTextColorMatchCount); | |
| 622 } | |
| 623 } | |
| 624 | |
| 625 bool FindBarView::FocusForwarderView::OnMousePressed( | |
| 626 const ui::MouseEvent& event) { | |
| 627 if (view_to_focus_on_mousedown_) | |
| 628 view_to_focus_on_mousedown_->RequestFocus(); | |
| 629 return true; | |
| 630 } | 418 } |
| 631 | 419 |
| 632 FindBarHost* FindBarView::find_bar_host() const { | 420 FindBarHost* FindBarView::find_bar_host() const { |
| 633 return static_cast<FindBarHost*>(host()); | 421 return static_cast<FindBarHost*>(host()); |
| 634 } | 422 } |
| 635 | 423 |
| 636 const char* FindBarView::GetClassName() const { | 424 const char* FindBarView::GetClassName() const { |
| 637 return "FindBarView"; | 425 return "FindBarView"; |
| 638 } | 426 } |
| 639 | 427 |
| 640 void FindBarView::OnThemeChanged() { | |
| 641 if (ui::MaterialDesignController::IsModeMaterial()) | |
| 642 return; | |
| 643 | |
| 644 ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance(); | |
| 645 if (GetThemeProvider()) { | |
| 646 close_button_->SetBackground( | |
| 647 GetThemeProvider()->GetColor(ThemeProperties::COLOR_TAB_TEXT), | |
| 648 rb.GetImageSkiaNamed(IDR_CLOSE_1), | |
| 649 rb.GetImageSkiaNamed(IDR_CLOSE_1_MASK)); | |
| 650 } | |
| 651 } | |
| 652 | |
| 653 void FindBarView::OnNativeThemeChanged(const ui::NativeTheme* theme) { | 428 void FindBarView::OnNativeThemeChanged(const ui::NativeTheme* theme) { |
| 654 if (!ui::MaterialDesignController::IsModeMaterial()) | |
| 655 return; | |
| 656 | |
| 657 SkColor bg_color = theme->GetSystemColor( | 429 SkColor bg_color = theme->GetSystemColor( |
| 658 ui::NativeTheme::kColorId_TextfieldDefaultBackground); | 430 ui::NativeTheme::kColorId_TextfieldDefaultBackground); |
| 659 set_background(views::Background::CreateSolidBackground(bg_color)); | 431 set_background(views::Background::CreateSolidBackground(bg_color)); |
| 660 match_count_text_->SetBackgroundColor(bg_color); | 432 match_count_text_->SetBackgroundColor(bg_color); |
| 661 | 433 |
| 662 SkColor text_color = | 434 SkColor text_color = |
| 663 theme->GetSystemColor(ui::NativeTheme::kColorId_TextfieldDefaultColor); | 435 theme->GetSystemColor(ui::NativeTheme::kColorId_TextfieldDefaultColor); |
| 664 match_count_text_->SetEnabledColor(SkColorSetA(text_color, 0x69)); | 436 match_count_text_->SetEnabledColor(SkColorSetA(text_color, 0x69)); |
| 665 separator_->SetColor(SkColorSetA(text_color, 0x26)); | 437 separator_->SetColor(SkColorSetA(text_color, 0x26)); |
| 666 } | 438 } |
| 667 | 439 |
| OLD | NEW |