Skip to content

fix(datagrid): cap the width of a column sized to fit its content#1859

Merged
datlechin merged 2 commits into
mainfrom
fix/datagrid-fit-column-width-cap
Jul 13, 2026
Merged

fix(datagrid): cap the width of a column sized to fit its content#1859
datlechin merged 2 commits into
mainfrom
fix/datagrid-fit-column-width-cap

Conversation

@datlechin

Copy link
Copy Markdown
Member

Problem

Running Size All Columns to Fit (or Size to Fit, or double-clicking a column divider) on a table with a long TEXT/JSON value stretched that column across the screen.

DataGridCellFactory had three near-identical width calculators. Two clamped to an 800pt ceiling; calculateFitToContentWidth was a copy that had lost both bounds, so it measured the full length of a cell's text and returned it unclamped. A 4,000 character value produced a column thousands of points wide. All three fit entry points call that one function.

Second half of the bug: a programmatic column.width = x posts the same columnDidResizeNotification a drag does, so the oversized width was captured into the saved column layout and restored unclamped on every later open. Capping the calculation alone would leave an already stretched column stretched after a restart.

Fix

  • Collapse the two content calculators into one bounded measurement (cap, measured-char limit, early exit once the cap is reached). The duplication was the defect, so this removes it instead of pasting a min() into the copy.
  • Auto-fit cap: clamp(0.5 x visible grid width, 300, 800). A fitted column never takes more than half the visible grid, never drops below 300pt in a narrow window, and never exceeds the 800pt ceiling the initial layout path already applied, so first load and explicit "Size to Fit" stop disagreeing.
  • Data columns now carry NSTableColumn.maxWidth = 1200. AppKit documents this as a ceiling "whether the column is resized by the user or programmatically", so it clamps drags, auto-fit, and widths restored from disk in one place. That is what corrects a layout already saved with an oversized width: it is clamped on open and rewritten on the next capture.
  • Size All Columns to Fit now skips hidden columns.
  • Removed calculateColumnWidth, which no production code called.

Prior art

Sequel Ace (same AppKit hook) hard caps auto-fit at 400pt and truncates the measured string at 500 chars. Excel caps at 255 characters. TablePlus has no cap: TablePlus/TablePlus#1461 is this same bug, and their answer was a "reset column widths" command rather than a limit.

Tests

calculateFitToContentWidth had no coverage at all, which is how this shipped.

  • FitToContentWidthTests: long value caps at 800, half-viewport rule, 300pt floor in a narrow window, short values still size to content, long header alone, fitted width never exceeds the column ceiling.
  • DataGridColumnPoolTests: an oversized saved width is clamped to the ceiling on restore; data columns carry the min and max bounds.

All pass, along with the existing column width and column pool suites.

@datlechin datlechin merged commit 68ba2f3 into main Jul 13, 2026
2 checks passed
@datlechin datlechin deleted the fix/datagrid-fit-column-width-cap branch July 13, 2026 11:12
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant