Make 'current_monitor' return 'Option<MonitorHandle>'

On certain platforms window couldn't be on any monitor
resulting in failures of 'current_monitor' function.

Such issue was happening on Wayland, since the window
isn't on any monitor, unless the user has drawn something into it.

Returning 'Option<MonitorHandle>' will give an ability to
handle such situations gracefully by properly indicating that
there's no current monitor.

Fixes #793.
This commit is contained in:
Kirill Chibisov 2020-09-07 20:09:24 +03:00 committed by GitHub
parent e2cf2a5754
commit cac627ed05
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
12 changed files with 65 additions and 29 deletions

View file

@ -391,10 +391,10 @@ impl Window {
v
}
pub fn current_monitor(&self) -> monitor::MonitorHandle {
monitor::MonitorHandle {
pub fn current_monitor(&self) -> Option<monitor::MonitorHandle> {
Some(monitor::MonitorHandle {
inner: MonitorHandle,
}
})
}
pub fn scale_factor(&self) -> f64 {

View file

@ -224,7 +224,7 @@ impl Inner {
pub fn fullscreen(&self) -> Option<Fullscreen> {
unsafe {
let monitor = self.current_monitor();
let monitor = self.current_monitor_inner();
let uiscreen = monitor.inner.ui_screen();
let screen_space_bounds = self.screen_frame();
let screen_bounds: CGRect = msg_send![uiscreen, bounds];
@ -258,7 +258,8 @@ impl Inner {
warn!("`Window::set_ime_position` is ignored on iOS")
}
pub fn current_monitor(&self) -> RootMonitorHandle {
// Allow directly accessing the current monitor internally without unwrapping.
fn current_monitor_inner(&self) -> RootMonitorHandle {
unsafe {
let uiscreen: id = msg_send![self.window, screen];
RootMonitorHandle {
@ -267,6 +268,10 @@ impl Inner {
}
}
pub fn current_monitor(&self) -> Option<RootMonitorHandle> {
Some(self.current_monitor_inner())
}
pub fn available_monitors(&self) -> VecDeque<MonitorHandle> {
unsafe { monitor::uiscreens() }
}

View file

@ -425,9 +425,22 @@ impl Window {
}
#[inline]
pub fn current_monitor(&self) -> RootMonitorHandle {
RootMonitorHandle {
inner: x11_or_wayland!(match self; Window(window) => window.current_monitor(); as MonitorHandle),
pub fn current_monitor(&self) -> Option<RootMonitorHandle> {
match self {
#[cfg(feature = "x11")]
&Window::X(ref window) => {
let current_monitor = MonitorHandle::X(window.current_monitor());
Some(RootMonitorHandle {
inner: current_monitor,
})
}
#[cfg(feature = "wayland")]
&Window::Wayland(ref window) => {
let current_monitor = MonitorHandle::Wayland(window.current_monitor()?);
Some(RootMonitorHandle {
inner: current_monitor,
})
}
}
}

View file

@ -334,8 +334,10 @@ impl Window {
pub fn fullscreen(&self) -> Option<Fullscreen> {
if *(self.fullscreen.lock().unwrap()) {
// If the monitor cannot be determined, we cannot report any fullscreen mode.
let current_monitor = self.current_monitor()?;
Some(Fullscreen::Borderless(RootMonitorHandle {
inner: PlatformMonitorHandle::Wayland(self.current_monitor()),
inner: PlatformMonitorHandle::Wayland(current_monitor),
}))
} else {
None
@ -396,12 +398,12 @@ impl Window {
&self.surface
}
pub fn current_monitor(&self) -> MonitorHandle {
let output = get_outputs(&self.surface).last().unwrap().clone();
MonitorHandle {
pub fn current_monitor(&self) -> Option<MonitorHandle> {
let output = get_outputs(&self.surface).last()?.clone();
Some(MonitorHandle {
proxy: output,
mgr: self.outputs.clone(),
}
})
}
pub fn available_monitors(&self) -> VecDeque<MonitorHandle> {

View file

@ -972,7 +972,8 @@ impl UnownedWindow {
}
#[inline]
pub fn current_monitor(&self) -> RootMonitorHandle {
// Allow directly accessing the current monitor internally without unwrapping.
pub(crate) fn current_monitor_inner(&self) -> RootMonitorHandle {
unsafe {
let screen: id = msg_send![*self.ns_window, screen];
let desc = NSScreen::deviceDescription(screen);
@ -985,6 +986,11 @@ impl UnownedWindow {
}
}
#[inline]
pub fn current_monitor(&self) -> Option<RootMonitorHandle> {
Some(self.current_monitor_inner())
}
#[inline]
pub fn available_monitors(&self) -> VecDeque<MonitorHandle> {
monitor::available_monitors()

View file

@ -450,7 +450,8 @@ extern "C" fn window_will_enter_fullscreen(this: &Object, _: Sel, _: id) {
// Otherwise, we must've reached fullscreen by the user clicking
// on the green fullscreen button. Update state!
None => {
shared_state.fullscreen = Some(Fullscreen::Borderless(window.current_monitor()))
let current_monitor = window.current_monitor_inner();
shared_state.fullscreen = Some(Fullscreen::Borderless(current_monitor))
}
}
shared_state.in_fullscreen_transition = true;

View file

@ -201,7 +201,7 @@ impl Window {
#[inline]
pub fn fullscreen(&self) -> Option<Fullscreen> {
if self.canvas.is_fullscreen() {
Some(Fullscreen::Borderless(self.current_monitor()))
Some(Fullscreen::Borderless(self.current_monitor_inner()))
} else {
None
}
@ -237,12 +237,18 @@ impl Window {
}
#[inline]
pub fn current_monitor(&self) -> RootMH {
// Allow directly accessing the current monitor internally without unwrapping.
fn current_monitor_inner(&self) -> RootMH {
RootMH {
inner: monitor::Handle,
}
}
#[inline]
pub fn current_monitor(&self) -> Option<RootMH> {
Some(self.current_monitor_inner())
}
#[inline]
pub fn available_monitors(&self) -> VecDequeIter<monitor::Handle> {
VecDeque::new().into_iter()

View file

@ -577,10 +577,10 @@ impl Window {
}
#[inline]
pub fn current_monitor(&self) -> RootMonitorHandle {
RootMonitorHandle {
pub fn current_monitor(&self) -> Option<RootMonitorHandle> {
Some(RootMonitorHandle {
inner: monitor::current_monitor(self.window.0),
}
})
}
#[inline]