Skip to content

Commit

Permalink
Separate bookmarks and annotations nav. gestures
Browse files Browse the repository at this point in the history
  • Loading branch information
baskerville committed Sep 6, 2020
1 parent 9d45b98 commit d6d7e19
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 45 deletions.
3 changes: 2 additions & 1 deletion doc/MANUAL.md
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,8 @@ The following swipe sequences are recognized:

- Arrow west/east: go to the previous/next chapter in normal mode, the first/last results in search mode.
- Arrow north/south: start searching text backward/forward.
- Top left/right corner: go to the previous/next annotation, highlight or bookmark.
- Top left/right corner: go to the previous/next bookmark.
- Top left/right multi-corner: go to the previous/next annotation or highlight.
- Bottom left corner: guess the frontlight if there's more than two frontlight presets defined, toggle the frontlight otherwise.
- Bottom right corner: toggle the bitonal mode.

Expand Down
32 changes: 28 additions & 4 deletions src/gesture.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,21 @@ pub enum GestureEvent {
start: Point,
end: Point,
},
MultiArrow {
dir: Dir,
starts: [Point; 2],
ends: [Point; 2],
},
Corner {
dir: DiagDir,
start: Point,
end: Point,
},
MultiCorner {
dir: DiagDir,
starts: [Point; 2],
ends: [Point; 2],
},
Pinch {
axis: Axis,
strength: u32,
Expand Down Expand Up @@ -166,7 +176,7 @@ pub fn parse_gesture_events(rx: &Receiver<DeviceEvent>, ty: &Sender<Event>) {
dir: d1,
starts: [s1, s2],
ends: [e1, e2],
})).unwrap();
})).ok();
},
(GestureEvent::Swipe { dir: d1, start: s1, end: e1, .. },
GestureEvent::Swipe { dir: d2, start: s2, end: e2, .. }) if d1 == d2.opposite() => {
Expand All @@ -178,20 +188,34 @@ pub fn parse_gesture_events(rx: &Receiver<DeviceEvent>, ty: &Sender<Event>) {
starts: [s1, s2],
ends: [e1, e2],
strength: (ds - de) as u32,
})).unwrap();
})).ok();
} else {
ty.send(Event::Gesture(GestureEvent::Spread {
axis: d1.axis(),
starts: [s1, s2],
ends: [e1, e2],
strength: (de - ds) as u32,
})).unwrap();
})).ok();
}
},
(GestureEvent::Arrow { dir: Dir::East, start: s1, end: e1 }, GestureEvent::Arrow { dir: Dir::West, start: s2, end: e2 }) |
(GestureEvent::Arrow { dir: Dir::West, start: s2, end: e2 }, GestureEvent::Arrow { dir: Dir::East, start: s1, end: e1 }) if s1.x < s2.x => {
ty.send(Event::Gesture(GestureEvent::Cross((s1+e1+s2+e2)/4))).ok();
},
(GestureEvent::Arrow { dir: d1, start: s1, end: e1 }, GestureEvent::Arrow { dir: d2, start: s2, end: e2 }) if d1 == d2 => {
ty.send(Event::Gesture(GestureEvent::MultiArrow {
dir: d1,
starts: [s1, s2],
ends: [e1, e2],
})).ok();
},
(GestureEvent::Corner { dir: d1, start: s1, end: e1 }, GestureEvent::Corner { dir: d2, start: s2, end: e2 }) if d1 == d2 => {
ty.send(Event::Gesture(GestureEvent::MultiCorner {
dir: d1,
starts: [s1, s2],
ends: [e1, e2],
})).ok();
},
(GestureEvent::Tap(c), GestureEvent::Swipe { start: s, end: e, .. }) |
(GestureEvent::Swipe { start: s, end: e, .. }, GestureEvent::Tap(c)) |
(GestureEvent::Tap(c), GestureEvent::Arrow { start: s, end: e, .. }) |
Expand All @@ -205,7 +229,7 @@ pub fn parse_gesture_events(rx: &Receiver<DeviceEvent>, ty: &Sender<Event>) {
angle,
quarter_turns,
center: c,
})).unwrap();
})).ok();
},
_ => (),
}
Expand Down
79 changes: 39 additions & 40 deletions src/view/reader/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -530,49 +530,40 @@ impl Reader {
min_loc.and_then(|min| max_loc.map(|max| [min, max]))
}

