Repository navigation
feat(desktop): drag icons on a grid and remember the layout - #168
Conversation
…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
left a comment
There was a problem hiding this comment.
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:
- 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.
- 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.
291b84b
Ziqian-Huang0607
left a comment
There was a problem hiding this comment.
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.
md-abu-kayser
left a comment
There was a problem hiding this comment.
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.
|
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! |
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.
{ 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.onMouseLeave, so a quick drag can't lose the icon. Hold-to-drag (0.3s) is unchanged, so clicks never move an icon.preferences.desktopIconswith 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.desktopIconsis free-form in the schema, soPUT /api/users/preferencesruns it throughsanitizeLayout(): safe ids, integer cells in range, at most 200 icons, one icon per cell. Anything that isn't an object gets a 400.DesktopContextMenufor 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 insrc/hooks/useDesktopLayout.js.Testing
tests/desktopLayout.test.js(23 tests) covers:desktopIcons, restore from preferences, guest localStorage, shrink/grow restore, no layout before measuringdesktopIconsnext 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.