Merge pull request #5523 from Tyriar/4628

Add idle timeout which pauses cursor blink
This commit is contained in:
Daniel Imms
2025-12-28 14:04:49 -08:00
committed by GitHub
6 changed files with 155 additions and 12 deletions
+73 -2
View File
@@ -5,7 +5,7 @@
import { DomRendererRowFactory, RowCss } from 'browser/renderer/dom/DomRendererRowFactory';
import { WidthCache } from 'browser/renderer/dom/WidthCache';
import { INVERTED_DEFAULT_COLOR } from 'browser/renderer/shared/Constants';
import { INVERTED_DEFAULT_COLOR, RendererConstants } from 'browser/renderer/shared/Constants';
import { createRenderDimensions } from 'browser/renderer/shared/RendererUtils';
import { createSelectionRenderModel } from 'browser/renderer/shared/SelectionRenderModel';
import { IRenderDimensions, IRenderer, IRequestRedrawEvent, ISelectionRenderModel } from 'browser/renderer/shared/Types';
@@ -15,6 +15,7 @@ import { color } from 'common/Color';
import { Disposable, toDisposable } from 'vs/base/common/lifecycle';
import { IBufferService, ICoreService, IInstantiationService, IOptionsService } from 'common/services/Services';
import { Emitter } from 'vs/base/common/event';
import { addDisposableListener } from 'vs/base/browser/dom';
const TERMINAL_CLASS_PREFIX = 'xterm-dom-renderer-owner-';
@@ -23,6 +24,7 @@ const FG_CLASS_PREFIX = 'xterm-fg-';
const BG_CLASS_PREFIX = 'xterm-bg-';
const FOCUS_CLASS = 'xterm-focus';
const SELECTION_CLASS = 'xterm-selection';
const CURSOR_BLINK_IDLE_CLASS = 'xterm-cursor-blink-idle';
let nextTerminalId = 1;
@@ -42,6 +44,7 @@ export class DomRenderer extends Disposable implements IRenderer {
private _selectionContainer: HTMLElement;
private _widthCache: WidthCache;
private _selectionRenderModel: ISelectionRenderModel = createSelectionRenderModel();
private _cursorBlinkStateManager: CursorBlinkStateManager;
public dimensions: IRenderDimensions;
@@ -89,6 +92,10 @@ export class DomRenderer extends Disposable implements IRenderer {
this._register(this._linkifier2.onShowLinkUnderline(e => this._handleLinkHover(e)));
this._register(this._linkifier2.onHideLinkUnderline(e => this._handleLinkLeave(e)));
this._cursorBlinkStateManager = new CursorBlinkStateManager(this._rowContainer, this._coreBrowserService);
this._register(addDisposableListener(this._document, 'mousedown', () => this._cursorBlinkStateManager.restartBlinkAnimation()));
this._register(toDisposable(() => this._cursorBlinkStateManager.dispose()));
this._register(toDisposable(() => {
this._element.classList.remove(TERMINAL_CLASS_PREFIX + this._terminalClass);
@@ -225,6 +232,10 @@ export class DomRenderer extends Disposable implements IRenderer {
`${this._terminalSelector} .${ROW_CONTAINER_CLASS}.${FOCUS_CLASS} .${RowCss.CURSOR_CLASS}.${RowCss.CURSOR_BLINK_CLASS}.${RowCss.CURSOR_STYLE_BLOCK_CLASS} {` +
` animation: ${blinkAnimationBlockId} 1s step-end infinite;` +
`}` +
// Disable cursor blinking when idle
`${this._terminalSelector} .${ROW_CONTAINER_CLASS}.${CURSOR_BLINK_IDLE_CLASS} .${RowCss.CURSOR_CLASS}.${RowCss.CURSOR_BLINK_CLASS} {` +
` animation: none !important;` +
`}` +
// !important helps fix an issue where the cursor will not render on top of the selection,
// however it's very hard to fix this issue and retain the blink animation without the use of
// !important. So this edge case fails when cursor blink is on.
@@ -328,11 +339,13 @@ export class DomRenderer extends Disposable implements IRenderer {
public handleBlur(): void {
this._rowContainer.classList.remove(FOCUS_CLASS);
this._cursorBlinkStateManager.pause();
this.renderRows(0, this._bufferService.rows - 1);
}
public handleFocus(): void {
this._rowContainer.classList.add(FOCUS_CLASS);
this._cursorBlinkStateManager.resume();
this.renderRows(this._bufferService.buffer.y, this._bufferService.buffer.y);
}
@@ -406,7 +419,8 @@ export class DomRenderer extends Disposable implements IRenderer {
}
public handleCursorMove(): void {
// No-op, the cursor is drawn when rows are drawn
// Reset idle timer on cursor movement (which happens on input)
this._cursorBlinkStateManager.restartBlinkAnimation();
}
private _handleOptionsChanged(): void {
@@ -540,3 +554,60 @@ export class DomRenderer extends Disposable implements IRenderer {
}
}
}
class CursorBlinkStateManager {
private _idleTimeout: number | undefined;
private _isIdlePaused: boolean = false;
constructor(
private readonly _rowContainer: HTMLElement,
private readonly _coreBrowserService: ICoreBrowserService
) {
if (this._coreBrowserService.isFocused) {
this._resetIdleTimer();
}
}
public dispose(): void {
this._clearIdleTimer();
}
public restartBlinkAnimation(): void {
if (this._isIdlePaused) {
this._rowContainer.classList.remove(CURSOR_BLINK_IDLE_CLASS);
}
this._resetIdleTimer();
}
public pause(): void {
this._isIdlePaused = false;
this._clearIdleTimer();
}
public resume(): void {
this._isIdlePaused = false;
this._rowContainer.classList.remove(CURSOR_BLINK_IDLE_CLASS);
this._resetIdleTimer();
}
private _resetIdleTimer(): void {
this._isIdlePaused = false;
this._clearIdleTimer();
this._idleTimeout = this._coreBrowserService.window.setTimeout(() => {
this._stopBlinkingDueToIdle();
}, RendererConstants.CURSOR_BLINK_IDLE_TIMEOUT);
}
private _clearIdleTimer(): void {
if (this._idleTimeout) {
this._coreBrowserService.window.clearTimeout(this._idleTimeout);
this._idleTimeout = undefined;
}
}
private _stopBlinkingDueToIdle(): void {
this._rowContainer.classList.add(CURSOR_BLINK_IDLE_CLASS);
this._isIdlePaused = true;
this._idleTimeout = undefined;
}
}
+7
View File
@@ -4,3 +4,10 @@
*/
export const INVERTED_DEFAULT_COLOR = 257;
export const enum RendererConstants {
/**
* The idle time after which cursor blinking stops.
*/
CURSOR_BLINK_IDLE_TIMEOUT = 5 * 60 * 1000
}