From 5055eddda03711238c61b4ef4ff9ce4b3f7a09eb Mon Sep 17 00:00:00 2001 From: Sophie Herold Date: Sat, 29 Mar 2025 20:51:30 +0100 Subject: [PATCH] image_window: Don't rely on root existing This fixes a single case of root being access from the control hide animation. Apparently it's possible that the widget is unparented from the window and unmaped but not yet unrefed? Fixes #473 --- news.d/48.1/fixes-dont-crash-on-window-close | 1 + src/util/root.rs | 18 ++++++++++++- src/widgets/image_window/controls.rs | 28 ++++++++++---------- 3 files changed, 32 insertions(+), 15 deletions(-) create mode 100644 news.d/48.1/fixes-dont-crash-on-window-close diff --git a/news.d/48.1/fixes-dont-crash-on-window-close b/news.d/48.1/fixes-dont-crash-on-window-close new file mode 100644 index 0000000..c87cb25 --- /dev/null +++ b/news.d/48.1/fixes-dont-crash-on-window-close @@ -0,0 +1 @@ +Crash when closing the window, probably in the exact moment when the animnation for hiding controls starts. \ No newline at end of file diff --git a/src/util/root.rs b/src/util/root.rs index a389830..ae122db 100644 --- a/src/util/root.rs +++ b/src/util/root.rs @@ -24,12 +24,28 @@ pub trait ParentWindow: WidgetExt { let result = self.root().and_downcast(); if result.is_none() { - log::error!("Couldn't find LpWindow for {self:?}"); + log::error!( + "Couldn't find LpWindow for {self:?}. mapped={}", + self.is_mapped() + ); } result.unwrap() } + fn try_window(&self) -> Option { + let result = self.root().and_downcast(); + + if result.is_none() { + log::debug!( + "Couldn't find LpWindow for {self:?}. mapped={}", + self.is_mapped() + ); + } + + result + } + fn edit_window(&self) -> LpEditWindow { let parent = self.parent().unwrap(); diff --git a/src/widgets/image_window/controls.rs b/src/widgets/image_window/controls.rs index e6317bc..0c6fc52 100644 --- a/src/widgets/image_window/controls.rs +++ b/src/widgets/image_window/controls.rs @@ -1,4 +1,4 @@ -// Copyright (c) 2023-2024 Sophie Herold +// Copyright (c) 2023-2025 Sophie Herold // // This program is free software: you can redistribute it and/or modify // it under the terms of the GNU General Public License as published by @@ -26,21 +26,21 @@ impl LpImageWindow { /// Also changes headerbar transparency if it flat and hides/shows cursor on /// fullscreen fn set_control_opacity(&self, opacity: f64, hiding: bool) { - let window = self.window(); + if let Some(window) = self.try_window() { + self.image_view().controls_box_start().set_opacity(opacity); + self.image_view().controls_box_end().set_opacity(opacity); - self.image_view().controls_box_start().set_opacity(opacity); - self.image_view().controls_box_end().set_opacity(opacity); + if self.is_headerbar_flat() && window.is_fullscreen() { + self.headerbar().set_opacity(opacity); + } else { + self.headerbar().set_opacity(1.); + } - if self.is_headerbar_flat() && window.is_fullscreen() { - self.headerbar().set_opacity(opacity); - } else { - self.headerbar().set_opacity(1.); - } - - if window.is_fullscreen() && hiding && opacity < 0.9 { - self.set_cursor(gdk::Cursor::from_name("none", None).as_ref()); - } else { - self.set_cursor(None); + if window.is_fullscreen() && hiding && opacity < 0.9 { + self.set_cursor(gdk::Cursor::from_name("none", None).as_ref()); + } else { + self.set_cursor(None); + } } }