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 // For WinDDK ATL compatibility, these ATL headers must come first. | 5 // For WinDDK ATL compatibility, these ATL headers must come first. |
| 6 #include "build/build_config.h" | 6 #include "build/build_config.h" |
| 7 #if defined(OS_WIN) | 7 #if defined(OS_WIN) |
| 8 #include <atlbase.h> // NOLINT | 8 #include <atlbase.h> // NOLINT |
| 9 #include <atlwin.h> // NOLINT | 9 #include <atlwin.h> // NOLINT |
| 10 #endif | 10 #endif |
| (...skipping 10 matching lines...) Expand all Loading... | |
| 21 #include "grit/generated_resources.h" | 21 #include "grit/generated_resources.h" |
| 22 #include "grit/theme_resources.h" | 22 #include "grit/theme_resources.h" |
| 23 #include "ui/base/l10n/l10n_util.h" | 23 #include "ui/base/l10n/l10n_util.h" |
| 24 #include "ui/base/native_theme/native_theme.h" | 24 #include "ui/base/native_theme/native_theme.h" |
| 25 #include "ui/base/resource/resource_bundle.h" | 25 #include "ui/base/resource/resource_bundle.h" |
| 26 #include "ui/base/text/text_elider.h" | 26 #include "ui/base/text/text_elider.h" |
| 27 #include "ui/gfx/canvas.h" | 27 #include "ui/gfx/canvas.h" |
| 28 #include "ui/gfx/color_utils.h" | 28 #include "ui/gfx/color_utils.h" |
| 29 #include "ui/gfx/render_text.h" | 29 #include "ui/gfx/render_text.h" |
| 30 | 30 |
| 31 #if defined(OS_WIN) | |
| 32 #include "ui/base/native_theme/native_theme_win.h" | |
| 33 #endif | |
| 34 | |
| 35 #if defined(USE_AURA) | |
| 36 #include "ui/base/native_theme/native_theme_aura.h" | |
| 37 #endif | |
| 38 | |
| 31 namespace { | 39 namespace { |
| 32 | 40 |
| 33 const char16 kEllipsis[] = { 0x2026, 0x0 }; | 41 const char16 kEllipsis[] = { 0x2026, 0x0 }; |
| 34 | 42 |
| 35 // The minimum distance between the top and bottom of the {icon|text} and the | 43 // The minimum distance between the top and bottom of the {icon|text} and the |
| 36 // top or bottom of the row. | 44 // top or bottom of the row. |
| 37 const int kMinimumIconVerticalPadding = 2; | 45 const int kMinimumIconVerticalPadding = 2; |
| 38 const int kMinimumTextVerticalPadding = 3; | 46 const int kMinimumTextVerticalPadding = 3; |
| 39 | 47 |
| 40 } // namespace | 48 } // namespace |
| (...skipping 87 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 128 keyword_icon_->set_owned_by_client(); | 136 keyword_icon_->set_owned_by_client(); |
| 129 keyword_icon_->EnableCanvasFlippingForRTLUI(true); | 137 keyword_icon_->EnableCanvasFlippingForRTLUI(true); |
| 130 keyword_icon_->SetImage(GetKeywordIcon()); | 138 keyword_icon_->SetImage(GetKeywordIcon()); |
| 131 keyword_icon_->SizeToPreferredSize(); | 139 keyword_icon_->SizeToPreferredSize(); |
| 132 } | 140 } |
| 133 | 141 |
| 134 OmniboxResultView::~OmniboxResultView() { | 142 OmniboxResultView::~OmniboxResultView() { |
| 135 } | 143 } |
| 136 | 144 |
| 137 // static | 145 // static |
| 138 SkColor OmniboxResultView::GetColor(ResultViewState state, ColorKind kind) { | 146 SkColor OmniboxResultView::GetColor( |
| 139 static bool initialized = false; | 147 const ui::NativeTheme* theme, |
| 140 static SkColor colors[NUM_STATES][NUM_KINDS]; | 148 ResultViewState state, |
| 141 if (!initialized) { | 149 ColorKind kind) { |
| 142 #if defined(OS_WIN) | 150 #if defined(OS_WIN) |
| 143 colors[NORMAL][BACKGROUND] = color_utils::GetSysSkColor(COLOR_WINDOW); | 151 if (theme == ui::NativeThemeWin::instance()) { |
| 144 colors[SELECTED][BACKGROUND] = color_utils::GetSysSkColor(COLOR_HIGHLIGHT); | 152 static bool initialized = false; |
| 145 colors[NORMAL][TEXT] = color_utils::GetSysSkColor(COLOR_WINDOWTEXT); | 153 static SkColor colors[NUM_STATES][NUM_KINDS]; |
| 146 colors[SELECTED][TEXT] = color_utils::GetSysSkColor(COLOR_HIGHLIGHTTEXT); | 154 if (!initialized) { |
| 147 #elif defined(USE_AURA) | 155 colors[NORMAL][BACKGROUND] = color_utils::GetSysSkColor(COLOR_WINDOW); |
| 148 const ui::NativeTheme* theme = ui::NativeTheme::instance(); | 156 colors[SELECTED][BACKGROUND] = |
| 149 colors[SELECTED][BACKGROUND] = theme->GetSystemColor( | 157 color_utils::GetSysSkColor(COLOR_HIGHLIGHT); |
| 150 ui::NativeTheme::kColorId_TextfieldSelectionBackgroundFocused); | 158 colors[NORMAL][TEXT] = color_utils::GetSysSkColor(COLOR_WINDOWTEXT); |
| 151 colors[NORMAL][BACKGROUND] = theme->GetSystemColor( | 159 colors[SELECTED][TEXT] = color_utils::GetSysSkColor(COLOR_HIGHLIGHTTEXT); |
| 152 ui::NativeTheme::kColorId_TextfieldDefaultBackground); | 160 CommonInitColors(theme, colors); |
| 153 colors[NORMAL][URL] = SkColorSetARGB(0xff, 0x00, 0x99, 0x33); | 161 initialized = true; |
| 154 colors[SELECTED][URL] = SkColorSetARGB(0xff, 0x00, 0x66, 0x22); | 162 } |
| 155 colors[HOVERED][URL] = SkColorSetARGB(0xff, 0x00, 0x66, 0x22); | 163 return colors[state][kind]; |
| 156 #else | 164 } |
| 157 // TODO(beng): source from theme provider. | |
| 158 colors[NORMAL][BACKGROUND] = SK_ColorWHITE; | |
| 159 colors[SELECTED][BACKGROUND] = SK_ColorBLUE; | |
| 160 colors[NORMAL][TEXT] = SK_ColorBLACK; | |
| 161 colors[SELECTED][TEXT] = SK_ColorWHITE; | |
| 162 #endif | 165 #endif |
| 163 colors[HOVERED][BACKGROUND] = | 166 { |
|
sky
2012/11/08 02:04:24
I put this in a block so that there is no conflict
Peter Kasting
2012/11/08 18:55:47
Might be slightly clearer to add a qualifier to th
| |
| 164 color_utils::AlphaBlend(colors[SELECTED][BACKGROUND], | 167 static bool initialized = false; |
| 165 colors[NORMAL][BACKGROUND], 64); | 168 static SkColor colors[NUM_STATES][NUM_KINDS]; |
| 166 colors[HOVERED][TEXT] = colors[NORMAL][TEXT]; | 169 if (!initialized) { |
| 167 for (int i = 0; i < NUM_STATES; ++i) { | 170 colors[SELECTED][BACKGROUND] = theme->GetSystemColor( |
| 168 #if defined(USE_AURA) | 171 ui::NativeTheme::kColorId_TextfieldSelectionBackgroundFocused); |
| 169 colors[i][TEXT] = | 172 colors[NORMAL][BACKGROUND] = theme->GetSystemColor( |
| 170 color_utils::AlphaBlend(SK_ColorBLACK, colors[i][BACKGROUND], 0xdd); | 173 ui::NativeTheme::kColorId_TextfieldDefaultBackground); |
| 171 colors[i][DIMMED_TEXT] = | 174 colors[NORMAL][URL] = SkColorSetARGB(0xff, 0x00, 0x99, 0x33); |
| 172 color_utils::AlphaBlend(SK_ColorBLACK, colors[i][BACKGROUND], 0xbb); | 175 colors[SELECTED][URL] = SkColorSetARGB(0xff, 0x00, 0x66, 0x22); |
| 173 #else | 176 colors[HOVERED][URL] = SkColorSetARGB(0xff, 0x00, 0x66, 0x22); |
| 174 colors[i][DIMMED_TEXT] = | 177 CommonInitColors(theme, colors); |
| 175 color_utils::AlphaBlend(colors[i][TEXT], colors[i][BACKGROUND], 128); | 178 initialized = true; |
| 176 colors[i][URL] = color_utils::GetReadableColor(SkColorSetRGB(0, 128, 0), | |
| 177 colors[i][BACKGROUND]); | |
| 178 #endif | |
| 179 | |
| 180 // TODO(joi): Programmatically draw the dropdown border using | |
| 181 // this color as well. (Right now it's drawn as black with 25% | |
| 182 // alpha.) | |
| 183 colors[i][DIVIDER] = | |
| 184 color_utils::AlphaBlend(colors[i][TEXT], colors[i][BACKGROUND], 0x34); | |
| 185 } | 179 } |
| 186 initialized = true; | 180 return colors[state][kind]; |
| 187 } | 181 } |
| 188 | |
| 189 return colors[state][kind]; | |
| 190 } | 182 } |
| 191 | 183 |
| 192 void OmniboxResultView::SetMatch(const AutocompleteMatch& match) { | 184 void OmniboxResultView::SetMatch(const AutocompleteMatch& match) { |
| 193 match_ = match; | 185 match_ = match; |
| 194 animation_->Reset(); | 186 animation_->Reset(); |
| 195 | 187 |
| 196 if (match.associated_keyword.get()) { | 188 if (match.associated_keyword.get()) { |
| 197 keyword_icon_->SetImage(GetKeywordIcon()); | 189 keyword_icon_->SetImage(GetKeywordIcon()); |
| 198 | 190 |
| 199 if (!keyword_icon_->parent()) | 191 if (!keyword_icon_->parent()) |
| (...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 256 DrawString(canvas, match.description, match.description_class, true, x, | 248 DrawString(canvas, match.description, match.description_class, true, x, |
| 257 text_bounds_.y()); | 249 text_bounds_.y()); |
| 258 } | 250 } |
| 259 } | 251 } |
| 260 | 252 |
| 261 int OmniboxResultView::GetTextHeight() const { | 253 int OmniboxResultView::GetTextHeight() const { |
| 262 return std::max(normal_font_.GetHeight(), bold_font_.GetHeight()); | 254 return std::max(normal_font_.GetHeight(), bold_font_.GetHeight()); |
| 263 } | 255 } |
| 264 | 256 |
| 265 // static | 257 // static |
| 258 void OmniboxResultView::CommonInitColors(const ui::NativeTheme* theme, | |
| 259 SkColor colors[][NUM_KINDS]) { | |
| 260 colors[HOVERED][BACKGROUND] = | |
| 261 color_utils::AlphaBlend(colors[SELECTED][BACKGROUND], | |
| 262 colors[NORMAL][BACKGROUND], 64); | |
| 263 colors[HOVERED][TEXT] = colors[NORMAL][TEXT]; | |
| 264 #if defined(USE_AURA) | |
| 265 const bool is_aura = theme == ui::NativeThemeAura::instance(); | |
| 266 #else | |
| 267 const bool is_aura = false; | |
| 268 #endif | |
| 269 for (int i = 0; i < NUM_STATES; ++i) { | |
| 270 if (is_aura) { | |
| 271 colors[i][TEXT] = | |
| 272 color_utils::AlphaBlend(SK_ColorBLACK, colors[i][BACKGROUND], 0xdd); | |
| 273 colors[i][DIMMED_TEXT] = | |
| 274 color_utils::AlphaBlend(SK_ColorBLACK, colors[i][BACKGROUND], 0xbb); | |
| 275 } else { | |
| 276 colors[i][DIMMED_TEXT] = | |
| 277 color_utils::AlphaBlend(colors[i][TEXT], colors[i][BACKGROUND], 128); | |
| 278 colors[i][URL] = color_utils::GetReadableColor(SkColorSetRGB(0, 128, 0), | |
| 279 colors[i][BACKGROUND]); | |
| 280 } | |
| 281 | |
| 282 // TODO(joi): Programmatically draw the dropdown border using | |
| 283 // this color as well. (Right now it's drawn as black with 25% | |
| 284 // alpha.) | |
| 285 colors[i][DIVIDER] = | |
| 286 color_utils::AlphaBlend(colors[i][TEXT], colors[i][BACKGROUND], 0x34); | |
| 287 } | |
| 288 } | |
| 289 | |
| 290 // static | |
| 266 bool OmniboxResultView::SortRunsLogically(const RunData& lhs, | 291 bool OmniboxResultView::SortRunsLogically(const RunData& lhs, |
| 267 const RunData& rhs) { | 292 const RunData& rhs) { |
| 268 return lhs.run_start < rhs.run_start; | 293 return lhs.run_start < rhs.run_start; |
| 269 } | 294 } |
| 270 | 295 |
| 271 // static | 296 // static |
| 272 bool OmniboxResultView::SortRunsVisually(const RunData& lhs, | 297 bool OmniboxResultView::SortRunsVisually(const RunData& lhs, |
| 273 const RunData& rhs) { | 298 const RunData& rhs) { |
| 274 return lhs.visual_order < rhs.visual_order; | 299 return lhs.visual_order < rhs.visual_order; |
| 275 } | 300 } |
| (...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 337 // Split the text into visual runs. We do this first so that we don't need to | 362 // Split the text into visual runs. We do this first so that we don't need to |
| 338 // worry about whether our eliding might change the visual display in | 363 // worry about whether our eliding might change the visual display in |
| 339 // unintended ways, e.g. by removing directional markings or by adding an | 364 // unintended ways, e.g. by removing directional markings or by adding an |
| 340 // ellipsis that's not enclosed in appropriate markings. | 365 // ellipsis that's not enclosed in appropriate markings. |
| 341 base::i18n::BiDiLineIterator bidi_line; | 366 base::i18n::BiDiLineIterator bidi_line; |
| 342 if (!bidi_line.Open(text, base::i18n::IsRTL(), is_url)) | 367 if (!bidi_line.Open(text, base::i18n::IsRTL(), is_url)) |
| 343 return x; | 368 return x; |
| 344 const int num_runs = bidi_line.CountRuns(); | 369 const int num_runs = bidi_line.CountRuns(); |
| 345 ScopedVector<gfx::RenderText> render_texts; | 370 ScopedVector<gfx::RenderText> render_texts; |
| 346 Runs runs; | 371 Runs runs; |
| 372 const ui::NativeTheme* theme = GetNativeTheme(); | |
| 347 for (int run = 0; run < num_runs; ++run) { | 373 for (int run = 0; run < num_runs; ++run) { |
| 348 int run_start_int = 0, run_length_int = 0; | 374 int run_start_int = 0, run_length_int = 0; |
| 349 // The index we pass to GetVisualRun corresponds to the position of the run | 375 // The index we pass to GetVisualRun corresponds to the position of the run |
| 350 // in the displayed text. For example, the string "Google in HEBREW" (where | 376 // in the displayed text. For example, the string "Google in HEBREW" (where |
| 351 // HEBREW is text in the Hebrew language) has two runs: "Google in " which | 377 // HEBREW is text in the Hebrew language) has two runs: "Google in " which |
| 352 // is an LTR run, and "HEBREW" which is an RTL run. In an LTR context, the | 378 // is an LTR run, and "HEBREW" which is an RTL run. In an LTR context, the |
| 353 // run "Google in " has the index 0 (since it is the leftmost run | 379 // run "Google in " has the index 0 (since it is the leftmost run |
| 354 // displayed). In an RTL context, the same run has the index 1 because it | 380 // displayed). In an RTL context, the same run has the index 1 because it |
| 355 // is the rightmost run. This is why the order in which we traverse the | 381 // is the rightmost run. This is why the order in which we traverse the |
| 356 // runs is different depending on the locale direction. | 382 // runs is different depending on the locale direction. |
| (...skipping 23 matching lines...) Expand all Loading... | |
| 380 current_run->classifications.push_back(ClassificationData()); | 406 current_run->classifications.push_back(ClassificationData()); |
| 381 ClassificationData* current_data = | 407 ClassificationData* current_data = |
| 382 ¤t_run->classifications.back(); | 408 ¤t_run->classifications.back(); |
| 383 current_data->text = text.substr(text_start, text_end - text_start); | 409 current_data->text = text.substr(text_start, text_end - text_start); |
| 384 | 410 |
| 385 // Calculate style-related data. | 411 // Calculate style-related data. |
| 386 const int style = classifications[i].style; | 412 const int style = classifications[i].style; |
| 387 const bool use_bold_font = !!(style & ACMatchClassification::MATCH); | 413 const bool use_bold_font = !!(style & ACMatchClassification::MATCH); |
| 388 current_data->font = &(use_bold_font ? bold_font_ : normal_font_); | 414 current_data->font = &(use_bold_font ? bold_font_ : normal_font_); |
| 389 const ResultViewState state = GetState(); | 415 const ResultViewState state = GetState(); |
| 390 if (style & ACMatchClassification::URL) | 416 if (style & ACMatchClassification::URL) { |
| 391 current_data->color = GetColor(state, URL); | 417 current_data->color = GetColor(theme, state, URL); |
| 392 else if (style & ACMatchClassification::DIM) | 418 } else if (style & ACMatchClassification::DIM) { |
| 393 current_data->color = GetColor(state, DIMMED_TEXT); | 419 current_data->color = GetColor(theme, state, DIMMED_TEXT); |
| 394 else | 420 } else { |
| 395 current_data->color = GetColor(state, force_dim ? DIMMED_TEXT : TEXT); | 421 current_data->color = GetColor(theme, state, |
| 422 force_dim ? DIMMED_TEXT : TEXT); | |
| 423 } | |
| 396 | 424 |
| 397 render_texts.push_back(gfx::RenderText::CreateInstance()); | 425 render_texts.push_back(gfx::RenderText::CreateInstance()); |
| 398 current_data->render_text = render_texts.back(); | 426 current_data->render_text = render_texts.back(); |
| 399 current_data->render_text->SetFont(*current_data->font); | 427 current_data->render_text->SetFont(*current_data->font); |
| 400 current_data->render_text->SetText(current_data->text); | 428 current_data->render_text->SetText(current_data->text); |
| 401 | 429 |
| 402 gfx::StyleRange style_range; | 430 gfx::StyleRange style_range; |
| 403 style_range.foreground = current_data->color; | 431 style_range.foreground = current_data->color; |
| 404 style_range.font_style = current_data->font->GetStyle(); | 432 style_range.font_style = current_data->font->GetStyle(); |
| 405 current_data->render_text->set_default_style(style_range); | 433 current_data->render_text->set_default_style(style_range); |
| (...skipping 186 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 592 std::max(text_width, 0), text_height); | 620 std::max(text_width, 0), text_height); |
| 593 } | 621 } |
| 594 | 622 |
| 595 void OmniboxResultView::OnBoundsChanged(const gfx::Rect& previous_bounds) { | 623 void OmniboxResultView::OnBoundsChanged(const gfx::Rect& previous_bounds) { |
| 596 animation_->SetSlideDuration(width() / 4); | 624 animation_->SetSlideDuration(width() / 4); |
| 597 } | 625 } |
| 598 | 626 |
| 599 void OmniboxResultView::OnPaint(gfx::Canvas* canvas) { | 627 void OmniboxResultView::OnPaint(gfx::Canvas* canvas) { |
| 600 const ResultViewState state = GetState(); | 628 const ResultViewState state = GetState(); |
| 601 if (state != NORMAL) | 629 if (state != NORMAL) |
| 602 canvas->DrawColor(GetColor(state, BACKGROUND)); | 630 canvas->DrawColor(GetColor(GetNativeTheme(), state, BACKGROUND)); |
| 603 | 631 |
| 604 if (!match_.associated_keyword.get() || | 632 if (!match_.associated_keyword.get() || |
| 605 keyword_icon_->x() > icon_bounds_.right()) { | 633 keyword_icon_->x() > icon_bounds_.right()) { |
| 606 // Paint the icon. | 634 // Paint the icon. |
| 607 canvas->DrawImageInt(GetIcon(), GetMirroredXForRect(icon_bounds_), | 635 canvas->DrawImageInt(GetIcon(), GetMirroredXForRect(icon_bounds_), |
| 608 icon_bounds_.y()); | 636 icon_bounds_.y()); |
| 609 | 637 |
| 610 // Paint the text. | 638 // Paint the text. |
| 611 int x = GetMirroredXForRect(text_bounds_); | 639 int x = GetMirroredXForRect(text_bounds_); |
| 612 mirroring_context_->Initialize(x, text_bounds_.width()); | 640 mirroring_context_->Initialize(x, text_bounds_.width()); |
| 613 PaintMatch(canvas, match_, x); | 641 PaintMatch(canvas, match_, x); |
| 614 } | 642 } |
| 615 | 643 |
| 616 if (match_.associated_keyword.get()) { | 644 if (match_.associated_keyword.get()) { |
| 617 // Paint the keyword text. | 645 // Paint the keyword text. |
| 618 int x = GetMirroredXForRect(keyword_text_bounds_); | 646 int x = GetMirroredXForRect(keyword_text_bounds_); |
| 619 mirroring_context_->Initialize(x, keyword_text_bounds_.width()); | 647 mirroring_context_->Initialize(x, keyword_text_bounds_.width()); |
| 620 PaintMatch(canvas, *match_.associated_keyword.get(), x); | 648 PaintMatch(canvas, *match_.associated_keyword.get(), x); |
| 621 } | 649 } |
| 622 } | 650 } |
| 623 | 651 |
| 624 void OmniboxResultView::AnimationProgressed(const ui::Animation* animation) { | 652 void OmniboxResultView::AnimationProgressed(const ui::Animation* animation) { |
| 625 Layout(); | 653 Layout(); |
| 626 SchedulePaint(); | 654 SchedulePaint(); |
| 627 } | 655 } |
| OLD | NEW |