Skip to content

Commit 418c871

Browse files
committed
Add option to specify persistence path
1 parent 2f508d6 commit 418c871

File tree

5 files changed

+43
-15
lines changed

5 files changed

+43
-15
lines changed

crates/eframe/src/epi.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -362,6 +362,10 @@ pub struct NativeOptions {
362362
/// Controls whether or not the native window position and size will be
363363
/// persisted (only if the "persistence" feature is enabled).
364364
pub persist_window: bool,
365+
366+
/// The folder where `eframe` will store the app state. If not set, eframe will get the paths
367+
/// from [directories_next].
368+
pub persistence_path: Option<std::path::PathBuf>,
365369
}
366370

367371
#[cfg(not(target_arch = "wasm32"))]
@@ -379,6 +383,8 @@ impl Clone for NativeOptions {
379383
#[cfg(feature = "wgpu")]
380384
wgpu_options: self.wgpu_options.clone(),
381385

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

Lines changed: 11 additions & 0 deletions
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,15 @@ 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+
#[cfg(not(feature = "persistence"))]
140+
None
141+
}
142+
132143
// ----------------------------------------------------------------------------
133144

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

crates/eframe/src/native/file_storage.rs

Lines changed: 1 addition & 1 deletion
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

Lines changed: 11 additions & 7 deletions
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

Lines changed: 11 additions & 7 deletions
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)