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

Widget IDs? (for usability, persistence, animations) ("explicit identity") #318

Open
makoConstruct opened this issue May 26, 2024 · 2 comments

Comments

@makoConstruct
Copy link

makoConstruct commented May 26, 2024

In SwiftUI and Flutter, you're able to assign each widget an ID, which it will use to track the widgets between widget tree renders, which is good for two kinda big things:

  • Maintaining focus between renders. If a widget was in focus last render, the framework will know whether that widget is still around and make sure it's still in focus afterwards.
  • Animating the movement of the widgets. This has moderate usability benefits, as sudden changes to the content that aren't animated will usually be pretty confusing the first time the user encounters them, they wont know exactly what just happened. But it also, you know, looks and feels really great.

This is a situation where I'd really want an optional id parameter, but rust has alternatives to optional parameters (chainable setters (Widget::set_global_id(self, Box::new(ProfileID)): Widget)).

IDs can be local or global. A local ID will only be tracked against its siblings (the framework will assume that it wont leave that nesting level), and a global ID will be tracked wherever it goes in the tree (which requires more strictness in maintaining uniqueness, so you wouldn't do it by default). A local child of a global parent will be tracked even if the global parent moves.

To aid global uniqueness, it would be good to make use of Any stuff to use the type of the ID as a discriminator. For example, struct ProfileID; could be a type that was defined for one widget in the entire application, which effectively uses the compiler to maintain a guarantee of uniqueness from other type ids.

@Philipp-M
Copy link
Contributor

Philipp-M commented May 31, 2024

I'm not sure if I get what you mean, and whether Xilem or masonry is meant.
Xilem itself is using the type-system via the View trait to check whether the widgets have changed and retains the widget state (including focus), but as you note, it doesn't track tree-structure changes currently of views which are moved somewhere else in the view-tree.

I think you mean explicit identity of Views (and their respective Widgets), which I think is planned.

See also Demystify SwiftUI for more information (explicit identity starts around 5:00).

@makoConstruct
Copy link
Author

Explicit identity, yes, that's it.

@makoConstruct makoConstruct changed the title Widget IDs? (for usability, persistence, animations) Widget IDs? (for usability, persistence, animations) ("explicit identity") May 31, 2024
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

No branches or pull requests

2 participants