Skip to content

Stack widget#2405

Merged
hecrj merged 6 commits into
masterfrom
feature/stack-widget
Apr 25, 2024
Merged

Stack widget#2405
hecrj merged 6 commits into
masterfrom
feature/stack-widget

Conversation

@hecrj

@hecrj hecrj commented Apr 24, 2024

Copy link
Copy Markdown
Member

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
Comment thread widget/src/stack.rs
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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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

@hecrj hecrj Apr 25, 2024

Copy link
Copy Markdown
Member Author

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
@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