editor: Apply rotation to files

This commit is contained in:
Sophie Herold 2024-06-19 00:36:54 +02:00
parent 4164fa61c3
commit afcc24e235
10 changed files with 116 additions and 3 deletions

View file

@ -50,6 +50,7 @@ impl Glycin {
updater.clone().spawn_error_handled(async move {
log::trace!("Setting up loader");
let mut loader = glycin::Loader::new(file);
loader.apply_transformations(false);
#[cfg(feature = "disable-glycin-sandbox")]
loader.sandbox_mechanism(Some(glycin::SandboxMechanism::NotSandboxed));

63
src/editing.rs Normal file
View file

@ -0,0 +1,63 @@
use std::sync::Arc;
use arc_swap::ArcSwap;
use glycin::{EditOutcome, Operations};
use crate::deps::*;
use crate::widgets::LpImage;
/// Queue for image edit operations that still have to be applied.
///
/// A queue is needed since further operations might be triggered while an
/// editing operation is in progress.
#[derive(Clone, Default, Debug)]
pub struct Queue {
operations: Arc<ArcSwap<Vec<glycin::Operation>>>,
/// Lock for in progress image operation
lock: Arc<async_lock::Mutex<()>>,
}
impl Queue {
pub fn push(&self, operation: glycin::Operation) {
self.operations.rcu(move |current_operations| {
let mut new = (**current_operations).clone();
new.push(operation.clone());
new
});
}
pub fn write_to_image(&self, image: &LpImage) {
let queue = self.clone();
let image = image.clone();
glib::spawn_future_local(async move {
let lock: async_lock::MutexGuard<()> = queue.lock.lock().await;
if let Err(err) = queue.apply(image).await {
log::error!("Err: {err}");
}
drop(lock);
});
}
async fn apply(&self, image: LpImage) -> anyhow::Result<()> {
let file = image.file().unwrap();
#[allow(unused_mut)]
let mut editor = glycin::Editor::new(file.clone());
#[cfg(feature = "disable-glycin-sandbox")]
editor.sandbox_mechanism(Some(glycin::SandboxMechanism::NotSandboxed));
let mut list = Vec::new();
self.operations.rcu(|current_operations| {
list.clone_from(&(**current_operations));
Vec::new()
});
let operations = Operations::new(list);
let edit_instruction = editor.apply_sparse(operations).await?;
if edit_instruction.apply_to(file).await? == EditOutcome::Unchanged {
log::error!("Writing new files is not supported yet");
}
Ok(())
}
}

View file

@ -55,6 +55,7 @@ mod about;
mod application;
mod config;
mod decoder;
mod editing;
mod file_model;
mod metadata;
mod util;

View file

@ -135,11 +135,15 @@ pub enum FitMode {
mod imp {
use super::*;
use crate::decoder::tiling::SharedFrameBuffer;
use crate::editing;
#[derive(Debug, Default, Properties)]
#[properties(wrapper_type = super::LpImage)]
pub struct LpImage {
pub(super) file: RefCell<Option<gio::File>>,
/// Contains a file from which to reload the image after animation has
/// finished.
pub(super) queued_reload: RefCell<Option<gio::File>>,
#[property(get)]
pub(super) is_deleted: Cell<bool>,
#[property(get)]
@ -235,6 +239,9 @@ mod imp {
/// Number of snapshots created, debug only
pub(super) nth_snapshot: Cell<u8>,
/// Editing queue
pub(super) editing_queue: editing::Queue,
}
#[glib::object_subclass]

View file

@ -283,6 +283,12 @@ impl LpImage {
let imp = self.imp();
log::debug!("Loading file {}", file.uri());
if imp.rotation_animation().state() == adw::AnimationState::Playing {
log::debug!("Queueing image reload due to playing rotate animation.");
self.imp().queued_reload.replace(Some(file.clone()));
return;
}
imp.metadata.replace(Metadata::default());
self.imp().emmit_metadata_changed();
self.imp().set_file_loaded(file).await;

View file

@ -46,11 +46,27 @@ impl imp::LpImage {
pub(super) fn rotation_animation(&self) -> &adw::TimedAnimation {
let obj = self.obj().to_owned();
self.rotation_animation.get_or_init(|| {
adw::TimedAnimation::builder()
let animation = adw::TimedAnimation::builder()
.duration(ROTATION_ANIMATION_DURATION)
.widget(&obj)
.target(&adw::PropertyAnimationTarget::new(&obj, "rotation"))
.build()
.build();
animation.connect_done(glib::clone!(
#[weak]
obj,
move |_| {
let imp = obj.imp();
if let Some(new_file) = imp.queued_reload.replace(None) {
glib::spawn_future_local(async move {
log::debug!("Animation finished: Executing delayed reload");
obj.load(&new_file).await;
});
}
}
));
animation
})
}
}
@ -58,6 +74,7 @@ impl imp::LpImage {
impl LpImage {
/// Set rotation and mirroring to the state would have after loading
pub fn reset_rotation(&self) {
log::debug!("Resetting rotation");
let orientation = self.metadata().orientation();
self.imp().rotation_target.set(-orientation.rotation);
self.set_mirrored(orientation.mirrored);
@ -84,5 +101,13 @@ impl LpImage {
animation.set_value_to(imp.zoom_level_best_fit_for_rotation(target.get()));
animation.play();
}
if let Ok(r) = gufo_common::orientation::Rotation::try_from(angle) {
log::debug!("Editing image to rotate by {r:?}");
let operation = glycin::Operation::Rotate(r);
let editing_queue = &self.imp().editing_queue;
editing_queue.push(operation);
editing_queue.write_to_image(self);
}
}
}