-
Notifications
You must be signed in to change notification settings - Fork 788
C++: Expose raw window handle API #10214
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
base: master
Are you sure you want to change the base?
C++: Expose raw window handle API #10214
Conversation
862992e to
0f5b956
Compare
We had the raw_window_handle_06 feature and functions in Rust, but there wasn't an easy way to do the same in C++. This is especially useful for embedding windows (such as browsers, 3D views, etc.) or just wanting to access the platform's handles for other reasons. Similar to Rust, you need to enable a specific non-default feature for this API. I re-used the existing NativeWindowHandle struct for this, moving it out of slint-platform.h and adding getters to it.\
d542c70 to
53f2142
Compare
ogoffart
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the contribution. I think it indeed make sense to expose that, and it could be exposed to C++ without a feature.
| define_cargo_dependent_feature(testing "Enable support for testing API (experimental)" ON "NOT SLINT_FEATURE_FREESTANDING") | ||
| define_cargo_feature(experimental "Enable experimental features. (No backward compatibility guarantees)" OFF) | ||
| define_cargo_dependent_feature(system-testing "Enable system testing support (experimental)" OFF "SLINT_FEATURE_EXPERIMENTAL AND NOT SLINT_FEATURE_FREESTANDING") | ||
| define_cargo_feature(raw-window-handle-06 "Enable integration with raw-window-handle." OFF) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The reason it is a feature in Rust is because it is dependent of a specific Rust crate (raw-window-handle) and a specific version (0.6) and that in the future, Rust users may opt for a newer version of raw-window-handle and so wouldn't enable this feature anymore.
In C++ we don't have this as we'd have to re-expose the API anyway, so IMHO it shouldn't need to be a feature and it can be enabled by default
| } | ||
|
|
||
| /// Returns the NSView from this NativeWindowHandle. | ||
| NSView *appkit_view() const |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Don't we also need a getter for the NSWindow?
| } | ||
| } | ||
|
|
||
| #[cfg(feature = "raw-window-handle-06")] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
(maybe put things in a module to avoid repeating this too often?)
| } | ||
|
|
||
| #[cfg(feature = "raw-window-handle-06")] | ||
| unsafe impl std::marker::Send for RawHandlePair {} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This could get safety comment. There was a comment before
| } | ||
| } | ||
|
|
||
| NativeWindowHandle native_window_handle() const |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actually, looking again at my comment in #6248 (comment) , I wonder if we really need this NativeWindowHandle indirection and can just have things like win32_handle() directly on the Window.
@tronical: opinions?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think you're right. That would make for a simpler API. Just one function per platform (two for linux).
We had the raw_window_handle_06 feature and functions in Rust, but there wasn't an easy way to do the same in C++. This is especially useful for embedding windows (such as browsers, 3D views, etc.) or just wanting to access the platform's handles for other reasons.
Similar to Rust, you need to enable a specific non-default feature for this API. I re-used the existing NativeWindowHandle struct for this, moving it out of slint-platform.h and adding getters to it.
Examples
TODO
Notes
I intentionally only added getters for Wayland/Appkit/Win32 since that's the only platforms that currently matter to me. Since we didn't have a C++ API at all before that's still a net positive, and this can be easily extended later.
It should be safe to call this even if the window isn't initialized, or on the wrong platform.