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

Add support for raw-window-handle #4918

Merged
merged 7 commits into from
Mar 22, 2024
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/build_docs.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ jobs:
- name: Set up rgb crate rustdoc link
run: |
rgb_version=`grep 'rgb = ' internal/core/Cargo.toml | sed 's/^.*"\(.*\)"/\1/'`
echo "RUSTDOCFLAGS=$RUSTDOCFLAGS --extern-html-root-url rgb=https://docs.rs/rgb/$rgb_version/ --extern-html-root-url android_activity=https://docs.rs/android-activity/0.5/ -Z unstable-options" >> $GITHUB_ENV
echo "RUSTDOCFLAGS=$RUSTDOCFLAGS --extern-html-root-url rgb=https://docs.rs/rgb/$rgb_version/ --extern-html-root-url android_activity=https://docs.rs/android-activity/0.5/ --extern-html-root-url raw_window_handle=https://docs.rs/raw_window_handle/0.6 -Z unstable-options" >> $GITHUB_ENV
- uses: ./.github/actions/install-nodejs
- name: Cache mdbook and mdbook-linkcheck
uses: actions/cache@v4
Expand Down
2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,8 @@ strum = { version = "0.26.1", default-features = false, features = ["derive"] }
toml_edit = { version = "0.22.7" }
cfg_aliases = { version = "0.2.0" }

raw-window-handle-06 = { package = "raw-window-handle", version = "0.6", features = ["alloc"] }

[profile.release]
lto = true
panic = "abort"
Expand Down
8 changes: 8 additions & 0 deletions api/rs/slint/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,12 @@ unsafe-single-threaded = ["i-slint-core/unsafe-single-threaded"]
## APIs to support screen readers and other assistive technologies.
accessibility = ["i-slint-backend-selector/accessibility"]

## Enable integration with [raw-window-handle](raw_window_handle_06) version 0.6. This provides a
## [`Window::window_handle()`] function that returns a struct that implements
## [HasWindowHandle](raw_window_handle_06::HasWindowHandle) and
## [HasDisplayHandle](raw_window_handle_06::HasDisplayHandle) implementation.
raw-window-handle-06 = ["dep:raw-window-handle-06", "i-slint-backend-selector/raw-window-handle-06"]
tronical marked this conversation as resolved.
Show resolved Hide resolved

#! ### Backends

#! Slint needs a backend that will act as liaison between Slint and the OS.
Expand Down Expand Up @@ -175,6 +181,8 @@ num-traits = { version = "0.2", default-features = false }

log = { version = "0.4.17", optional = true }

raw-window-handle-06 = { workspace = true, optional = true }

