Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 54 additions & 9 deletions crates/faderframe-ui/src/canvas.rs
Original file line number Diff line number Diff line change
Expand Up @@ -999,7 +999,7 @@ impl CanvasWidget {
entry.set_text(&initial);
entry.set_width_chars(16);
popover.set_child(Some(&entry));
popover.set_parent(self);
attach_popover(&popover, self);
popover.set_pointing_to(Some(&gdk::Rectangle::new(
at.x as i32,
at.y as i32,
Expand All @@ -1019,10 +1019,6 @@ impl CanvasWidget {
}
}
));
popover.connect_closed(|p| {
let p = p.clone();
glib::idle_add_local_once(move || p.unparent());
});
popover.popup();
entry.grab_focus();
entry.select_region(0, -1);
Expand Down Expand Up @@ -1674,6 +1670,32 @@ impl CanvasWidget {
/// them and moving between levels never resizes or moves it (a popover
/// that grows by the edge of the screen may otherwise be closed by the
/// compositor). A long menu gets a search field over its levels.
/// Parent a transient popover to `parent` for as long as it is shown:
/// unparented once it closes (after GTK has finished closing it) — or,
/// when `parent` goes first (a list row rebuilt while the popover is
/// open), right then, so the popover never points at a freed widget
/// (unparenting one that did crashed GTK).
pub(crate) fn attach_popover(pop: &impl IsA<gtk::Popover>, parent: &impl IsA<gtk::Widget>) {
let pop: gtk::Popover = pop.clone().upcast();
pop.set_parent(parent);
let weak = pop.downgrade();
parent.connect_destroy(move |w| {
if let Some(p) = weak.upgrade()
&& p.parent().as_ref() == Some(w.upcast_ref::<gtk::Widget>())
{
p.unparent();
}
});
pop.connect_closed(|p| {
let p = p.clone();
glib::idle_add_local_once(move || {
if p.parent().is_some() {
p.unparent();
}
});
});
}

pub fn show_menu(
parent: &gtk::Widget,
at: Point,
Expand Down Expand Up @@ -1706,12 +1728,10 @@ pub fn show_menu(
}
None => popover.set_child(Some(&scroller)),
}
popover.set_parent(parent);
attach_popover(&popover, parent);
popover.set_pointing_to(Some(&gdk::Rectangle::new(at.x as i32, at.y as i32, 1, 1)));
popover.connect_closed(|p| {
popover.connect_closed(|_| {
OPEN_MENU.with(|m| m.borrow_mut().take());
let p = p.clone();
glib::idle_add_local_once(move || p.unparent());
});
OPEN_MENU.with(|m| {
*m.borrow_mut() = Some(OpenMenu {
Expand Down Expand Up @@ -2354,4 +2374,29 @@ mod tests {
assert_eq!(super::named_files(&text), vec![a, b]);
let _ = std::fs::remove_dir_all(&dir);
}

/// A popover whose parent goes while it is open (a sidebar row rebuilt)
/// is unparented then, and its closing later touches nothing freed: it
/// had crashed GTK in `gtk_widget_unparent` from the idle callback.
#[test]
#[ignore = "requires a GTK display"]
fn a_popover_outlives_its_parent_row_safely() {
use gtk::prelude::*;
gtk::init().expect("GTK display");
let list = gtk::Box::new(gtk::Orientation::Vertical, 0);
let row = gtk::Label::new(Some("Category"));
list.append(&row);
let pop = gtk::Popover::new();
super::attach_popover(&pop, &row);
assert_eq!(pop.parent().as_ref(), Some(row.upcast_ref::<gtk::Widget>()));
// The row goes (only the list held it) while the popover is open.
list.remove(&row);
drop(row);
assert!(pop.parent().is_none(), "unparented with its row");
// Closing it later, and the idle after, are harmless.
pop.emit_by_name::<()>("closed", &[]);
let ctx = gtk::glib::MainContext::default();
while ctx.iteration(false) {}
assert!(pop.parent().is_none());
}
}
6 changes: 1 addition & 5 deletions crates/faderframe-ui/src/learn.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ pub fn on_right_click_with(
let list = gtk::Box::new(gtk::Orientation::Vertical, 0);
let popover = gtk::Popover::new();
popover.set_child(Some(&list));
popover.set_parent(&anchor);
crate::canvas::attach_popover(&popover, &anchor);
popover.set_has_arrow(true);
for (k, (label, action)) in entries.into_iter().enumerate() {
if k == gap && gap > 0 {
Expand All @@ -63,10 +63,6 @@ pub fn on_right_click_with(
});
list.append(&b);
}
popover.connect_closed(|p| {
let p = p.clone();
gtk::glib::idle_add_local_once(move || p.unparent());
});
popover.popup();
});
widget.add_controller(click);
Expand Down
6 changes: 1 addition & 5 deletions crates/faderframe-ui/src/plugin_browser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -561,7 +561,7 @@ fn ask_name(
done: impl Fn(String) + 'static,
) -> gtk::Popover {
let pop = gtk::Popover::new();
pop.set_parent(parent);
crate::canvas::attach_popover(&pop, parent);
let column = gtk::Box::new(gtk::Orientation::Vertical, 6);
column.set_margin_top(4);
column.set_margin_bottom(4);
Expand Down Expand Up @@ -595,10 +595,6 @@ fn ask_name(
let again = go.clone();
entry.connect_activate(move |_| again());
ok.connect_clicked(move |_| go());
pop.connect_closed(|p| {
let p = p.clone();
glib::idle_add_local_once(move || p.unparent());
});
pop.popup();
entry.grab_focus();
pop
Expand Down
3 changes: 1 addition & 2 deletions crates/faderframe-ui/src/plugin_window/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1878,9 +1878,8 @@ fn open_generic(app: &Rc<AppState>, plugin: PluginInstanceId) {
list.append(&b);
}
popover.set_child(Some(&list));
popover.set_parent(&widget);
crate::canvas::attach_popover(&popover, &widget);
popover.set_pointing_to(Some(&gtk::gdk::Rectangle::new(x as i32, y as i32, 1, 1)));
popover.connect_closed(|p| p.unparent());
popover.popup();
});
row.row.add_controller(menu);
Expand Down
4 changes: 1 addition & 3 deletions crates/faderframe-ui/src/toggles.rs
Original file line number Diff line number Diff line change
Expand Up @@ -600,7 +600,7 @@ pub(crate) fn show_menu(app: &Rc<AppState>, name: &str) {
old.popdown();
}
let popover = gtk::PopoverMenu::from_model(Some(&sub));
popover.set_parent(&window);
crate::canvas::attach_popover(&popover, &window);
popover.set_has_arrow(false);
popover.set_position(gtk::PositionType::Bottom);
popover.set_pointing_to(Some(&gtk::gdk::Rectangle::new(120, 4, 1, 1)));
Expand All @@ -612,8 +612,6 @@ pub(crate) fn show_menu(app: &Rc<AppState>, name: &str) {
m.take();
}
});
let p = p.clone();
glib::idle_add_local_once(move || p.unparent());
});
SHOWN.with(|m| *m.borrow_mut() = Some(popover));
}
Expand Down