Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

eframe: Add NativeOptions::persistence_path #4423

Merged
merged 4 commits into from
May 27, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions crates/eframe/src/epi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -362,6 +362,10 @@ pub struct NativeOptions {
/// Controls whether or not the native window position and size will be
/// persisted (only if the "persistence" feature is enabled).
pub persist_window: bool,

/// The folder where `eframe` will store the app state. If not set, eframe will get the paths
/// from [directories_next].
pub persistence_path: Option<std::path::PathBuf>,
}

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

persistence_path: self.persistence_path.clone(),

..*self
}
}
Expand Down Expand Up @@ -418,6 +424,8 @@ impl Default for NativeOptions {
wgpu_options: egui_wgpu::WgpuConfiguration::default(),

persist_window: true,

persistence_path: None,
}
}
}
Expand Down
11 changes: 11 additions & 0 deletions crates/eframe/src/native/epi_integration.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
//! Common tools used by [`super::glow_integration`] and [`super::wgpu_integration`].

use web_time::Instant;

use std::path::PathBuf;
use winit::event_loop::EventLoopWindowTarget;

use raw_window_handle::{HasDisplayHandle as _, HasWindowHandle as _};
Expand Down Expand Up @@ -129,6 +131,15 @@ pub fn create_storage(_app_name: &str) -> Option<Box<dyn epi::Storage>> {
None
}

pub fn create_storage_with_file(file: impl Into<PathBuf>) -> Option<Box<dyn epi::Storage>> {
#[cfg(feature = "persistence")]
return Some(Box::new(
super::file_storage::FileStorage::from_ron_filepath(file),
));
#[cfg(not(feature = "persistence"))]
None
}

// ----------------------------------------------------------------------------

/// Everything needed to make a winit-based integration for [`epi`].
Expand Down
2 changes: 1 addition & 1 deletion crates/eframe/src/native/file_storage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ impl Drop for FileStorage {

impl FileStorage {
/// Store the state in this .ron file.
fn from_ron_filepath(ron_filepath: impl Into<PathBuf>) -> Self {
pub(crate) fn from_ron_filepath(ron_filepath: impl Into<PathBuf>) -> Self {
crate::profile_function!();
let ron_filepath: PathBuf = ron_filepath.into();
log::debug!("Loading app state from {:?}…", ron_filepath);
Expand Down
18 changes: 11 additions & 7 deletions crates/eframe/src/native/glow_integration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -195,13 +195,17 @@ impl GlowWinitApp {
) -> Result<&mut GlowWinitRunning> {
crate::profile_function!();

let storage = epi_integration::create_storage(
self.native_options
.viewport
.app_id
.as_ref()
.unwrap_or(&self.app_name),
);
let storage = if let Some(file) = &self.native_options.persistence_path {
epi_integration::create_storage_with_file(file)
} else {
epi_integration::create_storage(
self.native_options
.viewport
.app_id
.as_ref()
.unwrap_or(&self.app_name),
)
};

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

Expand Down
18 changes: 11 additions & 7 deletions crates/eframe/src/native/wgpu_integration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -420,13 +420,17 @@ impl WinitApp for WgpuWinitApp {
self.recreate_window(event_loop, running);
running
} else {
let storage = epi_integration::create_storage(
self.native_options
.viewport
.app_id
.as_ref()
.unwrap_or(&self.app_name),
);
let storage = if let Some(file) = &self.native_options.persistence_path {
epi_integration::create_storage_with_file(file)
} else {
epi_integration::create_storage(
self.native_options
.viewport
.app_id
.as_ref()
.unwrap_or(&self.app_name),
)
};
let egui_ctx = winit_integration::create_egui_context(storage.as_deref());
let (window, builder) = create_window(
&egui_ctx,
Expand Down
Loading