refactor: remove mutex

This commit is contained in:
Ashley Wulber 2021-12-01 09:21:04 -05:00
parent fba59c5290
commit ee6b7f1893
2 changed files with 57 additions and 61 deletions

View file

@ -8,8 +8,7 @@ use gtk::Application;
use gtk4 as gtk; use gtk4 as gtk;
use gtk4::CssProvider; use gtk4::CssProvider;
use gtk4::StyleContext; use gtk4::StyleContext;
use once_cell::sync::Lazy; use once_cell::sync::OnceCell;
use std::sync::Mutex;
use gtk::gio; use gtk::gio;
use gtk::glib; use gtk::glib;
@ -22,7 +21,7 @@ use self::application_object::ApplicationObject;
use self::window::Window; use self::window::Window;
const NUM_LAUNCHER_ITEMS: u8 = 10; const NUM_LAUNCHER_ITEMS: u8 = 10;
static TX: Lazy<Mutex<Option<Sender<Event>>>> = Lazy::new(|| Mutex::new(None)); static TX: OnceCell<Sender<Event>> = OnceCell::new();
fn icon_source(icon: &gtk::Image, source: &Option<pop_launcher::IconSource>) { fn icon_source(icon: &gtk::Image, source: &Option<pop_launcher::IconSource>) {
match source { match source {
@ -93,10 +92,10 @@ fn main() {
app.connect_activate(move |app| { app.connect_activate(move |app| {
let (tx, mut rx) = postage::mpsc::channel(1); let (tx, mut rx) = postage::mpsc::channel(1);
let mut launcher = spawn_launcher(tx.clone()); let mut launcher = spawn_launcher(tx.clone());
{ if TX.set(tx).is_err() {
let mut global_tx = TX.lock().unwrap(); println!("failed to set global Sender. Exiting");
*global_tx = Some(tx.clone()); std::process::exit(1);
} };
let window = Window::new(app); let window = Window::new(app);
let wclone = window.clone(); let wclone = window.clone();

View file

@ -69,10 +69,10 @@ impl Window {
let id = id.get::<u32>().expect("App ID must be u32"); let id = id.get::<u32>().expect("App ID must be u32");
glib::MainContext::default().spawn_local(async move { glib::MainContext::default().spawn_local(async move {
if let Ok(tx) = TX.lock() { if let Some(tx) = TX.get() {
if let Some(tx) = &*tx { let mut tx = tx.clone();
let _ = tx.clone().send(crate::Event::Activate(id)).await; let _ = tx.send(crate::Event::Activate(id)).await;
}} }
}); });
} }
})); }));
@ -89,10 +89,9 @@ impl Window {
let id = id.get::<u32>().expect("App ID must be u32"); let id = id.get::<u32>().expect("App ID must be u32");
glib::MainContext::default().spawn_local(async move { glib::MainContext::default().spawn_local(async move {
if let Ok(tx) = TX.lock() { if let Some(tx) = TX.get() {
if let Some(tx) = &*tx { let mut tx = tx.clone();
let _ = tx.clone().send(crate::Event::Activate(id)).await; let _ = tx.send(crate::Event::Activate(id)).await;
}
} }
}); });
} }
@ -102,10 +101,9 @@ impl Window {
let search = search.text().to_string(); let search = search.text().to_string();
glib::MainContext::default().spawn_local(async move { glib::MainContext::default().spawn_local(async move {
if let Ok(tx) = TX.lock() { if let Some(tx) = TX.get() {
if let Some(tx) = &*tx { let mut tx = tx.clone();
let _ = tx.clone().send(crate::Event::Search(search)).await; let _ = tx.send(crate::Event::Search(search)).await;
}
} }
}); });
}); });
@ -114,10 +112,9 @@ impl Window {
let search = search.text().to_string(); let search = search.text().to_string();
glib::MainContext::default().spawn_local(async move { glib::MainContext::default().spawn_local(async move {
if let Ok(tx) = TX.lock() { if let Some(tx) = TX.get() {
if let Some(tx) = &*tx { let mut tx = tx.clone();
let _ = tx.clone().send(crate::Event::Search(search)).await; let _ = tx.send(crate::Event::Search(search)).await;
}
} }
}); });
}); });