Skip to content

[BUG] Legacy (SwiftUI) grid: dragged icon stays stuck on screen after release #263

Open

Description

@Dino0005

Environment

  • Xcode 27.0 (27A266a), macOS 27.0.1
  • LaunchNext 2.4.2 (commit ccc5ca0)
  • Engine: Legacy / SwiftUI grid (useCAGridRenderer = false)

Description

When moving an icon on the main grid, the drag preview often stays frozen where the
mouse was released, and the drop is never completed.

Image

Cause

The drag is handled by a DragGesture attached to each tile in itemDraggable.
Its onEnded is the only place that calls finalizeDragOperation and clears
draggingItem. With debug logging I found that, right after the drag starts,
the tile hosting the gesture is torn down:

drag started
dragged tile disappeared
gridRefreshTrigger changed (draggingItem=true)

Two things destroy it:

  1. The pages use .id("grid_\(index)_\(appStore.gridRefreshTrigger.uuidString)"),
    and triggerGridRefresh() is called from many places (rescans, icon loading,
    saves), so a refresh during a drag rebuilds every tile.
  2. The tile itself is re-rendered once the drag starts (hidden, reordered).

When the hosting view goes away, SwiftUI cancels the gesture and onEnded is
never called
, so draggingItem stays set and the preview remains on screen.

Fix (works for me)

  1. Window-level mouse-up fallback: when a normal drag starts, install
    NSEvent.addLocalMonitorForEvents(matching: [.leftMouseDragged, .leftMouseUp]),
    the same approach already used for the folder handoff drag. On mouse-up it calls
    a shared endItemDrag(...), which runs finalizeDragOperation exactly once,
    whichever arrives first between the monitor and onEnded. If the gesture stops
    delivering updates for more than 0.1 s, the monitor keeps the preview following
    the pointer via handleHandoffDragMove.
  2. Defer grid rebuilds during a drag: the pages use a separate
    @State displayedGridRefreshID, which follows gridRefreshTrigger only while
    draggingItem == nil. Any pending refresh is applied as soon as the drag ends.

With both changes the drag works correctly (same page, across pages,
folder creation).

Activity

  1. added
    bug
    on Oct 2, 2026
  2. RoversX commented on Oct 3, 2026

    @RoversX
    Owner

    Thank you for your detailed investigation and suggested fix. The legacy SwiftUI engine is no longer actively maintained. 🥲In fact, I plan to remove the legacy engine. Please switch to the new engine in Settings. If you encounter any issues, please let me know.

  3. RoversX commented on Oct 3, 2026

    @RoversX
    Owner
  4. Dino0005 commented on Oct 3, 2026

    @Dino0005
    ContributorAuthor

    Thanks for the reply, and for 4dc37ab and 63e8953! Both fixes work for me.

    "Next Engine" was already selected. I had turned the
    "Next Engine With Core Animation" toggle off on purpose, because with it
    enabled I couldn't place icons where I wanted. That is how I ended up on the
    SwiftUI grid and hit the stuck-drag bug.

    So the real problem is drag placement on the Core Animation grid. I looked
    into it and found these causes (all in CAGridView):

    1. Gaps don't stay under the pointer. Icons shift visually, but hit-testing
      still uses the original layout. After a gap opens, the cell under the
      pointer is still treated as holding the icon that moved away, so the gap
      jumps elsewhere or a folder-merge starts over an empty slot. Aiming between
      two icons while dragging left-to-right ends up after the second one.
      The SwiftUI grid hit-tests the displayed layout (visualItems), so the gap
      stays where you point.
    2. Gap between cells goes entirely to the left cell. gridPositionAt()
      uses Int(x / stride), while the SwiftUI grid splits the spacing in half
      (GeometryUtils.indexAt: (x + spacing / 2) / stride).
    3. Dropping into the first column flips to the previous page. The edge zone
      is 60 pt inside the grid (edgeDragThreshold) and contentInsets.left is 0
      in windowed mode, so it overlaps the first column's drop area.
    4. Edge paging skips pages. After a flip edgeDragTimer is cleared and the
      next mouse move immediately arms a new 0.4 s timer, so a pointer resting
      past the edge flips every ~0.4 s.

    What I changed locally, and it now feels as precise as the SwiftUI grid:

    • Hit-test the displayed layout: the gap stays under the pointer, and a merge
      targets the icon actually shown in the cell.
    • Split the spacing between neighbouring cells, as GeometryUtils.indexAt does.
    • Edge zone of 15 pt (same as the SwiftUI grid). In windowed mode, flip only
      once the pointer leaves the grid, so the first and last columns stay usable.
    • 0.6 s cooldown after each edge flip.

    Environment: Xcode 27.0 (27A266a), macOS 27.0.1, LaunchNext 2.4.2 (63e8953).
    The Core Animation issues are still present on 63e8953.

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    bug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions