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 21 matching lines...) Expand all Loading... |
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() { |
42 // Search Provider Icon Sprite (Animated) | 42 scoped_refptr<cc::Layer> icon = nullptr; |
| 43 |
| 44 // Thumbnail. |
| 45 if (thumbnail_visible_) { |
| 46 if (thumbnail_layer_->parent() != layer_) |
| 47 layer_->AddChild(thumbnail_layer_); |
| 48 |
| 49 icon = thumbnail_layer_; |
| 50 } else if (thumbnail_layer_->parent()) { |
| 51 thumbnail_layer_->RemoveFromParent(); |
| 52 } |
| 53 |
| 54 // Search Provider Icon Sprite (Animated). |
43 if (search_provider_icon_sprite_visible_) { | 55 if (search_provider_icon_sprite_visible_) { |
44 if (search_provider_icon_sprite_->layer()->parent() != layer_) { | 56 if (search_provider_icon_sprite_->layer()->parent() != layer_) |
45 layer_->AddChild(search_provider_icon_sprite_->layer().get()); | 57 layer_->AddChild(search_provider_icon_sprite_->layer().get()); |
46 } | 58 |
47 search_provider_icon_sprite_->DrawSpriteFrame( | 59 search_provider_icon_sprite_->DrawSpriteFrame( |
48 resource_manager_, | 60 resource_manager_, |
49 panel_icon_resource_id_, | 61 panel_icon_resource_id_, |
50 search_provider_icon_sprite_metadata_resource_id_, | 62 search_provider_icon_sprite_metadata_resource_id_, |
51 search_provider_icon_sprite_completion_percentage_); | 63 search_provider_icon_sprite_completion_percentage_); |
52 } else { | 64 icon = search_provider_icon_sprite_->layer(); |
53 if (search_provider_icon_sprite_->layer().get() && | 65 } else if (search_provider_icon_sprite_->layer().get() && |
54 search_provider_icon_sprite_->layer()->parent()) { | 66 search_provider_icon_sprite_->layer()->parent()) { |
55 search_provider_icon_sprite_->layer()->RemoveFromParent(); | 67 search_provider_icon_sprite_->layer()->RemoveFromParent(); |
56 } | |
57 return nullptr; | |
58 } | 68 } |
59 | 69 |
60 return search_provider_icon_sprite_->layer(); | 70 return icon; |
61 } | 71 } |
62 | 72 |
63 void ContextualSearchLayer::SetProperties( | 73 void ContextualSearchLayer::SetProperties( |
64 int panel_shadow_resource_id, | 74 int panel_shadow_resource_id, |
65 int search_context_resource_id, | 75 int search_context_resource_id, |
66 int search_term_resource_id, | 76 int search_term_resource_id, |
67 int search_caption_resource_id, | 77 int search_caption_resource_id, |
68 int search_bar_shadow_resource_id, | 78 int search_bar_shadow_resource_id, |
69 int panel_icon_resource_id, | 79 int panel_icon_resource_id, |
70 int search_provider_icon_sprite_metadata_resource_id, | 80 int search_provider_icon_sprite_metadata_resource_id, |
(...skipping 24 matching lines...) Expand all Loading... |
95 float search_context_opacity, | 105 float search_context_opacity, |
96 float search_term_opacity, | 106 float search_term_opacity, |
97 float search_caption_animation_percentage, | 107 float search_caption_animation_percentage, |
98 bool search_caption_visible, | 108 bool search_caption_visible, |
99 bool search_bar_border_visible, | 109 bool search_bar_border_visible, |
100 float search_bar_border_height, | 110 float search_bar_border_height, |
101 bool search_bar_shadow_visible, | 111 bool search_bar_shadow_visible, |
102 float search_bar_shadow_opacity, | 112 float search_bar_shadow_opacity, |
103 bool search_provider_icon_sprite_visible, | 113 bool search_provider_icon_sprite_visible, |
104 float search_provider_icon_sprite_completion_percentage, | 114 float search_provider_icon_sprite_completion_percentage, |
| 115 bool thumbnail_visible, |
| 116 int thumbnail_size, |
105 float arrow_icon_opacity, | 117 float arrow_icon_opacity, |
106 float arrow_icon_rotation, | 118 float arrow_icon_rotation, |
107 float close_icon_opacity, | 119 float close_icon_opacity, |
108 bool progress_bar_visible, | 120 bool progress_bar_visible, |
109 float progress_bar_height, | 121 float progress_bar_height, |
110 float progress_bar_opacity, | 122 float progress_bar_opacity, |
111 int progress_bar_completion) { | 123 int progress_bar_completion) { |
112 | 124 |
113 search_provider_icon_sprite_visible_ = search_provider_icon_sprite_visible; | 125 search_provider_icon_sprite_visible_ = search_provider_icon_sprite_visible; |
114 search_provider_icon_sprite_metadata_resource_id_ = | 126 search_provider_icon_sprite_metadata_resource_id_ = |
115 search_provider_icon_sprite_metadata_resource_id; | 127 search_provider_icon_sprite_metadata_resource_id; |
116 search_provider_icon_sprite_completion_percentage_ = | 128 search_provider_icon_sprite_completion_percentage_ = |
117 search_provider_icon_sprite_completion_percentage; | 129 search_provider_icon_sprite_completion_percentage; |
118 | 130 |
| 131 thumbnail_visible_ = thumbnail_visible; |
| 132 thumbnail_size_ = thumbnail_size; |
| 133 |
119 // Grabs the dynamic Search Context resource. | 134 // Grabs the dynamic Search Context resource. |
120 ui::ResourceManager::Resource* search_context_resource = | 135 ui::ResourceManager::Resource* search_context_resource = |
121 resource_manager_->GetResource(ui::ANDROID_RESOURCE_TYPE_DYNAMIC, | 136 resource_manager_->GetResource(ui::ANDROID_RESOURCE_TYPE_DYNAMIC, |
122 search_context_resource_id); | 137 search_context_resource_id); |
123 | 138 |
124 // Round values to avoid pixel gap between layers. | 139 // Round values to avoid pixel gap between layers. |
125 search_bar_height = floor(search_bar_height); | 140 search_bar_height = floor(search_bar_height); |
126 | 141 |
127 float search_bar_top = search_peek_promo_height; | 142 float search_bar_top = search_peek_promo_height; |
128 float search_bar_bottom = search_bar_top + search_bar_height; | 143 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 { | 463 } else { |
449 // Removes Progress Bar and its Background from the Layer Tree. | 464 // Removes Progress Bar and its Background from the Layer Tree. |
450 if (progress_bar_background_.get() && progress_bar_background_->parent()) | 465 if (progress_bar_background_.get() && progress_bar_background_->parent()) |
451 progress_bar_background_->RemoveFromParent(); | 466 progress_bar_background_->RemoveFromParent(); |
452 | 467 |
453 if (progress_bar_.get() && progress_bar_->parent()) | 468 if (progress_bar_.get() && progress_bar_->parent()) |
454 progress_bar_->RemoveFromParent(); | 469 progress_bar_->RemoveFromParent(); |
455 } | 470 } |
456 } | 471 } |
457 | 472 |
| 473 void ContextualSearchLayer::SetThumbnail(const SkBitmap* thumbnail) { |
| 474 // Determine the scaled thumbnail width and height. If both the height and |
| 475 // width of |thumbnail| are larger than |thumbnail_size_|, the thumbnail will |
| 476 // be scaled down by a call to Layer::SetBounds() below. |
| 477 int min_dimension = std::min(thumbnail->width(), thumbnail->height()); |
| 478 int scaled_thumbnail_width = thumbnail->width(); |
| 479 int scaled_thumbnail_height = thumbnail->height(); |
| 480 if (min_dimension > thumbnail_size_) { |
| 481 scaled_thumbnail_width = |
| 482 scaled_thumbnail_width * thumbnail_size_ / min_dimension; |
| 483 scaled_thumbnail_height = |
| 484 scaled_thumbnail_height * thumbnail_size_ / min_dimension; |
| 485 } |
| 486 |
| 487 // Determine the UV transform coordinates. This will crop the thumbnail. |
| 488 // (0, 0) is the default top left corner. (1, 1) is the default bottom |
| 489 // right corner. |
| 490 float top_left_x = 0; |
| 491 float top_left_y = 0; |
| 492 float bottom_right_x = 1; |
| 493 float bottom_right_y = 1; |
| 494 |
| 495 if (scaled_thumbnail_width > thumbnail_size_) { |
| 496 // Crop an even amount on the left and right sides of the thumbnail. |
| 497 float top_left_x_px = (scaled_thumbnail_width - thumbnail_size_) / 2.f; |
| 498 float bottom_right_x_px = top_left_x_px + thumbnail_size_; |
| 499 |
| 500 top_left_x = top_left_x_px / scaled_thumbnail_width; |
| 501 bottom_right_x = bottom_right_x_px / scaled_thumbnail_width; |
| 502 } else if (scaled_thumbnail_height > thumbnail_size_) { |
| 503 // Crop an even amount on the top and bottom of the thumbnail. |
| 504 float top_left_y_px = (scaled_thumbnail_height - thumbnail_size_) / 2.f; |
| 505 float bottom_right_y_px = top_left_y_px + thumbnail_size_; |
| 506 |
| 507 top_left_y = top_left_y_px / scaled_thumbnail_height; |
| 508 bottom_right_y = bottom_right_y_px / scaled_thumbnail_height; |
| 509 } |
| 510 |
| 511 // If the original |thumbnail| height or width is smaller than |
| 512 // |thumbnail_size_| determine the x, y offset needed to center |
| 513 // the thumbnail. |
| 514 float x_pos = 0; |
| 515 float y_pos = 0; |
| 516 |
| 517 if (scaled_thumbnail_width < thumbnail_size_) { |
| 518 x_pos = (thumbnail_size_ - scaled_thumbnail_width) / 2.f; |
| 519 } |
| 520 |
| 521 if (scaled_thumbnail_height < thumbnail_size_) { |
| 522 y_pos = (thumbnail_size_ - scaled_thumbnail_height) / 2.f; |
| 523 } |
| 524 |
| 525 // Determine the layer bounds. This will down scale the thumbnail if |
| 526 // necessary and ensure it is displayed at |thumbnail_size_|. If |
| 527 // either the original |thumbnail| height or width is smaller than |
| 528 // |thumbnail_size_|, the thumbnail will not be scaled. |
| 529 int layer_width = std::min(thumbnail_size_, scaled_thumbnail_width); |
| 530 int layer_height = std::min(thumbnail_size_, scaled_thumbnail_height); |
| 531 |
| 532 // UIResourceLayer requires an immutable copy of the input |thumbnail|. |
| 533 SkBitmap thumbnail_copy; |
| 534 if (thumbnail->isImmutable()) { |
| 535 thumbnail_copy = *thumbnail; |
| 536 } else { |
| 537 thumbnail->copyTo(&thumbnail_copy); |
| 538 thumbnail_copy.setImmutable(); |
| 539 } |
| 540 |
| 541 thumbnail_layer_->SetBitmap(thumbnail_copy); |
| 542 thumbnail_layer_->SetBounds(gfx::Size(layer_width, layer_height)); |
| 543 thumbnail_layer_->SetPosition(gfx::PointF(x_pos, y_pos)); |
| 544 thumbnail_layer_->SetUV(gfx::PointF(top_left_x, top_left_y), |
| 545 gfx::PointF(bottom_right_x, bottom_right_y)); |
| 546 } |
| 547 |
458 ContextualSearchLayer::ContextualSearchLayer( | 548 ContextualSearchLayer::ContextualSearchLayer( |
459 ui::ResourceManager* resource_manager) | 549 ui::ResourceManager* resource_manager) |
460 : OverlayPanelLayer(resource_manager), | 550 : OverlayPanelLayer(resource_manager), |
461 search_context_(cc::UIResourceLayer::Create()), | 551 search_context_(cc::UIResourceLayer::Create()), |
462 search_provider_icon_sprite_(CrushedSpriteLayer::Create()), | 552 search_provider_icon_sprite_(CrushedSpriteLayer::Create()), |
| 553 thumbnail_layer_(cc::UIResourceLayer::Create()), |
463 arrow_icon_(cc::UIResourceLayer::Create()), | 554 arrow_icon_(cc::UIResourceLayer::Create()), |
464 search_promo_(cc::UIResourceLayer::Create()), | 555 search_promo_(cc::UIResourceLayer::Create()), |
465 search_promo_container_(cc::SolidColorLayer::Create()), | 556 search_promo_container_(cc::SolidColorLayer::Create()), |
466 peek_promo_container_(cc::SolidColorLayer::Create()), | 557 peek_promo_container_(cc::SolidColorLayer::Create()), |
467 peek_promo_ripple_(cc::NinePatchLayer::Create()), | 558 peek_promo_ripple_(cc::NinePatchLayer::Create()), |
468 peek_promo_text_(cc::UIResourceLayer::Create()), | 559 peek_promo_text_(cc::UIResourceLayer::Create()), |
469 progress_bar_(cc::NinePatchLayer::Create()), | 560 progress_bar_(cc::NinePatchLayer::Create()), |
470 progress_bar_background_(cc::NinePatchLayer::Create()), | 561 progress_bar_background_(cc::NinePatchLayer::Create()), |
471 search_caption_(cc::UIResourceLayer::Create()) { | 562 search_caption_(cc::UIResourceLayer::Create()) { |
472 // Search Peek Promo | 563 // Search Peek Promo |
(...skipping 22 matching lines...) Expand all Loading... |
495 search_promo_container_->SetBackgroundColor(kSearchBackgroundColor); | 586 search_promo_container_->SetBackgroundColor(kSearchBackgroundColor); |
496 search_promo_->SetIsDrawable(true); | 587 search_promo_->SetIsDrawable(true); |
497 | 588 |
498 // Progress Bar Background | 589 // Progress Bar Background |
499 progress_bar_background_->SetIsDrawable(true); | 590 progress_bar_background_->SetIsDrawable(true); |
500 progress_bar_background_->SetFillCenter(true); | 591 progress_bar_background_->SetFillCenter(true); |
501 | 592 |
502 // Progress Bar | 593 // Progress Bar |
503 progress_bar_->SetIsDrawable(true); | 594 progress_bar_->SetIsDrawable(true); |
504 progress_bar_->SetFillCenter(true); | 595 progress_bar_->SetFillCenter(true); |
| 596 |
| 597 // Thumbnail |
| 598 thumbnail_layer_->SetIsDrawable(true); |
505 } | 599 } |
506 | 600 |
507 ContextualSearchLayer::~ContextualSearchLayer() { | 601 ContextualSearchLayer::~ContextualSearchLayer() { |
508 } | 602 } |
509 | 603 |
510 } // namespace android | 604 } // namespace android |
511 } // namespace chrome | 605 } // namespace chrome |
OLD | NEW |