Merge pull request #406 from hecrj/feature/background-color

Add `background_color` to `Application` and `Sandbox`
This commit is contained in:
Héctor Ramón 2020-06-14 18:10:14 +02:00 committed by GitHub
commit 50c37ff3d7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
11 changed files with 81 additions and 17 deletions

View File

@ -1,4 +1,4 @@
use crate::{Backend, Renderer, Settings, Viewport}; use crate::{Backend, Color, Renderer, Settings, Viewport};
use core::ffi::c_void; use core::ffi::c_void;
use glow::HasContext; use glow::HasContext;
@ -21,8 +21,6 @@ impl iced_graphics::window::GLCompositor for Compositor {
) -> (Self, Self::Renderer) { ) -> (Self, Self::Renderer) {
let gl = glow::Context::from_loader_function(loader_function); let gl = glow::Context::from_loader_function(loader_function);
gl.clear_color(1.0, 1.0, 1.0, 1.0);
// Enable auto-conversion from/to sRGB // Enable auto-conversion from/to sRGB
gl.enable(glow::FRAMEBUFFER_SRGB); gl.enable(glow::FRAMEBUFFER_SRGB);
@ -60,12 +58,16 @@ impl iced_graphics::window::GLCompositor for Compositor {
&mut self, &mut self,
renderer: &mut Self::Renderer, renderer: &mut Self::Renderer,
viewport: &Viewport, viewport: &Viewport,
color: Color,
output: &<Self::Renderer as iced_native::Renderer>::Output, output: &<Self::Renderer as iced_native::Renderer>::Output,
overlay: &[T], overlay: &[T],
) -> mouse::Interaction { ) -> mouse::Interaction {
let gl = &self.gl; let gl = &self.gl;
let [r, g, b, a] = color.into_linear();
unsafe { unsafe {
gl.clear_color(r, g, b, a);
gl.clear(glow::COLOR_BUFFER_BIT); gl.clear(glow::COLOR_BUFFER_BIT);
} }

View File

@ -47,6 +47,7 @@ pub fn run<A, E, C>(
let mut title = application.title(); let mut title = application.title();
let mut mode = application.mode(); let mut mode = application.mode();
let mut background_color = application.background_color();
let context = { let context = {
let builder = settings.window.into_builder( let builder = settings.window.into_builder(
@ -138,6 +139,9 @@ pub fn run<A, E, C>(
mode = new_mode; mode = new_mode;
} }
// Update background color
background_color = program.background_color();
} }
context.window().request_redraw(); context.window().request_redraw();
@ -164,6 +168,7 @@ pub fn run<A, E, C>(
let new_mouse_interaction = compositor.draw( let new_mouse_interaction = compositor.draw(
&mut renderer, &mut renderer,
&viewport, &viewport,
background_color,
state.primitive(), state.primitive(),
&debug.overlay(), &debug.overlay(),
); );

View File

@ -35,6 +35,6 @@ pub use transformation::Transformation;
pub use viewport::Viewport; pub use viewport::Viewport;
pub use iced_native::{ pub use iced_native::{
Background, Font, HorizontalAlignment, Point, Rectangle, Size, Vector, Background, Color, Font, HorizontalAlignment, Point, Rectangle, Size,
VerticalAlignment, Vector, VerticalAlignment,
}; };

View File

@ -1,4 +1,4 @@
use crate::Viewport; use crate::{Color, Viewport};
use iced_native::mouse; use iced_native::mouse;
use raw_window_handle::HasRawWindowHandle; use raw_window_handle::HasRawWindowHandle;
@ -49,6 +49,7 @@ pub trait Compositor: Sized {
renderer: &mut Self::Renderer, renderer: &mut Self::Renderer,
swap_chain: &mut Self::SwapChain, swap_chain: &mut Self::SwapChain,
viewport: &Viewport, viewport: &Viewport,
background_color: Color,
output: &<Self::Renderer as iced_native::Renderer>::Output, output: &<Self::Renderer as iced_native::Renderer>::Output,
overlay: &[T], overlay: &[T],
) -> mouse::Interaction; ) -> mouse::Interaction;

View File

@ -1,4 +1,4 @@
use crate::{Size, Viewport}; use crate::{Color, Size, Viewport};
use iced_native::mouse; use iced_native::mouse;
use core::ffi::c_void; use core::ffi::c_void;
@ -61,6 +61,7 @@ pub trait GLCompositor: Sized {
&mut self, &mut self,
renderer: &mut Self::Renderer, renderer: &mut Self::Renderer,
viewport: &Viewport, viewport: &Viewport,
background_color: Color,
output: &<Self::Renderer as iced_native::Renderer>::Output, output: &<Self::Renderer as iced_native::Renderer>::Output,
overlay: &[T], overlay: &[T],
) -> mouse::Interaction; ) -> mouse::Interaction;

View File

@ -1,4 +1,6 @@
use crate::{window, Command, Element, Executor, Settings, Subscription}; use crate::{
window, Color, Command, Element, Executor, Settings, Subscription,
};
/// An interactive cross-platform application. /// An interactive cross-platform application.
/// ///
@ -174,6 +176,16 @@ pub trait Application: Sized {
window::Mode::Windowed window::Mode::Windowed
} }
/// Returns the background color of the [`Application`].
///
/// By default, it returns [`Color::WHITE`].
///
/// [`Application`]: trait.Application.html
/// [`Color::WHITE`]: struct.Color.html#const.WHITE
fn background_color(&self) -> Color {
Color::WHITE
}
/// Runs the [`Application`]. /// Runs the [`Application`].
/// ///
/// On native platforms, this method will take control of the current thread /// On native platforms, this method will take control of the current thread
@ -256,6 +268,10 @@ where
fn subscription(&self) -> Subscription<Self::Message> { fn subscription(&self) -> Subscription<Self::Message> {
self.0.subscription() self.0.subscription()
} }
fn background_color(&self) -> Color {
self.0.background_color()
}
} }
#[cfg(target_arch = "wasm32")] #[cfg(target_arch = "wasm32")]

