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::CssProvider;
use gtk4::StyleContext;
use once_cell::sync::Lazy;
use std::sync::Mutex;
use once_cell::sync::OnceCell;
use gtk::gio;
use gtk::glib;
@ -22,7 +21,7 @@ use self::application_object::ApplicationObject;
use self::window::Window;
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>) {
match source {
@ -93,51 +92,51 @@ fn main() {
app.connect_activate(move |app| {
let (tx, mut rx) = postage::mpsc::channel(1);
let mut launcher = spawn_launcher(tx.clone());
{
let mut global_tx = TX.lock().unwrap();
*global_tx = Some(tx.clone());
}
if TX.set(tx).is_err() {
println!("failed to set global Sender. Exiting");
std::process::exit(1);
};
let window = Window::new(app);
let wclone = window.clone();
window.show();
let window = Window::new(app);
let wclone = window.clone();
window.show();
glib::MainContext::default().spawn_local(async move {
while let Some(event) = rx.recv().await {
match event {
Event::Search(search) => {
let _ = launcher.send(pop_launcher::Request::Search(search)).await;
}
Event::Activate(index) => {
let _ = launcher.send(pop_launcher::Request::Activate(index)).await;
}
glib::MainContext::default().spawn_local(async move {
while let Some(event) = rx.recv().await {
match event {
Event::Search(search) => {
let _ = launcher.send(pop_launcher::Request::Search(search)).await;
}
Event::Activate(index) => {
let _ = launcher.send(pop_launcher::Request::Activate(index)).await;
}
Event::Response(event) => {
if let pop_launcher::Response::Update(results) = event {
let model = window.model();
let model_len = model.n_items();
dbg!(&results);
let new_results: Vec<glib::Object> = results
[0..std::cmp::min(results.len(), NUM_LAUNCHER_ITEMS.into())]
.iter()
.map(|result| ApplicationObject::new(result).upcast())
.collect();
model.splice(0, model_len, &new_results[..]);
} else if let pop_launcher::Response::DesktopEntry {
path,
gpu_preference: _gpu_preference, // TODO use GPU preference when launching app
} = event
{
let app_info =
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.");
app_info
.launch(&[], Some(&wclone.display().app_launch_context().clone())).expect("failed to launch the application.");
}
}
}
}
})
Event::Response(event) => {
if let pop_launcher::Response::Update(results) = event {
let model = window.model();
let model_len = model.n_items();
dbg!(&results);
let new_results: Vec<glib::Object> = results
[0..std::cmp::min(results.len(), NUM_LAUNCHER_ITEMS.into())]
.iter()
.map(|result| ApplicationObject::new(result).upcast())
.collect();
model.splice(0, model_len, &new_results[..]);
} else if let pop_launcher::Response::DesktopEntry {
path,
gpu_preference: _gpu_preference, // TODO use GPU preference when launching app
} = event
{
let app_info =
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.");
app_info
.launch(&[], Some(&wclone.display().app_launch_context().clone())).expect("failed to launch the application.");
}
}
}
}
})
});
app.run();

View file

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