mirror of
https://github.com/anomalyco/hex.git
synced 2026-11-28 13:02:21 +01:00
* vocabulary: import and export CSV files behind a Lab The Vocabulary sheet gains Import… and Export… under Labs › Vocabulary files (off by default, every OS). Import merges a CSV or TXT file's terms (Teams' custom dictionary columns) into the list through the sheet's draft path and says what changed; Export writes the header and one row per entry through a native save dialog, atomically. The parser, merge, and writer live in dictation::vocabulary::csv with no new dependency, and desktop::file_dialog gains Rune's save target. * vocabulary: refuse bad imports and count merges exactly An unclosed quoted field, a path that isn't a regular file, an oversized file, or bytes that aren't UTF-8 now refuse the import with a translated reason, and a failed settings save says so instead of reporting success. A term this file added is never also counted as updated, a term counts once, and dropped spellings count as skipped. Export only adds .csv when that name is free, and one switch now hides the buttons, the status line, and the dialog answers together. * vocabulary: clarify csv export naming and fix a French string The export docs now say .csv is added only when the name has no extension and the .csv name is free, and the French unclosed-quote reason uses the masculine noun. The state-word bit says no gate reads it.
175 lines
5.5 KiB
Rust
175 lines
5.5 KiB
Rust
//! Native open and save dialogs for one file or one folder, through Rune's
|
||
//! `FileDialogs`: `NSOpenPanel`/`NSSavePanel` on macOS, the common item
|
||
//! dialog on Windows, and the FileChooser portal on Linux. Each runs on its
|
||
//! own thread and answers on a channel, so neither the window nor the runtime
|
||
//! waits on the person.
|
||
|
||
use std::path::PathBuf;
|
||
use std::sync::mpsc::{Receiver, SyncSender, TryRecvError, sync_channel};
|
||
|
||
fn show(dialog: Dialog, reply: Reply) {
|
||
// A failed spawn drops the reply, which answers "nothing chosen".
|
||
let _ = std::thread::Builder::new()
|
||
.name("hex-file-dialog".into())
|
||
.spawn(move || {
|
||
let dialogs = rune::FileDialogs::native();
|
||
let mut options = rune::FileDialogOptions {
|
||
title: dialog.title,
|
||
directory: dialog.directory.filter(|path| path.is_dir()),
|
||
..Default::default()
|
||
};
|
||
let choice = match dialog.target {
|
||
Target::Folder => pollster::block_on(dialogs.open_folder(options)),
|
||
Target::File(filters) => {
|
||
for filter in filters {
|
||
options = options.filter(filter.label, filter.extensions);
|
||
}
|
||
pollster::block_on(dialogs.open(options))
|
||
}
|
||
Target::Save { filters, file_name } => {
|
||
options.file_name = Some(file_name);
|
||
for filter in filters {
|
||
options = options.filter(filter.label, filter.extensions);
|
||
}
|
||
pollster::block_on(dialogs.save(options))
|
||
}
|
||
};
|
||
reply.send(choice.map_err(|error| error.to_string()));
|
||
});
|
||
}
|
||
|
||
pub(crate) struct Dialog {
|
||
pub(crate) title: String,
|
||
pub(crate) target: Target,
|
||
/// Where the dialog starts, when it exists.
|
||
pub(crate) directory: Option<PathBuf>,
|
||
}
|
||
|
||
pub(crate) enum Target {
|
||
/// One existing file.
|
||
File(Vec<Filter>),
|
||
/// A file to write, with the name the dialog offers first.
|
||
Save {
|
||
filters: Vec<Filter>,
|
||
file_name: String,
|
||
},
|
||
/// A folder: the macOS meetings and annotations folders, and Labs ›
|
||
/// Screenshots' saves folder.
|
||
#[cfg_attr(not(target_os = "macos"), allow(dead_code))]
|
||
Folder,
|
||
}
|
||
|
||
/// A named set of extensions without the dot ("app", "exe"); `"*"` is any
|
||
/// file.
|
||
pub(crate) struct Filter {
|
||
pub(crate) label: String,
|
||
pub(crate) extensions: &'static [&'static str],
|
||
}
|
||
|
||
/// The chosen path, `None` when the person cancelled.
|
||
pub(crate) type Choice = Result<Option<PathBuf>, String>;
|
||
|
||
/// Opens `dialog` without waiting for it; `wake` runs once it answers.
|
||
pub(crate) fn open(dialog: Dialog, wake: impl Fn() + Send + 'static) -> Pending {
|
||
let (reply, pending) = channel(wake);
|
||
show(dialog, reply);
|
||
pending
|
||
}
|
||
|
||
fn channel(wake: impl Fn() + Send + 'static) -> (Reply, Pending) {
|
||
let (sender, receiver) = sync_channel(1);
|
||
let reply = Reply {
|
||
sender,
|
||
wake: Box::new(wake),
|
||
answered: false,
|
||
};
|
||
(reply, Pending(receiver))
|
||
}
|
||
|
||
/// An open dialog's answer, polled from the window.
|
||
pub(crate) struct Pending(Receiver<Choice>);
|
||
|
||
impl Pending {
|
||
/// `None` while the dialog is open.
|
||
pub(crate) fn poll(&self) -> Option<Choice> {
|
||
match self.0.try_recv() {
|
||
Ok(choice) => Some(choice),
|
||
Err(TryRecvError::Empty) => None,
|
||
Err(TryRecvError::Disconnected) => Some(Ok(None)),
|
||
}
|
||
}
|
||
}
|
||
|
||
/// The dialog's one answer. Dropped unanswered (a dialog that never opened)
|
||
/// it answers "nothing chosen", so no caller waits on a dialog that is gone.
|
||
pub(crate) struct Reply {
|
||
sender: SyncSender<Choice>,
|
||
wake: Box<dyn Fn() + Send>,
|
||
answered: bool,
|
||
}
|
||
|
||
impl Reply {
|
||
pub(crate) fn send(mut self, choice: Choice) {
|
||
self.answer(choice);
|
||
}
|
||
|
||
fn answer(&mut self, choice: Choice) {
|
||
if std::mem::replace(&mut self.answered, true) {
|
||
return;
|
||
}
|
||
let _ = self.sender.try_send(choice);
|
||
(self.wake)();
|
||
}
|
||
}
|
||
|
||
impl Drop for Reply {
|
||
fn drop(&mut self) {
|
||
self.answer(Ok(None));
|
||
}
|
||
}
|
||
|
||
#[cfg(test)]
|
||
mod tests {
|
||
use std::sync::Arc;
|
||
use std::sync::atomic::{AtomicUsize, Ordering};
|
||
|
||
use super::*;
|
||
|
||
fn counted() -> (Arc<AtomicUsize>, impl Fn() + Send + 'static) {
|
||
let wakes = Arc::new(AtomicUsize::new(0));
|
||
let counter = wakes.clone();
|
||
(wakes, move || {
|
||
counter.fetch_add(1, Ordering::Relaxed);
|
||
})
|
||
}
|
||
|
||
#[test]
|
||
fn the_chosen_path_arrives_once_and_wakes_the_window() {
|
||
let (wakes, wake) = counted();
|
||
let (reply, pending) = channel(wake);
|
||
assert_eq!(pending.poll(), None);
|
||
reply.send(Ok(Some(PathBuf::from("/Applications/Zed.app"))));
|
||
assert_eq!(
|
||
pending.poll(),
|
||
Some(Ok(Some(PathBuf::from("/Applications/Zed.app"))))
|
||
);
|
||
assert_eq!(wakes.load(Ordering::Relaxed), 1);
|
||
}
|
||
|
||
#[test]
|
||
fn a_dialog_that_never_answers_reads_as_cancelled() {
|
||
let (wakes, wake) = counted();
|
||
let (reply, pending) = channel(wake);
|
||
drop(reply);
|
||
assert_eq!(pending.poll(), Some(Ok(None)));
|
||
assert_eq!(wakes.load(Ordering::Relaxed), 1);
|
||
}
|
||
|
||
#[test]
|
||
fn failures_reach_the_caller() {
|
||
let (_, wake) = counted();
|
||
let (reply, pending) = channel(wake);
|
||
reply.send(Err("no file chooser portal".into()));
|
||
assert_eq!(pending.poll(), Some(Err("no file chooser portal".into())));
|
||
}
|
||
}
|