Introduce ViewContext::on_window_should_close

This is a new callback that can be used to interrupt closing the window
when the user has unsaved changes.
This commit is contained in:
Antonio Scandurra 2022-06-23 11:43:19 +02:00
parent ca8ddcdeec
commit 06033d7fa9
4 changed files with 78 additions and 3 deletions

View file

@ -37,6 +37,7 @@ pub struct Window {
event_handlers: Vec<Box<dyn FnMut(super::Event) -> bool>>,
resize_handlers: Vec<Box<dyn FnMut()>>,
close_handlers: Vec<Box<dyn FnOnce()>>,
should_close_handler: Option<Box<dyn FnMut() -> bool>>,
pub(crate) title: Option<String>,
pub(crate) edited: bool,
pub(crate) pending_prompts: RefCell<VecDeque<oneshot::Sender<usize>>>,
@ -186,9 +187,10 @@ impl Window {
fn new(size: Vector2F) -> Self {
Self {
size,
event_handlers: Vec::new(),
resize_handlers: Vec::new(),
close_handlers: Vec::new(),
event_handlers: Default::default(),
resize_handlers: Default::default(),
close_handlers: Default::default(),
should_close_handler: Default::default(),
scale_factor: 1.0,
current_scene: None,
title: None,
@ -264,6 +266,10 @@ impl super::Window for Window {
fn set_edited(&mut self, edited: bool) {
self.edited = edited;
}
fn on_should_close(&mut self, callback: Box<dyn FnMut() -> bool>) {
self.should_close_handler = Some(callback);
}
}
pub fn platform() -> Platform {