Skip to content

Commit

Permalink
Update glutin and glutin-winit to support winit 0.30, removed rwh_05 …
Browse files Browse the repository at this point in the history
…and updated / implemented accesskit
  • Loading branch information
AndriBaal committed Jun 10, 2024
1 parent 6ecdf37 commit 683c0b1
Show file tree
Hide file tree
Showing 10 changed files with 183 additions and 276 deletions.
284 changes: 85 additions & 199 deletions Cargo.lock

Large diffs are not rendered by default.

19 changes: 4 additions & 15 deletions crates/eframe/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -59,14 +59,7 @@ android-native-activity = ["egui-winit/android-native-activity"]
default_fonts = ["egui/default_fonts"]

## Use [`glow`](https://github.com/grovesNL/glow) for painting, via [`egui_glow`](https://github.com/emilk/egui/tree/master/crates/egui_glow).
glow = [
"dep:egui_glow",
"dep:glow",
"dep:glutin-winit",
"dep:glutin",
"dep:rwh_05",
"winit/rwh_05",
]
glow = ["dep:egui_glow", "dep:glow", "dep:glutin-winit", "dep:glutin"]

## Enable saving app state to disk.
persistence = [
Expand Down Expand Up @@ -141,10 +134,6 @@ web-time.workspace = true

egui_glow = { workspace = true, optional = true, default-features = false }
glow = { workspace = true, optional = true }
# glutin stuck on old version of raw-window-handle:
rwh_05 = { package = "raw-window-handle", version = "0.5.2", optional = true, features = [
"std",
] }
ron = { version = "0.8", optional = true, features = ["integer128"] }
serde = { version = "1", optional = true, features = ["derive"] }

Expand All @@ -158,7 +147,7 @@ egui-winit = { workspace = true, default-features = false, features = [
image = { version = "0.24", default-features = false, features = [
"png",
] } # Needed for app icon
winit = { workspace = true, default-features = false, features = ["rwh_06"] }
winit = { workspace = true, default-features = false }

# optional native:
directories-next = { version = "2", optional = true }
Expand All @@ -169,8 +158,8 @@ pollster = { version = "0.3", optional = true } # needed for wgpu

# we can expose these to user so that they can select which backends they want to enable to avoid compiling useless deps.
# this can be done at the same time we expose x11/wayland features of winit crate.
glutin = { version = "0.31", optional = true }
glutin-winit = { version = "0.4", optional = true }
glutin = { version = "0.32", optional = true }
glutin-winit = { version = "0.5", optional = true }
puffin = { workspace = true, optional = true }
wgpu = { workspace = true, optional = true, features = [
# Let's enable some backends so that users can use `eframe` out-of-the-box
Expand Down
46 changes: 29 additions & 17 deletions crates/eframe/src/native/glow_integration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ use glutin::{
prelude::{GlDisplay, PossiblyCurrentGlContext},
surface::GlSurface,
};
use raw_window_handle::HasWindowHandle;
use winit::{
event_loop::{ActiveEventLoop, EventLoop, EventLoopProxy},
window::{Window, WindowId},
Expand Down Expand Up @@ -466,23 +467,32 @@ impl WinitApp for GlowWinitApp {
}

#[cfg(feature = "accesskit")]
winit::event::Event::UserEvent(UserEvent::AccessKitActionRequest {
request,
window_id,
}) => {
winit::event::Event::UserEvent(UserEvent::AccessKitEvent(
egui_winit::accesskit_winit::Event {
window_id,
window_event,
},
)) => {
if let Some(running) = &self.running {
let mut glutin = running.glutin.borrow_mut();
if let Some(viewport_id) = glutin.viewport_from_window.get(window_id).copied() {
if let Some(viewport) = glutin.viewports.get_mut(&viewport_id) {
if let Some(egui_winit) = &mut viewport.egui_winit {
crate::profile_scope!("on_accesskit_action_request");
egui_winit.on_accesskit_action_request(request.clone());
crate::profile_scope!("on_accesskit_window_event");
winit_integration::on_accesskit_window_event(
egui_winit,
*window_id,
window_event,
)
} else {
EventResult::Wait
}
} else {
EventResult::Wait
}
} else {
EventResult::Wait
}
// As a form of user input, accessibility actions should
// lead to a repaint.
EventResult::RepaintNext(*window_id)
} else {
EventResult::Wait
}
Expand Down Expand Up @@ -939,7 +949,7 @@ impl GlutinWindowContext {
let display_builder = glutin_winit::DisplayBuilder::new()
// we might want to expose this option to users in the future. maybe using an env var or using native_options.
.with_preference(glutin_winit::ApiPreference::FallbackEgl) // https://github.com/emilk/egui/issues/2520#issuecomment-1367841150
.with_window_builder(Some(egui_winit::create_winit_window_attributes(
.with_window_attributes(Some(egui_winit::create_winit_window_attributes(
egui_ctx,
event_loop,
viewport_builder.clone(),
Expand Down Expand Up @@ -974,10 +984,9 @@ impl GlutinWindowContext {
gl_display.version_string(),
gl_display.supported_features()
);
let glutin_raw_window_handle = window.as_ref().map(|w| {
use rwh_05::HasRawWindowHandle as _; // glutin stuck on old version of raw-window-handle
w.raw_window_handle()
});
let glutin_raw_window_handle = window
.as_ref()
.and_then(|w| w.window_handle().ok().map(|h| h.as_raw()));
log::debug!("creating gl context using raw window handle: {glutin_raw_window_handle:?}");

// create gl context. if core context cannot be created, try gl es context as fallback.
Expand Down Expand Up @@ -1092,7 +1101,7 @@ impl GlutinWindowContext {
window
} else {
log::debug!("Creating a window for viewport {viewport_id:?}");
let window_builder = egui_winit::create_winit_window_builder(
let window_builder = egui_winit::create_winit_window_attributes(
&self.egui_ctx,
event_loop,
viewport.builder.clone(),
Expand Down Expand Up @@ -1132,9 +1141,12 @@ impl GlutinWindowContext {
let width_px = NonZeroU32::new(width_px).unwrap_or(NonZeroU32::MIN);
let height_px = NonZeroU32::new(height_px).unwrap_or(NonZeroU32::MIN);
let surface_attributes = {
use rwh_05::HasRawWindowHandle as _; // glutin stuck on old version of raw-window-handle
glutin::surface::SurfaceAttributesBuilder::<glutin::surface::WindowSurface>::new()
.build(window.raw_window_handle(), width_px, height_px)
.build(
window.window_handle().unwrap().as_raw(),
width_px,
height_px,
)
};

log::trace!("creating surface with attributes: {surface_attributes:?}");
Expand Down
23 changes: 15 additions & 8 deletions crates/eframe/src/native/wgpu_integration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -467,10 +467,12 @@ impl WinitApp for WgpuWinitApp {
}

#[cfg(feature = "accesskit")]
winit::event::Event::UserEvent(UserEvent::AccessKitActionRequest {
request,
window_id,
}) => {
winit::event::Event::UserEvent(UserEvent::AccessKitEvent(
egui_winit::accesskit_winit::Event {
window_id,
window_event,
},
)) => {
if let Some(running) = &mut self.running {
let mut shared_lock = running.shared.borrow_mut();
let SharedState {
Expand All @@ -483,12 +485,17 @@ impl WinitApp for WgpuWinitApp {
.and_then(|id| viewports.get_mut(id))
{
if let Some(egui_winit) = &mut viewport.egui_winit {
egui_winit.on_accesskit_action_request(request.clone());
winit_integration::on_accesskit_window_event(
egui_winit,
*window_id,
window_event,
)
} else {
EventResult::Wait
}
} else {
EventResult::Wait
}
// As a form of user input, accessibility actions should
// lead to a repaint.
EventResult::RepaintNext(*window_id)
} else {
EventResult::Wait
}
Expand Down
53 changes: 36 additions & 17 deletions crates/eframe/src/native/winit_integration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@ use winit::{
window::{Window, WindowId},
};

#[cfg(feature = "accesskit")]
use egui::accesskit;
use egui::ViewportId;
#[cfg(feature = "accesskit")]
use egui_winit::accesskit_winit;
Expand Down Expand Up @@ -48,26 +46,47 @@ pub enum UserEvent {
frame_nr: u64,
},

/// A request related to [`accesskit`](https://accesskit.dev/).
/// An event related to [`accesskit`](https://accesskit.dev/).
#[cfg(feature = "accesskit")]
AccessKitActionRequest {
request: accesskit::ActionRequest,
window_id: WindowId,
},
AccessKitEvent(accesskit_winit::Event),
}

#[cfg(feature = "accesskit")]
impl From<accesskit_winit::Event> for UserEvent {
fn from(event: accesskit_winit::Event) -> Self {
match event.window_event {
accesskit_winit::WindowEvent::InitialTreeRequested => todo!(),
accesskit_winit::WindowEvent::AccessibilityDeactivated => todo!(),
accesskit_winit::WindowEvent::ActionRequested(request) => {
Self::AccessKitActionRequest {
request,
window_id: event.window_id,
}
}
UserEvent::AccessKitEvent(event)
}
}

#[cfg(feature = "accesskit")]
pub(crate) fn on_accesskit_window_event(
egui_winit: &mut egui_winit::State,
window_id: WindowId,
event: &accesskit_winit::WindowEvent,
) -> EventResult {
match event {
accesskit_winit::WindowEvent::InitialTreeRequested => {
egui_winit.egui_ctx().enable_accesskit();
// Because we can't provide the initial tree synchronously
// (because that would require the activation handler to access
// the same mutable state as the winit event handler), some
// AccessKit platform adapters will use a placeholder tree
// until we send the first tree update. To minimize the possible
// bad effects of that workaround, repaint and send the tree
// immediately.
EventResult::RepaintNow(window_id)
}
accesskit_winit::WindowEvent::ActionRequested(request) => {
egui_winit.on_accesskit_action_request(request.clone());
// As a form of user input, accessibility actions should cause
// a repaint, but not until the next regular frame.
EventResult::RepaintNext(window_id)
}
accesskit_winit::WindowEvent::AccessibilityDeactivated => {
egui_winit.egui_ctx().disable_accesskit();
// Disabling AccessKit support should have no visible effect,
// so there's no need to repaint.
EventResult::Wait
}
}
}
Expand Down Expand Up @@ -134,7 +153,7 @@ pub fn short_event_description(event: &winit::event::Event<UserEvent>) -> &'stat
winit::event::Event::UserEvent(user_event) => match user_event {
UserEvent::RequestRepaint { .. } => "UserEvent::RequestRepaint",
#[cfg(feature = "accesskit")]
UserEvent::AccessKitActionRequest { .. } => "UserEvent::AccessKitActionRequest",
UserEvent::AccessKitEvent(_) => "UserEvent::AccessKitEvent",
},
_ => egui_winit::short_generic_event_description(event),
}
Expand Down
4 changes: 1 addition & 3 deletions crates/egui-wgpu/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,7 @@ wgpu = { workspace = true, features = ["wgsl"] }

# Optional dependencies:

winit = { workspace = true, optional = true, default-features = false, features = [
"rwh_06",
] }
winit = { workspace = true, optional = true, default-features = false }

# Native:
[target.'cfg(not(target_arch = "wasm32"))'.dependencies]
Expand Down
4 changes: 2 additions & 2 deletions crates/egui-winit/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -62,12 +62,12 @@ egui = { workspace = true, default-features = false, features = ["log"] }
log.workspace = true
raw-window-handle.workspace = true
web-time.workspace = true
winit = { workspace = true, default-features = false, features = ["rwh_06"] }
winit = { workspace = true, default-features = false }

#! ### Optional dependencies

# feature accesskit
accesskit_winit = { version = "0.20.0", optional = true }
accesskit_winit = { version = "0.21.0", optional = true }

## Enable this when generating docs.
document-features = { workspace = true, optional = true }
Expand Down
2 changes: 1 addition & 1 deletion crates/egui/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ ahash.workspace = true
nohash-hasher.workspace = true

#! ### Optional dependencies
accesskit = { version = "0.14", optional = true }
accesskit = { version = "0.15", optional = true }

backtrace = { workspace = true, optional = true }

Expand Down
6 changes: 6 additions & 0 deletions crates/egui/src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2855,6 +2855,12 @@ impl Context {
self.write(|ctx| ctx.is_accesskit_enabled = true);
}

/// Disable generation of AccessKit tree updates in all future frames.
#[cfg(feature = "accesskit")]
pub fn disable_accesskit(&self) {
self.write(|ctx| ctx.is_accesskit_enabled = false);
}

/// Return a tree update that the egui integration should provide to the
/// AccessKit adapter if it cannot immediately run the egui application
/// to get a full tree update after running [`Context::enable_accesskit`].
Expand Down
18 changes: 4 additions & 14 deletions crates/egui_glow/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -68,9 +68,7 @@ document-features = { workspace = true, optional = true }
# Native:
[target.'cfg(not(target_arch = "wasm32"))'.dependencies]
puffin = { workspace = true, optional = true }
winit = { workspace = true, optional = true, default-features = false, features = [
"rwh_06", # for compatibility with egui-winit
] }
winit = { workspace = true, optional = true, default-features = false }

# Web:
[target.'cfg(target_arch = "wasm32")'.dependencies]
Expand All @@ -79,17 +77,9 @@ wasm-bindgen = "0.2"


[dev-dependencies]
glutin = "0.31" # examples/pure_glow
glutin-winit = "0.4.0"
# glutin stuck on old version of raw-window-handle:
rwh_05 = { package = "raw-window-handle", version = "0.5.2", features = [
"std",
] }
glutin = "0.32" # examples/pure_glow
glutin-winit = "0.5.0"

[[example]]
name = "pure_glow"
required-features = [
"winit",
"egui/default_fonts",
"winit/rwh_05", # glutin stuck on old version of raw-window-handle
]
required-features = ["winit", "egui/default_fonts"]

0 comments on commit 683c0b1

Please sign in to comment.