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 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290
|
// 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 "ash/controls/rounded_scroll_bar.h"
#include <limits>
#include "ash/public/cpp/style/color_provider.h"
#include "ash/style/ash_color_id.h"
#include "base/functional/bind.h"
#include "base/memory/raw_ptr.h"
#include "base/numerics/ranges.h"
#include "base/time/time.h"
#include "cc/paint/paint_flags.h"
#include "ui/base/metadata/metadata_impl_macros.h"
#include "ui/chromeos/styles/cros_tokens_color_mappings.h"
#include "ui/compositor/layer.h"
#include "ui/compositor/scoped_layer_animation_settings.h"
#include "ui/gfx/canvas.h"
#include "ui/gfx/geometry/dip_util.h"
#include "ui/gfx/scoped_canvas.h"
#include "ui/views/controls/button/button.h"
#include "ui/views/controls/scrollbar/base_scroll_bar_thumb.h"
namespace ash {
namespace {
// Thickness of scroll bar thumb.
constexpr int kScrollThumbThicknessDp = 8;
constexpr int kScrollThumbThicknessHoverInsets = 2;
constexpr int kScrollThumbOutlineTickness = 1;
// How long for the scrollbar to hide after no scroll events have been received?
constexpr base::TimeDelta kScrollThumbHideTimeout = base::Milliseconds(500);
// How long for the scrollbar to fade away?
constexpr base::TimeDelta kScrollThumbFadeDuration = base::Milliseconds(240);
// Opacity values from go/semantic-color-system for "Scrollbar".
constexpr float kActiveOpacity = 1.0f;
// The active state is when the thumb is hovered or pressed.
bool IsActiveState(views::Button::ButtonState state) {
return state == views::Button::STATE_HOVERED ||
state == views::Button::STATE_PRESSED;
}
// Draws a fully rounded rectangle filling in the given bounds.
void DrawFullyRoundedRect(gfx::Canvas* canvas,
const gfx::RectF& bounds,
const cc::PaintFlags& flags) {
const SkScalar corner_radius = std::min(bounds.width(), bounds.height()) / 2;
SkPath rounded_rect;
rounded_rect.addRoundRect(gfx::RectFToSkRect(bounds), corner_radius,
corner_radius);
canvas->DrawPath(rounded_rect, flags);
}
} // namespace
// A scroll bar "thumb" that paints itself with rounded ends.
class RoundedScrollBar::Thumb : public views::BaseScrollBarThumb {
public:
explicit Thumb(RoundedScrollBar* scroll_bar)
: BaseScrollBarThumb(scroll_bar), scroll_bar_(scroll_bar) {}
Thumb(const Thumb&) = delete;
Thumb& operator=(const Thumb&) = delete;
~Thumb() override = default;
bool ShouldPaintAsActive() const {
// The thumb is active during hover and also when the user is dragging the
// thumb with the mouse. In the latter case, the mouse might be outside the
// scroll bar, due to mouse capture.
return IsActiveState(GetState());
}
int GetThumbThickness() const {
if (ShouldPaintAsActive()) {
return kScrollThumbThicknessDp;
}
return kScrollThumbThicknessDp - kScrollThumbThicknessHoverInsets;
}
// views::BaseScrollBarThumb:
gfx::Size CalculatePreferredSize(
const views::SizeBounds& /*available_size*/) const override {
const int thickness = GetThumbThickness();
return gfx::Size(thickness, thickness);
}
void OnPaint(gfx::Canvas* canvas) override {
// Scale bounds with device scale factor to make sure border bounds match
// thumb bounds.
gfx::ScopedCanvas scoped_canvas(canvas);
const float dsf = canvas->UndoDeviceScaleFactor();
const gfx::RectF local_bounds =
gfx::ConvertRectToPixels(GetLocalBounds(), dsf);
gfx::RectF thumb_bounds(local_bounds);
// Can be nullptr in tests.
auto* color_provider = GetColorProvider();
// Paint outline.
cc::PaintFlags stroke_flags;
stroke_flags.setStyle(cc::PaintFlags::kStroke_Style);
if (color_provider) {
stroke_flags.setColor(
color_provider->GetColor(cros_tokens::kCrosSysScrollbarBorder));
}
stroke_flags.setStrokeWidth(kScrollThumbOutlineTickness);
stroke_flags.setAntiAlias(true);
gfx::RectF border_bounds = local_bounds;
border_bounds.Inset(kScrollThumbOutlineTickness / 2.0f);
DrawFullyRoundedRect(canvas, border_bounds, stroke_flags);
thumb_bounds.Inset(kScrollThumbOutlineTickness);
// Paint thumb.
cc::PaintFlags fill_flags;
fill_flags.setStyle(cc::PaintFlags::kFill_Style);
fill_flags.setAntiAlias(true);
if (color_provider) {
fill_flags.setColor(color_provider->GetColor(
ShouldPaintAsActive() ? cros_tokens::kCrosSysScrollbarHover
: cros_tokens::kCrosSysScrollbar));
}
DrawFullyRoundedRect(canvas, thumb_bounds, fill_flags);
}
void OnBoundsChanged(const gfx::Rect& previous_bounds) override {
scroll_bar_->OnThumbBoundsChanged();
}
void OnStateChanged() override {
scroll_bar_->OnThumbStateChanged(current_state_);
current_state_ = GetState();
}
private:
const raw_ptr<RoundedScrollBar> scroll_bar_;
views::Button::ButtonState current_state_ = views::Button::STATE_NORMAL;
};
RoundedScrollBar::RoundedScrollBar(Orientation orientation)
: ScrollBar(orientation),
thumb_(new Thumb(this)), // Owned by views hierarchy.
hide_scrollbar_timer_(
FROM_HERE,
kScrollThumbHideTimeout,
base::BindRepeating(&RoundedScrollBar::HideScrollBar,
base::Unretained(this))) {
// Moving the mouse directly into the thumb will also notify this view.
SetNotifyEnterExitOnChild(true);
SetThumb(thumb_);
thumb_->SetPaintToLayer();
thumb_->layer()->SetFillsBoundsOpaquely(false);
// The thumb is hidden by default.
thumb_->layer()->SetOpacity(0.f);
}
RoundedScrollBar::~RoundedScrollBar() = default;
void RoundedScrollBar::SetInsets(const gfx::Insets& insets) {
insets_ = insets;
}
void RoundedScrollBar::SetSnapBackOnDragOutside(bool snap) {
thumb_->SetSnapBackOnDragOutside(snap);
}
void RoundedScrollBar::SetShowOnThumbBoundsChanged(bool show) {
show_on_thumb_bounds_changed_ = show;
}
gfx::Rect RoundedScrollBar::GetTrackBounds() const {
gfx::Rect bounds = GetLocalBounds();
bounds.Inset(insets_);
return bounds;
}
bool RoundedScrollBar::OverlapsContent() const {
return true;
}
int RoundedScrollBar::GetThickness() const {
// Extend the thickness by the insets on the sides of the bar.
const int sides = GetOrientation() == Orientation::kHorizontal
? insets_.top() + insets_.bottom()
: insets_.left() + insets_.right();
return thumb_->GetThumbThickness() + sides;
}
void RoundedScrollBar::OnMouseEntered(const ui::MouseEvent& event) {
ShowScrollbar();
}
void RoundedScrollBar::OnMouseExited(const ui::MouseEvent& event) {
if (!hide_scrollbar_timer_.IsRunning() && !always_show_thumb_) {
hide_scrollbar_timer_.Reset();
}
}
void RoundedScrollBar::ScrollToPosition(int position) {
ShowScrollbar();
views::ScrollBar::ScrollToPosition(position);
}
void RoundedScrollBar::ObserveScrollEvent(const ui::ScrollEvent& event) {
// Scroll fling events are generated by moving a single finger over the
// trackpad; do not show the scrollbar for these events.
if (event.type() == ui::EventType::kScrollFlingCancel) {
return;
}
ShowScrollbar();
}
void RoundedScrollBar::SetAlwaysShowThumb(bool always_show_thumb) {
always_show_thumb_ = always_show_thumb;
if (always_show_thumb_) {
hide_scrollbar_timer_.Stop();
ShowScrollbar();
return;
}
hide_scrollbar_timer_.Reset();
}
views::BaseScrollBarThumb* RoundedScrollBar::GetThumbForTest() const {
return thumb_;
}
void RoundedScrollBar::ShowScrollbar() {
if (!IsMouseHovered() && !always_show_thumb_) {
hide_scrollbar_timer_.Reset();
}
const float target_opacity = kActiveOpacity;
if (base::IsApproximatelyEqual(thumb_->layer()->GetTargetOpacity(),
target_opacity,
std::numeric_limits<float>::epsilon())) {
return;
}
ui::ScopedLayerAnimationSettings animation(thumb_->layer()->GetAnimator());
animation.SetTransitionDuration(kScrollThumbFadeDuration);
thumb_->layer()->SetOpacity(target_opacity);
}
void RoundedScrollBar::HideScrollBar() {
// Never hide the scrollbar if the mouse is over it. The auto-hide timer
// will be reset when the mouse leaves the scrollable area.
if (IsMouseHovered() || always_show_thumb_) {
return;
}
hide_scrollbar_timer_.Stop();
ui::ScopedLayerAnimationSettings animation(thumb_->layer()->GetAnimator());
animation.SetTransitionDuration(kScrollThumbFadeDuration);
thumb_->layer()->SetOpacity(0.f);
}
void RoundedScrollBar::OnThumbStateChanged(
views::Button::ButtonState old_state) {
// Update the scroll bar track and thumb bounds as needed. This won't
// re-layout the scroll contents since the scroll bar overlaps the contents.
if (IsActiveState(old_state) != thumb_->ShouldPaintAsActive()) {
PreferredSizeChanged();
}
// If the mouse is still in the scroll bar, the thumb hover state may have
// changed, so recompute opacity.
if (IsMouseHovered()) {
ShowScrollbar();
}
}
void RoundedScrollBar::OnThumbBoundsChanged() {
// Optionally show the scroll bar on thumb bounds changes (e.g. keyboard
// driven scroll position changes).
if (show_on_thumb_bounds_changed_) {
ShowScrollbar();
}
}
BEGIN_METADATA(RoundedScrollBar)
END_METADATA
} // namespace ash
|