application: Stop using expressions
This commit moves the logic from the expressions to the window via GSignalGroup.
This commit is contained in:
parent
3d445f25fe
commit
4d4bc4bb38
2 changed files with 32 additions and 58 deletions
|
|
@ -26,7 +26,6 @@ use glib::clone;
|
||||||
use gtk_macros::spawn;
|
use gtk_macros::spawn;
|
||||||
|
|
||||||
use crate::config;
|
use crate::config;
|
||||||
use crate::widgets::{LpImage, LpImagePage};
|
|
||||||
use crate::window::LpWindow;
|
use crate::window::LpWindow;
|
||||||
|
|
||||||
mod imp {
|
mod imp {
|
||||||
|
|
@ -79,35 +78,6 @@ mod imp {
|
||||||
|
|
||||||
let win = LpWindow::new(&*application);
|
let win = LpWindow::new(&*application);
|
||||||
win.set_image_from_path(&path);
|
win.set_image_from_path(&path);
|
||||||
|
|
||||||
// show window when image size is known
|
|
||||||
let watch = win
|
|
||||||
.imp()
|
|
||||||
.image_view
|
|
||||||
.property_expression("current-page")
|
|
||||||
.chain_property::<LpImagePage>("image")
|
|
||||||
.chain_property::<LpImage>("image-size")
|
|
||||||
.watch(
|
|
||||||
glib::Object::NONE,
|
|
||||||
glib::clone!(@weak win => move || {
|
|
||||||
win.image_size_ready();
|
|
||||||
}),
|
|
||||||
);
|
|
||||||
win.imp().watch_image_size.replace(Some(watch));
|
|
||||||
|
|
||||||
// show if loading image errors
|
|
||||||
let watch = win
|
|
||||||
.imp()
|
|
||||||
.image_view
|
|
||||||
.property_expression("current-page")
|
|
||||||
.chain_property::<LpImagePage>("error")
|
|
||||||
.watch(
|
|
||||||
glib::Object::NONE,
|
|
||||||
glib::clone!(@weak win => move || {
|
|
||||||
win.image_error();
|
|
||||||
}),
|
|
||||||
);
|
|
||||||
win.imp().watch_image_error.replace(Some(watch));
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -26,7 +26,6 @@ use glib::clone;
|
||||||
use gtk::CompositeTemplate;
|
use gtk::CompositeTemplate;
|
||||||
use gtk_macros::spawn;
|
use gtk_macros::spawn;
|
||||||
|
|
||||||
use std::cell::RefCell;
|
|
||||||
use std::path::{Path, PathBuf};
|
use std::path::{Path, PathBuf};
|
||||||
|
|
||||||
use crate::config;
|
use crate::config;
|
||||||
|
|
@ -34,6 +33,9 @@ use crate::util::{self, Direction, Position};
|
||||||
use crate::widgets::{LpImage, LpImageView, LpPropertiesView};
|
use crate::widgets::{LpImage, LpImageView, LpPropertiesView};
|
||||||
|
|
||||||
mod imp {
|
mod imp {
|
||||||
|
use crate::widgets::LpImagePage;
|
||||||
|
use once_cell::sync::OnceCell;
|
||||||
|
|
||||||
use super::*;
|
use super::*;
|
||||||
|
|
||||||
// To use composite templates, you need
|
// To use composite templates, you need
|
||||||
|
|
@ -77,8 +79,7 @@ mod imp {
|
||||||
#[template_child]
|
#[template_child]
|
||||||
pub drop_target: TemplateChild<gtk::DropTarget>,
|
pub drop_target: TemplateChild<gtk::DropTarget>,
|
||||||
|
|
||||||
pub watch_image_size: RefCell<Option<gtk::ExpressionWatch>>,
|
current_page_signals: OnceCell<glib::SignalGroup>,
|
||||||
pub watch_image_error: RefCell<Option<gtk::ExpressionWatch>>,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[glib::object_subclass]
|
#[glib::object_subclass]
|
||||||
|
|
@ -228,7 +229,7 @@ mod imp {
|
||||||
|
|
||||||
obj.set_actions_enabled(false);
|
obj.set_actions_enabled(false);
|
||||||
|
|
||||||
let signal_group = self.image_view.current_image_signals();
|
let current_image_signals = self.image_view.current_image_signals();
|
||||||
// clone! is a macro from glib-rs that allows
|
// clone! is a macro from glib-rs that allows
|
||||||
// you to easily handle references in callbacks
|
// you to easily handle references in callbacks
|
||||||
// without refcycles or leaks.
|
// without refcycles or leaks.
|
||||||
|
|
@ -237,12 +238,12 @@ mod imp {
|
||||||
// Object alive, pass as @weak. Otherwise, pass
|
// Object alive, pass as @weak. Otherwise, pass
|
||||||
// as @strong. Most of the time you will want
|
// as @strong. Most of the time you will want
|
||||||
// to use @weak.
|
// to use @weak.
|
||||||
signal_group.connect_bind_local(glib::clone!(@weak obj => move |_, _| {
|
current_image_signals.connect_bind_local(glib::clone!(@weak obj => move |_, _| {
|
||||||
obj.on_zoom_status_changed()
|
obj.on_zoom_status_changed()
|
||||||
}));
|
}));
|
||||||
|
|
||||||
let win = &*obj;
|
let win = &*obj;
|
||||||
signal_group.connect_closure(
|
current_image_signals.connect_closure(
|
||||||
"notify::best-fit",
|
"notify::best-fit",
|
||||||
false,
|
false,
|
||||||
// `closure_local!` is similar to `clone`, but you use `@watch` instead of clone.
|
// `closure_local!` is similar to `clone`, but you use `@watch` instead of clone.
|
||||||
|
|
@ -253,7 +254,7 @@ mod imp {
|
||||||
}),
|
}),
|
||||||
);
|
);
|
||||||
|
|
||||||
signal_group.connect_closure(
|
current_image_signals.connect_closure(
|
||||||
"notify::is-max-zoom",
|
"notify::is-max-zoom",
|
||||||
false,
|
false,
|
||||||
glib::closure_local!(@watch win => move |_: &LpImage, _: &glib::ParamSpec| {
|
glib::closure_local!(@watch win => move |_: &LpImage, _: &glib::ParamSpec| {
|
||||||
|
|
@ -261,13 +262,33 @@ mod imp {
|
||||||
}),
|
}),
|
||||||
);
|
);
|
||||||
|
|
||||||
self.image_view.connect_notify_local(
|
current_image_signals.connect_closure(
|
||||||
Some("current-page"),
|
"notify::image-size",
|
||||||
glib::clone!(@weak obj => move |_, _| {
|
false,
|
||||||
obj.images_available();
|
glib::closure_local!(@watch win => move |_: &LpImage, _: &glib::ParamSpec| {
|
||||||
|
win.image_size_ready();
|
||||||
}),
|
}),
|
||||||
);
|
);
|
||||||
|
|
||||||
|
let current_page_signals = glib::SignalGroup::new(LpImagePage::static_type());
|
||||||
|
self.image_view.connect_notify_local(
|
||||||
|
Some("current-page"),
|
||||||
|
glib::clone!(@weak obj, @weak current_page_signals => move |view, _| {
|
||||||
|
current_page_signals.set_target(view.current_page().as_ref());
|
||||||
|
obj.images_available();
|
||||||
|
}),
|
||||||
|
);
|
||||||
|
|
||||||
|
current_page_signals.connect_closure(
|
||||||
|
"notify::error",
|
||||||
|
false,
|
||||||
|
glib::closure_local!(@watch win => move |_: &LpImagePage, _: &glib::ParamSpec| {
|
||||||
|
win.image_error();
|
||||||
|
}),
|
||||||
|
);
|
||||||
|
|
||||||
|
self.current_page_signals.set(current_page_signals).unwrap();
|
||||||
|
|
||||||
// action win.previous status
|
// action win.previous status
|
||||||
self.image_view.connect_notify_local(
|
self.image_view.connect_notify_local(
|
||||||
Some("is-previous-available"),
|
Some("is-previous-available"),
|
||||||
|
|
@ -349,10 +370,6 @@ mod imp {
|
||||||
}),
|
}),
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
fn dispose(&self) {
|
|
||||||
self.obj().disconnect_present_watches();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
impl WidgetImpl for LpWindow {}
|
impl WidgetImpl for LpWindow {}
|
||||||
|
|
@ -603,7 +620,6 @@ impl LpWindow {
|
||||||
pub fn image_size_ready(&self) {
|
pub fn image_size_ready(&self) {
|
||||||
// if visible for whatever reason, don't do any resize
|
// if visible for whatever reason, don't do any resize
|
||||||
if self.is_visible() {
|
if self.is_visible() {
|
||||||
self.disconnect_present_watches();
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -618,7 +634,6 @@ impl LpWindow {
|
||||||
log::debug!("Showing window because image size is ready");
|
log::debug!("Showing window because image size is ready");
|
||||||
// this let's the window determine the default size from LpImage's natural size
|
// this let's the window determine the default size from LpImage's natural size
|
||||||
self.set_default_size(-1, -1);
|
self.set_default_size(-1, -1);
|
||||||
self.disconnect_present_watches();
|
|
||||||
self.present();
|
self.present();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -626,7 +641,6 @@ impl LpWindow {
|
||||||
|
|
||||||
pub fn image_error(&self) {
|
pub fn image_error(&self) {
|
||||||
if self.is_visible() {
|
if self.is_visible() {
|
||||||
self.disconnect_present_watches();
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -635,21 +649,11 @@ impl LpWindow {
|
||||||
if let Some(page) = current_page {
|
if let Some(page) = current_page {
|
||||||
if page.error().is_some() {
|
if page.error().is_some() {
|
||||||
log::debug!("Showing window because loading image failed");
|
log::debug!("Showing window because loading image failed");
|
||||||
self.disconnect_present_watches();
|
|
||||||
self.present();
|
self.present();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn disconnect_present_watches(&self) {
|
|
||||||
if let Some(watch) = self.imp().watch_image_size.take() {
|
|
||||||
watch.unwatch();
|
|
||||||
}
|
|
||||||
if let Some(watch) = self.imp().watch_image_error.take() {
|
|
||||||
watch.unwatch();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fn on_zoom_status_changed(&self) {
|
fn on_zoom_status_changed(&self) {
|
||||||
let can_zoom_out = self
|
let can_zoom_out = self
|
||||||
.image_view()
|
.image_view()
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue