·5 min read
Why a Desktop Grid zone looked right while dragging and re-inflated after the drop
A zone on my desktop looked the right size while I dragged it, and then grew back the moment I let go. The size was never wrong. The border was being stretched to wrap icons that no longer existed.
- Project
- Desktop Grid
- Date
- Jul 2026
- Stack
- Python, PySide6, ctypes
- Commit
- 5b9033d
Two pieces that interact
Desktop Grid draws labeled zones over the Windows desktop and remembers where icons belong. A pinned zone with "keep icons" stores a dictionary of icon key to position. A timer, every 2 seconds, compares what is on the desktop with that dictionary and puts strays back.
Separately, an earlier commit (8cefab6) made the drawn border hug overflowing icons. If a kept icon sits outside the zone rectangle, the border stretches outward to wrap it, while the stored zone size and position never change. The point was that the zone snaps back to its own shape when the icons fit again. The helper is short:
def _effective_rect(self, z):
r = self._z_rect(z)
if not (self.locked and z.get("pin") and z.get("icons")):
return r
ICON_W, ICON_H, PAD = 80, 92, 8
x1, y1, x2, y2 = r.left(), r.top(), r.right(), r.bottom()
for pos in z["icons"].values():
ix, iy = pos[0] - self.vx, pos[1] - self.vy
x1 = min(x1, ix - PAD)
y1 = min(y1, iy - PAD)
x2 = max(x2, ix + ICON_W + PAD)
y2 = max(y2, iy + ICON_H + PAD)
return QRect(x1, y1, x2 - x1, y2 - y1)Each of these is reasonable alone. Together they have a hole.
The symptom
While dragging a zone, the code shows the zone at its true size. After the drop, the border jumped outward to a rectangle that held nothing I could see.
The drag code is not where the problem is. _effective_rect returns the plain stored rectangle unless the overlay is locked and the zone is pinned with an icons dictionary. While I am editing zones the overlay is not locked, so that path is skipped and the zone looks correct. Once it is locked again, every stored position counts.
The cause
The enforcement loop, in the branch for an icon that is not on the desktop, did this:
cur = items.get(key)
if cur is None:
continue # icon gone (deleted/renamed); keep the slotThe comment explains the intent: keep the slot, so that an icon that returns lands where it belongs. For an icon that was actually deleted or renamed, the slot is never reclaimed. Its old coordinates stayed in z["icons"] indefinitely, and _effective_rect kept including them. A single deleted icon whose old spot lay outside the zone was enough to keep the border stretched toward it.
The commit message says it directly: deleted or renamed icons stayed in a zone's kept list forever, and their old coordinates kept stretching the border, so a zone looked right while dragging but re-inflated after the drop.
The fix
I did not want to delete the entry on the first miss, because the loop cannot tell a deleted icon from one that is briefly not reported. So the fix is a grace period. A counter keyed by zone and icon key counts consecutive ticks where the icon is absent:
mk = (id(zone), key)
if cur is None:
# icon gone (deleted/renamed): tolerate briefly, then
# forget it so its old spot stops stretching the border
self._miss[mk] = self._miss.get(mk, 0) + 1
if self._miss[mk] >= 5: # ~10 s absent
del kept[key]
self._miss.pop(mk, None)
cfg_dirty = zone_dirty = True
continue
self._miss.pop(mk, None)Seeing the icon again clears its counter, so only a continuous absence counts. At the 2-second interval set in an earlier commit (203168d, lowered from 4 seconds), 5 ticks is roughly 10 seconds. Pruning sets cfg_dirty and zone_dirty, so the config is saved and auto-fit zones re-fit around the remaining icons. The existing if moved_any or cfg_dirty check at the end of the pass repaints the overlay, so the border contracts without waiting for another event.
Tradeoffs I accepted
A counter in memory means a restart resets the grace period. That is fine, since an icon that is still absent just needs 5 more ticks. The key includes id(zone), which identifies the zone object for this process and is not stored.
Ten seconds is a judgment, not a measurement. I picked a value long enough that a brief absence does not wipe a slot and short enough that a deleted icon stops affecting the layout while I am still looking at the desktop. I have not tested what happens at other values.
The alternative I rejected was ignoring kept positions that are not on the desktop only when drawing. That would hide the symptom, but the dictionary would keep growing with entries for files that are gone, and the saved config would carry them forever.
What I took from it
The bug was in the interaction between a feature that reads state and a feature that was supposed to clean it up. The cleanup looked intentional because it had a comment, but the comment described a case, the returning icon, and ignored the other one. When a "keep" path has no exit, ask what would remove an entry for something that will never come back.
#python#pyside6#windows#debugging
Keep reading
Have a project in mind?