diff --git a/.changes/fix-current-monitor-panic.md b/.changes/fix-current-monitor-panic.md new file mode 100644 index 000000000..c14b08a3f --- /dev/null +++ b/.changes/fix-current-monitor-panic.md @@ -0,0 +1,5 @@ +--- +"tao": patch +--- + +The `current_monitor` function now fallbacks to the primary monitor when the window is invisible. diff --git a/src/platform_impl/linux/window.rs b/src/platform_impl/linux/window.rs index 44d9586f2..942066147 100644 --- a/src/platform_impl/linux/window.rs +++ b/src/platform_impl/linux/window.rs @@ -612,9 +612,19 @@ impl Window { pub fn current_monitor(&self) -> Option { let screen = self.window.display().default_screen(); - let window = self.window.window().unwrap(); - #[allow(deprecated)] // Gtk3 Window only accepts Gdkscreen - let number = screen.monitor_at_window(&window); + // `.window()` returns `None` if the window is invisible; + // we fallback to the primary monitor + let number = self + .window + .window() + .map(|window| { + #[allow(deprecated)] // Gtk3 Window only accepts Gdkscreen + screen.monitor_at_window(&window) + }) + .unwrap_or_else(|| { + #[allow(deprecated)] // Gtk3 Window only accepts Gdkscreen + screen.primary_monitor() + }); let handle = MonitorHandle::new(&self.window.display(), number); Some(RootMonitorHandle { inner: handle }) }