summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorLibravatar Héctor Ramón Jiménez <hector0193@gmail.com>2019-12-30 12:14:26 +0100
committerLibravatar Héctor Ramón Jiménez <hector0193@gmail.com>2019-12-30 12:14:26 +0100
commit8caa66be2708b1c83e20d905d69902c2567c4692 (patch)
treeee18296fc3d32f24bca90f97fc6845d97f3e4c21
parent89a6b8a9a173e767753ec777fd83c912c1be5ea3 (diff)
downloadiced-8caa66be2708b1c83e20d905d69902c2567c4692.tar.gz
iced-8caa66be2708b1c83e20d905d69902c2567c4692.tar.bz2
iced-8caa66be2708b1c83e20d905d69902c2567c4692.zip
Add `Renderer::Defaults` and style inheritance
-rw-r--r--examples/custom_widget.rs3
-rw-r--r--examples/pokedex.rs7
-rw-r--r--examples/stopwatch.rs6
-rw-r--r--examples/todos.rs86
-rw-r--r--examples/tour.rs13
-rw-r--r--native/src/element.rs10
-rw-r--r--native/src/renderer.rs2
-rw-r--r--native/src/renderer/debugger.rs1
-rw-r--r--native/src/renderer/null.rs19
-rw-r--r--native/src/user_interface.rs58
-rw-r--r--native/src/widget.rs1
-rw-r--r--native/src/widget/button.rs19
-rw-r--r--native/src/widget/checkbox.rs2
-rw-r--r--native/src/widget/column.rs4
-rw-r--r--native/src/widget/container.rs2
-rw-r--r--native/src/widget/image.rs1
-rw-r--r--native/src/widget/radio.rs2
-rw-r--r--native/src/widget/row.rs4
-rw-r--r--native/src/widget/scrollable.rs8
-rw-r--r--native/src/widget/slider.rs1
-rw-r--r--native/src/widget/svg.rs1
-rw-r--r--native/src/widget/text.rs3
-rw-r--r--native/src/widget/text_input.rs1
-rw-r--r--wgpu/src/defaults.rs27
-rw-r--r--wgpu/src/lib.rs2
-rw-r--r--wgpu/src/renderer.rs9
-rw-r--r--wgpu/src/renderer/widget/button.rs27
-rw-r--r--wgpu/src/renderer/widget/button/style.rs1
-rw-r--r--wgpu/src/renderer/widget/column.rs3
-rw-r--r--wgpu/src/renderer/widget/row.rs3
-rw-r--r--wgpu/src/renderer/widget/text.rs3
-rw-r--r--wgpu/src/widget/button.rs45
32 files changed, 224 insertions, 150 deletions
diff --git a/examples/custom_widget.rs b/examples/custom_widget.rs
index cf2f7792..ca562ead 100644
--- a/examples/custom_widget.rs
+++ b/examples/custom_widget.rs
@@ -13,7 +13,7 @@ mod circle {
layout, Background, Color, Element, Hasher, Layout, Length,
MouseCursor, Point, Size, Widget,
};
- use iced_wgpu::{Primitive, Renderer};
+ use iced_wgpu::{Defaults, Primitive, Renderer};
pub struct Circle {
radius: u16,
@@ -54,6 +54,7 @@ mod circle {
fn draw(
&self,
_renderer: &mut Renderer,
+ _defaults: &Defaults,
layout: Layout<'_>,
_cursor_position: Point,
) -> (Primitive, MouseCursor) {
diff --git a/examples/pokedex.rs b/examples/pokedex.rs
index 0dcf6981..35d38251 100644
--- a/examples/pokedex.rs
+++ b/examples/pokedex.rs
@@ -1,6 +1,6 @@
use iced::{
- button, image, Align, Application, Button, Color, Column, Command,
- Container, Element, Image, Length, Row, Settings, Text,
+ button, image, Align, Application, Button, Column, Command, Container,
+ Element, Image, Length, Row, Settings, Text,
};
pub fn main() {
@@ -219,7 +219,7 @@ impl From<surf::Exception> for Error {
}
fn button<'a>(state: &'a mut button::State, text: &str) -> Button<'a, Message> {
- Button::new(state, Text::new(text).color(Color::WHITE))
+ Button::new(state, Text::new(text))
.padding(10)
.style(style::Button::Primary)
}
@@ -239,6 +239,7 @@ mod style {
})),
border_radius: 12,
shadow_offset: 1.0,
+ text_color: Color::WHITE,
}
}
}
diff --git a/examples/stopwatch.rs b/examples/stopwatch.rs
index 0e0cdba5..99746609 100644
--- a/examples/stopwatch.rs
+++ b/examples/stopwatch.rs
@@ -1,6 +1,6 @@
use iced::{
- button, Align, Application, Button, Color, Column, Command, Container,
- Element, HorizontalAlignment, Length, Row, Settings, Subscription, Text,
+ button, Align, Application, Button, Column, Command, Container, Element,
+ HorizontalAlignment, Length, Row, Settings, Subscription, Text,
};
use std::time::{Duration, Instant};
@@ -102,7 +102,6 @@ impl Application for Stopwatch {
Button::new(
state,
Text::new(label)
- .color(Color::WHITE)
.horizontal_alignment(HorizontalAlignment::Center),
)
.min_width(80)
@@ -199,6 +198,7 @@ mod style {
})),
border_radius: 12,
shadow_offset: 1.0,
+ text_color: Color::WHITE,
}
}
}
diff --git a/examples/todos.rs b/examples/todos.rs
index 00edd7fb..ca20183f 100644
--- a/examples/todos.rs
+++ b/examples/todos.rs
@@ -1,7 +1,7 @@
use iced::{
button, scrollable, text_input, Align, Application, Button, Checkbox,
- Color, Column, Command, Container, Element, Font, HorizontalAlignment,
- Length, Row, Scrollable, Settings, Text, TextInput,
+ Column, Command, Container, Element, Font, HorizontalAlignment, Length,
+ Row, Scrollable, Settings, Text, TextInput,
};
use serde::{Deserialize, Serialize};
@@ -291,13 +291,10 @@ impl Task {
.align_items(Align::Center)
.push(checkbox)
.push(
- Button::new(
- edit_button,
- edit_icon().color([0.5, 0.5, 0.5]),
- )
- .on_press(TaskMessage::Edit)
- .padding(10)
- .style(style::Button::NoBackground),
+ Button::new(edit_button, edit_icon())
+ .on_press(TaskMessage::Edit)
+ .padding(10)
+ .style(style::Button::Icon),
)
.into()
}
@@ -321,14 +318,9 @@ impl Task {
.push(
Button::new(
delete_button,
- Row::new()
- .spacing(10)
- .push(delete_icon().color(Color::WHITE))
- .push(
- Text::new("Delete")
- .width(Length::Shrink)
- .color(Color::WHITE),
- ),
+ Row::new().spacing(10).push(delete_icon()).push(
+ Text::new("Delete").width(Length::Shrink),
+ ),
)
.on_press(TaskMessage::Delete)
.padding(10)
@@ -359,12 +351,10 @@ impl Controls {
let filter_button = |state, label, filter, current_filter| {
let label = Text::new(label).size(16).width(Length::Shrink);
- let button = if filter == current_filter {
- Button::new(state, label.color(Color::WHITE))
- .style(style::Button::FilterSelected)
- } else {
- Button::new(state, label).style(style::Button::NoBackground)
- };
+ let button =
+ Button::new(state, label).style(style::Button::Filter {
+ selected: filter == current_filter,
+ });
button.on_press(Message::FilterChanged(filter)).padding(8)
};
@@ -564,25 +554,38 @@ mod style {
use iced::{button, Background, Color};
pub enum Button {
- FilterSelected,
- NoBackground,
+ Filter { selected: bool },
+ Icon,
Destructive,
}
impl button::StyleSheet for Button {
fn active(&self) -> button::Style {
match self {
- Button::FilterSelected => button::Style {
- background: Some(Background::Color(Color::from_rgb(
- 0.2, 0.2, 0.7,
- ))),
- border_radius: 10,
- shadow_offset: 0.0,
- },
- Button::NoBackground => button::Style {
+ Button::Filter { selected } => {
+ if *selected {
+ button::Style {
+ background: Some(Background::Color(
+ Color::from_rgb(0.2, 0.2, 0.7),
+ )),
+ border_radius: 10,
+ shadow_offset: 0.0,
+ text_color: Color::WHITE,
+ }
+ } else {
+ button::Style {
+ background: None,
+ border_radius: 0,
+ shadow_offset: 0.0,
+ text_color: Color::BLACK,
+ }
+ }
+ }
+ Button::Icon => button::Style {
background: None,
border_radius: 0,
shadow_offset: 0.0,
+ text_color: Color::from_rgb(0.5, 0.5, 0.5),
},
Button::Destructive => button::Style {
background: Some(Background::Color(Color::from_rgb(
@@ -590,7 +593,24 @@ mod style {
))),
border_radius: 5,
shadow_offset: 1.0,
+ text_color: Color::WHITE,
+ },
+ }
+ }
+
+ fn hovered(&self) -> button::Style {
+ let active = self.active();
+
+ button::Style {
+ text_color: match self {
+ Button::Icon => Color::from_rgb(0.2, 0.2, 0.7),
+ Button::Filter { selected } if !selected => {
+ Color::from_rgb(0.2, 0.2, 0.7)
+ }
+ _ => active.text_color,
},
+ shadow_offset: active.shadow_offset + 1.0,
+ ..active
}
}
}
diff --git a/examples/tour.rs b/examples/tour.rs
index 402bf1c4..2429ca4f 100644
--- a/examples/tour.rs
+++ b/examples/tour.rs
@@ -694,9 +694,7 @@ fn button<'a, Message>(
) -> Button<'a, Message> {
Button::new(
state,
- Text::new(label)
- .color(Color::WHITE)
- .horizontal_alignment(HorizontalAlignment::Center),
+ Text::new(label).horizontal_alignment(HorizontalAlignment::Center),
)
.padding(12)
.min_width(100)
@@ -761,6 +759,15 @@ mod style {
})),
border_radius: 12,
shadow_offset: 1.0,
+ text_color: Color::from_rgb8(0xEE, 0xEE, 0xEE),
+ }
+ }
+
+ fn hovered(&self) -> button::Style {
+ button::Style {
+ text_color: Color::WHITE,
+ shadow_offset: 2.0,
+ ..self.active()
}
}
}
diff --git a/native/src/element.rs b/native/src/element.rs
index 63d2de0c..9b5adb9c 100644
--- a/native/src/element.rs
+++ b/native/src/element.rs
@@ -235,10 +235,12 @@ where
pub fn draw(
&self,
renderer: &mut Renderer,
+ defaults: &Renderer::Defaults,
layout: Layout<'_>,
cursor_position: Point,
) -> Renderer::Output {
- self.widget.draw(renderer, layout, cursor_position)
+ self.widget
+ .draw(renderer, defaults, layout, cursor_position)
}
pub(crate) fn hash_layout(&self, state: &mut Hasher) {
@@ -316,10 +318,12 @@ where
fn draw(
&self,
renderer: &mut Renderer,
+ defaults: &Renderer::Defaults,
layout: Layout<'_>,
cursor_position: Point,
) -> Renderer::Output {
- self.widget.draw(renderer, layout, cursor_position)
+ self.widget
+ .draw(renderer, defaults, layout, cursor_position)
}
fn hash_layout(&self, state: &mut Hasher) {
@@ -384,10 +388,12 @@ where
fn draw(
&self,
renderer: &mut Renderer,
+ defaults: &Renderer::Defaults,
layout: Layout<'_>,
cursor_position: Point,
) -> Renderer::Output {
renderer.explain(
+ defaults,
self.element.widget.as_ref(),
layout,
cursor_position,
diff --git a/native/src/renderer.rs b/native/src/renderer.rs
index 023dd42b..90cec6c8 100644
--- a/native/src/renderer.rs
+++ b/native/src/renderer.rs
@@ -44,6 +44,8 @@ pub trait Renderer: Sized {
/// [`Renderer`]: trait.Renderer.html
type Output;
+ type Defaults: Default;
+
/// Lays out the elements of a user interface.
///
/// You should override this if you need to perform any operations before or
diff --git a/native/src/renderer/debugger.rs b/native/src/renderer/debugger.rs
index 4cc50661..30f3d9a0 100644
--- a/native/src/renderer/debugger.rs
+++ b/native/src/renderer/debugger.rs
@@ -17,6 +17,7 @@ pub trait Debugger: super::Renderer {
/// [`Element::explain`]: struct.Element.html#method.explain
fn explain<Message>(
&mut self,
+ defaults: &Self::Defaults,
widget: &dyn Widget<Message, Self>,
layout: Layout<'_>,
cursor_position: Point,
diff --git a/native/src/renderer/null.rs b/native/src/renderer/null.rs
index 1be669c2..56d7e472 100644
--- a/native/src/renderer/null.rs
+++ b/native/src/renderer/null.rs
@@ -5,16 +5,26 @@ use crate::{
};
/// A renderer that does nothing.
+///
+/// It can be useful if you are writing tests!
#[derive(Debug, Clone, Copy)]
pub struct Null;
+impl Null {
+ pub fn new() -> Null {
+ Null
+ }
+}
+
impl Renderer for Null {
type Output = ();
+ type Defaults = ();
}
impl column::Renderer for Null {
fn draw<Message>(
&mut self,
+ _defaults: &Self::Defaults,
_content: &[Element<'_, Message, Self>],
_layout: Layout<'_>,
_cursor_position: Point,
@@ -25,6 +35,7 @@ impl column::Renderer for Null {
impl row::Renderer for Null {
fn draw<Message>(
&mut self,
+ _defaults: &Self::Defaults,
_content: &[Element<'_, Message, Self>],
_layout: Layout<'_>,
_cursor_position: Point,
@@ -49,6 +60,7 @@ impl text::Renderer for Null {
fn draw(
&mut self,
+ _defaults: &Self::Defaults,
_bounds: Rectangle,
_content: &str,
_size: u16,
@@ -119,13 +131,16 @@ impl text_input::Renderer for Null {
impl button::Renderer for Null {
type Style = ();
- fn draw(
+ fn draw<Message>(
&mut self,
+ _defaults: &Self::Defaults,
_bounds: Rectangle,
_cursor_position: Point,
+ _is_disabled: bool,
_is_pressed: bool,
_style: &Self::Style,
- _content: Self::Output,
+ _content: &Element<'_, Message, Self>,
+ _content_layout: Layout<'_>,
) -> Self::Output {
}
}
diff --git a/native/src/user_interface.rs b/native/src/user_interface.rs
index 07b936a9..970bf0c1 100644
--- a/native/src/user_interface.rs
+++ b/native/src/user_interface.rs
@@ -43,24 +43,7 @@ where
/// use iced_wgpu::Renderer;
///
/// # mod iced_wgpu {
- /// # pub struct Renderer;
- /// #
- /// # impl Renderer {
- /// # pub fn new() -> Self { Renderer }
- /// # }
- /// #
- /// # impl iced_native::Renderer for Renderer { type Output = (); }
- /// #
- /// # impl iced_native::column::Renderer for Renderer {
- /// # fn draw<Message>(
- /// # &mut self,
- /// # _children: &[iced_native::Element<'_, Message, Self>],
- /// # _layout: iced_native::Layout<'_>,
- /// # _cursor_position: iced_native::Point,
- /// # ) -> Self::Output {
- /// # ()
- /// # }
- /// # }
+ /// # pub use iced_native::renderer::Null as Renderer;
/// # }
/// #
/// # use iced_native::Column;
@@ -139,24 +122,7 @@ where
/// use iced_wgpu::Renderer;
///
/// # mod iced_wgpu {
- /// # pub struct Renderer;
- /// #
- /// # impl Renderer {
- /// # pub fn new() -> Self { Renderer }
- /// # }
- /// #
- /// # impl iced_native::Renderer for Renderer { type Output = (); }
- /// #
- /// # impl iced_native::column::Renderer for Renderer {
- /// # fn draw<Message>(
- /// # &mut self,
- /// # _children: &[iced_native::Element<'_, Message, Self>],
- /// # _layout: iced_native::Layout<'_>,
- /// # _cursor_position: iced_native::Point,
- /// # ) -> Self::Output {
- /// # ()
- /// # }
- /// # }
+ /// # pub use iced_native::renderer::Null as Renderer;
/// # }
/// #
/// # use iced_native::Column;
@@ -241,24 +207,7 @@ where
/// use iced_wgpu::Renderer;
///
/// # mod iced_wgpu {
- /// # pub struct Renderer;
- /// #
- /// # impl Renderer {
- /// # pub fn new() -> Self { Renderer }
- /// # }
- /// #
- /// # impl iced_native::Renderer for Renderer { type Output = (); }
- /// #
- /// # impl iced_native::column::Renderer for Renderer {
- /// # fn draw<Message>(
- /// # &mut self,
- /// # _children: &[iced_native::Element<'_, Message, Self>],
- /// # _layout: iced_native::Layout<'_>,
- /// # _cursor_position: iced_native::Point,
- /// # ) -> Self::Output {
- /// # ()
- /// # }
- /// # }
+ /// # pub use iced_native::renderer::Null as Renderer;
/// # }
/// #
/// # use iced_native::Column;
@@ -304,6 +253,7 @@ where
pub fn draw(&self, renderer: &mut Renderer) -> Renderer::Output {
self.root.widget.draw(
renderer,
+ &Renderer::Defaults::default(),
Layout::new(&self.layout),
self.cursor_position,
)
diff --git a/native/src/widget.rs b/native/src/widget.rs
index 26889280..4aa7e7f0 100644
--- a/native/src/widget.rs
+++ b/native/src/widget.rs
@@ -101,6 +101,7 @@ where
fn draw(
&self,
renderer: &mut Renderer,
+ defaults: &Renderer::Defaults,
layout: Layout<'_>,
cursor_position: Point,
) -> Renderer::Output;
diff --git a/native/src/widget/button.rs b/native/src/widget/button.rs
index 4a7187da..75ef2693 100644
--- a/native/src/widget/button.rs
+++ b/native/src/widget/button.rs
@@ -216,21 +216,19 @@ where
fn draw(
&self,
renderer: &mut Renderer,
+ defaults: &Renderer::Defaults,
layout: Layout<'_>,
cursor_position: Point,
) -> Renderer::Output {
- let content = self.content.draw(
- renderer,
- layout.children().next().unwrap(),
- cursor_position,
- );
-
renderer.draw(
+ defaults,
layout.bounds(),
cursor_position,
+ self.on_press.is_none(),
self.state.is_pressed,
&self.style,
- content,
+ &self.content,
+ layout.children().next().unwrap(),
)
}
@@ -253,13 +251,16 @@ pub trait Renderer: crate::Renderer + Sized {
/// Draws a [`Button`].
///
/// [`Button`]: struct.Button.html
- fn draw(
+ fn draw<Message>(
&mut self,
+ defaults: &Self::Defaults,
bounds: Rectangle,
cursor_position: Point,
+ is_disabled: bool,
is_pressed: bool,
style: &Self::Style,
- content: Self::Output,
+ content: &Element<'_, Message, Self>,
+ content_layout: Layout<'_>,
) -> Self::Output;
}
diff --git a/native/src/widget/checkbox.rs b/native/src/widget/checkbox.rs
index 0dcac712..87a7f629 100644
--- a/native/src/widget/checkbox.rs
+++ b/native/src/widget/checkbox.rs
@@ -134,6 +134,7 @@ where
fn draw(
&self,
renderer: &mut Renderer,
+ defaults: &Renderer::Defaults,
layout: Layout<'_>,
cursor_position: Point,
) -> Renderer::Output {
@@ -146,6 +147,7 @@ where
let label = text::Renderer::draw(
renderer,
+ defaults,
label_layout.bounds(),
&self.label,
text::Renderer::default_size(renderer),
diff --git a/native/src/widget/column.rs b/native/src/widget/column.rs
index 4b5d631c..3418d4b0 100644
--- a/native/src/widget/column.rs
+++ b/native/src/widget/column.rs
@@ -173,10 +173,11 @@ where
fn draw(
&self,
renderer: &mut Renderer,
+ defaults: &Renderer::Defaults,
layout: Layout<'_>,
cursor_position: Point,
) -> Renderer::Output {
- renderer.draw(&self.children, layout, cursor_position)
+ renderer.draw(defaults, &self.children, layout, cursor_position)
}
fn hash_layout(&self, state: &mut Hasher) {
@@ -213,6 +214,7 @@ pub trait Renderer: crate::Renderer + Sized {
/// [`Layout`]: ../layout/struct.Layout.html
fn draw<Message>(
&mut self,
+ defaults: &Self::Defaults,
content: &[Element<'_, Message, Self>],
layout: Layout<'_>,
cursor_position: Point,
diff --git a/native/src/widget/container.rs b/native/src/widget/container.rs
index 74f0e0ef..93804c99 100644
--- a/native/src/widget/container.rs
+++ b/native/src/widget/container.rs
@@ -147,11 +147,13 @@ where
fn draw(
&self,
renderer: &mut Renderer,
+ defaults: &Renderer::Defaults,
layout: Layout<'_>,
cursor_position: Point,
) -> Renderer::Output {
self.content.draw(
renderer,
+ defaults,
layout.children().next().unwrap(),
cursor_position,
)
diff --git a/native/src/widget/image.rs b/native/src/widget/image.rs
index 20375822..1efe4570 100644
--- a/native/src/widget/image.rs
+++ b/native/src/widget/image.rs
@@ -95,6 +95,7 @@ where
fn draw(
&self,
renderer: &mut Renderer,
+ _defaults: &Renderer::Defaults,
layout: Layout<'_>,
_cursor_position: Point,
) -> Renderer::Output {
diff --git a/native/src/widget/radio.rs b/native/src/widget/radio.rs
index a9995b86..6ac00770 100644
--- a/native/src/widget/radio.rs
+++ b/native/src/widget/radio.rs
@@ -131,6 +131,7 @@ where
fn draw(
&self,
renderer: &mut Renderer,
+ defaults: &Renderer::Defaults,
layout: Layout<'_>,
cursor_position: Point,
) -> Renderer::Output {
@@ -143,6 +144,7 @@ where
let label = text::Renderer::draw(
renderer,
+ defaults,
label_layout.bounds(),
&self.label,
text::Renderer::default_size(renderer),
diff --git a/native/src/widget/row.rs b/native/src/widget/row.rs
index 3de65deb..76cca3d0 100644
--- a/native/src/widget/row.rs
+++ b/native/src/widget/row.rs
@@ -174,10 +174,11 @@ where
fn draw(
&self,
renderer: &mut Renderer,
+ defaults: &Renderer::Defaults,
layout: Layout<'_>,
cursor_position: Point,
) -> Renderer::Output {
- renderer.draw(&self.children, layout, cursor_position)
+ renderer.draw(defaults, &self.children, layout, cursor_position)
}
fn hash_layout(&self, state: &mut Hasher) {
@@ -215,6 +216,7 @@ pub trait Renderer: crate::Renderer + Sized {
/// [`Layout`]: ../layout/struct.Layout.html
fn draw<Message>(
&mut self,
+ defaults: &Self::Defaults,
children: &[Element<'_, Message, Self>],
layout: Layout<'_>,
cursor_position: Point,
diff --git a/native/src/widget/scrollable.rs b/native/src/widget/scrollable.rs
index 9fa602d5..9df09b14 100644
--- a/native/src/widget/scrollable.rs
+++ b/native/src/widget/scrollable.rs
@@ -255,6 +255,7 @@ where
fn draw(
&self,
renderer: &mut Renderer,
+ defaults: &Renderer::Defaults,
layout: Layout<'_>,
cursor_position: Point,
) -> Renderer::Output {
@@ -277,7 +278,12 @@ where
Point::new(cursor_position.x, -1.0)
};
- self.content.draw(renderer, content_layout, cursor_position)
+ self.content.draw(
+ renderer,
+ defaults,
+ content_layout,
+ cursor_position,
+ )
};
self::Renderer::draw(
diff --git a/native/src/widget/slider.rs b/native/src/widget/slider.rs
index f446f7e8..ea66a347 100644
--- a/native/src/widget/slider.rs
+++ b/native/src/widget/slider.rs
@@ -178,6 +178,7 @@ where
fn draw(
&self,
renderer: &mut Renderer,
+ _defaults: &Renderer::Defaults,
layout: Layout<'_>,
cursor_position: Point,
) -> Renderer::Output {
diff --git a/native/src/widget/svg.rs b/native/src/widget/svg.rs
index 9580f195..f6202f72 100644
--- a/native/src/widget/svg.rs
+++ b/native/src/widget/svg.rs
@@ -91,6 +91,7 @@ where
fn draw(
&self,
renderer: &mut Renderer,
+ _defaults: &Renderer::Defaults,
layout: Layout<'_>,
_cursor_position: Point,
) -> Renderer::Output {
diff --git a/native/src/widget/text.rs b/native/src/widget/text.rs
index cf9c9565..3a3db3cd 100644
--- a/native/src/widget/text.rs
+++ b/native/src/widget/text.rs
@@ -146,10 +146,12 @@ where
fn draw(
&self,
renderer: &mut Renderer,
+ defaults: &Renderer::Defaults,
layout: Layout<'_>,
_cursor_position: Point,
) -> Renderer::Output {
renderer.draw(
+ defaults,
layout.bounds(),
&self.content,
self.size.unwrap_or(renderer.default_size()),
@@ -209,6 +211,7 @@ pub trait Renderer: crate::Renderer {
/// [`VerticalAlignment`]: enum.VerticalAlignment.html
fn draw(
&mut self,
+ defaults: &Self::Defaults,
bounds: Rectangle,
content: &str,
size: u16,
diff --git a/native/src/widget/text_input.rs b/native/src/widget/text_input.rs
index 1d1c32a2..e2114f00 100644
--- a/native/src/widget/text_input.rs
+++ b/native/src/widget/text_input.rs
@@ -343,6 +343,7 @@ where
fn draw(
&self,
renderer: &mut Renderer,
+ _defaults: &Renderer::Defaults,
layout: Layout<'_>,
cursor_position: Point,
) -> Renderer::Output {
diff --git a/wgpu/src/defaults.rs b/wgpu/src/defaults.rs
new file mode 100644
index 00000000..8de8258b
--- /dev/null
+++ b/wgpu/src/defaults.rs
@@ -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,
+ }
+ }
+}
diff --git a/wgpu/src/lib.rs b/wgpu/src/lib.rs
index 55f93546..786b6872 100644
--- a/wgpu/src/lib.rs
+++ b/wgpu/src/lib.rs
@@ -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)]
diff --git a/wgpu/src/renderer.rs b/wgpu/src/renderer.rs
index 47b258ed..1b143d90 100644
--- a/wgpu/src/renderer.rs
+++ b/wgpu/src/renderer.rs
@@ -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);
diff --git a/wgpu/src/renderer/widget/button.rs b/wgpu/src/renderer/widget/button.rs
index f3817374..4fbd90d7 100644
--- a/wgpu/src/renderer/widget/button.rs
+++ b/wgpu/src/renderer/widget/button.rs
@@ -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,
diff --git a/wgpu/src/renderer/widget/button/style.rs b/wgpu/src/renderer/widget/button/style.rs
deleted file mode 100644
index 8b137891..00000000
--- a/wgpu/src/renderer/widget/button/style.rs
+++ /dev/null
@@ -1 +0,0 @@
-
diff --git a/wgpu/src/renderer/widget/column.rs b/wgpu/src/renderer/widget/column.rs
index 6c31af90..95a7463a 100644
--- a/wgpu/src/renderer/widget/column.rs
+++ b/wgpu/src/renderer/widget/column.rs
@@ -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;
diff --git a/wgpu/src/renderer/widget/row.rs b/wgpu/src/renderer/widget/row.rs
index f082dc61..bd9f1a04 100644
--- a/wgpu/src/renderer/widget/row.rs
+++ b/wgpu/src/renderer/widget/row.rs
@@ -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;
diff --git a/wgpu/src/renderer/widget/text.rs b/wgpu/src/renderer/widget/text.rs
index 08a162ba..d61c5523 100644
--- a/wgpu/src/renderer/widget/text.rs
+++ b/wgpu/src/renderer/widget/text.rs
@@ -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,
diff --git a/wgpu/src/widget/button.rs b/wgpu/src/widget/button.rs
index 7827f8b2..2c4e174f 100644
--- a/wgpu/src/widget/button.rs
+++ b/wgpu/src/widget/button.rs
@@ -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,
}
}
}