refactor(viewer): rename app to "mm" and improve navigation features
Some checks failed
build / checks-matrix (push) Successful in 19m24s
build / codecov (push) Failing after 19m34s
docs / docs (push) Has been cancelled
build / checks-build (push) Has been cancelled

This commit is contained in:
uttarayan21
2025-10-08 00:11:38 +05:30
parent 4608309c7e
commit e205c60187
4 changed files with 86 additions and 46 deletions

View File

@@ -23,6 +23,7 @@ pub fn main() -> Result<()> {
fn walker(input: impl AsRef<Path>) -> Vec<PathBuf> {
let mut tb = ignore::types::TypesBuilder::new();
tb.add("image", "*.jpg").expect("Failed to add image type");
tb.add("image", "*.png").expect("Failed to add image type");
ignore::WalkBuilder::new(input)
.types(
tb.select("image")

View File

@@ -1,5 +1,5 @@
use gpui::{
App, Application, Bounds, Context, KeyBinding, SharedString, Window, WindowBounds,
App, Application, Bounds, Context, FocusHandle, KeyBinding, SharedString, Window, WindowBounds,
WindowOptions, actions, div, img, prelude::*, px, rgb, rgba, size,
};
use nalgebra::Vector2;
@@ -11,17 +11,23 @@ struct MMViewer {
current: usize,
zoom: f32,
pan: Vector2<f32>,
focus: FocusHandle,
}
actions!(mm, [Quit, NextImage, PrevImage]);
actions!(mm, [Quit, NextImage, PrevImage, FirstImage, LastImage]);
impl Render for MMViewer {
fn render(&mut self, _window: &mut Window, cx: &mut Context<Self>) -> impl IntoElement {
div()
.size_full()
.flex()
.key_context("MMViewer")
.track_focus(&self.focus_handle(cx))
.on_action(cx.listener(Self::next_image))
.on_action(cx.listener(Self::prev_image))
.on_action(cx.listener(Self::first_image))
.on_action(cx.listener(Self::last_image))
.on_action(cx.listener(Self::quit))
.flex()
.size_full()
.flex_col()
.justify_center()
.bg(rgb(0x505050))
@@ -43,48 +49,79 @@ impl Render for MMViewer {
}
impl MMViewer {
fn focus_handle(&self, _: &App) -> FocusHandle {
self.focus.clone()
}
fn next_image(&mut self, _: &NextImage, _: &mut Window, cx: &mut Context<Self>) {
dbg!("aasdfasdf");
if self.current + 1 < self.files.len() {
self.current += 1;
cx.notify();
}
}
fn last_image(&mut self, _: &LastImage, _: &mut Window, cx: &mut Context<Self>) {
if !self.files.is_empty() {
self.current = self.files.len() - 1;
cx.notify();
}
}
fn prev_image(&mut self, _: &PrevImage, _: &mut Window, cx: &mut Context<Self>) {
dbg!("aasdfascawsdfasdfdf");
if self.current > 0 {
self.current -= 1;
cx.notify();
}
}
fn first_image(&mut self, _: &FirstImage, _: &mut Window, cx: &mut Context<Self>) {
if !self.files.is_empty() {
self.current = 0;
cx.notify();
}
}
fn quit(&mut self, _: &Quit, _: &mut Window, cx: &mut Context<Self>) {
cx.quit();
}
}
pub fn run(files: Vec<PathBuf>) {
Application::new().run(|cx: &mut App| {
let bounds = Bounds::centered(None, size(px(800f32), px(600f32)), cx);
cx.on_action(|_: &Quit, cx| cx.quit());
cx.bind_keys([
KeyBinding::new("q", Quit, None),
KeyBinding::new("Escape", Quit, None),
KeyBinding::new("j", NextImage, None),
KeyBinding::new("right", NextImage, None),
KeyBinding::new("k", PrevImage, None),
KeyBinding::new("left", PrevImage, None),
KeyBinding::new("shift-g", LastImage, None),
KeyBinding::new("g", FirstImage, None),
]);
cx.open_window(
WindowOptions {
window_bounds: Some(WindowBounds::Windowed(bounds)),
// bounds: WindowBounds::Fixed(bounds),
..Default::default()
},
|_, cx| {
cx.new(|_| MMViewer {
files,
current: 0,
zoom: 1.0,
pan: Vector2::new(0.0, 0.0),
})
},
)
.expect("Failed to open window");
cx.activate(true);
let window = cx
.open_window(
WindowOptions {
window_bounds: Some(WindowBounds::Windowed(bounds)),
titlebar: None,
..Default::default()
},
|_, cx| {
cx.new(|cx| MMViewer {
files,
current: 0,
zoom: 1.0,
pan: Vector2::new(0.0, 0.0),
focus: cx.focus_handle(),
})
},
)
.expect("Failed to open window");
window
.update(cx, |view, window, cx| {
window.focus(&view.focus_handle(cx));
cx.activate(true);
})
.expect("Failed to focus");
});
}