fn go_to_artefact(&mut self, dir: CycleDir, hub: &Hub, rq: &mut RenderQueue, context: &Context) {
let mut loc_bkm = None;
let mut loc_annot = None;

if let Some(ref r) = self.info.reader {
fn go_to_bookmark(&mut self, dir: CycleDir, hub: &Hub, rq: &mut RenderQueue, context: &Context) {
let loc_bkm = self.info.reader.as_ref().and_then(|r| {
match dir {
CycleDir::Next => {
loc_bkm = r.bookmarks.range(self.current_page+1 ..)
.next().cloned();
if let Some([_, max]) = self.text_location_range() {
loc_annot = r.annotations.iter()
.filter(|annot| annot.selection[0] > max)
.map(|annot| annot.selection[0]).min()
.map(|tl| tl.location());
}
},
CycleDir::Previous => {
loc_bkm = r.bookmarks.range(.. self.current_page)
.next_back().cloned();
if let Some([min, _]) = self.text_location_range() {
loc_annot = r.annotations.iter()
.filter(|annot| annot.selection[1] < min)
.map(|annot| annot.selection[1]).max()
.map(|tl| tl.location());
}
},
CycleDir::Next => r.bookmarks.range(self.current_page+1 ..)
.next().cloned(),
CycleDir::Previous => r.bookmarks.range(.. self.current_page)
.next_back().cloned(),
}
});

if let Some(location) = loc_bkm {
self.go_to_page(location, true, hub, rq, context);
}
}

let loc = match (loc_bkm, loc_annot) {
(Some(a), Some(b)) => {
if dir == CycleDir::Next {
Some(a.min(b))
} else {
Some(a.max(b))
}
},
(Some(a), None) => Some(a),
(None, Some(b)) => Some(b),
(None, None) => None,
};
fn go_to_annotation(&mut self, dir: CycleDir, hub: &Hub, rq: &mut RenderQueue, context: &Context) {
let loc_annot = self.info.reader.as_ref().and_then(|r| {
match dir {
CycleDir::Next => self.text_location_range().and_then(|[_, max]| {
r.annotations.iter()
.filter(|annot| annot.selection[0] > max)
.map(|annot| annot.selection[0]).min()
.map(|tl| tl.location())
}),
CycleDir::Previous => self.text_location_range().and_then(|[min, _]| {
r.annotations.iter()
.filter(|annot| annot.selection[1] < min)
.map(|annot| annot.selection[1]).max()
.map(|tl| tl.location())
}),
}
});

if let Some(location) = loc {
if let Some(location) = loc_annot {
self.go_to_page(location, true, hub, rq, context);
}
}
Expand Down Expand Up @@ -2470,8 +2461,8 @@ impl View for Reader {
},
Event::Gesture(GestureEvent::Corner { dir, .. }) => {
match dir {
DiagDir::NorthWest => self.go_to_artefact(CycleDir::Previous, hub, rq, context),
DiagDir::NorthEast => self.go_to_artefact(CycleDir::Next, hub, rq, context),
DiagDir::NorthWest => self.go_to_bookmark(CycleDir::Previous, hub, rq, context),
DiagDir::NorthEast => self.go_to_bookmark(CycleDir::Next, hub, rq, context),
DiagDir::SouthEast => {
hub.send(Event::Select(EntryId::ToggleMonochrome)).ok();
},
Expand All @@ -2496,6 +2487,14 @@ impl View for Reader {
};
true
},
Event::Gesture(GestureEvent::MultiCorner { dir, .. }) => {
match dir {
DiagDir::NorthWest => self.go_to_annotation(CycleDir::Previous, hub, rq, context),
DiagDir::NorthEast => self.go_to_annotation(CycleDir::Next, hub, rq, context),
_ => (),
}
true
},
Event::Gesture(GestureEvent::Cross(_)) => {
self.quit(context);
hub.send(Event::Back).ok();
Expand Down

0 comments on commit d6d7e19

Please sign in to comment.