Skip to content

Commit 041df56

Browse files
committed
save project from menu bar
1 parent a70538a commit 041df56

File tree

4 files changed

+38
-5
lines changed

4 files changed

+38
-5
lines changed

lang/en_us.yml

+1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ menu_bar:
22
file:
33
.: File
44
quit: Quit
5+
save: Save
56

67
tab:
78
game:

lang/zh_cn.yml

+1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ menu_bar:
22
file:
33
.: 文件
44
quit: 退出
5+
save: 保存
56

67
tab:
78
game:

src/main.rs

+25-5
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,8 @@ use crate::file::FilePickingPlugin;
3030
use crate::home::HomePlugin;
3131
use crate::misc::MiscPlugin;
3232
use crate::misc::WorkingDirectory;
33-
use crate::notification::NotificationPlugin;
34-
use crate::project::project_loaded;
33+
use crate::notification::{NotificationPlugin, ToastsExt, ToastsStorage};
34+
use crate::project::{Project, project_loaded};
3535
use crate::project::LoadProjectEvent;
3636
use crate::project::ProjectPlugin;
3737
use crate::score::ScorePlugin;
@@ -65,7 +65,7 @@ fn main() {
6565
.add_plugins(DefaultPickingPlugins)
6666
.add_plugins(EguiPlugin)
6767
.add_plugins(ProjectPlugin)
68-
.add_plugins(crate::selection::SelectionPlugin)
68+
.add_plugins(selection::SelectionPlugin)
6969
.add_plugins(MiscPlugin)
7070
.add_plugins(TabPlugin)
7171
.add_plugins(EditingPlugin)
@@ -249,10 +249,30 @@ fn ui_system(world: &mut World) {
249249

250250
let mut system_state: SystemState<Translator> = SystemState::new(world);
251251
let translator = system_state.get(world);
252+
let file = translator.tr("menu_bar.file");
253+
let save = translator.tr("menu_bar.file.save");
254+
let quit = translator.tr("menu_bar.file.quit");
255+
252256
egui::TopBottomPanel::top("phichain.MenuBar").show(ctx, |ui| {
253257
egui::menu::bar(ui, |ui| {
254-
ui.menu_button(translator.tr("menu_bar.file"), |ui| {
255-
if ui.button(translator.tr("menu_bar.file.quit")).clicked() {
258+
ui.menu_button(file, |ui| {
259+
if ui.button(save).clicked() {
260+
if let Ok(chart) = PhiChainExporter::export(world) {
261+
let project = world.resource::<Project>();
262+
let result = std::fs::write(project.root_dir.join("chart.json"), chart);
263+
let mut toasts = world.resource_mut::<ToastsStorage>();
264+
match result {
265+
Ok(_) => {
266+
toasts.success("Project saved");
267+
}
268+
Err(error) => {
269+
toasts.error(format!("Failed to save project: {}", error));
270+
}
271+
}
272+
}
273+
}
274+
ui.separator();
275+
if ui.button(quit).clicked() {
256276
std::process::exit(0);
257277
}
258278
});

src/notification.rs

+11
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ impl Plugin for NotificationPlugin {
1717

1818
pub trait ToastsExt {
1919
fn error(&mut self, message: impl Into<WidgetText>);
20+
fn success(&mut self, message: impl Into<WidgetText>);
2021
}
2122

2223
impl ToastsExt for Toasts {
@@ -29,6 +30,16 @@ impl ToastsExt for Toasts {
2930
.show_progress(true),
3031
});
3132
}
33+
34+
fn success(&mut self, text: impl Into<WidgetText>) {
35+
self.add(Toast {
36+
text: text.into(),
37+
kind: ToastKind::Success,
38+
options: ToastOptions::default()
39+
.duration_in_seconds(8.0)
40+
.show_progress(true),
41+
});
42+
}
3243
}
3344

3445
impl Default for ToastsStorage {

0 commit comments

Comments
 (0)