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

Implement TaffyLayout widget #140

Merged
merged 7 commits into from
Nov 28, 2023
Merged

Implement TaffyLayout widget #140

merged 7 commits into from
Nov 28, 2023

Conversation

nicoburns
Copy link
Contributor

@nicoburns nicoburns commented Nov 1, 2023

Objective

Enable Taffy based layout to be used with Xilem

Changes made

  • Gitignore .DS_Store files
  • Add compute_max_intrinsic method to Pod (this method already existed on the Widget trait)
  • Add downcast_ref method to Pod (there was already a downcast_mut method)
  • Added cx.request_paint() in the layout method of the button widget (otherwise it wasn't repainting on window resize)
  • Create TaffyLayout widget and corresponding view (adapted from the LinearLayout). Constructors for the view currently include flex_row, flex_column, and div.
  • Create taffy example which uses the new view/widget.

Notes

  • For the most part this could have been implemented as a 3rd party widget. The only changes required outside of creating the view/widget themselves were minor and not taffy-specific.
  • This is currently depending on it's own branch of Taffy. But the changes are relatively minor (making Taffy pass along which Axis it is measuring when requesting child measurement), and I don't anticipate major issues getting them merged. (Pass axis information when measuring a child's size DioxusLabs/taffy#575) Changes now merged to Taffy's main branch. I would anticipate a few further minor changes, but the integration works as-is.
  • Caching is important for performance and is not implemented yet. There are no known blockers, just haven't gotten around to this yet.
  • There is scope for the API to be improved. Currently it just stores a whole taffy::style::Style on each widget. But it would possible to create more specialised widgets to reduce memory overhead.

Screenshot

Screenshot 2023-11-02 at 00 28 22

Copy link
Contributor

@jaredoconnell jaredoconnell left a comment

Choose a reason for hiding this comment

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

Is it possible to reduce the redundancy between layout and compute_max_intrinsic?
Seems like a good start to get stuff moving. The cache will be very important in the future.

@nicoburns nicoburns force-pushed the taffy-layout branch 2 times, most recently from 6a6ff39 to 07372b7 Compare November 11, 2023 00:39
Copy link
Contributor

@azymohliad azymohliad left a comment

Choose a reason for hiding this comment

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

It works great! Really looking forward to have this merged!

Cargo.toml Outdated Show resolved Hide resolved
src/view/taffy_layout.rs Outdated Show resolved Hide resolved
examples/taffy.rs Outdated Show resolved Hide resolved
@giannissc
Copy link
Contributor

giannissc commented Nov 21, 2023

I will finally get around to reviewing this today!

Caching is important for performance and is not implemented yet. There are no known blockers, just haven't gotten around to this yet.

Is this something that will be in a separate PR? @nicoburns

@nicoburns
Copy link
Contributor Author

Caching is important for performance and is not implemented yet. There are no known blockers, just haven't gotten around to this yet.

Is this something that will be in a separate PR? @nicoburns

I had a quick think about how this might be implemented, and I think it will need to be. Adding the cache is relatively straightforward. The thing I am currently blocked on is cache invalidation. If the styles of a view/widget change, then the cache would need to be invalidated for that widget and all parents. And I'm not currently sure how to do that? (perhaps a message / event - but how does one send one of those?). It would also be possible to forgo caching between multiple layout runs (and only have it within a single run) and somehow have a "prelayout" or "postlayout" event in which we drop the caches.

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.

I'd be mostly happy to land this if the taffy feature were added.

I think it would be fine as not optional, but in that case a few things need to be addressed

.gitignore Show resolved Hide resolved
@@ -108,6 +108,7 @@ impl Widget for Button {
);
self.layout = Some(layout);
//(Size::new(10.0, min_height), size)
cx.request_paint();
Copy link
Member

Choose a reason for hiding this comment

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

Should this be automatic/implied?

If the widget is being re-layed out, it seems surprising that it would ever want to not be repainted.

I guess the case would be if it hasn't resized. But in that case, should we be resizing here?

Copy link
Contributor Author

@nicoburns nicoburns Nov 22, 2023

Choose a reason for hiding this comment

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

Possibly... I was calling it unconditionally, but now that I've added layout caching it actually doesn't get called if there is a cache hit. I'm not sure whether this would ever save a meaningful amount of work.

Cargo.toml Outdated Show resolved Hide resolved
src/view/taffy_layout.rs Show resolved Hide resolved
src/view/taffy_layout.rs Outdated Show resolved Hide resolved
}

/// creates a horizontal [`TaffyLayout`].
pub fn div<T, A, VT: ViewSequence<T, A>>(children: VT) -> TaffyLayout<T, A, VT> {
Copy link
Member

Choose a reason for hiding this comment

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

Div isn't a very compelling name. If there's an alternative you can think of, I'd like to change to it

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Hmm... it's called div because that's the name on web, and I can't think of why you'd want to use block layout unless you're emulating web. I could just remove it entirely? Or call it something like raw_taffy_layout?

src/widget/taffy_layout.rs Show resolved Hide resolved
@nicoburns
Copy link
Contributor Author

nicoburns commented Nov 22, 2023

@giannissc

Caching is important for performance and is not implemented yet. There are no known blockers, just haven't gotten around to this yet.

Is this something that will be in a separate PR?

Some basic caching is now implemented. It really ought to be implemented for the LinearLayout and Text widgets too, but I think those implementations can be in follow up PRs.

@nicoburns nicoburns force-pushed the taffy-layout branch 4 times, most recently from d9ee16f to fde5adb Compare November 23, 2023 19:26
@nicoburns
Copy link
Contributor Author

Is it possible to reduce the redundancy between layout and compute_max_intrinsic? Seems like a good start to get stuff moving. The cache will be very important in the future.

Not easily. As things stand you could combine them into one method with a parameter instead. But I would expect these methods to further diverge over time, and IMO this would already be more confusing than a small amount of duplication.

@nicoburns
Copy link
Contributor Author

I believe I've addressed all of the review feedback, so this is ready for re-review. I've also made #146 independent of this PR if anybody has time to review that (which is hopefully a little less controversial than this one).

Copy link
Contributor

@richard-uk1 richard-uk1 left a comment

Choose a reason for hiding this comment

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

Fab. Sorry no-one has reviewed until now.

@nicoburns nicoburns added this pull request to the merge queue Nov 28, 2023
Merged via the queue into main with commit c439885 Nov 28, 2023
7 checks passed
@DJMcNab DJMcNab deleted the taffy-layout branch April 28, 2024 16:29
@jaredoconnell jaredoconnell mentioned this pull request Oct 17, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants