xonecas.io
70160
Ce

cel-tui / commit 0160313

fix(core): TextInput padding now applied in layout and paint

Intrinsic height computation in layout.ts accounts for vertical and
horizontal padding. Paint insets the content area (text, cursor,
placeholder) by padding before rendering. Cursor movement in cel.ts
uses the padded content width for up/down navigation.

Added tests: padding insets content area, padding affects intrinsic height.

xonecas2026-04-04 0160313109d448e785e0dc2b4256eb94a48548d0 parent aff6be5

4 files changed

packages/core/src/cel.ts+14−5
@@ -642,11 +642,20 @@ function handleKeyEvent(key: string, rawData?: string): void {
642642 case "down":
643643 case "home":
644644 case "end":
645- newState = moveCursor(
646- editState,
647- key as "left" | "right" | "up" | "down" | "home" | "end",
648- focusedInput.rect.width,
649- );
645+ {
646+ const tiPadX =
647+ (focusedInput.node as import("@cel-tui/types").TextInputNode)
648+ .props.padding?.x ?? 0;
649+ const contentWidth = Math.max(
650+ 0,
651+ focusedInput.rect.width - tiPadX * 2,
652+ );
653+ newState = moveCursor(
654+ editState,
655+ key as "left" | "right" | "up" | "down" | "home" | "end",
656+ contentWidth,
657+ );
658+ }
650659 break;
651660 case "enter":
652661 newState = insertChar(editState, "\n");
packages/core/src/layout.ts+7−4
@@ -86,20 +86,23 @@ function intrinsicMainSize(
8686 }
8787
8888 if (node.type === "textinput") {
89+ const tiPadX = (node.props.padding?.x ?? 0) * 2;
90+ const tiPadY = (node.props.padding?.y ?? 0) * 2;
8991 if (isVertical) {
9092 const val = node.props.value || "";
91- if (val.length === 0) return 1;
93+ const innerCrossForTI = Math.max(1, crossSize - tiPadX);
94+ if (val.length === 0) return 1 + tiPadY;
9295 const lines = val.split("\n");
9396 let total = 0;
9497 for (const line of lines) {
9598 total += Math.max(
9699 1,
97- Math.ceil(visibleWidth(line) / Math.max(1, crossSize)),
100+ Math.ceil(visibleWidth(line) / Math.max(1, innerCrossForTI)),
98101 );
99102 }
100- return total;
103+ return total + tiPadY;
101104 }
102- return 0;
105+ return 0 + tiPadX;
103106 }
104107
105108 // Container: compute intrinsic size along the requested axis.
packages/core/src/paint.ts+25−15
@@ -442,28 +442,38 @@ function paintTextInput(
442442 const { x, y, width: w, height: h } = rect;
443443 if (w <= 0 || h <= 0) return;
444444
445+ // Apply padding — content is inset from the rect edges
446+ const padX = props.padding?.x ?? 0;
447+ const padY = props.padding?.y ?? 0;
448+ const cx = x + padX;
449+ const cy = y + padY;
450+ const cw = Math.max(0, w - padX * 2);
451+ const ch = Math.max(0, h - padY * 2);
452+ if (cw <= 0 || ch <= 0) return;
453+
445454 const value = props.value;
446455 const showPlaceholder = value.length === 0 && props.placeholder;
447456
448457 if (showPlaceholder && props.placeholder) {
449- // Paint placeholder text
458+ // Paint placeholder text in padded content area
459+ const contentRect: Rect = { x: cx, y: cy, width: cw, height: ch };
450460 paintText(
451461 props.placeholder.content,
452462 props.placeholder.props,
453- rect,
463+ contentRect,
454464 clipRect,
455465 buf,
456466 );
457467 return;
458468 }
459469
460- // Word-wrap value (always on for TextInput)
470+ // Word-wrap value (always on for TextInput) using content width
461471 const lines: string[] = [];
462472 for (const rawLine of value.split("\n")) {
463- if (visibleWidth(rawLine) <= w) {
473+ if (visibleWidth(rawLine) <= cw) {
464474 lines.push(rawLine);
465475 } else {
466- wrapLine(rawLine, w, lines);
476+ wrapLine(rawLine, cw, lines);
467477 }
468478 }
469479
@@ -472,10 +482,10 @@ function paintTextInput(
472482
473483 if (props.focused) {
474484 const cursorOffset = getTextInputCursor(props);
475- const cursorPos = offsetToWrappedPos(value, cursorOffset, w);
485+ const cursorPos = offsetToWrappedPos(value, cursorOffset, cw);
476486 // Scroll down if cursor is below viewport
477- if (cursorPos.line >= scrollOffset + h) {
478- scrollOffset = cursorPos.line - h + 1;
487+ if (cursorPos.line >= scrollOffset + ch) {
488+ scrollOffset = cursorPos.line - ch + 1;
479489 }
480490 // Scroll up if cursor is above viewport
481491 if (cursorPos.line < scrollOffset) {
@@ -484,23 +494,23 @@ function paintTextInput(
484494 setTextInputScroll(props, scrollOffset);
485495 }
486496
487- // Paint visible lines (grapheme-aware)
488- for (let row = 0; row < h; row++) {
497+ // Paint visible lines (grapheme-aware) in content area
498+ for (let row = 0; row < ch; row++) {
489499 const lineIdx = scrollOffset + row;
490500 if (lineIdx >= lines.length) break;
491501 const line = lines[lineIdx]!;
492- paintLineGraphemes(line, x, y + row, w, clipRect, props, buf);
502+ paintLineGraphemes(line, cx, cy + row, cw, clipRect, props, buf);
493503 }
494504
495505 // Paint cursor if focused
496506 if (props.focused) {
497507 const cursorOffset = getTextInputCursor(props);
498- const pos = offsetToWrappedPos(value, cursorOffset, w);
508+ const pos = offsetToWrappedPos(value, cursorOffset, cw);
499509 const screenRow = pos.line - scrollOffset;
500- if (screenRow >= 0 && screenRow < h && pos.col < w) {
501- const existing = buf.get(x + pos.col, y + screenRow);
510+ if (screenRow >= 0 && screenRow < ch && pos.col < cw) {
511+ const existing = buf.get(cx + pos.col, cy + screenRow);
502512 // Invert colors for cursor visibility
503- buf.set(x + pos.col, y + screenRow, {
513+ buf.set(cx + pos.col, cy + screenRow, {
504514 char: existing.char === " " && !existing.bgColor ? " " : existing.char,
505515 fgColor: existing.bgColor ?? "black",
506516 bgColor: existing.fgColor ?? "white",
packages/core/src/text-input.test.ts+55−0
@@ -319,4 +319,59 @@ describe("TextInput integration", () => {
319319 expect(value).toBe("text"); // Not consumed
320320 expect(receivedKey).toBe("a"); // Bubbled up
321321 });
322+
323+ test("padding insets content area", async () => {
324+ setup(20, 5);
325+ let value = "hi";
326+
327+ cel.viewport(() =>
328+ VStack({ width: 20, height: 5 }, [
329+ TextInput({
330+ width: 10,
331+ height: 3,
332+ padding: { x: 2, y: 1 },
333+ value,
334+ onChange: (v) => {
335+ value = v;
336+ },
337+ focused: true,
338+ }),
339+ ]),
340+ );
341+ await waitForRender();
342+
343+ const buf = cel._getBuffer()!;
344+ // With padding x=2, content starts at col 2. With padding y=1, content at row 1.
345+ // Content should be at (2, 1) not (0, 0)
346+ expect(buf.get(0, 0).char).toBe(" "); // padding
347+ expect(buf.get(1, 0).char).toBe(" "); // padding
348+ expect(buf.get(2, 1).char).not.toBe(" "); // content area — "h" or cursor
349+ });
350+
351+ test("padding affects intrinsic height", async () => {
352+ setup(20, 10);
353+ let value = "line1";
354+
355+ cel.viewport(() =>
356+ VStack({ width: 20, height: 10 }, [
357+ TextInput({
358+ width: 10,
359+ padding: { y: 1 },
360+ value,
361+ onChange: (v) => {
362+ value = v;
363+ },
364+ }),
365+ Text("after"),
366+ ]),
367+ );
368+ await waitForRender();
369+
370+ const buf = cel._getBuffer()!;
371+ // TextInput should have height 3 (1 content + 2 padding)
372+ // "after" should start at row 3
373+ let afterRow = "";
374+ for (let x = 0; x < 5; x++) afterRow += buf.get(x, 3).char;
375+ expect(afterRow).toBe("after");
376+ });
322377 });