OLD | NEW |
---|---|
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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/android/compositor/layer/contextual_search_layer.h" | 5 #include "chrome/browser/android/compositor/layer/contextual_search_layer.h" |
6 | 6 |
7 #include "cc/layers/layer.h" | 7 #include "cc/layers/layer.h" |
8 #include "cc/layers/nine_patch_layer.h" | 8 #include "cc/layers/nine_patch_layer.h" |
9 #include "cc/layers/solid_color_layer.h" | 9 #include "cc/layers/solid_color_layer.h" |
10 #include "cc/layers/ui_resource_layer.h" | 10 #include "cc/layers/ui_resource_layer.h" |
(...skipping 20 matching lines...) Expand all Loading... | |
31 | 31 |
32 namespace chrome { | 32 namespace chrome { |
33 namespace android { | 33 namespace android { |
34 | 34 |
35 // static | 35 // static |
36 scoped_refptr<ContextualSearchLayer> ContextualSearchLayer::Create( | 36 scoped_refptr<ContextualSearchLayer> ContextualSearchLayer::Create( |
37 ui::ResourceManager* resource_manager) { | 37 ui::ResourceManager* resource_manager) { |
38 return make_scoped_refptr(new ContextualSearchLayer(resource_manager)); | 38 return make_scoped_refptr(new ContextualSearchLayer(resource_manager)); |
39 } | 39 } |
40 | 40 |
41 scoped_refptr<cc::Layer> ContextualSearchLayer::GetIconLayer() { | 41 scoped_refptr<cc::Layer> ContextualSearchLayer::GetIconLayer() { |
Donn Denman
2016/09/08 18:13:39
nit: I think this section could benefit from some
Theresa
2016/09/08 20:18:22
I got rid off the duplicate code removing the spri
Donn Denman
2016/09/08 20:41:51
Yes, that's most of what I was looking for, and if
| |
42 if (thumbnail_visible_) { | |
43 if (search_provider_icon_sprite_->layer().get() && | |
44 search_provider_icon_sprite_->layer()->parent()) { | |
45 search_provider_icon_sprite_->layer()->RemoveFromParent(); | |
46 } | |
47 | |
48 if (thumbnail_layer_->parent() != layer_) | |
49 layer_->AddChild(thumbnail_layer_); | |
50 | |
51 return thumbnail_layer_; | |
52 } | |
53 | |
54 if (thumbnail_layer_->parent()) { | |
55 thumbnail_layer_->RemoveFromParent(); | |
56 } | |
57 | |
42 // Search Provider Icon Sprite (Animated) | 58 // Search Provider Icon Sprite (Animated) |
43 if (search_provider_icon_sprite_visible_) { | 59 if (search_provider_icon_sprite_visible_) { |
44 if (search_provider_icon_sprite_->layer()->parent() != layer_) { | 60 if (search_provider_icon_sprite_->layer()->parent() != layer_) { |
45 layer_->AddChild(search_provider_icon_sprite_->layer().get()); | 61 layer_->AddChild(search_provider_icon_sprite_->layer().get()); |
46 } | 62 } |
47 search_provider_icon_sprite_->DrawSpriteFrame( | 63 search_provider_icon_sprite_->DrawSpriteFrame( |
48 resource_manager_, | 64 resource_manager_, |
49 panel_icon_resource_id_, | 65 panel_icon_resource_id_, |
50 search_provider_icon_sprite_metadata_resource_id_, | 66 search_provider_icon_sprite_metadata_resource_id_, |
51 search_provider_icon_sprite_completion_percentage_); | 67 search_provider_icon_sprite_completion_percentage_); |
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
95 float search_context_opacity, | 111 float search_context_opacity, |
96 float search_term_opacity, | 112 float search_term_opacity, |
97 float search_caption_animation_percentage, | 113 float search_caption_animation_percentage, |
98 bool search_caption_visible, | 114 bool search_caption_visible, |
99 bool search_bar_border_visible, | 115 bool search_bar_border_visible, |
100 float search_bar_border_height, | 116 float search_bar_border_height, |
101 bool search_bar_shadow_visible, | 117 bool search_bar_shadow_visible, |
102 float search_bar_shadow_opacity, | 118 float search_bar_shadow_opacity, |
103 bool search_provider_icon_sprite_visible, | 119 bool search_provider_icon_sprite_visible, |
104 float search_provider_icon_sprite_completion_percentage, | 120 float search_provider_icon_sprite_completion_percentage, |
121 bool thumbnail_visible, | |
122 int thumbnail_size, | |
105 float arrow_icon_opacity, | 123 float arrow_icon_opacity, |
106 float arrow_icon_rotation, | 124 float arrow_icon_rotation, |
107 float close_icon_opacity, | 125 float close_icon_opacity, |
108 bool progress_bar_visible, | 126 bool progress_bar_visible, |
109 float progress_bar_height, | 127 float progress_bar_height, |
110 float progress_bar_opacity, | 128 float progress_bar_opacity, |
111 int progress_bar_completion) { | 129 int progress_bar_completion) { |
112 | 130 |
113 search_provider_icon_sprite_visible_ = search_provider_icon_sprite_visible; | 131 search_provider_icon_sprite_visible_ = search_provider_icon_sprite_visible; |
114 search_provider_icon_sprite_metadata_resource_id_ = | 132 search_provider_icon_sprite_metadata_resource_id_ = |
115 search_provider_icon_sprite_metadata_resource_id; | 133 search_provider_icon_sprite_metadata_resource_id; |
116 search_provider_icon_sprite_completion_percentage_ = | 134 search_provider_icon_sprite_completion_percentage_ = |
117 search_provider_icon_sprite_completion_percentage; | 135 search_provider_icon_sprite_completion_percentage; |
118 | 136 |
137 thumbnail_visible_ = thumbnail_visible; | |
138 thumbnail_size_ = thumbnail_size; | |
139 | |
119 // Grabs the dynamic Search Context resource. | 140 // Grabs the dynamic Search Context resource. |
120 ui::ResourceManager::Resource* search_context_resource = | 141 ui::ResourceManager::Resource* search_context_resource = |
121 resource_manager_->GetResource(ui::ANDROID_RESOURCE_TYPE_DYNAMIC, | 142 resource_manager_->GetResource(ui::ANDROID_RESOURCE_TYPE_DYNAMIC, |
122 search_context_resource_id); | 143 search_context_resource_id); |
123 | 144 |
124 // Round values to avoid pixel gap between layers. | 145 // Round values to avoid pixel gap between layers. |
125 search_bar_height = floor(search_bar_height); | 146 search_bar_height = floor(search_bar_height); |
126 | 147 |
127 float search_bar_top = search_peek_promo_height; | 148 float search_bar_top = search_peek_promo_height; |
128 float search_bar_bottom = search_bar_top + search_bar_height; | 149 float search_bar_bottom = search_bar_top + search_bar_height; |
(...skipping 319 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
448 } else { | 469 } else { |
449 // Removes Progress Bar and its Background from the Layer Tree. | 470 // Removes Progress Bar and its Background from the Layer Tree. |
450 if (progress_bar_background_.get() && progress_bar_background_->parent()) | 471 if (progress_bar_background_.get() && progress_bar_background_->parent()) |
451 progress_bar_background_->RemoveFromParent(); | 472 progress_bar_background_->RemoveFromParent(); |
452 | 473 |
453 if (progress_bar_.get() && progress_bar_->parent()) | 474 if (progress_bar_.get() && progress_bar_->parent()) |
454 progress_bar_->RemoveFromParent(); | 475 progress_bar_->RemoveFromParent(); |
455 } | 476 } |
456 } | 477 } |
457 | 478 |
479 void ContextualSearchLayer::SetThumbnail(const SkBitmap* thumbnail) { | |
Donn Denman
2016/09/08 18:21:44
Can this scale-and-crop logic be shared with simil
Theresa
2016/09/08 20:18:22
Unfortunately not. Downloads displays its thumbnai
Donn Denman
2016/09/08 20:41:52
Agreed!
| |
480 // Determine the scaled thumbnail width and height. If both the height and | |
Donn Denman
2016/09/08 18:13:39
Don't we need scaling if *either* height or width
Theresa
2016/09/08 20:18:23
No, only if they're both greater than thumbnail_si
Donn Denman
2016/09/08 20:41:51
Acknowledged. Sorry you wasted some time cuz I di
| |
481 // width of |thumbnail| are larger than |thumbnail_size_|, the thumbnail will | |
482 // be scaled down by a call to Layer::SetBounds() below. | |
483 int min_dimension = std::min(thumbnail->width(), thumbnail->height()); | |
484 int scaled_thumbnail_width = thumbnail->width(); | |
485 int scaled_thumbnail_height = thumbnail->height(); | |
486 if (min_dimension > thumbnail_size_) { | |
487 scaled_thumbnail_width = | |
Donn Denman
2016/09/08 18:13:39
nit: just use *= here, and below? Now that I thin
Theresa
2016/09/08 20:18:23
Done.
| |
488 thumbnail->width() * thumbnail_size_ / min_dimension; | |
489 scaled_thumbnail_height = | |
490 thumbnail->height() * thumbnail_size_ / min_dimension; | |
491 } | |
492 | |
493 // Determine the UV transform coordinates. This will crop the thumbnail. | |
494 // (0, 0) is the default top left corner. (1, 1) is the default bottom | |
495 // right corner. | |
496 float top_left_x = 0; | |
497 float top_left_y = 0; | |
498 float bottom_right_x = 1; | |
499 float bottom_right_y = 1; | |
500 | |
501 if (scaled_thumbnail_width > thumbnail_size_) { | |
502 // Crop an even amount on the left and right sides of the thumbnail. | |
503 float top_left_x_px = (scaled_thumbnail_width - thumbnail_size_) / 2.f; | |
504 float bottom_right_x_px = top_left_x_px + thumbnail_size_; | |
505 | |
506 top_left_x = top_left_x_px / scaled_thumbnail_width; | |
507 bottom_right_x = bottom_right_x_px / scaled_thumbnail_width; | |
508 } else if (scaled_thumbnail_height > thumbnail_size_) { | |
509 // Crop an even amount on the top and bottom of the thumbnail. | |
510 float top_left_y_px = (scaled_thumbnail_height - thumbnail_size_) / 2.f; | |
511 float bottom_right_y_px = top_left_y_px + thumbnail_size_; | |
512 | |
513 top_left_y = top_left_y_px / scaled_thumbnail_height; | |
514 bottom_right_y = bottom_right_y_px / scaled_thumbnail_height; | |
515 } | |
516 | |
517 // If the original |thumbnail| height or width is smaller than | |
518 // |thumbnail_size_| determine the x, y offset needed to center | |
519 // the thumbnail. | |
520 float x_pos = 0; | |
521 float y_pos = 0; | |
522 | |
523 if (scaled_thumbnail_width < thumbnail_size_) { | |
524 x_pos = (thumbnail_size_ - scaled_thumbnail_width) / 2.f; | |
525 } | |
526 | |
527 if (scaled_thumbnail_height < thumbnail_size_) { | |
528 y_pos = (thumbnail_size_ - scaled_thumbnail_height) / 2.f; | |
529 } | |
530 | |
531 // Determine the layer bounds. This will down scale the thumbnail if | |
532 // necessary and ensure it is displayed at |thumbnail_size_|. If | |
533 // either the original |thumbnail| height or width is smaller than | |
534 // |thumbnail_size_|, the thumbnail will not be scaled. | |
535 int layer_width = std::min(thumbnail_size_, scaled_thumbnail_width); | |
536 int layer_height = std::min(thumbnail_size_, scaled_thumbnail_height); | |
537 | |
538 // UIResourceLayer requires an immutable copy of the input |thumbnail|. | |
539 SkBitmap thumbnail_copy; | |
540 if (thumbnail->isImmutable()) { | |
541 thumbnail_copy = *thumbnail; | |
542 } else { | |
543 thumbnail->copyTo(&thumbnail_copy); | |
544 thumbnail_copy.setImmutable(); | |
545 } | |
546 | |
547 thumbnail_layer_->SetBitmap(thumbnail_copy); | |
548 thumbnail_layer_->SetBounds(gfx::Size(layer_width, layer_height)); | |
549 thumbnail_layer_->SetPosition(gfx::PointF(x_pos, y_pos)); | |
550 thumbnail_layer_->SetUV(gfx::PointF(top_left_x, top_left_y), | |
551 gfx::PointF(bottom_right_x, bottom_right_y)); | |
552 } | |
553 | |
458 ContextualSearchLayer::ContextualSearchLayer( | 554 ContextualSearchLayer::ContextualSearchLayer( |
459 ui::ResourceManager* resource_manager) | 555 ui::ResourceManager* resource_manager) |
460 : OverlayPanelLayer(resource_manager), | 556 : OverlayPanelLayer(resource_manager), |
461 search_context_(cc::UIResourceLayer::Create()), | 557 search_context_(cc::UIResourceLayer::Create()), |
462 search_provider_icon_sprite_(CrushedSpriteLayer::Create()), | 558 search_provider_icon_sprite_(CrushedSpriteLayer::Create()), |
559 thumbnail_layer_(cc::UIResourceLayer::Create()), | |
463 arrow_icon_(cc::UIResourceLayer::Create()), | 560 arrow_icon_(cc::UIResourceLayer::Create()), |
464 search_promo_(cc::UIResourceLayer::Create()), | 561 search_promo_(cc::UIResourceLayer::Create()), |
465 search_promo_container_(cc::SolidColorLayer::Create()), | 562 search_promo_container_(cc::SolidColorLayer::Create()), |
466 peek_promo_container_(cc::SolidColorLayer::Create()), | 563 peek_promo_container_(cc::SolidColorLayer::Create()), |
467 peek_promo_ripple_(cc::NinePatchLayer::Create()), | 564 peek_promo_ripple_(cc::NinePatchLayer::Create()), |
468 peek_promo_text_(cc::UIResourceLayer::Create()), | 565 peek_promo_text_(cc::UIResourceLayer::Create()), |
469 progress_bar_(cc::NinePatchLayer::Create()), | 566 progress_bar_(cc::NinePatchLayer::Create()), |
470 progress_bar_background_(cc::NinePatchLayer::Create()), | 567 progress_bar_background_(cc::NinePatchLayer::Create()), |
471 search_caption_(cc::UIResourceLayer::Create()) { | 568 search_caption_(cc::UIResourceLayer::Create()) { |
472 // Search Peek Promo | 569 // Search Peek Promo |
(...skipping 22 matching lines...) Expand all Loading... | |
495 search_promo_container_->SetBackgroundColor(kSearchBackgroundColor); | 592 search_promo_container_->SetBackgroundColor(kSearchBackgroundColor); |
496 search_promo_->SetIsDrawable(true); | 593 search_promo_->SetIsDrawable(true); |
497 | 594 |
498 // Progress Bar Background | 595 // Progress Bar Background |
499 progress_bar_background_->SetIsDrawable(true); | 596 progress_bar_background_->SetIsDrawable(true); |
500 progress_bar_background_->SetFillCenter(true); | 597 progress_bar_background_->SetFillCenter(true); |
501 | 598 |
502 // Progress Bar | 599 // Progress Bar |
503 progress_bar_->SetIsDrawable(true); | 600 progress_bar_->SetIsDrawable(true); |
504 progress_bar_->SetFillCenter(true); | 601 progress_bar_->SetFillCenter(true); |
602 | |
603 // Thumbnail | |
604 thumbnail_layer_->SetIsDrawable(true); | |
505 } | 605 } |
506 | 606 |
507 ContextualSearchLayer::~ContextualSearchLayer() { | 607 ContextualSearchLayer::~ContextualSearchLayer() { |
508 } | 608 } |
509 | 609 |
510 } // namespace android | 610 } // namespace android |
511 } // namespace chrome | 611 } // namespace chrome |
OLD | NEW |