-
Notifications
You must be signed in to change notification settings - Fork 105
/
mod.rs
664 lines (598 loc) · 17.3 KB
/
mod.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
//! Views are organized as a tree. A view might receive / send events and render itself.
//!
//! The z-level of the n-th child of a view is less or equal to the z-level of its n+1-th child.
//!
//! Events travel from the root to the leaves, only the leaf views will handle the root events, but
//! any view can send events to its parent. From the events it receives from its children, a view
//! resends the ones it doesn't handle to its own parent. Hence an event sent from a child might
//! bubble up to the root. If it reaches the root without being captured by any view, then it will
//! be written to the main event channel and will be sent to every leaf in one of the next loop
//! iterations.
pub mod common;
pub mod filler;
pub mod image;
pub mod icon;
pub mod label;
pub mod button;
pub mod rounded_button;
pub mod slider;
pub mod input_field;
pub mod page_label;
pub mod named_input;
pub mod labeled_icon;
pub mod top_bar;
pub mod search_bar;
pub mod dialog;
pub mod notification;
pub mod intermission;
pub mod frontlight;
pub mod presets_list;
pub mod preset;
pub mod menu;
pub mod menu_entry;
pub mod clock;
pub mod battery;
pub mod keyboard;
pub mod key;
pub mod home;
pub mod reader;
pub mod dictionary;
pub mod calculator;
pub mod sketch;
use std::ops::{Deref, DerefMut};
use std::time::Duration;
use std::path::PathBuf;
use std::sync::mpsc::Sender;
use std::sync::atomic::{AtomicU64, Ordering};
use std::collections::VecDeque;
use std::fmt::{self, Debug};
use fxhash::FxHashMap;
use downcast_rs::{Downcast, impl_downcast};
use crate::font::Fonts;
use crate::document::{Location, TextLocation, TocEntry};
use crate::settings::{ButtonScheme, FirstColumn, SecondColumn, RotationLock};
use crate::metadata::{Info, ZoomMode, SortMethod, TextAlign, SimpleStatus, PageScheme, Margin};
use crate::geom::{LinearDir, CycleDir, Rectangle, Boundary};
use crate::framebuffer::{Framebuffer, UpdateMode};
use crate::input::{DeviceEvent, FingerStatus};
use crate::gesture::GestureEvent;
use self::calculator::LineOrigin;
use self::key::KeyKind;
use self::intermission::IntermKind;
use crate::app::Context;
// Border thicknesses in pixels, at 300 DPI.
pub const THICKNESS_SMALL: f32 = 1.0;
pub const THICKNESS_MEDIUM: f32 = 2.0;
pub const THICKNESS_LARGE: f32 = 3.0;
// Border radii in pixels, at 300 DPI.
pub const BORDER_RADIUS_SMALL: f32 = 6.0;
pub const BORDER_RADIUS_MEDIUM: f32 = 9.0;
pub const BORDER_RADIUS_LARGE: f32 = 12.0;
// Big and small bar heights in pixels, at 300 DPI.
// On the *Aura ONE*, the height is exactly `2 * sb + 10 * bb`.
pub const SMALL_BAR_HEIGHT: f32 = 121.0;
pub const BIG_BAR_HEIGHT: f32 = 163.0;
pub const CLOSE_IGNITION_DELAY: Duration = Duration::from_millis(150);
pub type Bus = VecDeque<Event>;
pub type Hub = Sender<Event>;
pub trait View: Downcast {
fn handle_event(&mut self, evt: &Event, hub: &Hub, bus: &mut Bus, rq: &mut RenderQueue, context: &mut Context) -> bool;
fn render(&self, fb: &mut dyn Framebuffer, rect: Rectangle, fonts: &mut Fonts);
fn rect(&self) -> &Rectangle;
fn rect_mut(&mut self) -> &mut Rectangle;
fn children(&self) -> &Vec<Box<dyn View>>;
fn children_mut(&mut self) -> &mut Vec<Box<dyn View>>;
fn id(&self) -> Id;
fn render_rect(&self, _rect: &Rectangle) -> Rectangle {
*self.rect()
}
fn resize(&mut self, rect: Rectangle, _hub: &Hub, _rq: &mut RenderQueue, _context: &mut Context) {
*self.rect_mut() = rect;
}
fn child(&self, index: usize) -> &dyn View {
self.children()[index].as_ref()
}
fn child_mut(&mut self, index: usize) -> &mut dyn View {
self.children_mut()[index].as_mut()
}
fn len(&self) -> usize {
self.children().len()
}
fn might_skip(&self, _evt: &Event) -> bool {
false
}
fn might_rotate(&self) -> bool {
true
}
fn is_background(&self) -> bool {
false
}
fn view_id(&self) -> Option<ViewId> {
None
}
}
impl_downcast!(View);
impl Debug for Box<dyn View> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "Box<dyn View>")
}
}
// We start delivering events from the highest z-level to prevent views from capturing
// gestures that occurred in higher views.
// The consistency must also be ensured by the views: popups, for example, need to
// capture any tap gesture with a touch point inside their rectangle.
// A child can send events to the main channel through the *hub* or communicate with its parent through the *bus*.
// A view that wants to render can write to the rendering queue.
pub fn handle_event(view: &mut dyn View, evt: &Event, hub: &Hub, parent_bus: &mut Bus,
rq: &mut RenderQueue, context: &mut Context) -> bool {
if view.len() > 0 {
let mut captured = false;
if view.might_skip(evt) {
return captured;
}
let mut child_bus: Bus = VecDeque::with_capacity(1);
for i in (0..view.len()).rev() {
if handle_event(view.child_mut(i), evt, hub, &mut child_bus, rq, context) {
captured = true;
break;
}
}
let mut temp_bus: Bus = VecDeque::with_capacity(1);
child_bus.retain(|child_evt| !view.handle_event(child_evt, hub, &mut temp_bus, rq, context));
parent_bus.append(&mut child_bus);
parent_bus.append(&mut temp_bus);
captured || view.handle_event(evt, hub, parent_bus, rq, context)
} else {
view.handle_event(evt, hub, parent_bus, rq, context)
}
}
// We render from bottom to top. For a view to render it has to either appear in `ids` or intersect
// one of the rectangles in `bgs`. When we're about to render a view, if `wait` is true, we'll wait
// for all the updates in `updating` that intersect with the view.
pub fn render(view: &dyn View, wait: bool, ids: &FxHashMap<Id, Vec<Rectangle>>, rects: &mut Vec<Rectangle>,
bgs: &mut Vec<Rectangle>, fb: &mut dyn Framebuffer, fonts: &mut Fonts, updating: &mut FxHashMap<u32, Rectangle>) {
let mut render_rects = Vec::new();
if view.len() == 0 || view.is_background() {
for rect in ids.get(&view.id()).cloned().into_iter().flatten()
.chain(rects.iter().filter_map(|r| r.intersection(view.rect())))
.chain(bgs.iter().filter_map(|r| r.intersection(view.rect()))) {
let render_rect = view.render_rect(&rect);
if wait {
updating.retain(|tok, urect| {
!render_rect.overlaps(urect) ||
fb.wait(*tok).map_err(|err| eprintln!("{}", err)).is_err()
});
}
view.render(fb, rect, fonts);
render_rects.push(render_rect);
// Most views can't render a subrectangle of themselves.
if *view.rect() == render_rect {
break;
}
}
} else {
bgs.extend(ids.get(&view.id()).cloned().into_iter().flatten());
}
// Merge the contiguous zones to avoid having to schedule lots of small frambuffer updates.
for rect in render_rects.into_iter() {
if rects.is_empty() {
rects.push(rect);
} else {
if let Some(last) = rects.last_mut() {
if rect.touches(last) {
last.absorb(&rect);
let mut i = rects.len();
while i > 1 && rects[i-1].touches(&rects[i-2]) {
if let Some(rect) = rects.pop() {
if let Some(last) = rects.last_mut() {
last.absorb(&rect);
}
}
i -= 1;
}
} else {
let mut i = rects.len();
while i > 0 && !rects[i-1].contains(&rect) {
i -= 1;
}
if i == 0 {
rects.push(rect);
}
}
}
}
}
for i in 0..view.len() {
render(view.child(i), wait, ids, rects, bgs, fb, fonts, updating);
}
}
#[inline]
pub fn process_render_queue(view: &dyn View, rq: &mut RenderQueue, context: &mut Context, updating: &mut FxHashMap<u32, Rectangle>) {
for ((mode, wait), pairs) in rq.drain() {
let mut ids = FxHashMap::default();
let mut rects = Vec::new();
let mut bgs = Vec::new();
for (id, rect) in pairs.into_iter().rev() {
if let Some(id) = id {
ids.entry(id).or_insert_with(|| Vec::new()).push(rect);
} else {
bgs.push(rect);
}
}
render(view, wait, &ids, &mut rects, &mut bgs,
context.fb.as_mut(), &mut context.fonts, updating);
for rect in rects {
match context.fb.update(&rect, mode) {
Ok(tok) => { updating.insert(tok, rect); },
Err(err) => { eprintln!("{}", err); },
}
}
}
}
#[derive(Debug, Clone)]
pub enum Event {
Device(DeviceEvent),
Gesture(GestureEvent),
Keyboard(KeyboardEvent),
Key(KeyKind),
AddDocument(Box<Info>),
Open(Box<Info>),
OpenToc(Vec<TocEntry>, usize),
LoadPixmap(usize),
Update(UpdateMode),
Invalid(Box<Info>),
Notify(String),
Page(CycleDir),
ResultsPage(CycleDir),
GoTo(usize),
GoToLocation(Location),
ResultsGoTo(usize),
CropMargins(Box<Margin>),
Chapter(CycleDir),
Sort(SortMethod),
SelectDirectory(PathBuf),
ToggleSelectDirectory(PathBuf),
NavigationBarResized(i32),
Focus(Option<ViewId>),
Select(EntryId),
PropagateSelect(EntryId),
EditLanguages,
Define(String),
Submit(ViewId, String),
Slider(SliderId, f32, FingerStatus),
ToggleNear(ViewId, Rectangle),
ToggleInputHistoryMenu(ViewId, Rectangle),
ToggleBookMenu(Rectangle, usize),
TogglePresetMenu(Rectangle, usize),
SubMenu(Rectangle, Vec<EntryKind>),
ProcessLine(LineOrigin, String),
History(CycleDir, bool),
Toggle(ViewId),
Show(ViewId),
Close(ViewId),
CloseSub(ViewId),
Search(String),
SearchResult(usize, Vec<Boundary>),
EndOfSearch,
Finished,
ClockTick,
BatteryTick,
ToggleFrontlight,
Load(PathBuf),
LoadPreset(usize),
Scroll(i32),
Save,
Guess,
CheckBattery,
SetWifi(bool),
MightSuspend,
PrepareSuspend,
Suspend,
Share,
PrepareShare,
Validate,
Cancel,
Reseed,
Back,
Quit,
WakeUp,
}
#[derive(Debug, Clone, Eq, PartialEq)]
pub enum AppCmd {
Sketch,
Calculator,
Dictionary {
query: String,
language: String,
},
}
#[derive(Debug, Copy, Clone, Hash, Eq, PartialEq)]
pub enum ViewId {
Home,
Reader,
SortMenu,
MainMenu,
TitleMenu,
SelectionMenu,
AnnotationMenu,
BatteryMenu,
ClockMenu,
SearchTargetMenu,
InputHistoryMenu,
KeyboardLayoutMenu,
Frontlight,
Dictionary,
FontSizeMenu,
TextAlignMenu,
FontFamilyMenu,
MarginWidthMenu,
ContrastExponentMenu,
ContrastGrayMenu,
LineHeightMenu,
DirectoryMenu,
BookMenu,
LibraryMenu,
PageMenu,
PresetMenu,
MarginCropperMenu,
SearchMenu,
SketchMenu,
GoToPage,
GoToPageInput,
GoToResultsPage,
GoToResultsPageInput,
NamePage,
NamePageInput,
EditNote,
EditNoteInput,
EditLanguages,
EditLanguagesInput,
HomeSearchInput,
ReaderSearchInput,
DictionarySearchInput,
CalculatorInput,
SearchBar,
AddressBar,
AddressBarInput,
Keyboard,
AboutDialog,
ShareDialog,
MarginCropper,
TopBottomBars,
TableOfContents,
MessageNotif,
BoundaryNotif,
TakeScreenshotNotif,
SaveDocumentNotif,
SaveSketchNotif,
LoadSketchNotif,
NoSearchResultsNotif,
InvalidSearchQueryNotif,
LowBatteryNotif,
NetUpNotif,
SubMenu(u8),
}
#[derive(Debug, Copy, Clone, Eq, PartialEq)]
pub enum SliderId {
FontSize,
LightIntensity,
LightWarmth,
ContrastExponent,
ContrastGray,
}
impl SliderId {
pub fn label(self) -> String {
match self {
SliderId::LightIntensity => "Intensity".to_string(),
SliderId::LightWarmth => "Warmth".to_string(),
SliderId::FontSize => "Font Size".to_string(),
SliderId::ContrastExponent => "Contrast Exponent".to_string(),
SliderId::ContrastGray => "Contrast Gray".to_string(),
}
}
}
#[derive(Debug, Clone)]
pub enum Align {
Left(i32),
Right(i32),
Center,
}
impl Align {
#[inline]
pub fn offset(&self, width: i32, container_width: i32) -> i32 {
match *self {
Align::Left(dx) => dx,
Align::Right(dx) => container_width - width - dx,
Align::Center => (container_width - width) / 2,
}
}
}
#[derive(Debug, Copy, Clone)]
pub enum KeyboardEvent {
Append(char),
Partial(char),
Move { target: TextKind, dir: LinearDir },
Delete { target: TextKind, dir: LinearDir },
Submit,
}
#[derive(Debug, Copy, Clone)]
pub enum TextKind {
Char,
Word,
Extremum,
}
#[derive(Debug, Clone)]
pub enum EntryKind {
Message(String),
Command(String, EntryId),
CheckBox(String, EntryId, bool),
RadioButton(String, EntryId, bool),
SubMenu(String, Vec<EntryKind>),
Separator,
}
#[derive(Debug, Clone, Eq, PartialEq)]
pub enum EntryId {
About,
SystemInfo,
LoadLibrary(usize),
Load(PathBuf),
Flush,
Save,
Import,
CleanUp,
Sort(SortMethod),
ReverseOrder,
Remove(PathBuf),
MoveTo(PathBuf, usize),
AddDirectory(PathBuf),
SelectDirectory(PathBuf),
ToggleSelectDirectory(PathBuf),
SetStatus(PathBuf, SimpleStatus),
ToggleIntermissionImage(IntermKind, PathBuf),
RemovePreset(usize),
FirstColumn(FirstColumn),
SecondColumn(SecondColumn),
ApplyCroppings(usize, PageScheme),
RemoveCroppings,
SetZoomMode(ZoomMode),
SetPageName,
RemovePageName,
HighlightSelection,
AnnotateSelection,
DefineSelection,
SearchForSelection,
AdjustSelection,
RemoveAnnotation([TextLocation; 2]),
EditAnnotationNote([TextLocation; 2]),
RemoveAnnotationNote([TextLocation; 2]),
GoTo(usize),
GoToSelectedPageName,
SearchDirection(LinearDir),
SetButtonScheme(ButtonScheme),
SetFontFamily(String),
SetFontSize(i32),
SetTextAlign(TextAlign),
SetMarginWidth(i32),
SetLineHeight(i32),
SetContrastExponent(i32),
SetContrastGray(i32),
SetRotationLock(Option<RotationLock>),
SetSearchTarget(Option<String>),
SetInputText(ViewId, String),
SetKeyboardLayout(String),
ToggleShowHidden,
ToggleFuzzy,
ToggleInverted,
ToggleMonochrome,
ToggleWifi,
Rotate(i8),
Launch(AppCmd),
SetPenSize(i32),
SetPenColor(u8),
TogglePenDynamism,
ReloadDictionaries,
New,
Refresh,
TakeScreenshot,
Reboot,
RebootInNickel,
Quit,
}
impl EntryKind {
pub fn is_separator(&self) -> bool {
match *self {
EntryKind::Separator => true,
_ => false,
}
}
pub fn text(&self) -> &str {
match *self {
EntryKind::Message(ref s) |
EntryKind::Command(ref s, ..) |
EntryKind::CheckBox(ref s, ..) |
EntryKind::RadioButton(ref s, ..) |
EntryKind::SubMenu(ref s, ..) => s,
_ => "",
}
}
pub fn get(&self) -> Option<bool> {
match *self {
EntryKind::CheckBox(_, _, v) |
EntryKind::RadioButton(_, _, v) => Some(v),
_ => None,
}
}
pub fn set(&mut self, value: bool) {
match *self {
EntryKind::CheckBox(_, _, ref mut v) |
EntryKind::RadioButton(_, _, ref mut v) => *v = value,
_ => (),
}
}
}
pub struct RenderData {
pub id: Option<Id>,
pub rect: Rectangle,
pub mode: UpdateMode,
pub wait: bool,
}
impl RenderData {
pub fn new(id: Id, rect: Rectangle, mode: UpdateMode) -> RenderData {
RenderData {
id: Some(id),
rect,
mode,
wait: true,
}
}
pub fn no_wait(id: Id, rect: Rectangle, mode: UpdateMode) -> RenderData {
RenderData {
id: Some(id),
rect,
mode,
wait: false,
}
}
pub fn expose(rect: Rectangle, mode: UpdateMode) -> RenderData {
RenderData {
id: None,
rect,
mode,
wait: true,
}
}
}
type RQ = FxHashMap<(UpdateMode, bool), Vec<(Option<Id>, Rectangle)>>;
pub struct RenderQueue(RQ);
impl RenderQueue {
pub fn new() -> RenderQueue {
RenderQueue(FxHashMap::default())
}
pub fn add(&mut self, data: RenderData) {
self.entry((data.mode, data.wait)).or_insert_with(|| {
Vec::new()
}).push((data.id, data.rect));
}
}
impl Deref for RenderQueue {
type Target = RQ;
fn deref(&self) -> &Self::Target {
&self.0
}
}
impl DerefMut for RenderQueue {
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.0
}
}
pub static ID_FEEDER: IdFeeder = IdFeeder::new(1);
pub struct IdFeeder(AtomicU64);
pub type Id = u64;
impl IdFeeder {
pub const fn new(id: Id) -> Self {
IdFeeder(AtomicU64::new(id))
}
pub fn next(&self) -> Id {
self.0.fetch_add(1, Ordering::Relaxed)
}
}