Skip to content

Commit 37c9f11

Browse files
authored
Fix egui_glow when targeting wasm32-unknown-unknown (#1303)
* Gate winit/glow and epi correctly * Add check to CI * Fix epi cfg
1 parent e3d1fa2 commit 37c9f11

File tree

3 files changed

+121
-102
lines changed

3 files changed

+121
-102
lines changed

.github/workflows/rust.yml

+14
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,20 @@ jobs:
5656
command: check
5757
args: -p egui_demo_app --lib --target wasm32-unknown-unknown
5858

59+
check_wasm_eframe_with_features:
60+
name: cargo check wasm eframe
61+
runs-on: ubuntu-20.04
62+
steps:
63+
- uses: actions/checkout@v2
64+
- uses: actions-rs/toolchain@v1
65+
with:
66+
profile: minimal
67+
toolchain: 1.56.0
68+
override: true
69+
- run: rustup target add wasm32-unknown-unknown
70+
- name: check
71+
run: cargo check -p eframe --lib --no-default-features --features egui_glow,persistence --target wasm32-unknown-unknown
72+
5973
check_web_all_features:
6074
name: cargo check web --all-features
6175
runs-on: ubuntu-latest

egui_glow/src/lib.rs

+15-102
Original file line numberDiff line numberDiff line change
@@ -88,114 +88,27 @@
8888
#![allow(clippy::manual_range_contains)]
8989

9090
pub mod painter;
91-
#[cfg(feature = "winit")]
92-
use egui_winit::winit;
9391
pub use glow;
9492
pub use painter::Painter;
95-
#[cfg(feature = "winit")]
96-
mod epi_backend;
9793
mod misc_util;
9894
mod post_process;
9995
mod shader_version;
10096
mod vao_emulate;
10197

102-
#[cfg(not(target_arch = "wasm32"))]
103-
#[cfg(feature = "egui_winit")]
104-
pub use egui_winit;
98+
#[cfg(all(not(target_arch = "wasm32"), feature = "winit"))]
99+
pub mod winit;
100+
#[cfg(all(not(target_arch = "wasm32"), feature = "winit"))]
101+
pub use winit::*;
105102

106-
#[cfg(all(feature = "epi", feature = "winit"))]
103+
#[cfg(all(
104+
not(target_arch = "wasm32"),
105+
feature = "persistence",
106+
feature = "winit"
107+
))]
108+
mod epi_backend;
109+
#[cfg(all(
110+
not(target_arch = "wasm32"),
111+
feature = "persistence",
112+
feature = "winit"
113+
))]
107114
pub use epi_backend::{run, NativeOptions};
108-
109-
// ----------------------------------------------------------------------------
110-
111-
/// Use [`egui`] from a [`glow`] app.
112-
#[cfg(feature = "winit")]
113-
pub struct EguiGlow {
114-
pub egui_ctx: egui::Context,
115-
pub egui_winit: egui_winit::State,
116-
pub painter: crate::Painter,
117-
118-
shapes: Vec<egui::epaint::ClippedShape>,
119-
textures_delta: egui::TexturesDelta,
120-
}
121-
122-
#[cfg(feature = "winit")]
123-
impl EguiGlow {
124-
pub fn new(window: &winit::window::Window, gl: &glow::Context) -> Self {
125-
let painter = crate::Painter::new(gl, None, "")
126-
.map_err(|error| {
127-
tracing::error!("error occurred in initializing painter:\n{}", error);
128-
})
129-
.unwrap();
130-
131-
Self {
132-
egui_ctx: Default::default(),
133-
egui_winit: egui_winit::State::new(painter.max_texture_side(), window),
134-
painter,
135-
shapes: Default::default(),
136-
textures_delta: Default::default(),
137-
}
138-
}
139-
140-
/// Returns `true` if egui wants exclusive use of this event
141-
/// (e.g. a mouse click on an egui window, or entering text into a text field).
142-
/// For instance, if you use egui for a game, you want to first call this
143-
/// and only when this returns `false` pass on the events to your game.
144-
///
145-
/// Note that egui uses `tab` to move focus between elements, so this will always return `true` for tabs.
146-
pub fn on_event(&mut self, event: &winit::event::WindowEvent<'_>) -> bool {
147-
self.egui_winit.on_event(&self.egui_ctx, event)
148-
}
149-
150-
/// Returns `true` if egui requests a repaint.
151-
///
152-
/// Call [`Self::paint`] later to paint.
153-
pub fn run(
154-
&mut self,
155-
window: &winit::window::Window,
156-
run_ui: impl FnMut(&egui::Context),
157-
) -> bool {
158-
let raw_input = self.egui_winit.take_egui_input(window);
159-
let egui::FullOutput {
160-
platform_output,
161-
needs_repaint,
162-
textures_delta,
163-
shapes,
164-
} = self.egui_ctx.run(raw_input, run_ui);
165-
166-
self.egui_winit
167-
.handle_platform_output(window, &self.egui_ctx, platform_output);
168-
169-
self.shapes = shapes;
170-
self.textures_delta.append(textures_delta);
171-
needs_repaint
172-
}
173-
174-
/// Paint the results of the last call to [`Self::run`].
175-
pub fn paint(&mut self, window: &winit::window::Window, gl: &glow::Context) {
176-
let shapes = std::mem::take(&mut self.shapes);
177-
let mut textures_delta = std::mem::take(&mut self.textures_delta);
178-
179-
for (id, image_delta) in textures_delta.set {
180-
self.painter.set_texture(gl, id, &image_delta);
181-
}
182-
183-
let clipped_meshes = self.egui_ctx.tessellate(shapes);
184-
let dimensions: [u32; 2] = window.inner_size().into();
185-
self.painter.paint_meshes(
186-
gl,
187-
dimensions,
188-
self.egui_ctx.pixels_per_point(),
189-
clipped_meshes,
190-
);
191-
192-
for id in textures_delta.free.drain(..) {
193-
self.painter.free_texture(gl, id);
194-
}
195-
}
196-
197-
/// Call to release the allocated graphics resources.
198-
pub fn destroy(&mut self, gl: &glow::Context) {
199-
self.painter.destroy(gl);
200-
}
201-
}

egui_glow/src/winit.rs

+92
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
pub use egui_winit;
2+
use egui_winit::winit;
3+
4+
/// Use [`egui`] from a [`glow`] app.
5+
pub struct EguiGlow {
6+
pub egui_ctx: egui::Context,
7+
pub egui_winit: egui_winit::State,
8+
pub painter: crate::Painter,
9+
10+
shapes: Vec<egui::epaint::ClippedShape>,
11+
textures_delta: egui::TexturesDelta,
12+
}
13+
14+
impl EguiGlow {
15+
pub fn new(window: &winit::window::Window, gl: &glow::Context) -> Self {
16+
let painter = crate::Painter::new(gl, None, "")
17+
.map_err(|error| {
18+
tracing::error!("error occurred in initializing painter:\n{}", error);
19+
})
20+
.unwrap();
21+
22+
Self {
23+
egui_ctx: Default::default(),
24+
egui_winit: egui_winit::State::new(painter.max_texture_side(), window),
25+
painter,
26+
shapes: Default::default(),
27+
textures_delta: Default::default(),
28+
}
29+
}
30+
31+
/// Returns `true` if egui wants exclusive use of this event
32+
/// (e.g. a mouse click on an egui window, or entering text into a text field).
33+
/// For instance, if you use egui for a game, you want to first call this
34+
/// and only when this returns `false` pass on the events to your game.
35+
///
36+
/// Note that egui uses `tab` to move focus between elements, so this will always return `true` for tabs.
37+
pub fn on_event(&mut self, event: &winit::event::WindowEvent<'_>) -> bool {
38+
self.egui_winit.on_event(&self.egui_ctx, event)
39+
}
40+
41+
/// Returns `true` if egui requests a repaint.
42+
///
43+
/// Call [`Self::paint`] later to paint.
44+
pub fn run(
45+
&mut self,
46+
window: &winit::window::Window,
47+
run_ui: impl FnMut(&egui::Context),
48+
) -> bool {
49+
let raw_input = self.egui_winit.take_egui_input(window);
50+
let egui::FullOutput {
51+
platform_output,
52+
needs_repaint,
53+
textures_delta,
54+
shapes,
55+
} = self.egui_ctx.run(raw_input, run_ui);
56+
57+
self.egui_winit
58+
.handle_platform_output(window, &self.egui_ctx, platform_output);
59+
60+
self.shapes = shapes;
61+
self.textures_delta.append(textures_delta);
62+
needs_repaint
63+
}
64+
65+
/// Paint the results of the last call to [`Self::run`].
66+
pub fn paint(&mut self, window: &winit::window::Window, gl: &glow::Context) {
67+
let shapes = std::mem::take(&mut self.shapes);
68+
let mut textures_delta = std::mem::take(&mut self.textures_delta);
69+
70+
for (id, image_delta) in textures_delta.set {
71+
self.painter.set_texture(gl, id, &image_delta);
72+
}
73+
74+
let clipped_meshes = self.egui_ctx.tessellate(shapes);
75+
let dimensions: [u32; 2] = window.inner_size().into();
76+
self.painter.paint_meshes(
77+
gl,
78+
dimensions,
79+
self.egui_ctx.pixels_per_point(),
80+
clipped_meshes,
81+
);
82+
83+
for id in textures_delta.free.drain(..) {
84+
self.painter.free_texture(gl, id);
85+
}
86+
}
87+
88+
/// Call to release the allocated graphics resources.
89+
pub fn destroy(&mut self, gl: &glow::Context) {
90+
self.painter.destroy(gl);
91+
}
92+
}

0 commit comments

Comments
 (0)