| 1 |
/** |
| 2 |
* Copyright (c) 2017 The xterm.js authors. All rights reserved. |
| 3 |
* @license MIT |
| 4 |
*/ |
| 5 |
|
| 6 |
import { ICoreBrowserService } from 'browser/services/Services'; |
| 7 |
|
| 8 |
/** |
| 9 |
* The time between cursor blinks. |
| 10 |
*/ |
| 11 |
const BLINK_INTERVAL = 600; |
| 12 |
|
| 13 |
export class CursorBlinkStateManager { |
| 14 |
public isCursorVisible: boolean; |
| 15 |
|
| 16 |
private _animationFrame: number | undefined; |
| 17 |
private _blinkStartTimeout: number | undefined; |
| 18 |
private _blinkInterval: number | undefined; |
| 19 |
|
| 20 |
/** |
| 21 |
* The time at which the animation frame was restarted, this is used on the |
| 22 |
* next render to restart the timers so they don't need to restart the timers |
| 23 |
* multiple times over a short period. |
| 24 |
*/ |
| 25 |
private _animationTimeRestarted: number | undefined; |
| 26 |
|
| 27 |
constructor( |
| 28 |
private _renderCallback: () => void, |
| 29 |
private _coreBrowserService: ICoreBrowserService |
| 30 |
) { |
| 31 |
this.isCursorVisible = true; |
| 32 |
if (this._coreBrowserService.isFocused) { |
| 33 |
this._restartInterval(); |
| 34 |
} |
| 35 |
} |
| 36 |
|
| 37 |
public get isPaused(): boolean { return !(this._blinkStartTimeout || this._blinkInterval); } |
| 38 |
|
| 39 |
public dispose(): void { |
| 40 |
if (this._blinkInterval) { |
| 41 |
this._coreBrowserService.window.clearInterval(this._blinkInterval); |
| 42 |
this._blinkInterval = undefined; |
| 43 |
} |
| 44 |
if (this._blinkStartTimeout) { |
| 45 |
this._coreBrowserService.window.clearTimeout(this._blinkStartTimeout); |
| 46 |
this._blinkStartTimeout = undefined; |
| 47 |
} |
| 48 |
if (this._animationFrame) { |
| 49 |
this._coreBrowserService.window.cancelAnimationFrame(this._animationFrame); |
| 50 |
this._animationFrame = undefined; |
| 51 |
} |
| 52 |
} |
| 53 |
|
| 54 |
public restartBlinkAnimation(): void { |
| 55 |
if (this.isPaused) { |
| 56 |
return; |
| 57 |
} |
| 58 |
// Save a timestamp so that the restart can be done on the next interval |
| 59 |
this._animationTimeRestarted = Date.now(); |
| 60 |
// Force a cursor render to ensure it's visible and in the correct position |
| 61 |
this.isCursorVisible = true; |
| 62 |
if (!this._animationFrame) { |
| 63 |
this._animationFrame = this._coreBrowserService.window.requestAnimationFrame(() => { |
| 64 |
this._renderCallback(); |
| 65 |
this._animationFrame = undefined; |
| 66 |
}); |
| 67 |
} |
| 68 |
} |
| 69 |
|
| 70 |
private _restartInterval(timeToStart: number = BLINK_INTERVAL): void { |
| 71 |
// Clear any existing interval |
| 72 |
if (this._blinkInterval) { |
| 73 |
this._coreBrowserService.window.clearInterval(this._blinkInterval); |
| 74 |
this._blinkInterval = undefined; |
| 75 |
} |
| 76 |
|
| 77 |
// Setup the initial timeout which will hide the cursor, this is done before |
| 78 |
// the regular interval is setup in order to support restarting the blink |
| 79 |
// animation in a lightweight way (without thrashing clearInterval and |
| 80 |
// setInterval). |
| 81 |
this._blinkStartTimeout = this._coreBrowserService.window.setTimeout(() => { |
| 82 |
// Check if another animation restart was requested while this was being |
| 83 |
// started |
| 84 |
if (this._animationTimeRestarted) { |
| 85 |
const time = BLINK_INTERVAL - (Date.now() - this._animationTimeRestarted); |
| 86 |
this._animationTimeRestarted = undefined; |
| 87 |
if (time > 0) { |
| 88 |
this._restartInterval(time); |
| 89 |
return; |
| 90 |
} |
| 91 |
} |
| 92 |
|
| 93 |
// Hide the cursor |
| 94 |
this.isCursorVisible = false; |
| 95 |
this._animationFrame = this._coreBrowserService.window.requestAnimationFrame(() => { |
| 96 |
this._renderCallback(); |
| 97 |
this._animationFrame = undefined; |
| 98 |
}); |
| 99 |
|
| 100 |
// Setup the blink interval |
| 101 |
this._blinkInterval = this._coreBrowserService.window.setInterval(() => { |
| 102 |
// Adjust the animation time if it was restarted |
| 103 |
if (this._animationTimeRestarted) { |
| 104 |
// calc time diff |
| 105 |
// Make restart interval do a setTimeout initially? |
| 106 |
const time = BLINK_INTERVAL - (Date.now() - this._animationTimeRestarted); |
| 107 |
this._animationTimeRestarted = undefined; |
| 108 |
this._restartInterval(time); |
| 109 |
return; |
| 110 |
} |
| 111 |
|
| 112 |
// Invert visibility and render |
| 113 |
this.isCursorVisible = !this.isCursorVisible; |
| 114 |
this._animationFrame = this._coreBrowserService.window.requestAnimationFrame(() => { |
| 115 |
this._renderCallback(); |
| 116 |
this._animationFrame = undefined; |
| 117 |
}); |
| 118 |
}, BLINK_INTERVAL); |
| 119 |
}, timeToStart); |
| 120 |
} |
| 121 |
|
| 122 |
public pause(): void { |
| 123 |
this.isCursorVisible = true; |
| 124 |
if (this._blinkInterval) { |
| 125 |
this._coreBrowserService.window.clearInterval(this._blinkInterval); |
| 126 |
this._blinkInterval = undefined; |
| 127 |
} |
| 128 |
if (this._blinkStartTimeout) { |
| 129 |
this._coreBrowserService.window.clearTimeout(this._blinkStartTimeout); |
| 130 |
this._blinkStartTimeout = undefined; |
| 131 |
} |
| 132 |
if (this._animationFrame) { |
| 133 |
this._coreBrowserService.window.cancelAnimationFrame(this._animationFrame); |
| 134 |
this._animationFrame = undefined; |
| 135 |
} |
| 136 |
} |
| 137 |
|
| 138 |
public resume(): void { |
| 139 |
// Clear out any existing timers just in case |
| 140 |
this.pause(); |
| 141 |
|
| 142 |
this._animationTimeRestarted = undefined; |
| 143 |
this._restartInterval(); |
| 144 |
this.restartBlinkAnimation(); |
| 145 |
} |
| 146 |
} |