Debian Codegraph @

src/pyj/image_popup.pyj

raw
1 # vim:fileencoding=utf-8
2 # License: GPL v3 Copyright: 2023, Kovid Goyal <kovid at kovidgoyal.net>
3 from __python__ import bound_methods, hash_literals
4
5 from elementmaker import E
6 from gettext import gettext as _
7
8 from ajax import absolute_path
9 from dom import add_extra_css, svgicon, unique_id
10 from popups import MODAL_Z_INDEX
11 from read_book.flow_mode import FlickAnimator
12 from read_book.touch import TouchHandler, install_handlers
13 from utils import debounce
14
15 add_extra_css(def ():
16 # Modern, minimal "glass" image viewer styling
17 css = ''
18 css += '.calibre-image-popup { background: rgba(10, 10, 12, 0.92); color: rgba(255,255,255,0.92); }\n'
19 css += '.calibre-image-popup canvas { outline: none; }\n'
20 css += '.calibre-image-popup .cs-image-toolbar {'
21 css += ' position: fixed; top: 0; left: 0; width: 100%;'
22 css += ' box-sizing: border-box; display: flex; justify-content: space-between; align-items: center;'
23 css += ' padding: calc(env(safe-area-inset-top, 0px) + 10px) 12px 10px 12px;'
24 css += ' background: rgba(20,20,24,0.55);'
25 css += ' border-bottom: var(--calibre-border-style-subtle);'
26 css += ' backdrop-filter: blur(14px); -webkit-backdrop-filter: blur(14px);'
27 css += ' }\n'
28 css += '.calibre-image-popup a {'
29 css += ' display: inline-flex; align-items: center; justify-content: center;'
30 css += ' gap: 0.4rem;'
31 css += ' padding: 0.55rem 0.75rem;'
32 css += ' border-radius: 999px;'
33 css += ' cursor: pointer; user-select: none;'
34 css += ' background: rgba(255,255,255,0.10);'
35 css += ' border: var(--calibre-border-style-subtle);'
36 css += ' color: inherit;'
37 css += ' }\n'
38 css += '.calibre-image-popup a:hover { background: rgba(255,255,255,0.16); }\n'
39 css += '.calibre-image-popup a:active { transform: scale(0.98); }\n'
40 css += '.calibre-image-popup a:focus { outline: none; box-shadow: 0 0 0 3px rgba(108,180,238,0.35); }\n'
41 css += '.calibre-image-popup svg { width: 2.1ex; height: 2.1ex; }\n'
42 css += '.calibre-image-popup.fit-to-window a.fit-to-window {'
43 css += ' background: rgba(108,180,238,0.28);'
44 css += ' border-color: rgba(108,180,238,0.38);'
45 css += ' }\n'
46 css += '@media (max-width: 720px) {'
47 css += ' .calibre-image-popup .cs-image-toolbar { padding-left: 10px; padding-right: 10px; }'
48 css += ' .calibre-image-popup a { padding: 0.65rem 0.9rem; }'
49 css += ' }\n'
50 return css
51 )
52
53
54 def fit_image(width, height, pwidth, pheight):
55 '''
56 Fit image in box of width pwidth and height pheight.
57 @param width: Width of image
58 @param height: Height of image
59 @param pwidth: Width of box
60 @param pheight: Height of box
61 @return: scaled, new_width, new_height. scaled is True iff new_width and/or new_height is different from width or height.
62 '''
63 scaled = height > pheight or width > pwidth
64 if height > pheight:
65 corrf = pheight / float(height)
66 width, height = Math.floor(corrf * width), pheight
67 if width > pwidth:
68 corrf = pwidth / float(width)
69 width, height = pwidth, Math.floor(corrf * height)
70 if height > pheight:
71 corrf = pheight / float(height)
72 width, height = Math.floor(corrf * width), pheight
73
74 return scaled, int(width), int(height)
75
76
77 class ImagePopup(TouchHandler):
78 def __init__(self):
79 TouchHandler.__init__(self)
80 self.flick_animator = fa = FlickAnimator()
81 fa.scroll_x = self.scroll_x
82 fa.scroll_y = self.scroll_y
83 fa.cancel_current_drag_scroll = def (): pass
84 self._container = None
85 self.container_id = unique_id('image-popup')
86 self.img = None
87 self.img_loading = False
88 self.img_ok = False
89
90 @property
91 def container(self):
92 if self._container is None:
93 self._container = E.div(
94 style=(
95 f'display:none; position:absolute; left:0; top: 0; z-index: {MODAL_Z_INDEX};'
96 + ' width: 100vw; height: 100vh; padding: 0; margin: 0; border-width: 0; box-sizing: border-box'
97 ),
98 id=self.container_id,
99 tabindex='0',
100 class_='calibre-image-popup',
101 onkeydown=self.onkeydown,
102 onwheel=self.onwheel,
103 E.div(
104 class_='cs-image-toolbar',
105 E.a(svgicon('fit-to-screen'), title=_('Fit image to screen'), class_='fit-to-window', onclick=self.toggle_fit_to_window,),
106 E.a(svgicon('close'), title=_('Close'), onclick=self.hide_container),
107 ),
108 E.canvas(
109 width=window.innerWidth + '', height=window.innerHeight + '',
110 style='width: 100%; height: 100%; margin: 0; padding: 0; border-width: 0; box-sizing: border-box; display: block',
111 aria_label='Popup view of image',
112 )
113 )
114 document.body.appendChild(self._container)
115 install_handlers(self.canvas, self)
116 window.addEventListener('resize', debounce(self.resize_canvas, 250))
117 return self._container
118
119 def onwheel(self, ev):
120 ev.stopPropagation(), ev.preventDefault()
121 dy = ev.deltaY
122 dx = ev.deltaX
123 if ev.deltaMode is window.WheelEvent.DOM_DELTA_LINE:
124 dy *= 20
125 dx *= 20
126 self.scroll_y(dy)
127 self.scroll_x(dx)
128
129 def onkeydown(self, ev):
130 ev.preventDefault(), ev.stopPropagation()
131 if ev.key is ' ':
132 return self.toggle_fit_to_window()
133 if ev.key is 'Escape':
134 return self.hide_container()
135 if ev.key is 'ArrowDown':
136 return self.scroll_down((window.innerHeight - 10) if ev.getModifierState('Control') else 10)
137 if ev.key is 'PageDown':
138 return self.scroll_down(window.innerHeight - 10)
139 if ev.key is 'ArrowUp':
140 return self.scroll_up((window.innerHeight - 10) if ev.getModifierState('Control') else 10)
141 if ev.key is 'PageUp':
142 return self.scroll_up(window.innerHeight - 10)
143 if ev.key is 'ArrowLeft':
144 return self.scroll_left((window.innerWidth - 10) if ev.getModifierState('Control') else 10)
145 if ev.key is 'ArrowRight':
146 return self.scroll_right((window.innerWidth - 10) if ev.getModifierState('Control') else 10)
147
148 def handle_gesture(self, gesture):
149 self.flick_animator.stop()
150 if gesture.type is 'swipe':
151 if not self.img_ok or self.fit_to_window:
152 return
153 if gesture.points.length > 1 and not gesture.is_held:
154 delta = gesture.points[-2] - gesture.points[-1]
155 if Math.abs(delta) >= 1:
156 if gesture.axis is 'vertical':
157 self.scroll_y(delta)
158 else:
159 self.scroll_x(delta)
160 if not gesture.active and not gesture.is_held:
161 self.flick_animator.start(gesture)
162 if gesture.type is 'pinch' and self.img_ok:
163 if gesture.direction is 'in':
164 self.fit_to_window = True
165 else:
166 self.fit_to_window = False
167 self.update_canvas()
168
169 @property
170 def vertical_max_bounce_distance(self):
171 return Math.min(60, window.innerHeight / 2)
172
173 @property
174 def horizontal_max_bounce_distance(self):
175 return Math.min(60, window.innerWidth / 2)
176
177 def scroll_down(self, amt):
178 if self.img_ok and not self.fit_to_window:
179 canvas_height = self.canvas_height
180 if self.img.height <= canvas_height:
181 return
182 miny = canvas_height - self.img.height - self.vertical_max_bounce_distance
183 y = Math.max(self.y - amt, miny)
184 if y is not self.y:
185 self.y = y
186 self.update_canvas()
187
188 def scroll_up(self, amt):
189 if self.img_ok and not self.fit_to_window:
190 canvas_height = self.canvas_height
191 if self.img.height <= canvas_height:
192 return
193 y = Math.min(self.y + amt, self.vertical_max_bounce_distance)
194 if y is not self.y:
195 self.y = y
196 self.update_canvas()
197
198 def scroll_right(self, amt):
199 if self.img_ok and not self.fit_to_window:
200 canvas_width = self.canvas_width
201 if self.img.width <= canvas_width:
202 return
203 minx = canvas_width - self.img.width - self.horizontal_max_bounce_distance
204 x = Math.max(self.x - amt, minx)
205 if x is not self.x:
206 self.x = x
207 self.update_canvas()
208
209 def scroll_left(self, amt):
210 if self.img_ok and not self.fit_to_window:
211 canvas_width = self.canvas_width
212 if self.img.width <= canvas_width:
213 return
214 x = Math.min(self.x + amt, self.horizontal_max_bounce_distance)
215 if x is not self.x:
216 self.x = x
217 self.update_canvas()
218
219 def scroll_y(self, amt):
220 if amt < 0:
221 self.scroll_up(-amt)
222 elif amt > 0:
223 self.scroll_down(amt)
224
225 def scroll_x(self, amt):
226 if amt < 0:
227 self.scroll_left(-amt)
228 elif amt > 0:
229 self.scroll_right(amt)
230
231 @property
232 def canvas(self):
233 return self.container.getElementsByTagName('canvas')[0]
234
235 @property
236 def fit_to_window(self):
237 return self.container.classList.contains('fit-to-window')
238
239 @fit_to_window.setter
240 def fit_to_window(self, val):
241 c = self.container
242 if val:
243 c.classList.add('fit-to-window')
244 c.querySelector('a.fit-to-window').title = _('Do not fit image to window')
245 else:
246 c.classList.remove('fit-to-window')
247 c.querySelector('a.fit-to-window').title = _('Fit image to window')
248
249 def resize_canvas(self):
250 c = self.canvas
251 dpr = Math.max(1, window.devicePixelRatio)
252 c.width = Math.ceil(window.innerWidth * dpr)
253 c.height = Math.ceil(window.innerHeight * dpr)
254 c.style.width = (c.width / dpr) + 'px'
255 c.style.height = (c.height / dpr) + 'px'
256 ctx = c.getContext('2d')
257 ctx.setTransform(1, 0, 0, 1, 0, 0)
258 ctx.scale(dpr, dpr)
259 ctx.fillStyle = ctx.strokeStyle = '#eee'
260 ctx.font = '16px sans-serif'
261 self.update_canvas()
262
263 def show_container(self):
264 self.flick_animator.stop()
265 self.container.style.display = 'block'
266
267 def hide_container(self):
268 self.flick_animator.stop()
269 self.container.style.display = 'none'
270
271 def show_url(self, url):
272 self.img = Image()
273 self.img.addEventListener('load', self.image_loaded)
274 self.img.addEventListener('error', self.image_failed)
275 self.img_loading = True
276 self.x = self.y = 0
277 self.img_ok = False
278 self.fit_to_window = True
279 self.img.src = url
280 self.show_container()
281 self.resize_canvas()
282
283 @property
284 def canvas_width(self):
285 return self.canvas.width / Math.max(1, window.devicePixelRatio)
286
287 @property
288 def canvas_height(self):
289 return self.canvas.height / Math.max(1, window.devicePixelRatio)
290
291 def update_canvas(self):
292 canvas = self.canvas
293 ctx = canvas.getContext('2d')
294 ctx.clearRect(0, 0, canvas.width, canvas.height)
295 canvas_width, canvas_height = self.canvas_width, self.canvas_height
296
297 def draw_centered_text(text):
298 tm = ctx.measureText(text)
299 x = Math.max(0, (canvas_width - tm.width) / 2)
300 y = (canvas_height - 16) / 2
301 ctx.fillText(text, x, y)
302
303 if self.img_loading:
304 draw_centered_text(_('Loading image, please wait…'))
305 return
306 if not self.img_ok:
307 draw_centered_text(_('Loading the image failed'))
308 return
309
310 def draw_full_image_fit_to_canvas():
311 scaled, width, height = fit_image(self.img.width, self.img.height, canvas_width, canvas_height)
312 scaled
313 x = (canvas_width - width) / 2
314 y = (canvas_height - height) / 2
315 ctx.drawImage(self.img, x, y, width, height)
316
317 if self.fit_to_window:
318 draw_full_image_fit_to_canvas()
319 return
320
321 ctx.drawImage(self.img, self.x, self.y)
322
323 def toggle_fit_to_window(self):
324 self.fit_to_window ^= True
325 self.x = self.y = 0
326 self.update_canvas()
327
328 def image_loaded(self):
329 self.img_loading = False
330 self.img_ok = True
331 self.update_canvas()
332
333 def image_failed(self):
334 self.img_loading = False
335 self.img_ok = False
336 self.update_canvas()
337
338
339 popup = None
340
341
342 def show_image(url):
343 nonlocal popup
344 if popup is None:
345 popup = ImagePopup()
346 popup.show_url(url)
347
348
349 def develop(container):
350 show_image(absolute_path('get/cover/1698'))