66
77use dyn_hash:: DynHash ;
88use eframe:: egui;
9- use gantz_core:: { Edge , steel:: steel_vm:: engine:: Engine } ;
9+ use gantz_core:: steel:: steel_vm:: engine:: Engine ;
1010use gantz_egui:: widget:: gantz:: { INLET_NAME , OUTLET_NAME } ;
1111use petgraph:: visit:: EdgeRef ;
1212use petgraph:: visit:: { IntoEdgeReferences , IntoNodeReferences , NodeRef } ;
@@ -125,27 +125,6 @@ fn node_type_registry() -> NodeTypeRegistry {
125125type Graph = gantz_core:: node:: graph:: Graph < Box < dyn Node > > ;
126126type GraphNode = gantz_core:: node:: GraphNode < Box < dyn Node > > ;
127127
128- /// Setup a simple demo graph.
129- fn new_graph ( ) -> GraphNode {
130- let mut graph = GraphNode :: default ( ) ;
131-
132- let button = graph. add_node ( Box :: new ( gantz_std:: Bang :: default ( ) ) as Box < dyn Node > ) ;
133- let n0 = graph. add_node ( Box :: new ( gantz_std:: Number :: default ( ) ) as Box < _ > ) ;
134- let n1 = graph. add_node ( Box :: new ( gantz_std:: Number :: default ( ) ) as Box < _ > ) ;
135- let add = graph. add_node ( Box :: new ( gantz_std:: ops:: Add :: default ( ) ) as Box < _ > ) ;
136- let n2 = graph. add_node ( Box :: new ( gantz_std:: Number :: default ( ) ) as Box < _ > ) ;
137- let log = graph. add_node ( Box :: new ( gantz_std:: log:: Log :: default ( ) ) as Box < _ > ) ;
138-
139- graph. add_edge ( button, n0, Edge :: from ( ( 0 , 0 ) ) ) ;
140- graph. add_edge ( button, n1, Edge :: from ( ( 0 , 0 ) ) ) ;
141- graph. add_edge ( n0, add, Edge :: from ( ( 0 , 0 ) ) ) ;
142- graph. add_edge ( n1, add, Edge :: from ( ( 0 , 1 ) ) ) ;
143- graph. add_edge ( add, n2, Edge :: from ( ( 0 , 0 ) ) ) ;
144- graph. add_edge ( n2, log, Edge :: from ( ( 0 , 0 ) ) ) ;
145-
146- graph
147- }
148-
149128// ----------------------------------------------
150129// Model
151130// ----------------------------------------------
@@ -158,6 +137,7 @@ struct State {
158137 graph : GraphNode ,
159138 graph_hash : u64 ,
160139 compiled_module : String ,
140+ logger : gantz_egui:: widget:: log_view:: Logger ,
161141 gantz : gantz_egui:: widget:: GantzState ,
162142 node_ty_reg : NodeTypeRegistry ,
163143 vm : Engine ,
@@ -168,9 +148,41 @@ struct State {
168148// ----------------------------------------------
169149
170150impl App {
151+ /// The key at which the graph is to be saved/loaded.
152+ const GRAPH_KEY : & str = "graph" ;
153+ /// The key at which the gantz widget state is to be saved/loaded.
154+ const GANTZ_GUI_STATE_KEY : & str = "gantz-widget-state" ;
155+
171156 pub fn new ( cc : & eframe:: CreationContext < ' _ > ) -> Self {
172- // Graph setup
173- let graph = new_graph ( ) ;
157+ // Setup logging.
158+ let logger = gantz_egui:: widget:: log_view:: Logger :: default ( ) ;
159+ log:: set_boxed_logger ( Box :: new ( logger. clone ( ) ) ) . unwrap ( ) ;
160+ log:: set_max_level ( log:: LevelFilter :: Info ) ;
161+
162+ // Load the graph or fallback to a default empty one.
163+ let graph = cc
164+ . storage
165+ . as_ref ( )
166+ . and_then ( |storage| {
167+ let Some ( graph_str) = storage. get_string ( Self :: GRAPH_KEY ) else {
168+ log:: info!( "No existing graph to load" ) ;
169+ return None ;
170+ } ;
171+ match ron:: de:: from_str ( & graph_str) {
172+ Ok ( graph) => {
173+ log:: info!( "Successfully loaded graph from storage" ) ;
174+ Some ( graph)
175+ }
176+ Err ( e) => {
177+ log:: error!( "Failed to deserialize graph: {e}" ) ;
178+ None
179+ }
180+ }
181+ } )
182+ . unwrap_or_else ( || {
183+ log:: info!( "Initialising default graph" ) ;
184+ GraphNode :: default ( )
185+ } ) ;
174186 let graph_hash = graph_hash ( & graph) ;
175187
176188 // VM setup
@@ -183,9 +195,34 @@ impl App {
183195 // GUI setup
184196 let ctx = & cc. egui_ctx ;
185197 ctx. set_fonts ( egui:: FontDefinitions :: default ( ) ) ;
186- let gantz = gantz_egui:: widget:: GantzState :: new ( ) ;
198+
199+ // Load the gantz GUI state or fallback to default.
200+ let gantz = cc
201+ . storage
202+ . as_ref ( )
203+ . and_then ( |storage| {
204+ let Some ( gantz_str) = storage. get_string ( Self :: GANTZ_GUI_STATE_KEY ) else {
205+ log:: info!( "No existing gantz GUI state to load" ) ;
206+ return None ;
207+ } ;
208+ match ron:: de:: from_str ( & gantz_str) {
209+ Ok ( gantz) => {
210+ log:: info!( "Successfully loaded gantz GUI state from storage" ) ;
211+ Some ( gantz)
212+ }
213+ Err ( e) => {
214+ log:: error!( "Failed to deserialize gantz GUI state: {e}" ) ;
215+ None
216+ }
217+ }
218+ } )
219+ . unwrap_or_else ( || {
220+ log:: info!( "Initialising default gantz GUI state" ) ;
221+ gantz_egui:: widget:: GantzState :: new ( )
222+ } ) ;
187223
188224 let state = State {
225+ logger,
189226 gantz,
190227 graph,
191228 graph_hash,
@@ -213,6 +250,35 @@ impl eframe::App for App {
213250 // Process any pending commands generated from the UI.
214251 process_cmds ( & mut self . state . gantz , & mut self . state . vm ) ;
215252 }
253+
254+ fn save ( & mut self , storage : & mut dyn eframe:: Storage ) {
255+ // Save the graph.
256+ let graph_str = match ron:: to_string ( & self . state . graph ) {
257+ Err ( e) => {
258+ log:: error!( "Failed to serialize and save graph: {e}" ) ;
259+ return ;
260+ }
261+ Ok ( s) => s,
262+ } ;
263+ storage. set_string ( Self :: GRAPH_KEY , graph_str) ;
264+ log:: info!( "Successfully persisted graph" ) ;
265+
266+ // Save the gantz GUI state.
267+ let gantz_str = match ron:: to_string ( & self . state . gantz ) {
268+ Err ( e) => {
269+ log:: error!( "Failed to serialize and save gantz GUI state: {e}" ) ;
270+ return ;
271+ }
272+ Ok ( s) => s,
273+ } ;
274+ storage. set_string ( Self :: GANTZ_GUI_STATE_KEY , gantz_str) ;
275+ log:: info!( "Successfully persisted gantz GUI state" ) ;
276+ }
277+
278+ // Persist GUI state.
279+ fn persist_egui_memory ( & self ) -> bool {
280+ true
281+ }
216282}
217283
218284// Drain the commands provided by the UI and process them.
@@ -281,6 +347,7 @@ fn gui(ctx: &egui::Context, state: &mut State) {
281347 . show ( ctx, |ui| {
282348 gantz_egui:: widget:: Gantz :: new ( & state. node_ty_reg , & mut state. graph ) . show (
283349 & mut state. gantz ,
350+ & state. logger ,
284351 & state. compiled_module ,
285352 & mut state. vm ,
286353 ui,
0 commit comments