2023-04-19 20:02:01 +02:00
|
|
|
// Copyright (c) 2020-2023 Christopher Davis
|
|
|
|
|
// Copyright (c) 2022-2023 Sophie Herold
|
|
|
|
|
// Copyright (c) 2022 Elton A Rodrigues
|
|
|
|
|
// Copyright (c) 2022 Maximiliano Sandoval R
|
2023-06-07 23:44:20 +02:00
|
|
|
// Copyright (c) 2023 FineFindus
|
2023-07-16 22:47:10 +02:00
|
|
|
// Copyright (c) 2023 Huan Nguyen
|
2023-04-19 20:02:01 +02:00
|
|
|
// Copyright (c) 2023 Philipp Kiemle
|
2020-12-26 02:05:34 -08:00
|
|
|
//
|
|
|
|
|
// 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
|
|
|
|
|
|
2023-03-06 22:59:40 +01:00
|
|
|
//! A widget that shows the [`LpSlidingView`] and coordinates it's content
|
|
|
|
|
//!
|
|
|
|
|
//! This widget coordinates the content of [`LpSlidingView`] with the
|
|
|
|
|
//! [`LpFileModel`]. It also handles input that changes the image.
|
|
|
|
|
//!
|
|
|
|
|
//! [`LpSlidingView`]: crate::widgets::LpSlidingView
|
|
|
|
|
//! [`LpFileModel`]: crate::file_model::LpFileModel
|
|
|
|
|
|
2021-12-21 16:09:48 -08:00
|
|
|
use crate::deps::*;
|
2023-01-23 01:10:15 +01:00
|
|
|
use crate::file_model::LpFileModel;
|
2023-04-11 00:02:28 +02:00
|
|
|
use crate::util::gettext::*;
|
2023-02-06 19:14:01 +01:00
|
|
|
use crate::util::{Direction, Position};
|
2023-04-02 01:39:23 +02:00
|
|
|
use crate::widgets::{LpImage, LpImagePage, LpPrint, LpSlidingView};
|
2021-12-21 16:09:48 -08:00
|
|
|
|
2022-11-16 20:14:39 +01:00
|
|
|
use adw::prelude::*;
|
2021-12-21 12:15:46 -08:00
|
|
|
use adw::subclass::prelude::*;
|
2023-01-23 01:10:15 +01:00
|
|
|
use anyhow::Context;
|
2023-05-24 15:34:52 +00:00
|
|
|
use ashpd::desktop::{wallpaper, ResponseError};
|
2022-08-17 21:31:36 -03:00
|
|
|
use ashpd::Error;
|
2021-08-17 20:37:04 -07:00
|
|
|
use ashpd::WindowIdentifier;
|
2023-01-23 01:10:15 +01:00
|
|
|
use glib::clone;
|
2023-05-25 10:56:38 +00:00
|
|
|
use glib::translate::IntoGlib;
|
2023-11-29 19:10:57 +01:00
|
|
|
use glib::Properties;
|
2023-01-23 01:10:15 +01:00
|
|
|
use gtk::CompositeTemplate;
|
2022-08-03 19:32:02 -04:00
|
|
|
|
2023-01-23 01:10:15 +01:00
|
|
|
use std::cell::{Cell, RefCell};
|
2023-11-29 19:10:57 +01:00
|
|
|
use std::{cell::OnceCell, marker::PhantomData};
|
2022-01-19 23:11:38 -08:00
|
|
|
|
2022-06-29 18:25:45 -04:00
|
|
|
// The number of pages we want to buffer
|
|
|
|
|
// on either side of the current page.
|
2023-01-23 01:10:15 +01:00
|
|
|
const BUFFER: usize = 2;
|
2021-09-05 05:51:34 -07:00
|
|
|
|
2020-12-26 02:05:34 -08:00
|
|
|
mod imp {
|
2023-03-02 22:43:29 -05:00
|
|
|
|
2020-12-26 02:05:34 -08:00
|
|
|
use super::*;
|
|
|
|
|
|
2023-11-29 19:10:57 +01:00
|
|
|
#[derive(Debug, Default, CompositeTemplate, Properties)]
|
|
|
|
|
#[properties(wrapper_type = super::LpImageView)]
|
2023-11-28 21:50:51 +01:00
|
|
|
#[template(file = "image_view.ui")]
|
2021-12-21 10:55:02 -08:00
|
|
|
pub struct LpImageView {
|
2022-11-16 20:14:39 +01:00
|
|
|
/// Direct child of this Adw::Bin
|
|
|
|
|
#[template_child]
|
2023-03-14 18:56:35 +01:00
|
|
|
pub(super) bin_child: TemplateChild<gtk::Widget>,
|
2022-12-08 02:00:50 +01:00
|
|
|
/// overlayed controls
|
|
|
|
|
#[template_child]
|
2023-03-14 18:56:35 +01:00
|
|
|
pub(super) controls_box_start: TemplateChild<gtk::Widget>,
|
2023-04-20 02:32:41 +02:00
|
|
|
#[template_child]
|
|
|
|
|
pub(super) controls_box_start_events: TemplateChild<gtk::EventControllerMotion>,
|
2022-12-08 02:00:50 +01:00
|
|
|
/// overlayed controls
|
|
|
|
|
#[template_child]
|
2023-03-14 18:56:35 +01:00
|
|
|
pub(super) controls_box_end: TemplateChild<gtk::Widget>,
|
2023-04-20 02:32:41 +02:00
|
|
|
#[template_child]
|
|
|
|
|
pub(super) controls_box_end_events: TemplateChild<gtk::EventControllerMotion>,
|
|
|
|
|
|
2020-12-26 02:05:34 -08:00
|
|
|
#[template_child]
|
2023-01-23 01:10:15 +01:00
|
|
|
pub sliding_view: TemplateChild<LpSlidingView>,
|
2022-11-16 20:14:39 +01:00
|
|
|
|
2023-07-15 20:21:00 +02:00
|
|
|
pub drag_source: gtk::DragSource,
|
|
|
|
|
|
2023-03-14 18:56:35 +01:00
|
|
|
pub(super) model: RefCell<LpFileModel>,
|
|
|
|
|
pub(super) preserve_content: Cell<bool>,
|
2023-03-02 22:43:29 -05:00
|
|
|
|
2023-03-14 18:56:35 +01:00
|
|
|
pub(super) current_image_signals: OnceCell<glib::SignalGroup>,
|
2023-11-29 19:10:57 +01:00
|
|
|
|
|
|
|
|
#[property(get = Self::current_page)]
|
|
|
|
|
_current_page: PhantomData<Option<LpImagePage>>,
|
|
|
|
|
|
|
|
|
|
#[property(get = Self::is_previous_available)]
|
|
|
|
|
_is_previous_available: PhantomData<bool>,
|
|
|
|
|
|
|
|
|
|
#[property(get = Self::is_next_available)]
|
|
|
|
|
_is_next_available: PhantomData<bool>,
|
2020-12-26 02:05:34 -08:00
|
|
|
}
|
|
|
|
|
|
2021-06-27 15:28:35 -07:00
|
|
|
#[glib::object_subclass]
|
2021-12-21 10:55:02 -08:00
|
|
|
impl ObjectSubclass for LpImageView {
|
|
|
|
|
const NAME: &'static str = "LpImageView";
|
|
|
|
|
type Type = super::LpImageView;
|
2021-12-21 12:15:46 -08:00
|
|
|
type ParentType = adw::Bin;
|
2020-12-26 02:05:34 -08:00
|
|
|
|
|
|
|
|
fn class_init(klass: &mut Self::Class) {
|
2021-01-30 15:52:32 -08:00
|
|
|
Self::bind_template(klass);
|
2022-01-19 23:11:38 -08:00
|
|
|
Self::Type::bind_template_callbacks(klass);
|
2020-12-26 02:05:34 -08:00
|
|
|
}
|
2021-01-13 23:05:45 -08:00
|
|
|
|
2021-06-27 15:28:35 -07:00
|
|
|
fn instance_init(obj: &glib::subclass::InitializingObject<Self>) {
|
2021-01-13 23:05:45 -08:00
|
|
|
obj.init_template();
|
|
|
|
|
}
|
2020-12-26 02:05:34 -08:00
|
|
|
}
|
|
|
|
|
|
2021-12-21 10:55:02 -08:00
|
|
|
impl ObjectImpl for LpImageView {
|
2021-01-30 15:52:32 -08:00
|
|
|
fn properties() -> &'static [glib::ParamSpec] {
|
2023-11-29 19:10:57 +01:00
|
|
|
Self::derived_properties()
|
2020-12-26 02:05:34 -08:00
|
|
|
}
|
|
|
|
|
|
2023-11-29 19:10:57 +01:00
|
|
|
fn set_property(&self, id: usize, value: &glib::Value, pspec: &glib::ParamSpec) {
|
|
|
|
|
self.derived_set_property(id, value, pspec)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn property(&self, id: usize, pspec: &glib::ParamSpec) -> glib::Value {
|
|
|
|
|
self.derived_property(id, pspec)
|
2020-12-26 02:05:34 -08:00
|
|
|
}
|
|
|
|
|
|
2022-11-02 21:11:43 +01:00
|
|
|
fn constructed(&self) {
|
2023-02-12 02:07:42 +01:00
|
|
|
let obj = self.obj();
|
2022-11-02 21:11:43 +01:00
|
|
|
|
|
|
|
|
self.parent_constructed();
|
2021-01-30 15:52:32 -08:00
|
|
|
|
2022-11-16 20:14:39 +01:00
|
|
|
// Manually mange widget layout, see `WidgetImpl` for details
|
2023-02-12 02:07:42 +01:00
|
|
|
obj.set_layout_manager(None::<gtk::LayoutManager>);
|
2022-11-16 20:14:39 +01:00
|
|
|
|
2023-11-29 20:15:42 +01:00
|
|
|
self.sliding_view.connect_current_page_notify(
|
|
|
|
|
glib::clone!(@weak obj => move |_| obj.page_changed()),
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
self.sliding_view.connect_target_page_reached(
|
|
|
|
|
glib::clone!(@weak obj => move || obj.target_page_reached()),
|
|
|
|
|
);
|
|
|
|
|
|
2023-08-02 14:27:23 +02:00
|
|
|
let signal_group = glib::SignalGroup::new::<LpImage>();
|
2023-03-02 22:43:29 -05:00
|
|
|
obj.connect_notify_local(
|
|
|
|
|
Some("current-page"),
|
|
|
|
|
clone!(@weak signal_group => move |obj, _| {
|
|
|
|
|
signal_group.set_target(obj.current_image().as_ref());
|
|
|
|
|
}),
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
let view = &*obj;
|
|
|
|
|
signal_group.connect_closure(
|
2023-04-12 04:13:30 +02:00
|
|
|
"notify::file",
|
2023-03-02 22:43:29 -05:00
|
|
|
true,
|
|
|
|
|
glib::closure_local!(@watch view => move |_: LpImage, _: glib::ParamSpec| {
|
|
|
|
|
view.current_image_path_changed();
|
|
|
|
|
}),
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
signal_group.connect_closure(
|
|
|
|
|
"notify::is-deleted",
|
|
|
|
|
true,
|
|
|
|
|
glib::closure_local!(@watch view => move |_: LpImage, _: glib::ParamSpec| {
|
|
|
|
|
view.current_image_path_changed();
|
|
|
|
|
}),
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
self.current_image_signals.set(signal_group).unwrap();
|
2023-01-23 01:10:15 +01:00
|
|
|
|
2023-07-15 20:21:00 +02:00
|
|
|
self.drag_source.set_exclusive(true);
|
2022-01-08 16:21:27 +01:00
|
|
|
|
2023-07-15 20:21:00 +02:00
|
|
|
self.drag_source.connect_prepare(
|
2022-11-10 19:39:23 +01:00
|
|
|
glib::clone!(@weak obj => @default-return None, move |gesture, _, _| {
|
|
|
|
|
let is_scrollable = obj
|
|
|
|
|
.current_page()
|
|
|
|
|
.map(|p| p.image().is_hscrollable() || p.image().is_vscrollable());
|
|
|
|
|
|
2023-07-31 21:13:27 +02:00
|
|
|
if is_scrollable == Some(true)
|
|
|
|
|
|| gesture.device().map(|x| x.source())
|
|
|
|
|
== Some(gdk::InputSource::Touchscreen)
|
|
|
|
|
{
|
|
|
|
|
// Do scrolling if scrollable and no drag and drop on touchscreen
|
2022-11-10 19:39:23 +01:00
|
|
|
gesture.set_state(gtk::EventSequenceState::Denied);
|
|
|
|
|
None
|
|
|
|
|
} else {
|
2023-05-01 17:44:45 +02:00
|
|
|
gesture.set_state(gtk::EventSequenceState::Claimed);
|
2022-11-28 18:27:40 +01:00
|
|
|
obj.current_page().and_then(|p| p.content_provider())
|
2022-11-10 19:39:23 +01:00
|
|
|
}
|
2022-01-08 16:21:27 +01:00
|
|
|
}),
|
|
|
|
|
);
|
|
|
|
|
|
2023-07-15 20:21:00 +02:00
|
|
|
self.drag_source.connect_drag_begin(glib::clone!(@weak obj => move |source, _| {
|
2023-03-07 20:48:09 +01:00
|
|
|
if let Some(paintable) = obj.current_image().and_then(|p| p.thumbnail()) {
|
|
|
|
|
// -6 for cursor width, +16 for margin in .drag-icon
|
|
|
|
|
source.set_icon(Some(&paintable), paintable.intrinsic_width() / 2 - 6 + 16, -12);
|
|
|
|
|
if let Some(drag) = source.drag() {
|
|
|
|
|
let drag_icon = gtk::DragIcon::for_drag(&drag);
|
|
|
|
|
// Rounds corners, adds outline and shadow
|
|
|
|
|
drag_icon.add_css_class("drag-icon");
|
|
|
|
|
// Make border-radius clip the image
|
|
|
|
|
drag_icon.set_overflow(gtk::Overflow::Hidden);
|
|
|
|
|
}
|
2022-01-08 16:21:27 +01:00
|
|
|
};
|
|
|
|
|
}));
|
|
|
|
|
|
2023-07-15 20:21:00 +02:00
|
|
|
obj.add_controller(self.drag_source.clone());
|
2021-01-30 15:52:32 -08:00
|
|
|
}
|
2020-12-26 02:05:34 -08:00
|
|
|
}
|
|
|
|
|
|
2022-11-16 20:14:39 +01:00
|
|
|
impl WidgetImpl for LpImageView {
|
2023-03-21 21:50:54 +01:00
|
|
|
/// This manual implementation makes sure left an right overlay both fit into the window
|
2022-11-16 20:14:39 +01:00
|
|
|
fn measure(&self, orientation: gtk::Orientation, for_size: i32) -> (i32, i32, i32, i32) {
|
|
|
|
|
// Measure child of AdwBin
|
2022-12-08 02:00:50 +01:00
|
|
|
let (child_min, child_natural, _, _) = self.bin_child.measure(orientation, for_size);
|
|
|
|
|
|
|
|
|
|
// measure size of controls overlays
|
|
|
|
|
let (overlay1_min, _, _, _) = self.controls_box_start.measure(orientation, for_size);
|
|
|
|
|
let (overlay2_min, _, _, _) = self.controls_box_end.measure(orientation, for_size);
|
|
|
|
|
|
2023-05-03 16:12:28 +02:00
|
|
|
let overlay_min = match orientation {
|
|
|
|
|
gtk::Orientation::Horizontal => overlay1_min + overlay2_min,
|
|
|
|
|
gtk::Orientation::Vertical => i32::max(overlay1_min, overlay2_min),
|
|
|
|
|
_ => unreachable!(),
|
|
|
|
|
};
|
|
|
|
|
|
2022-12-08 02:00:50 +01:00
|
|
|
// take as minimum whatever is larger
|
2023-05-03 16:12:28 +02:00
|
|
|
let min = i32::max(child_min, overlay_min);
|
2023-03-21 21:50:54 +01:00
|
|
|
// take as natural whatever is larger
|
|
|
|
|
let natural = i32::max(child_natural, min);
|
2022-11-16 20:14:39 +01:00
|
|
|
|
2023-03-21 21:50:54 +01:00
|
|
|
(min, natural, -1, -1)
|
2022-11-16 20:14:39 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// this is necessary because we do layout manually
|
|
|
|
|
fn size_allocate(&self, width: i32, height: i32, baseline: i32) {
|
|
|
|
|
self.bin_child.allocate(width, height, baseline, None);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-12-21 10:55:02 -08:00
|
|
|
impl BinImpl for LpImageView {}
|
2023-11-29 19:10:57 +01:00
|
|
|
|
|
|
|
|
impl LpImageView {
|
|
|
|
|
pub fn current_page(&self) -> Option<LpImagePage> {
|
|
|
|
|
self.sliding_view.current_page()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Returns `true` if there is an image before the current one
|
|
|
|
|
pub fn is_previous_available(&self) -> bool {
|
|
|
|
|
let obj = self.obj();
|
|
|
|
|
|
|
|
|
|
if let Some(file) = obj.current_file() {
|
|
|
|
|
obj.model().index_of(&file) != Some(0)
|
|
|
|
|
} else {
|
|
|
|
|
false
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Returns `true` if there is an image after the current one
|
|
|
|
|
pub fn is_next_available(&self) -> bool {
|
|
|
|
|
let obj = self.obj();
|
|
|
|
|
|
|
|
|
|
if let Some(file) = obj.current_file() {
|
|
|
|
|
let model = obj.model();
|
|
|
|
|
model.index_of(&file) != Some(model.n_files().saturating_sub(1))
|
|
|
|
|
} else {
|
|
|
|
|
false
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2020-12-26 02:05:34 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
glib::wrapper! {
|
2021-12-21 10:55:02 -08:00
|
|
|
pub struct LpImageView(ObjectSubclass<imp::LpImageView>)
|
2021-12-21 12:15:46 -08:00
|
|
|
@extends gtk::Widget, adw::Bin,
|
2020-12-26 02:05:34 -08:00
|
|
|
@implements gtk::Buildable, gtk::ConstraintTarget, gtk::Orientable;
|
|
|
|
|
}
|
|
|
|
|
|
2022-01-19 23:11:38 -08:00
|
|
|
#[gtk::template_callbacks]
|
2021-12-21 10:55:02 -08:00
|
|
|
impl LpImageView {
|
2023-05-30 12:52:59 +00:00
|
|
|
pub fn set_images_from_files(&self, files: Vec<gio::File>) {
|
2023-03-08 01:40:47 +01:00
|
|
|
// Add image to recently used file. Does not work in Flatpaks:
|
|
|
|
|
// <https://github.com/flatpak/xdg-desktop-portal/issues/215>
|
2023-04-18 01:52:06 +02:00
|
|
|
let recent_manager = gtk::RecentManager::default();
|
|
|
|
|
for file in files.iter() {
|
|
|
|
|
recent_manager.add_item(&file.uri());
|
|
|
|
|
}
|
2023-05-30 12:52:59 +00:00
|
|
|
|
|
|
|
|
if files.len() == 1 {
|
|
|
|
|
self.load_file(files[0].clone());
|
|
|
|
|
} else {
|
|
|
|
|
self.load_files(files);
|
|
|
|
|
}
|
2023-04-18 01:52:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn load_files(&self, files: Vec<gio::File>) {
|
|
|
|
|
let sliding_view = self.sliding_view();
|
|
|
|
|
|
|
|
|
|
if let Some(first) = files.first().cloned() {
|
|
|
|
|
let model = LpFileModel::from_files(files);
|
|
|
|
|
self.set_model(model);
|
2023-04-28 00:44:04 +02:00
|
|
|
let page = self.new_image_page(&first);
|
2023-04-18 01:52:06 +02:00
|
|
|
|
|
|
|
|
sliding_view.clear();
|
|
|
|
|
sliding_view.append(&page);
|
|
|
|
|
self.update_sliding_view(&first);
|
|
|
|
|
} else {
|
|
|
|
|
log::error!("File list was empty");
|
2021-09-05 05:51:34 -07:00
|
|
|
}
|
2021-03-02 16:39:03 -08:00
|
|
|
}
|
|
|
|
|
|
2022-01-19 23:11:38 -08:00
|
|
|
// Builds an `LpFileModel`, which is an implementation of `gio::ListModel`
|
|
|
|
|
// that holds a `gio::File` for each child within the same directory as the
|
|
|
|
|
// file we pass. This model will update with changes to the directory,
|
|
|
|
|
// and in turn we'll update our `adw::Carousel`.
|
2023-04-18 01:52:06 +02:00
|
|
|
fn load_file(&self, file: gio::File) {
|
2023-04-12 01:11:59 +02:00
|
|
|
let directory = file.parent();
|
2021-09-05 05:51:34 -07:00
|
|
|
|
2023-04-12 01:11:59 +02:00
|
|
|
let is_same_directory = if let (Some(d1), Some(d2)) = (&directory, self.model().directory())
|
|
|
|
|
{
|
|
|
|
|
d1.equal(&d2)
|
|
|
|
|
} else {
|
|
|
|
|
false
|
|
|
|
|
};
|
2022-11-16 20:14:39 +01:00
|
|
|
|
2023-01-23 01:10:15 +01:00
|
|
|
if is_same_directory {
|
|
|
|
|
log::debug!("Re-using old model and navigating to the current file");
|
2023-04-18 01:52:06 +02:00
|
|
|
self.navigate_to_file(&file);
|
|
|
|
|
return;
|
2023-01-23 01:10:15 +01:00
|
|
|
}
|
2022-11-26 00:02:56 +01:00
|
|
|
|
2023-04-18 01:52:06 +02:00
|
|
|
let model = LpFileModel::from_file(file.clone());
|
2023-01-23 01:10:15 +01:00
|
|
|
self.set_model(model);
|
|
|
|
|
log::debug!("New model created");
|
2022-11-16 20:14:39 +01:00
|
|
|
|
2023-04-18 01:52:06 +02:00
|
|
|
self.update_sliding_view(&file);
|
2023-01-23 01:10:15 +01:00
|
|
|
|
|
|
|
|
// List other files in directory
|
|
|
|
|
if let Some(directory) = directory {
|
2023-11-22 09:12:07 +01:00
|
|
|
glib::spawn_future_local(glib::clone!(@weak self as obj => async move {
|
2023-04-12 01:11:59 +02:00
|
|
|
if let Err(err) = obj.model().load_directory(directory.clone()).await {
|
2023-11-16 15:29:16 +08:00
|
|
|
log::warn!("Failed to load directory: {}", err.root_cause());
|
|
|
|
|
obj.activate_action("win.show-toast", Some(&(format!("{} {}", err, err.root_cause()), adw::ToastPriority::High.into_glib()).to_variant()))
|
2023-01-23 01:10:15 +01:00
|
|
|
.unwrap();
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2023-04-12 01:11:59 +02:00
|
|
|
obj.update_sliding_view(&file);
|
2023-01-23 01:10:15 +01:00
|
|
|
}));
|
|
|
|
|
}
|
2022-01-19 23:11:38 -08:00
|
|
|
}
|
2022-01-16 19:03:13 -08:00
|
|
|
|
2023-01-23 01:10:15 +01:00
|
|
|
/// Move forward or backwards
|
2023-02-06 19:14:01 +01:00
|
|
|
pub fn navigate(&self, direction: Direction) {
|
2023-04-12 01:11:59 +02:00
|
|
|
if let Some(current_file) = self.current_file() {
|
|
|
|
|
let new_file = match direction {
|
|
|
|
|
Direction::Forward => self.model().after(¤t_file),
|
|
|
|
|
Direction::Back => self.model().before(¤t_file),
|
2023-01-23 01:10:15 +01:00
|
|
|
};
|
|
|
|
|
|
2023-04-12 01:11:59 +02:00
|
|
|
if let Some(new_file) = new_file {
|
|
|
|
|
self.scroll_sliding_view(&new_file);
|
2022-01-19 23:11:38 -08:00
|
|
|
}
|
2023-01-23 01:10:15 +01:00
|
|
|
}
|
2021-09-05 05:51:34 -07:00
|
|
|
}
|
|
|
|
|
|
2023-02-06 19:14:01 +01:00
|
|
|
/// Jump to position
|
|
|
|
|
pub fn jump(&self, position: Position) {
|
2023-04-12 01:11:59 +02:00
|
|
|
let new_file = match position {
|
2023-02-06 19:14:01 +01:00
|
|
|
Position::First => self.model().first(),
|
|
|
|
|
Position::Last => self.model().last(),
|
|
|
|
|
};
|
|
|
|
|
|
2023-04-12 01:11:59 +02:00
|
|
|
if let Some(new_file) = new_file {
|
|
|
|
|
self.navigate_to_file(&new_file);
|
2023-02-06 19:14:01 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-01-23 01:10:15 +01:00
|
|
|
/// Used for drag and drop
|
2023-04-12 01:11:59 +02:00
|
|
|
fn navigate_to_file(&self, new_file: &gio::File) {
|
2023-01-23 01:10:15 +01:00
|
|
|
let sliding_view = self.sliding_view();
|
2022-12-06 18:24:32 +01:00
|
|
|
|
2023-06-26 11:59:17 +00:00
|
|
|
if self
|
|
|
|
|
.current_file()
|
|
|
|
|
.is_some_and(|current_file| current_file.equal(new_file))
|
|
|
|
|
{
|
|
|
|
|
return;
|
2023-01-23 01:10:15 +01:00
|
|
|
}
|
2022-06-28 22:54:26 -04:00
|
|
|
|
2023-04-12 01:11:59 +02:00
|
|
|
let Some(new_index) = self.model().index_of(new_file) else {
|
|
|
|
|
log::warn!("Could not navigate to file {}", new_file.uri());
|
2022-06-28 22:54:26 -04:00
|
|
|
return;
|
2023-01-23 01:10:15 +01:00
|
|
|
};
|
2022-06-28 22:54:26 -04:00
|
|
|
|
2023-01-23 01:10:15 +01:00
|
|
|
let current_index = self
|
2023-04-12 01:11:59 +02:00
|
|
|
.current_file()
|
2023-01-23 01:10:15 +01:00
|
|
|
.and_then(|x| self.model().index_of(&x))
|
|
|
|
|
.unwrap_or_default();
|
2022-06-28 22:54:26 -04:00
|
|
|
|
2023-04-12 04:13:30 +02:00
|
|
|
let page = if let Some(page) = sliding_view.get(new_file) {
|
2023-01-23 01:10:15 +01:00
|
|
|
page
|
2022-06-28 22:54:26 -04:00
|
|
|
} else {
|
2023-04-28 00:44:04 +02:00
|
|
|
let page = self.new_image_page(new_file);
|
2022-06-28 22:54:26 -04:00
|
|
|
|
2023-01-23 01:10:15 +01:00
|
|
|
if new_index > current_index {
|
|
|
|
|
sliding_view.append(&page);
|
|
|
|
|
} else {
|
|
|
|
|
sliding_view.prepend(&page);
|
|
|
|
|
}
|
2022-12-06 18:24:32 +01:00
|
|
|
|
2023-01-23 01:10:15 +01:00
|
|
|
page
|
|
|
|
|
};
|
|
|
|
|
|
2023-04-12 01:11:59 +02:00
|
|
|
log::debug!("Scrolling to page for {}", new_file.uri());
|
2023-01-23 01:10:15 +01:00
|
|
|
self.imp().preserve_content.set(true);
|
|
|
|
|
sliding_view.scroll_to(&page);
|
2022-06-29 12:22:39 -04:00
|
|
|
}
|
|
|
|
|
|
2023-01-23 01:10:15 +01:00
|
|
|
/// Ensures the sliding view contains the correct images
|
2023-04-12 01:11:59 +02:00
|
|
|
fn update_sliding_view(&self, current_file: &gio::File) {
|
|
|
|
|
log::debug!(
|
|
|
|
|
"Updating sliding_view neighbors for current path {}",
|
|
|
|
|
current_file.uri()
|
|
|
|
|
);
|
2023-01-23 01:10:15 +01:00
|
|
|
let sliding_view = self.sliding_view();
|
2022-06-28 22:54:26 -04:00
|
|
|
|
2023-01-23 01:10:15 +01:00
|
|
|
self.imp().preserve_content.set(false);
|
|
|
|
|
|
2023-04-12 04:13:30 +02:00
|
|
|
let existing = sliding_view.pages();
|
|
|
|
|
let target = self.model().files_around(current_file, BUFFER);
|
2023-01-23 01:10:15 +01:00
|
|
|
|
|
|
|
|
// remove old pages
|
2023-04-12 01:11:59 +02:00
|
|
|
for (uri, page) in &existing {
|
|
|
|
|
if !target.contains_key(uri) {
|
2023-04-12 04:13:30 +02:00
|
|
|
sliding_view.remove(page);
|
2022-06-28 22:54:26 -04:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-01-23 01:10:15 +01:00
|
|
|
// add missing pages or put in correct position
|
2023-04-12 01:11:59 +02:00
|
|
|
for (position, (_, file)) in target.iter().enumerate() {
|
|
|
|
|
if let Some(page) = existing.get(&file.uri()) {
|
2023-04-12 04:13:30 +02:00
|
|
|
sliding_view.move_to(page, position);
|
2023-01-23 01:10:15 +01:00
|
|
|
} else {
|
2023-04-28 00:44:04 +02:00
|
|
|
sliding_view.insert(&self.new_image_page(file), position);
|
2022-06-28 22:54:26 -04:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-12-06 18:24:32 +01:00
|
|
|
self.notify("is-previous-available");
|
|
|
|
|
self.notify("is-next-available");
|
2022-06-29 12:22:39 -04:00
|
|
|
}
|
|
|
|
|
|
2023-04-12 01:11:59 +02:00
|
|
|
fn scroll_sliding_view(&self, file: &gio::File) {
|
|
|
|
|
let Some(current_page) = self.sliding_view().pages().remove(&file.uri()) else {
|
2023-07-06 23:48:56 +02:00
|
|
|
log::error!(
|
|
|
|
|
"Current path not available in sliding_view for scrolling: {}",
|
|
|
|
|
file.uri()
|
|
|
|
|
);
|
2023-01-23 01:10:15 +01:00
|
|
|
return;
|
|
|
|
|
};
|
2022-06-29 12:22:39 -04:00
|
|
|
|
2023-01-23 01:10:15 +01:00
|
|
|
self.sliding_view().scroll_to(¤t_page);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn sliding_view(&self) -> LpSlidingView {
|
|
|
|
|
self.imp().sliding_view.clone()
|
|
|
|
|
}
|
|
|
|
|
|
2023-04-20 02:32:41 +02:00
|
|
|
pub fn controls_box_start(&self) -> gtk::Widget {
|
|
|
|
|
self.imp().controls_box_start.clone()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn controls_box_start_events(&self) -> gtk::EventControllerMotion {
|
|
|
|
|
self.imp().controls_box_start_events.clone()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn controls_box_end(&self) -> gtk::Widget {
|
|
|
|
|
self.imp().controls_box_end.clone()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn controls_box_end_events(&self) -> gtk::EventControllerMotion {
|
|
|
|
|
self.imp().controls_box_end_events.clone()
|
|
|
|
|
}
|
|
|
|
|
|
2023-01-23 01:10:15 +01:00
|
|
|
fn model(&self) -> LpFileModel {
|
|
|
|
|
self.imp().model.borrow().clone()
|
|
|
|
|
}
|
|
|
|
|
|
2023-04-28 00:44:04 +02:00
|
|
|
/// Create new image that communicates updates to file model
|
|
|
|
|
fn new_image_page(&self, file: &gio::File) -> LpImagePage {
|
|
|
|
|
let page = LpImagePage::from_file(file);
|
|
|
|
|
|
|
|
|
|
page.image().connect_notify_local(
|
|
|
|
|
Some("is-unsupported"),
|
|
|
|
|
glib::clone!(@weak self as obj => move |image, _| {
|
|
|
|
|
if image.is_unsupported() {
|
|
|
|
|
if obj.current_image().as_ref() == Some(image) {
|
|
|
|
|
log::debug!(
|
|
|
|
|
"Image format unsupported but not removing since current image"
|
|
|
|
|
);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if let Some(file) = image.file() {
|
|
|
|
|
log::debug!("Removing image with unsupported format {:?}", file.uri());
|
|
|
|
|
obj.model().remove(&file);
|
2023-09-05 19:16:30 +02:00
|
|
|
if let Some(current_file) = obj.current_file() {
|
|
|
|
|
obj.update_sliding_view(¤t_file);
|
|
|
|
|
}
|
2023-04-28 00:44:04 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}),
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
page
|
|
|
|
|
}
|
|
|
|
|
|
2023-01-23 01:10:15 +01:00
|
|
|
fn set_model(&self, model: LpFileModel) {
|
|
|
|
|
model.connect_changed(
|
|
|
|
|
glib::clone!(@weak self as obj => move || obj.model_content_changed_cb()),
|
|
|
|
|
);
|
|
|
|
|
self.imp().model.replace(model);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Handle files are added or removed from directory
|
|
|
|
|
fn model_content_changed_cb(&self) {
|
2023-07-06 23:48:56 +02:00
|
|
|
let Some(current_file) = self.current_file() else {
|
|
|
|
|
return;
|
|
|
|
|
};
|
2022-06-29 12:22:39 -04:00
|
|
|
|
2023-01-23 01:10:15 +01:00
|
|
|
// LpImage did not get the update yet
|
|
|
|
|
// Update will be handled by current_image_path_changed
|
2023-04-12 01:11:59 +02:00
|
|
|
if !self.model().contains(¤t_file) {
|
2023-01-23 01:10:15 +01:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2023-04-12 01:11:59 +02:00
|
|
|
self.update_sliding_view(¤t_file);
|
2023-01-23 01:10:15 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Handle current image being moved or deleted
|
|
|
|
|
///
|
|
|
|
|
/// This is handled separately since we want to delete animations and
|
|
|
|
|
/// want to still show the same image if renamed.
|
|
|
|
|
fn current_image_path_changed(&self) {
|
|
|
|
|
if let Some(image) = self.current_page().map(|x| x.image()) {
|
|
|
|
|
if image.is_deleted() {
|
|
|
|
|
self.sliding_view().scroll_to_neighbor();
|
2022-06-29 12:22:39 -04:00
|
|
|
}
|
2023-01-23 01:10:15 +01:00
|
|
|
}
|
|
|
|
|
|
2023-04-12 01:11:59 +02:00
|
|
|
if let Some(current_file) = self.current_file() {
|
|
|
|
|
if self.model().contains(¤t_file) && !self.imp().preserve_content.get() {
|
|
|
|
|
self.update_sliding_view(¤t_file);
|
2022-06-29 12:22:39 -04:00
|
|
|
}
|
|
|
|
|
}
|
2023-01-23 01:10:15 +01:00
|
|
|
}
|
2022-12-06 18:24:32 +01:00
|
|
|
|
2023-01-30 19:07:17 +01:00
|
|
|
pub fn current_image(&self) -> Option<LpImage> {
|
|
|
|
|
self.imp().sliding_view.current_page().map(|x| x.image())
|
|
|
|
|
}
|
|
|
|
|
|
2023-03-02 23:14:14 -05:00
|
|
|
pub fn current_image_signals(&self) -> &glib::SignalGroup {
|
|
|
|
|
self.imp()
|
|
|
|
|
.current_image_signals
|
|
|
|
|
.get()
|
|
|
|
|
.expect("Signal group should be set up during construction")
|
|
|
|
|
}
|
|
|
|
|
|
2023-04-12 01:11:59 +02:00
|
|
|
pub fn current_uri(&self) -> Option<glib::GString> {
|
|
|
|
|
self.imp()
|
|
|
|
|
.sliding_view
|
|
|
|
|
.current_page()
|
|
|
|
|
.map(|x| x.file().uri())
|
2023-01-23 01:10:15 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn current_file(&self) -> Option<gio::File> {
|
|
|
|
|
self.imp()
|
|
|
|
|
.sliding_view
|
|
|
|
|
.current_page()
|
|
|
|
|
.and_then(|x| x.image().file())
|
2022-06-28 22:54:26 -04:00
|
|
|
}
|
|
|
|
|
|
2023-07-15 20:21:00 +02:00
|
|
|
pub fn drag_source(&self) -> gtk::DragSource {
|
|
|
|
|
self.imp().drag_source.clone()
|
|
|
|
|
}
|
|
|
|
|
|
2023-11-29 20:15:42 +01:00
|
|
|
fn page_changed(&self) {
|
|
|
|
|
self.notify_current_page();
|
|
|
|
|
self.notify_is_next_available();
|
|
|
|
|
self.notify_is_previous_available();
|
2022-08-19 12:06:07 -04:00
|
|
|
|
2023-01-23 01:10:15 +01:00
|
|
|
let Some(new_page) = self.current_page() else {
|
|
|
|
|
log::debug!("Page changed but no current page");
|
|
|
|
|
return;
|
|
|
|
|
};
|
2022-06-28 18:52:52 -04:00
|
|
|
|
2023-01-23 01:10:15 +01:00
|
|
|
if !self.imp().preserve_content.get() {
|
2023-04-12 01:11:59 +02:00
|
|
|
self.update_sliding_view(&new_page.file());
|
2022-08-19 12:06:07 -04:00
|
|
|
}
|
2023-01-23 01:10:15 +01:00
|
|
|
}
|
2022-01-19 23:11:38 -08:00
|
|
|
|
2023-02-02 20:23:05 +01:00
|
|
|
/// Called when page change animation completed
|
2023-11-29 20:15:42 +01:00
|
|
|
fn target_page_reached(&self) {
|
2023-02-02 20:23:05 +01:00
|
|
|
let current_page = self.current_page();
|
|
|
|
|
|
2023-01-23 01:10:15 +01:00
|
|
|
if self.imp().preserve_content.get() {
|
2023-02-02 20:23:05 +01:00
|
|
|
if let Some(new_page) = ¤t_page {
|
2023-04-12 01:11:59 +02:00
|
|
|
self.update_sliding_view(&new_page.file());
|
2023-01-23 01:10:15 +01:00
|
|
|
} else {
|
|
|
|
|
log::error!("No LpImagePage");
|
2022-01-19 23:11:38 -08:00
|
|
|
}
|
|
|
|
|
}
|
2023-02-02 20:23:05 +01:00
|
|
|
|
|
|
|
|
// Reset zoom and rotation of other pages
|
|
|
|
|
if let Some(new_page) = ¤t_page {
|
|
|
|
|
let mut other_pages = self.sliding_view().pages();
|
2023-04-12 01:11:59 +02:00
|
|
|
other_pages.remove(&new_page.file().uri());
|
2023-02-02 20:23:05 +01:00
|
|
|
for (_, page) in other_pages {
|
|
|
|
|
let image = page.image();
|
decoder: Use image-rs, libheif-rs, librsvg
This adds proper support for a lot of image formats including SVGs.
It will be the basis for many features to come. The technical core
is an asynchronous renderer that can arrange textures in smaller tiles
which might be generated on demand.
Closes #34, #40, #41, #49
Basis for fixing the following issues #7, #27, #36, #43, #68, #71
2023-03-04 03:14:52 +01:00
|
|
|
image.reset_rotation();
|
2023-02-02 20:23:05 +01:00
|
|
|
image.set_best_fit(true);
|
|
|
|
|
image.zoom_best_fit();
|
|
|
|
|
}
|
|
|
|
|
}
|
2022-01-19 23:11:38 -08:00
|
|
|
}
|
|
|
|
|
|
2022-07-10 19:39:03 -04:00
|
|
|
#[template_callback]
|
|
|
|
|
fn get_fullscreen_icon(&self, fullscreened: bool) -> &'static str {
|
|
|
|
|
if fullscreened {
|
|
|
|
|
"view-restore-symbolic"
|
|
|
|
|
} else {
|
|
|
|
|
"view-fullscreen-symbolic"
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-11-10 19:39:23 +01:00
|
|
|
pub fn zoom_out(&self) {
|
|
|
|
|
if let Some(current_page) = self.current_page() {
|
|
|
|
|
current_page.image().zoom_out();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn zoom_in(&self) {
|
|
|
|
|
if let Some(current_page) = self.current_page() {
|
|
|
|
|
current_page.image().zoom_in();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-11-05 00:57:18 +01:00
|
|
|
pub fn rotate_image(&self, angle: f64) {
|
2022-11-03 17:40:06 +01:00
|
|
|
if let Some(current_page) = self.current_page() {
|
2022-11-10 19:39:23 +01:00
|
|
|
current_page.image().rotate_by(angle);
|
2022-11-03 17:40:06 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-01-22 12:18:43 -05:00
|
|
|
pub async fn set_background(&self) -> anyhow::Result<()> {
|
2023-04-12 01:11:59 +02:00
|
|
|
let uri = self
|
|
|
|
|
.current_file()
|
|
|
|
|
.and_then(|f| url::Url::parse(f.uri().as_str()).ok())
|
|
|
|
|
.context("Invalid URL for background image")?;
|
|
|
|
|
let native = self.native().context("View should have a GtkNative")?;
|
2023-01-22 12:18:43 -05:00
|
|
|
let id = WindowIdentifier::from_native(&native).await;
|
|
|
|
|
|
|
|
|
|
match wallpaper::WallpaperRequest::default()
|
|
|
|
|
.set_on(wallpaper::SetOn::Background)
|
|
|
|
|
.show_preview(true)
|
|
|
|
|
.identifier(id)
|
2023-04-12 01:11:59 +02:00
|
|
|
.build_uri(&uri)
|
2023-01-22 12:18:43 -05:00
|
|
|
.await
|
2023-05-24 15:34:52 +00:00
|
|
|
.and_then(|req| req.response())
|
2023-01-22 12:18:43 -05:00
|
|
|
{
|
|
|
|
|
Ok(_) => self
|
|
|
|
|
.activate_action(
|
|
|
|
|
"win.show-toast",
|
2023-04-18 09:08:41 +00:00
|
|
|
Some(
|
|
|
|
|
&(
|
|
|
|
|
// Translators: This is a toast notification, informing the user that an image has been set as background.
|
|
|
|
|
gettext("Set as background."),
|
2023-05-25 10:56:38 +00:00
|
|
|
adw::ToastPriority::High.into_glib(),
|
2023-04-18 09:08:41 +00:00
|
|
|
)
|
|
|
|
|
.to_variant(),
|
|
|
|
|
),
|
2023-01-22 12:18:43 -05:00
|
|
|
)
|
|
|
|
|
.unwrap(),
|
|
|
|
|
Err(err) => {
|
|
|
|
|
if !matches!(err, Error::Response(ResponseError::Cancelled)) {
|
|
|
|
|
self.activate_action(
|
2022-11-02 21:11:43 +01:00
|
|
|
"win.show-toast",
|
2023-05-25 10:56:38 +00:00
|
|
|
Some(
|
|
|
|
|
&(
|
|
|
|
|
gettext("Could not set background."),
|
|
|
|
|
adw::ToastPriority::High.into_glib(),
|
|
|
|
|
)
|
|
|
|
|
.to_variant(),
|
|
|
|
|
),
|
2022-11-02 21:11:43 +01:00
|
|
|
)
|
2023-01-22 12:18:43 -05:00
|
|
|
.unwrap();
|
2022-11-02 21:11:43 +01:00
|
|
|
}
|
2023-01-22 12:18:43 -05:00
|
|
|
}
|
|
|
|
|
};
|
2020-12-26 02:05:34 -08:00
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn print(&self) -> anyhow::Result<()> {
|
2023-04-02 01:39:23 +02:00
|
|
|
let image = self
|
|
|
|
|
.current_image()
|
|
|
|
|
.context("No current image for printing")?;
|
|
|
|
|
|
|
|
|
|
let root = self.root().context("Could not get root for widget")?;
|
|
|
|
|
let window = root
|
|
|
|
|
.downcast_ref::<gtk::Window>()
|
|
|
|
|
.context("Could not downcast to GtkWindow")?;
|
|
|
|
|
|
2023-07-29 18:29:09 +02:00
|
|
|
LpPrint::new(image, window.clone(), None, None).run()?;
|
2023-04-02 01:39:23 +02:00
|
|
|
|
2020-12-26 02:05:34 -08:00
|
|
|
Ok(())
|
|
|
|
|
}
|
|
|
|
|
|
2022-01-08 14:02:16 +01:00
|
|
|
pub fn copy(&self) -> anyhow::Result<()> {
|
|
|
|
|
let clipboard = self.clipboard();
|
|
|
|
|
|
2023-01-24 01:51:59 +01:00
|
|
|
let content_provider = self
|
|
|
|
|
.current_page()
|
|
|
|
|
.context("No current page")?
|
|
|
|
|
.content_provider();
|
|
|
|
|
clipboard.set_content(content_provider.as_ref())?;
|
2022-01-08 14:02:16 +01:00
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
|
}
|
2020-12-26 02:05:34 -08:00
|
|
|
}
|