Skip to content

Commit

Permalink
Remove MonitorHandle::size/refresh_rate_millihertz()
Browse files Browse the repository at this point in the history
  • Loading branch information
daxpedda committed Aug 7, 2024
1 parent 2e53533 commit 0ffcfd8
Show file tree
Hide file tree
Showing 13 changed files with 81 additions and 218 deletions.
28 changes: 17 additions & 11 deletions examples/window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -302,15 +302,17 @@ impl Application {
info!("{intro}: [no name]");
}

let PhysicalSize { width, height } = monitor.size();
info!(
" Current mode: {width}x{height}{}",
if let Some(m_hz) = monitor.refresh_rate_millihertz() {
format!(" @ {}.{} Hz", m_hz / 1000, m_hz % 1000)
} else {
String::new()
}
);
if let Some(current_mode) = monitor.current_video_mode() {
let PhysicalSize { width, height } = current_mode.size();
info!(
" Current mode: {width}x{height}{}",
if let Some(m_hz) = current_mode.refresh_rate_millihertz() {
format!(" @ {}.{} Hz", m_hz / 1000, m_hz % 1000)
} else {
String::new()
}
);
}

let PhysicalPosition { x, y } = monitor.position();
info!(" Position: {x},{y}");
Expand All @@ -321,8 +323,12 @@ impl Application {
for mode in monitor.video_modes() {
let PhysicalSize { width, height } = mode.size();
let bits = mode.bit_depth();
let m_hz = mode.refresh_rate_millihertz();
info!(" {width}x{height}x{bits} @ {}.{} Hz", m_hz / 1000, m_hz % 1000);
let m_hz = if let Some(m_hz) = mode.refresh_rate_millihertz() {
format!(" @ {}.{} Hz", m_hz / 1000, m_hz % 1000)
} else {
String::new()
};
info!(" {width}x{height}x{bits}{m_hz}");
}
}
}
Expand Down
3 changes: 3 additions & 0 deletions src/changelog/unreleased.md
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ changelog entry.
accelerated, if the browser supports it.
- `(Active)EventLoop::create_custom_cursor()` now returns a `Result<CustomCursor, ExternalError>`.
- Changed how `ModifiersState` is serialized by Serde.
- `VideoModeHandle::refresh_rate_millihertz()` now returns an `Option`.

### Removed

Expand All @@ -118,6 +119,8 @@ changelog entry.
- Remove `DeviceEvent::Added` and `DeviceEvent::Removed`.
- Remove `DeviceEvent::Motion` and `WindowEvent::AxisMotion`.
- Remove `Touch::id` in favor of `Touch::finger_id`.
- Remove `MonitorHandle::size()` and `refresh_rate_millihertz()` in favor of
`MonitorHandle::current_video_mode()`.

### Fixed

Expand Down
32 changes: 3 additions & 29 deletions src/monitor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,12 +64,8 @@ impl VideoModeHandle {
}

