Skip to content

Commit

Permalink
fix(config)
Browse files Browse the repository at this point in the history
  • Loading branch information
rsuu committed Jul 28, 2024
1 parent 7545280 commit a56c1e9
Show file tree
Hide file tree
Showing 8 changed files with 63 additions and 64 deletions.
2 changes: 1 addition & 1 deletion assets/gestures.zip
Git LFS file not shown
3 changes: 1 addition & 2 deletions src/app/canvas.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down
10 changes: 4 additions & 6 deletions src/app/gesture.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,16 @@ pub struct Gesture {
}

impl Gesture {
pub fn new(zip_path: &str) -> eyre::Result<Self> {
pub fn load(zip_path: &str) -> eyre::Result<Self> {
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`"));
}
Expand Down
11 changes: 6 additions & 5 deletions src/app/web.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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()
Expand Down
56 changes: 23 additions & 33 deletions src/app/window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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 = {
Expand All @@ -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");
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -271,31 +263,29 @@ impl App {
sf: f64,
PhysicalPosition { x, y }: PhysicalPosition<f64>,
) -> 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(())
Expand Down Expand Up @@ -350,7 +340,7 @@ impl App {
ref max_zoom,
ref min_zoom,
..
} => 's: {
} => {
*mouse_pos = self.event_info.mouse_pos;
*flag_scroll = true;

Expand Down
27 changes: 16 additions & 11 deletions src/archive.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Self> {
if path.is_dir() {
return Ok(Self::Dir {
Expand All @@ -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)?,
Expand All @@ -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<Vec<Page>> {
Ok(match &self {
Self::Archive { filelist, .. } | Self::Dir { filelist, .. } => {
Expand Down
16 changes: 11 additions & 5 deletions src/data/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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);

Expand All @@ -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();
Expand Down Expand Up @@ -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(())
}
Expand Down
2 changes: 1 addition & 1 deletion src/frame.rs
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ impl Frame {

pub fn resize(blob: &[u8], dst_size: Size, algo: ResizeAlg) -> eyre::Result<Self> {
use imagesize::{blob_size, image_type, ImageType};

let ty = image_type(blob)?;
let size = blob_size(blob)?;

Expand Down

0 comments on commit a56c1e9

Please sign in to comment.