View File

@ -1,4 +1,6 @@
use crate::{executor, Application, Command, Element, Settings, Subscription}; use crate::{
executor, Application, Color, Command, Element, Settings, Subscription,
};
/// A sandboxed [`Application`]. /// A sandboxed [`Application`].
/// ///
@ -124,6 +126,16 @@ pub trait Sandbox {
/// [`Sandbox`]: trait.Sandbox.html /// [`Sandbox`]: trait.Sandbox.html
fn view(&mut self) -> Element<'_, Self::Message>; fn view(&mut self) -> Element<'_, Self::Message>;
/// Returns the background color of the [`Sandbox`].
///
/// By default, it returns [`Color::WHITE`].
///
/// [`Application`]: trait.Application.html
/// [`Color::WHITE`]: struct.Color.html#const.WHITE
fn background_color(&self) -> Color {
Color::WHITE
}
/// Runs the [`Sandbox`]. /// Runs the [`Sandbox`].
/// ///
/// On native platforms, this method will take control of the current thread /// On native platforms, this method will take control of the current thread
@ -169,4 +181,8 @@ where
fn view(&mut self) -> Element<'_, T::Message> { fn view(&mut self) -> Element<'_, T::Message> {
T::view(self) T::view(self)
} }
fn background_color(&self) -> Color {
T::background_color(self)
}
} }

View File

@ -2,6 +2,8 @@
//! //!
//! It contains a set of styles and stylesheets for most of the built-in //! It contains a set of styles and stylesheets for most of the built-in
//! widgets. //! widgets.
pub use iced_core::{Background, Color};
pub mod button; pub mod button;
pub mod checkbox; pub mod checkbox;
pub mod container; pub mod container;

View File