[target.'cfg(not(target_os = "android"))'.dependencies]
# FemtoVG is disabled on android because it doesn't compile without setting RUST_FONTCONFIG_DLOPEN=on
# end even then wouldn't work because it can't load fonts
Expand Down
2 changes: 2 additions & 0 deletions internal/backends/selector/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ renderer-software = ["i-slint-backend-winit?/renderer-software", "i-slint-core/s
rtti = ["i-slint-core/rtti", "i-slint-backend-qt?/rtti"]
accessibility = ["i-slint-backend-winit?/accessibility"]

raw-window-handle-06 = ["i-slint-core/raw-window-handle-06", "i-slint-backend-winit?/raw-window-handle-06"]

# note that default enable the i-slint-backend-qt, but not its enable feature
default = ["i-slint-backend-qt", "backend-winit"]

Expand Down
5 changes: 4 additions & 1 deletion internal/backends/winit/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ renderer-skia-opengl = ["renderer-skia", "i-slint-renderer-skia/opengl"]
renderer-skia-vulkan = ["renderer-skia", "i-slint-renderer-skia/vulkan"]
renderer-software = ["dep:softbuffer", "dep:imgref", "dep:rgb", "i-slint-core/software-renderer-systemfonts", "dep:bytemuck", "winit/rwh_05"]
accessibility = ["dep:accesskit", "dep:accesskit_winit"]
raw-window-handle-06 = ["dep:raw-window-handle-06", "winit/rwh_06"]
default = []

[dependencies]
Expand Down Expand Up @@ -58,6 +59,8 @@ imgref = { version = "1.6.1", optional = true }
rgb = { version = "0.8.27", optional = true }
bytemuck = { workspace = true, optional = true, features = ["derive"] }

raw-window-handle-06 = { workspace = true, optional = true }

[target.'cfg(target_arch = "wasm32")'.dependencies]
web-sys = { version = "0.3", features=["HtmlInputElement", "HtmlCanvasElement", "Window", "Document", "Event", "KeyboardEvent", "InputEvent", "CompositionEvent", "DomStringMap", "ClipboardEvent", "DataTransfer"] }
wasm-bindgen = { version = "0.2" }
Expand All @@ -77,7 +80,7 @@ cocoa = { version = "0.25.0" }
cfg_aliases = { workspace = true }

[dev-dependencies]
slint = { path = "../../../api/rs/slint", default-features = false, features = ["std", "compat-1-2", "backend-winit", "renderer-software"] }
slint = { path = "../../../api/rs/slint", default-features = false, features = ["std", "compat-1-2", "backend-winit", "renderer-software", "raw-window-handle-06"] }

[package.metadata.docs.rs]
features = ["wayland", "renderer-software"]
6 changes: 5 additions & 1 deletion internal/backends/winit/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -408,11 +408,15 @@ mod testui {
// Sorry, can't test with rust test harness and multiple threads.
#[cfg(not(any(target_arch = "wasm32", target_os = "macos", target_os = "ios")))]
#[test]
fn test_window_accessor() {
fn test_window_accessor_and_rwh() {
slint::platform::set_platform(Box::new(crate::Backend::new().unwrap())).unwrap();

use testui::*;
let app = App::new().unwrap();
let slint_window = app.window();
assert!(slint_window.has_winit_window());
let handle = slint_window.window_handle();
use raw_window_handle_06::{HasDisplayHandle, HasWindowHandle};
assert!(handle.window_handle().is_ok());
assert!(handle.display_handle().is_ok());
}
14 changes: 14 additions & 0 deletions internal/backends/winit/winitwindowadapter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -617,6 +617,20 @@ impl WindowAdapter for WinitWindowAdapter {
fn internal(&self, _: corelib::InternalToken) -> Option<&dyn WindowAdapterInternal> {
Some(self)
}

#[cfg(feature = "raw-window-handle-06")]
fn window_handle_06(
&self,
) -> Result<raw_window_handle_06::WindowHandle<'_>, raw_window_handle_06::HandleError> {
raw_window_handle_06::HasWindowHandle::window_handle(&self.winit_window)
}

#[cfg(feature = "raw-window-handle-06")]
fn display_handle_06(
&self,
) -> Result<raw_window_handle_06::DisplayHandle<'_>, raw_window_handle_06::HandleError> {
raw_window_handle_06::HasDisplayHandle::display_handle(&self.winit_window)
}
}

impl WindowAdapterInternal for WinitWindowAdapter {
Expand Down
8 changes: 6 additions & 2 deletions internal/core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ libm = ["num-traits/libm", "euclid/libm"]
# Allow the viewer to query at runtime information about item types
rtti = []
# Use the standard library
std = ["euclid/std", "once_cell/std", "scoped-tls-hkt", "lyon_path", "lyon_algorithms", "lyon_geom", "lyon_extra", "dep:web-time", "image-decoders", "svg"]
std = ["euclid/std", "once_cell/std", "scoped-tls-hkt", "lyon_path", "lyon_algorithms", "lyon_geom", "lyon_extra", "dep:web-time", "image-decoders", "svg", "raw-window-handle-06?/std"]
# Unsafe feature meaning that there is only one core running and all thread_local are static.
# You can only enable this feature if you are sure that any API of this crate is only called
# from a single core, and not in a interrupt or signal handler.
Expand All @@ -44,6 +44,8 @@ box-shadow-cache = []

shared-fontdb = ["i-slint-common/shared-fontdb"]

raw-window-handle-06 = ["dep:raw-window-handle-06"]

default = ["std", "unicode"]

[dependencies]
Expand Down Expand Up @@ -86,6 +88,8 @@ resvg = { workspace = true, optional = true }
fontdb = { workspace = true, optional = true }
serde = { version = "1.0.163", features = ["derive"], optional = true }

raw-window-handle-06 = { workspace = true, optional = true }

[target.'cfg(target_family = "unix")'.dependencies]
gettext-rs = { version = "0.7", optional = true, features = ["gettext-system"] }

Expand All @@ -107,4 +111,4 @@ ttf-parser = "0.20.0"
fontdb = { workspace = true, default-features = true }
serde_json = "1.0.96"
tiny-skia = "0.11.0"
tokio = { version = "1.35", features = ["rt-multi-thread"] }
tokio = { version = "1.35", features = ["rt-multi-thread"] }
35 changes: 35 additions & 0 deletions internal/core/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -324,6 +324,34 @@ pub enum SetRenderingNotifierError {
AlreadySet,
}

/// This struct represents a persistent handle to a window and implements the
/// [`raw_window_handle_06::HasWindowHandle`] and [`raw_window_handle_06::HasDisplayHandle`]
/// traits for accessing exposing raw window and display handles.
/// Obtain an instance of this by calling [`Window::window_handle()`].
#[cfg(feature = "raw-window-handle-06")]
#[derive(Clone)]
pub struct WindowHandle {
tronical marked this conversation as resolved.
Show resolved Hide resolved
adapter: alloc::rc::Rc<dyn WindowAdapter>,
}

#[cfg(feature = "raw-window-handle-06")]
impl raw_window_handle_06::HasWindowHandle for WindowHandle {
fn window_handle<'a>(
&'a self,
) -> Result<raw_window_handle_06::WindowHandle<'a>, raw_window_handle_06::HandleError> {
self.adapter.window_handle_06()
}
}

