forked from StarCrossPortal/bug-hunting-101
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mac_scrollbar_animator_impl.mm
871 lines (730 loc) · 29.9 KB
/
mac_scrollbar_animator_impl.mm
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
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
// Copyright 2021 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "third_party/blink/renderer/core/scroll/mac_scrollbar_animator_impl.h"
#import <AppKit/AppKit.h>
#include "base/mac/scoped_nsobject.h"
#include "third_party/blink/public/platform/platform.h"
#include "third_party/blink/renderer/core/scroll/ns_scroller_imp_details.h"
#include "third_party/blink/renderer/core/scroll/scroll_animator.h"
#include "third_party/blink/renderer/core/scroll/scrollbar_theme_mac.h"
#include "third_party/blink/renderer/platform/animation/timing_function.h"
#include "third_party/blink/renderer/platform/mac/block_exceptions.h"
#include "third_party/blink/renderer/platform/scheduler/public/thread_scheduler.h"
namespace {
bool SupportsUIStateTransitionProgress() {
// FIXME: This is temporary until all platforms that support ScrollbarPainter
// support this part of the API.
static bool global_supports_ui_state_transition_progress =
[NSClassFromString(@"NSScrollerImp")
instancesRespondToSelector:@selector(mouseEnteredScroller)];
return global_supports_ui_state_transition_progress;
}
bool SupportsExpansionTransitionProgress() {
static bool global_supports_expansion_transition_progress =
[NSClassFromString(@"NSScrollerImp")
instancesRespondToSelector:@selector(expansionTransitionProgress)];
return global_supports_expansion_transition_progress;
}
bool SupportsContentAreaScrolledInDirection() {
static bool global_supports_content_area_scrolled_in_direction =
[NSClassFromString(@"NSScrollerImpPair")
instancesRespondToSelector:@selector
(contentAreaScrolledInDirection:)];
return global_supports_content_area_scrolled_in_direction;
}
blink::ScrollbarThemeMac* MacOverlayScrollbarTheme(
blink::ScrollbarTheme& scrollbar_theme) {
return !scrollbar_theme.IsMockTheme()
? static_cast<blink::ScrollbarThemeMac*>(&scrollbar_theme)
: nil;
}
ScrollbarPainter ScrollbarPainterForScrollbar(blink::Scrollbar& scrollbar) {
if (blink::ScrollbarThemeMac* scrollbar_theme =
MacOverlayScrollbarTheme(scrollbar.GetTheme()))
return scrollbar_theme->PainterForScrollbar(scrollbar);
return nil;
}
} // namespace
// This class is a delegator of ScrollbarPainterController to ScrollableArea
// that has the scrollbars of a ScrollbarPainter.
@interface BlinkScrollbarPainterControllerDelegate : NSObject {
blink::ScrollableArea* _scrollableArea;
}
- (id)initWithScrollableArea:(blink::ScrollableArea*)scrollableArea;
@end
@implementation BlinkScrollbarPainterControllerDelegate
- (id)initWithScrollableArea:(blink::ScrollableArea*)scrollableArea {
self = [super init];
if (!self)
return nil;
_scrollableArea = scrollableArea;
return self;
}
- (void)invalidate {
_scrollableArea = 0;
}
- (NSRect)contentAreaRectForScrollerImpPair:(id)scrollerImpPair {
if (!_scrollableArea)
return NSZeroRect;
blink::IntSize contentsSize = _scrollableArea->ContentsSize();
return NSMakeRect(0, 0, contentsSize.Width(), contentsSize.Height());
}
- (BOOL)inLiveResizeForScrollerImpPair:(id)scrollerImpPair {
return NO;
}
- (NSPoint)mouseLocationInContentAreaForScrollerImpPair:(id)scrollerImpPair {
if (!_scrollableArea)
return NSZeroPoint;
return _scrollableArea->LastKnownMousePosition();
}
- (NSPoint)scrollerImpPair:(id)scrollerImpPair
convertContentPoint:(NSPoint)pointInContentArea
toScrollerImp:(id)scrollerImp {
if (!_scrollableArea || !scrollerImp)
return NSZeroPoint;
blink::Scrollbar* scrollbar = nil;
if ([scrollerImp isHorizontal])
scrollbar = _scrollableArea->HorizontalScrollbar();
else
scrollbar = _scrollableArea->VerticalScrollbar();
// It is possible to have a null scrollbar here since it is possible for this
// delegate method to be called between the moment when a scrollbar has been
// set to 0 and the moment when its destructor has been called.
if (!scrollbar)
return NSZeroPoint;
DCHECK_EQ(scrollerImp, ScrollbarPainterForScrollbar(*scrollbar));
return scrollbar->ConvertFromContainingEmbeddedContentView(
blink::IntPoint(pointInContentArea));
}
- (void)scrollerImpPair:(id)scrollerImpPair
setContentAreaNeedsDisplayInRect:(NSRect)rect {
if (!_scrollableArea)
return;
if (!_scrollableArea->ScrollbarsCanBeActive())
return;
_scrollableArea->ContentAreaWillPaint();
}
- (void)scrollerImpPair:(id)scrollerImpPair
updateScrollerStyleForNewRecommendedScrollerStyle:
(NSScrollerStyle)newRecommendedScrollerStyle {
// Chrome has a single process mode which is used for testing on Mac. In that
// mode, WebKit runs on a thread in the
// browser process. This notification is called by the OS on the main thread
// in the browser process, and not on the
// the WebKit thread. Better to not update the style than crash.
// http://crbug.com/126514
if (!IsMainThread())
return;
if (!_scrollableArea)
return;
[scrollerImpPair setScrollerStyle:newRecommendedScrollerStyle];
_scrollableArea->GetMacScrollbarAnimator()->UpdateScrollerStyle();
}
@end
enum FeatureToAnimate {
ThumbAlpha,
TrackAlpha,
UIStateTransition,
ExpansionTransition
};
@class BlinkScrollbarPartAnimation;
namespace blink {
// This class is used to drive the animation timer for
// BlinkScrollbarPartAnimation
// objects. This is used instead of NSAnimation because CoreAnimation
// establishes connections to the WindowServer, which should not be done in a
// sandboxed renderer process.
class BlinkScrollbarPartAnimationTimer {
public:
BlinkScrollbarPartAnimationTimer(
BlinkScrollbarPartAnimation* animation,
CFTimeInterval duration,
scoped_refptr<base::SingleThreadTaskRunner> task_runner)
: timer_(std::move(task_runner),
this,
&BlinkScrollbarPartAnimationTimer::TimerFired),
start_time_(0.0),
duration_(duration),
animation_(animation),
timing_function_(CubicBezierTimingFunction::Preset(
CubicBezierTimingFunction::EaseType::EASE_IN_OUT)) {}
~BlinkScrollbarPartAnimationTimer() {}
void Start() {
start_time_ = base::Time::Now().ToDoubleT();
// Set the framerate of the animation. NSAnimation uses a default
// framerate of 60 Hz, so use that here.
timer_.StartRepeating(base::TimeDelta::FromSecondsD(1.0 / 60.0), FROM_HERE);
}
void Stop() { timer_.Stop(); }
void SetDuration(CFTimeInterval duration) { duration_ = duration; }
// This is a speculative fix for crbug.com/1183276.
// In BlinkScrollbarPainterDelegate::setUpAlphaAnimation we are
// deallocating BlinkScrollbarPartAnimation and create a new one.
// The problem seems to be with BlinkScrollbarPartAnimation, passing a
// pointer to itself to BlinkScrollbarPartAnimationTimer.
// BlinkScrollbarPartAnimationTimer uses a TaskRunnerTimer to schedule
// the animation to run 60 times second.
// When we deallocate BlinkScrollbarPartAnimation,
// BlinkScrollbarPartAnimationTimer fires again, I believe because it
// uses PostTaskDelayed to schedule the next animation.
// Ideally the timer won't fire again.
void CancelAnimation() { animation_ = nullptr; }
private:
void TimerFired(TimerBase*) {
double current_time = base::Time::Now().ToDoubleT();
double delta = current_time - start_time_;
if (delta >= duration_)
timer_.Stop();
// This is a speculative fix for crbug.com/1183276.
if (!animation_)
return;
double fraction = delta / duration_;
fraction = clampTo(fraction, 0.0, 1.0);
double progress = timing_function_->Evaluate(fraction);
[animation_ setCurrentProgress:progress];
}
TaskRunnerTimer<BlinkScrollbarPartAnimationTimer> timer_;
double start_time_; // In seconds.
double duration_; // In seconds.
BlinkScrollbarPartAnimation* animation_; // Weak, owns this.
scoped_refptr<CubicBezierTimingFunction> timing_function_;
};
} // namespace blink
// This class handles the animation of a |_featureToAnimate| part of
// |_scrollbar|.
@interface BlinkScrollbarPartAnimation : NSObject {
blink::Scrollbar* _scrollbar;
std::unique_ptr<blink::BlinkScrollbarPartAnimationTimer> _timer;
base::scoped_nsobject<ScrollbarPainter> _scrollbarPainter;
FeatureToAnimate _featureToAnimate;
CGFloat _startValue;
CGFloat _endValue;
}
- (id)initWithScrollbar:(blink::Scrollbar*)scrollbar
featureToAnimate:(FeatureToAnimate)featureToAnimate
animateFrom:(CGFloat)startValue
animateTo:(CGFloat)endValue
duration:(NSTimeInterval)duration
taskRunner:(scoped_refptr<base::SingleThreadTaskRunner>)taskRunner;
@end
@implementation BlinkScrollbarPartAnimation
- (id)initWithScrollbar:(blink::Scrollbar*)scrollbar
featureToAnimate:(FeatureToAnimate)featureToAnimate
animateFrom:(CGFloat)startValue
animateTo:(CGFloat)endValue
duration:(NSTimeInterval)duration
taskRunner:
(scoped_refptr<base::SingleThreadTaskRunner>)taskRunner {
self = [super init];
if (!self)
return nil;
_timer = std::make_unique<blink::BlinkScrollbarPartAnimationTimer>(
self, duration, std::move(taskRunner));
_scrollbar = scrollbar;
_featureToAnimate = featureToAnimate;
_startValue = startValue;
_endValue = endValue;
return self;
}
- (void)startAnimation {
DCHECK(_scrollbar);
_scrollbarPainter.reset(ScrollbarPainterForScrollbar(*_scrollbar),
base::scoped_policy::RETAIN);
_timer->Start();
}
- (void)stopAnimation {
_timer->Stop();
}
- (void)setDuration:(CFTimeInterval)duration {
_timer->SetDuration(duration);
}
- (void)setStartValue:(CGFloat)startValue {
_startValue = startValue;
}
- (void)setEndValue:(CGFloat)endValue {
_endValue = endValue;
}
- (void)setCurrentProgress:(NSAnimationProgress)progress {
DCHECK(_scrollbar);
CGFloat currentValue;
if (_startValue > _endValue)
currentValue = 1 - progress;
else
currentValue = progress;
blink::ScrollbarPart invalidParts = blink::kNoPart;
switch (_featureToAnimate) {
case ThumbAlpha:
[_scrollbarPainter setKnobAlpha:currentValue];
break;
case TrackAlpha:
[_scrollbarPainter setTrackAlpha:currentValue];
invalidParts = static_cast<blink::ScrollbarPart>(~blink::kThumbPart);
break;
case UIStateTransition:
[_scrollbarPainter setUiStateTransitionProgress:currentValue];
invalidParts = blink::kAllParts;
break;
case ExpansionTransition:
[_scrollbarPainter setExpansionTransitionProgress:currentValue];
invalidParts = blink::kThumbPart;
break;
}
_scrollbar->SetNeedsPaintInvalidation(invalidParts);
}
- (void)invalidate {
BEGIN_BLOCK_OBJC_EXCEPTIONS;
[self stopAnimation];
END_BLOCK_OBJC_EXCEPTIONS;
_scrollbar = nullptr;
_timer->CancelAnimation();
}
@end
// This class is a delegator of ScrollbarPainter to the 4 types of animation
// it can run. The animations are run through BlinkScrollbarPartAnimation.
@interface BlinkScrollbarPainterDelegate : NSObject <NSAnimationDelegate> {
blink::Scrollbar* _scrollbar;
scoped_refptr<base::SingleThreadTaskRunner> _taskRunner;
base::scoped_nsobject<BlinkScrollbarPartAnimation> _knobAlphaAnimation;
base::scoped_nsobject<BlinkScrollbarPartAnimation> _trackAlphaAnimation;
base::scoped_nsobject<BlinkScrollbarPartAnimation>
_uiStateTransitionAnimation;
base::scoped_nsobject<BlinkScrollbarPartAnimation>
_expansionTransitionAnimation;
BOOL _hasExpandedSinceInvisible;
}
- (id)initWithScrollbar:(blink::Scrollbar*)scrollbar
taskRunner:(scoped_refptr<base::SingleThreadTaskRunner>)taskRunner;
- (void)updateVisibilityImmediately:(bool)show;
- (void)cancelAnimations;
@end
@implementation BlinkScrollbarPainterDelegate
- (id)initWithScrollbar:(blink::Scrollbar*)scrollbar
taskRunner:
(scoped_refptr<base::SingleThreadTaskRunner>)taskRunner {
self = [super init];
if (!self)
return nil;
_scrollbar = scrollbar;
_taskRunner = taskRunner;
return self;
}
- (void)updateVisibilityImmediately:(bool)show {
[self cancelAnimations];
[ScrollbarPainterForScrollbar(*_scrollbar) setKnobAlpha:(show ? 1.0 : 0.0)];
}
- (void)cancelAnimations {
BEGIN_BLOCK_OBJC_EXCEPTIONS;
[_knobAlphaAnimation stopAnimation];
[_trackAlphaAnimation stopAnimation];
[_uiStateTransitionAnimation stopAnimation];
[_expansionTransitionAnimation stopAnimation];
END_BLOCK_OBJC_EXCEPTIONS;
}
- (blink::ScrollAnimator&)scrollAnimator {
return static_cast<blink::ScrollAnimator&>(
_scrollbar->GetScrollableArea()->GetScrollAnimator());
}
- (NSRect)convertRectToBacking:(NSRect)aRect {
return aRect;
}
- (NSRect)convertRectFromBacking:(NSRect)aRect {
return aRect;
}
- (NSPoint)mouseLocationInScrollerForScrollerImp:(id)scrollerImp {
if (!_scrollbar)
return NSZeroPoint;
DCHECK_EQ(scrollerImp, ScrollbarPainterForScrollbar(*_scrollbar));
return _scrollbar->ConvertFromContainingEmbeddedContentView(
_scrollbar->GetScrollableArea()->LastKnownMousePosition());
}
- (void)setUpAlphaAnimation:
(base::scoped_nsobject<BlinkScrollbarPartAnimation>&)
scrollbarPartAnimation
scrollerPainter:(ScrollbarPainter)scrollerPainter
part:(blink::ScrollbarPart)part
animateAlphaTo:(CGFloat)newAlpha
duration:(NSTimeInterval)duration {
blink::MacScrollbarAnimator* scrollbar_animator =
_scrollbar->GetScrollableArea()->GetMacScrollbarAnimator();
DCHECK(scrollbar_animator);
// If the user has scrolled the page, then the scrollbars must be animated
// here.
// This overrides the early returns.
bool mustAnimate = [self scrollAnimator].HaveScrolledSincePageLoad();
if (scrollbar_animator->ScrollbarPaintTimerIsActive() && !mustAnimate)
return;
if (_scrollbar->GetScrollableArea()->ShouldSuspendScrollAnimations() &&
!mustAnimate) {
scrollbar_animator->StartScrollbarPaintTimer();
return;
}
// At this point, we are definitely going to animate now, so stop the timer.
scrollbar_animator->StopScrollbarPaintTimer();
// If we are currently animating, stop
if (scrollbarPartAnimation) {
[scrollbarPartAnimation invalidate];
scrollbarPartAnimation.reset();
}
scrollbarPartAnimation.reset([[BlinkScrollbarPartAnimation alloc]
initWithScrollbar:_scrollbar
featureToAnimate:part == blink::kThumbPart ? ThumbAlpha : TrackAlpha
animateFrom:part == blink::kThumbPart ? [scrollerPainter knobAlpha]
: [scrollerPainter trackAlpha]
animateTo:newAlpha
duration:duration
taskRunner:_taskRunner]);
[scrollbarPartAnimation startAnimation];
}
- (void)scrollerImp:(id)scrollerImp
animateKnobAlphaTo:(CGFloat)newKnobAlpha
duration:(NSTimeInterval)duration {
if (!_scrollbar)
return;
DCHECK_EQ(scrollerImp, ScrollbarPainterForScrollbar(*_scrollbar));
ScrollbarPainter scrollerPainter = (ScrollbarPainter)scrollerImp;
[self setUpAlphaAnimation:_knobAlphaAnimation
scrollerPainter:scrollerPainter
part:blink::kThumbPart
animateAlphaTo:newKnobAlpha
duration:duration];
}
- (void)scrollerImp:(id)scrollerImp
animateTrackAlphaTo:(CGFloat)newTrackAlpha
duration:(NSTimeInterval)duration {
if (!_scrollbar)
return;
DCHECK_EQ(scrollerImp, ScrollbarPainterForScrollbar(*_scrollbar));
ScrollbarPainter scrollerPainter = (ScrollbarPainter)scrollerImp;
[self setUpAlphaAnimation:_trackAlphaAnimation
scrollerPainter:scrollerPainter
part:blink::kBackTrackPart
animateAlphaTo:newTrackAlpha
duration:duration];
}
- (void)scrollerImp:(id)scrollerImp
animateUIStateTransitionWithDuration:(NSTimeInterval)duration {
if (!_scrollbar)
return;
if (!SupportsUIStateTransitionProgress())
return;
DCHECK_EQ(scrollerImp, ScrollbarPainterForScrollbar(*_scrollbar));
ScrollbarPainter scrollbarPainter = (ScrollbarPainter)scrollerImp;
// UIStateTransition always animates to 1. In case an animation is in progress
// this avoids a hard transition.
[scrollbarPainter
setUiStateTransitionProgress:1 - [scrollerImp uiStateTransitionProgress]];
if (!_uiStateTransitionAnimation)
_uiStateTransitionAnimation.reset([[BlinkScrollbarPartAnimation alloc]
initWithScrollbar:_scrollbar
featureToAnimate:UIStateTransition
animateFrom:[scrollbarPainter uiStateTransitionProgress]
animateTo:1.0
duration:duration
taskRunner:_taskRunner]);
else {
// If we don't need to initialize the animation, just reset the values in
// case they have changed.
[_uiStateTransitionAnimation
setStartValue:[scrollbarPainter uiStateTransitionProgress]];
[_uiStateTransitionAnimation setEndValue:1.0];
[_uiStateTransitionAnimation setDuration:duration];
}
[_uiStateTransitionAnimation startAnimation];
}
- (void)scrollerImp:(id)scrollerImp
animateExpansionTransitionWithDuration:(NSTimeInterval)duration {
if (!_scrollbar)
return;
if (!SupportsExpansionTransitionProgress())
return;
DCHECK_EQ(scrollerImp, ScrollbarPainterForScrollbar(*_scrollbar));
ScrollbarPainter scrollbarPainter = (ScrollbarPainter)scrollerImp;
// ExpansionTransition always animates to 1. In case an animation is in
// progress this avoids a hard transition.
[scrollbarPainter
setExpansionTransitionProgress:1 -
[scrollerImp expansionTransitionProgress]];
if (!_expansionTransitionAnimation) {
_expansionTransitionAnimation.reset([[BlinkScrollbarPartAnimation alloc]
initWithScrollbar:_scrollbar
featureToAnimate:ExpansionTransition
animateFrom:[scrollbarPainter expansionTransitionProgress]
animateTo:1.0
duration:duration
taskRunner:_taskRunner]);
} else {
// If we don't need to initialize the animation, just reset the values in
// case they have changed.
[_expansionTransitionAnimation
setStartValue:[scrollbarPainter uiStateTransitionProgress]];
[_expansionTransitionAnimation setEndValue:1.0];
[_expansionTransitionAnimation setDuration:duration];
}
[_expansionTransitionAnimation startAnimation];
}
- (void)scrollerImp:(id)scrollerImp
overlayScrollerStateChangedTo:(NSUInteger)newOverlayScrollerState {
// The names of these states are based on their observed behavior, and are not
// based on documentation.
enum {
NSScrollerStateInvisible = 0,
NSScrollerStateKnob = 1,
NSScrollerStateExpanded = 2
};
// We do not receive notifications about the thumb un-expanding when the
// scrollbar fades away. Ensure
// that we re-paint the thumb the next time that we transition away from being
// invisible, so that
// the thumb doesn't stick in an expanded state.
if (newOverlayScrollerState == NSScrollerStateExpanded) {
_hasExpandedSinceInvisible = YES;
} else if (newOverlayScrollerState != NSScrollerStateInvisible &&
_hasExpandedSinceInvisible) {
_scrollbar->SetNeedsPaintInvalidation(blink::kThumbPart);
_hasExpandedSinceInvisible = NO;
}
}
- (void)invalidate {
_scrollbar = 0;
BEGIN_BLOCK_OBJC_EXCEPTIONS;
[_knobAlphaAnimation invalidate];
[_trackAlphaAnimation invalidate];
[_uiStateTransitionAnimation invalidate];
[_expansionTransitionAnimation invalidate];
END_BLOCK_OBJC_EXCEPTIONS;
}
@end
namespace blink {
MacScrollbarAnimatorImpl::MacScrollbarAnimatorImpl(
ScrollableArea* scrollable_area)
: task_runner_(scrollable_area->GetCompositorTaskRunner()),
scrollable_area_(scrollable_area) {
scrollbar_painter_controller_delegate_.reset(
[[BlinkScrollbarPainterControllerDelegate alloc]
initWithScrollableArea:scrollable_area]);
scrollbar_painter_controller_.reset(
[[[NSClassFromString(@"NSScrollerImpPair") alloc] init] autorelease],
base::scoped_policy::RETAIN);
[scrollbar_painter_controller_
performSelector:@selector(setDelegate:)
withObject:scrollbar_painter_controller_delegate_];
[scrollbar_painter_controller_
setScrollerStyle:ScrollbarThemeMac::RecommendedScrollerStyle()];
}
void MacScrollbarAnimatorImpl::Dispose() {
BEGIN_BLOCK_OBJC_EXCEPTIONS;
ScrollbarPainter horizontal_scrollbar_painter =
[scrollbar_painter_controller_ horizontalScrollerImp];
[horizontal_scrollbar_painter setDelegate:nil];
ScrollbarPainter vertical_scrollbar_painter =
[scrollbar_painter_controller_ verticalScrollerImp];
[vertical_scrollbar_painter setDelegate:nil];
[scrollbar_painter_controller_delegate_ invalidate];
[scrollbar_painter_controller_ setDelegate:nil];
[horizontal_scrollbar_painter_delegate_ invalidate];
[vertical_scrollbar_painter_delegate_ invalidate];
END_BLOCK_OBJC_EXCEPTIONS;
initial_scrollbar_paint_task_handle_.Cancel();
send_content_area_scrolled_task_handle_.Cancel();
}
void MacScrollbarAnimatorImpl::ContentAreaWillPaint() const {
if (!scrollable_area_->ScrollbarsCanBeActive())
return;
[scrollbar_painter_controller_ contentAreaWillDraw];
}
void MacScrollbarAnimatorImpl::MouseEnteredContentArea() const {
if (!scrollable_area_->ScrollbarsCanBeActive())
return;
[scrollbar_painter_controller_ mouseEnteredContentArea];
}
void MacScrollbarAnimatorImpl::MouseExitedContentArea() const {
if (!scrollable_area_->ScrollbarsCanBeActive())
return;
[scrollbar_painter_controller_ mouseExitedContentArea];
}
void MacScrollbarAnimatorImpl::MouseMovedInContentArea() const {
if (!scrollable_area_->ScrollbarsCanBeActive())
return;
[scrollbar_painter_controller_ mouseMovedInContentArea];
}
void MacScrollbarAnimatorImpl::MouseEnteredScrollbar(
Scrollbar& scrollbar) const {
if (!scrollable_area_->ScrollbarsCanBeActive())
return;
if (!SupportsUIStateTransitionProgress())
return;
if (ScrollbarPainter painter = ScrollbarPainterForScrollbar(scrollbar))
[painter mouseEnteredScroller];
}
void MacScrollbarAnimatorImpl::MouseExitedScrollbar(
Scrollbar& scrollbar) const {
if (!scrollable_area_->ScrollbarsCanBeActive())
return;
if (!SupportsUIStateTransitionProgress())
return;
if (ScrollbarPainter painter = ScrollbarPainterForScrollbar(scrollbar))
[painter mouseExitedScroller];
}
void MacScrollbarAnimatorImpl::ContentsResized() const {
if (!scrollable_area_->ScrollbarsCanBeActive())
return;
[scrollbar_painter_controller_ contentAreaDidResize];
}
void MacScrollbarAnimatorImpl::DidAddVerticalScrollbar(Scrollbar& scrollbar) {
ScrollbarPainter painter = ScrollbarPainterForScrollbar(scrollbar);
if (!painter)
return;
DCHECK(!vertical_scrollbar_painter_delegate_);
vertical_scrollbar_painter_delegate_.reset(
[[BlinkScrollbarPainterDelegate alloc] initWithScrollbar:&scrollbar
taskRunner:task_runner_]);
[painter setDelegate:vertical_scrollbar_painter_delegate_];
[scrollbar_painter_controller_ setVerticalScrollerImp:painter];
}
void MacScrollbarAnimatorImpl::WillRemoveVerticalScrollbar(
Scrollbar& scrollbar) {
ScrollbarPainter painter = ScrollbarPainterForScrollbar(scrollbar);
DCHECK_EQ([scrollbar_painter_controller_ verticalScrollerImp], painter);
if (!painter)
DCHECK(!vertical_scrollbar_painter_delegate_);
[painter setDelegate:nil];
[vertical_scrollbar_painter_delegate_ invalidate];
vertical_scrollbar_painter_delegate_.reset();
[scrollbar_painter_controller_ setVerticalScrollerImp:nil];
}
void MacScrollbarAnimatorImpl::DidAddHorizontalScrollbar(Scrollbar& scrollbar) {
ScrollbarPainter painter = ScrollbarPainterForScrollbar(scrollbar);
if (!painter)
return;
DCHECK(!horizontal_scrollbar_painter_delegate_);
horizontal_scrollbar_painter_delegate_.reset(
[[BlinkScrollbarPainterDelegate alloc] initWithScrollbar:&scrollbar
taskRunner:task_runner_]);
[painter setDelegate:horizontal_scrollbar_painter_delegate_];
[scrollbar_painter_controller_ setHorizontalScrollerImp:painter];
}
void MacScrollbarAnimatorImpl::WillRemoveHorizontalScrollbar(
Scrollbar& scrollbar) {
ScrollbarPainter painter = ScrollbarPainterForScrollbar(scrollbar);
DCHECK_EQ([scrollbar_painter_controller_ horizontalScrollerImp], painter);
if (!painter)
DCHECK(!horizontal_scrollbar_painter_delegate_);
[painter setDelegate:nil];
[horizontal_scrollbar_painter_delegate_ invalidate];
horizontal_scrollbar_painter_delegate_.reset();
[scrollbar_painter_controller_ setHorizontalScrollerImp:nil];
}
bool MacScrollbarAnimatorImpl::SetScrollbarsVisibleForTesting(bool show) {
if (show)
[scrollbar_painter_controller_ flashScrollers];
else
[scrollbar_painter_controller_ hideOverlayScrollers];
[vertical_scrollbar_painter_delegate_ updateVisibilityImmediately:show];
[horizontal_scrollbar_painter_delegate_ updateVisibilityImmediately:show];
return true;
}
void MacScrollbarAnimatorImpl::UpdateScrollerStyle() {
if (!scrollable_area_->ScrollbarsCanBeActive())
return;
blink::ScrollbarThemeMac* mac_theme =
MacOverlayScrollbarTheme(scrollable_area_->GetPageScrollbarTheme());
if (!mac_theme)
return;
NSScrollerStyle new_style = [scrollbar_painter_controller_ scrollerStyle];
if (Scrollbar* vertical_scrollbar = scrollable_area_->VerticalScrollbar()) {
vertical_scrollbar->SetNeedsPaintInvalidation(kAllParts);
ScrollbarPainter old_vertical_painter =
[scrollbar_painter_controller_ verticalScrollerImp];
ScrollbarPainter new_vertical_painter = [NSClassFromString(@"NSScrollerImp")
scrollerImpWithStyle:new_style
controlSize:NSRegularControlSize
horizontal:NO
replacingScrollerImp:old_vertical_painter];
[old_vertical_painter setDelegate:nil];
[new_vertical_painter setDelegate:vertical_scrollbar_painter_delegate_];
[scrollbar_painter_controller_ setVerticalScrollerImp:new_vertical_painter];
mac_theme->SetNewPainterForScrollbar(*vertical_scrollbar,
new_vertical_painter);
// The different scrollbar styles have different thicknesses, so we must
// re-set the frameRect to the new thickness, and the re-layout below will
// ensure the offset and length are properly updated.
int thickness =
mac_theme->ScrollbarThickness(vertical_scrollbar->ScaleFromDIP());
vertical_scrollbar->SetFrameRect(IntRect(0, 0, thickness, thickness));
}
if (Scrollbar* horizontal_scrollbar =
scrollable_area_->HorizontalScrollbar()) {
horizontal_scrollbar->SetNeedsPaintInvalidation(kAllParts);
ScrollbarPainter old_horizontal_painter =
[scrollbar_painter_controller_ horizontalScrollerImp];
ScrollbarPainter new_horizontal_painter =
[NSClassFromString(@"NSScrollerImp")
scrollerImpWithStyle:new_style
controlSize:NSRegularControlSize
horizontal:YES
replacingScrollerImp:old_horizontal_painter];
[old_horizontal_painter setDelegate:nil];
[new_horizontal_painter setDelegate:horizontal_scrollbar_painter_delegate_];
[scrollbar_painter_controller_
setHorizontalScrollerImp:new_horizontal_painter];
mac_theme->SetNewPainterForScrollbar(*horizontal_scrollbar,
new_horizontal_painter);
// The different scrollbar styles have different thicknesses, so we must
// re-set the
// frameRect to the new thickness, and the re-layout below will ensure the
// offset
// and length are properly updated.
int thickness =
mac_theme->ScrollbarThickness(horizontal_scrollbar->ScaleFromDIP());
horizontal_scrollbar->SetFrameRect(IntRect(0, 0, thickness, thickness));
}
}
void MacScrollbarAnimatorImpl::StartScrollbarPaintTimer() {
// Post a task with 1 ms delay to give a chance to run other immediate tasks
// that may cancel this.
initial_scrollbar_paint_task_handle_ = PostDelayedCancellableTask(
*task_runner_, FROM_HERE,
WTF::Bind(&MacScrollbarAnimatorImpl::InitialScrollbarPaintTask,
WrapWeakPersistent(this)),
base::TimeDelta::FromMilliseconds(1));
}
bool MacScrollbarAnimatorImpl::ScrollbarPaintTimerIsActive() const {
return initial_scrollbar_paint_task_handle_.IsActive();
}
void MacScrollbarAnimatorImpl::StopScrollbarPaintTimer() {
initial_scrollbar_paint_task_handle_.Cancel();
}
void MacScrollbarAnimatorImpl::InitialScrollbarPaintTask() {
// To force the scrollbars to flash, we have to call hide first. Otherwise,
// the ScrollbarPainterController
// might think that the scrollbars are already showing and bail early.
[scrollbar_painter_controller_ hideOverlayScrollers];
[scrollbar_painter_controller_ flashScrollers];
}
void MacScrollbarAnimatorImpl::DidChangeUserVisibleScrollOffset(
const ScrollOffset& delta) {
content_area_scrolled_timer_scroll_delta_ = delta;
if (send_content_area_scrolled_task_handle_.IsActive())
return;
send_content_area_scrolled_task_handle_ = PostCancellableTask(
*task_runner_, FROM_HERE,
WTF::Bind(&MacScrollbarAnimatorImpl::SendContentAreaScrolledTask,
WrapWeakPersistent(this)));
}
void MacScrollbarAnimatorImpl::SendContentAreaScrolledTask() {
if (SupportsContentAreaScrolledInDirection()) {
[scrollbar_painter_controller_
contentAreaScrolledInDirection:
NSMakePoint(content_area_scrolled_timer_scroll_delta_.Width(),
content_area_scrolled_timer_scroll_delta_.Height())];
content_area_scrolled_timer_scroll_delta_ = ScrollOffset();
} else
[scrollbar_painter_controller_ contentAreaScrolled];
}
MacScrollbarAnimator* MacScrollbarAnimator::Create(
ScrollableArea* scrollable_area) {
return MakeGarbageCollected<MacScrollbarAnimatorImpl>(
const_cast<ScrollableArea*>(scrollable_area));
}
} // namespace blink