Wire up styling to Button in iced_native
This commit is contained in:
parent
95acc1deb8
commit
3140cdc4ba
12 changed files with 55 additions and 96 deletions
|
|
@ -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()
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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));
|
||||
|
|
|
|||
|
|
@ -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 {
|
||||
|
|
|
|||
|
|
@ -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()
|
||||
|
|
|
|||
|
|
@ -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,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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),
|
||||
|
|
|
|||
|
|
@ -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),
|
||||
);
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -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>;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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 = ();
|
||||
|
||||
|
|
|
|||
|
|
@ -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>,
|
||||
|
|
|
|||
|
|
@ -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
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue