summaryrefslogtreecommitdiffstats
path: root/examples
diff options
context:
space:
mode:
authorLibravatar Héctor Ramón Jiménez <hector@hecrj.dev>2024-03-05 03:48:08 +0100
committerLibravatar Héctor Ramón Jiménez <hector@hecrj.dev>2024-03-05 03:48:08 +0100
commit29326215ccf13e1d1e25bf3bf5ada007856bff69 (patch)
tree9a286433affc511da2d8926d0f6862b664184b56 /examples
parent1f0a0c235a7729cf2d5716efeecb9c4dc972fdfa (diff)
downloadiced-29326215ccf13e1d1e25bf3bf5ada007856bff69.tar.gz
iced-29326215ccf13e1d1e25bf3bf5ada007856bff69.tar.bz2
iced-29326215ccf13e1d1e25bf3bf5ada007856bff69.zip
Simplify theming for `Container` widget
Diffstat (limited to 'examples')
-rw-r--r--examples/editor/src/main.rs5
-rw-r--r--examples/gradient/src/main.rs34
-rw-r--r--examples/layout/src/main.rs7
-rw-r--r--examples/modal/src/main.rs3
-rw-r--r--examples/pane_grid/src/main.rs20
-rw-r--r--examples/screenshot/src/main.rs3
-rw-r--r--examples/toast/src/main.rs76
-rw-r--r--examples/tooltip/src/main.rs3
-rw-r--r--examples/visible_bounds/src/main.rs7
9 files changed, 97 insertions, 61 deletions
diff --git a/examples/editor/src/main.rs b/examples/editor/src/main.rs
index b5870e9e..60a6ca5a 100644
--- a/examples/editor/src/main.rs
+++ b/examples/editor/src/main.rs
@@ -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()
diff --git a/examples/gradient/src/main.rs b/examples/gradient/src/main.rs
index a021c164..1a264063 100644
--- a/examples/gradient/src/main.rs
+++ b/examples/gradient/src/main.rs
@@ -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();
-
- container::Appearance {
- background: Some(Background::Gradient(gradient)),
- ..Default::default()
- }
- });
+ 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()
+ }
+ };
+
+ let gradient_box = themer(
+ move |_| appearance,
+ container(horizontal_space())
+ .width(Length::Fill)
+ .height(Length::Fill),
+ );
let angle_picker = row![
text("Angle").width(64),
diff --git a/examples/layout/src/main.rs b/examples/layout/src/main.rs
index 39c8315f..b2d28a1c 100644
--- a/examples/layout/src/main.rs
+++ b/examples/layout/src/main.rs
@@ -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();
diff --git a/examples/modal/src/main.rs b/examples/modal/src/main.rs
index 938ce32c..df3da1cd 100644
--- a/examples/modal/src/main.rs
+++ b/examples/modal/src/main.rs
@@ -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)
diff --git a/examples/pane_grid/src/main.rs b/examples/pane_grid/src/main.rs
index 2bed5a03..eb904c3f 100644
--- a/examples/pane_grid/src/main.rs
+++ b/examples/pane_grid/src/main.rs
@@ -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 {
diff --git a/examples/screenshot/src/main.rs b/examples/screenshot/src/main.rs
index dc4684d4..c670b20b 100644
--- a/examples/screenshot/src/main.rs
+++ b/examples/screenshot/src/main.rs
@@ -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()
diff --git a/examples/toast/src/main.rs b/examples/toast/src/main.rs
index c1d29193..49626710 100644
--- a/examples/toast/src/main.rs
+++ b/examples/toast/src/main.rs
@@ -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)
+ }
}
diff --git a/examples/tooltip/src/main.rs b/examples/tooltip/src/main.rs
index a904cce0..c83b671f 100644
--- a/examples/tooltip/src/main.rs
+++ b/examples/tooltip/src/main.rs
@@ -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)
diff --git a/examples/visible_bounds/src/main.rs b/examples/visible_bounds/src/main.rs
index 10cdc783..d7f5a81d 100644
--- a/examples/visible_bounds/src/main.rs
+++ b/examples/visible_bounds/src/main.rs
@@ -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)