Skip to content

feat(desktop): drag icons on a grid and remember the layout - #168

Merged
md-abu-kayser merged 2 commits into
codehubbers:mainfrom
dailker:feat/desktop-icon-layout
Dec 2, 2026
Merged

md-abu-kayser merged 2 commits into
codehubbers:mainfrom
dailker:feat/desktop-icon-layout

Conversation

@dailker

@dailker dailker commented Dec 1, 2026 •

Copy link
Copy Markdown
Member

Closes #93

What and why

Desktop icons could already be dragged, but nothing was remembered, a fast drag dropped the icon as soon as the cursor outran it, and the window size was only read once. This makes the layout a real, saved grid.

  • Grid snapping, no overlaps. Icons live in cells ({ col, row }). Dropping on an occupied cell swaps the two icons, and dropping outside the grid puts the icon back. While dragging, the target cell is highlighted.
  • Pointer events with capture, instead of mouse events plus onMouseLeave, so a quick drag can't lose the icon. Hold-to-drag (0.3s) is unchanged, so clicks never move an icon.
  • Saved per user. The layout goes to preferences.desktopIcons with a 1s debounce, sending only that key (the API merges per key, Music Player: keep the playlist after a page reload #127). A per-user localStorage copy covers guests and the local admin, and acts as a fallback.
  • Validated on the server. desktopIcons is free-form in the schema, so PUT /api/users/preferences runs it through sanitizeLayout(): safe ids, integer cells in range, at most 200 icons, one icon per cell. Anything that isn't an object gets a 400.
  • Re-flow without losing your layout. When the window shrinks, icons that don't fit move into free cells, but the saved layout isn't touched, so they go back when the window grows. Moving one icon pins the others where they are instead of shuffling them.
  • No hydration mismatch (Hydration Error #4). Icons render only after the client has measured the window.
  • Sort by name / Auto-arrange. Right-click the empty desktop. The menu is deliberately minimal, so 🖱️ Right-click context menus for desktop, icons and files #94 can grow DesktopContextMenu for icons and files. Strings are in all five locales.

The grid maths lives in src/lib/desktopLayout.js (pure functions) and the state and persistence in src/hooks/useDesktopLayout.js.

Testing

  • tests/desktopLayout.test.js (23 tests) covers:
    • grid maths, sanitising, re-flow, swap, sort and auto-arrange
    • the hook: debounced save with only desktopIcons, restore from preferences, guest localStorage, shrink/grow restore, no layout before measuring
    • drag via pointer events: drop on a cell, quick click doesn't move, drop outside reverts
    • the preferences API cleaning and rejecting desktopIcons
  • Full suite: 395/395 passing. Lint and Prettier are clean on the changed files.
  • Checked in the browser (next dev, local admin): drag, swap, reload restores the layout, the right-click menu works, re-flow at 700×560 and back, and no hydration warnings.

Known limit: if the window has fewer cells than there are apps (for example 8 cells for 9 apps), the extra icon sits just past the right edge rather than overlapping another one. #99 (responsive layout) is the place for a smaller grid on phones.

…ers#93)

Icons snap to grid cells and never share one: dropping on an occupied
cell swaps the two icons, and dropping outside the grid puts the icon
back. Dragging uses pointer events with capture, so a fast drag no
longer loses the icon when the cursor outruns it.

The layout is saved per user in preferences.desktopIcons (debounced,
only that key) with a per-user localStorage copy for guests and as a
fallback. The preferences API cleans the layout before storing it.

Icons that don't fit a smaller window re-flow into free cells without
overwriting the saved layout, so they go back when the window grows.
Positions are only applied after the client has measured the window,
so server and client HTML match.

Right-clicking the empty desktop offers "Sort by name" and
"Auto-arrange icons" (translated in all five locales).

@Ziqian-Huang0607 Ziqian-Huang0607 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewed the full diff and ran the tests locally (desktopLayout + i18n parity + preferences API: 50/50 green, CI green everywhere).

The design decisions here are all sound:

  • Pure grid maths in lib/desktopLayout.js, state and persistence in the hook, thin components. That split is exactly right and it shows in the test quality.
  • Pointer events with setPointerCapture fix the 'fast drag loses the icon' bug properly, and hold-to-drag means clicks never move icons.
  • The saved-vs-resolved layout split is the clever part: the saved layout keeps cells that do not fit the current window, so shrink then grow restores your arrangement, and committing on top of the visible layout pins auto-placed icons instead of shuffling them.
  • Server side is taken seriously: sanitizeLayout() drops unsafe ids (including proto), non-integer or out-of-range cells, caps at 200 icons, guarantees one icon per cell, and a non-object gets a 400. The debounced save sends only the desktopIcons key so it cannot clobber other preferences.
  • Guests and local admin get the per-user localStorage copy, and icons render only after the client measures the window, so no hydration mismatch.
  • The context menu is deliberately minimal and keyboard navigable, a good foundation for #94.

Two non-blocking notes:

  1. The single-click hint notification ('Double-click to open / Hold 0.3s to drag') is still hardcoded English. Pre-existing string, but this PR localized everything else it touched, so it stands out now.
  2. The documented known limit (more apps than cells overflows past the right edge) is acceptable for now, and #99 is the right place to fix it.

Approve from me.

MXD-K1
MXD-K1 previously approved these changes Dec 2, 2026
@Ziqian-Huang0607
Ziqian-Huang0607 dismissed stale reviews from MXD-K1 and themself via 291b84b December 2, 2026 11:20

@Ziqian-Huang0607 Ziqian-Huang0607 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Re-approving after the branch update. The only new commit is the merge of main that I resolved and pushed myself: the conflicts were the adjacent locale keys and the User.js preferences block from #170, resolved by keeping both sides. Verified locally before pushing: all five locale files parse, prettier clean, desktopLayout + launcher + i18n + preferences suites 71/71 green, and CI has just gone green on the merged result.

@dailker
dailker requested a review from MXD-K1 December 2, 2026 13:11
@dailker dailker added the enhancement New feature or request label Dec 2, 2026
@dailker dailker self-assigned this Dec 2, 2026
@dailker dailker added the frontend UI / React / Next.js pages label Dec 2, 2026
@dailker
dailker requested a review from md-abu-kayser December 2, 2026 13:11

@md-abu-kayser md-abu-kayser left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @dailker for the implementation.

I reviewed the updated PR, including the latest merge from "main".

The implementation is well-structured, with grid calculations separated into reusable helper functions and layout state management handled independently through a custom hook.

The pointer-based drag-and-drop implementation, collision handling, responsive reflow, and per-user persistence are well considered. I also reviewed the server-side layout sanitization and the associated test coverage.

I did not find any blocking correctness issues in the reviewed changes. The implementation looks consistent with the intended functionality, and the reported CI checks are passing.

Approved.

@md-abu-kayser
md-abu-kayser merged commit 768bbd0 into codehubbers:main Dec 2, 2026
5 checks passed
@md-abu-kayser

Copy link
Copy Markdown
Contributor

Thanks @dailker for the opportunity to review this PR.

Glad to contribute to OrbitOS through the review process. Looking forward to collaborating on more open-source improvements.

Keep up the great work!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request frontend UI / React / Next.js pages

Projects

None yet

Development

Successfully merging this pull request may close these issues.

🗂️ Desktop icons: drag to rearrange and persist positions

4 participants