Co-authored-by: Claude Fable 5 <[email protected]> Co-authored-by: Julius Marminge <[email protected]>
25 lines
896 B
TypeScript
25 lines
896 B
TypeScript
interface LineGeometry {
|
|
readonly top: number;
|
|
readonly height: number;
|
|
}
|
|
|
|
interface CenteredFileLineScrollInput {
|
|
readonly scrollTop: number;
|
|
readonly scrollHeight: number;
|
|
readonly viewportTop: number;
|
|
readonly viewportHeight: number;
|
|
readonly fileTop: number;
|
|
readonly estimatedLine: LineGeometry;
|
|
readonly renderedLine?: LineGeometry;
|
|
}
|
|
|
|
export function resolveCenteredFileLineScrollTop(input: CenteredFileLineScrollInput): number {
|
|
const lineTop =
|
|
input.renderedLine === undefined
|
|
? input.fileTop + input.estimatedLine.top
|
|
: input.scrollTop + input.renderedLine.top - input.viewportTop;
|
|
const lineHeight = input.renderedLine?.height ?? input.estimatedLine.height;
|
|
const centeredTop = Math.max(0, lineTop - Math.max(0, (input.viewportHeight - lineHeight) / 2));
|
|
return Math.min(centeredTop, Math.max(0, input.scrollHeight - input.viewportHeight));
|
|
}
|