mirror of
https://github.com/itsjunetime/tdf.git
synced 2026-06-01 23:51:46 -04:00
Compare commits
3 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 06f737b1fe | |||
| 95c24206ca | |||
| 5e4fbff9cd |
+52
-22
@@ -72,7 +72,7 @@ struct PageConstraints {
|
|||||||
r_to_l: bool
|
r_to_l: bool
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Default, Debug)]
|
#[derive(Default, Debug, Clone, Copy)]
|
||||||
struct Zoom {
|
struct Zoom {
|
||||||
// just how much 'zoom' you have. 0 means it fills the screen (instead of fits), such
|
// just how much 'zoom' you have. 0 means it fills the screen (instead of fits), such
|
||||||
// that one axis is fully on-screen
|
// that one axis is fully on-screen
|
||||||
@@ -86,7 +86,7 @@ struct Zoom {
|
|||||||
}
|
}
|
||||||
impl Zoom {
|
impl Zoom {
|
||||||
/// Returns the zoom factor, where 1 is the default and means fill-screen
|
/// Returns the zoom factor, where 1 is the default and means fill-screen
|
||||||
fn factor(&self) -> f32 {
|
fn factor(self) -> f32 {
|
||||||
// TODO: Make these configurable once we have a good way to set options after startup
|
// TODO: Make these configurable once we have a good way to set options after startup
|
||||||
const ZOOM_RATE: f32 = 1.1;
|
const ZOOM_RATE: f32 = 1.1;
|
||||||
const ZOOM_RATE_GRANULAR: f32 = 1.05;
|
const ZOOM_RATE_GRANULAR: f32 = 1.05;
|
||||||
@@ -262,7 +262,7 @@ impl Tui {
|
|||||||
let img_page_w_ratio = img_section_w / initial_page_w;
|
let img_page_w_ratio = img_section_w / initial_page_w;
|
||||||
let img_page_h_ratio = img_section_h / initial_page_h;
|
let img_page_h_ratio = img_section_h / initial_page_h;
|
||||||
|
|
||||||
let shrink_move_page = |dim: &mut u16, pos: &mut u16, axis_zoom_factor: f32| {
|
fn shrink_move_page(dim: &mut u16, pos: &mut u16, axis_zoom_factor: f32) {
|
||||||
let old_dim = *dim;
|
let old_dim = *dim;
|
||||||
// The axis zoom factor tells us what portion of the axis
|
// The axis zoom factor tells us what portion of the axis
|
||||||
// we need to show.
|
// we need to show.
|
||||||
@@ -272,26 +272,56 @@ impl Tui {
|
|||||||
.checked_sub(*dim)
|
.checked_sub(*dim)
|
||||||
.expect("zooming out should shrink the image")
|
.expect("zooming out should shrink the image")
|
||||||
/ 2;
|
/ 2;
|
||||||
};
|
|
||||||
|
|
||||||
// TODO: Detect max zoom-out in zoom levels
|
|
||||||
if img_page_w_ratio < img_page_h_ratio {
|
|
||||||
// vertical scroll / tall image. zooming out means decreasing the width of the page area
|
|
||||||
shrink_move_page(
|
|
||||||
&mut img_area.width,
|
|
||||||
&mut img_area.x,
|
|
||||||
// disallow zooming out past fit-screen
|
|
||||||
zoom_factor.max(1.0 / img_page_h_ratio)
|
|
||||||
);
|
|
||||||
} else {
|
|
||||||
// horizontal scroll / wide image. zooming out means decreasing the width of the page area
|
|
||||||
shrink_move_page(
|
|
||||||
&mut img_area.height,
|
|
||||||
&mut img_area.y,
|
|
||||||
// disallow zooming out past fit-screen
|
|
||||||
zoom_factor.max(1.0 / img_page_w_ratio)
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn shrink_move_two_pass(
|
||||||
|
zoom: &mut Zoom,
|
||||||
|
dim: &mut u16,
|
||||||
|
pos: &mut u16,
|
||||||
|
zoom_factor: f32,
|
||||||
|
axis_zoom_factor: f32
|
||||||
|
) {
|
||||||
|
// To detect if we're already at fit-screen, we perform a first
|
||||||
|
// pass on `img_area` that emulates the last call to
|
||||||
|
// `render_zoomed` by subtracting from the `zoom`'s level. This
|
||||||
|
// holds because the `img_area` that gets passed is always the
|
||||||
|
// same.
|
||||||
|
let mut first_pass_dim = *dim;
|
||||||
|
let mut first_pass_pos = *pos;
|
||||||
|
let mut first_pass_zoom = *zoom;
|
||||||
|
|
||||||
|
if first_pass_zoom.level.is_negative() {
|
||||||
|
first_pass_zoom.step_in();
|
||||||
|
}
|
||||||
|
let first_pass_zoom_factor = first_pass_zoom.factor();
|
||||||
|
shrink_move_page(
|
||||||
|
&mut first_pass_dim,
|
||||||
|
&mut first_pass_pos,
|
||||||
|
first_pass_zoom_factor.max(1.0 / axis_zoom_factor)
|
||||||
|
);
|
||||||
|
|
||||||
|
log::debug!("first_pass_dim: {first_pass_dim}, first_pass_pos: {first_pass_pos}");
|
||||||
|
// vertical scroll / tall image. zooming out means decreasing
|
||||||
|
// the width of the page area
|
||||||
|
// use `max` to disallow zooming out past fit-screen
|
||||||
|
shrink_move_page(dim, pos, zoom_factor.max(1.0 / axis_zoom_factor));
|
||||||
|
|
||||||
|
log::debug!("new dim: {dim}, new pos: {pos}");
|
||||||
|
// The moment the image area is left unmodified, we've hit
|
||||||
|
// fit-screen and the zoom level ought be normalized.
|
||||||
|
if first_pass_dim == *dim && first_pass_pos == *pos {
|
||||||
|
zoom.step_in();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
let (dim, pos, axis_zoom_factor) = if img_page_w_ratio < img_page_h_ratio {
|
||||||
|
(&mut img_area.width, &mut img_area.x, img_page_h_ratio)
|
||||||
|
} else {
|
||||||
|
// horizontal scroll / wide image. zooming out means decreasing
|
||||||
|
// the height of the page area
|
||||||
|
(&mut img_area.height, &mut img_area.y, img_page_w_ratio)
|
||||||
|
};
|
||||||
|
shrink_move_two_pass(zoom, dim, pos, zoom_factor, axis_zoom_factor);
|
||||||
}
|
}
|
||||||
log::debug!("after adjustment, page area is {img_area:#?}");
|
log::debug!("after adjustment, page area is {img_area:#?}");
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user