refactor: remove cell::RefCell, rename TIMERS_QUEUE variable, and etc. (check desc)

Removed unused import, renamed static variable (TIMERS_QUEUE ->
TASKS_QUEUE) and refactored get_instance() and show_infobar() methods.
Return value has been changed (Self -> &'static Self) in get_instance()
method.
A check for opening an infobar has been added and renamed used static
variables in show_infobar() method.
This commit is contained in:
doryan 2024-08-02 23:58:24 +04:00
parent a2cb65d570
commit c9dbe4ba6e

View File

@ -1,6 +1,6 @@
use gtk4 as gtk; use gtk4 as gtk;
use std::{cell::RefCell, collections::VecDeque, sync::LazyLock}; use std::{collections::VecDeque, sync::LazyLock};
use gtk::{ use gtk::{
builders::{BoxBuilder, ButtonBuilder, LabelBuilder}, builders::{BoxBuilder, ButtonBuilder, LabelBuilder},
@ -29,7 +29,9 @@ pub struct InfoBarBuilder {
button: ButtonBuilder, button: ButtonBuilder,
} }
static mut TIMERS_QUEUE: VecDeque<JoinHandle<()>> = VecDeque::new(); // TODO: Develop a method to safely mutate static.
// Not necessary.
static mut TASKS_QUEUE: VecDeque<JoinHandle<()>> = VecDeque::new();
// //
// Singleton pattern implementation // Singleton pattern implementation
@ -43,8 +45,8 @@ static INFO_BAR_INSTANCE: LazyLock<InfoBar> = LazyLock::new(|| InfoBar {
}); });
impl InfoBar { impl InfoBar {
pub fn get_instance() -> Self { pub fn get_instance() -> &'static Self {
INFO_BAR_INSTANCE.clone() &INFO_BAR_INSTANCE
} }
pub fn set_text_label(&self, text_label: Option<&str>) { pub fn set_text_label(&self, text_label: Option<&str>) {
@ -64,7 +66,9 @@ impl InfoBar {
} }
pub fn show_infobar(&self, duration_secs: u64) { pub fn show_infobar(&self, duration_secs: u64) {
if !INFO_BAR_INSTANCE.instance.reveals_child() {
INFO_BAR_INSTANCE.set_reveal_child(true); INFO_BAR_INSTANCE.set_reveal_child(true);
}
let callback = spawn(async move { let callback = spawn(async move {
sleep(Duration::from_secs(duration_secs)).await; sleep(Duration::from_secs(duration_secs)).await;
@ -72,11 +76,11 @@ impl InfoBar {
}); });
unsafe { unsafe {
if !TIMERS_QUEUE.is_empty() { if !TASKS_QUEUE.is_empty() {
TIMERS_QUEUE.pop_back().unwrap().abort(); TASKS_QUEUE.pop_back().unwrap().abort();
} }
TIMERS_QUEUE.push_back(callback); TASKS_QUEUE.push_back(callback);
} }
} }
} }