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

View file

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

View file

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

View file

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

View file

@ -348,7 +348,10 @@ mod style {
use iced::widget::container;
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();
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();
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();
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();
container::Appearance {

View file

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

View file

@ -209,27 +209,6 @@ mod toast {
&[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 {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
@ -282,14 +261,17 @@ mod toast {
)
.width(Length::Fill)
.padding(5)
.style(
theme::Container::Custom(Box::new(toast.status))
),
.style(match toast.status {
Status::Primary => primary,
Status::Secondary => secondary,
Status::Success => success,
Status::Danger => danger,
}),
horizontal_rule(1),
container(text(toast.body.as_str()))
.width(Length::Fill)
.padding(5)
.style(theme::Container::Box),
.style(container::box_),
])
.max_width(200)
.into()
@ -676,4 +658,48 @@ mod toast {
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::{button, container, tooltip};
use iced::{Element, Length, Sandbox, Settings};
@ -53,7 +52,7 @@ impl Sandbox for Example {
self.position,
)
.gap(10)
.style(theme::Container::Box);
.style(container::box_);
container(tooltip)
.width(Length::Fill)

View file

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