From 4112f17acebe8ea1745c0b7d51901f9436b874b1 Mon Sep 17 00:00:00 2001 From: Matt Campbell Date: Mon, 14 Nov 2022 15:59:36 -0600 Subject: [PATCH] Make AccessKit an optional feature in egui-winit, so we don't have to change egui_glium and egui_glow, and only eframe is opinionated --- Cargo.lock | 4 +- crates/eframe/Cargo.toml | 1 + crates/eframe/src/native/epi_integration.rs | 3 +- crates/egui-winit/Cargo.toml | 8 +++- crates/egui-winit/src/lib.rs | 50 ++++++++++++-------- crates/egui_glium/examples/native_texture.rs | 9 ++-- crates/egui_glium/examples/pure_glium.rs | 9 ++-- crates/egui_glium/src/lib.rs | 12 ++--- crates/egui_glow/examples/pure_glow.rs | 17 ++----- crates/egui_glow/src/winit.rs | 7 +-- 10 files changed, 57 insertions(+), 63 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 726da788888..4542eae6c22 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -57,9 +57,9 @@ dependencies = [ [[package]] name = "accesskit_winit" -version = "0.4.0" +version = "0.5.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ad4cb0fa97029e143320f31c0b7dea5cc5586a47341b3e8fa5fb8d19ccd23d2d" +checksum = "9106530b580dd0b4f98fcd9160fe2a533e328e670923aa7fbe6bcfc07e6a0d07" dependencies = [ "accesskit", "accesskit_windows", diff --git a/crates/eframe/Cargo.toml b/crates/eframe/Cargo.toml index 9fe2022febe..fa55c24b57e 100644 --- a/crates/eframe/Cargo.toml +++ b/crates/eframe/Cargo.toml @@ -78,6 +78,7 @@ serde = { version = "1", optional = true, features = ["derive"] } # native: [target.'cfg(not(target_arch = "wasm32"))'.dependencies] egui-winit = { version = "0.19.0", path = "../egui-winit", default-features = false, features = [ + "accesskit", "clipboard", "links", ] } diff --git a/crates/eframe/src/native/epi_integration.rs b/crates/eframe/src/native/epi_integration.rs index c2d45a8d4b5..319d5ed1f26 100644 --- a/crates/eframe/src/native/epi_integration.rs +++ b/crates/eframe/src/native/epi_integration.rs @@ -251,7 +251,8 @@ impl EpiIntegration { wgpu_render_state, }; - let mut egui_winit = egui_winit::State::new(event_loop, event_loop_proxy, window); + let mut egui_winit = egui_winit::State::new(event_loop); + egui_winit.init_accesskit(window, event_loop_proxy); egui_winit.set_max_texture_side(max_texture_side); egui_winit.set_pixels_per_point(native_pixels_per_point); diff --git a/crates/egui-winit/Cargo.toml b/crates/egui-winit/Cargo.toml index 5284910efe0..86258e2839f 100644 --- a/crates/egui-winit/Cargo.toml +++ b/crates/egui-winit/Cargo.toml @@ -20,6 +20,9 @@ all-features = true [features] default = ["clipboard", "links", "wayland", "winit/default"] +## Enable platform accessibility API implementations through [AccessKit](https://accesskit.dev/). +accesskit = ["accesskit_winit", "egui/accesskit"] + ## [`bytemuck`](https://docs.rs/bytemuck) enables you to cast [`egui::epaint::Vertex`], [`egui::Vec2`] etc to `&[u8]`. bytemuck = ["egui/bytemuck"] @@ -43,9 +46,7 @@ serde = ["egui/serde", "dep:serde"] wayland = ["winit/wayland"] [dependencies] -accesskit_winit = "0.4.0" egui = { version = "0.19.0", path = "../egui", default-features = false, features = [ - "accesskit", "tracing", ] } instant = { version = "0.1", features = [ @@ -59,6 +60,9 @@ winit = { version = "0.27.2", default-features = false } ## Enable this when generating docs. document-features = { version = "0.2", optional = true } +# feature accesskit +accesskit_winit = { version = "0.5.0", optional = true } + puffin = { version = "0.14", optional = true } serde = { version = "1.0", optional = true, features = ["derive"] } diff --git a/crates/egui-winit/src/lib.rs b/crates/egui-winit/src/lib.rs index 71220d64b0c..a33640f7161 100644 --- a/crates/egui-winit/src/lib.rs +++ b/crates/egui-winit/src/lib.rs @@ -11,8 +11,10 @@ use std::os::raw::c_void; +#[cfg(feature = "accesskit")] pub use accesskit_winit; pub use egui; +#[cfg(feature = "accesskit")] use egui::accesskit; pub use winit; @@ -22,7 +24,7 @@ mod window_settings; pub use window_settings::WindowSettings; -use winit::event_loop::{EventLoopProxy, EventLoopWindowTarget}; +use winit::event_loop::EventLoopWindowTarget; #[cfg(feature = "wayland")] #[cfg(any( @@ -89,23 +91,16 @@ pub struct State { /// track ime state input_method_editor_started: bool, - accesskit: accesskit_winit::Adapter, + #[cfg(feature = "accesskit")] + accesskit: Option, } impl State { - pub fn new + Send>( - event_loop: &EventLoopWindowTarget, - event_loop_proxy: EventLoopProxy, - window: &winit::window::Window, - ) -> Self { - Self::new_with_wayland_display(wayland_display(event_loop), event_loop_proxy, window) + pub fn new(event_loop: &EventLoopWindowTarget) -> Self { + Self::new_with_wayland_display(wayland_display(event_loop)) } - pub fn new_with_wayland_display + Send>( - wayland_display: Option<*mut c_void>, - event_loop_proxy: EventLoopProxy, - window: &winit::window::Window, - ) -> Self { + pub fn new_with_wayland_display(wayland_display: Option<*mut c_void>) -> Self { let egui_input = egui::RawInput { has_focus: false, // winit will tell us when we have focus ..Default::default() @@ -127,14 +122,24 @@ impl State { input_method_editor_started: false, - accesskit: accesskit_winit::Adapter::new( - window, - Box::new(egui::accesskit_placeholder_tree_update), - event_loop_proxy, - ), + #[cfg(feature = "accesskit")] + accesskit: None, } } + #[cfg(feature = "accesskit")] + pub fn init_accesskit + Send>( + &mut self, + window: &winit::window::Window, + event_loop_proxy: winit::event_loop::EventLoopProxy, + ) { + self.accesskit = Some(accesskit_winit::Adapter::new( + window, + Box::new(egui::accesskit_placeholder_tree_update), + event_loop_proxy, + )); + } + /// Call this once a graphics context has been created to update the maximum texture dimensions /// that egui will use. pub fn set_max_texture_side(&mut self, max_texture_side: usize) { @@ -395,6 +400,7 @@ impl State { /// Call this when there is a new [`accesskit::ActionRequest`]. /// /// The result can be found in [`Self::egui_input`] and be extracted with [`Self::take_egui_input`]. + #[cfg(feature = "accesskit")] pub fn on_accesskit_action_request(&mut self, request: accesskit::ActionRequest) { self.egui_input .events @@ -619,6 +625,7 @@ impl State { events: _, // handled above mutable_text_under_cursor: _, // only used in eframe web text_cursor_pos, + #[cfg(feature = "accesskit")] accesskit_update, } = platform_output; self.current_pixels_per_point = egui_ctx.pixels_per_point(); // someone can have changed it to scale the UI @@ -637,7 +644,12 @@ impl State { window.set_ime_position(winit::dpi::LogicalPosition { x, y }); } - self.accesskit.update(accesskit_update); + #[cfg(feature = "accesskit")] + { + if let Some(accesskit) = self.accesskit.as_ref() { + accesskit.update(accesskit_update); + } + } } fn set_cursor_icon(&mut self, window: &winit::window::Window, cursor_icon: egui::CursorIcon) { diff --git a/crates/egui_glium/examples/native_texture.rs b/crates/egui_glium/examples/native_texture.rs index 96fc6e8ee49..19e4eaec6af 100644 --- a/crates/egui_glium/examples/native_texture.rs +++ b/crates/egui_glium/examples/native_texture.rs @@ -1,15 +1,12 @@ #![cfg_attr(not(debug_assertions), windows_subsystem = "windows")] // hide console window on Windows in release -use egui_winit::accesskit_winit; use glium::glutin; fn main() { - let event_loop = - glutin::event_loop::EventLoopBuilder::::with_user_event().build(); + let event_loop = glutin::event_loop::EventLoopBuilder::with_user_event().build(); let display = create_display(&event_loop); - let mut egui_glium = - egui_glium::EguiGlium::new(&display, &event_loop, event_loop.create_proxy()); + let mut egui_glium = egui_glium::EguiGlium::new(&display, &event_loop); let png_data = include_bytes!("../../../examples/retained_image/src/rust-logo-256x256.png"); let image = load_glium_image(png_data); @@ -107,7 +104,7 @@ fn main() { }); } -fn create_display(event_loop: &glutin::event_loop::EventLoop) -> glium::Display { +fn create_display(event_loop: &glutin::event_loop::EventLoop<()>) -> glium::Display { let window_builder = glutin::window::WindowBuilder::new() .with_resizable(true) .with_inner_size(glutin::dpi::LogicalSize { diff --git a/crates/egui_glium/examples/pure_glium.rs b/crates/egui_glium/examples/pure_glium.rs index c339ab447da..dc7ad58f09e 100644 --- a/crates/egui_glium/examples/pure_glium.rs +++ b/crates/egui_glium/examples/pure_glium.rs @@ -2,16 +2,13 @@ #![cfg_attr(not(debug_assertions), windows_subsystem = "windows")] // hide console window on Windows in release -use egui_winit::accesskit_winit; use glium::glutin; fn main() { - let event_loop = - glutin::event_loop::EventLoopBuilder::::with_user_event().build(); + let event_loop = glutin::event_loop::EventLoopBuilder::with_user_event().build(); let display = create_display(&event_loop); - let mut egui_glium = - egui_glium::EguiGlium::new(&display, &event_loop, event_loop.create_proxy()); + let mut egui_glium = egui_glium::EguiGlium::new(&display, &event_loop); let mut color_test = egui_demo_lib::ColorTest::default(); @@ -93,7 +90,7 @@ fn main() { }); } -fn create_display(event_loop: &glutin::event_loop::EventLoop) -> glium::Display { +fn create_display(event_loop: &glutin::event_loop::EventLoop<()>) -> glium::Display { let window_builder = glutin::window::WindowBuilder::new() .with_resizable(true) .with_inner_size(glutin::dpi::LogicalSize { diff --git a/crates/egui_glium/src/lib.rs b/crates/egui_glium/src/lib.rs index b5b603a504f..1d42ef18a6e 100644 --- a/crates/egui_glium/src/lib.rs +++ b/crates/egui_glium/src/lib.rs @@ -16,8 +16,7 @@ pub use painter::Painter; pub use egui_winit; -use egui_winit::accesskit_winit; -use egui_winit::winit::event_loop::{EventLoopProxy, EventLoopWindowTarget}; +use egui_winit::winit::event_loop::EventLoopWindowTarget; pub use egui_winit::EventResponse; // ---------------------------------------------------------------------------- @@ -33,15 +32,10 @@ pub struct EguiGlium { } impl EguiGlium { - pub fn new + Send>( - display: &glium::Display, - event_loop: &EventLoopWindowTarget, - event_loop_proxy: EventLoopProxy, - ) -> Self { + pub fn new(display: &glium::Display, event_loop: &EventLoopWindowTarget) -> Self { let painter = crate::Painter::new(display); - let mut egui_winit = - egui_winit::State::new(event_loop, event_loop_proxy, display.gl_window().window()); + let mut egui_winit = egui_winit::State::new(event_loop); egui_winit.set_max_texture_side(painter.max_texture_side()); let pixels_per_point = display.gl_window().window().scale_factor() as f32; egui_winit.set_pixels_per_point(pixels_per_point); diff --git a/crates/egui_glow/examples/pure_glow.rs b/crates/egui_glow/examples/pure_glow.rs index 86ec81751fe..1ef40f2f033 100644 --- a/crates/egui_glow/examples/pure_glow.rs +++ b/crates/egui_glow/examples/pure_glow.rs @@ -3,23 +3,14 @@ #![cfg_attr(not(debug_assertions), windows_subsystem = "windows")] // hide console window on Windows in release #![allow(unsafe_code)] -use egui_winit::accesskit_winit; - fn main() { let mut clear_color = [0.1, 0.1, 0.1]; - let event_loop = - glutin::event_loop::EventLoopBuilder::::with_user_event().build(); + let event_loop = glutin::event_loop::EventLoopBuilder::with_user_event().build(); let (gl_window, gl) = create_display(&event_loop); let gl = std::sync::Arc::new(gl); - let mut egui_glow = egui_glow::EguiGlow::new( - &event_loop, - event_loop.create_proxy(), - gl_window.window(), - gl.clone(), - None, - ); + let mut egui_glow = egui_glow::EguiGlow::new(&event_loop, gl.clone(), None); event_loop.run(move |event, _, control_flow| { let mut redraw = || { @@ -108,8 +99,8 @@ fn main() { }); } -fn create_display( - event_loop: &glutin::event_loop::EventLoop, +fn create_display( + event_loop: &glutin::event_loop::EventLoop<()>, ) -> ( glutin::WindowedContext, glow::Context, diff --git a/crates/egui_glow/src/winit.rs b/crates/egui_glow/src/winit.rs index c9c6fd44375..8cb9f930836 100644 --- a/crates/egui_glow/src/winit.rs +++ b/crates/egui_glow/src/winit.rs @@ -1,6 +1,5 @@ use crate::shader_version::ShaderVersion; pub use egui_winit; -use egui_winit::accesskit_winit; use egui_winit::winit; pub use egui_winit::EventResponse; @@ -16,10 +15,8 @@ pub struct EguiGlow { impl EguiGlow { /// For automatic shader version detection set `shader_version` to `None`. - pub fn new + Send>( + pub fn new( event_loop: &winit::event_loop::EventLoopWindowTarget, - event_loop_proxy: winit::event_loop::EventLoopProxy, - window: &winit::window::Window, gl: std::sync::Arc, shader_version: Option, ) -> Self { @@ -31,7 +28,7 @@ impl EguiGlow { Self { egui_ctx: Default::default(), - egui_winit: egui_winit::State::new(event_loop, event_loop_proxy, window), + egui_winit: egui_winit::State::new(event_loop), painter, shapes: Default::default(), textures_delta: Default::default(),