From 229bbd491364583198fd637e9fbd837692acf004 Mon Sep 17 00:00:00 2001 From: Jonathan Johnson Date: Sat, 14 Sep 2024 10:33:42 -0700 Subject: [PATCH] Window::on_init --- CHANGELOG.md | 2 ++ Cargo.lock | 15 ++++++++------- src/window.rs | 42 ++++++++++++++++++++++++++++++++++++++++-- 3 files changed, 50 insertions(+), 9 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index e57e9de7a..0f0d37cfb 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -150,6 +150,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 closing automatically when the final window is closed. - `WindowBehavior::initialized` is invoked once the window has been fully initialized. +- `Window::on_init` is a new callback that is invoked before the winit window is + initialized. [139]: https://github.com/khonsulabs/cushy/issues/139 diff --git a/Cargo.lock b/Cargo.lock index 42a6f5a5d..840a413df 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -123,8 +123,9 @@ checksum = "4e1496f8fb1fbf272686b8d37f523dab3e4a7443300055e74cdaa449f3114356" [[package]] name = "appit" -version = "0.3.2" -source = "git+https://github.com/khonsulabs/appit#57acfd2e15523a160fbfd0db4c91d2407ed8d4c5" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "75ed795976c3816aeceb52f1971d3e27f5403d58b16531edea7fe97f63e75f08" dependencies = [ "winit", ] @@ -175,9 +176,9 @@ dependencies = [ [[package]] name = "arrayref" -version = "0.3.8" +version = "0.3.9" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9d151e35f61089500b617991b791fc8bfd237ae50cd5950803758a179b41e67a" +checksum = "76a2e8124351fda1ef8aaaa3bbd7ebbcb486bbcd4225aca0aa0d84bb2db8fecb" [[package]] name = "arrayvec" @@ -1311,7 +1312,7 @@ checksum = "e2db585e1d738fc771bf08a151420d3ed193d9d895a36df7f6f8a9456b911ddc" [[package]] name = "kludgine" version = "0.10.0" -source = "git+https://github.com/khonsulabs/kludgine#236c9773a323ee63dc38bdfa78476ff336547640" +source = "git+https://github.com/khonsulabs/kludgine#aa0d2fe77a92a034f7481a239e4f223a651a8c2a" dependencies = [ "ahash", "alot", @@ -1963,9 +1964,9 @@ dependencies = [ [[package]] name = "once_cell" -version = "1.19.0" +version = "1.20.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3fdb12b2476b595f9358c5161aa467c2438859caa136dec86c26fdd2efe17b92" +checksum = "33ea5043e58958ee56f3e15a90aee535795cd7dfd319846288d93c5b57d85cbe" [[package]] name = "orbclient" diff --git a/src/window.rs b/src/window.rs index edbeba935..9093c3dea 100644 --- a/src/window.rs +++ b/src/window.rs @@ -39,7 +39,7 @@ use kludgine::shapes::Shape; use kludgine::wgpu::{self, CompositeAlphaMode, COPY_BYTES_PER_ROW_ALIGNMENT}; use kludgine::{Color, DrawableExt, Kludgine, KludgineId, Origin, Texture}; use parking_lot::{Mutex, MutexGuard}; -use sealed::{Ize, WindowExecute}; +use sealed::{Ize, PreShowCallback, WindowExecute}; use tracing::Level; use unicode_segmentation::UnicodeSegmentation; @@ -525,6 +525,7 @@ where pending: PendingWindow, attributes: WindowAttributes, on_closed: Option, + on_init: Option, on_open: Option>, inner_size: Option>>, zoom: Option>, @@ -647,6 +648,7 @@ where enabled_buttons: None, fullscreen: None, shortcuts: Value::default(), + on_init: None, } } @@ -970,6 +972,15 @@ where self } + /// Invokes `on_init` before the window initialization begins. + pub fn on_init(mut self, on_init: Function) -> Self + where + Function: FnOnce(&winit::window::Window) + Send + 'static, + { + self.on_init = Some(PreShowCallback(Box::new(Some(on_init)))); + self + } + /// Invokes `on_close` when this window is closed. pub fn on_close(mut self, on_close: Function) -> Self where @@ -1094,6 +1105,7 @@ where title: this.title, redraw_status: this.pending.0.redraw_status.clone(), on_open: this.on_open, + on_init: this.on_init, on_closed: this.on_closed, transparent: this.attributes.transparent, attributes: Some(this.attributes), @@ -2390,13 +2402,20 @@ where { type Context = sealed::Context; + fn pre_initialize(context: &Self::Context, winit: &winit::window::Window) { + let Some(mut on_init) = context.settings.borrow_mut().on_init.take() else { + return; + }; + on_init.0.pre_show(winit); + } + fn initialize( window: kludgine::app::Window<'_, WindowCommand>, graphics: &mut kludgine::Graphics<'_>, context: Self::Context, ) -> Self { context.pending.opened(window.handle()); - let settings = context.settings.borrow_mut(); + let settings = context.settings.borrow(); let cushy = settings.cushy.clone(); let _guard = cushy.enter_runtime(); let mut window = RunningWindow::new( @@ -2833,6 +2852,7 @@ pub(crate) mod sealed { use figures::units::{Px, UPx}; use figures::{Fraction, Point, Size}; use image::{DynamicImage, RgbaImage}; + use kludgine::app::winit; use kludgine::app::winit::event::Modifiers; use kludgine::app::winit::window::{Fullscreen, UserAttentionType, WindowButtons, WindowLevel}; use kludgine::Color; @@ -2873,6 +2893,7 @@ pub(crate) mod sealed { pub cursive_font_family: FontFamilyList, pub font_data_to_load: FontCollection, pub on_open: Option>, + pub on_init: Option, pub on_closed: Option, pub vsync: bool, pub multisample_count: NonZeroU32, @@ -2960,6 +2981,22 @@ pub(crate) mod sealed { fn load_image(data: &[u8], size: Size) -> DynamicImage; fn pixel_color(location: Point, data: &[u8], size: Size) -> Color; } + + pub struct PreShowCallback(pub Box); + + pub trait PreShowFn: Send + 'static { + fn pre_show(&mut self, winit: &winit::window::Window); + } + + impl PreShowFn for Option + where + F: FnOnce(&winit::window::Window) + Send + 'static, + { + fn pre_show(&mut self, winit: &winit::window::Window) { + let Some(this) = self.take() else { return }; + this(winit); + } + } } /// Controls whether the light or dark theme is applied. @@ -3561,6 +3598,7 @@ impl StandaloneWindowBuilder { enabled_buttons: Value::dynamic(WindowButtons::all()), fullscreen: Value::default(), shortcuts: Value::default(), + on_init: None, }, );