Skip to content

Commit

Permalink
add save_as
Browse files Browse the repository at this point in the history
  • Loading branch information
dzhou121 committed Aug 8, 2023
1 parent a9d18d2 commit 28d2026
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 1 deletion.
1 change: 0 additions & 1 deletion examples/counter/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use floem::{
menu::{Menu, MenuItem},
peniko::Color,
reactive::create_signal,
style::Style,
Expand Down
19 changes: 19 additions & 0 deletions src/app_handle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,10 @@ pub enum UpdateMessage {
options: FileDialogOptions,
file_info_action: Box<dyn Fn(Option<FileInfo>)>,
},
SaveAs {
options: FileDialogOptions,
file_info_action: Box<dyn Fn(Option<FileInfo>)>,
},
RequestTimer {
deadline: std::time::Duration,
action: Box<dyn FnOnce()>,
Expand Down Expand Up @@ -545,6 +549,15 @@ impl<V: View> AppHandle<V> {
self.file_dialogs.insert(token, file_info_action);
}
}
UpdateMessage::SaveAs {
options,
file_info_action,
} => {
let token = self.handle.save_as(options);
if let Some(token) = token {
self.file_dialogs.insert(token, file_info_action);
}
}
UpdateMessage::RequestTimer { deadline, action } => {
cx.app_state.request_timer(deadline, action);
}
Expand Down Expand Up @@ -900,6 +913,12 @@ impl<V: View> WinHandler for AppHandle<V> {
}
}

fn save_as(&mut self, token: FileDialogToken, file: Option<FileInfo>) {
if let Some(action) = self.file_dialogs.remove(&token) {
action(file);
}
}

fn timer(&mut self, token: TimerToken) {
if let Some(action) = self.app_state.timers.remove(&token) {
action();
Expand Down
17 changes: 17 additions & 0 deletions src/id.rs
Original file line number Diff line number Diff line change
Expand Up @@ -347,6 +347,23 @@ impl Id {
}
}

pub fn save_as(
&self,
options: FileDialogOptions,
file_info_action: impl Fn(Option<FileInfo>) + 'static,
) {
if let Some(root) = self.root_id() {
UPDATE_MESSAGES.with(|msgs| {
let mut msgs = msgs.borrow_mut();
let msgs = msgs.entry(root).or_default();
msgs.push(UpdateMessage::SaveAs {
options,
file_info_action: Box::new(file_info_action),
})
});
}
}

pub fn update_context_menu(&self, menu: Box<MenuCallback>) {
if let Some(root) = self.root_id() {
UPDATE_MESSAGES.with(|msgs| {
Expand Down

0 comments on commit 28d2026

Please sign in to comment.