Add optional automatic annotation copying

This commit is contained in:
rope 2026-09-19 09:52:32 +01:00
parent f65d5548de
commit 6477f8b500
5 changed files with 67 additions and 4 deletions

View file

@ -5,5 +5,9 @@
<default>false</default> <default>false</default>
<summary>Show the image properties sidebar</summary> <summary>Show the image properties sidebar</summary>
</key> </key>
<key name="auto-copy-annotations" type="b">
<default>true</default>
<summary>Copy the image to the clipboard after annotation changes</summary>
</key>
</schema> </schema>
</schemalist> </schemalist>

View file

@ -459,13 +459,24 @@ impl LpImage {
} }
pub fn undo_stroke(&self) { pub fn undo_stroke(&self) {
self.imp().strokes.borrow_mut().pop(); let changed = self.imp().strokes.borrow_mut().pop().is_some();
self.queue_draw(); self.queue_draw();
if changed {
self.copy_annotated_to_clipboard();
}
} }
pub fn clear_strokes(&self) { pub fn clear_strokes(&self) {
self.imp().strokes.borrow_mut().clear(); let changed = {
let mut strokes = self.imp().strokes.borrow_mut();
let changed = !strokes.is_empty();
strokes.clear();
changed
};
self.queue_draw(); self.queue_draw();
if changed {
self.copy_annotated_to_clipboard();
}
} }
pub fn has_strokes(&self) -> bool { pub fn has_strokes(&self) -> bool {
@ -506,4 +517,22 @@ impl LpImage {
renderer.unrealize(); renderer.unrealize();
Some(texture) Some(texture)
} }
/// Copy the current image and its annotations after a completed edit.
pub fn copy_annotated_to_clipboard(&self) {
if !crate::application::LpApplication::default()
.settings()
.boolean("auto-copy-annotations")
{
return;
}
let Some(texture) = self.render_annotated_texture() else {
tracing::error!("Failed to render annotated image for clipboard");
return;
};
let content = gdk::ContentProvider::for_value(&texture.to_value());
if let Err(err) = self.clipboard().set_content(Some(&content)) {
tracing::error!("Failed to copy annotated image: {err}");
}
}
} }

View file

@ -95,10 +95,16 @@ impl imp::LpImage {
} }
} }
} }
if let Some(stroke) = cur.and_then(|s| s.into_stroke()) { let changed = if let Some(stroke) = cur.and_then(|s| s.into_stroke()) {
imp.strokes.borrow_mut().push(stroke); imp.strokes.borrow_mut().push(stroke);
} true
} else {
false
};
obj.queue_draw(); obj.queue_draw();
if changed {
obj.copy_annotated_to_clipboard();
}
} }
)); ));
} }

View file

@ -133,6 +133,8 @@ mod imp {
pub(super) draw_line_width_btn: TemplateChild<gtk::SpinButton>, pub(super) draw_line_width_btn: TemplateChild<gtk::SpinButton>,
#[template_child] #[template_child]
pub(super) draw_clear_btn: TemplateChild<gtk::Button>, pub(super) draw_clear_btn: TemplateChild<gtk::Button>,
#[template_child]
pub(super) draw_auto_copy_btn: TemplateChild<gtk::ToggleButton>,
} }
#[glib::object_subclass] #[glib::object_subclass]
@ -475,6 +477,18 @@ mod imp {
imp.draw_color_btn imp.draw_color_btn
.set_rgba(&gdk::RGBA::new(0.96, 0.22, 0.22, 1.0)); .set_rgba(&gdk::RGBA::new(0.96, 0.22, 0.22, 1.0));
// Persist automatic clipboard updates as an app preference.
let settings = LpApplication::default().settings();
imp.draw_auto_copy_btn
.set_active(settings.boolean("auto-copy-annotations"));
imp.draw_auto_copy_btn.connect_toggled(move |btn| {
if let Err(err) =
settings.set_boolean("auto-copy-annotations", btn.is_active())
{
tracing::warn!("Failed to save auto-copy setting: {err}");
}
});
// Connect draw mode button (headerbar toggle) // Connect draw mode button (headerbar toggle)
imp.draw_mode_button.connect_toggled(glib::clone!( imp.draw_mode_button.connect_toggled(glib::clone!(
#[weak] #[weak]

View file

@ -265,6 +265,16 @@
</style> </style>
</object> </object>
</child> </child>
<!-- Copy each completed annotation change to the clipboard -->
<child>
<object class="GtkToggleButton" id="draw_auto_copy_btn">
<property name="label" translatable="yes">Auto-copy</property>
<property name="tooltip-text" translatable="yes">Copy to Clipboard After Each Change</property>
<style>
<class name="flat" />
</style>
</object>
</child>
<!-- Spacer --> <!-- Spacer -->
<child> <child>
<object class="GtkBox"> <object class="GtkBox">