Skip to content

Commit

Permalink
Merge pull request #2405 from iced-rs/feature/stack-widget
Browse files Browse the repository at this point in the history
`Stack` widget
  • Loading branch information
hecrj authored Apr 25, 2024
2 parents 5ef593c + 4fc342c commit 2d01d55
Show file tree
Hide file tree
Showing 17 changed files with 609 additions and 359 deletions.
2 changes: 1 addition & 1 deletion core/src/layout.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ impl<'a> Layout<'a> {
}

/// Returns an iterator over the [`Layout`] of the children of a [`Node`].
pub fn children(self) -> impl Iterator<Item = Layout<'a>> {
pub fn children(self) -> impl DoubleEndedIterator<Item = Layout<'a>> {
self.node.children().iter().map(move |node| {
Layout::with_offset(
Vector::new(self.position.x, self.position.y),
Expand Down
1 change: 1 addition & 0 deletions core/src/mouse/interaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#[allow(missing_docs)]
pub enum Interaction {
#[default]
None,
Idle,
Pointer,
Grab,
Expand Down
2 changes: 1 addition & 1 deletion core/src/overlay.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ where
_viewport: &Rectangle,
_renderer: &Renderer,
) -> mouse::Interaction {
mouse::Interaction::Idle
mouse::Interaction::None
}

/// Returns true if the cursor is over the [`Overlay`].
Expand Down
2 changes: 1 addition & 1 deletion core/src/widget.rs
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ where
_viewport: &Rectangle,
_renderer: &Renderer,
) -> mouse::Interaction {
mouse::Interaction::Idle
mouse::Interaction::None
}

/// Returns the overlay of the [`Widget`], if there is any.
Expand Down
53 changes: 35 additions & 18 deletions examples/bezier_tool/src/main.rs
Original file line number Diff line number Diff line change
@@ -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()
}
Expand Down Expand Up @@ -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()
}
}
Expand Down Expand Up @@ -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]
}
Expand Down Expand Up @@ -182,15 +188,20 @@ 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);
p.quadratic_curve_to(curve.control, curve.to);
}
});

frame.stroke(&curves, Stroke::default().with_width(2.0));
frame.stroke(
&curves,
Stroke::default()
.with_width(2.0)
.with_color(theme.palette().text),
);
}
}

Expand All @@ -204,6 +215,7 @@ mod bezier {
fn draw(
&self,
renderer: &Renderer,
theme: &Theme,
bounds: Rectangle,
cursor: mouse::Cursor,
) -> Geometry {
Expand All @@ -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 {
Expand All @@ -222,7 +239,7 @@ mod bezier {
control: cursor_position,
};

Curve::draw_all(&[curve], &mut frame);
Curve::draw_all(&[curve], &mut frame, theme);
}
};
}
Expand Down
2 changes: 1 addition & 1 deletion examples/loupe/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ mod loupe {
if cursor.is_over(layout.bounds()) {
mouse::Interaction::ZoomIn
} else {
mouse::Interaction::Idle
mouse::Interaction::None
}
}
}
Expand Down
Loading

0 comments on commit 2d01d55

Please sign in to comment.