window: Don't store window to avoid ref cycles

Use root() instead and move one constructor to on root for having the
root available.
This commit is contained in:
Sophie Herold 2024-12-18 17:12:17 +01:00
parent 8345640757
commit b9f22ebfc3
12 changed files with 77 additions and 51 deletions

View file

@ -82,7 +82,7 @@ mod imp {
} }
fn activate(&self) { fn activate(&self) {
log::trace!("Activate"); log::debug!("Showing window via 'activate'");
let application = self.obj(); let application = self.obj();
let window = LpWindow::new(&*application); let window = LpWindow::new(&*application);
window.present(); window.present();
@ -138,6 +138,7 @@ impl LpApplication {
gio::ActionEntryBuilder::new("new-window") gio::ActionEntryBuilder::new("new-window")
.activate(|app: &Self, _, _| { .activate(|app: &Self, _, _| {
let win = LpWindow::new(app); let win = LpWindow::new(app);
log::debug!("Showing new window");
win.present(); win.present();
}) })
.build(), .build(),

View file

@ -17,6 +17,7 @@
// SPDX-License-Identifier: GPL-3.0-or-later // SPDX-License-Identifier: GPL-3.0-or-later
pub mod gettext; pub mod gettext;
pub mod root;
use std::ffi::CStr; use std::ffi::CStr;
use std::fmt::{Debug, Write}; use std::fmt::{Debug, Write};
@ -261,3 +262,8 @@ pub fn locale_settings() -> LocaleSettings {
locale_settings locale_settings
} }
} }
pub enum ErrorType {
Loader,
General,
}

34
src/util/root.rs Normal file
View file

@ -0,0 +1,34 @@
// Copyright (c) 2024 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
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
//
// SPDX-License-Identifier: GPL-3.0-or-later
use gtk::prelude::*;
use crate::widgets::LpWindow;
pub trait ParentWindow: WidgetExt {
fn window(&self) -> LpWindow {
let result = self.root().and_downcast();
if result.is_none() {
log::error!("Couldn't find LpWindow for {self:?}");
}
result.unwrap()
}
}
impl<T: WidgetExt> ParentWindow for T {}

View file

@ -23,9 +23,9 @@ use adw::{glib, gtk};
use glycin::Operation; use glycin::Operation;
use crate::editing::preview::EditingError; use crate::editing::preview::EditingError;
use crate::util::gettext::*; use crate::util::root::ParentWindow;
use crate::util::{gettext::*, ErrorType};
use crate::widgets::edit::LpEditCropSelection; use crate::widgets::edit::LpEditCropSelection;
use crate::widgets::error_details::ErrorType;
use crate::widgets::{LpEditWindow, LpImage}; use crate::widgets::{LpEditWindow, LpImage};
#[derive(Debug, Clone, Copy, Default, glib::Enum)] #[derive(Debug, Clone, Copy, Default, glib::Enum)]

View file

@ -23,14 +23,14 @@ use adw::subclass::prelude::*;
use glycin::Operations; use glycin::Operations;
use super::edit::LpEditCrop; use super::edit::LpEditCrop;
use super::{LpImage, LpWindow}; use super::LpImage;
use crate::deps::*; use crate::deps::*;
use crate::util::gettext::*; use crate::util::gettext::*;
use crate::util::root::ParentWindow;
use crate::util::ErrorType;
mod imp { mod imp {
use super::*; use super::*;
use crate::widgets::error_details::ErrorType;
#[derive(Debug, Default, gtk::CompositeTemplate, glib::Properties)] #[derive(Debug, Default, gtk::CompositeTemplate, glib::Properties)]
#[properties(wrapper_type = super::LpEditWindow)] #[properties(wrapper_type = super::LpEditWindow)]
@ -43,8 +43,6 @@ mod imp {
#[template_child] #[template_child]
save: TemplateChild<gtk::Button>, save: TemplateChild<gtk::Button>,
#[property(get, construct_only)]
window: OnceCell<LpWindow>,
#[property(get, construct_only)] #[property(get, construct_only)]
original_image: OnceCell<LpImage>, original_image: OnceCell<LpImage>,
@ -170,9 +168,8 @@ glib::wrapper! {
} }
impl LpEditWindow { impl LpEditWindow {
pub fn new(window: LpWindow, image: LpImage) -> Self { pub fn new(image: LpImage) -> Self {
glib::Object::builder() glib::Object::builder()
.property("window", window)
.property("original_image", image) .property("original_image", image)
.build() .build()
} }

View file

@ -21,6 +21,7 @@ use gtk::CompositeTemplate;
use crate::deps::*; use crate::deps::*;
use crate::util::gettext::*; use crate::util::gettext::*;
use crate::util::ErrorType;
mod imp { mod imp {
use super::*; use super::*;
@ -108,12 +109,8 @@ impl LpErrorDetails {
} }
)); ));
log::debug!("Showing error details");
obj.present(Some(root)); obj.present(Some(root));
obj obj
} }
} }
pub enum ErrorType {
Loader,
General,
}

View file

