11use std:: fs;
22
3+ use anyhow:: anyhow;
34use egui:: { Color32 , DragValue , RichText , Ui } ;
45use log:: * ;
56
@@ -23,28 +24,23 @@ pub fn general_agent_area(ui: &mut Ui, model: &mut Model) {
2324 ui. small ( "(No JSON file loaded)" ) ;
2425 }
2526 ui. horizontal ( |ui| {
26- if ui. button ( "Save" ) . clicked ( ) {
27- if let Some ( path) = rfd:: FileDialog :: new ( )
27+ if let Some ( path_string) = & model. json_file {
28+ if ui. button ( "Save" ) . clicked ( ) {
29+ save_to_disk ( model, path_string) . expect ( "failed to save" ) ;
30+ }
31+ }
32+
33+ if ui. button ( "Save As..." ) . clicked ( ) {
34+ if let Some ( path_string) = rfd:: FileDialog :: new ( )
2835 . add_filter ( "text" , & [ "json" ] )
2936 . save_file ( )
37+ . map ( |path| path. display ( ) . to_string ( ) )
3038 {
31- // if model.project.tether_settings.was_changed {
32- // info!("Tether Settings were edited; copying these to project");
33- // model.project.tether_settings = Some(model.editable_tether_settings.clone());
34- // };
35- let path_string = path. display ( ) . to_string ( ) ;
36- let text = serde_json:: to_string_pretty ( & model. project )
37- . expect ( "failed to serialise widget data" ) ;
38- match fs:: write ( path_string, text) {
39- Ok ( ( ) ) => {
40- info ! ( "Saved OK" ) ;
41- }
42- Err ( e) => {
43- error ! ( "Error saving to disk: {:?}" , e) ;
44- }
45- }
39+ save_to_disk ( model, & path_string) . expect ( "failed to save as" ) ;
40+ model. json_file = Some ( path_string) ;
4641 }
4742 }
43+
4844 if ui. button ( "Load" ) . clicked ( ) {
4945 if let Some ( path) = rfd:: FileDialog :: new ( )
5046 . add_filter ( "text" , & [ "json" ] )
@@ -60,7 +56,7 @@ pub fn general_agent_area(ui: &mut Ui, model: &mut Model) {
6056 }
6157 }
6258 }
63- if ui. button ( "Clear " ) . clicked ( ) {
59+ if ui. button ( "New " ) . clicked ( ) {
6460 model. project . widgets . clear ( ) ;
6561 model. json_file = None ;
6662 }
@@ -127,3 +123,15 @@ pub fn general_agent_area(ui: &mut Ui, model: &mut Model) {
127123 }
128124 }
129125}
126+
127+ fn save_to_disk ( model : & Model , path : & str ) -> anyhow:: Result < ( ) > {
128+ let text =
129+ serde_json:: to_string_pretty ( & model. project ) . expect ( "failed to serialise widget data" ) ;
130+ match fs:: write ( path, text) {
131+ Ok ( _) => {
132+ info ! ( "Saved OK to \" {}\" " , path) ;
133+ Ok ( ( ) )
134+ }
135+ Err ( e) => Err ( anyhow ! ( "Error writing to disk: {}" , e) ) ,
136+ }
137+ }
0 commit comments