1use super::book::Book;
2use crate::color::{SEPARATOR_NORMAL, WHITE};
3use crate::context::Context;
4use crate::device::CURRENT_DEVICE;
5use crate::document::open;
6use crate::font::Fonts;
7use crate::framebuffer::{Framebuffer, UpdateMode};
8use crate::geom::divide;
9use crate::geom::{halves, CycleDir, Dir, Rectangle};
10use crate::gesture::GestureEvent;
11use crate::metadata::Info;
12use crate::settings::{FirstColumn, SecondColumn};
13use crate::unit::scale_by_dpi;
14use crate::view::filler::Filler;
15use crate::view::{Bus, Event, Hub, Id, RenderData, RenderQueue, View, ID_FEEDER};
16use crate::view::{BIG_BAR_HEIGHT, THICKNESS_MEDIUM};
17use lazy_static::lazy_static;
18use std::path::PathBuf;
19use std::sync::Mutex;
20use std::thread;
21
22lazy_static! {
23 static ref EXCLUSIVE_ACCESS: Mutex<u8> = Mutex::new(0);
24}
25
26pub struct Shelf {
27 id: Id,
28 pub rect: Rectangle,
29 children: Vec<Box<dyn View>>,
30 pub max_lines: usize,
31 first_column: FirstColumn,
32 second_column: SecondColumn,
33 thumbnail_previews: bool,
34}
35
36impl Shelf {
37 pub fn new(
38 rect: Rectangle,
39 first_column: FirstColumn,
40 second_column: SecondColumn,
41 thumbnail_previews: bool,
42 ) -> Shelf {
43 let dpi = CURRENT_DEVICE.dpi;
44 let big_height = scale_by_dpi(BIG_BAR_HEIGHT, dpi) as i32;
45 let thickness = scale_by_dpi(THICKNESS_MEDIUM, dpi) as i32;
46 let max_lines = ((rect.height() as i32 + thickness) / big_height) as usize;
47 Shelf {
48 id: ID_FEEDER.next(),
49 rect,
50 children: Vec::new(),
51 max_lines,
52 first_column,
53 second_column,
54 thumbnail_previews,
55 }
56 }
57
58 pub fn set_first_column(&mut self, first_column: FirstColumn) {
59 self.first_column = first_column;
60 }
61
62 pub fn set_second_column(&mut self, second_column: SecondColumn) {
63 self.second_column = second_column;
64 }
65
66 pub fn set_thumbnail_previews(&mut self, thumbnail_previews: bool) {
67 self.thumbnail_previews = thumbnail_previews;
68 }
69
70 pub fn update(
71 &mut self,
72 metadata: &[Info],
73 hub: &Hub,
74 rq: &mut RenderQueue,
75 context: &Context,
76 ) {
77 self.children.clear();
78 let dpi = CURRENT_DEVICE.dpi;
79 let big_height = scale_by_dpi(BIG_BAR_HEIGHT, dpi) as i32;
80 let thickness = scale_by_dpi(THICKNESS_MEDIUM, dpi) as i32;
81 let (small_thickness, big_thickness) = halves(thickness);
82 let max_lines = ((self.rect.height() as i32 + thickness) / big_height) as usize;
83 let book_heights = divide(self.rect.height() as i32, max_lines as i32);
84 let mut y_pos = self.rect.min.y;
85 let th = big_height;
86 let tw = 3 * th / 4;
87
88 for (index, info) in metadata.iter().enumerate() {
89 let y_min = y_pos + if index > 0 { big_thickness } else { 0 };
90 let y_max = y_pos + book_heights[index]
91 - if index < max_lines - 1 {
92 small_thickness
93 } else {
94 0
95 };
96
97 let preview_path: Option<PathBuf> = if self.thumbnail_previews {
98 let thumb_path = context.library.thumbnail_preview(&info.file.path);
99 if !thumb_path.exists() {
100 let hub2 = hub.clone();
101 let thumb_path2 = thumb_path.to_string_lossy().into_owned();
102 let path = info.file.path.clone();
103 let full_path = context.library.home.join(&info.file.path);
104 thread::spawn(move || {
105 let _guard = EXCLUSIVE_ACCESS.lock().unwrap();
108 open(full_path)
109 .and_then(|mut doc| {
110 doc.preview_pixmap(
111 tw as f32,
112 th as f32,
113 CURRENT_DEVICE.color_samples(),
114 )
115 })
116 .map(|pixmap| {
117 if pixmap.save(&thumb_path2).is_ok() {
118 hub2.send(Event::RefreshBookPreview(
119 path,
120 Some(PathBuf::from(thumb_path2)),
121 ))
122 .ok();
123 }
124 })
125 });
126 Some(PathBuf::default())
127 } else {
128 Some(thumb_path)
129 }
130 } else {
131 None
132 };
133
134 let book = Book::new(
135 rect![self.rect.min.x, y_min, self.rect.max.x, y_max],
136 info.clone(),
137 index,
138 self.first_column,
139 self.second_column,
140 preview_path,
141 );
142 self.children.push(Box::new(book) as Box<dyn View>);
143
144 if index < max_lines - 1 {
145 let separator = Filler::new(
146 rect![self.rect.min.x, y_max, self.rect.max.x, y_max + thickness],
147 SEPARATOR_NORMAL,
148 );
149 self.children.push(Box::new(separator) as Box<dyn View>);
150 }
151
152 y_pos += book_heights[index];
153 }
154
155 if metadata.len() < max_lines {
156 let y_start = y_pos + if metadata.is_empty() { 0 } else { thickness };
157 let filler = Filler::new(
158 rect![self.rect.min.x, y_start, self.rect.max.x, self.rect.max.y],
159 WHITE,
160 );
161 self.children.push(Box::new(filler) as Box<dyn View>);
162 }
163
164 self.max_lines = max_lines;
165 rq.add(RenderData::new(self.id, self.rect, UpdateMode::Partial));
166 }
167}
168
169impl View for Shelf {
170 #[cfg_attr(feature = "otel", tracing::instrument(skip(self, _hub, bus, _rq, _context), fields(event = ?evt), ret(level=tracing::Level::TRACE)))]
171 fn handle_event(
172 &mut self,
173 evt: &Event,
174 _hub: &Hub,
175 bus: &mut Bus,
176 _rq: &mut RenderQueue,
177 _context: &mut Context,
178 ) -> bool {
179 match *evt {
180 Event::Gesture(GestureEvent::Swipe { dir, start, .. }) if self.rect.includes(start) => {
181 match dir {
182 Dir::West => {
183 bus.push_back(Event::Page(CycleDir::Next));
184 true
185 }
186 Dir::East => {
187 bus.push_back(Event::Page(CycleDir::Previous));
188 true
189 }
190 _ => false,
191 }
192 }
193 _ => false,
194 }
195 }
196
197 #[cfg_attr(feature = "otel", tracing::instrument(skip(self, _fb, _fonts, _rect), fields(rect = ?_rect)))]
198 fn render(&self, _fb: &mut dyn Framebuffer, _rect: Rectangle, _fonts: &mut Fonts) {}
199
200 fn rect(&self) -> &Rectangle {
201 &self.rect
202 }
203
204 fn rect_mut(&mut self) -> &mut Rectangle {
205 &mut self.rect
206 }
207
208 fn children(&self) -> &Vec<Box<dyn View>> {
209 &self.children
210 }
211
212 fn children_mut(&mut self) -> &mut Vec<Box<dyn View>> {
213 &mut self.children
214 }
215
216 fn id(&self) -> Id {
217 self.id
218 }
219}