Skip to content

Commit 5d3be5b

Browse files
committed
Add option to specify persistence path
1 parent 2f508d6 commit 5d3be5b

File tree

5 files changed

+42
-15
lines changed

5 files changed

+42
-15
lines changed

crates/eframe/src/epi.rs

+9
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
#[cfg(target_arch = "wasm32")]
1010
use std::any::Any;
11+
use std::path::PathBuf;
1112

1213
#[cfg(not(target_arch = "wasm32"))]
1314
#[cfg(any(feature = "glow", feature = "wgpu"))]
@@ -362,6 +363,8 @@ pub struct NativeOptions {
362363
/// Controls whether or not the native window position and size will be
363364
/// persisted (only if the "persistence" feature is enabled).
364365
pub persist_window: bool,
366+
367+
pub persistence_path: Option<std::path::PathBuf>,
365368
}
366369

367370
#[cfg(not(target_arch = "wasm32"))]
@@ -379,6 +382,9 @@ impl Clone for NativeOptions {
379382
#[cfg(feature = "wgpu")]
380383
wgpu_options: self.wgpu_options.clone(),
381384

385+
#[cfg(feature = "persistence")]
386+
persistence_path: self.persistence_path.clone(),
387+
382388
..*self
383389
}
384390
}
@@ -418,6 +424,9 @@ impl Default for NativeOptions {
418424
wgpu_options: egui_wgpu::WgpuConfiguration::default(),
419425

420426
persist_window: true,
427+
428+
#[cfg(feature = "persistence")]
429+
persistence_path: None,
421430
}
422431
}
423432
}

crates/eframe/src/native/epi_integration.rs

+10
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
//! Common tools used by [`super::glow_integration`] and [`super::wgpu_integration`].
22
33
use web_time::Instant;
4+
5+
use std::path::PathBuf;
46
use winit::event_loop::EventLoopWindowTarget;
57

68
use raw_window_handle::{HasDisplayHandle as _, HasWindowHandle as _};
@@ -129,6 +131,14 @@ pub fn create_storage(_app_name: &str) -> Option<Box<dyn epi::Storage>> {
129131
None
130132
}
131133

134+
pub fn create_storage_with_file(file: impl Into<PathBuf>) -> Option<Box<dyn epi::Storage>> {
135+
#[cfg(feature = "persistence")]
136+
return Some(Box::new(
137+
super::file_storage::FileStorage::from_ron_filepath(file),
138+
));
139+
None
140+
}
141+
132142
// ----------------------------------------------------------------------------
133143

134144
/// Everything needed to make a winit-based integration for [`epi`].

crates/eframe/src/native/file_storage.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ impl Drop for FileStorage {
4141

4242
impl FileStorage {
4343
/// Store the state in this .ron file.
44-
fn from_ron_filepath(ron_filepath: impl Into<PathBuf>) -> Self {
44+
pub(crate) fn from_ron_filepath(ron_filepath: impl Into<PathBuf>) -> Self {
4545
crate::profile_function!();
4646
let ron_filepath: PathBuf = ron_filepath.into();
4747
log::debug!("Loading app state from {:?}…", ron_filepath);

crates/eframe/src/native/glow_integration.rs

+11-7
Original file line numberDiff line numberDiff line change
@@ -195,13 +195,17 @@ impl GlowWinitApp {
195195
) -> Result<&mut GlowWinitRunning> {
196196
crate::profile_function!();
197197

198-
let storage = epi_integration::create_storage(
199-
self.native_options
200-
.viewport
201-
.app_id
202-
.as_ref()
203-
.unwrap_or(&self.app_name),
204-
);
198+
let storage = if let Some(file) = &self.native_options.persistence_path {
199+
epi_integration::create_storage_with_file(file)
200+
} else {
201+
epi_integration::create_storage(
202+
self.native_options
203+
.viewport
204+
.app_id
205+
.as_ref()
206+
.unwrap_or(&self.app_name),
207+
)
208+
};
205209

206210
let egui_ctx = create_egui_context(storage.as_deref());
207211

crates/eframe/src/native/wgpu_integration.rs

+11-7
Original file line numberDiff line numberDiff line change
@@ -420,13 +420,17 @@ impl WinitApp for WgpuWinitApp {
420420
self.recreate_window(event_loop, running);
421421
running
422422
} else {
423-
let storage = epi_integration::create_storage(
424-
self.native_options
425-
.viewport
426-
.app_id
427-
.as_ref()
428-
.unwrap_or(&self.app_name),
429-
);
423+
let storage = if let Some(file) = &self.native_options.persistence_path {
424+
epi_integration::create_storage_with_file(file)
425+
} else {
426+
epi_integration::create_storage(
427+
self.native_options
428+
.viewport
429+
.app_id
430+
.as_ref()
431+
.unwrap_or(&self.app_name),
432+
)
433+
};
430434
let egui_ctx = winit_integration::create_egui_context(storage.as_deref());
431435
let (window, builder) = create_window(
432436
&egui_ctx,

0 commit comments

Comments
 (0)