Skip to content

Commit

Permalink
enhance: hide Settings for raster image export
Browse files Browse the repository at this point in the history
  • Loading branch information
carrascomj committed Oct 27, 2023
1 parent 49c620c commit faa02dd
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 7 deletions.
12 changes: 9 additions & 3 deletions src/gui.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ pub struct UiState {
pub map_path: String,
pub data_path: String,
pub screen_path: String,
pub hide: bool,
// since this type and field are private, Self has to be initialized
// with Default::default(), ensuring that the fallbacks for colors (empty string) are set.
_init: Init,
Expand Down Expand Up @@ -144,6 +145,7 @@ impl Default for UiState {
screen_path: format!("screenshot-{}.svg", Utc::now().format("%T-%Y")),
map_path: String::from("my_map.json"),
data_path: String::from("my_data.metabolism.json"),
hide: false,
_init: Init,
}
}
Expand Down Expand Up @@ -179,18 +181,21 @@ impl UiState {
}

#[derive(Event)]
struct SaveEvent(String);
pub struct SaveEvent(String);

/// Settings for appearance of map and plots.
/// This is managed by [`bevy_egui`] and it is separate from the rest of the GUI.
fn ui_settings(
pub fn ui_settings(
mut egui_context: EguiContexts,
mut state: ResMut<UiState>,
mut save_events: EventWriter<SaveEvent>,
mut load_events: EventWriter<FileDragAndDrop>,
mut screen_events: EventWriter<ScreenshotEvent>,
windows: Query<(Entity, &Window), With<PrimaryWindow>>,
) {
if state.hide {
return;
}
egui::Window::new("Settings").show(egui_context.ctx_mut(), |ui| {
for (geom, ext) in ["Reaction", "Metabolite"]
.into_iter()
Expand Down Expand Up @@ -248,7 +253,8 @@ fn ui_settings(
if ui.button("Image").clicked() {
screen_events.send(ScreenshotEvent {
path: state.screen_path.clone(),
})
});
state.hide = true;
}
ui.text_edit_singleline(&mut state.screen_path);
})
Expand Down
33 changes: 29 additions & 4 deletions src/screenshot.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use crate::{
escher::MapDimensions,
funcplot::IgnoreSave,
geom::Drag,
gui::UiState,
info::Info,
legend::{Xmax, Xmin},
};
Expand All @@ -20,7 +21,14 @@ impl Plugin for ScreenShotPlugin {
.add_event::<SvgScreenshotEvent>()
.add_asset::<RawAsset>()
.init_asset_loader::<RawAssetLoader>()
.add_systems(Update, (screenshot_on_event, save_svg_file));
.add_systems(Startup, setup_timer)
.add_systems(
Update,
(
screenshot_on_event.before(crate::gui::ui_settings),
save_svg_file,
),
);
}
}

Expand All @@ -34,29 +42,46 @@ pub struct SvgScreenshotEvent {
pub file_path: String,
}

#[derive(Component, Deref, DerefMut)]
struct HideUiTimer(Timer);

fn setup_timer(mut commands: Commands) {
commands.spawn(HideUiTimer(Timer::from_seconds(0.2, TimerMode::Once)));
}

fn screenshot_on_event(
mut save_events: EventReader<ScreenshotEvent>,
mut send_svg_events: EventWriter<SvgScreenshotEvent>,
time: Res<Time>,
mut ui_state: ResMut<UiState>,
mut info_state: ResMut<Info>,
main_window: Query<Entity, With<PrimaryWindow>>,
mut screenshot_manager: ResMut<ScreenshotManager>,
main_window: Query<Entity, With<PrimaryWindow>>,
mut timer: Query<&mut HideUiTimer>,
mut counter: Local<u32>,
) {
let Ok(mut timer) = timer.get_single_mut() else {
return;
};
if timer.tick(time.delta()).just_finished() {
ui_state.hide = false;
}
for ScreenshotEvent { path } in save_events.iter() {
// if there is no extension, add a
timer.reset();
if path.ends_with("svg") {
info_state.notify("Writing SVG...");
send_svg_events.send(SvgScreenshotEvent {
file_path: path.clone(),
});
continue;
}
// if there is no extension, add png
let suffix = if path.split('.').count() >= 2 {
""
} else {
".png"
};
info_state.notify("Writing rastered image...");
info!("Writing raster imag...");
let path = format!("{path}{suffix}");
*counter += 1;
if let Err(e) = screenshot_manager.save_screenshot_to_disk(main_window.single(), path) {
Expand Down

0 comments on commit faa02dd

Please sign in to comment.