Improved the state machine in GuestView.
[chromium-blink-merge.git] / pdf / fading_control.cc
blob7e4d8ef63e4a34a8ffdf03edcde6e4dc0ba949f2
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.
5 #include "pdf/fading_control.h"
7 #include <math.h>
9 #include "base/logging.h"
10 #include "pdf/draw_utils.h"
11 #include "pdf/resource_consts.h"
13 namespace chrome_pdf {
15 FadingControl::FadingControl()
16 : alpha_shift_(0), timer_id_(0) {
19 FadingControl::~FadingControl() {
22 void FadingControl::OnTimerFired(uint32 timer_id) {
23 if (timer_id == timer_id_) {
24 int32 new_alpha = transparency() + alpha_shift_;
25 if (new_alpha <= kTransparentAlpha) {
26 Show(false, true);
27 OnFadeOutComplete();
28 return;
30 if (new_alpha >= kOpaqueAlpha) {
31 AdjustTransparency(kOpaqueAlpha, true);
32 OnFadeInComplete();
33 return;
36 AdjustTransparency(static_cast<uint8>(new_alpha), true);
37 timer_id_ = owner()->ScheduleTimer(id(), kFadingTimeoutMs);
41 // Fade In/Out control depending on visible flag over the time of time_ms.
42 void FadingControl::Fade(bool show, uint32 time_ms) {
43 DCHECK(time_ms != 0);
44 // Check if we already in the same state.
45 if (!visible() && !show)
46 return;
47 if (!visible() && show) {
48 Show(show, false);
49 AdjustTransparency(kTransparentAlpha, false);
50 OnFadeOutComplete();
52 if (transparency() == kOpaqueAlpha && show) {
53 OnFadeInComplete();
54 return;
57 int delta = show ? kOpaqueAlpha - transparency() : transparency();
58 double shift =
59 static_cast<double>(delta) * kFadingTimeoutMs / time_ms;
60 if (shift > delta)
61 alpha_shift_ = delta;
62 else
63 alpha_shift_ = static_cast<int>(ceil(shift));
65 if (alpha_shift_ == 0)
66 alpha_shift_ = 1;
68 // If disabling, make alpha shift negative.
69 if (!show)
70 alpha_shift_ = -alpha_shift_;
72 timer_id_ = owner()->ScheduleTimer(id(), kFadingTimeoutMs);
75 } // namespace chrome_pdf