mirror of
https://github.com/tauri-apps/plugins-workspace.git
synced 2026-07-24 17:20:51 +02:00
refactor(window-state)!: use json instead of bincode (#1078)
* feat(window-state): add option to use json * feat(window-state): change state file from bincode to serdejson * change file * fmt
This commit is contained in:
@@ -0,0 +1,5 @@
|
|||||||
|
---
|
||||||
|
"window-state": patch
|
||||||
|
---
|
||||||
|
|
||||||
|
**Breaking change**: Changed the format of the state file from bincode to json. Also changed the filename to from `.window-state` to `.window-state.json`.
|
||||||
@@ -21,5 +21,4 @@ serde_json = { workspace = true }
|
|||||||
tauri = { workspace = true }
|
tauri = { workspace = true }
|
||||||
log = { workspace = true }
|
log = { workspace = true }
|
||||||
thiserror = { workspace = true }
|
thiserror = { workspace = true }
|
||||||
bincode = "1.3"
|
|
||||||
bitflags = "2"
|
bitflags = "2"
|
||||||
|
|||||||
@@ -23,13 +23,12 @@ use tauri::{
|
|||||||
use std::{
|
use std::{
|
||||||
collections::{HashMap, HashSet},
|
collections::{HashMap, HashSet},
|
||||||
fs::{create_dir_all, File},
|
fs::{create_dir_all, File},
|
||||||
io::Write,
|
|
||||||
sync::{Arc, Mutex},
|
sync::{Arc, Mutex},
|
||||||
};
|
};
|
||||||
|
|
||||||
mod cmd;
|
mod cmd;
|
||||||
|
|
||||||
pub const STATE_FILENAME: &str = ".window-state";
|
pub const STATE_FILENAME: &str = ".window-state.json";
|
||||||
|
|
||||||
#[derive(Debug, thiserror::Error)]
|
#[derive(Debug, thiserror::Error)]
|
||||||
pub enum Error {
|
pub enum Error {
|
||||||
@@ -38,7 +37,7 @@ pub enum Error {
|
|||||||
#[error(transparent)]
|
#[error(transparent)]
|
||||||
Tauri(#[from] tauri::Error),
|
Tauri(#[from] tauri::Error),
|
||||||
#[error(transparent)]
|
#[error(transparent)]
|
||||||
Bincode(#[from] Box<bincode::ErrorKind>),
|
SerdeJson(#[from] serde_json::Error),
|
||||||
}
|
}
|
||||||
|
|
||||||
pub type Result<T> = std::result::Result<T, Error>;
|
pub type Result<T> = std::result::Result<T, Error>;
|
||||||
@@ -116,10 +115,7 @@ impl<R: Runtime> AppHandleExt for tauri::AppHandle<R> {
|
|||||||
create_dir_all(&app_dir)
|
create_dir_all(&app_dir)
|
||||||
.map_err(Error::Io)
|
.map_err(Error::Io)
|
||||||
.and_then(|_| File::create(state_path).map_err(Into::into))
|
.and_then(|_| File::create(state_path).map_err(Into::into))
|
||||||
.and_then(|mut f| {
|
.and_then(|mut f| serde_json::to_writer_pretty(&mut f, &*state).map_err(Into::into))
|
||||||
f.write_all(&bincode::serialize(&*state).map_err(Error::Bincode)?)
|
|
||||||
.map_err(Into::into)
|
|
||||||
})
|
|
||||||
} else {
|
} else {
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
@@ -324,23 +320,24 @@ impl Builder {
|
|||||||
cmd::restore_state
|
cmd::restore_state
|
||||||
])
|
])
|
||||||
.setup(|app, _api| {
|
.setup(|app, _api| {
|
||||||
let cache: Arc<Mutex<HashMap<String, WindowState>>> = if let Ok(app_dir) =
|
let cache: Arc<Mutex<HashMap<String, WindowState>>> =
|
||||||
app.path().app_config_dir()
|
if let Ok(app_dir) = app.path().app_config_dir() {
|
||||||
{
|
let state_path = app_dir.join(STATE_FILENAME);
|
||||||
let state_path = app_dir.join(STATE_FILENAME);
|
if state_path.exists() {
|
||||||
if state_path.exists() {
|
Arc::new(Mutex::new(
|
||||||
Arc::new(Mutex::new(
|
std::fs::read(state_path)
|
||||||
std::fs::read(state_path)
|
.map_err(Error::from)
|
||||||
.map_err(Error::from)
|
.and_then(|state| {
|
||||||
.and_then(|state| bincode::deserialize(&state).map_err(Into::into))
|
serde_json::from_slice(&state).map_err(Into::into)
|
||||||
.unwrap_or_default(),
|
})
|
||||||
))
|
.unwrap_or_default(),
|
||||||
|
))
|
||||||
|
} else {
|
||||||
|
Default::default()
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
Default::default()
|
Default::default()
|
||||||
}
|
};
|
||||||
} else {
|
|
||||||
Default::default()
|
|
||||||
};
|
|
||||||
app.manage(WindowStateCache(cache));
|
app.manage(WindowStateCache(cache));
|
||||||
Ok(())
|
Ok(())
|
||||||
})
|
})
|
||||||
|
|||||||
Reference in New Issue
Block a user