Skip to content

Commit

Permalink
Merge pull request #5 from edvorg/main
Browse files Browse the repository at this point in the history
add JSON import, fix #2
  • Loading branch information
grantshandy authored Sep 3, 2024
2 parents 39e5423 + f8d5985 commit 36dfeff
Show file tree
Hide file tree
Showing 3 changed files with 106 additions and 16 deletions.
13 changes: 5 additions & 8 deletions src/export.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use std::{future::Future, time::Duration};

use eframe::{
egui::{ComboBox, Context, Direction, Layout, Style, Ui},
egui::{ComboBox, Direction, Layout, Style, Ui},
emath::Align,
};
use egui_notify::Toasts;
Expand All @@ -17,14 +17,13 @@ const TEMPLATE: &str = include_str!("template.rs.hbs");

#[derive(Default)]
pub struct ExportMenu {
toasts: Toasts,
eframe: bool,
export_format: ExportFormat,
json_pretty: bool,
}

impl ExportMenu {
pub fn ui(&mut self, ui: &mut Ui, ctx: &Context, style: &Style) {
pub fn ui(&mut self, ui: &mut Ui, style: &Style, toasts: &mut Toasts) {
ui.add(section_title("Export", None));

ui.with_layout(Layout::top_down(Align::Max), |ui| {
Expand Down Expand Up @@ -54,15 +53,13 @@ impl ExportMenu {
Layout::centered_and_justified(Direction::TopDown),
|ui| {
if ui.button("Export").clicked() {
self.export(style);
self.export(style, toasts);
}
},
);

self.toasts.show(ctx);
}

pub fn export(&mut self, style: &Style) {
pub fn export(&mut self, style: &Style, toasts: &mut Toasts) {
let generated = match (self.export_format, self.json_pretty) {
(ExportFormat::RustSource, _) => self.generate_source(style),
(ExportFormat::Json, true) => {
Expand All @@ -89,7 +86,7 @@ impl ExportMenu {
});
}
Err(err) => {
self.toasts
toasts
.error(format!("Export Error: {err}"))
.set_duration(Some(Duration::from_secs(5)));
}
Expand Down
56 changes: 56 additions & 0 deletions src/import.rs
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();
}
});
}
}
);
}
}
53 changes: 45 additions & 8 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use std::sync::mpsc::{sync_channel, Receiver, SyncSender};
use eframe::{
egui::{
Frame, Hyperlink, Layout, Margin, Response, RichText, ScrollArea, SidePanel, Style, Ui,
Expand All @@ -6,25 +7,28 @@ use eframe::{
emath::Align,
};
use egui_demo_lib::DemoWindows;
use egui_notify::Toasts;
use export::ExportMenu;
use interaction::InteractionMenu;
use misc::MiscMenu;
use spacing::SpacingMenu;
use visuals::VisualsMenu;
use import::ImportMenu;

mod export;
mod interaction;
mod misc;
mod pickers;
mod spacing;
mod visuals;
mod import;

#[cfg(not(target_arch = "wasm32"))]
fn main() {
eframe::run_native(
"Egui Themer",
eframe::NativeOptions::default(),
Box::new(|_| Ok(Box::new(Themer::default()))),
Box::new(|_| Ok(Box::new(Themer::new()))),
)
.expect("run eframe native app");
}
Expand All @@ -36,30 +40,54 @@ fn main() {
.start(
"app",
eframe::WebOptions::default(),
Box::new(|_| Ok(Box::new(Themer::default()))),
Box::new(|_| Ok(Box::new(Themer::new()))),
)
.await
.expect("failed to start eframe");
});
}

#[derive(Default)]
struct Themer {
toasts: Toasts,
toasts_tx: SyncSender<Box<dyn Fn(&mut Toasts) + Send>>,
toasts_rx: Receiver<Box<dyn Fn(&mut Toasts) + Send>>,
demo: DemoWindows,
import: ImportMenu,
export: ExportMenu,
visuals: VisualsMenu,
misc: MiscMenu,
spacing: SpacingMenu,
interaction: InteractionMenu,
}

impl Themer {
fn new() -> Self {
let (toasts_tx, toasts_rx) = sync_channel(100);
Self {
toasts: Default::default(),
toasts_tx,
toasts_rx,
demo: Default::default(),
import: Default::default(),
export: Default::default(),
visuals: Default::default(),
misc: Default::default(),
spacing: Default::default(),
interaction: Default::default(),
}
}
}

impl eframe::App for Themer {
fn update(&mut self, ctx: &eframe::egui::Context, _frame: &mut eframe::Frame) {
if let Ok(f) = self.toasts_rx.try_recv() {
f(&mut self.toasts);
ctx.request_repaint();
}

SidePanel::left("themer_side_panel")
.min_width(370.0)
.show(ctx, |ui| {
let mut style = (*ctx.style()).clone();

ui.hyperlink_to(
RichText::new("Egui Themer").heading(),
"https://github.com/grantshandy/egui-themer/",
Expand All @@ -69,25 +97,32 @@ impl eframe::App for Themer {
cols[0].label("Reset:");
cols[1].with_layout(Layout::right_to_left(Align::Min), |ui| {
if ui.button("Light").clicked() {
style = Style {
let style = Style {
visuals: Visuals::light(),
..Default::default()
};
ctx.set_style(style);
self.visuals = VisualsMenu::default();
}

if ui.button("Dark").clicked() {
style = Style {
let style = Style {
visuals: Visuals::dark(),
..Default::default()
};
ctx.set_style(style);
self.visuals = VisualsMenu::default();
}
})
});
ui.separator();

self.export.ui(ui, ctx, &style);
self.import.ui(ui, ctx, &self.toasts_tx);
ui.separator();

let mut style = (*ctx.style()).clone();

self.export.ui(ui, &style, &mut self.toasts);
ui.separator();

ScrollArea::both().show(ui, |ui| {
Expand All @@ -107,6 +142,8 @@ impl eframe::App for Themer {
});

self.demo.ui(ctx);

self.toasts.show(ctx);
}
}

Expand Down

0 comments on commit 36dfeff

Please sign in to comment.