Home | History | Annotate | Download | only in launcher
      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
      3 // found in the LICENSE file.
      4 
      5 #include "ash/launcher/app_list_button.h"
      6 
      7 #include <vector>
      8 
      9 #include "ash/launcher/launcher_button_host.h"
     10 #include "ash/launcher/launcher_types.h"
     11 #include "grit/ash_resources.h"
     12 #include "grit/ash_strings.h"
     13 #include "ui/base/accessibility/accessible_view_state.h"
     14 #include "ui/base/l10n/l10n_util.h"
     15 #include "ui/base/resource/resource_bundle.h"
     16 #include "ui/compositor/layer.h"
     17 #include "ui/compositor/layer_animation_element.h"
     18 #include "ui/compositor/layer_animation_sequence.h"
     19 #include "ui/compositor/scoped_layer_animation_settings.h"
     20 
     21 namespace ash {
     22 namespace internal {
     23 
     24 namespace {
     25 
     26 const int kAnimationDurationInMs = 600;
     27 const float kAnimationOpacity[] = { 1.0f, 0.4f, 1.0f };
     28 const int kBorderSize = 9;
     29 }  // namespace
     30 
     31 AppListButton::AppListButton(views::ButtonListener* listener,
     32                              LauncherButtonHost* host)
     33     : views::ImageButton(listener),
     34       host_(host) {
     35   ResourceBundle& rb = ResourceBundle::GetSharedInstance();
     36   SetImage(
     37       views::CustomButton::STATE_NORMAL,
     38       rb.GetImageNamed(IDR_AURA_LAUNCHER_ICON_APPLIST).ToImageSkia());
     39   SetImage(
     40       views::CustomButton::STATE_HOVERED,
     41       rb.GetImageNamed(IDR_AURA_LAUNCHER_ICON_APPLIST_HOT).
     42           ToImageSkia());
     43   SetImage(
     44       views::CustomButton::STATE_PRESSED,
     45       rb.GetImageNamed(IDR_AURA_LAUNCHER_ICON_APPLIST_PUSHED).
     46           ToImageSkia());
     47   SetAccessibleName(l10n_util::GetStringUTF16(IDS_AURA_APP_LIST_TITLE));
     48   SetSize(gfx::Size(kLauncherPreferredSize, kLauncherPreferredSize));
     49   SetImageAlignment(ImageButton::ALIGN_CENTER, ImageButton::ALIGN_TOP);
     50 }
     51 
     52 AppListButton::~AppListButton() {
     53 }
     54 
     55 void AppListButton::StartLoadingAnimation() {
     56   layer()->GetAnimator()->StopAnimating();
     57 
     58   scoped_ptr<ui::LayerAnimationSequence> opacity_sequence(
     59       new ui::LayerAnimationSequence());
     60 
     61   opacity_sequence->set_is_cyclic(true);
     62 
     63   for (size_t i = 0; i < arraysize(kAnimationOpacity); ++i) {
     64     opacity_sequence->AddElement(
     65         ui::LayerAnimationElement::CreateOpacityElement(
     66             kAnimationOpacity[i],
     67             base::TimeDelta::FromMilliseconds(kAnimationDurationInMs)));
     68   }
     69 
     70   ui::LayerAnimationElement::AnimatableProperties opacity_properties;
     71   opacity_properties.insert(ui::LayerAnimationElement::OPACITY);
     72   opacity_sequence->AddElement(
     73       ui::LayerAnimationElement::CreatePauseElement(
     74           opacity_properties,
     75           base::TimeDelta::FromMilliseconds(kAnimationDurationInMs)));
     76 
     77   // LayerAnimator takes ownership of the sequences.
     78   layer()->GetAnimator()->ScheduleAnimation(opacity_sequence.release());
     79 }
     80 
     81 void AppListButton::StopLoadingAnimation() {
     82   layer()->GetAnimator()->StopAnimating();
     83 
     84   ui::ScopedLayerAnimationSettings settings(layer()->GetAnimator());
     85   settings.SetTransitionDuration(
     86       base::TimeDelta::FromMilliseconds(kAnimationDurationInMs));
     87   layer()->SetOpacity(1.0f);
     88   layer()->SetTransform(gfx::Transform());
     89 }
     90 
     91 bool AppListButton::OnMousePressed(const ui::MouseEvent& event) {
     92   ImageButton::OnMousePressed(event);
     93   host_->PointerPressedOnButton(this, LauncherButtonHost::MOUSE, event);
     94   return true;
     95 }
     96 
     97 void AppListButton::OnMouseReleased(const ui::MouseEvent& event) {
     98   ImageButton::OnMouseReleased(event);
     99   host_->PointerReleasedOnButton(this, LauncherButtonHost::MOUSE, false);
    100 }
    101 
    102 void AppListButton::OnMouseCaptureLost() {
    103   host_->PointerReleasedOnButton(this, LauncherButtonHost::MOUSE, true);
    104   ImageButton::OnMouseCaptureLost();
    105 }
    106 
    107 bool AppListButton::OnMouseDragged(const ui::MouseEvent& event) {
    108   ImageButton::OnMouseDragged(event);
    109   host_->PointerDraggedOnButton(this, LauncherButtonHost::MOUSE, event);
    110   return true;
    111 }
    112 
    113 void AppListButton::OnMouseMoved(const ui::MouseEvent& event) {
    114   ImageButton::OnMouseMoved(event);
    115   host_->MouseMovedOverButton(this);
    116 }
    117 
    118 void AppListButton::OnMouseEntered(const ui::MouseEvent& event) {
    119   ImageButton::OnMouseEntered(event);
    120   host_->MouseEnteredButton(this);
    121 }
    122 
    123 void AppListButton::OnMouseExited(const ui::MouseEvent& event) {
    124   ImageButton::OnMouseExited(event);
    125   host_->MouseExitedButton(this);
    126 }
    127 
    128 void AppListButton::GetAccessibleState(ui::AccessibleViewState* state) {
    129   state->role = ui::AccessibilityTypes::ROLE_PUSHBUTTON;
    130   state->name = host_->GetAccessibleName(this);
    131 }
    132 
    133 }  // namespace internal
    134 }  // namespace ash
    135