/// Returns the refresh rate of this video mode in mHz.
///
/// ## Platform-specific
///
/// **Web:** Always returns `0`.
#[inline]
pub fn refresh_rate_millihertz(&self) -> u32 {
pub fn refresh_rate_millihertz(&self) -> Option<u32> {
self.video_mode.refresh_rate_millihertz()
}

Expand All @@ -85,10 +81,10 @@ impl std::fmt::Display for VideoModeHandle {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(
f,
"{}x{} @ {} mHz ({} bpp)",
"{}x{} {}({} bpp)",
self.size().width,
self.size().height,
self.refresh_rate_millihertz(),
self.refresh_rate_millihertz().map(|rate| format!("@ {rate} mHz ")).unwrap_or_default(),
self.bit_depth()
)
}
Expand Down Expand Up @@ -139,12 +135,6 @@ impl MonitorHandle {
self.inner.name()
}

/// Returns the monitor's resolution.
#[inline]
pub fn size(&self) -> PhysicalSize<u32> {
self.inner.size()
}

/// Returns the top-left corner position of the monitor relative to the larger full
/// screen area.
///
Expand All @@ -161,22 +151,6 @@ impl MonitorHandle {
self.inner.position()
}

/// The monitor refresh rate used by the system.
///
/// Return `Some` if succeed, or `None` if failed, which usually happens when the monitor
/// the window is on is removed.
///
/// When using exclusive fullscreen, the refresh rate of the [`VideoModeHandle`] that was
/// used to enter fullscreen should be used instead.
///
/// ## Platform-specific
///
/// **Web:** Always returns [`None`].
#[inline]
pub fn refresh_rate_millihertz(&self) -> Option<u32> {
self.inner.refresh_rate_millihertz()
}

/// Returns the scale factor of the underlying monitor. To map logical pixels to physical
/// pixels and vice versa, use [`Window::scale_factor`].
///
Expand Down
43 changes: 16 additions & 27 deletions src/platform_impl/android/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -204,9 +204,7 @@ impl EventLoop {
let old_scale_factor = monitor.scale_factor();
let scale_factor = monitor.scale_factor();
if (scale_factor - old_scale_factor).abs() < f64::EPSILON {
let new_inner_size = Arc::new(Mutex::new(
MonitorHandle::new(self.android_app.clone()).size(),
));
let new_inner_size = Arc::new(Mutex::new(screen_size(&self.android_app)));
let window_id = window::WindowId(WindowId);
let event = event::WindowEvent::ScaleFactorChanged {
inner_size_writer: InnerSizeWriter::new(Arc::downgrade(
Expand Down Expand Up @@ -789,7 +787,7 @@ impl Window {
}

pub fn outer_size(&self) -> PhysicalSize<u32> {
MonitorHandle::new(self.app.clone()).size()
screen_size(&self.app)
}

pub fn set_min_inner_size(&self, _: Option<Size>) {}
Expand Down Expand Up @@ -982,14 +980,6 @@ impl MonitorHandle {
Some("Android Device".to_owned())
}

pub fn size(&self) -> PhysicalSize<u32> {
if let Some(native_window) = self.app.native_window() {
PhysicalSize::new(native_window.width() as _, native_window.height() as _)
} else {
PhysicalSize::new(0, 0)
}
}

pub fn position(&self) -> PhysicalPosition<i32> {
(0, 0).into()
}
Expand All @@ -998,19 +988,11 @@ impl MonitorHandle {
self.app.config().density().map(|dpi| dpi as f64 / 160.0).unwrap_or(1.0)
}

pub fn refresh_rate_millihertz(&self) -> Option<u32> {
// FIXME no way to get real refresh rate for now.
None
}

pub fn current_video_mode(&self) -> Option<VideoModeHandle> {
let size = self.size().into();
// FIXME this is not the real refresh rate
// (it is guaranteed to support 32 bit color though)
Some(VideoModeHandle {
size,
size: screen_size(&self.app),
// FIXME: it is guaranteed to support 32 bit color though
bit_depth: 32,
refresh_rate_millihertz: 60000,
monitor: self.clone(),
})
}
Expand All @@ -1022,26 +1004,33 @@ impl MonitorHandle {

#[derive(Clone, Debug, Eq, Hash, PartialEq)]
pub struct VideoModeHandle {
size: (u32, u32),
size: PhysicalSize<u32>,
bit_depth: u16,
refresh_rate_millihertz: u32,
monitor: MonitorHandle,
}

impl VideoModeHandle {
pub fn size(&self) -> PhysicalSize<u32> {
self.size.into()
self.size
}

pub fn bit_depth(&self) -> u16 {
self.bit_depth
}

pub fn refresh_rate_millihertz(&self) -> u32 {
self.refresh_rate_millihertz
pub fn refresh_rate_millihertz(&self) -> Option<u32> {
None
}

pub fn monitor(&self) -> MonitorHandle {
self.monitor.clone()
}
}

fn screen_size(app: &AndroidApp) -> PhysicalSize<u32> {
if let Some(native_window) = app.native_window() {
PhysicalSize::new(native_window.width() as _, native_window.height() as _)
} else {
PhysicalSize::new(0, 0)
}
}
27 changes: 11 additions & 16 deletions src/platform_impl/apple/appkit/monitor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ use crate::dpi::{LogicalPosition, PhysicalPosition, PhysicalSize};
pub struct VideoModeHandle {
size: PhysicalSize<u32>,
bit_depth: u16,
refresh_rate_millihertz: u32,
refresh_rate_millihertz: Option<u32>,
pub(crate) monitor: MonitorHandle,
pub(crate) native_mode: NativeDisplayMode,
}
Expand Down Expand Up @@ -80,7 +80,11 @@ impl Clone for NativeDisplayMode {
}

impl VideoModeHandle {
fn new(monitor: MonitorHandle, mode: NativeDisplayMode, refresh_rate_millihertz: u32) -> Self {
fn new(
monitor: MonitorHandle,
mode: NativeDisplayMode,
refresh_rate_millihertz: Option<u32>,
) -> Self {
unsafe {
let pixel_encoding =
CFString::wrap_under_create_rule(ffi::CGDisplayModeCopyPixelEncoding(mode.0))
Expand Down Expand Up @@ -116,7 +120,7 @@ impl VideoModeHandle {
self.bit_depth
}

pub fn refresh_rate_millihertz(&self) -> u32 {
pub fn refresh_rate_millihertz(&self) -> Option<u32> {
self.refresh_rate_millihertz
}

Expand Down Expand Up @@ -186,7 +190,6 @@ impl fmt::Debug for MonitorHandle {
f.debug_struct("MonitorHandle")
.field("name", &self.name())
.field("native_identifier", &self.native_identifier())
.field("size", &self.size())
.field("position", &self.position())
.field("scale_factor", &self.scale_factor())
.field("refresh_rate_millihertz", &self.refresh_rate_millihertz())
Expand All @@ -212,14 +215,6 @@ impl MonitorHandle {
self.0
}

pub fn size(&self) -> PhysicalSize<u32> {
let MonitorHandle(display_id) = *self;
let display = CGDisplay::new(display_id);
let height = display.pixels_high();
let width = display.pixels_wide();
PhysicalSize::from_logical::<_, f64>((width as f64, height as f64), self.scale_factor())
}

#[inline]
pub fn position(&self) -> PhysicalPosition<i32> {
// This is already in screen coordinates. If we were using `NSScreen`,
Expand All @@ -239,20 +234,20 @@ impl MonitorHandle {
})
}

pub fn refresh_rate_millihertz(&self) -> Option<u32> {
fn refresh_rate_millihertz(&self) -> Option<u32> {
let current_display_mode =
NativeDisplayMode(unsafe { CGDisplayCopyDisplayMode(self.0) } as _);
refresh_rate_millihertz(self.0, &current_display_mode)
}

pub fn current_video_mode(&self) -> Option<VideoModeHandle> {
let mode = NativeDisplayMode(unsafe { CGDisplayCopyDisplayMode(self.0) } as _);
let refresh_rate_millihertz = refresh_rate_millihertz(self.0, &mode).unwrap_or(0);
let refresh_rate_millihertz = refresh_rate_millihertz(self.0, &mode);
Some(VideoModeHandle::new(self.clone(), mode, refresh_rate_millihertz))
}

pub fn video_modes(&self) -> impl Iterator<Item = VideoModeHandle> {
let refresh_rate_millihertz = self.refresh_rate_millihertz().unwrap_or(0);
let refresh_rate_millihertz = self.refresh_rate_millihertz();
let monitor = self.clone();

unsafe {
Expand All @@ -277,7 +272,7 @@ impl MonitorHandle {
// CGDisplayModeGetRefreshRate returns 0.0 for any display that
// isn't a CRT
let refresh_rate_millihertz = if cg_refresh_rate_hertz > 0 {
(cg_refresh_rate_hertz * 1000) as u32
Some((cg_refresh_rate_hertz * 1000) as u32)
} else {
refresh_rate_millihertz
};
Expand Down
15 changes: 2 additions & 13 deletions src/platform_impl/apple/uikit/monitor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,8 @@ impl VideoModeHandle {
self.bit_depth
}

pub fn refresh_rate_millihertz(&self) -> u32 {
self.refresh_rate_millihertz
pub fn refresh_rate_millihertz(&self) -> Option<u32> {
Some(self.refresh_rate_millihertz)
}

pub fn monitor(&self) -> MonitorHandle {
Expand Down Expand Up @@ -131,10 +131,8 @@ impl fmt::Debug for MonitorHandle {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("MonitorHandle")
.field("name", &self.name())
.field("size", &self.size())
.field("position", &self.position())
.field("scale_factor", &self.scale_factor())
.field("refresh_rate_millihertz", &self.refresh_rate_millihertz())
.finish_non_exhaustive()
}
}
Expand Down Expand Up @@ -164,11 +162,6 @@ impl MonitorHandle {
})
}

pub fn size(&self) -> PhysicalSize<u32> {
let bounds = self.ui_screen.get_on_main(|ui_screen| ui_screen.nativeBounds());
PhysicalSize::new(bounds.size.width as u32, bounds.size.height as u32)
}

pub fn position(&self) -> PhysicalPosition<i32> {
let bounds = self.ui_screen.get_on_main(|ui_screen| ui_screen.nativeBounds());
(bounds.origin.x as f64, bounds.origin.y as f64).into()
Expand All @@ -178,10 +171,6 @@ impl MonitorHandle {
self.ui_screen.get_on_main(|ui_screen| ui_screen.nativeScale()) as f64
}

pub fn refresh_rate_millihertz(&self) -> Option<u32> {
Some(self.ui_screen.get_on_main(|ui_screen| refresh_rate_millihertz(ui_screen)))
}

pub fn current_video_mode(&self) -> Option<VideoModeHandle> {
Some(run_on_main(|mtm| {
VideoModeHandle::new(
Expand Down
12 changes: 1 addition & 11 deletions src/platform_impl/linux/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -241,21 +241,11 @@ impl MonitorHandle {
x11_or_wayland!(match self; MonitorHandle(m) => m.native_identifier())
}

#[inline]
pub fn size(&self) -> PhysicalSize<u32> {
x11_or_wayland!(match self; MonitorHandle(m) => m.size())
}

#[inline]
pub fn position(&self) -> PhysicalPosition<i32> {
x11_or_wayland!(match self; MonitorHandle(m) => m.position())
}

#[inline]
pub fn refresh_rate_millihertz(&self) -> Option<u32> {
x11_or_wayland!(match self; MonitorHandle(m) => m.refresh_rate_millihertz())
}

#[inline]
pub fn scale_factor(&self) -> f64 {
x11_or_wayland!(match self; MonitorHandle(m) => m.scale_factor() as _)
Expand Down Expand Up @@ -292,7 +282,7 @@ impl VideoModeHandle {
}

#[inline]
pub fn refresh_rate_millihertz(&self) -> u32 {
pub fn refresh_rate_millihertz(&self) -> Option<u32> {
x11_or_wayland!(match self; VideoModeHandle(m) => m.refresh_rate_millihertz())
}

Expand Down
Loading

0 comments on commit 0ffcfd8

Please sign in to comment.