Skip to content

Commit 8a01097

Browse files
fix: proper suspend/resume lifecycle and native scale factor on Android
Add suspended() handler to drop renderer and window on suspend so stale references are cleaned up. Replace first_window_handle guard with initialized flag to prevent double logger init. Use native scale factor on Android instead of hardcoded 1.0 for correct high-DPI rendering.
1 parent 6dbbb02 commit 8a01097

File tree

1 file changed

+25
-12
lines changed

1 file changed

+25
-12
lines changed

src/lib.rs

Lines changed: 25 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -77,10 +77,20 @@ pub struct App {
7777
#[cfg(target_arch = "wasm32")]
7878
renderer_receiver: Option<futures::channel::oneshot::Receiver<Renderer>>,
7979
last_size: (u32, u32),
80+
initialized: bool,
8081
}
8182

8283
impl ApplicationHandler for App {
84+
fn suspended(&mut self, _event_loop: &winit::event_loop::ActiveEventLoop) {
85+
self.renderer = None;
86+
self.window = None;
87+
}
88+
8389
fn resumed(&mut self, event_loop: &winit::event_loop::ActiveEventLoop) {
90+
if self.window.is_some() {
91+
return;
92+
}
93+
8494
let mut attributes = Window::default_attributes();
8595

8696
#[cfg(not(target_arch = "wasm32"))]
@@ -113,12 +123,9 @@ impl ApplicationHandler for App {
113123
return;
114124
};
115125

116-
let first_window_handle = self.window.is_none();
117126
let window_handle = Arc::new(window);
118127
self.window = Some(window_handle.clone());
119-
if !first_window_handle {
120-
return;
121-
}
128+
122129
let gui_context = egui::Context::default();
123130

124131
#[cfg(not(target_arch = "wasm32"))]
@@ -134,7 +141,8 @@ impl ApplicationHandler for App {
134141

135142
#[cfg(target_os = "android")]
136143
{
137-
gui_context.set_pixels_per_point(1.0);
144+
let scale_factor = window_handle.scale_factor() as f32;
145+
gui_context.set_pixels_per_point(scale_factor);
138146
}
139147

140148
let viewport_id = gui_context.viewport_id();
@@ -155,7 +163,9 @@ impl ApplicationHandler for App {
155163

156164
#[cfg(all(not(target_arch = "wasm32"), not(target_os = "android")))]
157165
{
158-
env_logger::init();
166+
if !self.initialized {
167+
env_logger::init();
168+
}
159169
let renderer = pollster::block_on(async move {
160170
Renderer::new(window_handle.clone(), width, height).await
161171
});
@@ -174,8 +184,10 @@ impl ApplicationHandler for App {
174184
{
175185
let (sender, receiver) = futures::channel::oneshot::channel();
176186
self.renderer_receiver = Some(receiver);
177-
std::panic::set_hook(Box::new(console_error_panic_hook::hook));
178-
console_log::init().expect("Failed to initialize logger!");
187+
if !self.initialized {
188+
std::panic::set_hook(Box::new(console_error_panic_hook::hook));
189+
console_log::init().expect("Failed to initialize logger!");
190+
}
179191
log::info!("Canvas dimensions: ({canvas_width} x {canvas_height})");
180192
wasm_bindgen_futures::spawn_local(async move {
181193
let renderer =
@@ -188,6 +200,7 @@ impl ApplicationHandler for App {
188200

189201
self.gui_state = Some(gui_state);
190202
self.last_render_time = Some(Instant::now());
203+
self.initialized = true;
191204
}
192205

193206
fn window_event(
@@ -240,12 +253,12 @@ impl ApplicationHandler for App {
240253
}
241254
}
242255
WindowEvent::ScaleFactorChanged { .. } => {
243-
#[cfg(all(not(target_arch = "wasm32"), not(target_os = "android")))]
256+
#[cfg(not(target_arch = "wasm32"))]
244257
{
245258
let scale_factor = window.scale_factor() as f32;
246259
gui_state.egui_ctx().set_pixels_per_point(scale_factor);
247260
}
248-
#[cfg(any(target_arch = "wasm32", target_os = "android"))]
261+
#[cfg(target_arch = "wasm32")]
249262
{
250263
gui_state.egui_ctx().set_pixels_per_point(1.0);
251264
}
@@ -259,12 +272,12 @@ impl ApplicationHandler for App {
259272
renderer.resize(width, height);
260273
self.last_size = (width, height);
261274

262-
#[cfg(all(not(target_arch = "wasm32"), not(target_os = "android")))]
275+
#[cfg(not(target_arch = "wasm32"))]
263276
{
264277
let scale_factor = window.scale_factor() as f32;
265278
gui_state.egui_ctx().set_pixels_per_point(scale_factor);
266279
}
267-
#[cfg(any(target_arch = "wasm32", target_os = "android"))]
280+
#[cfg(target_arch = "wasm32")]
268281
{
269282
gui_state.egui_ctx().set_pixels_per_point(1.0);
270283
}

0 commit comments

Comments
 (0)