diff --git a/Cargo.lock b/Cargo.lock index 4c989ec..b7f8883 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1178,7 +1178,7 @@ checksum = "e2db585e1d738fc771bf08a151420d3ed193d9d895a36df7f6f8a9456b911ddc" [[package]] name = "kludgine" version = "0.6.1" -source = "git+https://github.com/khonsulabs/kludgine#4da9c9aef372ea6ae22fc18095a098d7189984c2" +source = "git+https://github.com/khonsulabs/kludgine#daf7a2df751c2e77ac226d3ddf5a408b85172cc9" dependencies = [ "ahash", "alot", @@ -3232,9 +3232,9 @@ dependencies = [ [[package]] name = "winnow" -version = "0.5.30" +version = "0.5.31" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9b5c3db89721d50d0e2a673f5043fc4722f76dcc352d7b1ab8b8288bed4ed2c5" +checksum = "97a4882e6b134d6c28953a387571f1acdd3496830d5e36c5e3a1075580ea641c" dependencies = [ "memchr", ] diff --git a/src/app.rs b/src/app.rs index ec3e599..b3a74a9 100644 --- a/src/app.rs +++ b/src/app.rs @@ -5,6 +5,7 @@ use kludgine::app::{AppEvent, AsApplication}; use crate::utils::IgnorePoison; use crate::window::sealed::WindowCommand; +use crate::window::WindowHandle; /// A Gooey application that has not started running yet. pub struct PendingApp { @@ -116,7 +117,7 @@ pub trait Run: Sized { /// A type that can be opened as a window in an application. pub trait Open: Sized { /// Opens the provided type as a window inside of `app`. - fn open(self, app: &App) -> crate::Result + fn open(self, app: &App) -> crate::Result> where App: Application; diff --git a/src/widget.rs b/src/widget.rs index a54c985..205df55 100644 --- a/src/widget.rs +++ b/src/widget.rs @@ -467,7 +467,7 @@ impl Open for T where T: MakeWidget, { - fn open(self, app: &App) -> crate::Result + fn open(self, app: &App) -> crate::Result> where App: Application, { diff --git a/src/window.rs b/src/window.rs index 6903882..ecdfed0 100644 --- a/src/window.rs +++ b/src/window.rs @@ -327,12 +327,12 @@ impl Open for Window where Behavior: WindowBehavior, { - fn open(self, app: &App) -> crate::Result + fn open(self, app: &App) -> crate::Result> where App: Application, { let gooey = app.gooey().clone(); - let _handle = GooeyWindow::::open_with( + let handle = GooeyWindow::::open_with( app, sealed::Context { user: self.context, @@ -356,7 +356,7 @@ where }, )?; - Ok(()) + Ok(handle.map(WindowHandle::from)) } fn run_in(self, app: PendingApp) -> crate::Result { @@ -1318,6 +1318,18 @@ where WindowCommand::Redraw => { window.set_needs_redraw(); } + WindowCommand::RequestClose => { + let mut window = RunningWindow::new( + window, + &self.gooey, + &self.focused, + &self.occluded, + &self.inner_size, + ); + if self.behavior.close_requested(&mut window) { + window.close(); + } + } } } } @@ -1386,7 +1398,7 @@ pub(crate) mod sealed { #[derive(Clone)] pub enum WindowCommand { Redraw, - // RequestClose, + RequestClose, } } @@ -1487,3 +1499,22 @@ fn default_family(query: Family<'_>) -> Option { .and_then(|output| String::from_utf8(output.stdout).ok()) .map(FamilyOwned::Name) } + +/// A handle to an open Gooey window. +pub struct WindowHandle(kludgine::app::WindowHandle); + +impl WindowHandle { + /// Request that the window closes. + /// + /// A window may disallow itself from being closed by customizing + /// [`WindowBehavior::close_requested`]. + pub fn request_close(&self) { + let _result = self.0.send(sealed::WindowCommand::RequestClose); + } +} + +impl From> for WindowHandle { + fn from(handle: kludgine::app::WindowHandle) -> Self { + WindowHandle(handle) + } +}