Slightly reduce number of cfgs (#3071)

* Make Linux platforms less dependent on the root monitor handle

* Add various functions to the Wayland platform to reduce cfgs

* Don't use a cfg in listen_device_events

* Don't use a cfg in set_content_protected

* Fix instance of a target_os cfg
This commit is contained in:
Mads Marquart 2023-09-01 23:14:16 +02:00 committed by GitHub
parent 67b041e231
commit a06ea45c0f
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
19 changed files with 121 additions and 125 deletions

View file

@ -1317,7 +1317,8 @@ impl<T: 'static> EventProcessor<T> {
for (window_id, window) in wt.windows.borrow().iter() {
if let Some(window) = window.upgrade() {
// Check if the window is on this monitor
let monitor = window.current_monitor();
let monitor =
window.shared_state_lock().last_monitor.clone();
if monitor.name == new_monitor.name {
let (width, height) = window.inner_size_physical();
let (new_width, new_height) = window.adjust_for_dpi(

View file

@ -706,7 +706,15 @@ impl<T> EventLoopWindowTarget<T> {
&self.xconn
}
pub fn set_listen_device_events(&self, allowed: DeviceEvents) {
pub fn available_monitors(&self) -> impl Iterator<Item = MonitorHandle> {
self.xconn.available_monitors().into_iter().flatten()
}
pub fn primary_monitor(&self) -> Option<MonitorHandle> {
self.xconn.primary_monitor().ok()
}
pub fn listen_device_events(&self, allowed: DeviceEvents) {
self.device_events.set(allowed);
}

View file

@ -1,7 +1,7 @@
use super::{util, X11Error, XConnection};
use crate::{
dpi::{PhysicalPosition, PhysicalSize},
platform_impl::{MonitorHandle as PlatformMonitorHandle, VideoMode as PlatformVideoMode},
platform_impl::VideoMode as PlatformVideoMode,
};
use x11rb::{
connection::RequestConnection,
@ -47,8 +47,8 @@ impl VideoMode {
}
#[inline]
pub fn monitor(&self) -> PlatformMonitorHandle {
PlatformMonitorHandle::X(self.monitor.clone().unwrap())
pub fn monitor(&self) -> MonitorHandle {
self.monitor.clone().unwrap()
}
}

View file

@ -1,7 +1,7 @@
#![allow(clippy::assertions_on_constants)]
use super::*;
use crate::icon::{Icon, Pixel, PIXEL_SIZE};
use crate::icon::{Pixel, RgbaIcon, PIXEL_SIZE};
impl Pixel {
pub fn to_packed_argb(&self) -> Cardinal {
@ -18,16 +18,15 @@ impl Pixel {
}
}
impl Icon {
impl RgbaIcon {
pub(crate) fn to_cardinals(&self) -> Vec<Cardinal> {
let rgba_icon = &self.inner;
assert_eq!(rgba_icon.rgba.len() % PIXEL_SIZE, 0);
let pixel_count = rgba_icon.rgba.len() / PIXEL_SIZE;
assert_eq!(pixel_count, (rgba_icon.width * rgba_icon.height) as usize);
assert_eq!(self.rgba.len() % PIXEL_SIZE, 0);
let pixel_count = self.rgba.len() / PIXEL_SIZE;
assert_eq!(pixel_count, (self.width * self.height) as usize);
let mut data = Vec::with_capacity(pixel_count);
data.push(rgba_icon.width as Cardinal);
data.push(rgba_icon.height as Cardinal);
let pixels = rgba_icon.rgba.as_ptr() as *const Pixel;
data.push(self.width as Cardinal);
data.push(self.height as Cardinal);
let pixels = self.rgba.as_ptr() as *const Pixel;
for pixel_index in 0..pixel_count {
let pixel = unsafe { &*pixels.add(pixel_index) };
data.push(pixel.to_packed_argb());

View file

@ -23,11 +23,11 @@ use crate::{
event_loop::AsyncRequestSerial,
platform_impl::{
x11::{atoms::*, MonitorHandle as X11MonitorHandle, WakeSender, X11Error},
Fullscreen, MonitorHandle as PlatformMonitorHandle, OsError,
Fullscreen, MonitorHandle as PlatformMonitorHandle, OsError, PlatformIcon,
PlatformSpecificWindowBuilderAttributes, VideoMode as PlatformVideoMode,
},
window::{
CursorGrabMode, CursorIcon, Icon, ImePurpose, ResizeDirection, Theme, UserAttentionType,
CursorGrabMode, CursorIcon, ImePurpose, ResizeDirection, Theme, UserAttentionType,
WindowAttributes, WindowButtons, WindowLevel,
},
};
@ -469,7 +469,7 @@ impl UnownedWindow {
// Set window icons
if let Some(icon) = window_attrs.window_icon {
leap!(window.set_icon_inner(icon)).ignore_error();
leap!(window.set_icon_inner(icon.inner)).ignore_error();
}
// Opt into handling window close
@ -778,7 +778,9 @@ impl UnownedWindow {
Fullscreen::Borderless(Some(PlatformMonitorHandle::X(monitor))) => {
(None, monitor)
}
Fullscreen::Borderless(None) => (None, self.current_monitor()),
Fullscreen::Borderless(None) => {
(None, self.shared_state_lock().last_monitor.clone())
}
#[cfg(wayland_platform)]
_ => unreachable!(),
};
@ -874,9 +876,8 @@ impl UnownedWindow {
}
}
#[inline]
pub fn current_monitor(&self) -> X11MonitorHandle {
self.shared_state_lock().last_monitor.clone()
pub fn current_monitor(&self) -> Option<X11MonitorHandle> {
Some(self.shared_state_lock().last_monitor.clone())
}
pub fn available_monitors(&self) -> Vec<X11MonitorHandle> {
@ -885,10 +886,12 @@ impl UnownedWindow {
.expect("Failed to get available monitors")
}
pub fn primary_monitor(&self) -> X11MonitorHandle {
self.xconn
.primary_monitor()
.expect("Failed to get primary monitor")
pub fn primary_monitor(&self) -> Option<X11MonitorHandle> {
Some(
self.xconn
.primary_monitor()
.expect("Failed to get primary monitor"),
)
}
#[inline]
@ -1077,7 +1080,7 @@ impl UnownedWindow {
.expect("Failed to set window-level state");
}
fn set_icon_inner(&self, icon: Icon) -> Result<VoidCookie<'_>, X11Error> {
fn set_icon_inner(&self, icon: PlatformIcon) -> Result<VoidCookie<'_>, X11Error> {
let atoms = self.xconn.atoms();
let icon_atom = atoms[_NET_WM_ICON];
let data = icon.to_cardinals();
@ -1104,7 +1107,7 @@ impl UnownedWindow {
}
#[inline]
pub fn set_window_icon(&self, icon: Option<Icon>) {
pub(crate) fn set_window_icon(&self, icon: Option<PlatformIcon>) {
match icon {
Some(icon) => self.set_icon_inner(icon),
None => self.unset_icon_inner(),
@ -1565,7 +1568,7 @@ impl UnownedWindow {
#[inline]
pub fn scale_factor(&self) -> f64 {
self.current_monitor().scale_factor
self.shared_state_lock().last_monitor.scale_factor
}
pub fn set_cursor_position_physical(&self, x: i32, y: i32) -> Result<(), ExternalError> {
@ -1806,6 +1809,8 @@ impl UnownedWindow {
None
}
pub fn set_content_protected(&self, _protected: bool) {}
#[inline]
pub fn has_focus(&self) -> bool {
self.shared_state_lock().has_focus