| 1 |
/*--------------------------------------------------------------------------------------------- |
| 2 |
* Copyright (c) Microsoft Corporation. All rights reserved. |
| 3 |
* Licensed under the MIT License. See License.txt in the project root for license information. |
| 4 |
*--------------------------------------------------------------------------------------------*/ |
| 5 |
|
| 6 |
import { GlobalPointerMoveMonitor } from 'vs/base/browser/globalPointerMoveMonitor'; |
| 7 |
import { Widget } from 'vs/base/browser/ui/widget'; |
| 8 |
import { TimeoutTimer } from 'vs/base/common/async'; |
| 9 |
import * as dom from 'vs/base/browser/dom'; |
| 10 |
|
| 11 |
/** |
| 12 |
* The arrow image size. |
| 13 |
*/ |
| 14 |
export const ARROW_IMG_SIZE = 11; |
| 15 |
|
| 16 |
export interface ScrollbarArrowOptions { |
| 17 |
onActivate: () => void; |
| 18 |
className: string; |
| 19 |
// icon: ThemeIcon; |
| 20 |
|
| 21 |
bgWidth: number; |
| 22 |
bgHeight: number; |
| 23 |
|
| 24 |
top?: number; |
| 25 |
left?: number; |
| 26 |
bottom?: number; |
| 27 |
right?: number; |
| 28 |
} |
| 29 |
|
| 30 |
export class ScrollbarArrow extends Widget { |
| 31 |
|
| 32 |
private _onActivate: () => void; |
| 33 |
public bgDomNode: HTMLElement; |
| 34 |
public domNode: HTMLElement; |
| 35 |
private _pointerdownRepeatTimer: dom.WindowIntervalTimer; |
| 36 |
private _pointerdownScheduleRepeatTimer: TimeoutTimer; |
| 37 |
private _pointerMoveMonitor: GlobalPointerMoveMonitor; |
| 38 |
|
| 39 |
constructor(opts: ScrollbarArrowOptions) { |
| 40 |
super(); |
| 41 |
this._onActivate = opts.onActivate; |
| 42 |
|
| 43 |
this.bgDomNode = document.createElement('div'); |
| 44 |
this.bgDomNode.className = 'arrow-background'; |
| 45 |
this.bgDomNode.style.position = 'absolute'; |
| 46 |
this.bgDomNode.style.width = opts.bgWidth + 'px'; |
| 47 |
this.bgDomNode.style.height = opts.bgHeight + 'px'; |
| 48 |
if (typeof opts.top !== 'undefined') { |
| 49 |
this.bgDomNode.style.top = '0px'; |
| 50 |
} |
| 51 |
if (typeof opts.left !== 'undefined') { |
| 52 |
this.bgDomNode.style.left = '0px'; |
| 53 |
} |
| 54 |
if (typeof opts.bottom !== 'undefined') { |
| 55 |
this.bgDomNode.style.bottom = '0px'; |
| 56 |
} |
| 57 |
if (typeof opts.right !== 'undefined') { |
| 58 |
this.bgDomNode.style.right = '0px'; |
| 59 |
} |
| 60 |
|
| 61 |
this.domNode = document.createElement('div'); |
| 62 |
this.domNode.className = opts.className; |
| 63 |
// this.domNode.classList.add(...ThemeIcon.asClassNameArray(opts.icon)); |
| 64 |
|
| 65 |
this.domNode.style.position = 'absolute'; |
| 66 |
this.domNode.style.width = ARROW_IMG_SIZE + 'px'; |
| 67 |
this.domNode.style.height = ARROW_IMG_SIZE + 'px'; |
| 68 |
if (typeof opts.top !== 'undefined') { |
| 69 |
this.domNode.style.top = opts.top + 'px'; |
| 70 |
} |
| 71 |
if (typeof opts.left !== 'undefined') { |
| 72 |
this.domNode.style.left = opts.left + 'px'; |
| 73 |
} |
| 74 |
if (typeof opts.bottom !== 'undefined') { |
| 75 |
this.domNode.style.bottom = opts.bottom + 'px'; |
| 76 |
} |
| 77 |
if (typeof opts.right !== 'undefined') { |
| 78 |
this.domNode.style.right = opts.right + 'px'; |
| 79 |
} |
| 80 |
|
| 81 |
this._pointerMoveMonitor = this._register(new GlobalPointerMoveMonitor()); |
| 82 |
this._register(dom.addStandardDisposableListener(this.bgDomNode, dom.EventType.POINTER_DOWN, (e) => this._arrowPointerDown(e))); |
| 83 |
this._register(dom.addStandardDisposableListener(this.domNode, dom.EventType.POINTER_DOWN, (e) => this._arrowPointerDown(e))); |
| 84 |
|
| 85 |
this._pointerdownRepeatTimer = this._register(new dom.WindowIntervalTimer()); |
| 86 |
this._pointerdownScheduleRepeatTimer = this._register(new TimeoutTimer()); |
| 87 |
} |
| 88 |
|
| 89 |
private _arrowPointerDown(e: PointerEvent): void { |
| 90 |
if (!e.target || !(e.target instanceof Element)) { |
| 91 |
return; |
| 92 |
} |
| 93 |
const scheduleRepeater = () => { |
| 94 |
this._pointerdownRepeatTimer.cancelAndSet(() => this._onActivate(), 1000 / 24, dom.getWindow(e)); |
| 95 |
}; |
| 96 |
|
| 97 |
this._onActivate(); |
| 98 |
this._pointerdownRepeatTimer.cancel(); |
| 99 |
this._pointerdownScheduleRepeatTimer.cancelAndSet(scheduleRepeater, 200); |
| 100 |
|
| 101 |
this._pointerMoveMonitor.startMonitoring( |
| 102 |
e.target, |
| 103 |
e.pointerId, |
| 104 |
e.buttons, |
| 105 |
(pointerMoveData) => { /* Intentional empty */ }, |
| 106 |
() => { |
| 107 |
this._pointerdownRepeatTimer.cancel(); |
| 108 |
this._pointerdownScheduleRepeatTimer.cancel(); |
| 109 |
} |
| 110 |
); |
| 111 |
|
| 112 |
e.preventDefault(); |
| 113 |
} |
| 114 |
} |