Skip to content

Commit

Permalink
v2
Browse files Browse the repository at this point in the history
  • Loading branch information
kchibisov committed Dec 27, 2024
1 parent 6c0c47d commit 05b0221
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 37 deletions.
4 changes: 2 additions & 2 deletions src/changelog/unreleased.md
Original file line number Diff line number Diff line change
Expand Up @@ -160,8 +160,8 @@ changelog entry.
- On macOS, no longer emit `Focused` upon window creation.
- On iOS, emit more events immediately, instead of queuing them.
- Update `smol_str` to version `0.3`
- `VideoModeHandle` to `VideoMode` which stores plain data.
- `Fullscreen::Exclusive` to take `(MonitorHandle, VideoMode)`.
- Rename `VideoModeHandle` to `VideoMode` which stores plain data.
- Make `Fullscreen::Exclusive` to take `(MonitorHandle, VideoMode)`.

### Removed

Expand Down
28 changes: 11 additions & 17 deletions src/monitor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use crate::platform_impl;

/// Describes a fullscreen video mode of a monitor.
///
/// Can be acquired with [`MonitorHandle::video_modes`].
/// Can be retrieved with [`MonitorHandle::video_modes()`].
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct VideoMode {
pub(crate) size: PhysicalSize<u32>,
Expand Down Expand Up @@ -39,24 +39,18 @@ impl VideoMode {

impl fmt::Display for VideoMode {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(
f,
"{}x{} {}{}",
self.size.width,
self.size.height,
self.refresh_rate_millihertz.map(|rate| format!("@ {rate} mHz ")).unwrap_or_default(),
self.bit_depth.map(|bit_depth| format!("({bit_depth} bpp)")).unwrap_or_default(),
)
}
}
write!(f, "{}x{}", self.size.width, self.size.height)?;

/// Fullscreen modes.
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum Fullscreen {
Exclusive(MonitorHandle, VideoMode),
if let Some(refresh_rate) = self.refresh_rate_millihertz.as_ref() {
write!(f, "@{refresh_rate}mHz")?;
}

/// Providing `None` to `Borderless` will fullscreen on the current monitor.
Borderless(Option<MonitorHandle>),
if let Some(bit_depth) = self.bit_depth.as_ref() {
write!(f, " ({bit_depth} bpp)")?;
}

Ok(())
}
}

/// Handle to a monitor.
Expand Down
16 changes: 5 additions & 11 deletions src/platform_impl/linux/x11/monitor.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
use std::num::{NonZeroU16, NonZeroU32};
use std::num::NonZeroU32;

use x11rb::connection::RequestConnection;
use x11rb::protocol::randr::{self, ConnectionExt as _};
use x11rb::protocol::xproto;

use super::{util, X11Error, XConnection};
use crate::dpi::{PhysicalPosition, PhysicalSize};
use crate::dpi::PhysicalPosition;
use crate::monitor::VideoMode;

// Used for testing. This should always be committed as false.
Expand All @@ -21,20 +21,14 @@ impl XConnection {
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct VideoModeHandle {
pub(crate) current: bool,
pub(crate) size: PhysicalSize<u32>,
pub(crate) bit_depth: Option<NonZeroU16>,
pub(crate) refresh_rate_millihertz: Option<NonZeroU32>,
pub(crate) mode: VideoMode,
pub(crate) native_mode: randr::Mode,
pub(crate) monitor: Option<MonitorHandle>,
}

impl From<VideoModeHandle> for VideoMode {
fn from(mode: VideoModeHandle) -> Self {
Self {
size: mode.size,
bit_depth: mode.bit_depth,
refresh_rate_millihertz: mode.refresh_rate_millihertz,
}
fn from(handle: VideoModeHandle) -> Self {
handle.mode
}
}

Expand Down
9 changes: 6 additions & 3 deletions src/platform_impl/linux/x11/util/randr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use x11rb::protocol::randr::{self, ConnectionExt as _};

use super::*;
use crate::dpi::validate_scale_factor;
use crate::monitor::VideoMode;
use crate::platform_impl::platform::x11::{monitor, VideoModeHandle};

/// Represents values of `WINIT_HIDPI_FACTOR`.
Expand Down Expand Up @@ -85,9 +86,11 @@ impl XConnection {
.map(|mode| {
VideoModeHandle {
current: mode.id == current_mode,
size: (mode.width as u32, mode.height as u32).into(),
refresh_rate_millihertz: monitor::mode_refresh_rate_millihertz(mode),
bit_depth: NonZeroU16::new(bit_depth as u16),
mode: VideoMode {
size: (mode.width as u32, mode.height as u32).into(),
refresh_rate_millihertz: monitor::mode_refresh_rate_millihertz(mode),
bit_depth: NonZeroU16::new(bit_depth as u16),
},
native_mode: mode.id,
// This is populated in `MonitorHandle::video_modes` as the
// video mode is returned to the user
Expand Down
5 changes: 1 addition & 4 deletions src/platform_impl/linux/x11/window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1088,10 +1088,7 @@ impl UnownedWindow {

if let Some(native_mode) = video_mode.and_then(|requested| {
monitor.video_modes.iter().find_map(|mode| {
if mode.refresh_rate_millihertz == requested.refresh_rate_millihertz
&& mode.size == requested.size
&& mode.bit_depth == requested.bit_depth
{
if mode.mode == requested {
Some(mode.native_mode)
} else {
None
Expand Down

0 comments on commit 05b0221

Please sign in to comment.