-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #5 from edvorg/main
add JSON import, fix #2
- Loading branch information
Showing
3 changed files
with
106 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
use std::sync::mpsc::SyncSender; | ||
use eframe::egui::{Direction, Layout, Style, Ui}; | ||
use egui_notify::Toasts; | ||
use crate::export::execute; | ||
use crate::section_title; | ||
|
||
#[derive(Default)] | ||
pub struct ImportMenu { | ||
} | ||
|
||
impl ImportMenu { | ||
pub fn ui( | ||
&mut self, | ||
ui: &mut Ui, | ||
ctx: &eframe::egui::Context, | ||
toasts_tx: &SyncSender<Box<dyn Fn(&mut Toasts) + Send>>, | ||
) { | ||
ui.add(section_title("Import", None)); | ||
|
||
ui.allocate_ui_with_layout( | ||
[ui.available_width(), 0.0].into(), | ||
Layout::centered_and_justified(Direction::TopDown), | ||
|ui| { | ||
if ui.button("Import JSON").clicked() { | ||
let task = rfd::AsyncFileDialog::new() | ||
.add_filter("JSON file", &["json"]) | ||
.pick_file(); | ||
let ctx = ctx.clone(); | ||
let toasts_tx = toasts_tx.clone(); | ||
execute(async move { | ||
let file = task.await; | ||
if let Some(file) = file { | ||
let text = file.read().await; | ||
let string = String::from_utf8_lossy(&text).to_string(); | ||
match serde_json::from_str::<Style>(&string) { | ||
Ok(style) => { | ||
ctx.set_style(style); | ||
toasts_tx.try_send(Box::new(|toasts| { | ||
toasts.info("Import successful"); | ||
})).unwrap(); | ||
} | ||
Err(e) => { | ||
let message = format!("Import failed: {}", e); | ||
toasts_tx.try_send(Box::new(move |toasts| { | ||
toasts.error(message.clone()); | ||
})).unwrap(); | ||
} | ||
} | ||
ctx.request_repaint(); | ||
} | ||
}); | ||
} | ||
} | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters