Implement additional helpers for Border and container::Appearance
This commit is contained in:
parent
b8f05eb8dd
commit
7ece5eea50
12 changed files with 71 additions and 35 deletions
|
|
@ -1,5 +1,5 @@
|
||||||
//! Draw lines around containers.
|
//! Draw lines around containers.
|
||||||
use crate::Color;
|
use crate::{Color, Pixels};
|
||||||
|
|
||||||
/// A border.
|
/// A border.
|
||||||
#[derive(Debug, Clone, Copy, PartialEq, Default)]
|
#[derive(Debug, Clone, Copy, PartialEq, Default)]
|
||||||
|
|
@ -15,11 +15,38 @@ pub struct Border {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Border {
|
impl Border {
|
||||||
/// Creates a new default [`Border`] with the given [`Radius`].
|
/// Creates a new default rounded [`Border`] with the given [`Radius`].
|
||||||
pub fn with_radius(radius: impl Into<Radius>) -> Self {
|
///
|
||||||
|
/// ```
|
||||||
|
/// # use iced_core::Border;
|
||||||
|
/// #
|
||||||
|
/// assert_eq!(Border::rounded(10), Border::default().with_radius(10));
|
||||||
|
/// ```
|
||||||
|
pub fn rounded(radius: impl Into<Radius>) -> Self {
|
||||||
|
Self::default().with_radius(radius)
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Updates the [`Color`] of the [`Border`].
|
||||||
|
pub fn with_color(self, color: impl Into<Color>) -> Self {
|
||||||
|
Self {
|
||||||
|
color: color.into(),
|
||||||
|
..self
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Updates the [`Radius`] of the [`Border`].
|
||||||
|
pub fn with_radius(self, radius: impl Into<Radius>) -> Self {
|
||||||
Self {
|
Self {
|
||||||
radius: radius.into(),
|
radius: radius.into(),
|
||||||
..Self::default()
|
..self
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Updates the width of the [`Border`].
|
||||||
|
pub fn with_width(self, width: impl Into<Pixels>) -> Self {
|
||||||
|
Self {
|
||||||
|
width: width.into().0,
|
||||||
|
..self
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -62,7 +62,7 @@ mod circle {
|
||||||
renderer.fill_quad(
|
renderer.fill_quad(
|
||||||
renderer::Quad {
|
renderer::Quad {
|
||||||
bounds: layout.bounds(),
|
bounds: layout.bounds(),
|
||||||
border: Border::with_radius(self.radius),
|
border: Border::rounded(self.radius),
|
||||||
..renderer::Quad::default()
|
..renderer::Quad::default()
|
||||||
},
|
},
|
||||||
Color::BLACK,
|
Color::BLACK,
|
||||||
|
|
|
||||||
|
|
@ -4,8 +4,7 @@ use iced::widget::{
|
||||||
};
|
};
|
||||||
use iced::{gradient, window};
|
use iced::{gradient, window};
|
||||||
use iced::{
|
use iced::{
|
||||||
Alignment, Background, Color, Element, Length, Radians, Sandbox, Settings,
|
Alignment, Color, Element, Length, Radians, Sandbox, Settings, Theme,
|
||||||
Theme,
|
|
||||||
};
|
};
|
||||||
|
|
||||||
pub fn main() -> iced::Result {
|
pub fn main() -> iced::Result {
|
||||||
|
|
@ -71,20 +70,12 @@ impl Sandbox for Gradient {
|
||||||
transparent,
|
transparent,
|
||||||
} = *self;
|
} = *self;
|
||||||
|
|
||||||
let appearance = {
|
let gradient = gradient::Linear::new(angle)
|
||||||
let gradient = gradient::Linear::new(angle)
|
.add_stop(0.0, start)
|
||||||
.add_stop(0.0, start)
|
.add_stop(1.0, end);
|
||||||
.add_stop(1.0, end)
|
|
||||||
.into();
|
|
||||||
|
|
||||||
container::Appearance {
|
|
||||||
background: Some(Background::Gradient(gradient)),
|
|
||||||
..Default::default()
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
let gradient_box = themer(
|
let gradient_box = themer(
|
||||||
appearance,
|
gradient,
|
||||||
container(horizontal_space())
|
container(horizontal_space())
|
||||||
.width(Length::Fill)
|
.width(Length::Fill)
|
||||||
.height(Length::Fill),
|
.height(Length::Fill),
|
||||||
|
|
|
||||||
|
|
@ -543,7 +543,7 @@ fn styled(pair: palette::Pair) -> Appearance {
|
||||||
Appearance {
|
Appearance {
|
||||||
background: Some(Background::Color(pair.color)),
|
background: Some(Background::Color(pair.color)),
|
||||||
text_color: pair.text,
|
text_color: pair.text,
|
||||||
border: Border::with_radius(2),
|
border: Border::rounded(2),
|
||||||
..Appearance::default()
|
..Appearance::default()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,7 @@
|
||||||
//! Decorate content and apply alignment.
|
//! Decorate content and apply alignment.
|
||||||
use crate::core::alignment::{self, Alignment};
|
use crate::core::alignment::{self, Alignment};
|
||||||
use crate::core::event::{self, Event};
|
use crate::core::event::{self, Event};
|
||||||
|
use crate::core::gradient::{self, Gradient};
|
||||||
use crate::core::layout;
|
use crate::core::layout;
|
||||||
use crate::core::mouse;
|
use crate::core::mouse;
|
||||||
use crate::core::overlay;
|
use crate::core::overlay;
|
||||||
|
|
@ -510,8 +511,7 @@ pub struct Appearance {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Appearance {
|
impl Appearance {
|
||||||
/// Derives a new [`Appearance`] with a border of the given [`Color`] and
|
/// Updates the border of the [`Appearance`] with the given [`Color`] and `width`.
|
||||||
/// `width`.
|
|
||||||
pub fn with_border(
|
pub fn with_border(
|
||||||
self,
|
self,
|
||||||
color: impl Into<Color>,
|
color: impl Into<Color>,
|
||||||
|
|
@ -527,7 +527,7 @@ impl Appearance {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Derives a new [`Appearance`] with the given [`Background`].
|
/// Updates the background of the [`Appearance`].
|
||||||
pub fn with_background(self, background: impl Into<Background>) -> Self {
|
pub fn with_background(self, background: impl Into<Background>) -> Self {
|
||||||
Self {
|
Self {
|
||||||
background: Some(background.into()),
|
background: Some(background.into()),
|
||||||
|
|
@ -566,6 +566,24 @@ impl DefaultStyle for Appearance {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl DefaultStyle for Color {
|
||||||
|
fn default_style() -> Style<Self> {
|
||||||
|
|color, _status| Appearance::default().with_background(*color)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl DefaultStyle for Gradient {
|
||||||
|
fn default_style() -> Style<Self> {
|
||||||
|
|gradient, _status| Appearance::default().with_background(*gradient)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl DefaultStyle for gradient::Linear {
|
||||||
|
fn default_style() -> Style<Self> {
|
||||||
|
|gradient, _status| Appearance::default().with_background(*gradient)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/// A transparent [`Container`].
|
/// A transparent [`Container`].
|
||||||
pub fn transparent<Theme>(_theme: &Theme, _status: Status) -> Appearance {
|
pub fn transparent<Theme>(_theme: &Theme, _status: Status) -> Appearance {
|
||||||
Appearance::default()
|
Appearance::default()
|
||||||
|
|
@ -577,7 +595,7 @@ pub fn box_(theme: &Theme, _status: Status) -> Appearance {
|
||||||
|
|
||||||
Appearance {
|
Appearance {
|
||||||
background: Some(palette.background.weak.color.into()),
|
background: Some(palette.background.weak.color.into()),
|
||||||
border: Border::with_radius(2),
|
border: Border::rounded(2),
|
||||||
..Appearance::default()
|
..Appearance::default()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -539,7 +539,7 @@ where
|
||||||
width: bounds.width - appearance.border.width * 2.0,
|
width: bounds.width - appearance.border.width * 2.0,
|
||||||
..bounds
|
..bounds
|
||||||
},
|
},
|
||||||
border: Border::with_radius(appearance.border.radius),
|
border: Border::rounded(appearance.border.radius),
|
||||||
..renderer::Quad::default()
|
..renderer::Quad::default()
|
||||||
},
|
},
|
||||||
appearance.selected_background,
|
appearance.selected_background,
|
||||||
|
|
|
||||||
|
|
@ -134,7 +134,7 @@ where
|
||||||
width: active_progress_width,
|
width: active_progress_width,
|
||||||
..bounds
|
..bounds
|
||||||
},
|
},
|
||||||
border: Border::with_radius(appearance.border.radius),
|
border: Border::rounded(appearance.border.radius),
|
||||||
..renderer::Quad::default()
|
..renderer::Quad::default()
|
||||||
},
|
},
|
||||||
appearance.bar,
|
appearance.bar,
|
||||||
|
|
@ -230,6 +230,6 @@ fn styled(
|
||||||
Appearance {
|
Appearance {
|
||||||
background: background.into(),
|
background: background.into(),
|
||||||
bar: bar.into(),
|
bar: bar.into(),
|
||||||
border: Border::with_radius(2),
|
border: Border::rounded(2),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -328,7 +328,7 @@ where
|
||||||
width: bounds.width - dot_size,
|
width: bounds.width - dot_size,
|
||||||
height: bounds.height - dot_size,
|
height: bounds.height - dot_size,
|
||||||
},
|
},
|
||||||
border: Border::with_radius(dot_size / 2.0),
|
border: Border::rounded(dot_size / 2.0),
|
||||||
..renderer::Quad::default()
|
..renderer::Quad::default()
|
||||||
},
|
},
|
||||||
appearance.dot_color,
|
appearance.dot_color,
|
||||||
|
|
|
||||||
|
|
@ -118,7 +118,7 @@ where
|
||||||
renderer.fill_quad(
|
renderer.fill_quad(
|
||||||
renderer::Quad {
|
renderer::Quad {
|
||||||
bounds,
|
bounds,
|
||||||
border: Border::with_radius(appearance.radius),
|
border: Border::rounded(appearance.radius),
|
||||||
..renderer::Quad::default()
|
..renderer::Quad::default()
|
||||||
},
|
},
|
||||||
appearance.color,
|
appearance.color,
|
||||||
|
|
|
||||||
|
|
@ -1692,10 +1692,10 @@ pub fn default(theme: &Theme, status: Status) -> Appearance {
|
||||||
|
|
||||||
let scrollbar = Scrollbar {
|
let scrollbar = Scrollbar {
|
||||||
background: Some(palette.background.weak.color.into()),
|
background: Some(palette.background.weak.color.into()),
|
||||||
border: Border::with_radius(2),
|
border: Border::rounded(2),
|
||||||
scroller: Scroller {
|
scroller: Scroller {
|
||||||
color: palette.background.strong.color,
|
color: palette.background.strong.color,
|
||||||
border: Border::with_radius(2),
|
border: Border::rounded(2),
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -392,7 +392,7 @@ where
|
||||||
width: offset + handle_width / 2.0,
|
width: offset + handle_width / 2.0,
|
||||||
height: style.rail.width,
|
height: style.rail.width,
|
||||||
},
|
},
|
||||||
border: Border::with_radius(style.rail.border_radius),
|
border: Border::rounded(style.rail.border_radius),
|
||||||
..renderer::Quad::default()
|
..renderer::Quad::default()
|
||||||
},
|
},
|
||||||
style.rail.colors.0,
|
style.rail.colors.0,
|
||||||
|
|
@ -406,7 +406,7 @@ where
|
||||||
width: bounds.width - offset - handle_width / 2.0,
|
width: bounds.width - offset - handle_width / 2.0,
|
||||||
height: style.rail.width,
|
height: style.rail.width,
|
||||||
},
|
},
|
||||||
border: Border::with_radius(style.rail.border_radius),
|
border: Border::rounded(style.rail.border_radius),
|
||||||
..renderer::Quad::default()
|
..renderer::Quad::default()
|
||||||
},
|
},
|
||||||
style.rail.colors.1,
|
style.rail.colors.1,
|
||||||
|
|
|
||||||
|
|
@ -397,7 +397,7 @@ where
|
||||||
width: style.rail.width,
|
width: style.rail.width,
|
||||||
height: offset + handle_width / 2.0,
|
height: offset + handle_width / 2.0,
|
||||||
},
|
},
|
||||||
border: Border::with_radius(style.rail.border_radius),
|
border: Border::rounded(style.rail.border_radius),
|
||||||
..renderer::Quad::default()
|
..renderer::Quad::default()
|
||||||
},
|
},
|
||||||
style.rail.colors.1,
|
style.rail.colors.1,
|
||||||
|
|
@ -411,7 +411,7 @@ where
|
||||||
width: style.rail.width,
|
width: style.rail.width,
|
||||||
height: bounds.height - offset - handle_width / 2.0,
|
height: bounds.height - offset - handle_width / 2.0,
|
||||||
},
|
},
|
||||||
border: Border::with_radius(style.rail.border_radius),
|
border: Border::rounded(style.rail.border_radius),
|
||||||
..renderer::Quad::default()
|
..renderer::Quad::default()
|
||||||
},
|
},
|
||||||
style.rail.colors.0,
|
style.rail.colors.0,
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue