summaryrefslogtreecommitdiffstats
path: root/chrome/browser/views/app_launched_animation_win.cc
blob: 5637fc5eb29419f5a0305ce485507b417de41eb8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
// Copyright (c) 2010 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chrome/browser/app_launched_animation.h"

#include "app/animation.h"
#include "app/slide_animation.h"
#include "chrome/browser/extensions/image_loading_tracker.h"
#include "chrome/common/extensions/extension.h"
#include "chrome/common/extensions/extension_resource.h"
#include "gfx/rect.h"
#include "views/controls/image_view.h"
#include "views/widget/widget_win.h"

namespace {

// Start at 1, end at 0
//    0ms You click the icon
//    0ms The launcher and contents begins to fade out, the icon
//        you clicked does not (stays opaque)
//    0ms The app begins loading behind the launcher
// +100ms The app icon begins to fade out.
// +200ms The launcher finishes fading out
// +350ms The app icon finishes fading out

// How long the fade should take.
static const int kDurationMS = 250;

// The delay before the fade out should start.
static const int kDelayMS = 100;

// AppLaunchedAnimation creates an animation. It loads the icon for the
// extension and once the image is loaded the animation starts. The icon fades
// out after a short delay.
class AppLaunchedAnimationWin : public AnimationDelegate,
                                public ImageLoadingTracker::Observer,
                                public views::ImageView {
 public:
  AppLaunchedAnimationWin(Extension* extension, const gfx::Rect& rect);

 private:
  // AnimationDelegate
  virtual void AnimationProgressed(const Animation* animation);
  virtual void AnimationCanceled(const Animation* animation);
  virtual void AnimationEnded(const Animation* animation);

  // We use a HWND for the popup so that it may float above any HWNDs in our UI.
  views::WidgetWin* popup_;

  // The rect to use for the popup.
  gfx::Rect rect_;

  // Used for loading extension application icon.
  ImageLoadingTracker app_icon_loader_;

  // ImageLoadingTracker::Observer.
  virtual void OnImageLoaded(SkBitmap* image, ExtensionResource resource,
                             int index);

  // Hover animation.
  scoped_ptr<SlideAnimation> animation_;

  DISALLOW_COPY_AND_ASSIGN(AppLaunchedAnimationWin);
};

AppLaunchedAnimationWin::AppLaunchedAnimationWin(Extension* extension,
                                                 const gfx::Rect& rect)
    : popup_(NULL),
      rect_(rect),
      ALLOW_THIS_IN_INITIALIZER_LIST(app_icon_loader_(this)) {
  DCHECK(extension);
  app_icon_loader_.LoadImage(
      extension,
      extension->GetIconPath(Extension::EXTENSION_ICON_LARGE),
      rect_.size(),
      ImageLoadingTracker::DONT_CACHE);
}

void AppLaunchedAnimationWin::AnimationCanceled(const Animation* animation) {
  AnimationEnded(animation);
}

void AppLaunchedAnimationWin::AnimationEnded(const Animation* animation) {
  popup_->Close();
}

void AppLaunchedAnimationWin::AnimationProgressed(const Animation* animation) {
  // GetCurrentValue goes from 1 to 0 since we are hiding.
  const double current_value = 1.0 - animation->GetCurrentValue();
  const double current_time = current_value * (kDelayMS + kDurationMS);

  double opacity = 1.0 -
      std::max(0.0, static_cast<double>(current_time - kDelayMS)) /
          static_cast<double>(kDurationMS);

  popup_->SetOpacity(static_cast<SkColor>(opacity * 255.0));
  SchedulePaint();
}

void AppLaunchedAnimationWin::OnImageLoaded(SkBitmap* image,
                                            ExtensionResource resource,
                                            int index) {
  if (image) {
    SetImage(image);

    popup_ = new views::WidgetWin;
    popup_->set_window_style(WS_POPUP);
    popup_->set_window_ex_style(WS_EX_LAYERED | WS_EX_TOOLWINDOW |
                                WS_EX_TRANSPARENT);
    popup_->SetOpacity(static_cast<SkColor>(255));

    popup_->Init(NULL, rect_);
    popup_->SetContentsView(this);
    popup_->Show();

    // Start animation.
    animation_.reset(new SlideAnimation(this));
    animation_->SetSlideDuration(kDelayMS + kDurationMS);
    animation_->SetTweenType(Tween::LINEAR);
    animation_->Reset(1.0);
    animation_->Hide();
  } else {
    popup_->Close();
  }
}

}  // namespace

// static
void AppLaunchedAnimation::Show(Extension* extension, const gfx::Rect& rect) {
  // The animation will delete itself when it's finished.
  new AppLaunchedAnimationWin(extension, rect);
}