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 "pdf/fading_control.h" 6 7#include <math.h> 8 9#include "base/logging.h" 10#include "pdf/draw_utils.h" 11#include "pdf/resource_consts.h" 12 13namespace chrome_pdf { 14 15FadingControl::FadingControl() 16 : alpha_shift_(0), timer_id_(0) { 17} 18 19FadingControl::~FadingControl() { 20} 21 22void 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; 29 } 30 if (new_alpha >= kOpaqueAlpha) { 31 AdjustTransparency(kOpaqueAlpha, true); 32 OnFadeInComplete(); 33 return; 34 } 35 36 AdjustTransparency(static_cast<uint8>(new_alpha), true); 37 timer_id_ = owner()->ScheduleTimer(id(), kFadingTimeoutMs); 38 } 39} 40 41// Fade In/Out control depending on visible flag over the time of time_ms. 42void 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(); 51 } 52 if (transparency() == kOpaqueAlpha && show) { 53 OnFadeInComplete(); 54 return; 55 } 56 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)); 64 65 if (alpha_shift_ == 0) 66 alpha_shift_ = 1; 67 68 // If disabling, make alpha shift negative. 69 if (!show) 70 alpha_shift_ = -alpha_shift_; 71 72 timer_id_ = owner()->ScheduleTimer(id(), kFadingTimeoutMs); 73} 74 75} // namespace chrome_pdf 76