From 7024ddda2c8d49a3fb3b054066fdc73130ff0aa4 Mon Sep 17 00:00:00 2001 From: barsoosayque Date: Sat, 1 Feb 2025 17:24:41 +0700 Subject: [PATCH] Editor: use bincode to hopefully fix persistent state --- crates/opensi-editor/Cargo.toml | 1 + crates/opensi-editor/src/app/mod.rs | 12 ++++++++++-- 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/crates/opensi-editor/Cargo.toml b/crates/opensi-editor/Cargo.toml index ff3332c..5e4bf8b 100644 --- a/crates/opensi-editor/Cargo.toml +++ b/crates/opensi-editor/Cargo.toml @@ -30,6 +30,7 @@ egui_extras = "0.30.0" itertools = "0.14.0" egui-modal = "0.6.0" const_format = "0.2.34" +bincode = "1.3.3" # native: [target.'cfg(not(target_arch = "wasm32"))'.dependencies] diff --git a/crates/opensi-editor/src/app/mod.rs b/crates/opensi-editor/src/app/mod.rs index 9c91024..66243c2 100644 --- a/crates/opensi-editor/src/app/mod.rs +++ b/crates/opensi-editor/src/app/mod.rs @@ -7,6 +7,7 @@ mod theme_tab; mod workarea; use itertools::Itertools; +use log::error; use opensi_core::prelude::*; use crate::{app::file_dialogs::LoadingPackageReceiver, icon_format, icon_str}; @@ -27,7 +28,9 @@ impl EditorApp { // Load previous app state (if any). // Note that you must enable the `persistence` feature for this to work. let app: Self = if let Some(storage) = cc.storage { - eframe::get_value(storage, eframe::APP_KEY).unwrap_or_default() + eframe::get_value::>(storage, eframe::APP_KEY) + .and_then(|binary| bincode::deserialize(&binary).ok()) + .unwrap_or_default() } else { Default::default() }; @@ -53,7 +56,12 @@ impl EditorApp { impl eframe::App for EditorApp { /// Called by the frame work to save state before shutdown. fn save(&mut self, storage: &mut dyn eframe::Storage) { - eframe::set_value(storage, eframe::APP_KEY, self); + match bincode::serialize(self) { + Ok(binary) => { + eframe::set_value(storage, eframe::APP_KEY, &binary); + }, + Err(err) => error!("Unable to bincode app state: {err}"), + } } fn update(&mut self, ctx: &egui::Context, _frame: &mut eframe::Frame) {