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,51 +92,51 @@ 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();
window.show(); window.show();
glib::MainContext::default().spawn_local(async move { glib::MainContext::default().spawn_local(async move {
while let Some(event) = rx.recv().await { while let Some(event) = rx.recv().await {
match event { match event {
Event::Search(search) => { Event::Search(search) => {
let _ = launcher.send(pop_launcher::Request::Search(search)).await; let _ = launcher.send(pop_launcher::Request::Search(search)).await;
} }
Event::Activate(index) => { Event::Activate(index) => {
let _ = launcher.send(pop_launcher::Request::Activate(index)).await; let _ = launcher.send(pop_launcher::Request::Activate(index)).await;
} }
Event::Response(event) => { Event::Response(event) => {
if let pop_launcher::Response::Update(results) = event { if let pop_launcher::Response::Update(results) = event {
let model = window.model(); let model = window.model();
let model_len = model.n_items(); let model_len = model.n_items();
dbg!(&results); dbg!(&results);
let new_results: Vec<glib::Object> = results let new_results: Vec<glib::Object> = results
[0..std::cmp::min(results.len(), NUM_LAUNCHER_ITEMS.into())] [0..std::cmp::min(results.len(), NUM_LAUNCHER_ITEMS.into())]
.iter() .iter()
.map(|result| ApplicationObject::new(result).upcast()) .map(|result| ApplicationObject::new(result).upcast())
.collect(); .collect();
model.splice(0, model_len, &new_results[..]); model.splice(0, model_len, &new_results[..]);
} else if let pop_launcher::Response::DesktopEntry { } else if let pop_launcher::Response::DesktopEntry {
path, path,
gpu_preference: _gpu_preference, // TODO use GPU preference when launching app gpu_preference: _gpu_preference, // TODO use GPU preference when launching app
} = event } = event
{ {
let app_info = let app_info =
DesktopAppInfo::new(&path.file_name().expect("desktop entry path needs to be a valid filename").to_string_lossy()) DesktopAppInfo::new(&path.file_name().expect("desktop entry path needs to be a valid filename").to_string_lossy())
.expect("failed to create a Desktop App info for launching the application."); .expect("failed to create a Desktop App info for launching the application.");
app_info app_info
.launch(&[], Some(&wclone.display().app_launch_context().clone())).expect("failed to launch the application."); .launch(&[], Some(&wclone.display().app_launch_context().clone())).expect("failed to launch the application.");
} }
} }
} }
} }
}) })
}); });
app.run(); app.run();

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;
}
} }
}); });
}); });