From a65d6a11cb82719a649110a8deaeb8f87e1c374b Mon Sep 17 00:00:00 2001 From: Greg V Date: Sun, 31 May 2020 16:53:57 +0300 Subject: [PATCH] Add custom window background/clear color (incl. transparency) support, fixes #272 wgpu would currently ignore the alpha: https://github.com/gfx-rs/wgpu/issues/687 glow (and naively patched wgpu) requires premultiplied alpha, so if you don't multiply the RGB by the A right now, the semi-transparent color would be wrong (too bright). winit with_transparent doesn't seem necessary. --- glow/src/settings.rs | 7 ++++++- glow/src/window/compositor.rs | 7 ++++++- src/application.rs | 1 + src/settings.rs | 28 ++++++++++++++++++++++++++-- wgpu/src/settings.rs | 7 ++++++- wgpu/src/window/compositor.rs | 8 ++++---- 6 files changed, 49 insertions(+), 9 deletions(-) diff --git a/glow/src/settings.rs b/glow/src/settings.rs index dce30029f2..68fde714fd 100644 --- a/glow/src/settings.rs +++ b/glow/src/settings.rs @@ -1,10 +1,11 @@ //! Configure a renderer. pub use iced_graphics::Antialiasing; +pub use iced_native::Color; /// The settings of a [`Renderer`]. /// /// [`Renderer`]: ../struct.Renderer.html -#[derive(Debug, Clone, Copy, PartialEq, Eq)] +#[derive(Debug, Clone, Copy, PartialEq)] pub struct Settings { /// The bytes of the font that will be used by default. /// @@ -13,6 +14,9 @@ pub struct Settings { /// The antialiasing strategy that will be used for triangle primitives. pub antialiasing: Option, + + /// The color that will be used to clear the window surface. + pub background_color: Color, } impl Default for Settings { @@ -20,6 +24,7 @@ impl Default for Settings { Settings { default_font: None, antialiasing: None, + background_color: Color::WHITE, } } } diff --git a/glow/src/window/compositor.rs b/glow/src/window/compositor.rs index 2f504ff75c..c93191b043 100644 --- a/glow/src/window/compositor.rs +++ b/glow/src/window/compositor.rs @@ -21,7 +21,12 @@ impl iced_graphics::window::GLCompositor for Compositor { ) -> (Self, Self::Renderer) { let gl = glow::Context::from_loader_function(loader_function); - gl.clear_color(1.0, 1.0, 1.0, 1.0); + gl.clear_color( + settings.background_color.r, + settings.background_color.g, + settings.background_color.b, + settings.background_color.a, + ); // Enable auto-conversion from/to sRGB gl.enable(glow::FRAMEBUFFER_SRGB); diff --git a/src/application.rs b/src/application.rs index 19cab7da3d..a529580e1b 100644 --- a/src/application.rs +++ b/src/application.rs @@ -195,6 +195,7 @@ pub trait Application: Sized { } else { None }, + background_color: settings.background_color, ..crate::renderer::Settings::default() }; diff --git a/src/settings.rs b/src/settings.rs index 01ad0ee044..1aa17571fd 100644 --- a/src/settings.rs +++ b/src/settings.rs @@ -1,8 +1,8 @@ //! Configure your application. -use crate::window; +use crate::{window, Color}; /// The settings of an application. -#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)] +#[derive(Debug, Clone, Copy, PartialEq)] pub struct Settings { /// The window settings. /// @@ -32,6 +32,29 @@ pub struct Settings { /// /// [`Canvas`]: ../widget/canvas/struct.Canvas.html pub antialiasing: bool, + + /// The background color of the window. + /// + /// On supported backends, this makes it possible to have + /// (semi-)transparent windows. + /// + /// By default, it is white. + pub background_color: Color, +} + +impl Default for Settings +where + Flags: Default, +{ + fn default() -> Self { + Self { + flags: Default::default(), + antialiasing: Default::default(), + default_font: Default::default(), + window: Default::default(), + background_color: Color::WHITE, + } + } } impl Settings { @@ -46,6 +69,7 @@ impl Settings { antialiasing: Default::default(), default_font: Default::default(), window: Default::default(), + background_color: Color::WHITE, } } } diff --git a/wgpu/src/settings.rs b/wgpu/src/settings.rs index 4655e64f64..16294ba3e4 100644 --- a/wgpu/src/settings.rs +++ b/wgpu/src/settings.rs @@ -1,10 +1,11 @@ //! Configure a renderer. pub use crate::Antialiasing; +pub use iced_native::Color; /// The settings of a [`Renderer`]. /// /// [`Renderer`]: ../struct.Renderer.html -#[derive(Debug, Clone, Copy, PartialEq, Eq)] +#[derive(Debug, Clone, Copy, PartialEq)] pub struct Settings { /// The output format of the [`Renderer`]. /// @@ -18,6 +19,9 @@ pub struct Settings { /// The antialiasing strategy that will be used for triangle primitives. pub antialiasing: Option, + + /// The color that will be used to clear the window surface. + pub background_color: Color, } impl Default for Settings { @@ -26,6 +30,7 @@ impl Default for Settings { format: wgpu::TextureFormat::Bgra8UnormSrgb, default_font: None, antialiasing: None, + background_color: Color::WHITE, } } } diff --git a/wgpu/src/window/compositor.rs b/wgpu/src/window/compositor.rs index 8345679a64..f85986f00d 100644 --- a/wgpu/src/window/compositor.rs +++ b/wgpu/src/window/compositor.rs @@ -119,10 +119,10 @@ impl iced_graphics::window::Compositor for Compositor { load_op: wgpu::LoadOp::Clear, store_op: wgpu::StoreOp::Store, clear_color: wgpu::Color { - r: 1.0, - g: 1.0, - b: 1.0, - a: 1.0, + r: self.settings.background_color.r.into(), + g: self.settings.background_color.g.into(), + b: self.settings.background_color.b.into(), + a: self.settings.background_color.a.into(), }, }], depth_stencil_attachment: None,