Loupe/src/widgets/image.rs

237 lines
7.4 KiB
Rust
Raw Normal View History

// image.rs
//
// Copyright 2021 Christopher Davis <christopherdavis@gnome.org>
//
// 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 crate::deps::*;
use gtk::{prelude::*, subclass::prelude::*};
use once_cell::sync::Lazy;
use std::cell::{Cell, RefCell};
mod imp {
use super::*;
#[derive(Debug, Default)]
pub struct LpImage {
pub file: RefCell<Option<gio::File>>,
pub image_width: Cell<i32>,
pub image_height: Cell<i32>,
pub texture: RefCell<Option<gdk::Texture>>,
}
#[glib::object_subclass]
impl ObjectSubclass for LpImage {
const NAME: &'static str = "LpImage";
type ParentType = gtk::Widget;
type Type = super::LpImage;
}
impl ObjectImpl for LpImage {
fn properties() -> &'static [glib::ParamSpec] {
static PROPERTIES: Lazy<Vec<glib::ParamSpec>> = Lazy::new(|| {
vec![glib::ParamSpecObject::new(
"file",
"File",
"The current file",
gio::File::static_type(),
glib::ParamFlags::READWRITE,
)]
});
PROPERTIES.as_ref()
}
fn property(&self, obj: &Self::Type, _id: usize, pspec: &glib::ParamSpec) -> glib::Value {
match pspec.name() {
"file" => obj.file().to_value(),
_ => unimplemented!(),
}
}
fn set_property(
&self,
obj: &Self::Type,
_id: usize,
value: &glib::Value,
pspec: &glib::ParamSpec,
) {
match pspec.name() {
"file" => obj.set_file(&value.get().unwrap()),
_ => unimplemented!(),
}
}
fn constructed(&self, obj: &Self::Type) {
obj.set_hexpand(true);
obj.set_vexpand(true);
}
widgets: Rework image_view with AdwCarousel, add ImagePage Previously we loaded images synchronously and manually changed `LpImage`. This could cause freezes when loading large images and SVGs. We also had no proper feedback for when a corrupted or unsupported image was loaded. This commit introduces a major rework to how we handle images and the gallery. It introduces a swipeable gallery that loads files asynchronously via a new widget: LpImagePage. LpImagePage owns the file and information related to the file, and on creation it spawns a future that either loads an error page when it can't load something (file deleted, corrupted, or unsupported) or the actual image via LpImage. LpImagePage now holds the right-click popover and the associated gestures. The gestures needed to be moved to LpImagePage in order to rework LpImageView's overlay - instead of having an overlay child that's a box with a few buttons, we now have the buttons as individual overlay children. This could cause them to overlap each other at certain sizes, but it's required in order to allow input to reach the carousel below. Right now this comes with a few regressions: * Occasionally input gets blocked across the applications. It's hard to tell if this is caused by swipes or right clicking. This input issue can cause a crash - no Rust backtrace, so a debugger is needed. * The "current" page doesn't seem to be properly loaded by the time `set_image_from_file ()` finishes, so we need to rework resizing from image dimensions * We need to re-implement loading different directories and files within directories There also seems to be an issue on touchscreens, swiping between images on a touchscreen causes responsiveness issues not present when swiping on a touchpad. My guess is that it's related to the long press gesture.
2022-01-19 23:11:38 -08:00
fn dispose(&self, obj: &Self::Type) {
while let Some(child) = obj.first_child() {
child.unparent();
}
}
}
impl WidgetImpl for LpImage {
fn snapshot(&self, widget: &Self::Type, snapshot: &gtk::Snapshot) {
if let Some(texture) = self.texture.borrow().as_ref() {
let width = widget.width() as f64;
let height = widget.height() as f64;
let texture_ratio = texture.intrinsic_aspect_ratio();
if texture_ratio == 0.0 {
texture.snapshot(snapshot.upcast_ref(), width, height);
} else {
let widget_ratio = width / height;
let (snapshot_width, snapshot_height) = if texture_ratio > widget_ratio {
(width, width / texture_ratio)
} else {
(height * texture_ratio, height)
};
let x = (width - snapshot_width.ceil()) / 2.0;
let y = (height - snapshot_height.ceil()).floor() / 2.0;
snapshot.save();
snapshot.translate(&graphene::Point::new(x as f32, y as f32));
texture.snapshot(snapshot.upcast_ref(), snapshot_width, snapshot_height);
snapshot.restore();
}
}
}
fn request_mode(&self, _widget: &Self::Type) -> gtk::SizeRequestMode {
gtk::SizeRequestMode::HeightForWidth
}
fn measure(
&self,
_widget: &Self::Type,
orienation: gtk::Orientation,
for_size: i32,
) -> (i32, i32, i32, i32) {
if let Some(texture) = self.texture.borrow().as_ref() {
let default_size = 16.0; // Fall back to default icon size; Just a guess since the API GtkPicture uses is private
let measurement = if for_size < 0 { 0 } else { for_size };
let (min_width, min_height): (f64, f64) = (0.0, 0.0);
let (min, nat) = if orienation == gtk::Orientation::Horizontal {
let size = texture.compute_concrete_size(
0.0,
measurement.into(),
default_size,
default_size,
);
let nat_width = size.0;
(min_width.ceil(), nat_width.ceil())
} else {
let size = texture.compute_concrete_size(
measurement.into(),
0.0,
default_size,
default_size,
);
let nat_height = size.1;
(min_height.ceil(), nat_height.ceil())
};
(min as i32, nat as i32, -1, -1)
} else {
(0, 0, -1, -1)
}
}
}
}
glib::wrapper! {
pub struct LpImage(ObjectSubclass<imp::LpImage>) @extends gtk::Widget;
}
impl LpImage {
pub fn file(&self) -> Option<gio::File> {
let imp = self.imp();
imp.file.borrow().clone()
}
pub fn set_file(&self, file: &gio::File) {
let imp = self.imp();
match gdk::Texture::from_file(file) {
Ok(texture) => {
imp.image_width.set(texture.width());
imp.image_height.set(texture.height());
imp.file.replace(Some(file.clone()));
imp.texture.replace(Some(texture));
}
Err(e) => log::error!("Could not load a valid image from this file: {}", e),
};
self.queue_draw();
self.queue_resize();
}
pub fn image_width(&self) -> i32 {
let imp = self.imp();
imp.image_width.get()
}
pub fn image_height(&self) -> i32 {
let imp = self.imp();
imp.image_height.get()
}
2022-01-08 14:02:16 +01:00
widgets: Rework image_view with AdwCarousel, add ImagePage Previously we loaded images synchronously and manually changed `LpImage`. This could cause freezes when loading large images and SVGs. We also had no proper feedback for when a corrupted or unsupported image was loaded. This commit introduces a major rework to how we handle images and the gallery. It introduces a swipeable gallery that loads files asynchronously via a new widget: LpImagePage. LpImagePage owns the file and information related to the file, and on creation it spawns a future that either loads an error page when it can't load something (file deleted, corrupted, or unsupported) or the actual image via LpImage. LpImagePage now holds the right-click popover and the associated gestures. The gestures needed to be moved to LpImagePage in order to rework LpImageView's overlay - instead of having an overlay child that's a box with a few buttons, we now have the buttons as individual overlay children. This could cause them to overlap each other at certain sizes, but it's required in order to allow input to reach the carousel below. Right now this comes with a few regressions: * Occasionally input gets blocked across the applications. It's hard to tell if this is caused by swipes or right clicking. This input issue can cause a crash - no Rust backtrace, so a debugger is needed. * The "current" page doesn't seem to be properly loaded by the time `set_image_from_file ()` finishes, so we need to rework resizing from image dimensions * We need to re-implement loading different directories and files within directories There also seems to be an issue on touchscreens, swiping between images on a touchscreen causes responsiveness issues not present when swiping on a touchpad. My guess is that it's related to the long press gesture.
2022-01-19 23:11:38 -08:00
pub fn set_texture_with_file(&self, texture: gdk::Texture, source_file: &gio::File) {
let imp = self.imp();
imp.texture.replace(Some(texture));
imp.file.replace(Some(source_file.clone()));
self.queue_draw();
self.queue_resize();
}
2022-01-08 14:02:16 +01:00
pub fn texture(&self) -> Option<gdk::Texture> {
let imp = self.imp();
imp.texture.borrow().clone()
}
widgets: Rework image_view with AdwCarousel, add ImagePage Previously we loaded images synchronously and manually changed `LpImage`. This could cause freezes when loading large images and SVGs. We also had no proper feedback for when a corrupted or unsupported image was loaded. This commit introduces a major rework to how we handle images and the gallery. It introduces a swipeable gallery that loads files asynchronously via a new widget: LpImagePage. LpImagePage owns the file and information related to the file, and on creation it spawns a future that either loads an error page when it can't load something (file deleted, corrupted, or unsupported) or the actual image via LpImage. LpImagePage now holds the right-click popover and the associated gestures. The gestures needed to be moved to LpImagePage in order to rework LpImageView's overlay - instead of having an overlay child that's a box with a few buttons, we now have the buttons as individual overlay children. This could cause them to overlap each other at certain sizes, but it's required in order to allow input to reach the carousel below. Right now this comes with a few regressions: * Occasionally input gets blocked across the applications. It's hard to tell if this is caused by swipes or right clicking. This input issue can cause a crash - no Rust backtrace, so a debugger is needed. * The "current" page doesn't seem to be properly loaded by the time `set_image_from_file ()` finishes, so we need to rework resizing from image dimensions * We need to re-implement loading different directories and files within directories There also seems to be an issue on touchscreens, swiping between images on a touchscreen causes responsiveness issues not present when swiping on a touchpad. My guess is that it's related to the long press gesture.
2022-01-19 23:11:38 -08:00
pub fn content_provider(&self) -> gdk::ContentProvider {
let imp = self.imp();
let mut contents = vec![];
if let Some(file) = imp.file.borrow().as_ref() {
let content = gdk::ContentProvider::for_value(&file.to_value());
contents.push(content);
}
if let Some(texture) = imp.texture.borrow().as_ref() {
let bytes = texture.save_to_png_bytes();
let content = gdk::ContentProvider::for_bytes("image/png", &bytes);
contents.push(content);
}
widgets: Rework image_view with AdwCarousel, add ImagePage Previously we loaded images synchronously and manually changed `LpImage`. This could cause freezes when loading large images and SVGs. We also had no proper feedback for when a corrupted or unsupported image was loaded. This commit introduces a major rework to how we handle images and the gallery. It introduces a swipeable gallery that loads files asynchronously via a new widget: LpImagePage. LpImagePage owns the file and information related to the file, and on creation it spawns a future that either loads an error page when it can't load something (file deleted, corrupted, or unsupported) or the actual image via LpImage. LpImagePage now holds the right-click popover and the associated gestures. The gestures needed to be moved to LpImagePage in order to rework LpImageView's overlay - instead of having an overlay child that's a box with a few buttons, we now have the buttons as individual overlay children. This could cause them to overlap each other at certain sizes, but it's required in order to allow input to reach the carousel below. Right now this comes with a few regressions: * Occasionally input gets blocked across the applications. It's hard to tell if this is caused by swipes or right clicking. This input issue can cause a crash - no Rust backtrace, so a debugger is needed. * The "current" page doesn't seem to be properly loaded by the time `set_image_from_file ()` finishes, so we need to rework resizing from image dimensions * We need to re-implement loading different directories and files within directories There also seems to be an issue on touchscreens, swiping between images on a touchscreen causes responsiveness issues not present when swiping on a touchpad. My guess is that it's related to the long press gesture.
2022-01-19 23:11:38 -08:00
gdk::ContentProvider::new_union(contents.as_slice())
}
}