Showcase new Stack widget in bezier_tool example

This commit is contained in:
Héctor Ramón Jiménez 2024-04-25 01:40:06 +02:00
parent 0c74d26456
commit 8b1c514c38
No known key found for this signature in database
GPG key ID: 7CC46565708259A7

View file

@ -1,9 +1,11 @@
//! This example showcases an interactive `Canvas` for drawing Bézier curves.
use iced::widget::{button, column, text};
use iced::{Alignment, Element, Length};
use iced::alignment;
use iced::widget::{button, container, stack};
use iced::{Element, Length, Theme};
pub fn main() -> iced::Result {
iced::program("Bezier Tool - Iced", Example::update, Example::view)
.theme(|_| Theme::CatppuccinMocha)
.antialiasing(true)
.run()
}
@ -35,16 +37,18 @@ impl Example {
}
fn view(&self) -> Element<Message> {
column![
text("Bezier tool example").width(Length::Shrink).size(50),
container(stack![
self.bezier.view(&self.curves).map(Message::AddCurve),
button("Clear")
.style(button::danger)
.on_press(Message::Clear),
]
container(
button("Clear")
.style(button::danger)
.on_press(Message::Clear)
)
.padding(10)
.width(Length::Fill)
.align_x(alignment::Horizontal::Right),
])
.padding(20)
.spacing(20)
.align_items(Alignment::Center)
.into()
}
}
@ -139,22 +143,24 @@ mod bezier {
&self,
state: &Self::State,
renderer: &Renderer,
_theme: &Theme,
theme: &Theme,
bounds: Rectangle,
cursor: mouse::Cursor,
) -> Vec<Geometry> {
let content =
self.state.cache.draw(renderer, bounds.size(), |frame| {
Curve::draw_all(self.curves, frame);
Curve::draw_all(self.curves, frame, theme);
frame.stroke(
&Path::rectangle(Point::ORIGIN, frame.size()),
Stroke::default().with_width(2.0),
Stroke::default()
.with_width(2.0)
.with_color(theme.palette().text),
);
});
if let Some(pending) = state {
vec![content, pending.draw(renderer, bounds, cursor)]
vec![content, pending.draw(renderer, theme, bounds, cursor)]
} else {
vec![content]
}
@ -182,7 +188,7 @@ mod bezier {
}
impl Curve {
fn draw_all(curves: &[Curve], frame: &mut Frame) {
fn draw_all(curves: &[Curve], frame: &mut Frame, theme: &Theme) {
let curves = Path::new(|p| {
for curve in curves {
p.move_to(curve.from);
@ -190,7 +196,12 @@ mod bezier {
}
});
frame.stroke(&curves, Stroke::default().with_width(2.0));
frame.stroke(
&curves,
Stroke::default()
.with_width(2.0)
.with_color(theme.palette().text),
);
}
}
@ -204,6 +215,7 @@ mod bezier {
fn draw(
&self,
renderer: &Renderer,
theme: &Theme,
bounds: Rectangle,
cursor: mouse::Cursor,
) -> Geometry {
@ -213,7 +225,12 @@ mod bezier {
match *self {
Pending::One { from } => {
let line = Path::line(from, cursor_position);
frame.stroke(&line, Stroke::default().with_width(2.0));
frame.stroke(
&line,
Stroke::default()
.with_width(2.0)
.with_color(theme.palette().text),
);
}
Pending::Two { from, to } => {
let curve = Curve {
@ -222,7 +239,7 @@ mod bezier {
control: cursor_position,
};
Curve::draw_all(&[curve], &mut frame);
Curve::draw_all(&[curve], &mut frame, theme);
}
};
}