From a56c1e9bfd48f3546f041b191d753258d1833af3 Mon Sep 17 00:00:00 2001 From: RSUU Date: Sun, 28 Jul 2024 15:54:36 +0800 Subject: [PATCH] fix(config) --- assets/gestures.zip | 2 +- src/app/canvas.rs | 3 +-- src/app/gesture.rs | 10 ++++---- src/app/web.rs | 11 +++++---- src/app/window.rs | 56 +++++++++++++++++++-------------------------- src/archive.rs | 27 +++++++++++++--------- src/data/config.rs | 16 +++++++++---- src/frame.rs | 2 +- 8 files changed, 63 insertions(+), 64 deletions(-) diff --git a/assets/gestures.zip b/assets/gestures.zip index acd7c7c..408f327 100644 --- a/assets/gestures.zip +++ b/assets/gestures.zip @@ -1,3 +1,3 @@ version https://git-lfs.github.com/spec/v1 -oid sha256:8da71bbdb6b5500488d11bd31d582f03519680d14909d7b9b82cf8e3e60f7b4c +oid sha256:bb28315c2a79a1d115f4fcfe2502f242b6957367c491b1982050d3c6c8524d4d size 1280 diff --git a/src/app/canvas.rs b/src/app/canvas.rs index 094e445..23d9e94 100644 --- a/src/app/canvas.rs +++ b/src/app/canvas.rs @@ -426,7 +426,7 @@ impl App { // REFS: http://phrogz.net/tmp/canvas_zoom_to_cursor.html if *flag_scroll { - // TODO: how align + // TODO: align let algo = fir::ResizeAlg::Nearest; let scale = 1.1_f32; @@ -436,7 +436,6 @@ impl App { let frame = Frame::resize(page.tmp_blob.as_slice(), dst_size, algo)?; page.frame = frame; page.dst_size = dst_size; - page.zoom_at(*mouse_pos, factor); *flag_scroll = false; diff --git a/src/app/gesture.rs b/src/app/gesture.rs index 595c4d7..1024309 100644 --- a/src/app/gesture.rs +++ b/src/app/gesture.rs @@ -15,18 +15,16 @@ pub struct Gesture { } impl Gesture { - pub fn new(zip_path: &str) -> eyre::Result { + pub fn load(zip_path: &str) -> eyre::Result { let mut temps = Vec::new(); let file = { if let Ok(file) = File::open(zip_path) { file + } else if let Some(mut p) = dirs_next::data_dir() { + p.push("rmg/gestures.zip"); - // $XDG_DATA_HOME/rmg/gestures.zip - } else if let Some(mut default_dir) = dirs_next::data_dir() { - default_dir.push("rmg/gestures.zip"); - - File::open(default_dir)? + File::open(p)? } else { return Err(eyre::eyre!("Unknown to read `gestures.zip`")); } diff --git a/src/app/web.rs b/src/app/web.rs index 340f72e..11b84eb 100644 --- a/src/app/web.rs +++ b/src/app/web.rs @@ -18,7 +18,7 @@ use std::{ use winit::{ dpi::{LogicalSize, PhysicalPosition}, event::{Event, KeyEvent, MouseButton, MouseScrollDelta, WindowEvent}, - event_loop::{ControlFlow, EventLoop, ActiveEventLoop}, + event_loop::{ActiveEventLoop, ControlFlow, EventLoop}, keyboard::{KeyCode, PhysicalKey}, monitor::{MonitorHandle, VideoMode}, window::Window, @@ -48,11 +48,12 @@ pub async fn main() { let event_loop = EventLoop::new().unwrap(); let size = LogicalSize::new(100.0, 100.0); - let attrs = Window::default_attributes().with_title("rmg") - .with_inner_size(size); - let window = event_loop.create_window(attrs).unwrap(); + let attrs = Window::default_attributes() + .with_title("rmg") + .with_inner_size(size); + let window = event_loop.create_window(attrs).unwrap(); let window = Rc::new(window); - + web_dom .body() .unwrap() diff --git a/src/app/window.rs b/src/app/window.rs index b29c633..6dee2a9 100644 --- a/src/app/window.rs +++ b/src/app/window.rs @@ -13,7 +13,7 @@ use std::{ use winit::{ dpi::{LogicalSize, PhysicalPosition}, event::{Event, KeyEvent, MouseButton, MouseScrollDelta, WindowEvent}, - event_loop::{self, ControlFlow, EventLoop, ActiveEventLoop}, + event_loop::{self, ActiveEventLoop, ControlFlow, EventLoop}, keyboard::{KeyCode, PhysicalKey}, monitor::{MonitorHandle, VideoMode}, window::Window, @@ -78,7 +78,7 @@ impl App { } fn new(config: Config) -> eyre::Result<(Self, EventLoop<()>)> { - let gestures = Gesture::new(config.gestures.data_path.as_str())?; + let gestures = Gesture::load(config.gestures.data_path.as_str())?; let (data, pool, elems); let canvas = { @@ -97,9 +97,9 @@ impl App { let window = { let size = canvas.size(); let size = LogicalSize::new(size.width(), size.height()); - let attrs = Window::default_attributes().with_title("rmg"); + let attrs = Window::default_attributes().with_title("rmg"); let window = event_loop.create_window(attrs)?; - + Rc::new(window) }; tracing::info!("Winit"); @@ -154,11 +154,7 @@ impl App { Ok(()) } - fn event_loop( - &mut self, - event: Event<()>, - elwt: &ActiveEventLoop, - ) -> eyre::Result<()> { + fn event_loop(&mut self, event: Event<()>, elwt: &ActiveEventLoop) -> eyre::Result<()> { let window = self.window(); match event { @@ -224,11 +220,7 @@ impl App { Ok(()) } - fn on_window_event( - &mut self, - elwt: &ActiveEventLoop, - e: WindowEvent, - ) -> eyre::Result<()> { + fn on_window_event(&mut self, elwt: &ActiveEventLoop, e: WindowEvent) -> eyre::Result<()> { // TODO: // [ ](window) full/mini/float screen // [ ](mouse) pick img @@ -271,31 +263,29 @@ impl App { sf: f64, PhysicalPosition { x, y }: PhysicalPosition, ) -> eyre::Result<()> { - if !self.env.flag_gesture { - return Ok(()); - } - let sf = sf as f32; let origin = Vec2::new(x as f32, y as f32).scale(sf, sf); self.event_info.mouse_pos = origin; - // dbg!(y, window.inner_size().height); - match self.action { - Action::Gesture { ref mut path, .. } => { - path.push(origin); - } + if self.env.flag_gesture { + // dbg!(y, window.inner_size().height); + match self.action { + Action::Gesture { ref mut path, .. } => { + path.push(origin); + } - Action::View => { - let red = RGBA8::new(255, 0, 0, 255); - self.action = Action::Gesture { - fill: red, - path: vec![], - stroke_width: 4.0, - }; - } + Action::View => { + let red = RGBA8::new(255, 0, 0, 255); + self.action = Action::Gesture { + fill: red, + path: vec![], + stroke_width: 4.0, + }; + } - _ => {} + _ => {} + } } Ok(()) @@ -350,7 +340,7 @@ impl App { ref max_zoom, ref min_zoom, .. - } => 's: { + } => { *mouse_pos = self.event_info.mouse_pos; *flag_scroll = true; diff --git a/src/archive.rs b/src/archive.rs index 25a2a8f..99110d5 100644 --- a/src/archive.rs +++ b/src/archive.rs @@ -79,14 +79,6 @@ impl FileList { } impl DataType { - pub fn file_nums(&self) -> usize { - match self { - DataType::Archive { filelist, .. } | DataType::Dir { filelist, .. } => filelist.len(), - DataType::SingleImg { .. } => 1, - _ => unreachable!(), - } - } - pub fn new(path: &Path) -> eyre::Result { if path.is_dir() { return Ok(Self::Dir { @@ -95,19 +87,20 @@ impl DataType { }); } - // BUG: remove space in the path + // FIXME: remove space in the path let Ok(ty) = infer::get_from_path(path) else { eyre::bail!("Unknown Type") }; - let Some(ext) = ty else { + let Some(t) = ty else { // Not archive. return Ok(Self::SingleImg { path: path.to_path_buf(), }); }; + let ext = t.extension(); - Ok(match ext.extension() { + Ok(match ext { "zip" => Self::Archive { fmt: ArchiveFmt::Zip, filelist: archive::zip::get_list(path)?, @@ -120,10 +113,22 @@ impl DataType { path: path.to_path_buf(), }, + _ if SUPPORTED_FORMAT.contains(&ext) => Self::SingleImg { + path: path.to_path_buf(), + }, + _ => Self::Unknown, }) } + pub fn file_nums(&self) -> usize { + match self { + DataType::Archive { filelist, .. } | DataType::Dir { filelist, .. } => filelist.len(), + DataType::SingleImg { .. } => 1, + _ => unreachable!(), + } + } + pub fn gen_empty_pages(&self, fname_padding: usize) -> eyre::Result> { Ok(match &self { Self::Archive { filelist, .. } | Self::Dir { filelist, .. } => { diff --git a/src/data/config.rs b/src/data/config.rs index f36aa80..6d38957 100644 --- a/src/data/config.rs +++ b/src/data/config.rs @@ -160,7 +160,7 @@ impl Config { pub fn update(&mut self) -> eyre::Result<()> { self.update_file()?; - // self.update_env()?; + self.update_env()?; self.update_cli()?; // dbg!(&self); @@ -175,8 +175,9 @@ impl Config { return Ok(()); }; - // e.g. ~/.config/rmg/config.rs - path.push("/rmg/config.rs"); + // e.g. $XDG_CONFIG_HOME/rmg/config.rs + // + path.push("rmg/config.rs"); // tracing::debug!("path: {:?}", path); @@ -195,7 +196,9 @@ impl Config { Ok(()) } - pub fn update_env(&mut self) {} + pub fn update_env(&mut self) -> eyre::Result<()> { + Ok(()) + } pub fn update_cli(&mut self) -> eyre::Result<()> { let mut args = Arguments::from_env(); @@ -250,7 +253,10 @@ impl Config { } // ConfApp - self.app.target = args.free_from_str().unwrap(); + self.app.target = args.free_from_str().unwrap_or_else(|_| { + println!("{}", gen_help()); + exit(0); + }); Ok(()) } diff --git a/src/frame.rs b/src/frame.rs index 0e09459..1ffc00d 100644 --- a/src/frame.rs +++ b/src/frame.rs @@ -169,7 +169,7 @@ impl Frame { pub fn resize(blob: &[u8], dst_size: Size, algo: ResizeAlg) -> eyre::Result { use imagesize::{blob_size, image_type, ImageType}; - + let ty = image_type(blob)?; let size = blob_size(blob)?;