Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix alert timer by adding controller #503

Merged
merged 2 commits into from
Jul 25, 2024
Merged
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
30 changes: 30 additions & 0 deletions psst-gui/src/controller/alert_cleanup.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
use crate::data::AppState;
use druid::{widget::Controller, Env, Event, EventCtx, Widget};
use std::time::Duration;

pub struct AlertCleanupController;

const CLEANUP_INTERVAL: Duration = Duration::from_secs(1);

impl<W: Widget<AppState>> Controller<AppState, W> for AlertCleanupController {
fn event(
&mut self,
child: &mut W,
ctx: &mut EventCtx,
event: &Event,
data: &mut AppState,
env: &Env,
) {
match event {
Event::WindowConnected => {
ctx.request_timer(CLEANUP_INTERVAL);
}
Event::Timer(_) => {
data.cleanup_alerts();
ctx.request_timer(CLEANUP_INTERVAL);
}
_ => {}
}
child.event(ctx, event, data, env)
}
}
2 changes: 2 additions & 0 deletions psst-gui/src/controller/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
mod after_delay;
mod alert_cleanup;
mod ex_click;
mod ex_cursor;
mod ex_scroll;
Expand All @@ -13,6 +14,7 @@ mod session;
mod sort;

pub use after_delay::AfterDelay;
pub use alert_cleanup::AlertCleanupController;
pub use ex_click::ExClick;
pub use ex_cursor::ExCursor;
pub use ex_scroll::ExScroll;
Expand Down
31 changes: 21 additions & 10 deletions psst-gui/src/data/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ use std::{
atomic::{AtomicUsize, Ordering},
Arc,
},
time::Duration,
time::{Duration, Instant},
};

use druid::{
Expand Down Expand Up @@ -60,6 +60,8 @@ pub use crate::data::{
utils::{Cached, Float64, Image, Page},
};

pub const ALERT_DURATION: Duration = Duration::from_secs(5);

#[derive(Clone, Data, Lens)]
pub struct AppState {
#[data(ignore)]
Expand Down Expand Up @@ -276,25 +278,33 @@ impl AppState {
}

impl AppState {
pub fn info_alert(&mut self, message: impl Display) {
self.alerts.push_back(Alert {
pub fn add_alert(&mut self, message: impl Display, style: AlertStyle) {
let alert = Alert {
message: message.to_string().into(),
style: AlertStyle::Info,
style,
id: Alert::fresh_id(),
});
created_at: Instant::now(),
};
self.alerts.push_back(alert);
}

pub fn info_alert(&mut self, message: impl Display) {
self.add_alert(message, AlertStyle::Info);
}

pub fn error_alert(&mut self, message: impl Display) {
self.alerts.push_back(Alert {
message: message.to_string().into(),
style: AlertStyle::Error,
id: Alert::fresh_id(),
});
self.add_alert(message, AlertStyle::Error);
}

pub fn dismiss_alert(&mut self, id: usize) {
self.alerts.retain(|a| a.id != id);
}

pub fn cleanup_alerts(&mut self) {
let now = Instant::now();
self.alerts
.retain(|alert| now.duration_since(alert.created_at) < ALERT_DURATION);
}
}

#[derive(Clone, Data, Lens)]
Expand Down Expand Up @@ -512,6 +522,7 @@ pub struct Alert {
pub id: usize,
pub message: Arc<str>,
pub style: AlertStyle,
pub created_at: Instant,
}

impl Alert {
Expand Down
7 changes: 5 additions & 2 deletions psst-gui/src/ui/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,12 @@ use druid_shell::Cursor;
use crate::data::config::SortCriteria;
use crate::{
cmd,
controller::{AfterDelay, NavController, SessionController, SortController},
controller::{
AfterDelay, AlertCleanupController, NavController, SessionController, SortController,
},
data::{
config::SortOrder, Alert, AlertStyle, AppState, Config, Nav, Playable, Playback, Route,
ALERT_DURATION,
},
widget::{
icons, icons::SvgIcon, Border, Empty, MyWidgetExt, Overlay, ThemeScope, ViewDispatcher,
Expand Down Expand Up @@ -170,7 +173,6 @@ fn root_widget() -> impl Widget<AppState> {
fn alert_widget() -> impl Widget<AppState> {
const BG: Key<Color> = Key::new("app.alert.BG");
const DISMISS_ALERT: Selector<usize> = Selector::new("app.alert.dismiss");
const ALERT_DURATION: Duration = Duration::from_secs(5);

List::new(|| {
Flex::row()
Expand Down Expand Up @@ -205,6 +207,7 @@ fn alert_widget() -> impl Widget<AppState> {
.on_command(DISMISS_ALERT, |_, &id, state| {
state.dismiss_alert(id);
})
.controller(AlertCleanupController)
}

fn route_widget() -> impl Widget<AppState> {
Expand Down
9 changes: 6 additions & 3 deletions psst-gui/src/ui/playback.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,9 +108,12 @@ fn playing_item_widget() -> impl Widget<NowPlaying> {
ctx.submit_command(cmd::NAVIGATE.with(now_playing.origin.to_nav()));
})
.context_menu(|now_playing| match &now_playing.item {
Playable::Track(track) => {
track::track_menu(track, &now_playing.library, &now_playing.origin, usize::MAX)
}
Playable::Track(track) => track::track_menu(
track,
&now_playing.library,
&now_playing.origin,
usize::MAX,
),
Playable::Episode(episode) => {
episode::episode_menu(episode, &now_playing.library)
}
Expand Down