Skip to content

fix(canvas): let region auto-sort use any grid shape and clear place that fits - #187

Merged
howdeploy merged 1 commit into
howdeploy:mainfrom
BIackFIame:pr185/autosort-feasible
Nov 1, 2026
Merged

howdeploy merged 1 commit into
howdeploy:mainfrom
BIackFIame:pr185/autosort-feasible

Conversation

@BIackFIame

Copy link
Copy Markdown
Contributor

Follows up on Auto-sort (#163) and the 1.8.5 known limitation about conservative region sorting.

The 1.8.5 notes list region sorting as conservative: it can reject layouts that fit. This PR fixes the two causes I could reproduce. Layouts that sorted before land exactly where they did.

Failing scenarios (all on 1.8.5, snapping on, 700 x 430 windows)

  1. Grid shape is fixed. Region 2200 x 560 at (3000, 0), so the content area is 2160 x 480 (20px edges, 60px title). Three windows, Grid. Sorting always builds the near-square 2 x 2 grid (880 tall), which does not fit, so the sort is refused. One row (3 x 700 + 2 x 20 = 2140 wide, 430 tall) fits: x = 3030 / 3750 / 4470, y = 90.
    The same happens with groups. By status, one needs_approval and three working windows in a 1500 x 1500 region (content 1460 x 1420). The usual shape (1 column + 2 columns) is 2160 wide and is refused. One column per group is 1440 x 1330 and fits.
  2. Only the exact center is checked. Region 3000 x 2400, two windows (block 1420 x 430), a 400 x 300 note in the middle at (4300, 1050). The centered block covers the note, so the sort is refused, though the block fits 20px below the note at y = 1370 (or above it at y = 600). The same applies when a partly overlapping region covers the center. The code even had a comment about this: "reject an obstructed centered block; add bounded placement search only if requested".

Root cause

centerInRegion took the single arrangement from arrangeCanvasAutoSort, which is always the near-square grid (ceil(sqrt(n)) columns, per group for status/project). It tried one position, the rounded center, and returned null if that position overflowed or touched any obstacle.

Rule after the fix

For a region sort, in this order:

  • Shape: first the usual near-square grid. If that does not fit, every other column limit, nearest to the usual count first, fewer columns before more on a tie. For status/project the limit applies to each group, and the order of groups and of windows is unchanged. The task tree keeps its own shape.
  • Place: the region's center if it is clear. Otherwise the clear place nearest the center by distance, topmost and then leftmost on a tie. The 20px edges, 60px title band, 20px obstacle gap and 10px grid all still apply.
  • The first shape that has any clear place wins. The sort is refused (same message as before) only when no shape fits anywhere in the region.

Candidate places are the region edges, the center, and each obstacle's edges plus the gap. Any free position can slide up and left until an edge or obstacle stops it, so this set contains a free place whenever one exists. The search is O(shapes x candidates^2 x obstacles), and only obstacles that overlap the region count.

Same input gives the same output. List order still does not matter, and sorting twice is a no-op. The canvas (non-region) sort is untouched: arrangeCanvasAutoSort still always uses the near-square grid and the right/down shift.

What changes for existing layouts

  • A region sort that used to succeed gives the same result, because the usual shape and the center are tried first.
  • Region sorts that used to show "does not fit" may now succeed with another column count or an off-center position.
  • One sentence about region sorting was added to docs/UI_CONTRACT (en/ru/zh-CN). No menu or i18n changes.

Validation

  • tests/canvas-auto-sort.test.mjs, new or updated:
    • another column count: the Grid and status scenarios above, each with the explicit valid placement asserted first, then asserted as the exact result. A roomy region still gets the 2 x 2 grid.
    • placement beside an obstacle: the note scenario with the explicit placement (3790, 1370) / (4510, 1370), checked for idempotence. Also a partly overlapping region.
    • the existing "refuses obstacles" check: an obstacle on the center now moves the block clear of it. An obstacle covering the whole content area is still refused.
    • a seeded property test (300 random regions/notes, all four criteria, snap on/off): every plan stays inside the content area, keeps the gap from notes and from other windows, and does not depend on list order.
  • With the 1.8.5 canvasAutoSort.ts the first three tests fail (3 fail / 28 pass). With this change, 32 / 32 pass.
  • npm run build, npm run audit:secrets and tests/renderer-bundle-splitting.test.mjs pass. The eager renderer entry is 1,198,508 bytes, unchanged (auto-sort is loaded on demand).
  • Full suite (node --test tests/*.test.mjs): 2654 tests, 2648 pass, 0 fail, 6 skipped.

A region sort refused whenever the near-square grid did not fit the
region or anything covered the region's center, even when the windows
fit in another column count or beside the obstacle.

Region sorting now keeps the near-square grid when it fits and
otherwise tries the other column counts, nearest to the usual one
first (fewer columns on a tie). The block goes to the region's center
when that is clear, else to the clear place nearest the center
(topmost, then leftmost). Candidate places are the region edges, the
center and the obstacles' edges, which covers every free position.
The task tree keeps its shape. Layouts that sorted before are
unchanged.
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 1, 2026 •

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-01T10:44:37.056106Z f524f93 PR opened
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@BIackFIame BIackFIame changed the title fix(canvas): LLet region auto-sort use any grid shape and clear place that fits fix(canvas): let region auto-sort use any grid shape and clear place that fits Nov 1, 2026
@howdeploy
howdeploy merged commit e3ff1f4 into howdeploy:main Nov 1, 2026
7 checks passed
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.

2 participants