Add Renderer::Defaults and style inheritance

This commit is contained in:
Héctor Ramón Jiménez 2019-12-30 12:14:26 +01:00
parent 89a6b8a9a1
commit 8caa66be27
32 changed files with 224 additions and 150 deletions

27
wgpu/src/defaults.rs Normal file
View file

@ -0,0 +1,27 @@
use iced_native::Color;
#[derive(Debug, Clone, Copy)]
pub struct Defaults {
pub text: Text,
}
impl Default for Defaults {
fn default() -> Defaults {
Defaults {
text: Text::default(),
}
}
}
#[derive(Debug, Clone, Copy)]
pub struct Text {
pub color: Color,
}
impl Default for Text {
fn default() -> Text {
Text {
color: Color::BLACK,
}
}
}

View file

@ -24,6 +24,7 @@
#![deny(unused_results)]
#![deny(unsafe_code)]
#![deny(rust_2018_idioms)]
pub mod defaults;
pub mod widget;
mod image;
@ -33,6 +34,7 @@ mod renderer;
mod text;
mod transformation;
pub use defaults::Defaults;
pub use primitive::Primitive;
pub use renderer::{Renderer, Target};
#[doc(no_inline)]

View file

@ -1,4 +1,6 @@
use crate::{image, quad, text, Image, Primitive, Quad, Transformation};
use crate::{
image, quad, text, Defaults, Image, Primitive, Quad, Transformation,
};
use iced_native::{
renderer::{Debugger, Windowed},
Background, Color, Layout, MouseCursor, Point, Rectangle, Vector, Widget,
@ -411,6 +413,7 @@ impl Renderer {
impl iced_native::Renderer for Renderer {
type Output = (Primitive, MouseCursor);
type Defaults = Defaults;
fn layout<'a, Message>(
&mut self,
@ -445,13 +448,15 @@ impl Windowed for Renderer {
impl Debugger for Renderer {
fn explain<Message>(
&mut self,
defaults: &Defaults,
widget: &dyn Widget<Message, Self>,
layout: Layout<'_>,
cursor_position: Point,
color: Color,
) -> Self::Output {
let mut primitives = Vec::new();
let (primitive, cursor) = widget.draw(self, layout, cursor_position);
let (primitive, cursor) =
widget.draw(self, defaults, layout, cursor_position);
explain_layout(layout, color, &mut primitives);
primitives.push(primitive);

View file

@ -1,21 +1,26 @@
use crate::{button::StyleSheet, Primitive, Renderer};
use iced_native::{Background, MouseCursor, Point, Rectangle};
use crate::{button::StyleSheet, defaults, Defaults, Primitive, Renderer};
use iced_native::{Background, Element, Layout, MouseCursor, Point, Rectangle};
impl iced_native::button::Renderer for Renderer {
type Style = Box<dyn StyleSheet>;
fn draw(
fn draw<Message>(
&mut self,
defaults: &Defaults,
bounds: Rectangle,
cursor_position: Point,
is_disabled: bool,
is_pressed: bool,
style: &Box<dyn StyleSheet>,
(content, _): Self::Output,
content: &Element<'_, Message, Self>,
content_layout: Layout<'_>,
) -> Self::Output {
let is_mouse_over = bounds.contains(cursor_position);
// TODO: Render proper shadows
let styling = if is_mouse_over {
let styling = if is_disabled {
style.disabled()
} else if is_mouse_over {
if is_pressed {
style.pressed()
} else {
@ -25,6 +30,18 @@ impl iced_native::button::Renderer for Renderer {
style.active()
};
let (content, _) = content.draw(
self,
&Defaults {
text: defaults::Text {
color: styling.text_color,
},
..*defaults
},
content_layout,
cursor_position,
);
(
match styling.background {
None => content,

View file

@ -1 +0,0 @@

View file

@ -4,6 +4,7 @@ use iced_native::{column, Element, Layout, MouseCursor, Point};
impl column::Renderer for Renderer {
fn draw<Message>(
&mut self,
defaults: &Self::Defaults,
content: &[Element<'_, Message, Self>],
layout: Layout<'_>,
cursor_position: Point,
@ -17,7 +18,7 @@ impl column::Renderer for Renderer {
.zip(layout.children())
.map(|(child, layout)| {
let (primitive, new_mouse_cursor) =
child.draw(self, layout, cursor_position);
child.draw(self, defaults, layout, cursor_position);
if new_mouse_cursor > mouse_cursor {
mouse_cursor = new_mouse_cursor;

View file

@ -4,6 +4,7 @@ use iced_native::{row, Element, Layout, MouseCursor, Point};
impl row::Renderer for Renderer {
fn draw<Message>(
&mut self,
defaults: &Self::Defaults,
children: &[Element<'_, Message, Self>],
layout: Layout<'_>,
cursor_position: Point,
@ -17,7 +18,7 @@ impl row::Renderer for Renderer {
.zip(layout.children())
.map(|(child, layout)| {
let (primitive, new_mouse_cursor) =
child.draw(self, layout, cursor_position);
child.draw(self, defaults, layout, cursor_position);
if new_mouse_cursor > mouse_cursor {
mouse_cursor = new_mouse_cursor;

View file

@ -27,6 +27,7 @@ impl text::Renderer for Renderer {
fn draw(
&mut self,
defaults: &Self::Defaults,
bounds: Rectangle,
content: &str,
size: u16,
@ -40,7 +41,7 @@ impl text::Renderer for Renderer {
content: content.to_string(),
size: f32::from(size),
bounds,
color: color.unwrap_or(Color::BLACK),
color: color.unwrap_or(defaults.text.color),
font,
horizontal_alignment,
vertical_alignment,

View file

@ -5,7 +5,7 @@
//! [`Button`]: type.Button.html
//! [`State`]: struct.State.html
use crate::Renderer;
use iced_native::Background;
use iced_native::{Background, Color};
pub use iced_native::button::State;
@ -19,6 +19,7 @@ pub struct Style {
pub shadow_offset: f32,
pub background: Option<Background>,
pub border_radius: u16,
pub text_color: Color,
}
pub trait StyleSheet {
@ -41,7 +42,22 @@ pub trait StyleSheet {
}
fn disabled(&self) -> Style {
self.active()
let active = self.active();
Style {
shadow_offset: 0.0,
background: active.background.map(|background| match background {
Background::Color(color) => Background::Color(Color {
a: color.a * 0.5,
..color
}),
}),
text_color: Color {
a: active.text_color.a * 0.5,
..active.text_color
},
..active
}
}
}
@ -53,30 +69,7 @@ impl StyleSheet for Default {
shadow_offset: 1.0,
background: Some(Background::Color([0.5, 0.5, 0.5].into())),
border_radius: 5,
}
}
fn hovered(&self) -> Style {
Style {
shadow_offset: 2.0,
background: Some(Background::Color([0.5, 0.5, 0.5].into())),
border_radius: 5,
}
}
fn pressed(&self) -> Style {
Style {
shadow_offset: 0.0,
background: Some(Background::Color([0.5, 0.5, 0.5].into())),
border_radius: 5,
}
}
fn disabled(&self) -> Style {
Style {
shadow_offset: 0.0,
background: Some(Background::Color([0.7, 0.7, 0.7].into())),
border_radius: 5,
text_color: Color::BLACK,
}
}
}