/
VerticalScrollOverflowView.js
91 lines (74 loc) · 2.6 KB
/
VerticalScrollOverflowView.js
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
/**
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @flow
*/
import type {Rect} from '../geometry';
import type {ScrollState} from '../utils/scrollState';
import type {Surface} from '../Surface';
import type {ViewState} from '../../types';
import {VerticalScrollBarView} from './VerticalScrollBarView';
import {withVerticalScrollbarLayout} from './withVerticalScrollbarLayout';
import {View} from '../View';
import {VerticalScrollView} from '../VerticalScrollView';
export class VerticalScrollOverflowView extends View {
_contentView: View;
_isProcessingOnChange: boolean = false;
_isScrolling: boolean = false;
_scrollOffset: number = 0;
_scrollBarView: VerticalScrollBarView;
_verticalScrollView: VerticalScrollView;
constructor(
surface: Surface,
frame: Rect,
contentView: View,
viewState: ViewState,
) {
super(surface, frame, withVerticalScrollbarLayout);
this._contentView = contentView;
this._verticalScrollView = new VerticalScrollView(
surface,
frame,
contentView,
viewState,
'VerticalScrollOverflowView',
);
this._verticalScrollView.onChange(this._onVerticalScrollViewChange);
this._scrollBarView = new VerticalScrollBarView(surface, frame, this);
this.addSubview(this._verticalScrollView);
this.addSubview(this._scrollBarView);
}
layoutSubviews() {
super.layoutSubviews();
const contentSize = this._contentView.desiredSize();
// This should be done after calling super.layoutSubviews() – calling it
// before somehow causes _contentView to need display on every mousemove
// event when the scroll bar is shown.
this._scrollBarView.setContentHeight(contentSize.height);
}
setScrollOffset(newScrollOffset: number, maxScrollOffset: number) {
const deltaY = newScrollOffset - this._scrollOffset;
if (!this._isProcessingOnChange) {
this._verticalScrollView.scrollBy(-deltaY);
}
this._scrollOffset = newScrollOffset;
this.setNeedsDisplay();
}
_onVerticalScrollViewChange = (
scrollState: ScrollState,
containerLength: number,
) => {
const maxOffset = scrollState.length - containerLength;
if (maxOffset === 0) {
return;
}
const percentage = Math.abs(scrollState.offset) / maxOffset;
const maxScrollThumbY = this._scrollBarView.getMaxScrollThumbY();
this._isProcessingOnChange = true;
this._scrollBarView.setScrollThumbY(percentage * maxScrollThumbY);
this._isProcessingOnChange = false;
};
}