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 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275
|
// Copyright 2021 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "ui/native_theme/scrollbar_animator_mac.h"
#include <algorithm>
#include "base/feature_list.h"
#include "base/task/single_thread_task_runner.h"
#include "ui/native_theme/features/native_theme_features.h"
namespace ui {
///////////////////////////////////////////////////////////////////////////////
// ScrollbarAnimationTimerMac
ScrollbarAnimationTimerMac::ScrollbarAnimationTimerMac(
base::RepeatingCallback<void(double)> callback,
base::TimeDelta duration,
scoped_refptr<base::SingleThreadTaskRunner> task_runner)
: duration_(duration), callback_(std::move(callback)) {
timing_function_ = gfx::CubicBezierTimingFunction::CreatePreset(
gfx::CubicBezierTimingFunction::EaseType::EASE_IN_OUT);
}
ScrollbarAnimationTimerMac::~ScrollbarAnimationTimerMac() {}
void ScrollbarAnimationTimerMac::Start() {
start_time_ = base::TimeTicks::Now();
// Set the framerate of the animation. NSAnimation uses a default
// framerate of 60 Hz, so use that here.
timer_.Start(FROM_HERE, base::Seconds(1.0 / 60.0), this,
&ScrollbarAnimationTimerMac::TimerFired);
}
void ScrollbarAnimationTimerMac::Stop() {
timer_.Stop();
}
void ScrollbarAnimationTimerMac::SetDuration(base::TimeDelta duration) {
duration_ = duration;
}
void ScrollbarAnimationTimerMac::TimerFired() {
base::TimeTicks current_time = base::TimeTicks::Now();
base::TimeDelta delta = current_time - start_time_;
if (delta >= duration_) {
timer_.Stop();
}
double fraction = delta / duration_;
fraction = std::clamp(fraction, 0.0, 1.0);
double progress = timing_function_->GetValue(fraction);
// Note that `this` may be destroyed from within `callback_`, so it is not
// safe to call any other code after it.
callback_.Run(progress);
}
///////////////////////////////////////////////////////////////////////////////
// OverlayScrollbarAnimatorMac
OverlayScrollbarAnimatorMac::OverlayScrollbarAnimatorMac(
Client* client,
int thumb_width_expanded,
int thumb_width_unexpanded,
scoped_refptr<base::SingleThreadTaskRunner> task_runner)
: client_(client),
thumb_width_expanded_(thumb_width_expanded),
thumb_width_unexpanded_(thumb_width_unexpanded),
thumb_width_(thumb_width_unexpanded),
animations_enabled_(
base::FeatureList::IsEnabled(features::kScrollbarAnimations)),
task_runner_(task_runner),
weak_factory_(this) {}
OverlayScrollbarAnimatorMac::~OverlayScrollbarAnimatorMac() = default;
void OverlayScrollbarAnimatorMac::MouseDidEnter() {
// If the scrollbar is completely hidden, ignore this. We will initialize
// the `mouse_in_track_` state if there's a scroll.
if (thumb_alpha_ == 0.f) {
return;
}
if (mouse_in_track_) {
return;
}
mouse_in_track_ = true;
// Cancel any in-progress fade-out, and ensure that the fade-out timer be
// disabled.
if (fade_out_animation_) {
FadeOutAnimationCancel();
}
FadeOutTimerUpdate();
// Start the fade-in animation (unless it is in progress or has already
// completed).
if (!fade_in_track_animation_ && track_alpha_ != 1.f) {
FadeInTrackAnimationStart();
}
// Start the expand-thumb animation (unless it is in progress or has already
// completed).
if (!expand_thumb_animation_ && thumb_width_ != thumb_width_expanded_) {
ExpandThumbAnimationStart();
}
}
void OverlayScrollbarAnimatorMac::MouseDidExit() {
// Ensure that the fade-out timer be re-enabled.
mouse_in_track_ = false;
FadeOutTimerUpdate();
}
void OverlayScrollbarAnimatorMac::DidScroll() {
// If we were fading out, then immediately return to being fully opaque for
// both the thumb and track.
if (fade_out_animation_) {
FadeOutAnimationCancel();
FadeOutTimerUpdate();
return;
}
// If the scrollbar was already fully visible, then just update the fade-out
// timer.
if (thumb_alpha_ == 1.f) {
FadeOutTimerUpdate();
return;
}
// This is an initial scroll causing the thumb to appear.
DCHECK_EQ(thumb_width_, thumb_width_unexpanded_);
DCHECK_EQ(thumb_alpha_, 0.f);
DCHECK(!fade_in_track_animation_);
thumb_width_ = thumb_width_unexpanded_;
thumb_alpha_ = 1;
client_->SetThumbNeedsDisplay();
client_->SetHidden(false);
// If the initial scroll is done inside the scrollbar's area, then also
// cause the track to appear and the thumb to enlarge.
if (client_->IsMouseInScrollbarFrameRect()) {
mouse_in_track_ = true;
thumb_width_ = thumb_width_expanded_;
track_alpha_ = 1;
client_->SetTrackNeedsDisplay();
}
// Update the fade-out timer (now that we know whether or not the mouse
// is in the track).
FadeOutTimerUpdate();
}
void OverlayScrollbarAnimatorMac::ExpandThumbAnimationStart() {
DCHECK(!expand_thumb_animation_);
DCHECK_NE(thumb_width_, thumb_width_expanded_);
expand_thumb_animation_ = std::make_unique<ScrollbarAnimationTimerMac>(
base::BindRepeating(
&OverlayScrollbarAnimatorMac::ExpandThumbAnimationTicked,
weak_factory_.GetWeakPtr()),
animations_enabled_ ? kAnimationDuration : base::TimeDelta(),
task_runner_);
expand_thumb_animation_->Start();
}
void OverlayScrollbarAnimatorMac::ExpandThumbAnimationTicked(double progress) {
thumb_width_ = (1 - progress) * thumb_width_unexpanded_ +
progress * thumb_width_expanded_;
client_->SetThumbNeedsDisplay();
if (progress == 1) {
expand_thumb_animation_.reset();
}
}
void OverlayScrollbarAnimatorMac::FadeInTrackAnimationStart() {
DCHECK(!fade_in_track_animation_);
DCHECK(!fade_out_animation_);
fade_in_track_animation_ = std::make_unique<ScrollbarAnimationTimerMac>(
base::BindRepeating(
&OverlayScrollbarAnimatorMac::FadeInTrackAnimationTicked,
weak_factory_.GetWeakPtr()),
animations_enabled_ ? kAnimationDuration : base::TimeDelta(),
task_runner_);
fade_in_track_animation_->Start();
}
void OverlayScrollbarAnimatorMac::FadeInTrackAnimationTicked(double progress) {
// Fade-in and fade-out are mutually exclusive.
DCHECK(!fade_out_animation_);
track_alpha_ = progress;
client_->SetTrackNeedsDisplay();
if (progress == 1) {
fade_in_track_animation_.reset();
}
}
void OverlayScrollbarAnimatorMac::FadeOutTimerUpdate() {
if (mouse_in_track_) {
start_scrollbar_fade_out_timer_.reset();
return;
}
// If animations aren't enabled, we will only hide the scrollbar if the
// mouse was previously over it.
if (!animations_enabled_ && thumb_width_ != thumb_width_expanded_) {
return;
}
if (!start_scrollbar_fade_out_timer_) {
start_scrollbar_fade_out_timer_ =
std::make_unique<base::RetainingOneShotTimer>(
FROM_HERE, animations_enabled_ ? kFadeOutDelay : base::TimeDelta(),
base::BindRepeating(
&OverlayScrollbarAnimatorMac::FadeOutAnimationStart,
weak_factory_.GetWeakPtr()));
start_scrollbar_fade_out_timer_->SetTaskRunner(task_runner_);
}
start_scrollbar_fade_out_timer_->Reset();
}
void OverlayScrollbarAnimatorMac::FadeOutAnimationStart() {
start_scrollbar_fade_out_timer_.reset();
fade_in_track_animation_.reset();
fade_out_animation_.reset();
fade_out_animation_ = std::make_unique<ScrollbarAnimationTimerMac>(
base::BindRepeating(&OverlayScrollbarAnimatorMac::FadeOutAnimationTicked,
weak_factory_.GetWeakPtr()),
animations_enabled_ ? kAnimationDuration : base::TimeDelta(),
task_runner_);
fade_out_animation_->Start();
}
void OverlayScrollbarAnimatorMac::FadeOutAnimationTicked(double progress) {
DCHECK(!fade_in_track_animation_);
// Fade out the thumb.
thumb_alpha_ = 1 - progress;
client_->SetThumbNeedsDisplay();
// If the track is not already invisible, fade it out.
if (track_alpha_ != 0) {
track_alpha_ = 1 - progress;
client_->SetTrackNeedsDisplay();
}
// Once completely faded out, reset all state.
if (progress == 1) {
expand_thumb_animation_.reset();
fade_out_animation_.reset();
thumb_width_ = thumb_width_unexpanded_;
DCHECK_EQ(thumb_alpha_, 0.f);
DCHECK_EQ(track_alpha_, 0.f);
// Mark that the scrollbars were hidden.
client_->SetHidden(true);
}
}
void OverlayScrollbarAnimatorMac::FadeOutAnimationCancel() {
DCHECK(fade_out_animation_);
fade_out_animation_.reset();
thumb_alpha_ = 1;
client_->SetThumbNeedsDisplay();
if (track_alpha_ > 0) {
track_alpha_ = 1;
client_->SetTrackNeedsDisplay();
}
}
} // namespace ui
|