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

Ability to manually save state #5243

Open
duckinator opened this issue Oct 10, 2024 · 1 comment
Open

Ability to manually save state #5243

duckinator opened this issue Oct 10, 2024 · 1 comment
Labels
egui feature New feature or request

Comments

@duckinator
Copy link

duckinator commented Oct 10, 2024

Is your feature request related to a problem? Please describe.

If I know a particular action changes something significant enough to warrant saving the state to disk, I want to be able to request a save (similar to how you can use ctx.request_repaint() and related functions if you know a repaint will be needed).

Describe the solution you'd like

The idea that comes to mind is adding a ctx.request_save() or similar, which triggers the same behavior as if the autosave duration was reached.

Describe alternatives you've considered

I tried storing the creation context passed to the eframe::App, but that caused ownership problems.

I tried storing a reference to the eframe::Storage instead, and also ran into ownership problems.

I can't even figure out a messy way to do it with existing functionality. (EDIT: I found a way to implement it. See #5243 (comment).)

@lucasmerlin lucasmerlin added feature New feature or request egui labels Oct 10, 2024
@duckinator
Copy link
Author

duckinator commented Oct 10, 2024

I've found an approach that seems to work, basically by making auto_save_interval return 0 milliseconds if a save was requested, and 30 seconds otherwise. Setting self.needs_save = true means the next time maybe_autosave is called, it should save.

I think it'd make sense for this to potentially be integrated into egui/eframe, though.

#[derive(Default)]
struct MainApp {
    // ...
    tasks: TaskList,
    needs_save: bool,
}

impl eframe::App for MainApp {
    // ...

    fn auto_save_interval(&self) -> std::time::Duration {
        if self.needs_save {
            std::time::Duration::from_secs(0)
        } else {
            std::time::Duration::from_secs(30)
        }
    }

    fn save(&mut self, storage: &mut dyn eframe::Storage) {
        eframe::set_value(storage, eframe::APP_KEY, &self.tasks);
        self.needs_save = false;
    }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
egui feature New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants