Skip to content

Commit

Permalink
Convert window position to logical coordinates
Browse files Browse the repository at this point in the history
`ViewportBuilder` expects position to be in logical coordinates, but it was being passed in pixels. To fix, scale by the active monitor size and `egui_zoom_factor`.
  • Loading branch information
avery-radmacher committed May 2, 2024
1 parent 6937f39 commit 9aeb724
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 3 deletions.
6 changes: 5 additions & 1 deletion crates/eframe/src/native/epi_integration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,11 @@ pub fn viewport_builder<E>(
}
window_settings.clamp_position_to_monitors(egui_zoom_factor, event_loop);

viewport_builder = window_settings.initialize_viewport_builder(viewport_builder);
viewport_builder = window_settings.initialize_viewport_builder(
egui_zoom_factor,
event_loop,
viewport_builder,
);
window_settings.inner_size_points()
} else {
if let Some(pos) = viewport_builder.position {
Expand Down
14 changes: 12 additions & 2 deletions crates/egui-winit/src/window_settings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,10 @@ impl WindowSettings {
self.inner_size_points
}

pub fn initialize_viewport_builder(
pub fn initialize_viewport_builder<E>(
&self,
egui_zoom_factor: f32,
event_loop: &winit::event_loop::EventLoopWindowTarget<E>,
mut viewport_builder: ViewportBuilder,
) -> ViewportBuilder {
crate::profile_function!();
Expand All @@ -64,7 +66,15 @@ impl WindowSettings {
self.outer_position_pixels
};
if let Some(pos) = pos_px {
viewport_builder = viewport_builder.with_position(pos);
let monitor_scale_factor = if let Some(inner_size_points) = self.inner_size_points {
find_active_monitor(egui_zoom_factor, event_loop, inner_size_points, &pos)
.map_or(1.0, |monitor| monitor.scale_factor() as f32)
} else {
1.0
};

let scaled_pos = pos / (egui_zoom_factor * monitor_scale_factor);
viewport_builder = viewport_builder.with_position(scaled_pos);
}

if let Some(inner_size_points) = self.inner_size_points {
Expand Down

0 comments on commit 9aeb724

Please sign in to comment.