| 1 | use std::ops::Range; |
| 2 | |
| 3 | pub type Pair = (f32, f32); |
| 4 | |
| 5 | pub const MAX_ZOOM: f32 = 4.; |
| 6 | pub const ZOOM_STEP: f32 = 1.25; |
| 7 | const WHEEL_SENSITIVITY: f32 = 0.01; |
| 8 | const SCALE_EPSILON: f32 = 0.001; |
| 9 | |
| 10 | pub fn rotated_size(size: Pair, quarter_turns: u8) -> Pair { |
| 11 | if quarter_turns % 2 == 0 { |
| 12 | size |
| 13 | } else { |
| 14 | (size.1, size.0) |
| 15 | } |
| 16 | } |
| 17 | |
| 18 | pub fn next_rotation(quarter_turns: u8) -> u8 { |
| 19 | (quarter_turns + 1) % 4 |
| 20 | } |
| 21 | |
| 22 | pub fn fit_scale(image: Pair, stage: Pair) -> f32 { |
| 23 | if image.0 <= 0. || image.1 <= 0. || stage.0 <= 0. || stage.1 <= 0. { |
| 24 | return 1.; |
| 25 | } |
| 26 | (stage.0 / image.0).min(stage.1 / image.1).min(1.) |
| 27 | } |
| 28 | |
| 29 | pub fn clamp_scale(scale: f32, fit: f32) -> f32 { |
| 30 | scale.clamp(fit, MAX_ZOOM.max(fit)) |
| 31 | } |
| 32 | |
| 33 | pub fn display_size(image: Pair, scale: f32) -> Pair { |
| 34 | (image.0 * scale, image.1 * scale) |
| 35 | } |
| 36 | |
| 37 | pub fn clamp_pan(pan: Pair, display: Pair, stage: Pair) -> Pair { |
| 38 | let limit = |display: f32, stage: f32| ((display - stage) / 2.).max(0.); |
| 39 | let (limit_x, limit_y) = (limit(display.0, stage.0), limit(display.1, stage.1)); |
| 40 | ( |
| 41 | pan.0.clamp(-limit_x, limit_x), |
| 42 | pan.1.clamp(-limit_y, limit_y), |
| 43 | ) |
| 44 | } |
| 45 | |
| 46 | pub fn image_origin(display: Pair, stage: Pair, pan: Pair) -> Pair { |
| 47 | ( |
| 48 | (stage.0 - display.0) / 2. + pan.0, |
| 49 | (stage.1 - display.1) / 2. + pan.1, |
| 50 | ) |
| 51 | } |
| 52 | |
| 53 | #[derive(Debug, Clone, Copy, PartialEq)] |
| 54 | pub struct View { |
| 55 | pub scale: f32, |
| 56 | pub pan: Pair, |
| 57 | } |
| 58 | |
| 59 | pub fn zoom_at(view: View, wanted: f32, anchor: Pair, image: Pair, stage: Pair) -> View { |
| 60 | let fit = fit_scale(image, stage); |
| 61 | let scale = clamp_scale(wanted, fit); |
| 62 | let ratio = scale / view.scale; |
| 63 | let pan = ( |
| 64 | anchor.0 - (anchor.0 - view.pan.0) * ratio, |
| 65 | anchor.1 - (anchor.1 - view.pan.1) * ratio, |
| 66 | ); |
| 67 | View { |
| 68 | scale, |
| 69 | pan: clamp_pan(pan, display_size(image, scale), stage), |
| 70 | } |
| 71 | } |
| 72 | |
| 73 | pub fn wheel_scale(scale: f32, delta_y: f32) -> f32 { |
| 74 | scale * (delta_y * WHEEL_SENSITIVITY).exp() |
| 75 | } |
| 76 | |
| 77 | pub fn step_scale(scale: f32, direction: i32) -> f32 { |
| 78 | scale * ZOOM_STEP.powi(direction) |
| 79 | } |
| 80 | |
| 81 | pub fn toggle_target(scale: f32, fit: f32) -> f32 { |
| 82 | if scale > fit + SCALE_EPSILON { |
| 83 | fit |
| 84 | } else { |
| 85 | 1_f32.max(fit) |
| 86 | } |
| 87 | } |
| 88 | |
| 89 | pub fn percent_label(scale: f32) -> String { |
| 90 | format!("{}%", (scale * 100.).round() as u32) |
| 91 | } |
| 92 | |
| 93 | pub fn step_index(current: usize, len: usize, delta: isize) -> usize { |
| 94 | if len == 0 { |
| 95 | return 0; |
| 96 | } |
| 97 | current.saturating_add_signed(delta).min(len - 1) |
| 98 | } |
| 99 | |
| 100 | pub fn counter_label(index: usize, len: usize) -> String { |
| 101 | format!("{} of {}", index + 1, len) |
| 102 | } |
| 103 | |
| 104 | pub fn thumbnail_window(index: usize, len: usize, capacity: usize) -> Range<usize> { |
| 105 | let capacity = capacity.max(1).min(len); |
| 106 | let start = index |
| 107 | .saturating_sub(capacity / 2) |
| 108 | .min(len.saturating_sub(capacity)); |
| 109 | start..start + capacity |
| 110 | } |
| 111 | |
| 112 | #[cfg(test)] |
| 113 | mod tests { |
| 114 | use super::*; |
| 115 | |
| 116 | const STAGE: Pair = (800., 600.); |
| 117 | |
| 118 | #[test] |
| 119 | fn fit_contains_large_images_and_never_upscales() { |
| 120 | assert_eq!(fit_scale((1600., 1200.), STAGE), 0.5); |
| 121 | assert_eq!(fit_scale((1600., 300.), STAGE), 0.5); |
| 122 | assert_eq!(fit_scale((100., 100.), STAGE), 1.); |
| 123 | assert_eq!(fit_scale((0., 100.), STAGE), 1.); |
| 124 | } |
| 125 | |
| 126 | #[test] |
| 127 | fn rotation_swaps_the_sides_on_odd_quarter_turns() { |
| 128 | assert_eq!(rotated_size((400., 200.), 0), (400., 200.)); |
| 129 | assert_eq!(rotated_size((400., 200.), 1), (200., 400.)); |
| 130 | assert_eq!(rotated_size((400., 200.), 2), (400., 200.)); |
| 131 | assert_eq!(rotated_size((400., 200.), 3), (200., 400.)); |
| 132 | assert_eq!(next_rotation(3), 0); |
| 133 | assert_eq!(next_rotation(0), 1); |
| 134 | } |
| 135 | |
| 136 | #[test] |
| 137 | fn scale_is_limited_to_fit_and_four_times_the_source() { |
| 138 | assert_eq!(clamp_scale(0.1, 0.5), 0.5); |
| 139 | assert_eq!(clamp_scale(9., 0.5), MAX_ZOOM); |
| 140 | assert_eq!(clamp_scale(2., 0.5), 2.); |
| 141 | } |
| 142 | |
| 143 | #[test] |
| 144 | fn pan_is_clamped_to_the_image_bounds() { |
| 145 | let display = (1600., 400.); |
| 146 | assert_eq!(clamp_pan((900., 50.), display, STAGE), (400., 0.)); |
| 147 | assert_eq!(clamp_pan((-900., -50.), display, STAGE), (-400., 0.)); |
| 148 | assert_eq!(clamp_pan((10., 10.), (400., 300.), STAGE), (0., 0.)); |
| 149 | } |
| 150 | |
| 151 | #[test] |
| 152 | fn image_origin_centers_and_follows_the_pan() { |
| 153 | assert_eq!(image_origin((400., 300.), STAGE, (0., 0.)), (200., 150.)); |
| 154 | assert_eq!(image_origin((1600., 600.), STAGE, (-100., 0.)), (-500., 0.)); |
| 155 | } |
| 156 | |
| 157 | #[test] |
| 158 | fn zoom_keeps_the_point_under_the_cursor_fixed() { |
| 159 | let image = (2000., 1500.); |
| 160 | let start = View { |
| 161 | scale: 0.4, |
| 162 | pan: (0., 0.), |
| 163 | }; |
| 164 | let anchor = (100., 50.); |
| 165 | let zoomed = zoom_at(start, 1., anchor, image, STAGE); |
| 166 | assert_eq!(zoomed.scale, 1.); |
| 167 | let before = ( |
| 168 | (anchor.0 - start.pan.0) / start.scale, |
| 169 | (anchor.1 - start.pan.1) / start.scale, |
| 170 | ); |
| 171 | let after = ( |
| 172 | (anchor.0 - zoomed.pan.0) / zoomed.scale, |
| 173 | (anchor.1 - zoomed.pan.1) / zoomed.scale, |
| 174 | ); |
| 175 | assert!((before.0 - after.0).abs() < 0.01); |
| 176 | assert!((before.1 - after.1).abs() < 0.01); |
| 177 | } |
| 178 | |
| 179 | #[test] |
| 180 | fn zooming_back_to_fit_recenters() { |
| 181 | let image = (2000., 1500.); |
| 182 | let zoomed = View { |
| 183 | scale: 2., |
| 184 | pan: (300., -200.), |
| 185 | }; |
| 186 | let fit = zoom_at(zoomed, 0.1, (50., 50.), image, STAGE); |
| 187 | assert_eq!(fit.scale, 0.4); |
| 188 | assert_eq!(fit.pan, (0., 0.)); |
| 189 | } |
| 190 | |
| 191 | #[test] |
| 192 | fn zoom_never_pans_past_the_edges() { |
| 193 | let image = (2000., 1500.); |
| 194 | let start = View { |
| 195 | scale: 0.4, |
| 196 | pan: (0., 0.), |
| 197 | }; |
| 198 | let zoomed = zoom_at(start, 4., (-400., -300.), image, STAGE); |
| 199 | let limit = (2000. * 4. - 800.) / 2.; |
| 200 | assert!(zoomed.pan.0.abs() <= limit); |
| 201 | } |
| 202 | |
| 203 | #[test] |
| 204 | fn wheel_zooms_in_up_and_out_down_symmetrically() { |
| 205 | assert!(wheel_scale(1., 20.) > 1.); |
| 206 | assert!(wheel_scale(1., -20.) < 1.); |
| 207 | assert!((wheel_scale(wheel_scale(1., 20.), -20.) - 1.).abs() < 1e-5); |
| 208 | assert_eq!(wheel_scale(1., 0.), 1.); |
| 209 | } |
| 210 | |
| 211 | #[test] |
| 212 | fn keyboard_steps_multiply_and_divide_by_the_step() { |
| 213 | assert_eq!(step_scale(1., 1), ZOOM_STEP); |
| 214 | assert!((step_scale(ZOOM_STEP, -1) - 1.).abs() < 1e-6); |
| 215 | } |
| 216 | |
| 217 | #[test] |
| 218 | fn double_click_toggles_between_fit_and_full_size() { |
| 219 | assert_eq!(toggle_target(0.4, 0.4), 1.); |
| 220 | assert_eq!(toggle_target(1., 0.4), 0.4); |
| 221 | assert_eq!(toggle_target(2., 0.4), 0.4); |
| 222 | assert_eq!(toggle_target(1., 1.), 1.); |
| 223 | } |
| 224 | |
| 225 | #[test] |
| 226 | fn percent_label_is_relative_to_the_source_size() { |
| 227 | assert_eq!(percent_label(1.), "100%"); |
| 228 | assert_eq!(percent_label(0.375), "38%"); |
| 229 | assert_eq!(percent_label(4.), "400%"); |
| 230 | } |
| 231 | |
| 232 | #[test] |
| 233 | fn navigation_stops_at_both_ends() { |
| 234 | assert_eq!(step_index(0, 5, -1), 0); |
| 235 | assert_eq!(step_index(4, 5, 1), 4); |
| 236 | assert_eq!(step_index(2, 5, 1), 3); |
| 237 | assert_eq!(step_index(2, 5, -1), 1); |
| 238 | assert_eq!(step_index(0, 0, 1), 0); |
| 239 | } |
| 240 | |
| 241 | #[test] |
| 242 | fn counter_is_one_based() { |
| 243 | assert_eq!(counter_label(2, 12), "3 of 12"); |
| 244 | } |
| 245 | |
| 246 | #[test] |
| 247 | fn thumbnail_window_follows_the_current_image_inside_the_list() { |
| 248 | assert_eq!(thumbnail_window(0, 20, 5), 0..5); |
| 249 | assert_eq!(thumbnail_window(10, 20, 5), 8..13); |
| 250 | assert_eq!(thumbnail_window(19, 20, 5), 15..20); |
| 251 | assert_eq!(thumbnail_window(1, 3, 5), 0..3); |
| 252 | assert_eq!(thumbnail_window(0, 0, 5), 0..0); |
| 253 | assert_eq!(thumbnail_window(2, 4, 0), 2..3); |
| 254 | } |
| 255 | } |