Skip to content

Keep session alive across transient device disappearance#79

Closed
ImmortalDragonm wants to merge 1 commit into
ergohaven:mainfrom
ImmortalDragonm:fix/keep-session-on-transient-device-loss
Closed

Keep session alive across transient device disappearance#79
ImmortalDragonm wants to merge 1 commit into
ergohaven:mainfrom
ImmortalDragonm:fix/keep-session-on-transient-device-loss

Conversation

@ImmortalDragonm

Copy link
Copy Markdown
Contributor

Problem

After importing an .entlayout, the layout-image / .entlayout Export actions could go completely dead — clicking Export did nothing and no file dialog appeared.

Root cause

The export menu items (and other layout-gated UI) are enabled only while a keyboard is connected — self.layout.is_some(). So a dead Export button means the layout had been cleared out from under it.

A large import writes a burst of HID transactions (keymap for every layer × key, encoders, the macro buffer, combos). Under that load the board briefly re-enumerates on USB. The very next device scan sees an empty list and immediately tears the whole session down (clear_connected_keyboard_statelayout = None, selected_device = None). On Linux the follow-up scan can then hit the udev-rules guard and refuse to auto-reconnect, leaving Export greyed out until the user manually re-selects the device.

Fix

Debounce the disappearance in apply_device_scan_result:

  • Require the selected device to stay absent for 3 consecutive scans (~3s) before clearing state. Within that window the device list, selection and layout are preserved untouched, so Export and other actions keep working.
  • When the device reappears after a transient absence, the previously opened HID handle is stale, so force a reconnect to reopen it even when a layout is still present.

A genuine unplug still surfaces as "No device detected" after the short debounce window.

Testing

  • cargo build, cargo test (59 passed).
  • Change is confined to the device-scan reconciliation path; no behavior change when the device never disappears.

🤖 Generated with Claude Code

After importing an .entlayout, the export actions (and other layout-gated
UI) could go dead: clicking Export did nothing and opened no file dialog.
The menu items are enabled only while a keyboard is connected
(self.layout.is_some()), so the layout had been cleared out from under them.

A large import writes a burst of HID transactions (keymap for every layer
and key, encoders, the macro buffer, combos). Under that load the board
briefly re-enumerates on USB. The very next device scan sees an empty list
and immediately tore the whole session down (layout = None, selected
device = None) via clear_connected_keyboard_state, and on Linux the
follow-up scan could then hit the udev-rules guard and refuse to
auto-reconnect, leaving export permanently greyed out until a manual
re-select.

Debounce the disappearance: require the selected device to stay absent for
three consecutive scans (~3s) before clearing state. While within the
window the device list, selection and layout are preserved untouched, so
export and other actions keep working. When the device reappears after a
transient absence the previously opened HID handle is stale, so force a
reconnect to reopen it even when a layout is still present.

A genuine unplug still surfaces as "No device detected" after the short
debounce window.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01S2vEWd6sxhZLCa4PULq5Ti
@ImmortalDragonm

Copy link
Copy Markdown
Contributor Author

Closing: this was based on a wrong hypothesis. Reproducing the reported "menu dead after import" on real hardware showed the device does not disappear/re-enumerate during import (layout stays connected the whole time) — the actual cause is synchronous native file dialogs blocking the UI thread, fixed in #80. This debounce addresses a transient-disappearance scenario that was never actually observed, and it delays the genuine-unplug notice by ~3s, so it's not worth merging.

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant