Skip to content

Commit

Permalink
Make AccessKit an optional feature in egui-winit, so we don't have to…
Browse files Browse the repository at this point in the history
… change egui_glium and egui_glow, and only eframe is opinionated
  • Loading branch information
mwcampbell committed Nov 14, 2022
1 parent b0f2ae5 commit 4112f17
Show file tree
Hide file tree
Showing 10 changed files with 57 additions and 63 deletions.
4 changes: 2 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions crates/eframe/Cargo.toml
Expand Up @@ -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",
] }
Expand Down
3 changes: 2 additions & 1 deletion crates/eframe/src/native/epi_integration.rs
Expand Up @@ -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);

Expand Down
8 changes: 6 additions & 2 deletions crates/egui-winit/Cargo.toml
Expand Up @@ -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"]

Expand All @@ -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 = [
Expand All @@ -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"] }

Expand Down
50 changes: 31 additions & 19 deletions crates/egui-winit/src/lib.rs
Expand Up @@ -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;

Expand All @@ -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(
Expand Down Expand Up @@ -89,23 +91,16 @@ pub struct State {
/// track ime state
input_method_editor_started: bool,

accesskit: accesskit_winit::Adapter,
#[cfg(feature = "accesskit")]
accesskit: Option<accesskit_winit::Adapter>,
}

impl State {
pub fn new<T: From<accesskit_winit::ActionRequestEvent> + Send>(
event_loop: &EventLoopWindowTarget<T>,
event_loop_proxy: EventLoopProxy<T>,
window: &winit::window::Window,
) -> Self {
Self::new_with_wayland_display(wayland_display(event_loop), event_loop_proxy, window)
pub fn new<T>(event_loop: &EventLoopWindowTarget<T>) -> Self {
Self::new_with_wayland_display(wayland_display(event_loop))
}

pub fn new_with_wayland_display<T: From<accesskit_winit::ActionRequestEvent> + Send>(
wayland_display: Option<*mut c_void>,
event_loop_proxy: EventLoopProxy<T>,
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()
Expand All @@ -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<T: From<accesskit_winit::ActionRequestEvent> + Send>(
&mut self,
window: &winit::window::Window,
event_loop_proxy: winit::event_loop::EventLoopProxy<T>,
) {
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) {
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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) {
Expand Down
9 changes: 3 additions & 6 deletions 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::<accesskit_winit::ActionRequestEvent>::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);
Expand Down Expand Up @@ -107,7 +104,7 @@ fn main() {
});
}

fn create_display<E>(event_loop: &glutin::event_loop::EventLoop<E>) -> 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 {
Expand Down
9 changes: 3 additions & 6 deletions crates/egui_glium/examples/pure_glium.rs
Expand Up @@ -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::<accesskit_winit::ActionRequestEvent>::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();

Expand Down Expand Up @@ -93,7 +90,7 @@ fn main() {
});
}

fn create_display<E>(event_loop: &glutin::event_loop::EventLoop<E>) -> 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 {
Expand Down
12 changes: 3 additions & 9 deletions crates/egui_glium/src/lib.rs
Expand Up @@ -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;

// ----------------------------------------------------------------------------
Expand All @@ -33,15 +32,10 @@ pub struct EguiGlium {
}

impl EguiGlium {
pub fn new<E: From<accesskit_winit::ActionRequestEvent> + Send>(
display: &glium::Display,
event_loop: &EventLoopWindowTarget<E>,
event_loop_proxy: EventLoopProxy<E>,
) -> Self {
pub fn new<E>(display: &glium::Display, event_loop: &EventLoopWindowTarget<E>) -> 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);
Expand Down
17 changes: 4 additions & 13 deletions crates/egui_glow/examples/pure_glow.rs
Expand Up @@ -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::<accesskit_winit::ActionRequestEvent>::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 = || {
Expand Down Expand Up @@ -108,8 +99,8 @@ fn main() {
});
}

fn create_display<E>(
event_loop: &glutin::event_loop::EventLoop<E>,
fn create_display(
event_loop: &glutin::event_loop::EventLoop<()>,
) -> (
glutin::WindowedContext<glutin::PossiblyCurrent>,
glow::Context,
Expand Down
7 changes: 2 additions & 5 deletions 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;

Expand All @@ -16,10 +15,8 @@ pub struct EguiGlow {

impl EguiGlow {
/// For automatic shader version detection set `shader_version` to `None`.
pub fn new<E: From<accesskit_winit::ActionRequestEvent> + Send>(
pub fn new<E>(
event_loop: &winit::event_loop::EventLoopWindowTarget<E>,
event_loop_proxy: winit::event_loop::EventLoopProxy<E>,
window: &winit::window::Window,
gl: std::sync::Arc<glow::Context>,
shader_version: Option<ShaderVersion>,
) -> Self {
Expand All @@ -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(),
Expand Down

0 comments on commit 4112f17

Please sign in to comment.