#[cfg(feature = "raw-window-handle-06")]
impl raw_window_handle_06::HasDisplayHandle for WindowHandle {
fn display_handle<'a>(
&'a self,
) -> Result<raw_window_handle_06::DisplayHandle<'a>, raw_window_handle_06::HandleError> {
self.adapter.display_handle_06()
}
}

/// This type represents a window towards the windowing system, that's used to render the
/// scene of a component. It provides API to control windowing system specific aspects such
/// as the position on the screen.
Expand Down Expand Up @@ -571,6 +599,13 @@ impl Window {
pub fn is_visible(&self) -> bool {
self.0.is_visible()
}

/// Returns a struct that implements the raw window handle traits to access the windowing system specific window
/// and display handles. This function is only accessible if you enable the `raw-window-handle-06` crate feature.
#[cfg(feature = "raw-window-handle-06")]
pub fn window_handle(&self) -> WindowHandle {
WindowHandle { adapter: self.0.window_adapter() }
}
}

pub use crate::SharedString;
Expand Down
2 changes: 1 addition & 1 deletion internal/core/translations.rs
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,7 @@ fn translate_gettext(string: &str, ctx: &str, domain: &str, n: i32, plural: &str
}

#[cfg(feature = "gettext-rs")]
/// Initialize the translation by calling the [`bindtextdomain`](https://man7.org/linux/man-pages/man3/bindtextdomain.3.html) function from gettext
/// Initialize the translation by calling the [`bindtextdomain`](https://man7.org/unix/man-pages/man3/bindtextdomain.3.html) function from gettext
tronical marked this conversation as resolved.
Show resolved Hide resolved
pub fn gettext_bindtextdomain(_domain: &str, _dirname: std::path::PathBuf) -> std::io::Result<()> {
#[cfg(target_family = "unix")]
{
Expand Down
16 changes: 16 additions & 0 deletions internal/core/window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,22 @@ pub trait WindowAdapter {
fn internal(&self, _: crate::InternalToken) -> Option<&dyn WindowAdapterInternal> {
None
}

/// Re-implement this to support exposing raw window handles (version 0.6).
#[cfg(feature = "raw-window-handle-06")]
fn window_handle_06(
&self,
) -> Result<raw_window_handle_06::WindowHandle<'_>, raw_window_handle_06::HandleError> {
Err(raw_window_handle_06::HandleError::NotSupported)
}

/// Re-implement this to support exposing raw display handles (version 0.6).
#[cfg(feature = "raw-window-handle-06")]
fn display_handle_06(
&self,
) -> Result<raw_window_handle_06::DisplayHandle<'_>, raw_window_handle_06::HandleError> {
Err(raw_window_handle_06::HandleError::NotSupported)
}
}

/// Implementation details behind [`WindowAdapter`], but since this
Expand Down
7 changes: 7 additions & 0 deletions internal/interpreter/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,12 @@ renderer-winit-software = ["renderer-software"]
## APIs to support screen readers and other assistive technologies.
accessibility = ["i-slint-backend-selector/accessibility"]

## Enable integration with [raw-window-handle](raw_window_handle_06) version 0.6. This provides a
## [`Window::window_handle()`] function that returns a struct that implements
## [HasWindowHandle](raw_window_handle_06::HasWindowHandle) and
## [HasDisplayHandle](raw_window_handle_06::HasDisplayHandle) implementation.
raw-window-handle-06 = ["dep:raw-window-handle-06", "i-slint-backend-selector/raw-window-handle-06"]

## Features used internally by Slint tooling that are not stable and come without
## any stability guarantees whatsoever.
internal = []
Expand All @@ -126,6 +132,7 @@ once_cell = "1.5"
thiserror = "1"
document-features = { version = "0.2.0", optional = true }
spin_on = { version = "0.1", optional = true }
raw-window-handle-06 = { workspace = true, optional = true }

[target.'cfg(target_arch = "wasm32")'.dependencies]
i-slint-backend-winit = { workspace = true }
Expand Down