Skip to content

Commit 6caebe2

Browse files
authored
Merge pull request #1260 from JakeStanger/fix/ipc-popup2
More IPC popup fixes
2 parents e1b1231 + a067914 commit 6caebe2

File tree

5 files changed

+25
-26
lines changed

5 files changed

+25
-26
lines changed

src/ipc/server/bar.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@ use super::Response;
22
use crate::Ironbar;
33
use crate::bar::Bar;
44
use crate::ipc::{BarCommand, BarCommandType};
5-
use glib::prelude::Cast;
6-
use gtk::Button;
75
use std::rc::Rc;
86

97
pub fn handle_command(command: &BarCommand, ironbar: &Rc<Ironbar>) -> Response {
@@ -60,7 +58,7 @@ pub fn handle_command(command: &BarCommand, ironbar: &Rc<Ironbar>) -> Response {
6058
values.push(v);
6159
Response::Multi { values }
6260
}
63-
_ => unreachable!(),
61+
(acc, _) => acc,
6462
})
6563
.unwrap_or(Response::error("Invalid bar name"))
6664
}
@@ -78,11 +76,13 @@ fn show_popup(bar: &Bar, widget_name: &str) -> Response {
7876

7977
let module_ref = bar.modules().iter().find(|m| m.name == widget_name);
8078

81-
let module_button = module_ref.and_then(|m| m.widget.downcast_ref::<Button>());
79+
let module_button = module_ref
80+
.and_then(|m| m.popup.clone())
81+
.and_then(|popup| popup.buttons.first().cloned());
8282

8383
match (module_ref, module_button) {
8484
(Some(module_ref), Some(button)) => {
85-
if popup.show_for(module_ref.id, button) {
85+
if popup.show_for(module_ref.id, &button) {
8686
Response::Ok
8787
} else {
8888
Response::error("Module has no popup functionality")

src/ipc/server/style.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ pub fn handle_command(command: StyleCommand, ironbar: &Ironbar) -> Response {
5252
}
5353

5454
for module in modules {
55-
if module.widget.has_css_class(&name) {
55+
if module.root_widget.has_css_class(&name) {
5656
module.remove_css_class(&name);
5757
} else {
5858
module.add_css_class(&name);
@@ -73,18 +73,18 @@ fn modules_by_name<'a>(bars: &'a [Bar], name: &str) -> Vec<&'a ModuleRef> {
7373

7474
impl ModuleRef {
7575
fn add_css_class(&self, name: &str) {
76-
self.widget.add_css_class(name);
76+
self.root_widget.add_css_class(name);
7777

7878
if let Some(ref popup) = self.popup {
79-
popup.add_css_class(name);
79+
popup.container.add_css_class(name);
8080
}
8181
}
8282

8383
fn remove_css_class(&self, name: &str) {
84-
self.widget.remove_css_class(name);
84+
self.root_widget.remove_css_class(name);
8585

8686
if let Some(ref popup) = self.popup {
87-
popup.remove_css_class(name);
87+
popup.container.remove_css_class(name);
8888
}
8989
}
9090
}

src/modules/launcher/mod.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ use gtk::prelude::*;
2020
use gtk::{Button, Orientation};
2121
use indexmap::IndexMap;
2222
use serde::Deserialize;
23+
use std::rc::Rc;
2324
use std::sync::Arc;
2425
use tokio::sync::mpsc;
2526
use tracing::{debug, error, trace, warn};
@@ -588,7 +589,7 @@ impl Module<gtk::Box> for LauncherModule {
588589

589590
let popup = self
590591
.into_popup(context, info)
591-
.into_popup_parts_with_finder(Box::new(move |id| {
592+
.into_popup_parts_with_finder(Rc::new(move |id| {
592593
buttons
593594
.borrow()
594595
.values()

src/modules/mod.rs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -144,8 +144,8 @@ where
144144
pub struct ModuleRef {
145145
pub id: usize,
146146
pub name: String,
147-
pub widget: Widget,
148-
pub popup: Option<gtk::Box>,
147+
pub root_widget: Widget,
148+
pub popup: Option<ModulePopupParts>,
149149
}
150150

151151
pub struct ModuleParts<W: IsA<Widget>> {
@@ -182,14 +182,15 @@ impl<W: IsA<Widget>> ModuleParts<W> {
182182
}
183183
}
184184

185+
#[derive(Clone)]
185186
pub struct ModulePopupParts {
186187
/// The popup container, with all its contents
187188
pub container: gtk::Box,
188189
/// An array of buttons which can be used for opening the popup.
189190
/// For most modules, this will only be a single button.
190191
pub buttons: Vec<Button>,
191192

192-
pub button_finder: Option<Box<ButtonFinder>>,
193+
pub button_finder: Option<Rc<ButtonFinder>>,
193194
}
194195

195196
impl Debug for ModulePopupParts {
@@ -206,7 +207,7 @@ pub trait ModulePopup {
206207
fn into_popup_parts(self, buttons: Vec<&Button>) -> Option<ModulePopupParts>;
207208
fn into_popup_parts_owned(self, buttons: Vec<Button>) -> Option<ModulePopupParts>;
208209

209-
fn into_popup_parts_with_finder(self, finder: Box<ButtonFinder>) -> Option<ModulePopupParts>;
210+
fn into_popup_parts_with_finder(self, finder: Rc<ButtonFinder>) -> Option<ModulePopupParts>;
210211
}
211212

212213
impl ModulePopup for Option<gtk::Box> {
@@ -222,7 +223,7 @@ impl ModulePopup for Option<gtk::Box> {
222223
})
223224
}
224225

225-
fn into_popup_parts_with_finder(self, finder: Box<ButtonFinder>) -> Option<ModulePopupParts> {
226+
fn into_popup_parts_with_finder(self, finder: Rc<ButtonFinder>) -> Option<ModulePopupParts> {
226227
self.map(|container| ModulePopupParts {
227228
container,
228229
buttons: vec![],
@@ -354,8 +355,7 @@ pub trait ModuleFactory {
354355

355356
module_parts.setup_identifiers(&common);
356357

357-
let popup_container = module_parts.popup.as_ref().map(|p| p.container.clone());
358-
if let Some(popup_content) = module_parts.popup {
358+
if let Some(popup_content) = module_parts.popup.clone() {
359359
popup_content
360360
.container
361361
.add_css_class(&format!("popup-{module_name}"));
@@ -375,8 +375,8 @@ pub trait ModuleFactory {
375375
Ok(ModuleRef {
376376
id,
377377
name: instance_name,
378-
widget: module_parts.widget.upcast(),
379-
popup: popup_container,
378+
root_widget: module_parts.widget.upcast(),
379+
popup: module_parts.popup,
380380
})
381381
}
382382

src/popup.rs

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ pub type ButtonFinder = dyn Fn(usize) -> Option<Button> + 'static;
2626
pub struct Popup {
2727
pub popover: Popover,
2828
pub container_cache: Rc<RefCell<HashMap<usize, PopupCacheValue>>>,
29-
pub button_finder_cache: Rc<RefCell<HashMap<usize, Box<ButtonFinder>>>>,
29+
pub button_finder_cache: Rc<RefCell<HashMap<usize, Rc<ButtonFinder>>>>,
3030
pub button_cache: Rc<RefCell<Vec<Button>>>,
3131
pos: BarPosition,
3232
current_widget: Rc<RefCell<Option<CurrentWidgetInfo>>>,
@@ -108,7 +108,7 @@ impl Popup {
108108
if let Some(button_finder) = content.button_finder {
109109
self.button_finder_cache
110110
.borrow_mut()
111-
.insert(key, Box::new(button_finder));
111+
.insert(key, button_finder);
112112
}
113113
}
114114

@@ -117,10 +117,8 @@ impl Popup {
117117
self.button_cache.borrow_mut().push(button);
118118
}
119119

120-
pub fn register_button_finder(&self, key: usize, finder: Box<ButtonFinder>) {
121-
self.button_finder_cache
122-
.borrow_mut()
123-
.insert(key, Box::new(finder));
120+
pub fn register_button_finder(&self, key: usize, finder: Rc<ButtonFinder>) {
121+
self.button_finder_cache.borrow_mut().insert(key, finder);
124122
}
125123

126124
pub fn unregister_button(&self, button: &Button) {

0 commit comments

Comments
 (0)