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

Stack widget #2405

merged 6 commits into from
Apr 25, 2024

Conversation

hecrj
Copy link
Member

@hecrj hecrj commented Apr 24, 2024

This PR implements a simple Stack widget that can be used to display elements on top of each other.

The Stack uses the first element as its base layer and every consecutive element is displayed on top of the previous one. The base element defines the intrinsic size of the Stack, so the elements on top can easily position themselves using other existing widgets (e.g. container).

Like column and row, it comes with both its function and macro helpers.

The bezier_tool example showcases how we can use this new widget to show the "Clear" button at the top right corner of the Canvas:

container(stack![
    self.bezier.view(&self.curves).map(Message::AddCurve),
    container(
        button("Clear")
            .style(button::danger)
            .on_press(Message::Clear)
    )
    .padding(10)
    .width(Length::Fill)
    .align_x(alignment::Horizontal::Right),
])
.padding(20)
.into()

This produces (amazing art not included!):

image

hecrj added 2 commits April 25, 2024 01:39
It can be used to stack elements on top of each other!
@hecrj hecrj added this to the 0.13 milestone Apr 24, 2024
@hecrj hecrj added feature New feature or request widget layout addition labels Apr 24, 2024
@hecrj hecrj force-pushed the feature/stack-widget branch from 5acda41 to 9492da1 Compare April 25, 2024 00:27
let limits = layout::Limits::new(Size::ZERO, size);

let nodes = std::iter::once(base)
.chain(self.children[1..].iter().zip(&mut tree.children[1..]).map(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Possible index error? We only check is_empty

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It'll just return an empty slice, which is what we want.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How did I not know this? I guess the spread operator here protects it

Copy link
Member Author

@hecrj hecrj Apr 25, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep, it's a range equivalent to 1..self.children.len(). Thus, empty with a single child.

@hecrj hecrj merged commit 2d01d55 into master Apr 25, 2024
24 checks passed
@hecrj hecrj deleted the feature/stack-widget branch April 25, 2024 21:19
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants