Added WindowHandle

Closes #109
This commit is contained in:
Jonathan Johnson 2023-12-27 08:10:44 -08:00
parent 492da3b6ed
commit 884febecfa
No known key found for this signature in database
GPG key ID: A66D6A34D6620579
4 changed files with 41 additions and 9 deletions

6
Cargo.lock generated
View file

@ -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",
]

View file

@ -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<App>(self, app: &App) -> crate::Result
fn open<App>(self, app: &App) -> crate::Result<Option<WindowHandle>>
where
App: Application;

View file

@ -467,7 +467,7 @@ impl<T> Open for T
where
T: MakeWidget,
{
fn open<App>(self, app: &App) -> crate::Result
fn open<App>(self, app: &App) -> crate::Result<Option<crate::window::WindowHandle>>
where
App: Application,
{

View file

@ -327,12 +327,12 @@ impl<Behavior> Open for Window<Behavior>
where
Behavior: WindowBehavior,
{
fn open<App>(self, app: &App) -> crate::Result
fn open<App>(self, app: &App) -> crate::Result<Option<WindowHandle>>
where
App: Application,
{
let gooey = app.gooey().clone();
let _handle = GooeyWindow::<Behavior>::open_with(
let handle = GooeyWindow::<Behavior>::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<FamilyOwned> {
.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<sealed::WindowCommand>);
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<kludgine::app::WindowHandle<sealed::WindowCommand>> for WindowHandle {
fn from(handle: kludgine::app::WindowHandle<sealed::WindowCommand>) -> Self {
WindowHandle(handle)
}
}