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

Add padding attribute to sized_box view #736

Merged
merged 7 commits into from
Nov 12, 2024

Conversation

viktorstrate
Copy link
Contributor

@viktorstrate viktorstrate commented Nov 10, 2024

Changes

Masonry

  • Added new padding attribute to sized_box Masonry widget.
  • 3 unit tests demonstrating how padding is used.

Xilem

  • Added Padding struct with convenience methods for working with paddings.
  • Updated http_cats example to use padding when more convenient and fixed hack.

Examples

sized_box(label("hello world")).padding(10.) // Equal padding on all edges
sized_box(label("hello world")).padding(Padding::top(10.)) // Padding only on top edge
sized_box(label("hello world")).padding((10., 20., 30., 40.)) // Different padding for each edge

HTTP Cats

Added padding on the left in the http_cats example, to make it more balanced with the right side.
Screenshot 2024-11-10 at 16 22 52

Discussion

Rename sized_box to frame?

In swiftUI the view modifier .frame() is used to change the size of a view. I think the name frame better describes the purpose of sized_box, since it also does backgrounds, borders (and now padding). So I wanted to suggest that sized_box be renamed to frame.

Add SizedBoxExt for better ergonomics?

Similar to FlexExt and GridExt, I was thinking that a new SizedBoxExt could be introduced to easily wrap any view in a sized_box, something like the following.

pub trait SizedBoxExt<State, Action>: WidgetView<State, Action> {
  fn sized_box(self) -> SizedBox<Self, State, Action> {
    sized_box(self)
  }
}

This would allow for chaining modifiers like this:

label("Hello world")
  .text_size(20.)
  .sized_box() // After this padding, background, width, height can be added
  .padding(20.)

Or even somthing more advanced like this:

label("Hello world")
  .sized_box()
  .padding(20.)
  .background(Color::Teal)
  .border(Color::Blue, 4.)
  .sized_box() // By wrapping in another sized box we add another border rather than overwriting the previous one
  .border(Color::Magenta, 4.)

- Also add Padding struct with convenience methods for working with paddings.
- Updated `http_cats` example to use padding when more convenient.
Copy link
Member

@DJMcNab DJMcNab left a comment

Choose a reason for hiding this comment

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

This looks really great, thank you! There are a few things I'd like to see changed before this lands, but this is idiomatic for how Xilem/Masonry are currently designed. I think the behaviour with dynamic paddings is broken, which I'd like to see addressed (or a convincing check that it isn't).

The suggested name of Frame is certainly compelling, but should be a follow-up.


let mut harness = TestHarness::create(widget);

assert_debug_snapshot!(harness.root_widget());
Copy link
Member

Choose a reason for hiding this comment

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

I'm not sure these debug snapshot tests are needed here.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Should I remove them? I just copied them from the other test cases in the file.

Copy link
Member

Choose a reason for hiding this comment

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

We can leave them from consistency. @PoignardAzur what are these snapshots actually testing?

masonry/src/widget/sized_box.rs Outdated Show resolved Hide resolved
masonry/src/widget/sized_box.rs Outdated Show resolved Hide resolved
masonry/src/widget/sized_box.rs Outdated Show resolved Hide resolved
xilem/examples/http_cats.rs Outdated Show resolved Hide resolved
xilem/src/view/sized_box.rs Outdated Show resolved Hide resolved
xilem/src/view/sized_box.rs Outdated Show resolved Hide resolved
xilem/src/view/sized_box.rs Outdated Show resolved Hide resolved
xilem/src/view/sized_box.rs Outdated Show resolved Hide resolved
xilem/src/view/sized_box.rs Outdated Show resolved Hide resolved
xilem/examples/http_cats.rs Outdated Show resolved Hide resolved
xilem/examples/http_cats.rs Outdated Show resolved Hide resolved
Copy link
Member

@DJMcNab DJMcNab left a comment

Choose a reason for hiding this comment

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

Looks good. Some tweaks to the docs and ordering in the file are still needed, but this is nearly ready.

masonry/src/widget/sized_box.rs Outdated Show resolved Hide resolved
masonry/src/widget/sized_box.rs Outdated Show resolved Hide resolved
masonry/src/widget/sized_box.rs Outdated Show resolved Hide resolved
masonry/src/widget/sized_box.rs Outdated Show resolved Hide resolved
}

impl From<f64> for Padding {
/// Converts the value to a [Padding] object with that amount of padding on all edges.
Copy link
Member

Choose a reason for hiding this comment

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

I've looked at the generated docs page, and these are really hard to discover.

These docs are in the right place, but we should also mention these impls at the top level of Padding

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I'm unsure how to best do this?

Copy link
Member

Choose a reason for hiding this comment

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

As in, adding some text to the doc comment on Padding describing that these implementations exist for convenience?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Copy link
Member

Choose a reason for hiding this comment

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

I'd probably choose slightly different wording, but that form looks good. Thanks!

xilem/src/view/sized_box.rs Outdated Show resolved Hide resolved
xilem/src/view/sized_box.rs Show resolved Hide resolved
@Philipp-M
Copy link
Contributor

Rename sized_box to frame?

I'm in favor, but I think this should be a follow-up PR.

Add SizedBoxExt for better ergonomics?

I guess we can do that as well. (Could also be a follow-up)

Copy link
Member

@DJMcNab DJMcNab left a comment

Choose a reason for hiding this comment

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

The unresolved comments still apply (#736 (comment) and #736 (comment)), plus this new one.

Otherwise this is really good, thanks!

xilem/examples/http_cats.rs Outdated Show resolved Hide resolved
}

impl From<f64> for Padding {
/// Converts the value to a [Padding] object with that amount of padding on all edges.
Copy link
Member

Choose a reason for hiding this comment

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

As in, adding some text to the doc comment on Padding describing that these implementations exist for convenience?

@DJMcNab DJMcNab added this pull request to the merge queue Nov 12, 2024
@DJMcNab
Copy link
Member

DJMcNab commented Nov 12, 2024

And yes, the two suggested follow-ups (probably renaming to Frame first) would be welcome.

Merged via the queue into linebender:main with commit 9ee0280 Nov 12, 2024
17 checks passed
@viktorstrate viktorstrate deleted the sized_box-padding branch November 12, 2024 14:13
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants