mirror of
https://github.com/itsjunetime/tdf.git
synced 2026-06-02 08:01:47 -04:00
Compare commits
5 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| e2a9c90b34 | |||
| 16c0aed9a3 | |||
| 19030f7fd4 | |||
| d5d62c81a3 | |||
| 7b9e1462da |
@@ -5,10 +5,7 @@ pkgs.mkShell {
|
|||||||
nativeBuildInputs = [ pkgs.pkg-config ];
|
nativeBuildInputs = [ pkgs.pkg-config ];
|
||||||
|
|
||||||
buildInputs = [
|
buildInputs = [
|
||||||
pkgs.cargo
|
|
||||||
pkgs.rustc
|
|
||||||
pkgs.rustPlatform.bindgenHook
|
pkgs.rustPlatform.bindgenHook
|
||||||
pkgs.cairo
|
pkgs.cairo
|
||||||
pkgs.rust-analyzer
|
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
|
|||||||
+29
-13
@@ -39,7 +39,7 @@ use tdf::{
|
|||||||
PrerenderLimit,
|
PrerenderLimit,
|
||||||
converter::{ConvertedPage, ConverterMsg, run_conversion_loop},
|
converter::{ConvertedPage, ConverterMsg, run_conversion_loop},
|
||||||
kitty::{KittyDisplay, display_kitty_images, do_shms_work, run_action},
|
kitty::{KittyDisplay, display_kitty_images, do_shms_work, run_action},
|
||||||
renderer::{self, RenderError, RenderInfo, RenderNotif},
|
renderer::{self, MUPDF_BLACK, MUPDF_WHITE, RenderError, RenderInfo, RenderNotif},
|
||||||
tui::{BottomMessage, InputAction, MessageSetting, Tui}
|
tui::{BottomMessage, InputAction, MessageSetting, Tui}
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -128,21 +128,37 @@ async fn inner_main() -> Result<(), WrappedErr> {
|
|||||||
.canonicalize()
|
.canonicalize()
|
||||||
.map_err(|e| WrappedErr(format!("Cannot canonicalize provided file: {e}").into()))?;
|
.map_err(|e| WrappedErr(format!("Cannot canonicalize provided file: {e}").into()))?;
|
||||||
|
|
||||||
let black =
|
let black = flags
|
||||||
parse_color_to_i32(flags.black_color.as_deref().unwrap_or("000000")).map_err(|e| {
|
.black_color
|
||||||
WrappedErr(
|
.as_deref()
|
||||||
format!("Couldn't parse black color: {e} - is it formatted like a CSS color?")
|
.map(|color| {
|
||||||
|
parse_color_to_i32(color).map_err(|e| {
|
||||||
|
WrappedErr(
|
||||||
|
format!(
|
||||||
|
"Couldn't parse black color {color:?}: {e} - is it formatted like a CSS color?"
|
||||||
|
)
|
||||||
.into()
|
.into()
|
||||||
)
|
)
|
||||||
})?;
|
})
|
||||||
|
})
|
||||||
|
.transpose()?
|
||||||
|
.unwrap_or(MUPDF_BLACK);
|
||||||
|
|
||||||
let white =
|
let white = flags
|
||||||
parse_color_to_i32(flags.white_color.as_deref().unwrap_or("FFFFFF")).map_err(|e| {
|
.white_color
|
||||||
WrappedErr(
|
.as_deref()
|
||||||
format!("Couldn't parse white color: {e} - is it formatted like a CSS color?")
|
.map(|color| {
|
||||||
|
parse_color_to_i32(color).map_err(|e| {
|
||||||
|
WrappedErr(
|
||||||
|
format!(
|
||||||
|
"Couldn't parse white color {color:?}: {e} - is it formatted like a CSS color?"
|
||||||
|
)
|
||||||
.into()
|
.into()
|
||||||
)
|
)
|
||||||
})?;
|
})
|
||||||
|
})
|
||||||
|
.transpose()?
|
||||||
|
.unwrap_or(MUPDF_WHITE);
|
||||||
|
|
||||||
// need to keep it around throughout the lifetime of the program, but don't rly need to use it.
|
// need to keep it around throughout the lifetime of the program, but don't rly need to use it.
|
||||||
// Just need to make sure it doesn't get dropped yet.
|
// Just need to make sure it doesn't get dropped yet.
|
||||||
|
|||||||
+16
-5
@@ -57,8 +57,8 @@ struct PrevRender {
|
|||||||
num_search_found: Option<usize>
|
num_search_found: Option<usize>
|
||||||
}
|
}
|
||||||
|
|
||||||
const MUPDF_BLACK: i32 = 0;
|
pub const MUPDF_BLACK: i32 = 0;
|
||||||
const MUPDF_WHITE: i32 = i32::from_be_bytes([0, 0xff, 0xff, 0xff]);
|
pub const MUPDF_WHITE: i32 = i32::from_be_bytes([0, 0xff, 0xff, 0xff]);
|
||||||
|
|
||||||
#[inline]
|
#[inline]
|
||||||
pub fn fill_default<T: Default>(vec: &mut Vec<T>, size: usize) {
|
pub fn fill_default<T: Default>(vec: &mut Vec<T>, size: usize) {
|
||||||
@@ -152,8 +152,8 @@ pub fn start_rendering(
|
|||||||
|
|
||||||
sender.send(Ok(RenderInfo::NumPages(n_pages.get())))?;
|
sender.send(Ok(RenderInfo::NumPages(n_pages.get())))?;
|
||||||
|
|
||||||
// We're using this vec of bools to indicate which page numbers have already been rendered,
|
// We're using this vec to indicate which page numbers have already been rendered, to
|
||||||
// to support people jumping to specific pages and having quick rendering results. We
|
// support people jumping to specific pages and having quick rendering results. We
|
||||||
// `split_at_mut` at 0 initially (which bascially makes `right == rendered && left == []`),
|
// `split_at_mut` at 0 initially (which bascially makes `right == rendered && left == []`),
|
||||||
// doing basically nothing, but if we get a notification that something has been jumped to,
|
// doing basically nothing, but if we get a notification that something has been jumped to,
|
||||||
// then we can split at that page and render at both sides of it
|
// then we can split at that page and render at both sides of it
|
||||||
@@ -277,7 +277,7 @@ pub fn start_rendering(
|
|||||||
// we only want to continue if one of the following is met:
|
// we only want to continue if one of the following is met:
|
||||||
// 1. It failed to render last time (we want to retry)
|
// 1. It failed to render last time (we want to retry)
|
||||||
// 2. The `contained_term` is set to Unknown, meaning that we need to at least
|
// 2. The `contained_term` is set to Unknown, meaning that we need to at least
|
||||||
// check if it contains the current term to see if it needs a re-render
|
// check if it contains the current term to see if it needs a re-render
|
||||||
if rendered.successful && rendered.num_search_found.is_some() {
|
if rendered.successful && rendered.num_search_found.is_some() {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
@@ -570,4 +570,15 @@ impl Iterator for PopOnNext<'_> {
|
|||||||
fn next(&mut self) -> Option<Self::Item> {
|
fn next(&mut self) -> Option<Self::Item> {
|
||||||
self.inner.pop_front()
|
self.inner.pop_front()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn size_hint(&self) -> (usize, Option<usize>) {
|
||||||
|
let l = self.len();
|
||||||
|
(l, Some(l))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl ExactSizeIterator for PopOnNext<'_> {
|
||||||
|
fn len(&self) -> usize {
|
||||||
|
self.inner.len()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+97
-41
@@ -102,6 +102,64 @@ impl Zoom {
|
|||||||
ZOOM_RATE_GRANULAR.powi(self.level.into())
|
ZOOM_RATE_GRANULAR.powi(self.level.into())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn step_in(&mut self) {
|
||||||
|
self.level = self.level.saturating_add(1);
|
||||||
|
}
|
||||||
|
fn step_out(&mut self) {
|
||||||
|
self.level = self.level.saturating_sub(1);
|
||||||
|
}
|
||||||
|
|
||||||
|
// TODO: Make this configurable, maybe allow fractional steps?
|
||||||
|
// With fractional steps, it might also be a good idea to have these
|
||||||
|
// have the same ratio as the font aspect ratio.
|
||||||
|
const PAN_STEP_X: i16 = 2;
|
||||||
|
const PAN_STEP_Y: i16 = 1;
|
||||||
|
|
||||||
|
fn pan(&mut self, direction: Direction) {
|
||||||
|
let (target, sign) = match direction {
|
||||||
|
Direction::Up => (&mut self.cell_pan_from_top, -1),
|
||||||
|
Direction::Down => (&mut self.cell_pan_from_top, 1),
|
||||||
|
Direction::Left => (&mut self.cell_pan_from_left, -1),
|
||||||
|
Direction::Right => (&mut self.cell_pan_from_left, 1)
|
||||||
|
};
|
||||||
|
let step = if direction.is_vertical() {
|
||||||
|
Self::PAN_STEP_Y
|
||||||
|
} else {
|
||||||
|
Self::PAN_STEP_X
|
||||||
|
};
|
||||||
|
*target = target.saturating_add_signed(sign * step);
|
||||||
|
}
|
||||||
|
fn pan_bottom(&mut self) {
|
||||||
|
self.cell_pan_from_top = 0;
|
||||||
|
}
|
||||||
|
fn pan_top(&mut self) {
|
||||||
|
self.cell_pan_from_top = u16::MAX;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
#[derive(Clone, Copy, Debug)]
|
||||||
|
enum Direction {
|
||||||
|
Up,
|
||||||
|
Down,
|
||||||
|
Left,
|
||||||
|
Right
|
||||||
|
}
|
||||||
|
impl Direction {
|
||||||
|
/// Flips the directions for vertical and horizonal panning.
|
||||||
|
fn flip_mouse_xy(self) -> Self {
|
||||||
|
match self {
|
||||||
|
Self::Up => Self::Left,
|
||||||
|
Self::Left => Self::Up,
|
||||||
|
Self::Down => Self::Right,
|
||||||
|
Self::Right => Self::Down
|
||||||
|
}
|
||||||
|
}
|
||||||
|
fn is_vertical(self) -> bool {
|
||||||
|
match self {
|
||||||
|
Self::Up | Self::Down => true,
|
||||||
|
Self::Left | Self::Right => false
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// This seems like a kinda weird struct because it holds two optionals but any representation
|
// This seems like a kinda weird struct because it holds two optionals but any representation
|
||||||
@@ -637,6 +695,8 @@ impl Tui {
|
|||||||
InputAction::JumpingToPage(new_page)
|
InputAction::JumpingToPage(new_page)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
let can_zoom = self.is_kitty && self.zoom.is_some();
|
||||||
|
|
||||||
match ev {
|
match ev {
|
||||||
Event::Key(key) => {
|
Event::Key(key) => {
|
||||||
match key.code {
|
match key.code {
|
||||||
@@ -653,7 +713,7 @@ impl Tui {
|
|||||||
self.bottom_msg
|
self.bottom_msg
|
||||||
{
|
{
|
||||||
if c == 'g' && self.is_kitty {
|
if c == 'g' && self.is_kitty {
|
||||||
self.update_zoom(|z| z.cell_pan_from_top = 0);
|
self.update_zoom(Zoom::pan_bottom);
|
||||||
self.set_msg(MessageSetting::Pop);
|
self.set_msg(MessageSetting::Pop);
|
||||||
return Some(InputAction::Redraw);
|
return Some(InputAction::Redraw);
|
||||||
}
|
}
|
||||||
@@ -762,24 +822,13 @@ impl Tui {
|
|||||||
self.last_render.rect = Rect::default();
|
self.last_render.rect = Rect::default();
|
||||||
Some(InputAction::SwitchRenderZoom(f_or_f))
|
Some(InputAction::SwitchRenderZoom(f_or_f))
|
||||||
}
|
}
|
||||||
'o' if self.is_kitty =>
|
'o' if can_zoom => self.update_zoom(Zoom::step_in),
|
||||||
self.update_zoom(|z| z.level = z.level.saturating_add(1)),
|
'O' if can_zoom => self.update_zoom(Zoom::step_out),
|
||||||
'O' if self.is_kitty =>
|
'L' if can_zoom => self.update_zoom(|z| z.pan(Direction::Right)),
|
||||||
self.update_zoom(|z| z.level = z.level.saturating_sub(1)),
|
'H' if can_zoom => self.update_zoom(|z| z.pan(Direction::Left)),
|
||||||
'L' if self.is_kitty => self.update_zoom(|z| {
|
'J' if can_zoom => self.update_zoom(|z| z.pan(Direction::Down)),
|
||||||
z.cell_pan_from_left = z.cell_pan_from_left.saturating_add(1);
|
'K' if can_zoom => self.update_zoom(|z| z.pan(Direction::Up)),
|
||||||
}),
|
'G' if can_zoom => self.update_zoom(Zoom::pan_top),
|
||||||
'H' if self.is_kitty => self.update_zoom(|z| {
|
|
||||||
z.cell_pan_from_left = z.cell_pan_from_left.saturating_sub(1);
|
|
||||||
}),
|
|
||||||
'J' if self.is_kitty => self.update_zoom(|z| {
|
|
||||||
z.cell_pan_from_top = z.cell_pan_from_top.saturating_add(1);
|
|
||||||
}),
|
|
||||||
'K' if self.is_kitty => self.update_zoom(|z| {
|
|
||||||
z.cell_pan_from_top = z.cell_pan_from_top.saturating_sub(1);
|
|
||||||
}),
|
|
||||||
'G' if self.is_kitty =>
|
|
||||||
self.update_zoom(|z| z.cell_pan_from_top = u16::MAX),
|
|
||||||
_ => None
|
_ => None
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -866,29 +915,36 @@ impl Tui {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
Event::Mouse(mouse) => {
|
Event::Mouse(mouse) => {
|
||||||
if mouse.modifiers.contains(KeyModifiers::CONTROL)
|
let mut handle_scroll = |mut direction: Direction| {
|
||||||
&& self.is_kitty
|
if can_zoom {
|
||||||
&& self.zoom.is_some()
|
if mouse.modifiers.contains(KeyModifiers::CONTROL) {
|
||||||
{
|
match direction {
|
||||||
match mouse.kind {
|
Direction::Up => self.update_zoom(Zoom::step_in),
|
||||||
MouseEventKind::ScrollUp =>
|
Direction::Down => self.update_zoom(Zoom::step_out),
|
||||||
self.update_zoom(|z| z.level = z.level.saturating_add(1).min(0)),
|
_ => None
|
||||||
MouseEventKind::ScrollDown =>
|
}
|
||||||
self.update_zoom(|z| z.level = z.level.saturating_sub(1)),
|
} else {
|
||||||
_ => None
|
if mouse.modifiers.contains(KeyModifiers::SHIFT) {
|
||||||
}
|
direction = direction.flip_mouse_xy();
|
||||||
} else {
|
}
|
||||||
match mouse.kind {
|
self.update_zoom(|z| z.pan(direction))
|
||||||
MouseEventKind::ScrollRight =>
|
}
|
||||||
self.change_page(PageChange::Next, ChangeAmount::Single),
|
} else {
|
||||||
MouseEventKind::ScrollDown =>
|
let (change, amount) = match direction {
|
||||||
self.change_page(PageChange::Next, ChangeAmount::WholeScreen),
|
Direction::Right => (PageChange::Next, ChangeAmount::Single),
|
||||||
MouseEventKind::ScrollLeft =>
|
Direction::Down => (PageChange::Next, ChangeAmount::WholeScreen),
|
||||||
self.change_page(PageChange::Prev, ChangeAmount::Single),
|
Direction::Left => (PageChange::Prev, ChangeAmount::Single),
|
||||||
MouseEventKind::ScrollUp =>
|
Direction::Up => (PageChange::Prev, ChangeAmount::WholeScreen)
|
||||||
self.change_page(PageChange::Prev, ChangeAmount::WholeScreen),
|
};
|
||||||
_ => None
|
self.change_page(change, amount)
|
||||||
}
|
}
|
||||||
|
};
|
||||||
|
match mouse.kind {
|
||||||
|
MouseEventKind::ScrollRight => handle_scroll(Direction::Right),
|
||||||
|
MouseEventKind::ScrollDown => handle_scroll(Direction::Down),
|
||||||
|
MouseEventKind::ScrollLeft => handle_scroll(Direction::Left),
|
||||||
|
MouseEventKind::ScrollUp => handle_scroll(Direction::Up),
|
||||||
|
_ => None
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Event::Resize(_, _) => Some(InputAction::Redraw),
|
Event::Resize(_, _) => Some(InputAction::Redraw),
|
||||||
|
|||||||
Reference in New Issue
Block a user