summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorLibravatar Héctor Ramón Jiménez <hector0193@gmail.com>2021-10-18 16:02:30 +0700
committerLibravatar Héctor Ramón Jiménez <hector0193@gmail.com>2021-10-18 16:02:30 +0700
commit3140cdc4babcefc444f1c1d30eb0f5f4ed1df054 (patch)
treeb3ea963a73118d5f0bfd7d875ba776ee51466f9b
parent95acc1deb89c4e75b513edb0f4d53b83c7f75b30 (diff)
downloadiced-3140cdc4babcefc444f1c1d30eb0f5f4ed1df054.tar.gz
iced-3140cdc4babcefc444f1c1d30eb0f5f4ed1df054.tar.bz2
iced-3140cdc4babcefc444f1c1d30eb0f5f4ed1df054.zip
Wire up styling to `Button` in `iced_native`
-rw-r--r--examples/game_of_life/src/main.rs6
-rw-r--r--examples/pane_grid/src/main.rs10
-rw-r--r--examples/pokedex/src/main.rs2
-rw-r--r--examples/stopwatch/src/main.rs6
-rw-r--r--examples/styling/src/main.rs8
-rw-r--r--examples/todos/src/main.rs40
-rw-r--r--examples/tour/src/main.rs4
-rw-r--r--graphics/src/widget/button.rs15
-rw-r--r--native/src/renderer/null.rs6
-rw-r--r--native/src/widget/button.rs35
-rw-r--r--style/src/button.rs13
-rw-r--r--web/src/widget/button.rs6
12 files changed, 55 insertions, 96 deletions
diff --git a/examples/game_of_life/src/main.rs b/examples/game_of_life/src/main.rs
index ee425f44..3e94bd44 100644
--- a/examples/game_of_life/src/main.rs
+++ b/examples/game_of_life/src/main.rs
@@ -834,12 +834,12 @@ impl Controls {
Text::new(if is_playing { "Pause" } else { "Play" }),
)
.on_press(Message::TogglePlayback)
- .style(style::Button),
+ .style(&style::Button),
)
.push(
Button::new(&mut self.next_button, Text::new("Next"))
.on_press(Message::Next)
- .style(style::Button),
+ .style(&style::Button),
);
let speed_controls = Row::new()
@@ -883,7 +883,7 @@ impl Controls {
.push(
Button::new(&mut self.clear_button, Text::new("Clear"))
.on_press(Message::Clear)
- .style(style::Clear),
+ .style(&style::Clear),
)
.into()
}
diff --git a/examples/pane_grid/src/main.rs b/examples/pane_grid/src/main.rs
index 844b604d..4126485d 100644
--- a/examples/pane_grid/src/main.rs
+++ b/examples/pane_grid/src/main.rs
@@ -158,7 +158,7 @@ impl Application for Example {
let pin_button =
Button::new(&mut pane.pin_button, Text::new(text).size(14))
.on_press(Message::TogglePin(id))
- .style(style::Button::Pin)
+ .style(&style::Button::Pin)
.padding(3);
let title = Row::with_children(vec![
@@ -316,13 +316,13 @@ impl Content {
split_horizontally,
"Split horizontally",
Message::Split(pane_grid::Axis::Horizontal, pane),
- style::Button::Primary,
+ &style::Button::Primary,
))
.push(button(
split_vertically,
"Split vertically",
Message::Split(pane_grid::Axis::Vertical, pane),
- style::Button::Primary,
+ &style::Button::Primary,
));
if total_panes > 1 && !is_pinned {
@@ -330,7 +330,7 @@ impl Content {
close,
"Close",
Message::Close(pane),
- style::Button::Destructive,
+ &style::Button::Destructive,
));
}
@@ -364,7 +364,7 @@ impl Controls {
) -> Element<Message> {
let mut button =
Button::new(&mut self.close, Text::new("Close").size(14))
- .style(style::Button::Control)
+ .style(&style::Button::Control)
.padding(3);
if total_panes > 1 && !is_pinned {
button = button.on_press(Message::Close(pane));
diff --git a/examples/pokedex/src/main.rs b/examples/pokedex/src/main.rs
index c99240a1..c9930158 100644
--- a/examples/pokedex/src/main.rs
+++ b/examples/pokedex/src/main.rs
@@ -243,7 +243,7 @@ impl From<reqwest::Error> for Error {
fn button<'a>(state: &'a mut button::State, text: &str) -> Button<'a, Message> {
Button::new(state, Text::new(text))
.padding(10)
- .style(style::Button::Primary)
+ .style(&style::Button::Primary)
}
mod style {
diff --git a/examples/stopwatch/src/main.rs b/examples/stopwatch/src/main.rs
index dc8a4de7..e6743620 100644
--- a/examples/stopwatch/src/main.rs
+++ b/examples/stopwatch/src/main.rs
@@ -112,15 +112,15 @@ impl Application for Stopwatch {
let toggle_button = {
let (label, color) = match self.state {
- State::Idle => ("Start", style::Button::Primary),
- State::Ticking { .. } => ("Stop", style::Button::Destructive),
+ State::Idle => ("Start", &style::Button::Primary),
+ State::Ticking { .. } => ("Stop", &style::Button::Destructive),
};
button(&mut self.toggle, label, color).on_press(Message::Toggle)
};
let reset_button =
- button(&mut self.reset, "Reset", style::Button::Secondary)
+ button(&mut self.reset, "Reset", &style::Button::Secondary)
.on_press(Message::Reset);
let controls = Row::new()
diff --git a/examples/styling/src/main.rs b/examples/styling/src/main.rs
index d8254dd9..38ab0411 100644
--- a/examples/styling/src/main.rs
+++ b/examples/styling/src/main.rs
@@ -82,7 +82,7 @@ impl Sandbox for Styling {
let button = Button::new(&mut self.button, Text::new("Submit"))
.padding(10)
.on_press(Message::ButtonPressed)
- .style(self.theme);
+ .style(self.theme.into());
let slider = Slider::new(
&mut self.slider,
@@ -203,11 +203,11 @@ mod style {
}
}
- impl From<Theme> for Box<dyn button::StyleSheet> {
+ impl From<Theme> for &'static dyn button::StyleSheet {
fn from(theme: Theme) -> Self {
match theme {
- Theme::Light => light::Button.into(),
- Theme::Dark => dark::Button.into(),
+ Theme::Light => &light::Button,
+ Theme::Dark => &dark::Button,
}
}
}
diff --git a/examples/todos/src/main.rs b/examples/todos/src/main.rs
index 11f23fd4..1734772d 100644
--- a/examples/todos/src/main.rs
+++ b/examples/todos/src/main.rs
@@ -304,7 +304,7 @@ impl Task {
Button::new(edit_button, edit_icon())
.on_press(TaskMessage::Edit)
.padding(10)
- .style(style::Button::Icon),
+ .style(&style::Button::Icon),
)
.into()
}
@@ -335,7 +335,7 @@ impl Task {
)
.on_press(TaskMessage::Delete)
.padding(10)
- .style(style::Button::Destructive),
+ .style(&style::Button::Destructive),
)
.into()
}
@@ -363,8 +363,10 @@ impl Controls {
let filter_button = |state, label, filter, current_filter| {
let label = Text::new(label).size(16);
let button =
- Button::new(state, label).style(style::Button::Filter {
- selected: filter == current_filter,
+ Button::new(state, label).style(if filter == current_filter {
+ &style::Button::FilterSelected
+ } else {
+ &style::Button::FilterActive
});
button.on_press(Message::FilterChanged(filter)).padding(8)
@@ -602,7 +604,8 @@ mod style {
use iced::{button, Background, Color, Vector};
pub enum Button {
- Filter { selected: bool },
+ FilterActive,
+ FilterSelected,
Icon,
Destructive,
}
@@ -610,20 +613,15 @@ mod style {
impl button::StyleSheet for Button {
fn active(&self) -> button::Style {
match self {
- Button::Filter { selected } => {
- if *selected {
- button::Style {
- background: Some(Background::Color(
- Color::from_rgb(0.2, 0.2, 0.7),
- )),
- border_radius: 10.0,
- text_color: Color::WHITE,
- ..button::Style::default()
- }
- } else {
- button::Style::default()
- }
- }
+ Button::FilterActive => button::Style::default(),
+ Button::FilterSelected => button::Style {
+ background: Some(Background::Color(Color::from_rgb(
+ 0.2, 0.2, 0.7,
+ ))),
+ border_radius: 10.0,
+ text_color: Color::WHITE,
+ ..button::Style::default()
+ },
Button::Icon => button::Style {
text_color: Color::from_rgb(0.5, 0.5, 0.5),
..button::Style::default()
@@ -646,9 +644,7 @@ mod style {
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)
- }
+ Button::FilterActive => Color::from_rgb(0.2, 0.2, 0.7),
_ => active.text_color,
},
shadow_offset: active.shadow_offset + Vector::new(0.0, 1.0),
diff --git a/examples/tour/src/main.rs b/examples/tour/src/main.rs
index b5af48c7..176d275c 100644
--- a/examples/tour/src/main.rs
+++ b/examples/tour/src/main.rs
@@ -64,7 +64,7 @@ impl Sandbox for Tour {
controls = controls.push(
button(back_button, "Back")
.on_press(Message::BackPressed)
- .style(style::Button::Secondary),
+ .style(&style::Button::Secondary),
);
}
@@ -74,7 +74,7 @@ impl Sandbox for Tour {
controls = controls.push(
button(next_button, "Next")
.on_press(Message::NextPressed)
- .style(style::Button::Primary),
+ .style(&style::Button::Primary),
);
}
diff --git a/graphics/src/widget/button.rs b/graphics/src/widget/button.rs
index 990aac9f..3d3e6675 100644
--- a/graphics/src/widget/button.rs
+++ b/graphics/src/widget/button.rs
@@ -1,23 +1,12 @@
//! Allow your users to perform actions by pressing a button.
//!
//! A [`Button`] has some local [`State`].
-use crate::{Backend, Renderer};
-use iced_native::Padding;
+use crate::Renderer;
-pub use iced_native::button::State;
-pub use iced_style::button::{Style, StyleSheet};
+pub use iced_native::button::{State, Style, StyleSheet};
/// A widget that produces a message when clicked.
///
/// This is an alias of an `iced_native` button with an `iced_wgpu::Renderer`.
pub type Button<'a, Message, Backend> =
iced_native::Button<'a, Message, Renderer<Backend>>;
-
-impl<B> iced_native::button::Renderer for Renderer<B>
-where
- B: Backend,
-{
- const DEFAULT_PADDING: Padding = Padding::new(5);
-
- type Style = Box<dyn StyleSheet>;
-}
diff --git a/native/src/renderer/null.rs b/native/src/renderer/null.rs
index fdb6ea92..07023a67 100644
--- a/native/src/renderer/null.rs
+++ b/native/src/renderer/null.rs
@@ -90,12 +90,6 @@ impl text_input::Renderer for Null {
}
}
-impl button::Renderer for Null {
- const DEFAULT_PADDING: Padding = Padding::ZERO;
-
- type Style = ();
-}
-
impl radio::Renderer for Null {
type Style = ();
diff --git a/native/src/widget/button.rs b/native/src/widget/button.rs
index 2aeffd03..77148673 100644
--- a/native/src/widget/button.rs
+++ b/native/src/widget/button.rs
@@ -11,8 +11,11 @@ use crate::{
Clipboard, Element, Hasher, Layout, Length, Padding, Point, Rectangle,
Widget,
};
+
use std::hash::Hash;
+pub use iced_style::button::{Style, StyleSheet};
+
/// A generic widget that produces a message when pressed.
///
/// ```
@@ -54,7 +57,7 @@ use std::hash::Hash;
/// }
/// ```
#[allow(missing_debug_implementations)]
-pub struct Button<'a, Message, Renderer: self::Renderer> {
+pub struct Button<'a, Message, Renderer> {
state: &'a mut State,
content: Element<'a, Message, Renderer>,
on_press: Option<Message>,
@@ -63,13 +66,13 @@ pub struct Button<'a, Message, Renderer: self::Renderer> {
min_width: u32,
min_height: u32,
padding: Padding,
- style: Renderer::Style,
+ style: &'a dyn StyleSheet,
}
impl<'a, Message, Renderer> Button<'a, Message, Renderer>
where
Message: Clone,
- Renderer: self::Renderer,
+ Renderer: crate::Renderer,
{
/// Creates a new [`Button`] with some local [`State`] and the given
/// content.
@@ -85,8 +88,8 @@ where
height: Length::Shrink,
min_width: 0,
min_height: 0,
- padding: Renderer::DEFAULT_PADDING,
- style: Renderer::Style::default(),
+ padding: Padding::new(5),
+ style: Default::default(),
}
}
@@ -128,8 +131,8 @@ where
}
/// Sets the style of the [`Button`].
- pub fn style(mut self, style: impl Into<Renderer::Style>) -> Self {
- self.style = style.into();
+ pub fn style(mut self, style: &'a dyn StyleSheet) -> Self {
+ self.style = style;
self
}
}
@@ -151,7 +154,7 @@ impl<'a, Message, Renderer> Widget<Message, Renderer>
for Button<'a, Message, Renderer>
where
Message: Clone,
- Renderer: self::Renderer,
+ Renderer: crate::Renderer,
{
fn width(&self) -> Length {
self.width
@@ -268,25 +271,11 @@ where
}
}
-/// The renderer of a [`Button`].
-///
-/// Your [renderer] will need to implement this trait before being
-/// able to use a [`Button`] in your user interface.
-///
-/// [renderer]: crate::renderer
-pub trait Renderer: crate::Renderer + Sized {
- /// The default padding of a [`Button`].
- const DEFAULT_PADDING: Padding;
-
- /// The style supported by this renderer.
- type Style: Default;
-}
-
impl<'a, Message, Renderer> From<Button<'a, Message, Renderer>>
for Element<'a, Message, Renderer>
where
Message: 'a + Clone,
- Renderer: 'a + self::Renderer,
+ Renderer: 'a + crate::Renderer,
{
fn from(
button: Button<'a, Message, Renderer>,
diff --git a/style/src/button.rs b/style/src/button.rs
index 2281e32f..608f344b 100644
--- a/style/src/button.rs
+++ b/style/src/button.rs
@@ -80,17 +80,8 @@ impl StyleSheet for Default {
}
}
-impl std::default::Default for Box<dyn StyleSheet> {
+impl std::default::Default for &'static dyn StyleSheet {
fn default() -> Self {
- Box::new(Default)
- }
-}
-
-impl<T> From<T> for Box<dyn StyleSheet>
-where
- T: 'static + StyleSheet,
-{
- fn from(style: T) -> Self {
- Box::new(style)
+ &Default
}
}
diff --git a/web/src/widget/button.rs b/web/src/widget/button.rs
index cd450b55..1ae78201 100644
--- a/web/src/widget/button.rs
+++ b/web/src/widget/button.rs
@@ -51,7 +51,7 @@ pub struct Button<'a, Message> {
#[allow(dead_code)]
min_height: u32,
padding: Padding,
- style: Box<dyn StyleSheet>,
+ style: &'a dyn StyleSheet,
}
impl<'a, Message> Button<'a, Message> {
@@ -104,8 +104,8 @@ impl<'a, Message> Button<'a, Message> {
}
/// Sets the style of the [`Button`].
- pub fn style(mut self, style: impl Into<Box<dyn StyleSheet>>) -> Self {
- self.style = style.into();
+ pub fn style(mut self, style: &'a dyn StyleSheet) -> Self {
+ self.style = style;
self
}