App: Fix refference cycles in actions.

This commit is contained in:
Jordan Petridis 2018-07-27 19:18:23 +03:00
parent e4e35e4c57
commit 3496df24f8
No known key found for this signature in database
GPG Key ID: CEABAD9F5683B9A6

View File

@ -187,15 +187,12 @@ impl App {
#[cfg_attr(rustfmt, rustfmt_skip)] #[cfg_attr(rustfmt, rustfmt_skip)]
fn setup_gactions(&self) { fn setup_gactions(&self) {
let sender = &self.sender; let sender = &self.sender;
let win = &self.window; let weak_win = self.window.downgrade();
let instance = &self.instance;
let header = &self.headerbar;
let settings = &self.settings;
// Create the `refresh` action. // Create the `refresh` action.
// //
// This will trigger a refresh of all the shows in the database. // This will trigger a refresh of all the shows in the database.
action(win, "refresh", clone!(sender => move |_, _| { action(&self.window, "refresh", clone!(sender => move |_, _| {
gtk::idle_add(clone!(sender => move || { gtk::idle_add(clone!(sender => move || {
let s: Option<Vec<_>> = None; let s: Option<Vec<_>> = None;
utils::refresh(s, sender.clone()); utils::refresh(s, sender.clone());
@ -205,29 +202,39 @@ impl App {
self.instance.set_accels_for_action("win.refresh", &["<primary>r"]); self.instance.set_accels_for_action("win.refresh", &["<primary>r"]);
// Create the `OPML` import action // Create the `OPML` import action
action(win, "import", clone!(sender, win => move |_, _| { action(&self.window, "import", clone!(sender, weak_win => move |_, _| {
utils::on_import_clicked(&win, &sender) weak_win.upgrade().map(|win| utils::on_import_clicked(&win, &sender));
})); }));
// Create the action that shows a `gtk::AboutDialog` // Create the action that shows a `gtk::AboutDialog`
action(win, "about", clone!(win => move |_, _| about_dialog(&win))); action(&self.window, "about", clone!(weak_win => move |_, _| {
weak_win.upgrade().map(|win| about_dialog(&win));
}));
// Create the quit action // Create the quit action
action(win, "quit", clone!(instance => move |_, _| instance.quit())); let weak_instance = self.instance.downgrade();
action(&self.window, "quit", move |_, _| {
weak_instance.upgrade().map(|app| app.quit());
});
self.instance.set_accels_for_action("win.quit", &["<primary>q"]); self.instance.set_accels_for_action("win.quit", &["<primary>q"]);
action( let weak_settings = self.settings.downgrade();
win, action(&self.window, "preferences", clone!(weak_win => move |_, _| {
"preferences", let (win, settings) = match (weak_win.upgrade(), weak_settings.upgrade()) {
clone!(win, settings => move |_, _| { (Some(win), Some(settings)) => (win, settings),
_ => return,
};
let dialog = Prefs::new(&settings); let dialog = Prefs::new(&settings);
dialog.show(&win); dialog.show(&win);
}) }));
);
self.instance.set_accels_for_action("win.preferences", &["<primary>e"]); self.instance.set_accels_for_action("win.preferences", &["<primary>e"]);
// Create the menu action // Create the menu action
action(win, "menu",clone!(header => move |_, _| header.open_menu())); let header = Rc::downgrade(&self.headerbar);
action(&self.window, "menu", move |_, _| {
header.upgrade().map(|h| h.open_menu());
});
// Bind the hamburger menu button to `F10` // Bind the hamburger menu button to `F10`
self.instance.set_accels_for_action("win.menu", &["F10"]); self.instance.set_accels_for_action("win.menu", &["F10"]);
} }