Add image.rs viewer

This commit is contained in:
2026-03-19 00:32:01 +08:00
parent 2aab6ee09f
commit af58582274

62
src/viewer/image.rs Normal file
View File

@@ -0,0 +1,62 @@
//! 圖片檢視器
use anyhow::Result;
use std::path::Path;
#[derive(Debug, Clone)]
pub struct ImageViewer {
path: Option<String>,
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
)
}
}