-
Notifications
You must be signed in to change notification settings - Fork 115
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add padding attribute to
sized_box
view (#736)
## 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 ```rs 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. <img width="912" alt="Screenshot 2024-11-10 at 16 22 52" src="https://github.com/user-attachments/assets/ce5fd4e6-412b-46c1-9387-6886ef97e653"> ## Discussion ### Rename `sized_box` to `frame`? In swiftUI the view modifier [`.frame()`](https://developer.apple.com/documentation/swiftui/view/frame(width:height:alignment:)) 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`](https://github.com/linebender/xilem/blob/62588565692584e16197fb6d19cd3b41c104b675/xilem/src/view/flex.rs#L340C11-L340C18) and [`GridExt`](https://github.com/linebender/xilem/blob/62588565692584e16197fb6d19cd3b41c104b675/xilem/src/view/grid.rs#L248), I was thinking that a new `SizedBoxExt` could be introduced to easily wrap any view in a `sized_box`, something like the following. ```rust 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: ```rust 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: ```rust 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.) ```
- Loading branch information
1 parent
41207ef
commit 9ee0280
Showing
10 changed files
with
229 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
3 changes: 3 additions & 0 deletions
3
...ts/masonry__widget__sized_box__tests__label_box_with_background_and_padding.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
3 changes: 3 additions & 0 deletions
3
...screenshots/masonry__widget__sized_box__tests__label_box_with_outer_padding.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
3 changes: 3 additions & 0 deletions
3
...idget/screenshots/masonry__widget__sized_box__tests__label_box_with_padding.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
7 changes: 7 additions & 0 deletions
7
masonry/src/widget/snapshots/masonry__widget__sized_box__tests__label_box_with_padding.snap
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
--- | ||
source: masonry/src/widget/sized_box.rs | ||
expression: harness.root_widget() | ||
--- | ||
SizedBox( | ||
Label<hello>, | ||
) |
7 changes: 7 additions & 0 deletions
7
...t/snapshots/masonry__widget__sized_box__tests__label_box_with_padding_and_background.snap
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
--- | ||
source: masonry/src/widget/sized_box.rs | ||
expression: harness.root_widget() | ||
--- | ||
SizedBox( | ||
Label<hello>, | ||
) |
9 changes: 9 additions & 0 deletions
9
...c/widget/snapshots/masonry__widget__sized_box__tests__label_box_with_padding_outside.snap
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
--- | ||
source: masonry/src/widget/sized_box.rs | ||
expression: harness.root_widget() | ||
--- | ||
SizedBox( | ||
SizedBox( | ||
Label<hello>, | ||
), | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.