@ -29,6 +29,7 @@ use super::LpErrorDetails;
use crate::decoder::DecoderError; use crate::decoder::DecoderError;
use crate::deps::*; use crate::deps::*;
use crate::util::gettext::*; use crate::util::gettext::*;
use crate::util::ErrorType;
use crate::widgets::LpImage; use crate::widgets::LpImage;
mod imp { mod imp {
@ -255,11 +256,7 @@ impl LpImagePage {
#[weak(rename_to = obj)] #[weak(rename_to = obj)]
self, self,
move |_| { move |_| {
LpErrorDetails::new( LpErrorDetails::new(&obj.root().unwrap(), &err, ErrorType::Loader);
&obj.root().unwrap(),
&err,
super::error_details::ErrorType::Loader,
);
} }
)); ));
} }
@ -271,11 +268,7 @@ impl LpImagePage {
#[weak(rename_to = obj)] #[weak(rename_to = obj)]
self, self,
move |_| { move |_| {
LpErrorDetails::new( LpErrorDetails::new(&obj.root().unwrap(), &err, ErrorType::Loader);
&obj.root().unwrap(),
&err,
super::error_details::ErrorType::Loader,
);
} }
)); ));

View file

@ -42,11 +42,10 @@ use crate::application::LpApplication;
use crate::config; use crate::config;
use crate::deps::*; use crate::deps::*;
use crate::util::gettext::*; use crate::util::gettext::*;
use crate::util::root::ParentWindow;
use crate::util::Direction; use crate::util::Direction;
use crate::widgets::window::ActionPartGlobal; use crate::widgets::window::ActionPartGlobal;
use crate::widgets::{ use crate::widgets::{LpDragOverlay, LpImage, LpImageView, LpPropertiesView, LpWindowContent};
LpDragOverlay, LpImage, LpImageView, LpPropertiesView, LpWindow, LpWindowContent,
};
/// Animation duration for showing overlay buttons in milliseconds /// Animation duration for showing overlay buttons in milliseconds
const SHOW_CONTROLS_ANIMATION_DURATION: u32 = 200; const SHOW_CONTROLS_ANIMATION_DURATION: u32 = 200;
@ -62,9 +61,6 @@ mod imp {
#[properties(wrapper_type = super::LpImageWindow)] #[properties(wrapper_type = super::LpImageWindow)]
#[template(file = "image_window.ui")] #[template(file = "image_window.ui")]
pub struct LpImageWindow { pub struct LpImageWindow {
#[property(get, construct_only)]
window: OnceCell<LpWindow>,
#[template_child] #[template_child]
pub(super) window_content: TemplateChild<LpWindowContent>, pub(super) window_content: TemplateChild<LpWindowContent>,
@ -152,9 +148,11 @@ mod imp {
} }
#[glib::derived_properties] #[glib::derived_properties]
impl ObjectImpl for LpImageWindow { impl ObjectImpl for LpImageWindow {}
fn constructed(&self) {
self.parent_constructed(); impl WidgetImpl for LpImageWindow {
fn root(&self) {
self.parent_root();
let obj = self.obj(); let obj = self.obj();
@ -413,6 +411,7 @@ mod imp {
} }
// Maybe one day this will actually work // Maybe one day this will actually work
log::debug!("Trying to focus window after drag and drop");
obj.window().present(); obj.window().present();
true true
@ -421,7 +420,6 @@ mod imp {
} }
} }
impl WidgetImpl for LpImageWindow {}
impl BinImpl for LpImageWindow {} impl BinImpl for LpImageWindow {}
impl LpImageWindow { impl LpImageWindow {
@ -567,7 +565,7 @@ impl LpImageWindow {
/// Returns true if some text in metadata is currently selected /// Returns true if some text in metadata is currently selected
fn has_metadata_selected(&self) -> bool { fn has_metadata_selected(&self) -> bool {
if let Some(focus_widget) = self.window().focus() { if let Some(focus_widget) = GtkWindowExt::focus(&self.window()) {
if focus_widget.is_ancestor(&*self.imp().properties_view) { if focus_widget.is_ancestor(&*self.imp().properties_view) {
if let Ok(label) = focus_widget.downcast::<gtk::Label>() { if let Ok(label) = focus_widget.downcast::<gtk::Label>() {
return label.selection_bounds().is_some(); return label.selection_bounds().is_some();
@ -630,8 +628,7 @@ impl LpImageWindow {
}); });
} }
)); ));
todo!(); self.window().add_toast(toast);
//self.imp().toast_overlay.add_toast(toast);
} }
Err(err) => { Err(err) => {
if Some(gio::IOErrorEnum::NotSupported) == err.kind::<gio::IOErrorEnum>() { if Some(gio::IOErrorEnum::NotSupported) == err.kind::<gio::IOErrorEnum>() {

View file

@ -22,6 +22,7 @@ use gtk::prelude::*;
use strum::IntoEnumIterator; use strum::IntoEnumIterator;
use crate::deps::*; use crate::deps::*;
use crate::util::root::ParentWindow;
use crate::util::{Direction, Position}; use crate::util::{Direction, Position};
#[derive(strum::Display, strum::AsRefStr, strum::EnumIter)] #[derive(strum::Display, strum::AsRefStr, strum::EnumIter)]

View file

@ -406,6 +406,7 @@ mod imp {
obj.set_orientation(orientation); obj.set_orientation(orientation);
drop(ui_updates_disabled); drop(ui_updates_disabled);
log::debug!("Showing print dialog");
obj.present(); obj.present();
} }

View file

@ -23,12 +23,11 @@ use gio::glib::VariantTy;
use gtk::CompositeTemplate; use gtk::CompositeTemplate;
use strum::IntoEnumIterator; use strum::IntoEnumIterator;
use super::error_details::ErrorType;
use super::LpErrorDetails; use super::LpErrorDetails;
use crate::config; use crate::config;
use crate::deps::*; use crate::deps::*;
use crate::util::gettext::*; use crate::util::gettext::*;
use crate::util::Direction; use crate::util::{Direction, ErrorType};
use crate::widgets::{LpEditWindow, LpImageView, LpImageWindow}; use crate::widgets::{LpEditWindow, LpImageView, LpImageWindow};
/// Show window after X milliseconds even if image dimensions are not known yet /// Show window after X milliseconds even if image dimensions are not known yet
@ -129,6 +128,7 @@ mod imp {
#[weak] #[weak]
obj, obj,
move || if !obj.is_visible() { move || if !obj.is_visible() {
log::debug!("Showing window after timeout");
obj.present() obj.present()
} }
), ),
@ -149,7 +149,7 @@ mod imp {
glib::wrapper! { glib::wrapper! {
pub struct LpWindow(ObjectSubclass<imp::LpWindow>) pub struct LpWindow(ObjectSubclass<imp::LpWindow>)
@extends gtk::Widget, gtk::Window, gtk::ApplicationWindow, adw::ApplicationWindow, @extends gtk::Widget, gtk::Window, gtk::ApplicationWindow, adw::ApplicationWindow,
@implements gio::ActionMap, gio::ActionGroup, gtk::Native; @implements gio::ActionMap, gio::ActionGroup, gtk::Native, gtk::Root;
} }
impl LpWindow { impl LpWindow {
@ -195,18 +195,18 @@ impl LpWindow {
} }
} }
pub fn show_toast(&self, text: &str, priority: adw::ToastPriority) { pub fn add_toast(&self, toast: adw::Toast) {
let imp = self.imp(); self.imp().toast_overlay.add_toast(toast);
}
pub fn show_toast(&self, text: &str, priority: adw::ToastPriority) {
let toast = adw::Toast::new(text); let toast = adw::Toast::new(text);
toast.set_priority(priority); toast.set_priority(priority);
imp.toast_overlay.add_toast(toast); self.add_toast(toast);
} }
pub fn show_error(&self, stub: &str, details: &str, type_: ErrorType) { pub fn show_error(&self, stub: &str, details: &str, type_: ErrorType) {
let imp = self.imp();
let action = match type_ { let action = match type_ {
ErrorType::General => WindowAction::ShowError, ErrorType::General => WindowAction::ShowError,
ErrorType::Loader => WindowAction::ShowLoaderError, ErrorType::Loader => WindowAction::ShowLoaderError,
@ -220,7 +220,7 @@ impl LpWindow {
.action_target(&details.to_variant()) .action_target(&details.to_variant())
.build(); .build();
imp.toast_overlay.add_toast(toast); self.add_toast(toast);
} }
pub fn show_error_details(&self, details: &str) { pub fn show_error_details(&self, details: &str) {
@ -233,6 +233,7 @@ impl LpWindow {
pub async fn show_about(&self) { pub async fn show_about(&self) {
let about = crate::about::dialog().await; let about = crate::about::dialog().await;
log::debug!("Showing about dialog");
about.present(Some(self)); about.present(Some(self));
} }
@ -246,7 +247,7 @@ impl LpWindow {
if let Some(image) = self.image_view().current_image() { if let Some(image) = self.image_view().current_image() {
let edit_child = &*self.imp().edit_window_child; let edit_child = &*self.imp().edit_window_child;
edit_child.set_child(Some(&LpEditWindow::new(self.clone(), image))); edit_child.set_child(Some(&LpEditWindow::new(image)));
self.imp().stack.set_visible_child(edit_child); self.imp().stack.set_visible_child(edit_child);
} else { } else {
log::error!("Can't open image editor since no current image exists"); log::error!("Can't open image editor since no current image exists");

View file

@ -24,9 +24,7 @@
<property name="child"> <property name="child">
<object class="AdwViewStack" id="stack"> <object class="AdwViewStack" id="stack">
<child> <child>
<object class="LpImageWindow" id="image_window"> <object class="LpImageWindow" id="image_window" />
<property name="window">LpWindow</property>
</object>
</child> </child>
<child> <child>
<object class="AdwBin" id="edit_window_child"> <object class="AdwBin" id="edit_window_child">