InAppNotif: Refactor to infer the undo state
If we use an Optional instead of passing empty closures, we can infer if the Undo button needs to be shown.
This commit is contained in:
parent
866fa6a758
commit
01efbf5c79
@ -100,7 +100,6 @@ Tobias Bernard
|
|||||||
<child>
|
<child>
|
||||||
<object class="GtkButton" id="undo">
|
<object class="GtkButton" id="undo">
|
||||||
<property name="label" translatable="yes">Undo</property>
|
<property name="label" translatable="yes">Undo</property>
|
||||||
<property name="visible">True</property>
|
|
||||||
<property name="can_focus">True</property>
|
<property name="can_focus">True</property>
|
||||||
<property name="receives_default">True</property>
|
<property name="receives_default">True</property>
|
||||||
<property name="halign">center</property>
|
<property name="halign">center</property>
|
||||||
|
|||||||
@ -17,7 +17,7 @@ use settings::{self, WindowGeometry};
|
|||||||
use stacks::{Content, PopulatedState};
|
use stacks::{Content, PopulatedState};
|
||||||
use utils;
|
use utils;
|
||||||
use widgets::about_dialog;
|
use widgets::about_dialog;
|
||||||
use widgets::appnotif::{InAppNotification, UndoState};
|
use widgets::appnotif::InAppNotification;
|
||||||
use widgets::player;
|
use widgets::player;
|
||||||
use widgets::show_menu::{mark_all_notif, remove_show_notif, ShowMenu};
|
use widgets::show_menu::{mark_all_notif, remove_show_notif, ShowMenu};
|
||||||
|
|
||||||
@ -306,7 +306,8 @@ impl App {
|
|||||||
Action::ErrorNotification(err) => {
|
Action::ErrorNotification(err) => {
|
||||||
error!("An error notification was triggered: {}", err);
|
error!("An error notification was triggered: {}", err);
|
||||||
let callback = || glib::Continue(false);
|
let callback = || glib::Continue(false);
|
||||||
let notif = InAppNotification::new(&err, callback, || {}, UndoState::Hidden);
|
let undo_cb: Option<fn()> = None;
|
||||||
|
let notif = InAppNotification::new(&err, callback, undo_cb);
|
||||||
notif.show(&self.overlay);
|
notif.show(&self.overlay);
|
||||||
}
|
}
|
||||||
Action::InitEpisode(rowid) => {
|
Action::InitEpisode(rowid) => {
|
||||||
|
|||||||
@ -6,6 +6,7 @@ use std::cell::RefCell;
|
|||||||
use std::rc::Rc;
|
use std::rc::Rc;
|
||||||
|
|
||||||
#[derive(Debug, Clone, Copy)]
|
#[derive(Debug, Clone, Copy)]
|
||||||
|
#[allow(dead_code)]
|
||||||
pub(crate) enum UndoState {
|
pub(crate) enum UndoState {
|
||||||
Shown,
|
Shown,
|
||||||
Hidden,
|
Hidden,
|
||||||
@ -38,12 +39,7 @@ impl Default for InAppNotification {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl InAppNotification {
|
impl InAppNotification {
|
||||||
pub(crate) fn new<F, U>(
|
pub(crate) fn new<F, U>(text: &str, mut callback: F, undo_callback: Option<U>) -> Self
|
||||||
text: &str,
|
|
||||||
mut callback: F,
|
|
||||||
undo_callback: U,
|
|
||||||
show_undo: UndoState,
|
|
||||||
) -> Self
|
|
||||||
where
|
where
|
||||||
F: FnMut() -> glib::Continue + 'static,
|
F: FnMut() -> glib::Continue + 'static,
|
||||||
U: Fn() + 'static,
|
U: Fn() + 'static,
|
||||||
@ -58,6 +54,10 @@ impl InAppNotification {
|
|||||||
});
|
});
|
||||||
let id = Rc::new(RefCell::new(Some(id)));
|
let id = Rc::new(RefCell::new(Some(id)));
|
||||||
|
|
||||||
|
if undo_callback.is_some() {
|
||||||
|
notif.set_undo_state(UndoState::Shown)
|
||||||
|
};
|
||||||
|
|
||||||
// Cancel the callback
|
// Cancel the callback
|
||||||
let revealer = notif.revealer.clone();
|
let revealer = notif.revealer.clone();
|
||||||
notif.undo.connect_clicked(move |_| {
|
notif.undo.connect_clicked(move |_| {
|
||||||
@ -66,7 +66,9 @@ impl InAppNotification {
|
|||||||
glib::source::source_remove(id);
|
glib::source::source_remove(id);
|
||||||
}
|
}
|
||||||
|
|
||||||
undo_callback();
|
if let Some(ref f) = undo_callback {
|
||||||
|
f();
|
||||||
|
}
|
||||||
|
|
||||||
// Hide the notification
|
// Hide the notification
|
||||||
revealer.set_reveal_child(false);
|
revealer.set_reveal_child(false);
|
||||||
@ -78,11 +80,6 @@ impl InAppNotification {
|
|||||||
revealer.set_reveal_child(false);
|
revealer.set_reveal_child(false);
|
||||||
});
|
});
|
||||||
|
|
||||||
match show_undo {
|
|
||||||
UndoState::Shown => (),
|
|
||||||
UndoState::Hidden => notif.undo.hide(),
|
|
||||||
}
|
|
||||||
|
|
||||||
notif
|
notif
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -96,4 +93,11 @@ impl InAppNotification {
|
|||||||
// so there will be a nice animation.
|
// so there will be a nice animation.
|
||||||
self.revealer.set_reveal_child(true);
|
self.revealer.set_reveal_child(true);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub(crate) fn set_undo_state(&self, state: UndoState) {
|
||||||
|
match state {
|
||||||
|
UndoState::Shown => self.undo.show(),
|
||||||
|
UndoState::Hidden => self.undo.hide(),
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -13,7 +13,7 @@ use podcasts_data::Show;
|
|||||||
|
|
||||||
use app::Action;
|
use app::Action;
|
||||||
use utils;
|
use utils;
|
||||||
use widgets::appnotif::{InAppNotification, UndoState};
|
use widgets::appnotif::InAppNotification;
|
||||||
|
|
||||||
use std::sync::Arc;
|
use std::sync::Arc;
|
||||||
|
|
||||||
@ -145,7 +145,7 @@ pub(crate) fn mark_all_notif(pd: Arc<Show>, sender: &Sender<Action>) -> InAppNot
|
|||||||
|
|
||||||
let undo_callback = clone!(sender => move || sender.send(Action::RefreshWidgetIfSame(id)));
|
let undo_callback = clone!(sender => move || sender.send(Action::RefreshWidgetIfSame(id)));
|
||||||
let text = i18n("Marked all episodes as listened");
|
let text = i18n("Marked all episodes as listened");
|
||||||
InAppNotification::new(&text, callback, undo_callback, UndoState::Shown)
|
InAppNotification::new(&text, callback, Some(undo_callback))
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) fn remove_show_notif(pd: Arc<Show>, sender: Sender<Action>) -> InAppNotification {
|
pub(crate) fn remove_show_notif(pd: Arc<Show>, sender: Sender<Action>) -> InAppNotification {
|
||||||
@ -177,5 +177,5 @@ pub(crate) fn remove_show_notif(pd: Arc<Show>, sender: Sender<Action>) -> InAppN
|
|||||||
sender.send(Action::RefreshEpisodesView);
|
sender.send(Action::RefreshEpisodesView);
|
||||||
};
|
};
|
||||||
|
|
||||||
InAppNotification::new(&text, callback, undo_callback, UndoState::Shown)
|
InAppNotification::new(&text, callback, Some(undo_callback))
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user