From f69cac2bc55a2cde4969b1dd42d45f5444612625 Mon Sep 17 00:00:00 2001 From: Simon Hausmann Date: Tue, 13 Jun 2023 14:02:24 +0200 Subject: [PATCH] Make it possible to opt out of accessibility support This is only supported for winit with accesskit right now, with Qt it's always built-in. On macOS and Windows accessibility support is practically free. On Linux it pulls in zbus and async-io. --- api/cpp/CMakeLists.txt | 1 + api/cpp/Cargo.toml | 1 + api/rs/slint/Cargo.toml | 7 +++++++ internal/backends/selector/Cargo.toml | 1 + internal/backends/winit/Cargo.toml | 5 +++-- internal/backends/winit/build.rs | 1 + internal/backends/winit/event_loop.rs | 4 ++-- internal/backends/winit/lib.rs | 2 +- internal/backends/winit/winitwindowadapter.rs | 12 ++++++------ internal/interpreter/Cargo.toml | 5 +++++ 10 files changed, 28 insertions(+), 11 deletions(-) diff --git a/api/cpp/CMakeLists.txt b/api/cpp/CMakeLists.txt index 8478cb7c0d4..42a9d5d4bbb 100644 --- a/api/cpp/CMakeLists.txt +++ b/api/cpp/CMakeLists.txt @@ -111,6 +111,7 @@ define_cargo_feature(backend-qt "Enable Qt based rendering backend" ON) define_cargo_feature(experimental "Enable experimental features (no compatibility guarantees)" OFF) define_cargo_feature(gettext "Enable support of translations using gettext" OFF) +define_cargo_feature(accessibility "Enable integration with operating system provided accessibility APIs" ON) # Compat options option(SLINT_FEATURE_BACKEND_GL_ALL "This feature is an alias for SLINT_FEATURE_BACKEND_WINIT and SLINT_FEATURE_RENDERER_WINIT_FEMTOVG." OFF) diff --git a/api/cpp/Cargo.toml b/api/cpp/Cargo.toml index 9cd7fa9ccaf..ca7cf383725 100644 --- a/api/cpp/Cargo.toml +++ b/api/cpp/Cargo.toml @@ -36,6 +36,7 @@ renderer-winit-skia-opengl = ["i-slint-backend-selector/renderer-winit-skia-open renderer-winit-skia-vulkan = ["i-slint-backend-selector/renderer-winit-skia-vulkan"] renderer-winit-software = ["i-slint-backend-selector/renderer-winit-software"] gettext = ["i-slint-core/gettext-rs"] +accessibility = ["i-slint-backend-selector/accessibility"] experimental = ["i-slint-renderer-skia", "raw-window-handle"] diff --git a/api/rs/slint/Cargo.toml b/api/rs/slint/Cargo.toml index 5bbe14090dd..6864a585899 100644 --- a/api/rs/slint/Cargo.toml +++ b/api/rs/slint/Cargo.toml @@ -24,6 +24,7 @@ default = [ "backend-winit", "renderer-winit-femtovg", "backend-qt", + "accessibility", "compat-1-0", ] @@ -65,6 +66,12 @@ software-renderer-systemfonts = ["i-slint-core/software-renderer-systemfonts"] ## **Safety** : You must ensure that there is only one single thread that call into the Slint API unsafe-single-threaded = ["i-slint-core/unsafe-single-threaded"] +## Enable integration with operating system provided accessibility APIs (default: enabled) +## +## Enabling this feature will try to expose the tree of UI elements to OS provided accessibility +## APIs to support screen readers and other assistive technologies. +accessibility = ["i-slint-backend-selector/accessibility"] + #! ### Backends #! Slint needs a backend that will act as liaison between Slint and the OS. diff --git a/internal/backends/selector/Cargo.toml b/internal/backends/selector/Cargo.toml index c85c8c9b8c2..cef987eb000 100644 --- a/internal/backends/selector/Cargo.toml +++ b/internal/backends/selector/Cargo.toml @@ -26,6 +26,7 @@ renderer-winit-skia-vulkan = ["i-slint-backend-winit/renderer-winit-skia-vulkan" renderer-winit-software = ["i-slint-backend-winit/renderer-winit-software"] rtti = ["i-slint-backend-winit?/rtti", "i-slint-backend-qt?/rtti"] +accessibility = ["i-slint-backend-winit?/accessibility"] [dependencies] i-slint-core = { version = "=1.0.3", path = "../../../internal/core", default-features = false } diff --git a/internal/backends/winit/Cargo.toml b/internal/backends/winit/Cargo.toml index 82384e6dfe0..033e24dfa08 100644 --- a/internal/backends/winit/Cargo.toml +++ b/internal/backends/winit/Cargo.toml @@ -26,6 +26,7 @@ renderer-winit-skia = ["i-slint-renderer-skia"] renderer-winit-skia-opengl = ["renderer-winit-skia", "i-slint-renderer-skia/opengl"] renderer-winit-skia-vulkan = ["renderer-winit-skia", "i-slint-renderer-skia/vulkan"] renderer-winit-software = ["softbuffer", "imgref", "rgb", "i-slint-core/software-renderer-systemfonts"] +accessibility = ["accesskit", "accesskit_winit"] rtti = ["i-slint-core/rtti"] default = [] @@ -69,8 +70,8 @@ wasm-bindgen = { version = "0.2" } [target.'cfg(not(target_arch = "wasm32"))'.dependencies] glutin = { version = "0.30", optional = true, default-features = false, features = ["egl", "wgl"] } glutin-winit = { version = "0.3.0", optional = true, default-features = false, features = ["egl", "wgl"] } -accesskit = { version = "0.11.0" } -accesskit_winit = { version = "0.14.0" } +accesskit = { version = "0.11.0", optional = true } +accesskit_winit = { version = "0.14.0", optional = true } [target.'cfg(target_os = "macos")'.dependencies] # For GL rendering diff --git a/internal/backends/winit/build.rs b/internal/backends/winit/build.rs index a946131fcc3..bff306550ad 100644 --- a/internal/backends/winit/build.rs +++ b/internal/backends/winit/build.rs @@ -7,5 +7,6 @@ fn main() { // Setup cfg aliases cfg_aliases! { enable_skia_renderer: { any(feature = "renderer-winit-skia", feature = "renderer-winit-skia-opengl", feature = "renderer-winit-skia-vulkan")}, + enable_accesskit: { all(feature = "accessibility", not(target_arch = "wasm32")) }, } } diff --git a/internal/backends/winit/event_loop.rs b/internal/backends/winit/event_loop.rs index 6c705d42526..fa0d14f86f0 100644 --- a/internal/backends/winit/event_loop.rs +++ b/internal/backends/winit/event_loop.rs @@ -502,9 +502,9 @@ pub fn run() -> Result<(), corelib::platform::PlatformError> { match event { Event::WindowEvent { event, window_id } => { if let Some(window) = window_by_id(window_id) { - #[cfg(target_arch = "wasm32")] + #[cfg(not(enable_accesskit))] let process_event = true; - #[cfg(not(target_arch = "wasm32"))] + #[cfg(enable_accesskit)] let process_event = window.accesskit_adapter.on_event(&window.winit_window(), &event); diff --git a/internal/backends/winit/lib.rs b/internal/backends/winit/lib.rs index eb7d13d1d99..bacb697d5e6 100644 --- a/internal/backends/winit/lib.rs +++ b/internal/backends/winit/lib.rs @@ -53,7 +53,7 @@ mod renderer { pub(crate) mod sw; } -#[cfg(not(target_arch = "wasm32"))] +#[cfg(enable_accesskit)] mod accesskit; #[cfg(target_arch = "wasm32")] diff --git a/internal/backends/winit/winitwindowadapter.rs b/internal/backends/winit/winitwindowadapter.rs index 89b99173e69..2ae7931b576 100644 --- a/internal/backends/winit/winitwindowadapter.rs +++ b/internal/backends/winit/winitwindowadapter.rs @@ -20,9 +20,9 @@ use crate::renderer::WinitCompatibleRenderer; use const_field_offset::FieldOffsets; use corelib::component::ComponentRc; -#[cfg(not(target_arch = "wasm32"))] +#[cfg(enable_accesskit)] use corelib::component::ComponentRef; -#[cfg(not(target_arch = "wasm32"))] +#[cfg(enable_accesskit)] use corelib::items::ItemRef; use corelib::items::{ItemRc, MouseCursor}; @@ -123,7 +123,7 @@ pub struct WinitWindowAdapter { #[cfg(target_arch = "wasm32")] virtual_keyboard_helper: RefCell>, - #[cfg(not(target_arch = "wasm32"))] + #[cfg(enable_accesskit)] pub accesskit_adapter: crate::accesskit::AccessKitAdapter, } @@ -153,7 +153,7 @@ impl WinitWindowAdapter { renderer: Box::new(renderer), #[cfg(target_arch = "wasm32")] virtual_keyboard_helper: Default::default(), - #[cfg(not(target_arch = "wasm32"))] + #[cfg(enable_accesskit)] accesskit_adapter: crate::accesskit::AccessKitAdapter::new( self_weak.clone(), &*winit_window, @@ -655,11 +655,11 @@ impl WindowAdapterSealed for WinitWindowAdapter { } fn handle_focus_change(&self, _old: Option, _new: Option) { - #[cfg(not(target_arch = "wasm32"))] + #[cfg(enable_accesskit)] self.accesskit_adapter.handle_focus_change(_new); } - #[cfg(not(target_arch = "wasm32"))] + #[cfg(enable_accesskit)] fn unregister_component<'a>( &self, _component: ComponentRef, diff --git a/internal/interpreter/Cargo.toml b/internal/interpreter/Cargo.toml index fee6081c757..69b73a7e92a 100644 --- a/internal/interpreter/Cargo.toml +++ b/internal/interpreter/Cargo.toml @@ -81,6 +81,11 @@ renderer-winit-skia-vulkan = ["i-slint-backend-selector/renderer-winit-skia-vulk ## Must be used in combination with `backend-winit`, `backend-winit-x11`, or `backend-winit-wayland`. renderer-winit-software = ["i-slint-backend-selector/renderer-winit-software"] +## Enable integration with operating system provided accessibility APIs (default: enabled) +## +## Enabling this feature will try to expose the tree of UI elements to OS provided accessibility +## APIs to support screen readers and other assistive technologies. +accessibility = ["i-slint-backend-selector/accessibility"] [dependencies] i-slint-compiler = { version = "=1.0.3", path = "../compiler" }