Make 'primary_monitor' return 'Option<MonitorHandle>'

Certain platforms like Wayland don't have a concept of
primary Monitor in particular. To indicate that
'primary_monitor' will return 'None' as well as in cases
where the primary monitor can't be detected.

Fixes #1683.
This commit is contained in:
Kirill Chibisov 2020-09-07 20:20:47 +03:00 committed by GitHub
parent cac627ed05
commit d103dc2631
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
16 changed files with 86 additions and 56 deletions

View file

@ -463,8 +463,18 @@ impl Window {
}
#[inline]
pub fn primary_monitor(&self) -> MonitorHandle {
x11_or_wayland!(match self; Window(window) => window.primary_monitor(); as MonitorHandle)
pub fn primary_monitor(&self) -> Option<RootMonitorHandle> {
match self {
#[cfg(feature = "x11")]
&Window::X(ref window) => {
let primary_monitor = MonitorHandle::X(window.primary_monitor());
Some(RootMonitorHandle {
inner: primary_monitor,
})
}
#[cfg(feature = "wayland")]
&Window::Wayland(ref window) => window.primary_monitor(),
}
}
pub fn raw_window_handle(&self) -> RawWindowHandle {
@ -682,15 +692,16 @@ impl<T> EventLoopWindowTarget<T> {
}
#[inline]
pub fn primary_monitor(&self) -> MonitorHandle {
pub fn primary_monitor(&self) -> Option<RootMonitorHandle> {
match *self {
#[cfg(feature = "wayland")]
EventLoopWindowTarget::Wayland(ref evlp) => {
MonitorHandle::Wayland(evlp.primary_monitor())
}
EventLoopWindowTarget::Wayland(ref evlp) => evlp.primary_monitor(),
#[cfg(feature = "x11")]
EventLoopWindowTarget::X(ref evlp) => {
MonitorHandle::X(evlp.x_connection().primary_monitor())
let primary_monitor = MonitorHandle::X(evlp.x_connection().primary_monitor());
Some(RootMonitorHandle {
inner: primary_monitor,
})
}
}
}

View file

@ -648,8 +648,9 @@ impl<T> EventLoopWindowTarget<T> {
available_monitors(&self.outputs)
}
pub fn primary_monitor(&self) -> MonitorHandle {
primary_monitor(&self.outputs)
pub fn primary_monitor(&self) -> Option<RootMonitorHandle> {
// Wayland doesn't have a notion of primary monitor.
None
}
}
@ -1121,19 +1122,6 @@ impl MonitorHandle {
}
}
pub fn primary_monitor(outputs: &OutputMgr) -> MonitorHandle {
outputs.with_all(|list| {
if let Some(&(_, ref proxy, _)) = list.first() {
MonitorHandle {
proxy: proxy.clone(),
mgr: outputs.clone(),
}
} else {
panic!("No monitor is available.")
}
})
}
pub fn available_monitors(outputs: &OutputMgr) -> VecDeque<MonitorHandle> {
outputs.with_all(|list| {
list.iter()

View file

@ -10,8 +10,7 @@ use crate::{
error::{ExternalError, NotSupportedError, OsError as RootOsError},
monitor::MonitorHandle as RootMonitorHandle,
platform_impl::{
platform::wayland::event_loop::{available_monitors, primary_monitor},
MonitorHandle as PlatformMonitorHandle,
platform::wayland::event_loop::available_monitors, MonitorHandle as PlatformMonitorHandle,
PlatformSpecificWindowBuilderAttributes as PlAttributes,
},
window::{CursorIcon, Fullscreen, WindowAttributes},
@ -410,8 +409,9 @@ impl Window {
available_monitors(&self.outputs)
}
pub fn primary_monitor(&self) -> MonitorHandle {
primary_monitor(&self.outputs)
pub fn primary_monitor(&self) -> Option<RootMonitorHandle> {
// Wayland doesn't have a notion of primary monitor.
None
}
pub fn raw_window_handle(&self) -> WaylandHandle {