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 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375
|
// Copyright 2024 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/glanceables/common/glanceables_contents_scroll_view.h"
#include <limits>
#include <optional>
#include "ash/controls/rounded_scroll_bar.h"
#include "ash/glanceables/common/glanceables_time_management_bubble_view.h"
#include "ash/glanceables/common/glanceables_view_id.h"
#include "base/time/time.h"
#include "base/timer/timer.h"
#include "base/types/cxx23_to_underlying.h"
#include "ui/base/metadata/metadata_impl_macros.h"
#include "ui/events/event.h"
#include "ui/events/types/event_type.h"
#include "ui/views/controls/scrollbar/base_scroll_bar_thumb.h"
#include "ui/views/layout/flex_layout_types.h"
namespace ash {
namespace {
constexpr base::TimeDelta kMouseWheelOverscrollDelay = base::Milliseconds(150);
constexpr base::TimeDelta kScrollLockDelay = base::Milliseconds(400);
} // namespace
class GlanceablesContentsScrollView::ScrollBar : public RoundedScrollBar {
METADATA_HEADER(ScrollBar, RoundedScrollBar)
public:
explicit ScrollBar(TimeManagementContext context)
: RoundedScrollBar(Orientation::kVertical),
time_management_context_(context),
mouse_wheel_overscroll_timer_(
FROM_HERE,
kMouseWheelOverscrollDelay,
base::BindRepeating(&ScrollBar::OnMouseWheelTimerFired,
base::Unretained(this))) {}
ScrollBar(const ScrollBar&) = delete;
ScrollBar& operator=(const ScrollBar&) = delete;
~ScrollBar() override = default;
// views::ScrollBar:
void OnGestureEvent(ui::GestureEvent* event) override {
// For simplicity, reset the timer for mouse wheel event so that
// overscrolling check with mouse wheel always triggers by itself.
ResetMouseWheelTimer();
switch (event->type()) {
case ui::EventType::kGestureScrollBegin:
// Check if the position is at the max/min position at the start of the
// scroll event.
if (!GetVisible()) {
// If the scrollbar is not visible, which means that the scroll view
// is not scrollable, consider the scroll bar is at the max/min
// position at the same time.
scroll_starts_at_max_position_ = true;
scroll_starts_at_min_position_ = true;
} else {
scroll_starts_at_min_position_ = GetPosition() == 0;
// `GetMaxPosition()` in ScrollBar has different "position"
// definitions from `GetPosition()`. Calculate the max position of
// the thumb in the scrollbar for comparisons.
scroll_starts_at_max_position_ =
GetPosition() ==
GetTrackBounds().height() - GetThumb()->GetLength();
}
break;
case ui::EventType::kGestureScrollUpdate:
switch (time_management_context_) {
case TimeManagementContext::kTasks: {
// Note that max position is at the bottom of the scrollbar, while
// the event y offset is increasing upward.
const bool start_overscrolling_downward =
scroll_starts_at_max_position_ &&
event->details().scroll_y() < 0;
if (start_overscrolling_downward) {
on_overscroll_callback_.Run();
}
} break;
case TimeManagementContext::kClassroom: {
const bool start_overscrolling_upward =
scroll_starts_at_min_position_ &&
event->details().scroll_y() > 0;
if (start_overscrolling_upward) {
on_overscroll_callback_.Run();
}
} break;
}
// Reset the variables for the next scroll event.
scroll_starts_at_max_position_ = false;
scroll_starts_at_min_position_ = false;
break;
default:
// Reset the variables for the next scroll event.
scroll_starts_at_max_position_ = false;
scroll_starts_at_min_position_ = false;
break;
}
RoundedScrollBar::OnGestureEvent(event);
}
bool OnMouseWheel(const ui::MouseWheelEvent& event) override {
const bool scroll_toward_classroom =
time_management_context_ == TimeManagementContext::kTasks &&
event.y_offset() < 0;
const bool scroll_toward_tasks =
time_management_context_ == TimeManagementContext::kClassroom &&
event.y_offset() > 0;
// Handle the case when the scroll view is not scrollable.
if (!GetVisible()) {
if (scroll_toward_classroom || scroll_toward_tasks) {
on_overscroll_callback_.Run();
return true;
} else {
return RoundedScrollBar::OnMouseWheel(event);
}
}
const bool is_at_min_position = GetPosition() == 0;
const bool is_at_max_position =
GetPosition() == GetTrackBounds().height() - GetThumb()->GetLength();
bool scroll_to_other_bubble =
(scroll_toward_classroom && is_at_max_position) ||
(scroll_toward_tasks && is_at_min_position);
if (!scroll_to_other_bubble) {
// Reset timer if the event is not scrolling to the other time management
// bubble.
ResetMouseWheelTimer();
return RoundedScrollBar::OnMouseWheel(event);
}
if (can_overscroll_for_mouse_wheel_) {
// Trigger `on_overscroll_callback_` if the timer is fired.
on_overscroll_callback_.Run();
ResetMouseWheelTimer();
return true;
}
if (!mouse_wheel_overscroll_timer_.IsRunning()) {
// Start the timer if the mouse wheel is scrolling toward the other time
// management bubble and the timer wasn't fired before.
mouse_wheel_overscroll_timer_.Reset();
}
return RoundedScrollBar::OnMouseWheel(event);
}
bool MaybeHandleScrollEvent(ui::ScrollEvent* event) {
// For simplicity, reset the timer for mouse wheel event so that
// overscrolling with mouse wheel always triggers by itself.
ResetMouseWheelTimer();
switch (event->type()) {
case ui::EventType::kScrollFlingCancel:
// Check if the position is at the max/min position at the start of the
// scroll event.
if (!GetVisible()) {
// If the scrollbar is not visible, which means that the scroll view
// is not scrollable, consider the scroll bar is at the max/min
// position at the same time.
scroll_starts_at_max_position_ = true;
scroll_starts_at_min_position_ = true;
} else {
scroll_starts_at_min_position_ = GetPosition() == 0;
// `GetMaxPosition()` in ScrollBar has different "position"
// definitions from `GetPosition()`. Calculate the max position of
// the thumb in the scrollbar for comparisons.
scroll_starts_at_max_position_ =
GetPosition() ==
GetTrackBounds().height() - GetThumb()->GetLength();
}
break;
case ui::EventType::kScroll:
switch (time_management_context_) {
case TimeManagementContext::kTasks: {
// Note that max position is at the bottom of the scrollbar, while
// the event y offset is increasing upward.
const bool start_overscrolling_downward =
scroll_starts_at_max_position_ && event->y_offset() < 0;
if (start_overscrolling_downward) {
on_overscroll_callback_.Run();
return true;
}
} break;
case TimeManagementContext::kClassroom: {
const bool start_overscrolling_upward =
scroll_starts_at_min_position_ && event->y_offset() > 0;
if (start_overscrolling_upward) {
on_overscroll_callback_.Run();
return true;
}
} break;
}
// Reset the variables for the next scroll event.
scroll_starts_at_max_position_ = false;
scroll_starts_at_min_position_ = false;
break;
default:
// Reset the variables for the next scroll event.
scroll_starts_at_max_position_ = false;
scroll_starts_at_min_position_ = false;
break;
}
return false;
}
void SetOnOverscrollCallback(const base::RepeatingClosure& callback) {
on_overscroll_callback_ = std::move(callback);
}
void FireMouseWheelTimerForTest() {
if (mouse_wheel_overscroll_timer_.IsRunning()) {
mouse_wheel_overscroll_timer_.Stop();
OnMouseWheelTimerFired();
}
}
private:
void OnMouseWheelTimerFired() { can_overscroll_for_mouse_wheel_ = true; }
void ResetMouseWheelTimer() {
can_overscroll_for_mouse_wheel_ = false;
mouse_wheel_overscroll_timer_.Stop();
}
// Whether the glanceables that owns this scroll view is Tasks or Classroom.
// This will determine the overscroll behavior that triggers
// `on_overscroll_callback_`.
const TimeManagementContext time_management_context_;
// Whether the scroll bar is at its max position, which is bottom in this
// case, when a scroll event started.
bool scroll_starts_at_max_position_ = false;
// Whether the scroll bar is at its min position, which is top in this
// case, when a scroll event started.
bool scroll_starts_at_min_position_ = false;
// Called when the user attempts to overscroll to the direction that has
// another glanceables. Overscroll here means to either scroll down from the
// bottom of the scroll view, or scroll up from the top of the scroll view.
base::RepeatingClosure on_overscroll_callback_ = base::DoNothing();
// As there is no explicit "start" and "end" of a mouse wheel scroll action, a
// timer is used to determine if a mouse scroll should trigger
// `on_overscroll_callback_`. Once the timer is fired, set
// `can_overscroll_for_mouse_wheel_` to true to trigger
// `on_overscroll_callback_` in subsequent mouse scroll events.
base::RetainingOneShotTimer mouse_wheel_overscroll_timer_;
bool can_overscroll_for_mouse_wheel_ = false;
};
BEGIN_METADATA(GlanceablesContentsScrollView, ScrollBar)
END_METADATA
GlanceablesContentsScrollView::GlanceablesContentsScrollView(
TimeManagementContext context)
: scroll_lock_timer_(
FROM_HERE,
kScrollLockDelay,
base::BindRepeating(
&GlanceablesContentsScrollView::OnScrollLockTimerFired,
base::Unretained(this))) {
auto unique_scroll_bar = std::make_unique<ScrollBar>(context);
scroll_bar_ = unique_scroll_bar.get();
SetVerticalScrollBar(std::move(unique_scroll_bar));
SetID(base::to_underlying(GlanceablesViewId::kContentsScrollView));
SetProperty(views::kFlexBehaviorKey,
views::FlexSpecification(views::MinimumFlexSizeRule::kScaleToZero,
views::MaximumFlexSizeRule::kUnbounded)
.WithWeight(1));
ClipHeightTo(0, std::numeric_limits<int>::max());
SetBackgroundColor(std::nullopt);
SetDrawOverflowIndicator(false);
}
void GlanceablesContentsScrollView::SetOnOverscrollCallback(
const base::RepeatingClosure& callback) {
scroll_bar_->SetOnOverscrollCallback(std::move(callback));
}
void GlanceablesContentsScrollView::LockScroll() {
if (scroll_lock_) {
return;
}
scroll_lock_ = true;
scroll_lock_timer_.Reset();
}
void GlanceablesContentsScrollView::UnlockScroll() {
scroll_lock_ = false;
scroll_lock_timer_.Stop();
}
void GlanceablesContentsScrollView::FireScrollLockTimerForTest() {
scroll_lock_timer_.Stop();
OnScrollLockTimerFired();
}
void GlanceablesContentsScrollView::FireMouseWheelTimerForTest() {
scroll_bar_->FireMouseWheelTimerForTest(); // IN-TEST
}
void GlanceablesContentsScrollView::OnScrollEvent(ui::ScrollEvent* event) {
views::ScrollView::OnScrollEvent(event);
bool handled = scroll_bar_->MaybeHandleScrollEvent(event);
if (handled) {
event->SetHandled();
event->StopPropagation();
}
// Not handling `scroll_lock_` as unhandled scroll event will be transferred
// to a mouse wheel event in `Widget`.
}
bool GlanceablesContentsScrollView::OnMouseWheel(const ui::MouseWheelEvent& e) {
if (scroll_lock_ && scroll_bar_->GetVisible()) {
// Lock the scrolling by returning true without processing the event.
return true;
}
// Always pass the event to `scroll_bar_` to check the overscroll.
return scroll_bar_->OnMouseWheel(e);
}
void GlanceablesContentsScrollView::OnGestureEvent(ui::GestureEvent* event) {
// views::ScrollView::OnGestureEvent only processes the scroll event when the
// scroll bar is visible and the scroll view is scrollable, but the overscroll
// behavior also needs to consider the case where the scroll view is not
// scrollable.
bool scroll_event = event->type() == ui::EventType::kGestureScrollUpdate ||
event->type() == ui::EventType::kGestureScrollBegin ||
event->type() == ui::EventType::kGestureScrollEnd ||
event->type() == ui::EventType::kScrollFlingStart;
if (!scroll_bar_->GetVisible() && scroll_event) {
scroll_bar_->OnGestureEvent(event);
return;
}
views::ScrollView::OnGestureEvent(event);
}
void GlanceablesContentsScrollView::ChildPreferredSizeChanged(
views::View* view) {
PreferredSizeChanged();
}
void GlanceablesContentsScrollView::OnScrollLockTimerFired() {
scroll_lock_ = false;
}
BEGIN_METADATA(GlanceablesContentsScrollView)
END_METADATA
} // namespace ash
|