2026-02-22 10:14:37 +01:00
|
|
|
import type { XtermTerminal, CellDimensions } from './types.js';
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Get cell dimensions from the terminal, handling xterm.js v5 (private API)
|
|
|
|
|
* and v7+ (public API).
|
|
|
|
|
*
|
2026-03-01 16:27:40 +01:00
|
|
|
* Returns CSS-pixel values. xterm's `device.char` is in device pixels, so
|
|
|
|
|
* we divide by `devicePixelRatio` to stay consistent with `css.cell`.
|
|
|
|
|
*
|
2026-02-22 10:14:37 +01:00
|
|
|
* Returns `null` if the terminal is not yet rendered or dimensions are
|
|
|
|
|
* unavailable.
|
|
|
|
|
*/
|
|
|
|
|
export function getCellDimensions(terminal: XtermTerminal): CellDimensions | null {
|
|
|
|
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
|
|
|
const t = terminal as any;
|
2026-03-01 16:27:40 +01:00
|
|
|
const dpr = typeof devicePixelRatio === 'number' && devicePixelRatio > 0
|
|
|
|
|
? devicePixelRatio : 1;
|
2026-02-22 10:14:37 +01:00
|
|
|
|
|
|
|
|
// Try v7+ public API first
|
|
|
|
|
if (t.dimensions?.css?.cell) {
|
2026-03-01 16:27:40 +01:00
|
|
|
const cellH = t.dimensions.css.cell.height;
|
2026-02-22 10:14:37 +01:00
|
|
|
return {
|
|
|
|
|
width: t.dimensions.css.cell.width,
|
2026-03-01 16:27:40 +01:00
|
|
|
height: cellH,
|
|
|
|
|
charTop: (t.dimensions?.device?.char?.top ?? 0) / dpr,
|
|
|
|
|
charHeight: (t.dimensions?.device?.char?.height ?? (cellH * dpr)) / dpr,
|
2026-02-22 10:14:37 +01:00
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Fall back to v5 private API
|
|
|
|
|
try {
|
|
|
|
|
const dims = t._core?._renderService?.dimensions;
|
|
|
|
|
if (dims?.css?.cell) {
|
2026-03-01 16:27:40 +01:00
|
|
|
const cellH = dims.css.cell.height;
|
2026-02-22 10:14:37 +01:00
|
|
|
return {
|
|
|
|
|
width: dims.css.cell.width,
|
2026-03-01 16:27:40 +01:00
|
|
|
height: cellH,
|
|
|
|
|
charTop: (dims.device?.char?.top ?? 0) / dpr,
|
|
|
|
|
charHeight: (dims.device?.char?.height ?? (cellH * dpr)) / dpr,
|
2026-02-22 10:14:37 +01:00
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
} catch {
|
|
|
|
|
// Private API may throw in some environments
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return null;
|
|
|
|
|
}
|