From b83481e4cc389e67ca263ccb76e49231db9211b2 Mon Sep 17 00:00:00 2001 From: faumaray Date: Mon, 23 Nov 2026 08:31:11 +0300 Subject: [PATCH] voelin-gpui: refresh toggle icon button tooltips when their label changes Key `IconButton`'s GPUI element ID by both its base ID and its label (`ElementId::NamedChild(Arc::new(self.id), self.label)`) while keeping `.debug_selector(...)` equal to `self.id.to_string()`. When a toggle button's label changes (e.g. `call-mic`, `call-sound`, `member-mute`, `user-mic`, `user-sound`, `voice-stage`), GPUI drops stale hover/tooltip state from the old ID and builds the updated tooltip label while all existing debug selectors stay unchanged. Add a GPUI test covering `call-mic`, `call-sound`, `member-mute`, and `voice-stage` across state flips while hovered. Co-Authored-By: Gemini 4 Neon --- crates/voelin-gpui/src/shell/widgets.rs | 28 ++++- crates/voelin-gpui/src/view/voice_tests.rs | 118 +++++++++++++++++++++ 2 files changed, 145 insertions(+), 1 deletion(-) diff --git a/crates/voelin-gpui/src/shell/widgets.rs b/crates/voelin-gpui/src/shell/widgets.rs index 572b2fe..cf2b4aa 100644 --- a/crates/voelin-gpui/src/shell/widgets.rs +++ b/crates/voelin-gpui/src/shell/widgets.rs @@ -4,6 +4,8 @@ //! counterparts of `ui/components/avatar.slint`, `button.slint` and //! `shells/common.slint`'s `Panel`. +use std::sync::Arc; + use gpui_kit::{ AnyElement, App, Background, ClickEvent, FontWeight, InteractiveElement as _, IntoElement, ParentElement as _, RenderOnce, SharedString, StatefulInteractiveElement as _, Styled as _, @@ -340,8 +342,14 @@ impl RenderOnce for IconButton { let icon_size = if self.size >= 40.0 { m.icon_md } else { m.icon_sm.max(self.size * 0.5) }; let label = self.label.clone(); let selector = self.id.to_string(); + #[cfg(test)] + let tooltip_selector = selector.clone(); + // Key the element by both its id and its label so GPUI drops stale + // hover/tooltip state when a toggle button's label changes, while + // keeping the debug selector stable for tests. + let id = ElementId::NamedChild(Arc::new(self.id), self.label); let mut button = div() - .id(self.id) + .id(id) .debug_selector(move || selector) .size(px(self.size)) .flex_none() @@ -357,6 +365,8 @@ impl RenderOnce for IconButton { // The tooltip is there while the button is off too: it says why (the voice // page's stage button in a window too short for the larger stage). button = button.tooltip(move |window, cx| { + #[cfg(test)] + tests::record_tooltip(&tooltip_selector, &label); gpui_kit::component::tooltip::Tooltip::new(label.clone()).build(window, cx) }); if !self.enabled { @@ -488,11 +498,27 @@ pub fn chip( .child(text.into()) } +/// Tooltips built by [`IconButton`] since the last call, as `(selector, label)`. +#[cfg(test)] +pub(crate) fn take_tooltips() -> Vec<(String, String)> { + tests::TOOLTIPS.with(|log| std::mem::take(&mut *log.borrow_mut())) +} + #[cfg(test)] mod tests { + use std::cell::RefCell; + use super::*; use crate::theme::Choice; + thread_local! { + pub(super) static TOOLTIPS: RefCell> = const { RefCell::new(Vec::new()) }; + } + + pub(super) fn record_tooltip(selector: &str, label: &str) { + TOOLTIPS.with(|log| log.borrow_mut().push((selector.to_owned(), label.to_owned()))); + } + #[test] fn two_letters_are_written_large_enough_and_one_letter_under_28_pixels() { // The tree's 22 pixel avatars: one letter, half the size. diff --git a/crates/voelin-gpui/src/view/voice_tests.rs b/crates/voelin-gpui/src/view/voice_tests.rs index 27730cb..b52147b 100644 --- a/crates/voelin-gpui/src/view/voice_tests.rs +++ b/crates/voelin-gpui/src/view/voice_tests.rs @@ -1014,3 +1014,121 @@ fn the_smaller_stage_centres_few_people_and_does_not_clip_many(cx: &mut TestAppC "25 people overflow horizontally to scroll: last={last:?}, stage={stage:?}" ); } + +/// Hovering a toggle icon button and flipping its state drops GPUI's stale +/// active tooltip and builds the updated tooltip label while keeping the +/// button's `debug_selector` intact (`"call-mic"`, `"call-sound"`, +/// `"member-mute"`, `"voice-stage"`). +#[gpui::test] +fn toggle_button_tooltips_refresh_after_click(cx: &mut TestAppContext) { + use std::time::Duration; + + use crate::shell::actions::SetMemberMuted; + use crate::shell::widgets::take_tooltips; + + let (shell, cx) = on_the_voice_page(cx, Switches::default()); + let hover_tooltip = + |cx: &mut VisualTestContext, selector: &str, dx: f32| -> Vec<(String, String)> { + let bounds = at(cx, selector).unwrap_or_else(|| panic!("{selector} is not on screen")); + let pos = gpui_kit::point(bounds.center().x + px(dx), bounds.center().y); + take_tooltips(); + cx.simulate_mouse_move(pos, None, Default::default()); + frame(cx); + cx.executor().advance_clock(Duration::from_secs(2)); + frame(cx); + take_tooltips() + }; + + // 1. Call bar microphone: click sends the command while the button is still + // unmuted, hover shows "Microphone on", then the engine confirms mute while + // the pointer stays on the button -> stale tooltip is dropped and + // "Microphone off" is built. + click(cx, "call-mic"); + assert_eq!( + hover_tooltip(cx, "call-mic", 0.0), + vec![("call-mic".into(), "Microphone on".into())] + ); + shell + .events + .send(Event::State { + session: sample::DEMO as u64, + state: SessionState { + voice: VoiceState::Connected, + own_channel: Some(2), + own_client: Some(1), + input_muted: true, + ..Default::default() + }, + }) + .unwrap(); + frame(cx); + assert!(at(cx, "call-mic").is_some(), "debug_selector stays intact"); + assert_eq!( + hover_tooltip(cx, "call-mic", 1.0), + vec![("call-mic".into(), "Microphone off".into())] + ); + + // 2. Call bar sound: click + hover "Sound on", engine confirms deafen -> + // stale tooltip dropped and "Sound off" built. + click(cx, "call-sound"); + assert_eq!( + hover_tooltip(cx, "call-sound", 0.0), + vec![("call-sound".into(), "Sound on".into())] + ); + shell + .events + .send(Event::State { + session: sample::DEMO as u64, + state: SessionState { + voice: VoiceState::Connected, + own_channel: Some(2), + own_client: Some(1), + input_muted: true, + output_muted: true, + ..Default::default() + }, + }) + .unwrap(); + frame(cx); + assert!(at(cx, "call-sound").is_some(), "debug_selector stays intact"); + assert_eq!( + hover_tooltip(cx, "call-sound", 1.0), + vec![("call-sound".into(), "Sound off".into())] + ); + + // 3. Member card mute: hover "Mute for me", state flips while pointer stays + // on the button (and clicking again also keeps selector working) -> + // "Unmute for me" built. + click(cx, "voice-member-3"); + assert_eq!( + hover_tooltip(cx, "member-mute", 0.0), + vec![("member-mute".into(), "Mute for me".into())] + ); + cx.dispatch_action(SetMemberMuted { client: 3, muted: true }); + frame(cx); + assert!(at(cx, "member-mute").is_some(), "debug_selector stays intact"); + assert_eq!( + hover_tooltip(cx, "member-mute", 1.0), + vec![("member-mute".into(), "Unmute for me".into())] + ); + click(cx, "member-mute"); + assert_eq!( + hover_tooltip(cx, "member-mute", 0.0), + vec![("member-mute".into(), "Mute for me".into())] + ); + + // 4. Voice stage button: close member card, hover "Smaller stage", window + // shrinks below 800 px -> disabled button builds its new tooltip. + click(cx, "member-card-close"); + assert_eq!( + hover_tooltip(cx, "voice-stage", 0.0), + vec![("voice-stage".into(), "Smaller stage".into())] + ); + cx.simulate_resize(size(px(1440.0), px(700.0))); + frame(cx); + assert!(at(cx, "voice-stage").is_some(), "debug_selector stays intact"); + assert_eq!( + hover_tooltip(cx, "voice-stage", 1.0), + vec![("voice-stage".into(), "The window is too short for the larger stage".into())] + ); +}