From 244be0cf064ed0ec67f7ad03b7ef9c7805ebd0f4 Mon Sep 17 00:00:00 2001 From: Roland Fredenhagen Date: Sat, 11 Nov 2023 00:31:03 +0100 Subject: [PATCH] Support transparent windows --- Cargo.lock | 2 +- src/window.rs | 33 ++++++++++++++++++++++++++++----- 2 files changed, 29 insertions(+), 6 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index d49972b40..0c43f536a 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -880,7 +880,7 @@ checksum = "e2db585e1d738fc771bf08a151420d3ed193d9d895a36df7f6f8a9456b911ddc" [[package]] name = "kludgine" version = "0.1.0" -source = "git+https://github.com/khonsulabs/kludgine#a88961b726101ef9bb46bdae4737308d2dcb12a0" +source = "git+https://github.com/khonsulabs/kludgine#0239b35d765e37fbcc74a4f6dcd319622d221aa9" dependencies = [ "ahash", "alot", diff --git a/src/window.rs b/src/window.rs index 520b97aab..3cbdacab4 100644 --- a/src/window.rs +++ b/src/window.rs @@ -19,6 +19,7 @@ use kludgine::app::WindowBehavior as _; use kludgine::figures::units::{Px, UPx}; use kludgine::figures::{IntoSigned, IntoUnsigned, Point, Rect, ScreenScale, Size}; use kludgine::render::Drawing; +use kludgine::wgpu::CompositeAlphaMode; use kludgine::Kludgine; use tracing::Level; @@ -201,6 +202,7 @@ where GooeyWindow::::run_with(AssertUnwindSafe(sealed::Context { user: self.context, settings: RefCell::new(sealed::WindowSettings { + transparent: self.attributes.transparent, attributes: Some(self.attributes), occluded: self.occluded, focused: self.focused, @@ -258,6 +260,7 @@ struct GooeyWindow { max_inner_size: Option>, theme: Option>, current_theme: ThemePair, + transparent: bool, } impl GooeyWindow @@ -405,6 +408,7 @@ where .theme .take() .expect("theme always present"); + let transparent = context.settings.borrow().transparent; let mut behavior = T::initialize( &mut RunningWindow::new(window, &focused, &occluded), context.user, @@ -435,6 +439,7 @@ where max_inner_size: None, current_theme, theme, + transparent, } } @@ -473,8 +478,11 @@ where let mut layout_context = LayoutContext::new(&mut context); let window_size = layout_context.gfx.size(); - let background_color = layout_context.theme().surface.color; - layout_context.graphics.gfx.fill(background_color); + if !self.transparent { + let background_color = layout_context.theme().surface.color; + layout_context.graphics.gfx.fill(background_color); + } + let actual_size = layout_context.layout(if is_expanded { Size::new( ConstraintLimit::Known(window_size.width), @@ -577,9 +585,23 @@ where // wgpu::Limits::downlevel_webgl2_defaults().using_resolution(adapter_limits) // } - // fn clear_color() -> Option { - // Some(kludgine::Color::BLACK) - // } + fn clear_color(&self) -> Option { + Some(if self.transparent { + kludgine::Color::CLEAR_BLACK + } else { + kludgine::Color::BLACK + }) + } + + fn composite_alpha_mode(&self, supported_modes: &[CompositeAlphaMode]) -> CompositeAlphaMode { + if dbg!(self.transparent) + && dbg!(supported_modes).contains(&CompositeAlphaMode::PreMultiplied) + { + CompositeAlphaMode::PreMultiplied + } else { + CompositeAlphaMode::Auto + } + } // fn focus_changed(&mut self, window: kludgine::app::Window<'_, ()>) {} @@ -967,6 +989,7 @@ pub(crate) mod sealed { pub occluded: Option>, pub focused: Option>, pub theme: Option>, + pub transparent: bool, } pub enum WindowCommand {