Replies: 2 comments 1 reply
-
A way you can do it is by having separate state for the editable fields of your options dialog. You would copy your "real" application state data into these fields before you display the dialog. When the user presses OK then you copy the data the other way. |
Beta Was this translation helpful? Give feedback.
0 replies
-
Here's what I ended up with. Let me know if there's a better way: /// Stores a copy of the underlying data for editing, until the user chooses to apply it, cancel, or close the dialog.
#[derive(Default)]
struct DialogChangeBuffer<T> {
draft_of_changes: Option<T>,
}
/// What should we do with the changes?
#[derive(Clone)]
enum FateOfChanges {
Apply,
Cancel,
KeepDraft,
}
impl<T: Clone> DialogChangeBuffer<T> {
fn reset(&mut self) {
self.draft_of_changes = None;
}
fn buffer_changes(
&mut self,
dialog_open: &mut bool,
underlying_data: &mut T,
draft_changes: impl FnOnce(&mut FateOfChanges, &mut bool, &mut T) -> Option<FateOfChanges>,
) -> Option<FateOfChanges> {
if !*dialog_open {
return None;
}
// If this is the first time rendering the window, get a copy and store it in the buffer
if self.draft_of_changes.is_none() {
self.draft_of_changes = Some(underlying_data.clone());
}
let mut fate_of_changes = FateOfChanges::KeepDraft;
if let Some(fate) = draft_changes(
&mut fate_of_changes,
dialog_open,
self.draft_of_changes.as_mut().unwrap(),
) {
// Track what the user chooses to do: cancel, apply, or keep draft (no action this frame)
fate_of_changes = fate;
}
match fate_of_changes {
FateOfChanges::Apply => {
*underlying_data = self.draft_of_changes.as_ref().unwrap().clone();
*dialog_open = false;
}
FateOfChanges::Cancel => {
*dialog_open = false;
}
FateOfChanges::KeepDraft => {}
}
if !*dialog_open {
self.reset();
}
Some(fate_of_changes)
}
} |
Beta Was this translation helpful? Give feedback.
1 reply
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
It seems to me that editable
egui
widgets generally change their underlying data immediately when you edit them.I'm thinking of an application with an options dialog window where the various fields inside would:
The goal here is basically to allow the user to draft a set of changes slowly and then apply them all at once.
Does
egui
have a convenient interface for accomplishing this?Beta Was this translation helpful? Give feedback.
All reactions