-
Notifications
You must be signed in to change notification settings - Fork 102
/
intermission.rs
163 lines (143 loc) · 5.84 KB
/
intermission.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
use std::path::PathBuf;
use crate::device::CURRENT_DEVICE;
use crate::document::{Location, open};
use crate::geom::Rectangle;
use crate::font::{Fonts, font_from_style, DISPLAY_STYLE};
use super::{View, Event, Hub, Bus, Id, ID_FEEDER, RenderQueue};
use crate::framebuffer::Framebuffer;
use crate::settings::{IntermKind, LOGO_SPECIAL_PATH, COVER_SPECIAL_PATH};
use crate::metadata::{SortMethod, BookQuery, sort};
use crate::color::{TEXT_NORMAL, TEXT_INVERTED_HARD};
use crate::context::Context;
pub struct Intermission {
id: Id,
rect: Rectangle,
children: Vec<Box<dyn View>>,
message: Message,
halt: bool,
}
pub enum Message {
Text(String),
Image(PathBuf),
Cover(PathBuf),
}
impl Intermission {
pub fn new(rect: Rectangle, kind: IntermKind, context: &Context) -> Intermission {
let path = &context.settings.intermissions[kind];
let message = match path.to_str() {
Some(LOGO_SPECIAL_PATH) => Message::Text(kind.text().to_string()),
Some(COVER_SPECIAL_PATH) => {
let query = BookQuery {
reading: Some(true),
.. Default::default()
};
let (mut files, _) = context.library.list(&context.library.home, Some(&query), false);
sort(&mut files, SortMethod::Opened, true);
if !files.is_empty() {
Message::Cover(context.library.home.join(&files[0].file.path))
} else {
Message::Text(kind.text().to_string())
}
},
_ => Message::Image(path.clone()),
};
Intermission {
id: ID_FEEDER.next(),
rect,
children: Vec::new(),
message,
halt: kind == IntermKind::PowerOff,
}
}
}
impl View for Intermission {
fn handle_event(&mut self, _evt: &Event, _hub: &Hub, _bus: &mut Bus, _rq: &mut RenderQueue, _context: &mut Context) -> bool {
true
}
fn render(&self, fb: &mut dyn Framebuffer, _rect: Rectangle, fonts: &mut Fonts) {
let scheme = if self.halt ^ fb.inverted() {
TEXT_INVERTED_HARD
} else {
TEXT_NORMAL
};
fb.draw_rectangle(&self.rect, scheme[0]);
match self.message {
Message::Text(ref text) => {
let dpi = CURRENT_DEVICE.dpi;
let font = font_from_style(fonts, &DISPLAY_STYLE, dpi);
let padding = font.em() as i32;
let max_width = self.rect.width() as i32 - 3 * padding;
let mut plan = font.plan(text, None, None);
if plan.width > max_width {
let scale = max_width as f32 / plan.width as f32;
let size = (scale * DISPLAY_STYLE.size as f32) as u32;
font.set_size(size, dpi);
plan = font.plan(text, None, None);
}
let x_height = font.x_heights.0 as i32;
let dx = (self.rect.width() as i32 - plan.width) / 2;
let dy = (self.rect.height() as i32) / 3;
font.render(fb, scheme[1], &plan, pt!(dx, dy));
let mut doc = open("icons/dodecahedron.svg").unwrap();
let (width, height) = doc.dims(0).unwrap();
let scale = (plan.width as f32 / width.max(height) as f32) / 4.0;
let (pixmap, _) = doc.pixmap(Location::Exact(0), scale).unwrap();
let dx = (self.rect.width() as i32 - pixmap.width as i32) / 2;
let dy = dy + 2 * x_height;
let pt = self.rect.min + pt!(dx, dy);
fb.draw_blended_pixmap(&pixmap, pt, scheme[1]);
},
Message::Image(ref path) => {
if let Some(mut doc) = open(path) {
if let Some((width, height)) = doc.dims(0) {
let w_ratio = self.rect.width() as f32 / width;
let h_ratio = self.rect.height() as f32 / height;
let scale = w_ratio.min(h_ratio);
if let Some((pixmap, _)) = doc.pixmap(Location::Exact(0), scale) {
let dx = (self.rect.width() as i32 - pixmap.width as i32) / 2;
let dy = (self.rect.height() as i32 - pixmap.height as i32) / 2;
let pt = self.rect.min + pt!(dx, dy);
fb.draw_pixmap(&pixmap, pt);
if fb.inverted() {
let rect = pixmap.rect() + pt;
fb.invert_region(&rect);
}
}
}
}
},
Message::Cover(ref path) => {
if let Some(mut doc) = open(path) {
if let Some(pixmap) = doc.preview_pixmap(self.rect.width() as f32, self.rect.height() as f32) {
let dx = (self.rect.width() as i32 - pixmap.width as i32) / 2;
let dy = (self.rect.height() as i32 - pixmap.height as i32) / 2;
let pt = self.rect.min + pt!(dx, dy);
fb.draw_pixmap(&pixmap, pt);
if fb.inverted() {
let rect = pixmap.rect() + pt;
fb.invert_region(&rect);
}
}
}
},
}
}
fn might_rotate(&self) -> bool {
false
}
fn rect(&self) -> &Rectangle {
&self.rect
}
fn rect_mut(&mut self) -> &mut Rectangle {
&mut self.rect
}
fn children(&self) -> &Vec<Box<dyn View>> {
&self.children
}
fn children_mut(&mut self) -> &mut Vec<Box<dyn View>> {
&mut self.children
}
fn id(&self) -> Id {
self.id
}
}