Skip to content

Commit

Permalink
desktop: Use Arc+Mutex for PathOrUrlField.value
Browse files Browse the repository at this point in the history
This allows modifying it asynchronously.
  • Loading branch information
kjarosh authored and torokati44 committed Aug 20, 2024
1 parent d487d3f commit 8755dd9
Showing 1 changed file with 19 additions and 9 deletions.
28 changes: 19 additions & 9 deletions desktop/src/gui/widgets.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@ use crate::gui::text;
use crate::util::pick_file;
use egui::{TextEdit, Ui};
use std::path::Path;
use std::sync::Weak;
use std::sync::{Arc, Mutex, MutexGuard, Weak};
use unic_langid::LanguageIdentifier;
use url::Url;

pub struct PathOrUrlField {
window: Weak<winit::window::Window>,
value: String,
value: Arc<Mutex<String>>,
result: Option<Url>,
hint: &'static str,
}
Expand All @@ -24,7 +24,7 @@ impl PathOrUrlField {
if let Ok(path) = default.to_file_path() {
return Self {
window,
value: path.to_string_lossy().to_string(),
value: Arc::new(Mutex::new(path.to_string_lossy().to_string())),
result: Some(default),
hint,
};
Expand All @@ -33,20 +33,24 @@ impl PathOrUrlField {

return Self {
window,
value: default.to_string(),
value: Arc::new(Mutex::new(default.to_string())),
result: Some(default),
hint,
};
}

Self {
window,
value: "".to_string(),
value: Arc::new(Mutex::new("".to_string())),
result: None,
hint,
}
}

fn lock_value(value: &Arc<Mutex<String>>) -> MutexGuard<'_, String> {
value.lock().expect("Non-poisoned value")
}

pub fn ui(&mut self, locale: &LanguageIdentifier, ui: &mut Ui) -> &mut Self {
ui.with_layout(egui::Layout::right_to_left(egui::Align::Center), |ui| {
if ui.button(text(locale, "browse")).clicked() {
Expand All @@ -61,26 +65,32 @@ impl PathOrUrlField {
});

if let Some(path) = pick_file(true, dir, self.window.upgrade()) {
self.value = path.to_string_lossy().to_string();
let mut value_lock = Self::lock_value(&self.value);
*value_lock = path.to_string_lossy().to_string();
}
}

let mut value_locked = Self::lock_value(&self.value);
let mut value = value_locked.clone();
ui.add_sized(
ui.available_size(),
TextEdit::singleline(&mut self.value)
TextEdit::singleline(&mut value)
.hint_text(self.hint)
.text_color_opt(if self.result.is_none() {
Some(ui.style().visuals.error_fg_color)
} else {
None
}),
);
*value_locked = value;
});

let path = Path::new(&self.value);
let value = Self::lock_value(&self.value).clone();
let path = Path::new(&value);
self.result = if path.is_file() {
Url::from_file_path(path).ok()
} else {
Url::parse(&self.value).ok()
Url::parse(&value).ok()
};

self
Expand Down

0 comments on commit 8755dd9

Please sign in to comment.