Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(222)

Side by Side Diff: chrome/browser/android/compositor/layer/contextual_search_layer.cc

Issue 2363843002: [Contextual Search] Animate thumbnail appearance (Closed)
Patch Set: Changes from donnd@ review Created 4 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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
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() {
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).
55 if (search_provider_icon_sprite_visible_) {
56 if (search_provider_icon_sprite_->layer()->parent() != layer_)
57 layer_->AddChild(search_provider_icon_sprite_->layer().get());
58
59 search_provider_icon_sprite_->DrawSpriteFrame(
60 resource_manager_,
61 panel_icon_resource_id_,
62 search_provider_icon_sprite_metadata_resource_id_,
63 search_provider_icon_sprite_completion_percentage_);
64 icon = search_provider_icon_sprite_->layer();
65 } else if (search_provider_icon_sprite_->layer().get() &&
66 search_provider_icon_sprite_->layer()->parent()) {
67 search_provider_icon_sprite_->layer()->RemoveFromParent();
68 }
69
70 return icon;
71 }
72
73 void ContextualSearchLayer::SetProperties( 41 void ContextualSearchLayer::SetProperties(
74 int panel_shadow_resource_id, 42 int panel_shadow_resource_id,
75 int search_context_resource_id, 43 int search_context_resource_id,
76 int search_term_resource_id, 44 int search_term_resource_id,
77 int search_caption_resource_id, 45 int search_caption_resource_id,
78 int search_bar_shadow_resource_id, 46 int search_bar_shadow_resource_id,
79 int panel_icon_resource_id, 47 int panel_icon_resource_id,
80 int search_provider_icon_sprite_metadata_resource_id, 48 int search_provider_icon_sprite_metadata_resource_id,
81 int arrow_up_resource_id, 49 int arrow_up_resource_id,
82 int close_icon_resource_id, 50 int close_icon_resource_id,
(...skipping 23 matching lines...) Expand all
106 float search_term_opacity, 74 float search_term_opacity,
107 float search_caption_animation_percentage, 75 float search_caption_animation_percentage,
108 bool search_caption_visible, 76 bool search_caption_visible,
109 bool search_bar_border_visible, 77 bool search_bar_border_visible,
110 float search_bar_border_height, 78 float search_bar_border_height,
111 bool search_bar_shadow_visible, 79 bool search_bar_shadow_visible,
112 float search_bar_shadow_opacity, 80 float search_bar_shadow_opacity,
113 bool search_provider_icon_sprite_visible, 81 bool search_provider_icon_sprite_visible,
114 float search_provider_icon_sprite_completion_percentage, 82 float search_provider_icon_sprite_completion_percentage,
115 bool thumbnail_visible, 83 bool thumbnail_visible,
84 float thumbnail_visibility_percentage,
116 int thumbnail_size, 85 int thumbnail_size,
117 float arrow_icon_opacity, 86 float arrow_icon_opacity,
118 float arrow_icon_rotation, 87 float arrow_icon_rotation,
119 float close_icon_opacity, 88 float close_icon_opacity,
120 bool progress_bar_visible, 89 bool progress_bar_visible,
121 float progress_bar_height, 90 float progress_bar_height,
122 float progress_bar_opacity, 91 float progress_bar_opacity,
123 int progress_bar_completion) { 92 int progress_bar_completion) {
124 93
125 search_provider_icon_sprite_visible_ = search_provider_icon_sprite_visible;
126 search_provider_icon_sprite_metadata_resource_id_ =
127 search_provider_icon_sprite_metadata_resource_id;
128 search_provider_icon_sprite_completion_percentage_ =
129 search_provider_icon_sprite_completion_percentage;
130
131 thumbnail_visible_ = thumbnail_visible;
132 thumbnail_size_ = thumbnail_size;
133
134 // Grabs the dynamic Search Context resource. 94 // Grabs the dynamic Search Context resource.
135 ui::ResourceManager::Resource* search_context_resource = 95 ui::ResourceManager::Resource* search_context_resource =
136 resource_manager_->GetResource(ui::ANDROID_RESOURCE_TYPE_DYNAMIC, 96 resource_manager_->GetResource(ui::ANDROID_RESOURCE_TYPE_DYNAMIC,
137 search_context_resource_id); 97 search_context_resource_id);
138 98
139 // Round values to avoid pixel gap between layers. 99 // Round values to avoid pixel gap between layers.
140 search_bar_height = floor(search_bar_height); 100 search_bar_height = floor(search_bar_height);
141 101
142 float search_bar_top = search_peek_promo_height; 102 float search_bar_top = search_peek_promo_height;
143 float search_bar_bottom = search_bar_top + search_bar_height; 103 float search_bar_bottom = search_bar_top + search_bar_height;
(...skipping 318 matching lines...) Expand 10 before | Expand all | Expand 10 after
462 progress_bar_->SetPosition(gfx::PointF(0.f, progress_bar_y)); 422 progress_bar_->SetPosition(gfx::PointF(0.f, progress_bar_y));
463 progress_bar_->SetOpacity(progress_bar_opacity); 423 progress_bar_->SetOpacity(progress_bar_opacity);
464 } else { 424 } else {
465 // Removes Progress Bar and its Background from the Layer Tree. 425 // Removes Progress Bar and its Background from the Layer Tree.
466 if (progress_bar_background_.get() && progress_bar_background_->parent()) 426 if (progress_bar_background_.get() && progress_bar_background_->parent())
467 progress_bar_background_->RemoveFromParent(); 427 progress_bar_background_->RemoveFromParent();
468 428
469 if (progress_bar_.get() && progress_bar_->parent()) 429 if (progress_bar_.get() && progress_bar_->parent())
470 progress_bar_->RemoveFromParent(); 430 progress_bar_->RemoveFromParent();
471 } 431 }
432
433 // ---------------------------------------------------------------------------
434 // Icon Layer
435 // ---------------------------------------------------------------------------
436 thumbnail_size_ = thumbnail_size;
437 SetupIconLayer(search_provider_icon_sprite_visible,
438 search_provider_icon_sprite_metadata_resource_id,
439 search_provider_icon_sprite_completion_percentage,
440 thumbnail_visible,
441 thumbnail_visibility_percentage);
442 }
443
444 scoped_refptr<cc::Layer> ContextualSearchLayer::GetIconLayer() {
445 return icon_layer_;
446 }
447
448 void ContextualSearchLayer::SetupIconLayer(
449 bool search_provider_icon_sprite_visible,
450 int search_provider_icon_sprite_metadata_resource_id,
451 float search_provider_icon_sprite_completion_percentage,
452 bool thumbnail_visible,
453 float thumbnail_visibility_percentage) {
454 icon_layer_->SetBounds(gfx::Size(thumbnail_size_, thumbnail_size_));
455 icon_layer_->SetMasksToBounds(true);
456
457 // Thumbnail
458 if (thumbnail_visible) {
459 if (thumbnail_layer_->parent() != icon_layer_)
460 icon_layer_->AddChild(thumbnail_layer_);
461
462 thumbnail_layer_->SetOpacity(thumbnail_visibility_percentage);
463
464 // When animating, the thumbnail and icon sprite slide through
465 // |icon_layer_|. This effect is achieved by changing the y-offset
466 // for each child layer.
467 // If the thumbnail has a height less than |thumbnail_size_|, it will have
468 // a top margin that needs to be accounted for while running the
469 // animation. The final |thumbnail_y_offset| should be equal to
470 // |thumbnail_top_margin_|.
471 float thumbnail_y_offset =
472 (thumbnail_size_ * (1.f - thumbnail_visibility_percentage))
473 + thumbnail_top_margin_;
474 thumbnail_layer_->SetPosition(
475 gfx::PointF(thumbnail_side_margin_, thumbnail_y_offset));
476 } else if (thumbnail_layer_->parent()) {
477 thumbnail_layer_->RemoveFromParent();
478 }
479
480 // Search Provider Icon Sprite
481 if (search_provider_icon_sprite_visible) {
482 if (search_provider_icon_sprite_->layer()->parent() != icon_layer_)
483 icon_layer_->AddChild(search_provider_icon_sprite_->layer().get());
484
485 search_provider_icon_sprite_->DrawSpriteFrame(
486 resource_manager_,
487 panel_icon_resource_id_,
488 search_provider_icon_sprite_metadata_resource_id,
489 search_provider_icon_sprite_completion_percentage);
490
491 search_provider_icon_sprite_->layer()->SetOpacity(
492 1.f - thumbnail_visibility_percentage);
493
494 float icon_y_offset =
495 -(thumbnail_size_ * thumbnail_visibility_percentage);
496 search_provider_icon_sprite_->layer()->SetPosition(
497 gfx::PointF(0.f, icon_y_offset));
498
499 } else if (search_provider_icon_sprite_->layer().get() &&
500 search_provider_icon_sprite_->layer()->parent()) {
501 search_provider_icon_sprite_->layer()->RemoveFromParent();
502 }
472 } 503 }
473 504
474 void ContextualSearchLayer::SetThumbnail(const SkBitmap* thumbnail) { 505 void ContextualSearchLayer::SetThumbnail(const SkBitmap* thumbnail) {
475 // Determine the scaled thumbnail width and height. If both the height and 506 // Determine the scaled thumbnail width and height. If both the height and
476 // width of |thumbnail| are larger than |thumbnail_size_|, the thumbnail will 507 // width of |thumbnail| are larger than |thumbnail_size_|, the thumbnail will
477 // be scaled down by a call to Layer::SetBounds() below. 508 // be scaled down by a call to Layer::SetBounds() below.
478 int min_dimension = std::min(thumbnail->width(), thumbnail->height()); 509 int min_dimension = std::min(thumbnail->width(), thumbnail->height());
479 int scaled_thumbnail_width = thumbnail->width(); 510 int scaled_thumbnail_width = thumbnail->width();
480 int scaled_thumbnail_height = thumbnail->height(); 511 int scaled_thumbnail_height = thumbnail->height();
481 if (min_dimension > thumbnail_size_) { 512 if (min_dimension > thumbnail_size_) {
(...skipping 21 matching lines...) Expand all
503 } else if (scaled_thumbnail_height > thumbnail_size_) { 534 } else if (scaled_thumbnail_height > thumbnail_size_) {
504 // Crop an even amount on the top and bottom of the thumbnail. 535 // Crop an even amount on the top and bottom of the thumbnail.
505 float top_left_y_px = (scaled_thumbnail_height - thumbnail_size_) / 2.f; 536 float top_left_y_px = (scaled_thumbnail_height - thumbnail_size_) / 2.f;
506 float bottom_right_y_px = top_left_y_px + thumbnail_size_; 537 float bottom_right_y_px = top_left_y_px + thumbnail_size_;
507 538
508 top_left_y = top_left_y_px / scaled_thumbnail_height; 539 top_left_y = top_left_y_px / scaled_thumbnail_height;
509 bottom_right_y = bottom_right_y_px / scaled_thumbnail_height; 540 bottom_right_y = bottom_right_y_px / scaled_thumbnail_height;
510 } 541 }
511 542
512 // If the original |thumbnail| height or width is smaller than 543 // If the original |thumbnail| height or width is smaller than
513 // |thumbnail_size_| determine the x, y offset needed to center 544 // |thumbnail_size_| determine the side and top margins needed to center
514 // the thumbnail. 545 // the thumbnail.
515 float x_pos = 0; 546 thumbnail_side_margin_ = 0;
516 float y_pos = 0; 547 thumbnail_top_margin_ = 0;
517 548
518 if (scaled_thumbnail_width < thumbnail_size_) { 549 if (scaled_thumbnail_width < thumbnail_size_) {
519 x_pos = (thumbnail_size_ - scaled_thumbnail_width) / 2.f; 550 thumbnail_side_margin_ = (thumbnail_size_ - scaled_thumbnail_width) / 2.f;
520 } 551 }
521 552
522 if (scaled_thumbnail_height < thumbnail_size_) { 553 if (scaled_thumbnail_height < thumbnail_size_) {
523 y_pos = (thumbnail_size_ - scaled_thumbnail_height) / 2.f; 554 thumbnail_top_margin_ = (thumbnail_size_ - scaled_thumbnail_height) / 2.f;
524 } 555 }
525 556
526 // Determine the layer bounds. This will down scale the thumbnail if 557 // Determine the layer bounds. This will down scale the thumbnail if
527 // necessary and ensure it is displayed at |thumbnail_size_|. If 558 // necessary and ensure it is displayed at |thumbnail_size_|. If
528 // either the original |thumbnail| height or width is smaller than 559 // either the original |thumbnail| height or width is smaller than
529 // |thumbnail_size_|, the thumbnail will not be scaled. 560 // |thumbnail_size_|, the thumbnail will not be scaled.
530 int layer_width = std::min(thumbnail_size_, scaled_thumbnail_width); 561 int layer_width = std::min(thumbnail_size_, scaled_thumbnail_width);
531 int layer_height = std::min(thumbnail_size_, scaled_thumbnail_height); 562 int layer_height = std::min(thumbnail_size_, scaled_thumbnail_height);
532 563
533 // UIResourceLayer requires an immutable copy of the input |thumbnail|. 564 // UIResourceLayer requires an immutable copy of the input |thumbnail|.
534 SkBitmap thumbnail_copy; 565 SkBitmap thumbnail_copy;
535 if (thumbnail->isImmutable()) { 566 if (thumbnail->isImmutable()) {
536 thumbnail_copy = *thumbnail; 567 thumbnail_copy = *thumbnail;
537 } else { 568 } else {
538 thumbnail->copyTo(&thumbnail_copy); 569 thumbnail->copyTo(&thumbnail_copy);
539 thumbnail_copy.setImmutable(); 570 thumbnail_copy.setImmutable();
540 } 571 }
541 572
542 thumbnail_layer_->SetBitmap(thumbnail_copy); 573 thumbnail_layer_->SetBitmap(thumbnail_copy);
543 thumbnail_layer_->SetBounds(gfx::Size(layer_width, layer_height)); 574 thumbnail_layer_->SetBounds(gfx::Size(layer_width, layer_height));
544 thumbnail_layer_->SetPosition(gfx::PointF(x_pos, y_pos)); 575 thumbnail_layer_->SetPosition(
576 gfx::PointF(thumbnail_side_margin_, thumbnail_top_margin_));
545 thumbnail_layer_->SetUV(gfx::PointF(top_left_x, top_left_y), 577 thumbnail_layer_->SetUV(gfx::PointF(top_left_x, top_left_y),
546 gfx::PointF(bottom_right_x, bottom_right_y)); 578 gfx::PointF(bottom_right_x, bottom_right_y));
547 } 579 }
548 580
549 ContextualSearchLayer::ContextualSearchLayer( 581 ContextualSearchLayer::ContextualSearchLayer(
550 ui::ResourceManager* resource_manager) 582 ui::ResourceManager* resource_manager)
551 : OverlayPanelLayer(resource_manager), 583 : OverlayPanelLayer(resource_manager),
552 search_context_(cc::UIResourceLayer::Create()), 584 search_context_(cc::UIResourceLayer::Create()),
585 icon_layer_(cc::Layer::Create()),
553 search_provider_icon_sprite_(CrushedSpriteLayer::Create()), 586 search_provider_icon_sprite_(CrushedSpriteLayer::Create()),
554 thumbnail_layer_(cc::UIResourceLayer::Create()), 587 thumbnail_layer_(cc::UIResourceLayer::Create()),
555 arrow_icon_(cc::UIResourceLayer::Create()), 588 arrow_icon_(cc::UIResourceLayer::Create()),
556 search_promo_(cc::UIResourceLayer::Create()), 589 search_promo_(cc::UIResourceLayer::Create()),
557 search_promo_container_(cc::SolidColorLayer::Create()), 590 search_promo_container_(cc::SolidColorLayer::Create()),
558 peek_promo_container_(cc::SolidColorLayer::Create()), 591 peek_promo_container_(cc::SolidColorLayer::Create()),
559 peek_promo_ripple_(cc::NinePatchLayer::Create()), 592 peek_promo_ripple_(cc::NinePatchLayer::Create()),
560 peek_promo_text_(cc::UIResourceLayer::Create()), 593 peek_promo_text_(cc::UIResourceLayer::Create()),
561 progress_bar_(cc::NinePatchLayer::Create()), 594 progress_bar_(cc::NinePatchLayer::Create()),
562 progress_bar_background_(cc::NinePatchLayer::Create()), 595 progress_bar_background_(cc::NinePatchLayer::Create()),
(...skipping 25 matching lines...) Expand all
588 search_promo_->SetIsDrawable(true); 621 search_promo_->SetIsDrawable(true);
589 622
590 // Progress Bar Background 623 // Progress Bar Background
591 progress_bar_background_->SetIsDrawable(true); 624 progress_bar_background_->SetIsDrawable(true);
592 progress_bar_background_->SetFillCenter(true); 625 progress_bar_background_->SetFillCenter(true);
593 626
594 // Progress Bar 627 // Progress Bar
595 progress_bar_->SetIsDrawable(true); 628 progress_bar_->SetIsDrawable(true);
596 progress_bar_->SetFillCenter(true); 629 progress_bar_->SetFillCenter(true);
597 630
631 // Icon
632 icon_layer_->SetIsDrawable(true);
633 layer_->AddChild(icon_layer_);
634
598 // Thumbnail 635 // Thumbnail
599 thumbnail_layer_->SetIsDrawable(true); 636 thumbnail_layer_->SetIsDrawable(true);
600 } 637 }
601 638
602 ContextualSearchLayer::~ContextualSearchLayer() { 639 ContextualSearchLayer::~ContextualSearchLayer() {
603 } 640 }
604 641
605 } // namespace android 642 } // namespace android
606 } // namespace chrome 643 } // namespace chrome
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698