From f24b21d24226da12361c63e080382fbedfb4d114 Mon Sep 17 00:00:00 2001 From: Jake Stanger Date: Thu, 19 Oct 2023 20:21:27 +0100 Subject: [PATCH] fix(focused): clear when no window is focused Fixes #337 --- src/modules/focused.rs | 61 +++++++++++++++++++++++++++--------------- 1 file changed, 40 insertions(+), 21 deletions(-) diff --git a/src/modules/focused.rs b/src/modules/focused.rs index 93b58c68..167e5b27 100644 --- a/src/modules/focused.rs +++ b/src/modules/focused.rs @@ -49,7 +49,7 @@ const fn default_icon_size() -> i32 { } impl Module for FocusedModule { - type SendMessage = (String, String); + type SendMessage = Option<(String, String)>; type ReceiveMessage = (); fn name() -> &'static str { @@ -78,21 +78,34 @@ impl Module for FocusedModule { if let Some(focused) = focused { try_send!( tx, - ModuleUpdateEvent::Update((focused.title.clone(), focused.app_id)) + ModuleUpdateEvent::Update(Some((focused.title.clone(), focused.app_id))) ); }; while let Ok(event) = wlrx.recv().await { - if let ToplevelEvent::Update(handle) = event { - let info = handle.info().unwrap_or_default(); - - if info.focused { - debug!("Changing focus"); - send_async!( - tx, - ModuleUpdateEvent::Update((info.title.clone(), info.app_id.clone())) - ); + match event { + ToplevelEvent::Update(handle) => { + let info = handle.info().unwrap_or_default(); + + if info.focused { + debug!("Changing focus"); + send_async!( + tx, + ModuleUpdateEvent::Update(Some(( + info.title.clone(), + info.app_id.clone() + ))) + ); + } } + ToplevelEvent::Remove(handle) => { + let info = handle.info().unwrap_or_default(); + if info.focused { + debug!("Clearing focus"); + send_async!(tx, ModuleUpdateEvent::Update(None)); + } + } + ToplevelEvent::New(_) => {} } } }); @@ -126,18 +139,24 @@ impl Module for FocusedModule { { let icon_theme = icon_theme.clone(); - context.widget_rx.attach(None, move |(name, id)| { - if self.show_icon { - match ImageProvider::parse(&id, &icon_theme, true, self.icon_size) - .map(|image| image.load_into_image(icon.clone())) - { - Some(Ok(_)) => icon.show(), - _ => icon.hide(), + context.widget_rx.attach(None, move |data| { + if let Some((name, id)) = data { + if self.show_icon { + match ImageProvider::parse(&id, &icon_theme, true, self.icon_size) + .map(|image| image.load_into_image(icon.clone())) + { + Some(Ok(())) => icon.show(), + _ => icon.hide(), + } } - } - if self.show_title { - label.set_label(&name); + if self.show_title { + label.show(); + label.set_label(&name); + } + } else { + icon.hide(); + label.hide(); } Continue(true)