Skip to content

Commit

Permalink
Editor: use bincode to hopefully fix persistent state
Browse files Browse the repository at this point in the history
  • Loading branch information
barsoosayque committed Feb 1, 2025
1 parent 1b22dda commit 7024ddd
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 2 deletions.
1 change: 1 addition & 0 deletions crates/opensi-editor/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
12 changes: 10 additions & 2 deletions crates/opensi-editor/src/app/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand All @@ -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::<Vec<u8>>(storage, eframe::APP_KEY)
.and_then(|binary| bincode::deserialize(&binary).ok())
.unwrap_or_default()
} else {
Default::default()
};
Expand All @@ -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) {
Expand Down

0 comments on commit 7024ddd

Please sign in to comment.