Simplify theming for Container widget

This commit is contained in:
Héctor Ramón Jiménez 2024-03-05 03:48:08 +01:00
parent 1f0a0c235a
commit 29326215cc
No known key found for this signature in database
GPG key ID: 7CC46565708259A7
20 changed files with 275 additions and 186 deletions

View file

@ -1,14 +1,13 @@
use iced::executor; use iced::executor;
use iced::highlighter::{self, Highlighter}; use iced::highlighter::{self, Highlighter};
use iced::keyboard; use iced::keyboard;
use iced::theme::{self, Theme};
use iced::widget::{ use iced::widget::{
button, column, container, horizontal_space, pick_list, row, text, button, column, container, horizontal_space, pick_list, row, text,
text_editor, tooltip, text_editor, tooltip,
}; };
use iced::{ use iced::{
Alignment, Application, Command, Element, Font, Length, Settings, Alignment, Application, Command, Element, Font, Length, Settings,
Subscription, Subscription, Theme,
}; };
use std::ffi; use std::ffi;
@ -287,7 +286,7 @@ fn action<'a, Message: Clone + 'a>(
label, label,
tooltip::Position::FollowCursor, tooltip::Position::FollowCursor,
) )
.style(theme::Container::Box) .style(container::box_)
.into() .into()
} else { } else {
action.style(button::secondary).into() action.style(button::secondary).into()

View file

@ -1,7 +1,7 @@
use iced::application; use iced::application;
use iced::theme::{self, Theme}; use iced::theme::{self, Theme};
use iced::widget::{ use iced::widget::{
checkbox, column, container, horizontal_space, row, slider, text, checkbox, column, container, horizontal_space, row, slider, text, themer,
}; };
use iced::{gradient, window}; use iced::{gradient, window};
use iced::{ use iced::{
@ -71,20 +71,24 @@ impl Sandbox for Gradient {
transparent, transparent,
} = *self; } = *self;
let gradient_box = container(horizontal_space()) let appearance = {
.width(Length::Fill) let gradient = gradient::Linear::new(angle)
.height(Length::Fill) .add_stop(0.0, start)
.style(move |_: &_| { .add_stop(1.0, end)
let gradient = gradient::Linear::new(angle) .into();
.add_stop(0.0, start)
.add_stop(1.0, end)
.into();
container::Appearance { container::Appearance {
background: Some(Background::Gradient(gradient)), background: Some(Background::Gradient(gradient)),
..Default::default() ..Default::default()
} }
}); };
let gradient_box = themer(
move |_| appearance,
container(horizontal_space())
.width(Length::Fill)
.height(Length::Fill),
);
let angle_picker = row![ let angle_picker = row![
text("Angle").width(64), text("Angle").width(64),

View file

@ -1,7 +1,6 @@
use iced::executor; use iced::executor;
use iced::keyboard; use iced::keyboard;
use iced::mouse; use iced::mouse;
use iced::theme;
use iced::widget::{ use iced::widget::{
button, canvas, checkbox, column, container, horizontal_space, pick_list, button, canvas, checkbox, column, container, horizontal_space, pick_list,
row, scrollable, text, row, scrollable, text,
@ -98,7 +97,7 @@ impl Application for Layout {
} else { } else {
self.example.view() self.example.view()
}) })
.style(|theme: &Theme| { .style(|theme, _status| {
let palette = theme.extended_palette(); let palette = theme.extended_palette();
container::Appearance::default() container::Appearance::default()
@ -262,7 +261,7 @@ fn application<'a>() -> Element<'a, Message> {
.padding(10) .padding(10)
.align_items(Alignment::Center), .align_items(Alignment::Center),
) )
.style(|theme: &Theme| { .style(|theme, _status| {
let palette = theme.extended_palette(); let palette = theme.extended_palette();
container::Appearance::default() container::Appearance::default()
@ -276,7 +275,7 @@ fn application<'a>() -> Element<'a, Message> {
.width(200) .width(200)
.align_items(Alignment::Center), .align_items(Alignment::Center),
) )
.style(theme::Container::Box) .style(container::box_)
.height(Length::Fill) .height(Length::Fill)
.center_y(); .center_y();

View file

@ -2,7 +2,6 @@ use iced::event::{self, Event};
use iced::executor; use iced::executor;
use iced::keyboard; use iced::keyboard;
use iced::keyboard::key; use iced::keyboard::key;
use iced::theme;
use iced::widget::{ use iced::widget::{
self, button, column, container, horizontal_space, pick_list, row, text, self, button, column, container, horizontal_space, pick_list, row, text,
text_input, text_input,
@ -175,7 +174,7 @@ impl Application for App {
) )
.width(300) .width(300)
.padding(10) .padding(10)
.style(theme::Container::Box); .style(container::box_);
Modal::new(content, modal) Modal::new(content, modal)
.on_blur(Message::HideModal) .on_blur(Message::HideModal)

View file

@ -348,7 +348,10 @@ mod style {
use iced::widget::container; use iced::widget::container;
use iced::{Border, Theme}; use iced::{Border, Theme};
pub fn title_bar_active(theme: &Theme) -> container::Appearance { pub fn title_bar_active(
theme: &Theme,
_status: container::Status,
) -> container::Appearance {
let palette = theme.extended_palette(); let palette = theme.extended_palette();
container::Appearance { container::Appearance {
@ -358,7 +361,10 @@ mod style {
} }
} }
pub fn title_bar_focused(theme: &Theme) -> container::Appearance { pub fn title_bar_focused(
theme: &Theme,
_status: container::Status,
) -> container::Appearance {
let palette = theme.extended_palette(); let palette = theme.extended_palette();
container::Appearance { container::Appearance {
@ -368,7 +374,10 @@ mod style {
} }
} }
pub fn pane_active(theme: &Theme) -> container::Appearance { pub fn pane_active(
theme: &Theme,
_status: container::Status,
) -> container::Appearance {
let palette = theme.extended_palette(); let palette = theme.extended_palette();
container::Appearance { container::Appearance {
@ -382,7 +391,10 @@ mod style {
} }
} }
pub fn pane_focused(theme: &Theme) -> container::Appearance { pub fn pane_focused(
theme: &Theme,
_status: container::Status,
) -> container::Appearance {
let palette = theme.extended_palette(); let palette = theme.extended_palette();
container::Appearance { container::Appearance {

View file

@ -1,7 +1,6 @@
use iced::alignment; use iced::alignment;
use iced::executor; use iced::executor;
use iced::keyboard; use iced::keyboard;
use iced::theme;
use iced::widget::{button, column, container, image, row, text, text_input}; use iced::widget::{button, column, container, image, row, text, text_input};
use iced::window; use iced::window;
use iced::window::screenshot::{self, Screenshot}; use iced::window::screenshot::{self, Screenshot};
@ -149,7 +148,7 @@ impl Application for Example {
let image = container(image) let image = container(image)
.padding(10) .padding(10)
.style(theme::Container::Box) .style(container::box_)
.width(Length::FillPortion(2)) .width(Length::FillPortion(2))
.height(Length::Fill) .height(Length::Fill)
.center_x() .center_x()

View file

@ -209,27 +209,6 @@ mod toast {
&[Self::Primary, Self::Secondary, Self::Success, Self::Danger]; &[Self::Primary, Self::Secondary, Self::Success, Self::Danger];
} }
impl container::StyleSheet for Status {
type Style = Theme;
fn appearance(&self, theme: &Theme) -> container::Appearance {
let palette = theme.extended_palette();
let pair = match self {
Status::Primary => palette.primary.weak,
Status::Secondary => palette.secondary.weak,
Status::Success => palette.success.weak,
Status::Danger => palette.danger.weak,
};
container::Appearance {
background: Some(pair.color.into()),
text_color: pair.text.into(),
..Default::default()
}
}
}
impl fmt::Display for Status { impl fmt::Display for Status {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self { match self {
@ -282,14 +261,17 @@ mod toast {
) )
.width(Length::Fill) .width(Length::Fill)
.padding(5) .padding(5)
.style( .style(match toast.status {
theme::Container::Custom(Box::new(toast.status)) Status::Primary => primary,
), Status::Secondary => secondary,
Status::Success => success,
Status::Danger => danger,
}),
horizontal_rule(1), horizontal_rule(1),
container(text(toast.body.as_str())) container(text(toast.body.as_str()))
.width(Length::Fill) .width(Length::Fill)
.padding(5) .padding(5)
.style(theme::Container::Box), .style(container::box_),
]) ])
.max_width(200) .max_width(200)
.into() .into()
@ -676,4 +658,48 @@ mod toast {
Element::new(manager) Element::new(manager)
} }
} }
fn styled(pair: theme::palette::Pair) -> container::Appearance {
container::Appearance {
background: Some(pair.color.into()),
text_color: pair.text.into(),
..Default::default()
}
}
fn primary(
theme: &Theme,
_status: container::Status,
) -> container::Appearance {
let palette = theme.extended_palette();
styled(palette.primary.weak)
}
fn secondary(
theme: &Theme,
_status: container::Status,
) -> container::Appearance {
let palette = theme.extended_palette();
styled(palette.secondary.weak)
}
fn success(
theme: &Theme,
_status: container::Status,
) -> container::Appearance {
let palette = theme.extended_palette();
styled(palette.success.weak)
}
fn danger(
theme: &Theme,
_status: container::Status,
) -> container::Appearance {
let palette = theme.extended_palette();
styled(palette.danger.weak)
}
} }

View file

@ -1,4 +1,3 @@
use iced::theme;
use iced::widget::tooltip::Position; use iced::widget::tooltip::Position;
use iced::widget::{button, container, tooltip}; use iced::widget::{button, container, tooltip};
use iced::{Element, Length, Sandbox, Settings}; use iced::{Element, Length, Sandbox, Settings};
@ -53,7 +52,7 @@ impl Sandbox for Example {
self.position, self.position,
) )
.gap(10) .gap(10)
.style(theme::Container::Box); .style(container::box_);
container(tooltip) container(tooltip)
.width(Length::Fill) .width(Length::Fill)

View file

@ -1,14 +1,13 @@
use iced::event::{self, Event}; use iced::event::{self, Event};
use iced::executor; use iced::executor;
use iced::mouse; use iced::mouse;
use iced::theme::{self, Theme};
use iced::widget::{ use iced::widget::{
column, container, horizontal_space, row, scrollable, text, vertical_space, column, container, horizontal_space, row, scrollable, text, vertical_space,
}; };
use iced::window; use iced::window;
use iced::{ use iced::{
Alignment, Application, Color, Command, Element, Font, Length, Point, Alignment, Application, Color, Command, Element, Font, Length, Point,
Rectangle, Settings, Subscription, Rectangle, Settings, Subscription, Theme,
}; };
pub fn main() -> iced::Result { pub fn main() -> iced::Result {
@ -133,7 +132,7 @@ impl Application for Example {
container(text("I am the outer container!")) container(text("I am the outer container!"))
.id(OUTER_CONTAINER.clone()) .id(OUTER_CONTAINER.clone())
.padding(40) .padding(40)
.style(theme::Container::Box), .style(container::box_),
vertical_space().height(400), vertical_space().height(400),
scrollable( scrollable(
column![ column![
@ -142,7 +141,7 @@ impl Application for Example {
container(text("I am the inner container!")) container(text("I am the inner container!"))
.id(INNER_CONTAINER.clone()) .id(INNER_CONTAINER.clone())
.padding(40) .padding(40)
.style(theme::Container::Box), .style(container::box_),
vertical_space().height(400), vertical_space().height(400),
] ]
.padding(20) .padding(20)

View file

@ -20,7 +20,7 @@ use crate::text_editor;
use crate::text_input; use crate::text_input;
use crate::toggler; use crate::toggler;
use crate::core::{Background, Border, Color, Shadow}; use crate::core::{Background, Border, Color};
use std::fmt; use std::fmt;
use std::rc::Rc; use std::rc::Rc;
@ -283,59 +283,6 @@ impl<T: Fn(&Theme) -> application::Appearance> application::StyleSheet for T {
} }
} }
/// The style of a container.
#[derive(Default)]
pub enum Container {
/// No style.
#[default]
Transparent,
/// A simple box.
Box,
/// A custom style.
Custom(Box<dyn container::StyleSheet<Style = Theme>>),
}
impl From<container::Appearance> for Container {
fn from(appearance: container::Appearance) -> Self {
Self::Custom(Box::new(move |_: &_| appearance))
}
}
impl<T: Fn(&Theme) -> container::Appearance + 'static> From<T> for Container {
fn from(f: T) -> Self {
Self::Custom(Box::new(f))
}
}
impl container::StyleSheet for Theme {
type Style = Container;
fn appearance(&self, style: &Self::Style) -> container::Appearance {
match style {
Container::Transparent => container::Appearance::default(),
Container::Box => {
let palette = self.extended_palette();
container::Appearance {
text_color: None,
background: Some(palette.background.weak.color.into()),
border: Border::with_radius(2),
shadow: Shadow::default(),
}
}
Container::Custom(custom) => custom.appearance(self),
}
}
}
impl<T: Fn(&Theme) -> container::Appearance> container::StyleSheet for T {
type Style = Theme;
fn appearance(&self, style: &Self::Style) -> container::Appearance {
(self)(style)
}
}
impl slider::StyleSheet for Theme { impl slider::StyleSheet for Theme {
fn default() -> fn(&Self, slider::Status) -> slider::Appearance { fn default() -> fn(&Self, slider::Status) -> slider::Appearance {
slider slider

View file

@ -299,7 +299,7 @@ impl<'a, T, Message, Theme, Renderer> Widget<Message, Theme, Renderer>
where where
T: Display + Clone + 'static, T: Display + Clone + 'static,
Message: Clone, Message: Clone,
Theme: container::StyleSheet Theme: container::Style
+ text_input::StyleSheet + text_input::StyleSheet
+ scrollable::StyleSheet + scrollable::StyleSheet
+ menu::StyleSheet, + menu::StyleSheet,
@ -719,7 +719,7 @@ impl<'a, T, Message, Theme, Renderer>
where where
T: Display + Clone + 'static, T: Display + Clone + 'static,
Message: Clone + 'a, Message: Clone + 'a,
Theme: container::StyleSheet Theme: container::Style
+ text_input::StyleSheet + text_input::StyleSheet
+ scrollable::StyleSheet + scrollable::StyleSheet
+ menu::StyleSheet + menu::StyleSheet

View file

@ -8,12 +8,11 @@ use crate::core::renderer;
use crate::core::widget::tree::{self, Tree}; use crate::core::widget::tree::{self, Tree};
use crate::core::widget::{self, Operation}; use crate::core::widget::{self, Operation};
use crate::core::{ use crate::core::{
Background, Clipboard, Color, Element, Layout, Length, Padding, Pixels, Background, Border, Clipboard, Color, Element, Layout, Length, Padding,
Point, Rectangle, Shell, Size, Vector, Widget, Pixels, Point, Rectangle, Shadow, Shell, Size, Vector, Widget,
}; };
use crate::runtime::Command; use crate::runtime::Command;
use crate::style::Theme;
pub use iced_style::container::{Appearance, StyleSheet};
/// An element decorating some content. /// An element decorating some content.
/// ///
@ -25,7 +24,6 @@ pub struct Container<
Theme = crate::Theme, Theme = crate::Theme,
Renderer = crate::Renderer, Renderer = crate::Renderer,
> where > where
Theme: StyleSheet,
Renderer: crate::core::Renderer, Renderer: crate::core::Renderer,
{ {
id: Option<Id>, id: Option<Id>,
@ -36,19 +34,19 @@ pub struct Container<
max_height: f32, max_height: f32,
horizontal_alignment: alignment::Horizontal, horizontal_alignment: alignment::Horizontal,
vertical_alignment: alignment::Vertical, vertical_alignment: alignment::Vertical,
style: Theme::Style, style: fn(&Theme, Status) -> Appearance,
clip: bool, clip: bool,
content: Element<'a, Message, Theme, Renderer>, content: Element<'a, Message, Theme, Renderer>,
} }
impl<'a, Message, Theme, Renderer> Container<'a, Message, Theme, Renderer> impl<'a, Message, Theme, Renderer> Container<'a, Message, Theme, Renderer>
where where
Theme: StyleSheet,
Renderer: crate::core::Renderer, Renderer: crate::core::Renderer,
{ {
/// Creates an empty [`Container`]. /// Creates an empty [`Container`].
pub fn new<T>(content: T) -> Self pub fn new<T>(content: T) -> Self
where where
Theme: Style,
T: Into<Element<'a, Message, Theme, Renderer>>, T: Into<Element<'a, Message, Theme, Renderer>>,
{ {
let content = content.into(); let content = content.into();
@ -63,7 +61,7 @@ where
max_height: f32::INFINITY, max_height: f32::INFINITY,
horizontal_alignment: alignment::Horizontal::Left, horizontal_alignment: alignment::Horizontal::Left,
vertical_alignment: alignment::Vertical::Top, vertical_alignment: alignment::Vertical::Top,
style: Default::default(), style: Theme::default(),
clip: false, clip: false,
content, content,
} }
@ -130,8 +128,8 @@ where
} }
/// Sets the style of the [`Container`]. /// Sets the style of the [`Container`].
pub fn style(mut self, style: impl Into<Theme::Style>) -> Self { pub fn style(mut self, style: fn(&Theme, Status) -> Appearance) -> Self {
self.style = style.into(); self.style = style;
self self
} }
@ -146,7 +144,6 @@ where
impl<'a, Message, Theme, Renderer> Widget<Message, Theme, Renderer> impl<'a, Message, Theme, Renderer> Widget<Message, Theme, Renderer>
for Container<'a, Message, Theme, Renderer> for Container<'a, Message, Theme, Renderer>
where where
Theme: StyleSheet,
Renderer: crate::core::Renderer, Renderer: crate::core::Renderer,
{ {
fn tag(&self) -> tree::Tag { fn tag(&self) -> tree::Tag {
@ -262,10 +259,18 @@ where
cursor: mouse::Cursor, cursor: mouse::Cursor,
viewport: &Rectangle, viewport: &Rectangle,
) { ) {
let style = theme.appearance(&self.style); let bounds = layout.bounds();
if let Some(clipped_viewport) = layout.bounds().intersection(viewport) { let status = if cursor.is_over(bounds) {
draw_background(renderer, &style, layout.bounds()); Status::Hovered
} else {
Status::Idle
};
let style = (self.style)(theme, status);
if let Some(clipped_viewport) = bounds.intersection(viewport) {
draw_background(renderer, &style, bounds);
self.content.as_widget().draw( self.content.as_widget().draw(
tree, tree,
@ -307,7 +312,7 @@ impl<'a, Message, Theme, Renderer> From<Container<'a, Message, Theme, Renderer>>
for Element<'a, Message, Theme, Renderer> for Element<'a, Message, Theme, Renderer>
where where
Message: 'a, Message: 'a,
Theme: 'a + StyleSheet, Theme: 'a,
Renderer: 'a + crate::core::Renderer, Renderer: 'a + crate::core::Renderer,
{ {
fn from( fn from(
@ -482,3 +487,86 @@ pub fn visible_bounds(id: Id) -> Command<Option<Rectangle>> {
bounds: None, bounds: None,
}) })
} }
/// The appearance of a container.
#[derive(Debug, Clone, Copy, Default)]
pub struct Appearance {
/// The text [`Color`] of the container.
pub text_color: Option<Color>,
/// The [`Background`] of the container.
pub background: Option<Background>,
/// The [`Border`] of the container.
pub border: Border,
/// The [`Shadow`] of the container.
pub shadow: Shadow,
}
impl Appearance {
/// Derives a new [`Appearance`] with a border of the given [`Color`] and
/// `width`.
pub fn with_border(
self,
color: impl Into<Color>,
width: impl Into<Pixels>,
) -> Self {
Self {
border: Border {
color: color.into(),
width: width.into().0,
..Border::default()
},
..self
}
}
/// Derives a new [`Appearance`] with the given [`Background`].
pub fn with_background(self, background: impl Into<Background>) -> Self {
Self {
background: Some(background.into()),
..self
}
}
}
/// The possible status of a [`Container`].
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Status {
/// The [`Container`] is idle.
Idle,
/// The [`Container`] is being hovered.
Hovered,
}
/// The style of a [`Container`] for a theme.
pub trait Style {
/// The default style of a [`Container`].
fn default() -> fn(&Self, Status) -> Appearance;
}
impl Style for Theme {
fn default() -> fn(&Self, Status) -> Appearance {
transparent
}
}
impl Style for Appearance {
fn default() -> fn(&Self, Status) -> Appearance {
|appearance, _status| *appearance
}
}
/// A transparent [`Container`].
pub fn transparent(_theme: &Theme, _status: Status) -> Appearance {
<Appearance as Default>::default()
}
/// A rounded [`Container`] with a background.
pub fn box_(theme: &Theme, _status: Status) -> Appearance {
let palette = theme.extended_palette();
Appearance {
background: Some(palette.background.weak.color.into()),
border: Border::with_radius(2),
..<Appearance as Default>::default()
}
}

View file

@ -58,7 +58,7 @@ pub fn container<'a, Message, Theme, Renderer>(
content: impl Into<Element<'a, Message, Theme, Renderer>>, content: impl Into<Element<'a, Message, Theme, Renderer>>,
) -> Container<'a, Message, Theme, Renderer> ) -> Container<'a, Message, Theme, Renderer>
where where
Theme: container::StyleSheet, Theme: container::Style,
Renderer: core::Renderer, Renderer: core::Renderer,
{ {
Container::new(content) Container::new(content)
@ -134,7 +134,7 @@ pub fn tooltip<'a, Message, Theme, Renderer>(
position: tooltip::Position, position: tooltip::Position,
) -> crate::Tooltip<'a, Message, Theme, Renderer> ) -> crate::Tooltip<'a, Message, Theme, Renderer>
where where
Theme: container::StyleSheet + text::StyleSheet, Theme: container::Style + text::StyleSheet,
Renderer: core::text::Renderer, Renderer: core::text::Renderer,
{ {
Tooltip::new(content, tooltip, position) Tooltip::new(content, tooltip, position)
@ -278,7 +278,7 @@ where
Theme: pick_list::StyleSheet Theme: pick_list::StyleSheet
+ scrollable::StyleSheet + scrollable::StyleSheet
+ overlay::menu::StyleSheet + overlay::menu::StyleSheet
+ container::StyleSheet, + container::Style,
<Theme as overlay::menu::StyleSheet>::Style: <Theme as overlay::menu::StyleSheet>::Style:
From<<Theme as pick_list::StyleSheet>::Style>, From<<Theme as pick_list::StyleSheet>::Style>,
{ {

View file

@ -47,7 +47,7 @@ impl<'a, T, Message, Theme, Renderer> Menu<'a, T, Message, Theme, Renderer>
where where
T: ToString + Clone, T: ToString + Clone,
Message: 'a, Message: 'a,
Theme: StyleSheet + container::StyleSheet + scrollable::StyleSheet + 'a, Theme: StyleSheet + container::Style + scrollable::StyleSheet + 'a,
Renderer: text::Renderer + 'a, Renderer: text::Renderer + 'a,
{ {
/// Creates a new [`Menu`] with the given [`State`], a list of options, and /// Creates a new [`Menu`] with the given [`State`], a list of options, and
@ -165,7 +165,7 @@ impl Default for State {
struct Overlay<'a, Message, Theme, Renderer> struct Overlay<'a, Message, Theme, Renderer>
where where
Theme: StyleSheet + container::StyleSheet, Theme: StyleSheet + container::Style,
Renderer: crate::core::Renderer, Renderer: crate::core::Renderer,
{ {
position: Point, position: Point,
@ -179,7 +179,7 @@ where
impl<'a, Message, Theme, Renderer> Overlay<'a, Message, Theme, Renderer> impl<'a, Message, Theme, Renderer> Overlay<'a, Message, Theme, Renderer>
where where
Message: 'a, Message: 'a,
Theme: StyleSheet + container::StyleSheet + scrollable::StyleSheet + 'a, Theme: StyleSheet + container::Style + scrollable::StyleSheet + 'a,
Renderer: text::Renderer + 'a, Renderer: text::Renderer + 'a,
{ {
pub fn new<T>( pub fn new<T>(
@ -235,7 +235,7 @@ impl<'a, Message, Theme, Renderer>
crate::core::Overlay<Message, Theme, Renderer> crate::core::Overlay<Message, Theme, Renderer>
for Overlay<'a, Message, Theme, Renderer> for Overlay<'a, Message, Theme, Renderer>
where where
Theme: StyleSheet + container::StyleSheet, Theme: StyleSheet + container::Style,
Renderer: text::Renderer, Renderer: text::Renderer,
{ {
fn layout(&mut self, renderer: &Renderer, bounds: Size) -> layout::Node { fn layout(&mut self, renderer: &Renderer, bounds: Size) -> layout::Node {

View file

@ -32,7 +32,6 @@ pub use title_bar::TitleBar;
pub use crate::style::pane_grid::{Appearance, Line, StyleSheet}; pub use crate::style::pane_grid::{Appearance, Line, StyleSheet};
use crate::container;
use crate::core::event::{self, Event}; use crate::core::event::{self, Event};
use crate::core::layout; use crate::core::layout;
use crate::core::mouse; use crate::core::mouse;
@ -105,7 +104,7 @@ pub struct PaneGrid<
Theme = crate::Theme, Theme = crate::Theme,
Renderer = crate::Renderer, Renderer = crate::Renderer,
> where > where
Theme: StyleSheet + container::StyleSheet, Theme: StyleSheet,
Renderer: crate::core::Renderer, Renderer: crate::core::Renderer,
{ {
contents: Contents<'a, Content<'a, Message, Theme, Renderer>>, contents: Contents<'a, Content<'a, Message, Theme, Renderer>>,
@ -120,7 +119,7 @@ pub struct PaneGrid<
impl<'a, Message, Theme, Renderer> PaneGrid<'a, Message, Theme, Renderer> impl<'a, Message, Theme, Renderer> PaneGrid<'a, Message, Theme, Renderer>
where where
Theme: StyleSheet + container::StyleSheet, Theme: StyleSheet,
Renderer: crate::core::Renderer, Renderer: crate::core::Renderer,
{ {
/// Creates a [`PaneGrid`] with the given [`State`] and view function. /// Creates a [`PaneGrid`] with the given [`State`] and view function.
@ -240,7 +239,7 @@ impl<'a, Message, Theme, Renderer> Widget<Message, Theme, Renderer>
for PaneGrid<'a, Message, Theme, Renderer> for PaneGrid<'a, Message, Theme, Renderer>
where where
Renderer: crate::core::Renderer, Renderer: crate::core::Renderer,
Theme: StyleSheet + container::StyleSheet, Theme: StyleSheet,
{ {
fn tag(&self) -> tree::Tag { fn tag(&self) -> tree::Tag {
tree::Tag::of::<state::Action>() tree::Tag::of::<state::Action>()
@ -470,7 +469,7 @@ impl<'a, Message, Theme, Renderer> From<PaneGrid<'a, Message, Theme, Renderer>>
for Element<'a, Message, Theme, Renderer> for Element<'a, Message, Theme, Renderer>
where where
Message: 'a, Message: 'a,
Theme: StyleSheet + container::StyleSheet + 'a, Theme: StyleSheet + 'a,
Renderer: crate::core::Renderer + 'a, Renderer: crate::core::Renderer + 'a,
{ {
fn from( fn from(

View file

@ -20,25 +20,26 @@ pub struct Content<
Theme = crate::Theme, Theme = crate::Theme,
Renderer = crate::Renderer, Renderer = crate::Renderer,
> where > where
Theme: container::StyleSheet,
Renderer: crate::core::Renderer, Renderer: crate::core::Renderer,
{ {
title_bar: Option<TitleBar<'a, Message, Theme, Renderer>>, title_bar: Option<TitleBar<'a, Message, Theme, Renderer>>,
body: Element<'a, Message, Theme, Renderer>, body: Element<'a, Message, Theme, Renderer>,
style: Theme::Style, style: fn(&Theme, container::Status) -> container::Appearance,
} }
impl<'a, Message, Theme, Renderer> Content<'a, Message, Theme, Renderer> impl<'a, Message, Theme, Renderer> Content<'a, Message, Theme, Renderer>
where where
Theme: container::StyleSheet,
Renderer: crate::core::Renderer, Renderer: crate::core::Renderer,
{ {
/// Creates a new [`Content`] with the provided body. /// Creates a new [`Content`] with the provided body.
pub fn new(body: impl Into<Element<'a, Message, Theme, Renderer>>) -> Self { pub fn new(body: impl Into<Element<'a, Message, Theme, Renderer>>) -> Self
where
Theme: container::Style,
{
Self { Self {
title_bar: None, title_bar: None,
body: body.into(), body: body.into(),
style: Default::default(), style: Theme::default(),
} }
} }
@ -52,15 +53,17 @@ where
} }
/// Sets the style of the [`Content`]. /// Sets the style of the [`Content`].
pub fn style(mut self, style: impl Into<Theme::Style>) -> Self { pub fn style(
self.style = style.into(); mut self,
style: fn(&Theme, container::Status) -> container::Appearance,
) -> Self {
self.style = style;
self self
} }
} }
impl<'a, Message, Theme, Renderer> Content<'a, Message, Theme, Renderer> impl<'a, Message, Theme, Renderer> Content<'a, Message, Theme, Renderer>
where where
Theme: container::StyleSheet,
Renderer: crate::core::Renderer, Renderer: crate::core::Renderer,
{ {
pub(super) fn state(&self) -> Tree { pub(super) fn state(&self) -> Tree {
@ -104,7 +107,15 @@ where
let bounds = layout.bounds(); let bounds = layout.bounds();
{ {
let style = theme.appearance(&self.style); let style = {
let status = if cursor.is_over(bounds) {
container::Status::Hovered
} else {
container::Status::Idle
};
(self.style)(theme, status)
};
container::draw_background(renderer, &style, bounds); container::draw_background(renderer, &style, bounds);
} }
@ -370,7 +381,6 @@ where
impl<'a, Message, Theme, Renderer> Draggable impl<'a, Message, Theme, Renderer> Draggable
for &Content<'a, Message, Theme, Renderer> for &Content<'a, Message, Theme, Renderer>
where where
Theme: container::StyleSheet,
Renderer: crate::core::Renderer, Renderer: crate::core::Renderer,
{ {
fn can_be_dragged_at( fn can_be_dragged_at(
@ -393,7 +403,7 @@ impl<'a, T, Message, Theme, Renderer> From<T>
for Content<'a, Message, Theme, Renderer> for Content<'a, Message, Theme, Renderer>
where where
T: Into<Element<'a, Message, Theme, Renderer>>, T: Into<Element<'a, Message, Theme, Renderer>>,
Theme: container::StyleSheet, Theme: container::Style,
Renderer: crate::core::Renderer, Renderer: crate::core::Renderer,
{ {
fn from(element: T) -> Self { fn from(element: T) -> Self {

View file

@ -19,24 +19,23 @@ pub struct TitleBar<
Theme = crate::Theme, Theme = crate::Theme,
Renderer = crate::Renderer, Renderer = crate::Renderer,
> where > where
Theme: container::StyleSheet,
Renderer: crate::core::Renderer, Renderer: crate::core::Renderer,
{ {
content: Element<'a, Message, Theme, Renderer>, content: Element<'a, Message, Theme, Renderer>,
controls: Option<Element<'a, Message, Theme, Renderer>>, controls: Option<Element<'a, Message, Theme, Renderer>>,
padding: Padding, padding: Padding,
always_show_controls: bool, always_show_controls: bool,
style: Theme::Style, style: fn(&Theme, container::Status) -> container::Appearance,
} }
impl<'a, Message, Theme, Renderer> TitleBar<'a, Message, Theme, Renderer> impl<'a, Message, Theme, Renderer> TitleBar<'a, Message, Theme, Renderer>
where where
Theme: container::StyleSheet,
Renderer: crate::core::Renderer, Renderer: crate::core::Renderer,
{ {
/// Creates a new [`TitleBar`] with the given content. /// Creates a new [`TitleBar`] with the given content.
pub fn new<E>(content: E) -> Self pub fn new<E>(content: E) -> Self
where where
Theme: container::Style,
E: Into<Element<'a, Message, Theme, Renderer>>, E: Into<Element<'a, Message, Theme, Renderer>>,
{ {
Self { Self {
@ -44,7 +43,7 @@ where
controls: None, controls: None,
padding: Padding::ZERO, padding: Padding::ZERO,
always_show_controls: false, always_show_controls: false,
style: Default::default(), style: Theme::default(),
} }
} }
@ -64,8 +63,11 @@ where
} }
/// Sets the style of the [`TitleBar`]. /// Sets the style of the [`TitleBar`].
pub fn style(mut self, style: impl Into<Theme::Style>) -> Self { pub fn style(
self.style = style.into(); mut self,
style: fn(&Theme, container::Status) -> container::Appearance,
) -> Self {
self.style = style;
self self
} }
@ -85,7 +87,6 @@ where
impl<'a, Message, Theme, Renderer> TitleBar<'a, Message, Theme, Renderer> impl<'a, Message, Theme, Renderer> TitleBar<'a, Message, Theme, Renderer>
where where
Theme: container::StyleSheet,
Renderer: crate::core::Renderer, Renderer: crate::core::Renderer,
{ {
pub(super) fn state(&self) -> Tree { pub(super) fn state(&self) -> Tree {
@ -128,7 +129,17 @@ where
show_controls: bool, show_controls: bool,
) { ) {
let bounds = layout.bounds(); let bounds = layout.bounds();
let style = theme.appearance(&self.style);
let style = {
let status = if cursor.is_over(bounds) {
container::Status::Hovered
} else {
container::Status::Idle
};
(self.style)(theme, status)
};
let inherited_style = renderer::Style { let inherited_style = renderer::Style {
text_color: style.text_color.unwrap_or(inherited_style.text_color), text_color: style.text_color.unwrap_or(inherited_style.text_color),
}; };

View file

@ -64,7 +64,7 @@ where
Theme: StyleSheet Theme: StyleSheet
+ scrollable::StyleSheet + scrollable::StyleSheet
+ menu::StyleSheet + menu::StyleSheet
+ container::StyleSheet, + container::Style,
<Theme as menu::StyleSheet>::Style: From<<Theme as StyleSheet>::Style>, <Theme as menu::StyleSheet>::Style: From<<Theme as StyleSheet>::Style>,
Renderer: text::Renderer, Renderer: text::Renderer,
{ {
@ -179,7 +179,7 @@ where
Theme: StyleSheet Theme: StyleSheet
+ scrollable::StyleSheet + scrollable::StyleSheet
+ menu::StyleSheet + menu::StyleSheet
+ container::StyleSheet, + container::Style,
<Theme as menu::StyleSheet>::Style: From<<Theme as StyleSheet>::Style>, <Theme as menu::StyleSheet>::Style: From<<Theme as StyleSheet>::Style>,
Renderer: text::Renderer + 'a, Renderer: text::Renderer + 'a,
{ {
@ -320,7 +320,7 @@ where
Theme: StyleSheet Theme: StyleSheet
+ scrollable::StyleSheet + scrollable::StyleSheet
+ menu::StyleSheet + menu::StyleSheet
+ container::StyleSheet + container::Style
+ 'a, + 'a,
<Theme as menu::StyleSheet>::Style: From<<Theme as StyleSheet>::Style>, <Theme as menu::StyleSheet>::Style: From<<Theme as StyleSheet>::Style>,
Renderer: text::Renderer + 'a, Renderer: text::Renderer + 'a,
@ -630,7 +630,7 @@ where
Theme: StyleSheet Theme: StyleSheet
+ scrollable::StyleSheet + scrollable::StyleSheet
+ menu::StyleSheet + menu::StyleSheet
+ container::StyleSheet + container::Style
+ 'a, + 'a,
<Theme as menu::StyleSheet>::Style: From<<Theme as StyleSheet>::Style>, <Theme as menu::StyleSheet>::Style: From<<Theme as StyleSheet>::Style>,
Renderer: text::Renderer + 'a, Renderer: text::Renderer + 'a,

View file

@ -1,5 +1,5 @@
//! Navigate an endless amount of content with a scrollbar. //! Navigate an endless amount of content with a scrollbar.
use crate::container; // use crate::container;
use crate::core::event::{self, Event}; use crate::core::event::{self, Event};
use crate::core::keyboard; use crate::core::keyboard;
use crate::core::layout; use crate::core::layout;
@ -917,11 +917,11 @@ pub fn draw<Theme, Renderer>(
} }
}; };
container::draw_background( // container::draw_background(
renderer, // renderer,
&appearance.container, // &appearance.container,
layout.bounds(), // layout.bounds(),
); // );
// Draw inner content // Draw inner content
if scrollbars.active() { if scrollbars.active() {

View file

@ -20,7 +20,6 @@ pub struct Tooltip<
Theme = crate::Theme, Theme = crate::Theme,
Renderer = crate::Renderer, Renderer = crate::Renderer,
> where > where
Theme: container::StyleSheet,
Renderer: text::Renderer, Renderer: text::Renderer,
{ {
content: Element<'a, Message, Theme, Renderer>, content: Element<'a, Message, Theme, Renderer>,
@ -29,12 +28,11 @@ pub struct Tooltip<
gap: f32, gap: f32,
padding: f32, padding: f32,
snap_within_viewport: bool, snap_within_viewport: bool,
style: <Theme as container::StyleSheet>::Style, style: fn(&Theme, container::Status) -> container::Appearance,
} }
impl<'a, Message, Theme, Renderer> Tooltip<'a, Message, Theme, Renderer> impl<'a, Message, Theme, Renderer> Tooltip<'a, Message, Theme, Renderer>
where where
Theme: container::StyleSheet,
Renderer: text::Renderer, Renderer: text::Renderer,
{ {
/// The default padding of a [`Tooltip`] drawn by this renderer. /// The default padding of a [`Tooltip`] drawn by this renderer.
@ -47,7 +45,10 @@ where
content: impl Into<Element<'a, Message, Theme, Renderer>>, content: impl Into<Element<'a, Message, Theme, Renderer>>,
tooltip: impl Into<Element<'a, Message, Theme, Renderer>>, tooltip: impl Into<Element<'a, Message, Theme, Renderer>>,
position: Position, position: Position,
) -> Self { ) -> Self
where
Theme: container::Style,
{
Tooltip { Tooltip {
content: content.into(), content: content.into(),
tooltip: tooltip.into(), tooltip: tooltip.into(),
@ -55,7 +56,7 @@ where
gap: 0.0, gap: 0.0,
padding: Self::DEFAULT_PADDING, padding: Self::DEFAULT_PADDING,
snap_within_viewport: true, snap_within_viewport: true,
style: Default::default(), style: Theme::default(),
} }
} }
@ -80,9 +81,9 @@ where
/// Sets the style of the [`Tooltip`]. /// Sets the style of the [`Tooltip`].
pub fn style( pub fn style(
mut self, mut self,
style: impl Into<<Theme as container::StyleSheet>::Style>, style: fn(&Theme, container::Status) -> container::Appearance,
) -> Self { ) -> Self {
self.style = style.into(); self.style = style;
self self
} }
} }
@ -90,7 +91,6 @@ where
impl<'a, Message, Theme, Renderer> Widget<Message, Theme, Renderer> impl<'a, Message, Theme, Renderer> Widget<Message, Theme, Renderer>
for Tooltip<'a, Message, Theme, Renderer> for Tooltip<'a, Message, Theme, Renderer>
where where
Theme: container::StyleSheet + crate::text::StyleSheet,
Renderer: text::Renderer, Renderer: text::Renderer,
{ {
fn children(&self) -> Vec<widget::Tree> { fn children(&self) -> Vec<widget::Tree> {
@ -239,7 +239,7 @@ where
positioning: self.position, positioning: self.position,
gap: self.gap, gap: self.gap,
padding: self.padding, padding: self.padding,
style: &self.style, style: self.style,
}))) })))
} else { } else {
None None
@ -262,7 +262,7 @@ impl<'a, Message, Theme, Renderer> From<Tooltip<'a, Message, Theme, Renderer>>
for Element<'a, Message, Theme, Renderer> for Element<'a, Message, Theme, Renderer>
where where
Message: 'a, Message: 'a,
Theme: container::StyleSheet + crate::text::StyleSheet + 'a, Theme: 'a,
Renderer: text::Renderer + 'a, Renderer: text::Renderer + 'a,
{ {
fn from( fn from(
@ -298,7 +298,6 @@ enum State {
struct Overlay<'a, 'b, Message, Theme, Renderer> struct Overlay<'a, 'b, Message, Theme, Renderer>
where where
Theme: container::StyleSheet + widget::text::StyleSheet,
Renderer: text::Renderer, Renderer: text::Renderer,
{ {
position: Point, position: Point,
@ -310,14 +309,13 @@ where
positioning: Position, positioning: Position,
gap: f32, gap: f32,
padding: f32, padding: f32,
style: &'b <Theme as container::StyleSheet>::Style, style: fn(&Theme, container::Status) -> container::Appearance,
} }
impl<'a, 'b, Message, Theme, Renderer> impl<'a, 'b, Message, Theme, Renderer>
overlay::Overlay<Message, Theme, Renderer> overlay::Overlay<Message, Theme, Renderer>
for Overlay<'a, 'b, Message, Theme, Renderer> for Overlay<'a, 'b, Message, Theme, Renderer>
where where
Theme: container::StyleSheet + widget::text::StyleSheet,
Renderer: text::Renderer, Renderer: text::Renderer,
{ {
fn layout(&mut self, renderer: &Renderer, bounds: Size) -> layout::Node { fn layout(&mut self, renderer: &Renderer, bounds: Size) -> layout::Node {
@ -426,7 +424,7 @@ where
layout: Layout<'_>, layout: Layout<'_>,
cursor_position: mouse::Cursor, cursor_position: mouse::Cursor,
) { ) {
let style = container::StyleSheet::appearance(theme, self.style); let style = (self.style)(theme, container::Status::Idle);
container::draw_background(renderer, &style, layout.bounds()); container::draw_background(renderer, &style, layout.bounds());