mirror of https://codeberg.org/topola/topola.git
egui: get rid of FileHandlerData
This commit is contained in:
parent
4fef94a5c5
commit
d24acb601b
|
|
@ -1,6 +1,7 @@
|
|||
use serde::{Deserialize, Serialize};
|
||||
use std::{
|
||||
future::Future,
|
||||
io,
|
||||
sync::{
|
||||
mpsc::{channel, Receiver, Sender},
|
||||
Arc, Mutex,
|
||||
|
|
@ -21,7 +22,6 @@ use crate::{
|
|||
activity::{ActivityStatus, ActivityStepperWithStatus},
|
||||
config::Config,
|
||||
error_dialog::ErrorDialog,
|
||||
file_handler::FileHandlerData,
|
||||
layers::Layers,
|
||||
menu_bar::MenuBar,
|
||||
overlay::Overlay,
|
||||
|
|
@ -250,3 +250,13 @@ pub fn execute<F: Future<Output = ()> + Send + 'static>(f: F) {
|
|||
pub fn execute<F: Future<Output = ()> + 'static>(f: F) {
|
||||
wasm_bindgen_futures::spawn_local(f);
|
||||
}
|
||||
|
||||
pub async fn handle_file(file_handle: &rfd::FileHandle) -> io::Result<impl io::BufRead + io::Seek> {
|
||||
#[cfg(not(target_arch = "wasm32"))]
|
||||
let res = io::BufReader::new(std::fs::File::open(file_handle.path())?);
|
||||
|
||||
#[cfg(target_arch = "wasm32")]
|
||||
let res = io::Cursor::new(file_handle.read().await);
|
||||
|
||||
Ok(res)
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,61 +0,0 @@
|
|||
use std::io;
|
||||
use std::sync::mpsc::{SendError, Sender};
|
||||
|
||||
#[repr(transparent)]
|
||||
pub enum FileHandlerData {
|
||||
#[cfg(not(target_arch = "wasm32"))]
|
||||
File(io::BufReader<std::fs::File>),
|
||||
|
||||
#[cfg(target_arch = "wasm32")]
|
||||
Contents(io::Cursor<Vec<u8>>),
|
||||
}
|
||||
|
||||
macro_rules! fhd_forward {
|
||||
(match $self:expr => $fname:ident($($arg:expr,)*)) => {{
|
||||
match $self {
|
||||
#[cfg(not(target_arch = "wasm32"))]
|
||||
FileHandlerData::File(brf) => brf.$fname($($arg,)*),
|
||||
|
||||
#[cfg(target_arch = "wasm32")]
|
||||
FileHandlerData::Contents(curs) => curs.$fname($($arg,)*),
|
||||
}
|
||||
}};
|
||||
|
||||
(fn $fname:ident(&mut self $(,$arg:ident : $argty:ty)*)) => {
|
||||
#[inline]
|
||||
fn $fname(&mut self $(,$arg : $argty)*) {
|
||||
fhd_forward!(match self => $fname($($arg,)*))
|
||||
}
|
||||
};
|
||||
(fn $fname:ident(&mut self $(,$arg:ident : $argty:ty)*) -> $ret:ty) => {
|
||||
#[inline]
|
||||
fn $fname(&mut self $(,$arg : $argty)*) -> $ret {
|
||||
fhd_forward!(match self => $fname($($arg,)*))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl io::Read for FileHandlerData {
|
||||
fhd_forward!(fn read(&mut self, buf: &mut [u8]) -> io::Result<usize>);
|
||||
fhd_forward!(fn read_to_end(&mut self, buf: &mut Vec<u8>) -> io::Result<usize>);
|
||||
fhd_forward!(fn read_vectored(&mut self, bufs: &mut [io::IoSliceMut<'_>]) -> io::Result<usize>);
|
||||
}
|
||||
|
||||
impl io::BufRead for FileHandlerData {
|
||||
fhd_forward!(fn fill_buf(&mut self) -> io::Result<&[u8]>);
|
||||
fhd_forward!(fn consume(&mut self, amt: usize));
|
||||
}
|
||||
|
||||
pub async fn handle_file<R, E, C>(file_handle: &rfd::FileHandle, callback: C) -> Result<R, E>
|
||||
where
|
||||
E: From<std::io::Error>,
|
||||
C: FnOnce(FileHandlerData) -> Result<R, E>,
|
||||
{
|
||||
#[cfg(not(target_arch = "wasm32"))]
|
||||
let res = FileHandlerData::File(io::BufReader::new(std::fs::File::open(file_handle.path())?));
|
||||
|
||||
#[cfg(target_arch = "wasm32")]
|
||||
let res = FileHandlerData::Contents(io::Cursor::new(file_handle.read().await));
|
||||
|
||||
callback(res)
|
||||
}
|
||||
|
|
@ -5,7 +5,6 @@ mod activity;
|
|||
mod app;
|
||||
mod config;
|
||||
mod error_dialog;
|
||||
mod file_handler;
|
||||
mod layers;
|
||||
mod menu_bar;
|
||||
mod overlay;
|
||||
|
|
|
|||
|
|
@ -21,8 +21,7 @@ use topola::{
|
|||
use crate::{
|
||||
action::{Action, Switch, Trigger},
|
||||
activity::{ActivityStatus, ActivityStepperWithStatus},
|
||||
app::execute,
|
||||
file_handler::{handle_file, FileHandlerData},
|
||||
app::{execute, handle_file},
|
||||
overlay::Overlay,
|
||||
translator::Translator,
|
||||
viewport::Viewport,
|
||||
|
|
@ -243,9 +242,11 @@ impl MenuBar {
|
|||
|
||||
execute(async move {
|
||||
if let Some(file_handle) = task.await {
|
||||
content_sender.send(handle_file(&file_handle, |data| {
|
||||
SpecctraDesign::load(data)
|
||||
}).await);
|
||||
let data = handle_file(&file_handle)
|
||||
.await
|
||||
.map_err(Into::into)
|
||||
.and_then(SpecctraDesign::load);
|
||||
content_sender.send(data);
|
||||
ctx.request_repaint();
|
||||
}
|
||||
});
|
||||
|
|
@ -284,13 +285,14 @@ impl MenuBar {
|
|||
|
||||
execute(async move {
|
||||
if let Some(file_handle) = task.await {
|
||||
history_sender.send(handle_file(&file_handle, |data| {
|
||||
let data = handle_file(&file_handle).await.and_then(|data| {
|
||||
match serde_json::from_reader(data) {
|
||||
Ok(history) => Ok(Ok(history)),
|
||||
Err(err) if err.is_io() => Err(err.into()),
|
||||
Err(err) => Ok(Err(err)),
|
||||
}
|
||||
}).await);
|
||||
});
|
||||
history_sender.send(data);
|
||||
ctx.request_repaint();
|
||||
}
|
||||
});
|
||||
|
|
|
|||
Loading…
Reference in New Issue