Skip to content

Commit

Permalink
Fix wrong pixels-per-point for wgpu
Browse files Browse the repository at this point in the history
  • Loading branch information
emilk committed Nov 7, 2023
1 parent d313487 commit 25fd7ba
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 35 deletions.
77 changes: 46 additions & 31 deletions crates/eframe/src/native/run.rs
Original file line number Diff line number Diff line change
Expand Up @@ -787,10 +787,11 @@ mod glow_integration {
let native_pixels_per_point = window.scale_factor() as f32;

if win.egui_winit.is_none() {
let mut egui_winit = egui_winit::State::new(event_loop);
egui_winit.set_max_texture_side(self.max_texture_side);
egui_winit.set_pixels_per_point(native_pixels_per_point);
win.egui_winit = Some(egui_winit);
win.egui_winit = Some(egui_winit::State::new(
event_loop,
Some(native_pixels_per_point),
Some(self.max_texture_side),
));
}

win.gl_surface = Some(gl_surface);
Expand Down Expand Up @@ -1898,7 +1899,7 @@ mod wgpu_integration {
pub struct Viewport {
window: Option<Rc<RefCell<winit::window::Window>>>,

state: Rc<RefCell<Option<egui_winit::State>>>,
egui_winit: Rc<RefCell<Option<egui_winit::State>>>,

/// `None` for sync viewports.
viewport_ui_cb: Option<Arc<Box<ViewportUiCallback>>>,
Expand Down Expand Up @@ -2001,9 +2002,9 @@ mod wgpu_integration {
};
let viewport_builders = running.builders.borrow();

for (id, Viewport { window, state, .. }) in running.viewports.borrow_mut().iter_mut() {
for (id, viewport) in running.viewports.borrow_mut().iter_mut() {
let builder = viewport_builders.get(id).unwrap();
if window.is_some() {
if viewport.window.is_some() {
continue;
}

Expand All @@ -2012,8 +2013,8 @@ mod wgpu_integration {
builder,
&mut running.viewport_maps.borrow_mut(),
&mut running.painter.borrow_mut(),
window,
&mut state.borrow_mut(),
&mut viewport.window,
&mut viewport.egui_winit.borrow_mut(),
event_loop,
);
}
Expand All @@ -2025,7 +2026,7 @@ mod wgpu_integration {
windows_id: &mut HashMap<winit::window::WindowId, ViewportId>,
painter: &mut egui_wgpu::winit::Painter,
window: &mut Option<Rc<RefCell<winit::window::Window>>>,
state: &mut Option<egui_winit::State>,
egui_winit: &mut Option<egui_winit::State>,
event_loop: &EventLoopWindowTarget<UserEvent>,
) {
if let Ok(new_window) = create_winit_window_builder(builder).build(event_loop) {
Expand All @@ -2034,8 +2035,14 @@ mod wgpu_integration {
if let Err(err) = pollster::block_on(painter.set_window(id, Some(&new_window))) {
log::error!("on set_window: viewport_id {id:?} {err}");
}

*egui_winit = Some(egui_winit::State::new(
event_loop,
Some(new_window.scale_factor() as f32),
painter.max_texture_side(),
));

*window = Some(Rc::new(RefCell::new(new_window)));
*state = Some(egui_winit::State::new(event_loop));
}
}

Expand Down Expand Up @@ -2133,11 +2140,16 @@ mod wgpu_integration {
});
}

let mut state = egui_winit::State::new(event_loop);
let mut egui_winit = egui_winit::State::new(
event_loop,
Some(window.scale_factor() as f32),
painter.max_texture_side(),
);

#[cfg(feature = "accesskit")]
{
let event_loop_proxy = self.repaint_proxy.lock().clone();
integration.init_accesskit(&mut state, &window, event_loop_proxy);
integration.init_accesskit(&mut egui_winit, &window, event_loop_proxy);
}
let theme = system_theme.unwrap_or(self.native_options.default_theme);
integration.egui_ctx.set_visuals(theme.egui_visuals());
Expand All @@ -2160,7 +2172,7 @@ mod wgpu_integration {
};

if app.warm_up_enabled() {
integration.warm_up(app.as_mut(), &window, &mut state);
integration.warm_up(app.as_mut(), &window, &mut egui_winit);
}

let mut viewport_maps = HashMap::default();
Expand All @@ -2172,7 +2184,7 @@ mod wgpu_integration {
ViewportId::ROOT,
Viewport {
window: Some(Rc::new(RefCell::new(window))),
state: Rc::new(RefCell::new(Some(state))),
egui_winit: Rc::new(RefCell::new(Some(egui_winit))),
viewport_ui_cb: None,
parent_id: ViewportId::ROOT,
},
Expand Down Expand Up @@ -2263,13 +2275,16 @@ mod wgpu_integration {

let mut viewports = viewports.borrow_mut();

let Viewport { window, state, .. } =
viewports.entry(id_pair.this).or_insert(Viewport {
window: None,
state: Rc::new(RefCell::new(None)),
viewport_ui_cb: None,
parent_id: id_pair.parent,
});
let Viewport {
window,
egui_winit: state,
..
} = viewports.entry(id_pair.this).or_insert(Viewport {
window: None,
egui_winit: Rc::new(RefCell::new(None)),
viewport_ui_cb: None,
parent_id: id_pair.parent,
});
builders
.entry(id_pair.this)
.or_insert(viewport_builder.clone());
Expand All @@ -2295,7 +2310,7 @@ mod wgpu_integration {
// Render sync viewport:
let viewport = viewports.borrow().get(&id_pair.this).cloned();
let Some(viewport) = viewport else { return };
let Some(winit_state) = &mut *viewport.state.borrow_mut() else {
let Some(winit_state) = &mut *viewport.egui_winit.borrow_mut() else {
return;
};
let Some(window) = viewport.window else {
Expand Down Expand Up @@ -2431,7 +2446,7 @@ mod wgpu_integration {
viewport_id,
Viewport {
window: Some(window),
state,
egui_winit: state,
viewport_ui_cb,
parent_id,
},
Expand Down Expand Up @@ -2553,7 +2568,7 @@ mod wgpu_integration {
if recreate {
if let Some(viewport) = viewports.get_mut(&id_pair.this) {
viewport.window = None;
viewport.state.replace(None);
viewport.egui_winit.replace(None);
}
} else if let Some(Viewport {
window: Some(window),
Expand All @@ -2572,7 +2587,7 @@ mod wgpu_integration {
id_pair.this,
Viewport {
window: None,
state: Rc::new(RefCell::new(None)),
egui_winit: Rc::new(RefCell::new(None)),
viewport_ui_cb,
parent_id: id_pair.parent,
},
Expand Down Expand Up @@ -2768,11 +2783,11 @@ mod wgpu_integration {
_ => {}
};

let event_response = if let Some((id, Viewport { state, .. })) = viewport_id
.and_then(|id| {
let event_response = if let Some((id, viewport)) =
viewport_id.and_then(|id| {
running.viewports.borrow().get(&id).map(|w| (id, w.clone()))
}) {
if let Some(state) = &mut *state.borrow_mut() {
if let Some(state) = &mut *viewport.egui_winit.borrow_mut() {
Some(running.integration.borrow_mut().on_event(
running.app.as_mut(),
event,
Expand Down Expand Up @@ -2810,13 +2825,13 @@ mod wgpu_integration {
accesskit_winit::ActionRequestEvent { request, window_id },
)) => {
if let Some(running) = &mut self.running {
if let Some(Viewport { state, .. }) = running
if let Some(viewport) = running
.viewport_maps
.borrow()
.get(window_id)
.and_then(|id| running.viewports.borrow().get(id).cloned())
{
if let Some(state) = &mut *state.borrow_mut() {
if let Some(state) = &mut *viewport.egui_winit.borrow_mut() {
state.on_accesskit_action_request(request.clone());
}
}
Expand Down
15 changes: 13 additions & 2 deletions crates/egui-winit/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,13 +87,17 @@ pub struct State {

impl State {
/// Construct a new instance
pub fn new(display_target: &dyn HasRawDisplayHandle) -> Self {
pub fn new(
display_target: &dyn HasRawDisplayHandle,
native_pixels_per_point: Option<f32>,
max_texture_side: Option<usize>,
) -> Self {
let egui_input = egui::RawInput {
focused: false, // winit will tell us when we have focus
..Default::default()
};

Self {
let mut slf = Self {
start_time: web_time::Instant::now(),
egui_input,
pointer_pos_in_points: None,
Expand All @@ -112,7 +116,14 @@ impl State {
accesskit: None,

allow_ime: false,
};
if let Some(native_pixels_per_point) = native_pixels_per_point {
slf.set_pixels_per_point(native_pixels_per_point);
}
if let Some(max_texture_side) = max_texture_side {
slf.set_max_texture_side(max_texture_side);
}
slf
}

#[cfg(feature = "accesskit")]
Expand Down
2 changes: 1 addition & 1 deletion crates/egui_glow/examples/pure_glow.rs
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ fn main() {
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, gl.clone(), None);
let mut egui_glow = egui_glow::EguiGlow::new(&event_loop, gl.clone(), None, None);

let event_loop_proxy = egui::mutex::Mutex::new(event_loop.create_proxy());
egui_glow
Expand Down
7 changes: 6 additions & 1 deletion crates/egui_glow/src/winit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ impl EguiGlow {
event_loop: &winit::event_loop::EventLoopWindowTarget<E>,
gl: std::sync::Arc<glow::Context>,
shader_version: Option<ShaderVersion>,
native_pixels_per_point: Option<f32>,
) -> Self {
let painter = crate::Painter::new(gl, "", shader_version)
.map_err(|err| {
Expand All @@ -29,7 +30,11 @@ impl EguiGlow {

Self {
egui_ctx: Default::default(),
egui_winit: egui_winit::State::new(event_loop),
egui_winit: egui_winit::State::new(
event_loop,
native_pixels_per_point,
Some(painter.max_texture_side()),
),
painter,
shapes: Default::default(),
textures_delta: Default::default(),
Expand Down

0 comments on commit 25fd7ba

Please sign in to comment.