Experience
Projects
My Stack
Case studies
Contact me
Open my CV

·5 min read

My auto-fit zone never grew: enforcement put the icon back first

I had a zone on my desktop that was supposed to resize itself around its icons, and it never did. I would drag an icon past the edge, and about four seconds later it jumped back to where it started. The border stayed exactly the size it was. Two features were each doing their job correctly, and together they cancelled out.

Project
Desktop Grid
Date
Jul 2026
Stack
Python, Qt, Win32
Commit
203168d

Two features that agreed on nothing

Desktop Grid draws zones over the Windows desktop and can pin icons to them. A pinned zone remembers where each of its icons sits, in a kept dictionary keyed by icon, and a timer, enforce_loop, compares reality with that memory. The interval was a constant at the top of main.py:

python
ENFORCE_MS = 4000        # pinned-zone icon enforcement interval

The loop has two cases for an icon the zone owns. If the icon is still inside the zone but has moved, the new position is remembered, so you can rearrange inside a zone freely. If the icon is outside the zone and not where it was kept, the loop treats it as an escape:

python
elif [sx, sy] != pos:  # escaped the zone -> put it back
    di.set_position_screen(idx, pos[0], pos[1])
    moved_any = True

That is the right behaviour for a plain pinned zone. Windows likes to rearrange desktop icons, and the point of pinning is to undo that.

Then there is the second feature, auto-fit. A zone with auto_fit set is supposed to resize its rectangle to wrap whatever icons it owns. The loop already collected zones into a refit list when their icon set or layout changed, and called fit_zone on them after the pass.

The problem is the order of those two things. Auto-fit can only react to a change it can see. Dragging an icon outside the zone is the change that should make the zone grow, but that same drag is exactly what the first branch treats as an escape. Enforcement ran, put the icon back, and the zone found nothing to refit around. The icon left, the loop returned it, the zone never learned it had left.

What the symptom looked like

From the user's side nothing was broken in an obvious way. No error, no log line. The icon moved where I dropped it for a moment and then snapped back. The zone border did not change. If I had not known auto-fit existed I might have decided zones simply do not resize.

The commit message states the root cause plainly: auto-keep enforcement was returning any icon moved outside the zone before auto-fit could react.

The change

For zones with auto-fit enabled, an icon outside the zone is no longer an escape. The loop records the new position as the kept one and marks the zone dirty, so it lands in the refit list:

python
elif [sx, sy] != pos:
    if zone.get("auto_fit"):
        # auto-fit zones follow their icons instead of
        # dragging them back - the border re-fits around them
        kept[key] = [sx, sy]
        cfg_dirty = zone_dirty = True
    else:  # escaped the zone -> put it back
        di.set_position_screen(idx, pos[0], pos[1])
        moved_any = True

The rest of the loop was already doing the right thing with that dirty flag:

python
if zone_dirty and zone.get("auto_fit"):
    refit.append(zone)
...
for zone in refit:
    self.fit_zone(zone)

So the fix is a branch in the right place, ten lines added and four removed in main.py. Non auto-fit zones behave exactly as before.

I also lowered ENFORCE_MS from 4000 to 2000. The reason is feel, not correctness. With auto-fit now relying on the loop to notice a drag and re-fit the border, a four second wait before the border moved looked like the feature was lagging. Two seconds reads as live. I did not measure the cost of polling twice as often. The loop returns early when the overlay is busy or unlocked, or when no zone is pinned, so on an idle desktop the work per tick is small, but that is a reading of the code, not a benchmark.

The follow-up: the border that hugs

A related commit, 8cefab6, handled a neighbouring case. If a kept icon sticks out past the zone rectangle, the drawn border now stretches outward to wrap it with padding, while the stored zone size and position never change. When the icons fit again the zone snaps back to its own shape. Label, gear, grips and hit-testing follow the effective rectangle, and the overlay repaints when enforcement moves icons.

I like that split because it separates what the user configured from what is currently drawn. The stored rectangle is the user's intent. The effective rectangle is derived. Mixing them is how a temporary overflow would permanently rewrite someone's layout.

What I took from it

Two correct mechanisms can produce a wrong system when one of them eats the signal the other needs. Enforcement and auto-fit both read the same event, an icon outside its zone, and they disagreed about what it meant. The fix was not to weaken either feature. It was to make the meaning depend on the zone's mode, in the one place where the decision is made, instead of letting the first handler to run decide for everyone.

If you build a periodic corrector next to something that reacts to change, check which of them sees the change first. If the corrector runs first and erases it, the reactive part will look broken, and nothing in the logs will say why.

#python#qt#windows#state#debugging

Keep reading

Have a project in mind?