Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Stack widget #2405

Merged
merged 6 commits into from
Apr 25, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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
27 changes: 26 additions & 1 deletion widget/src/helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ use crate::text_input::{self, TextInput};
use crate::toggler::{self, Toggler};
use crate::tooltip::{self, Tooltip};
use crate::vertical_slider::{self, VerticalSlider};
use crate::{Column, MouseArea, Row, Space, Themer};
use crate::{Column, MouseArea, Row, Space, Stack, Themer};

use std::borrow::Borrow;
use std::ops::RangeInclusive;
Expand Down Expand Up @@ -52,6 +52,19 @@ macro_rules! row {
);
}

/// Creates a [`Stack`] with the given children.
///
/// [`Stack`]: crate::Stack
#[macro_export]
macro_rules! stack {
() => (
$crate::Stack::new()
);
($($x:expr),+ $(,)?) => (
$crate::Stack::with_children([$($crate::core::Element::from($x)),+])
);
}

/// Creates a new [`Container`] with the provided content.
///
/// [`Container`]: crate::Container
Expand Down Expand Up @@ -98,6 +111,18 @@ where
Row::with_children(children)
}

/// Creates a new [`Stack`] with the given children.
///
/// [`Stack`]: crate::Stack
pub fn stack<'a, Message, Theme, Renderer>(
children: impl IntoIterator<Item = Element<'a, Message, Theme, Renderer>>,
) -> Stack<'a, Message, Theme, Renderer>
where
Renderer: core::Renderer,
{
Stack::with_children(children)
}

/// Creates a new [`Scrollable`] with the provided content.
///
/// [`Scrollable`]: crate::Scrollable
Expand Down
3 changes: 3 additions & 0 deletions widget/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ mod column;
mod mouse_area;
mod row;
mod space;
mod stack;
mod themer;

pub mod button;
Expand Down Expand Up @@ -78,6 +79,8 @@ pub use slider::Slider;
#[doc(no_inline)]
pub use space::Space;
#[doc(no_inline)]
pub use stack::Stack;
#[doc(no_inline)]
pub use text::Text;
#[doc(no_inline)]
pub use text_editor::TextEditor;
Expand Down
Loading
Loading