From af585822746dd9eed3ec48df23dbb0dc74713f4c Mon Sep 17 00:00:00 2001 From: Warren Lo Date: Thu, 19 Mar 2026 00:32:01 +0800 Subject: [PATCH] Add image.rs viewer --- src/viewer/image.rs | 62 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 62 insertions(+) create mode 100644 src/viewer/image.rs diff --git a/src/viewer/image.rs b/src/viewer/image.rs new file mode 100644 index 0000000..d85b8ce --- /dev/null +++ b/src/viewer/image.rs @@ -0,0 +1,62 @@ +//! 圖片檢視器 + +use anyhow::Result; +use std::path::Path; + +#[derive(Debug, Clone)] +pub struct ImageViewer { + path: Option, + zoom: f32, + pan_x: i32, + pan_y: i32, +} + +impl ImageViewer { + pub fn new() -> Self { + Self { + path: None, + zoom: 1.0, + pan_x: 0, + pan_y: 0, + } + } + + pub fn open(&mut self, path: &str) -> Result<()> { + if !Path::new(path).exists() { + anyhow::bail!("File not found: {}", path); + } + self.path = Some(path.to_string()); + self.reset_view(); + Ok(()) + } + + pub fn zoom_in(&mut self) { + self.zoom = (self.zoom * 1.25).min(10.0); + } + + pub fn zoom_out(&mut self) { + self.zoom = (self.zoom / 1.25).max(0.1); + } + + pub fn pan(&mut self, dx: i32, dy: i32) { + self.pan_x += dx; + self.pan_y += dy; + } + + pub fn reset_view(&mut self) { + self.zoom = 1.0; + self.pan_x = 0; + self.pan_y = 0; + } + + pub fn get_path(&self) -> Option<&str> { + self.path.as_deref() + } + + pub fn get_transform(&self) -> String { + format!( + "scale({}) translate({}px, {}px)", + self.zoom, self.pan_x, self.pan_y + ) + } +}