Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 4 additions & 9 deletions winit-appkit/src/monitor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ impl MonitorHandle {
/// bar and the Dock (see `NSScreen.visibleFrame`). `None` if the underlying
/// `NSScreen` can no longer be found.
pub(crate) fn work_area(&self) -> Option<(PhysicalPosition<i32>, PhysicalSize<u32>)> {
let scale_factor = self.scale_factor();
let scale_factor = self.scale_factor().unwrap_or(1.0);
run_on_main(|mtm| {
let visible_frame = self.ns_screen(mtm)?.visibleFrame();
let origin = flip_window_screen_coordinates(visible_frame);
Expand Down Expand Up @@ -226,16 +226,11 @@ impl MonitorHandleProvider for MonitorHandle {
// flip_window_screen_coordinates(self.ns_screen(mtm)?.frame())
let bounds = CGDisplayBounds(self.display_id());
let position = LogicalPosition::new(bounds.origin.x, bounds.origin.y);
Some(position.to_physical(self.scale_factor()))
self.scale_factor().map(|scale_factor| position.to_physical(scale_factor))
}

fn scale_factor(&self) -> f64 {
run_on_main(|mtm| {
match self.ns_screen(mtm) {
Some(screen) => screen.backingScaleFactor() as f64,
None => 1.0, // default to 1.0 when we can't find the screen
}
})
fn scale_factor(&self) -> Option<f64> {
run_on_main(|mtm| self.ns_screen(mtm).map(|screen| screen.backingScaleFactor() as f64))
}

fn current_video_mode(&self) -> Option<VideoMode> {
Expand Down
7 changes: 4 additions & 3 deletions winit-core/src/monitor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,11 +98,12 @@ pub trait MonitorHandleProvider: Any + fmt::Debug + Send + Sync {
///
/// See the [`dpi`] module for more information.
///
/// - **Wayland:** May differ from [`Window::scale_factor`].
/// - **Web:** Always returns `0.0` without `detailed_monitor_permissions`.
/// - **Wayland:** Returns `None` since the precise scale factor is not known before window
/// creation.
/// - **Web:** Returns `None` without `detailed_monitor_permissions`.
///
/// [`Window::scale_factor`]: crate::window::Window::scale_factor
fn scale_factor(&self) -> f64;
fn scale_factor(&self) -> Option<f64>;

fn current_video_mode(&self) -> Option<VideoMode>;

Expand Down
4 changes: 2 additions & 2 deletions winit-uikit/src/monitor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,8 +109,8 @@ impl MonitorHandleProvider for MonitorHandle {
Some((bounds.origin.x as f64, bounds.origin.y as f64).into())
}

fn scale_factor(&self) -> f64 {
self.ui_screen.get_on_main(|ui_screen| ui_screen.nativeScale()) as f64
fn scale_factor(&self) -> Option<f64> {
Some(self.ui_screen.get_on_main(|ui_screen| ui_screen.nativeScale()) as f64)
}

fn current_video_mode(&self) -> Option<VideoMode> {
Expand Down
8 changes: 5 additions & 3 deletions winit-wayland/src/output.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,11 @@ impl CoreMonitorHandle for MonitorHandle {
}))
}

fn scale_factor(&self) -> f64 {
let output_data = self.proxy.data::<OutputData>().unwrap();
output_data.scale_factor() as f64
fn scale_factor(&self) -> Option<f64> {
// Although we can extract an integer scale factor via
// self.proxy.data::<OutputData>() this often differs from the real
// scale factor (due to usage of fractional scaling).
None
}

fn current_video_mode(&self) -> Option<winit_core::monitor::VideoMode> {
Expand Down
8 changes: 4 additions & 4 deletions winit-web/src/monitor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ impl MonitorHandleProvider for MonitorHandle {
self.id.unwrap_or_default()
}

fn scale_factor(&self) -> f64 {
fn scale_factor(&self) -> Option<f64> {
self.inner.queue(|inner| inner.scale_factor())
}

Expand Down Expand Up @@ -276,10 +276,10 @@ impl Inner {
Self { window, engine, screen, orientation: OnceCell::new() }
}

fn scale_factor(&self) -> f64 {
fn scale_factor(&self) -> Option<f64> {
match &self.screen {
Screen::Screen(_) => 0.,
Screen::Detailed { screen, .. } => screen.device_pixel_ratio(),
Screen::Screen(_) => None,
Screen::Detailed { screen, .. } => Some(screen.device_pixel_ratio()),
}
}

Expand Down
4 changes: 2 additions & 2 deletions winit-win32/src/monitor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -192,8 +192,8 @@ impl MonitorHandleProvider for MonitorHandle {
.ok()
}

fn scale_factor(&self) -> f64 {
dpi_to_scale_factor(get_monitor_dpi(self.0).unwrap_or(96))
fn scale_factor(&self) -> Option<f64> {
get_monitor_dpi(self.0).map(dpi_to_scale_factor)
}

fn current_video_mode(&self) -> Option<winit_core::monitor::VideoMode> {
Expand Down
4 changes: 2 additions & 2 deletions winit-x11/src/monitor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,8 @@ impl MonitorHandleProvider for MonitorHandle {
Some(self.position.into())
}

fn scale_factor(&self) -> f64 {
self.scale_factor
fn scale_factor(&self) -> Option<f64> {
Some(self.scale_factor)
}

fn current_video_mode(&self) -> Option<VideoMode> {
Expand Down
2 changes: 1 addition & 1 deletion winit-x11/src/window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -510,7 +510,7 @@ impl UnownedWindow {
})
.unwrap_or_else(|| monitors.swap_remove(0))
};
let scale_factor = guessed_monitor.scale_factor();
let scale_factor = guessed_monitor.scale_factor().unwrap_or(1.0);

info!("Guessed window scale factor: {}", scale_factor);

Expand Down
4 changes: 3 additions & 1 deletion winit/examples/application.rs
Original file line number Diff line number Diff line change
Expand Up @@ -342,7 +342,9 @@ impl Application {
info!(" Position: {x},{y}");
}

info!(" Scale factor: {}", monitor.scale_factor());
if let Some(factor) = monitor.scale_factor() {
info!(" Scale factor: {factor}");
}

info!(" Available modes (width x height x bit-depth):");
for mode in monitor.video_modes() {
Expand Down
3 changes: 3 additions & 0 deletions winit/src/changelog/unreleased.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,9 @@ changelog entry.

## Unreleased

### Changed
- `MonitorHandleProvider::scale_factor` now returns `Option<f64>`.

### Fixed

- On Windows, fix a crash occurring when trying to create a DXGI surface on a window that is both fullscreen and always on top.
Expand Down
Loading