-
Notifications
You must be signed in to change notification settings - Fork 599
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
Encapsulate widgets with rust logic #2031
Comments
Yes, at the moment you need to either forward it like that or you can also direct this through globals - that way you don't need to direct it all the way, but at the same time it's not per-instance. I hope that the groundwork for #1726 will also allow us to have a better per-instance encapsulation of native (Rust) logic and Slint components. #784 is also required I think for more modular application setups. |
For clarification, I think that this is a valid issue and the acceptance criteria would be to build on the linked issues implement support for encapsulation of elements that come with native code, document it, and perhaps use it in one of our examples. |
Okay, thank you :) |
I think the linked issue by @tronical are orthogonal to this.
And then initialize the callbacks on LoginScreenLogic so that the LoginScreen can be used by the rest of the slint code. But there is room for improvements there |
@ogoffart I think something like that would be fine for small applications but is miles away from being an acceptable solution for complex ones. Compared to other frameworks of course |
Olivier and I keep discussing this topic and iterating - it's a tricky problem that spans from the language syntax, to API, all the way to developr ergonomics (diagnostics when something goes wrong). In our latest iteration we're thinking of experimenting with the following approach. I'll explain by example: Suppose you want to write a Let's draft that in Slint code:
We'd annotate a component with When compiling to Rust, we could provide a trait that the user needs to implement. Let's call it // The Rust generated code for the internal VideoPlayer component expects `CustomInit` to be implemented, and
// internally would call init in its constructor to invoke the user code:
// ...
//. <Self as slint::CustomInit>::init(self>;
// ...
//
impl slint::CustomInit for VideoPlayer {
fn init(&self) {
self.set_native_data(Box::new(FFmpegVideoPlayer::new());
// Note how the callback takes a &Self argument, so that it's easily accessible
self.on_play(|player: &Self| {
self.native_data().downcast_ref::<FFmpegVideoPlayer>().play();
self.set_current_time(...);
});
}
} In C++ we would use ADL to let the compiler find the user supplied function: A catch-all in void slint_custom_init(auto& ) {
static_assert(`false, "You must declare a slint_custom_init function for the types that have @native-init");
} and then, before including the Slint generated code void slint_custom_init(const VideoPlayer &) {
} For the interpreter API it would have to be entirely dynamic, so there'd have to be a Note: This is somewhat orthogonal to the question of how to make the compilation itself modular, i.e. we still want the ability in the future to take a bunch of .slint files along with say Rust code, place it in a crate, compile it, and then use that crate and .slint files in the application. |
I will experiment with this as part of the 1.3 release cycle. If the experiment succeeds, then I think this can be resolved within the release. If the experiment fails (i.e. requires further research, not happy with the resulting API/approach), then we'll descope it from 1.3. |
Hey, I've read the documentation and examples, but couldn't quite figure out what the proper approach for more complex applications looks like.
Let's say, that I have some application with multiple screens using Rust. From what I understand, if I want to add some logic using rust, I have to do it via callbacks on the window?
Is there no way to create a widget using the slint language, add some logic using rust and then use that Widget, with the rust logic, within another slint language widget/window?
Having a single point to add the rust logic feels extremely inconvenient to me.
I would have imagined something like this: (pseudocode):
Is adding the logic via callbacks to the window and then forwarding these to the screens the only way?
The text was updated successfully, but these errors were encountered: