Skip to content

Commit

Permalink
Introduce new zoom mode: custom
Browse files Browse the repository at this point in the history
  • Loading branch information
baskerville committed Dec 22, 2020
1 parent 71cfb54 commit d8bec23
Show file tree
Hide file tree
Showing 5 changed files with 266 additions and 75 deletions.
5 changes: 5 additions & 0 deletions doc/MANUAL.md
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,11 @@ Swipe north/south to scroll the page stream when the zoom mode is fit-to-width.
Rotate to change the screen orientation (one finger is the center, the other describes the desired rotation with a circular motion around the center: the two fingers should land and take off simultaneously).

Spread (resp. pinch) horizontally to switch the zoom mode to fit-to-width (resp. fit-to-page).
Spread (resp. pinch) diagonally to zoom in (resp. out) on the current page (the zoom mode is set to *custom*).

When the zoom mode is *custom*:
- Tapping a peripheral region moves the view port in the corresponding direction.
- Swiping moves the view port in the swipe's opposite direction.

The following swipe sequences are recognized:

Expand Down
23 changes: 20 additions & 3 deletions src/emulator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ use sdl2::event::Event as SdlEvent;
use sdl2::keyboard::{Scancode, Keycode};
use sdl2::render::{WindowCanvas, BlendMode};
use sdl2::pixels::{Color as SdlColor, PixelFormatEnum};
use sdl2::mouse::MouseState;
use sdl2::rect::Point as SdlPoint;
use sdl2::rect::Rect as SdlRect;
use crate::framebuffer::{Framebuffer, UpdateMode};
Expand All @@ -56,8 +57,8 @@ use crate::view::common::{locate, locate_by_id, transfer_notifications, overlapp
use crate::view::common::{toggle_input_history_menu, toggle_keyboard_layout_menu};
use crate::helpers::{load_toml, save_toml};
use crate::settings::{Settings, SETTINGS_PATH};
use crate::geom::Rectangle;
use crate::gesture::gesture_events;
use crate::geom::{Rectangle, Axis};
use crate::gesture::{GestureEvent, gesture_events};
use crate::device::CURRENT_DEVICE;
use crate::battery::{Battery, FakeBattery};
use crate::frontlight::{Frontlight, LightLevels};
Expand Down Expand Up @@ -274,7 +275,8 @@ fn main() -> Result<(), Error> {
let mut bus = VecDeque::with_capacity(4);

'outer: loop {
if let Some(sdl_evt) = sdl_context.event_pump().unwrap().wait_event_timeout(20) {
let mut event_pump = sdl_context.event_pump().unwrap();
if let Some(sdl_evt) = event_pump.wait_event_timeout(20) {
match sdl_evt {
SdlEvent::Quit { .. } |
SdlEvent::KeyDown { keycode: Some(Keycode::Escape), .. } => {
Expand All @@ -297,6 +299,21 @@ fn main() -> Result<(), Error> {
Scancode::S => {
tx.send(Event::Select(EntryId::TakeScreenshot)).ok();
},
Scancode::I | Scancode::O => {
let mouse_state = MouseState::new(&event_pump);
let x = mouse_state.x() as i32;
let y = mouse_state.y() as i32;
let center = pt!(x, y);
if scancode == Scancode::I {
tx.send(Event::Gesture(GestureEvent::Spread { center,
factor: 2.0,
axis: Axis::Diagonal })).ok();
} else {
tx.send(Event::Gesture(GestureEvent::Pinch { center,
factor: 0.5,
axis: Axis::Diagonal })).ok();
}
},
_ => (),
}
},
Expand Down
2 changes: 1 addition & 1 deletion src/geom.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ impl LinearDir {
}
}

#[derive(Debug, Copy, Clone, Eq, PartialEq, Hash)]
#[derive(Debug, Copy, Clone, Eq, PartialEq, Hash, Serialize, Deserialize)]
pub struct Point {
pub x: i32,
pub y: i32,
Expand Down
21 changes: 18 additions & 3 deletions src/metadata.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ use fxhash::FxHashMap;
use serde::{Serialize, Deserialize};
use lazy_static::lazy_static;
use titlecase::titlecase;
use crate::geom::Point;
use crate::document::{Document, SimpleTocEntry, TextLocation};
use crate::document::asciify;
use crate::document::epub::EpubDocument;
Expand Down Expand Up @@ -199,7 +200,7 @@ pub struct ReaderInfo {
#[serde(skip_serializing_if = "Option::is_none")]
pub zoom_mode: Option<ZoomMode>,
#[serde(skip_serializing_if = "Option::is_none")]
pub top_offset: Option<i32>,
pub page_offset: Option<Point>,
#[serde(skip_serializing_if = "Option::is_none")]
pub rotation: Option<i8>,
#[serde(skip_serializing_if = "Option::is_none")]
Expand Down Expand Up @@ -228,12 +229,26 @@ pub struct ReaderInfo {
pub annotations: Vec<Annotation>,
}

#[derive(Serialize, Deserialize, Debug, Copy, Clone, PartialEq, Eq)]
#[derive(Serialize, Deserialize, Debug, Copy, Clone)]
pub enum ZoomMode {
FitToPage,
FitToWidth,
Custom(f32),
}

impl PartialEq for ZoomMode {
fn eq(&self, other: &Self) -> bool {
match (self, other) {
(ZoomMode::FitToPage, ZoomMode::FitToPage) => true,
(ZoomMode::FitToWidth, ZoomMode::FitToWidth) => true,
(ZoomMode::Custom(z1), ZoomMode::Custom(z2)) => (z1 - z2).abs() < f32::EPSILON,
_ => false,
}
}
}

impl Eq for ZoomMode {}

impl ReaderInfo {
pub fn progress(&self) -> f32 {
(self.current_page / self.pages_count) as f32
Expand All @@ -248,7 +263,7 @@ impl Default for ReaderInfo {
pages_count: 1,
finished: false,
zoom_mode: None,
top_offset: None,
page_offset: None,
rotation: None,
cropping_margins: None,
margin_width: None,
Expand Down
Loading

0 comments on commit d8bec23

Please sign in to comment.