Krolyxon
Commit c7666c2

Branches

Refs →

budctlc7666c2

budctl-gui: try again when the window gets focus after a failure

If the buds were not connected (or the channel was busy, or a command
failed), getting focus reads them again, as an old reading already did:
open the case, click the window, and it connects.

No new dependencies.
Author
krolyxon
Date
2026-10-28 01:17 +0530
Parent
6dcfe3c
Commit
c7666c27aef0e9bc46cdd69761c69cc66f232077

+22 −1

1 file changed

  1. M crates/budctl-gui/src/main.rs +22 −1
M crates/budctl-gui/src/main.rs View
@@ -400,7 +400,11 @@ impl App {
400400 Message::Worker(event) => self.answer(event),
401401 Message::Focused => {
402402 self.refresh_palette();
403− if self.read_at.is_some_and(|at| at.elapsed() >= STALE) && self.in_flight.is_empty() {
403+ // The phone may have changed something, or the buds may be back: read again
404+ // when the reading is old or the last command failed.
405+ let stale = self.read_at.is_some_and(|at| at.elapsed() >= STALE);
406+ let failed = matches!(self.link, Link::Failed { .. });
407+ if (stale || failed) && self.in_flight.is_empty() {
404408 self.refresh();
405409 }
406410 }
@@ -1490,6 +1494,23 @@ mod tests {
14901494 assert_eq!(app.items().len(), 10);
14911495 }
14921496
1497+ #[test]
1498+ fn focus_tries_again_after_a_failure() {
1499+ let (mut app, commands) = live();
1500+ let _ = commands.try_recv();
1501+ let _ = app.update(Message::Worker(Event::Failed {
1502+ kind: Kind::NotFound,
1503+ message: "realme Buds T310 is paired but not connected".to_owned(),
1504+ }));
1505+ let _ = app.update(Message::Focused);
1506+ assert!(matches!(commands.try_recv(), Ok(Command::Read)));
1507+ assert_eq!(app.link, Link::Connecting);
1508+ // A fresh reading is not read again on focus.
1509+ let _ = app.update(Message::Worker(Event::Read(Box::new(reading()))));
1510+ let _ = app.update(Message::Focused);
1511+ assert!(commands.try_recv().is_err());
1512+ }
1513+
14931514 #[test]
14941515 fn the_preset_in_use_is_not_written_again() {
14951516 let (mut app, commands) = live();