use crate::screenshot::action::{ActionContext, ActionResult, ScreenAction};
pub struct CopyAction;
impl CopyAction {
pub fn new() -> Self {
Self
}
}
impl Default for CopyAction {
fn default() -> Self {
Self::new()
}
}
impl ScreenAction for CopyAction {
fn id(&self) -> &str {
"copy"
}
fn name(&self) -> &str {
"Copy"
}
fn icon_id(&self) -> Option<&str> {
Some("copy")
}
fn on_click(&mut self, ctx: &ActionContext) -> ActionResult {
let region = match ctx.get_composited_region() {
Some(r) => r,
None => return ActionResult::Failure("No region selected".to_string()),
};
let mut png_bytes = Vec::new();
{
use image::ImageEncoder;
let encoder = image::codecs::png::PngEncoder::new(&mut png_bytes);
if let Err(e) = encoder.write_image(
region.as_raw(),
region.width(),
region.height(),
image::ExtendedColorType::Rgba8,
) {
return ActionResult::Failure(format!("Failed to encode image: {}", e));
}
}
match crate::clipboard::set_image(&png_bytes) {
Ok(()) => ActionResult::Exit,
Err(e) => ActionResult::Failure(format!("Failed to copy to clipboard: {}", e)),
}
}
}