feat(main): add any asynchronous timer that closes infobar automaticaly after 5 second

This commit is contained in:
doryan 2024-07-29 01:15:22 +04:00
parent 0aab9e9ebe
commit 0d676820c6

View File

@ -1,6 +1,6 @@
use gtk4 as gtk; use gtk4 as gtk;
use std::sync::LazyLock; use std::{cell::RefCell, collections::VecDeque, sync::LazyLock};
use gtk::{ use gtk::{
builders::{BoxBuilder, ButtonBuilder, LabelBuilder}, builders::{BoxBuilder, ButtonBuilder, LabelBuilder},
@ -13,6 +13,11 @@ use crate::{
model::{builder_traits::Product, models::EventHandler}, model::{builder_traits::Product, models::EventHandler},
}; };
use tokio::{
task::{spawn, JoinHandle},
time::{sleep, Duration},
};
#[derive(Clone)] #[derive(Clone)]
pub struct InfoBar { pub struct InfoBar {
instance: Revealer, instance: Revealer,
@ -24,6 +29,8 @@ pub struct InfoBarBuilder {
button: ButtonBuilder, button: ButtonBuilder,
} }
static mut TIMERS_QUEUE: VecDeque<JoinHandle<()>> = VecDeque::new();
// //
// Singleton pattern implementation // Singleton pattern implementation
// //
@ -55,6 +62,23 @@ impl InfoBar {
pub fn set_reveal_child(&self, is_reveal: bool) { pub fn set_reveal_child(&self, is_reveal: bool) {
let _ = &INFO_BAR_INSTANCE.instance.set_reveal_child(is_reveal); let _ = &INFO_BAR_INSTANCE.instance.set_reveal_child(is_reveal);
} }
pub fn show_infobar(&self, duration_secs: u64) {
INFO_BAR_INSTANCE.set_reveal_child(true);
let callback = spawn(async move {
sleep(Duration::from_secs(duration_secs)).await;
INFO_BAR_INSTANCE.set_reveal_child(false);
});
unsafe {
if !TIMERS_QUEUE.is_empty() {
TIMERS_QUEUE.pop_back().unwrap().abort();
}
TIMERS_QUEUE.push_back(callback);
}
}
} }
// //