Make container::Style API more consistent

This commit is contained in:
Héctor Ramón Jiménez 2024-07-20 15:53:50 +02:00
parent c851e67734
commit 05884870fc
No known key found for this signature in database
GPG key ID: 7CC46565708259A7
2 changed files with 26 additions and 17 deletions

View file

@ -1,3 +1,4 @@
use iced::border;
use iced::keyboard; use iced::keyboard;
use iced::mouse; use iced::mouse;
use iced::widget::{ use iced::widget::{
@ -85,7 +86,7 @@ impl Layout {
let palette = theme.extended_palette(); let palette = theme.extended_palette();
container::Style::default() container::Style::default()
.with_border(palette.background.strong.color, 4.0) .border(border::color(palette.background.strong.color).width(4))
}) })
.padding(4); .padding(4);
@ -240,7 +241,7 @@ fn application<'a>() -> Element<'a, Message> {
let palette = theme.extended_palette(); let palette = theme.extended_palette();
container::Style::default() container::Style::default()
.with_border(palette.background.strong.color, 1) .border(border::color(palette.background.strong.color).width(1))
}); });
let sidebar = container( let sidebar = container(

View file

@ -546,46 +546,54 @@ pub struct Style {
} }
impl Style { impl Style {
/// Updates the border of the [`Style`] with the given [`Color`] and `width`. /// Updates the text color of the [`Style`].
pub fn with_border( pub fn color(self, color: impl Into<Color>) -> Self {
self,
color: impl Into<Color>,
width: impl Into<Pixels>,
) -> Self {
Self { Self {
border: Border { text_color: Some(color.into()),
color: color.into(), ..self
width: width.into().0, }
..Border::default() }
},
/// Updates the border of the [`Style`].
pub fn border(self, border: impl Into<Border>) -> Self {
Self {
border: border.into(),
..self ..self
} }
} }
/// Updates the background of the [`Style`]. /// Updates the background of the [`Style`].
pub fn with_background(self, background: impl Into<Background>) -> Self { pub fn background(self, background: impl Into<Background>) -> Self {
Self { Self {
background: Some(background.into()), background: Some(background.into()),
..self ..self
} }
} }
/// Updates the shadow of the [`Style`].
pub fn shadow(self, shadow: impl Into<Shadow>) -> Self {
Self {
shadow: shadow.into(),
..self
}
}
} }
impl From<Color> for Style { impl From<Color> for Style {
fn from(color: Color) -> Self { fn from(color: Color) -> Self {
Self::default().with_background(color) Self::default().background(color)
} }
} }
impl From<Gradient> for Style { impl From<Gradient> for Style {
fn from(gradient: Gradient) -> Self { fn from(gradient: Gradient) -> Self {
Self::default().with_background(gradient) Self::default().background(gradient)
} }
} }
impl From<gradient::Linear> for Style { impl From<gradient::Linear> for Style {
fn from(gradient: gradient::Linear) -> Self { fn from(gradient: gradient::Linear) -> Self {
Self::default().with_background(gradient) Self::default().background(gradient)
} }
} }