@ -36,7 +36,7 @@ mod backend;
mod quad; mod quad;
mod text; mod text;
pub use iced_graphics::{Antialiasing, Defaults, Primitive, Viewport}; pub use iced_graphics::{Antialiasing, Color, Defaults, Primitive, Viewport};
pub use wgpu; pub use wgpu;
pub use backend::Backend; pub use backend::Backend;

View File

@ -1,4 +1,4 @@
use crate::{Backend, Renderer, Settings}; use crate::{Backend, Color, Renderer, Settings};
use iced_graphics::Viewport; use iced_graphics::Viewport;
use iced_native::{futures, mouse}; use iced_native::{futures, mouse};
@ -103,6 +103,7 @@ impl iced_graphics::window::Compositor for Compositor {
renderer: &mut Self::Renderer, renderer: &mut Self::Renderer,
swap_chain: &mut Self::SwapChain, swap_chain: &mut Self::SwapChain,
viewport: &Viewport, viewport: &Viewport,
background_color: Color,
output: &<Self::Renderer as iced_native::Renderer>::Output, output: &<Self::Renderer as iced_native::Renderer>::Output,
overlay: &[T], overlay: &[T],
) -> mouse::Interaction { ) -> mouse::Interaction {
@ -118,11 +119,15 @@ impl iced_graphics::window::Compositor for Compositor {
resolve_target: None, resolve_target: None,
load_op: wgpu::LoadOp::Clear, load_op: wgpu::LoadOp::Clear,
store_op: wgpu::StoreOp::Store, store_op: wgpu::StoreOp::Store,
clear_color: wgpu::Color { clear_color: {
r: 1.0, let [r, g, b, a] = background_color.into_linear();
g: 1.0,
b: 1.0, wgpu::Color {
a: 1.0, r: f64::from(r),
g: f64::from(g),
b: f64::from(b),
a: f64::from(a),
}
}, },
}], }],
depth_stencil_attachment: None, depth_stencil_attachment: None,

View File

@ -1,6 +1,6 @@
//! Create interactive, native cross-platform applications. //! Create interactive, native cross-platform applications.
use crate::{ use crate::{
conversion, mouse, Clipboard, Command, Debug, Executor, Mode, Proxy, conversion, mouse, Clipboard, Color, Command, Debug, Executor, Mode, Proxy,
Runtime, Settings, Size, Subscription, Runtime, Settings, Size, Subscription,
}; };
use iced_graphics::window; use iced_graphics::window;
@ -73,6 +73,17 @@ pub trait Application: Program {
fn mode(&self) -> Mode { fn mode(&self) -> Mode {
Mode::Windowed Mode::Windowed
} }
/// Returns the background [`Color`] of the [`Application`].
///
/// By default, it returns [`Color::WHITE`].
///
/// [`Color`]: struct.Color.html
/// [`Application`]: trait.Application.html
/// [`Color::WHITE`]: struct.Color.html#const.WHITE
fn background_color(&self) -> Color {
Color::WHITE
}
} }
/// Runs an [`Application`] with an executor, compositor, and the provided /// Runs an [`Application`] with an executor, compositor, and the provided
@ -112,6 +123,7 @@ pub fn run<A, E, C>(
let mut title = application.title(); let mut title = application.title();
let mut mode = application.mode(); let mut mode = application.mode();
let mut background_color = application.background_color();
let window = settings let window = settings
.window .window
@ -193,6 +205,9 @@ pub fn run<A, E, C>(
mode = new_mode; mode = new_mode;
} }
// Update background color
background_color = program.background_color();
} }
window.request_redraw(); window.request_redraw();
@ -219,6 +234,7 @@ pub fn run<A, E, C>(
&mut renderer, &mut renderer,
&mut swap_chain, &mut swap_chain,
&viewport, &viewport,
background_color,
state.primitive(), state.primitive(),
&debug.overlay(), &debug.overlay(),
); );