slint-ui / slint

Slint is a declarative GUI toolkit to build native user interfaces for Rust, C++, or JavaScript apps.
https://slint.dev
Other
16.94k stars 568 forks source link

Create Pythonic API to load Slint components #4134

Closed tronical closed 6 months ago

tronical commented 9 months ago

The objective of this task is explore the possibility of providing dynamically generated types for “.slint” files. Beyond classic objects with properties, we can experiment with dynamic imports like in the popular “sh” module. We should review this API with experienced Pythonistas in public discussions.

This work will be sponsored by the NLnet foundation.

Edit: The API review makes most sense after the documentation is up and the rest of the project is ready for review, so let's exclude that from the scope of this issue and include it in the before the final release.

The basic API that's implemented now can be summarized like this:

An example of all this in action can be found in https://github.com/slint-ui/slint/blob/master/examples/printerdemo/python/README.md

Overall README: https://github.com/slint-ui/slint/tree/master/api/python

tronical commented 6 months ago

The initial load_file function is added, which exposes instances as objects with properties and callbacks (as on-prefixed properties). The dynamic import is missing still.

tronical commented 6 months ago

One way for the dynamic import would be to allow for writing an __init__.py stub that associates a directory with a specific .slint file, and then allow for writing:

Support ui/__init__.py referenced ui/main_window.slint:

from ui import MainWindow

window = MainWindow()
window.show();
tronical commented 6 months ago

Without __init__.py a neater way might be to add a finder/loader to sys.meta_path and support import foo, which loads foo.slint.

Example:

foo.slint:

export component MainWindow { ... }

blah.py:

import slint
import foo

main_window = foo.MainWindow();
main_window.run()

(this would also work of course with import foo as bar or import MainWindow from foo)

tronical commented 6 months ago

I've updated the description with a summary of the implemented API. Closing this as the initial port being done. Documentation is still missing, but that's part of a milestone that was agreed on separately.