Debian Codegraph @

src/pyj/read_book/read_aloud.pyj

raw
1 # vim:fileencoding=utf-8
2 # License: GPL v3 Copyright: 2020, 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 book_list.globals import get_session_data
9 from book_list.globals import get_session_data
10 from book_list.theme import get_color
11 from dom import clear, svgicon, unique_id
12 from read_book.globals import ui_operations
13 from read_book.highlights import ICON_SIZE
14 from read_book.selection_bar import BUTTON_MARGIN, get_margins, map_to_iframe_coords
15 from read_book.shortcuts import shortcut_for_key_event
16
17 HIDDEN = 0
18 WAITING_FOR_PLAY_TO_START = 1
19 PAUSED = 2
20 PLAYING = 3
21 STOPPED = 4
22
23
24 def is_flow_mode():
25 sd = get_session_data()
26 mode = sd.get('read_mode')
27 return mode is 'flow'
28
29
30 def bar_class_and_position():
31 sd = get_session_data()
32 bp = sd.get('tts_bar_position')
33 iclass = 'floating' if 'float' in bp else 'docked'
34 return iclass, bp
35
36
37 class ReadAloud:
38 dont_hide_on_content_loaded = True
39 prevent_navigation = True
40
41 def __init__(self, view):
42 self.view = view
43 self._state = HIDDEN
44 self.bar_id = unique_id('bar')
45 container = self.container
46 container.setAttribute('tabindex', '0')
47 container.style.overflow = 'hidden'
48 container.appendChild(E.div(id=self.bar_id))
49 container.appendChild(E.style(
50 f'#{self.bar_id}.floating' + '{ border: solid 1px currentColor; border-radius: 5px; display: flex;' +
51 ' flex-direction: column; margin: 1rem; }\n\n',
52 f'#{self.bar_id}.docked' + '{ border-radius: 1em; height: 2em; padding:0.5em; display: flex; justify-content: center; align-items: center; }\n\n',
53 f'#{self.bar_id}.speaking ' + '{ opacity: 0.5 }\n\n',
54 f'#{self.bar_id}.speaking:hover ' + '{ opacity: 1.0 }\n\n',
55 ))
56 container.addEventListener('keydown', self.on_keydown, {'passive': False})
57 container.addEventListener('click', self.container_clicked, {'passive': False})
58 container.addEventListener('contextmenu', self.toggle, {'passive': False})
59
60 @property
61 def container(self):
62 return document.getElementById('book-read-aloud-overlay')
63
64 @property
65 def bar(self):
66 return document.getElementById(self.bar_id)
67
68 @property
69 def is_visible(self):
70 return self.container.style.display is not 'none'
71
72 @property
73 def state(self):
74 return self._state
75
76 @state.setter
77 def state(self, val):
78 if val is not self._state:
79 self._state = val
80 self.build_bar()
81
82 def hide(self):
83 if self.state is not HIDDEN:
84 ui_operations.tts('stop')
85 self.state = HIDDEN
86 self.container.style.display = 'none'
87 self.view.focus_iframe()
88 if ui_operations.read_aloud_state_changed:
89 ui_operations.read_aloud_state_changed(False)
90
91 def show(self):
92 if self.state is HIDDEN:
93 self.container.style.display = 'flex'
94 self.state = STOPPED
95 self.focus()
96 if ui_operations.read_aloud_state_changed:
97 ui_operations.read_aloud_state_changed(True)
98
99 def focus(self):
100 self.container.focus()
101
102 def build_docked_bar(self, bar_container, bar_position):
103 container = self.container
104 container.style.alignItems = 'flex-end' if 'bottom' in bar_position else 'flex-start'
105 container.style.justifyContent = 'flex-end' if 'right' in bar_position else ('flex-start' if 'left' in bar_position else 'center')
106 self.create_buttons(bar_container)
107
108 def create_buttons(self, bar):
109 def cb(name, icon, text):
110 ans = svgicon(icon, ICON_SIZE, ICON_SIZE, text)
111 if name:
112 ans.addEventListener('click', def (ev):
113 ev.stopPropagation(), ev.preventDefault()
114 self[name](ev)
115 self.view.focus_iframe()
116 )
117 ans.classList.add('simple-link')
118 ans.style.marginLeft = ans.style.marginRight = BUTTON_MARGIN
119 return ans
120
121 if self.state is PLAYING:
122 bar.appendChild(cb('pause', 'pause', _('Pause reading')))
123 elif self.state is WAITING_FOR_PLAY_TO_START:
124 bar.appendChild(cb(None, 'hourglass', _('Pause reading')))
125 else:
126 bar.appendChild(cb('play', 'play', _('Start reading') if self.state is STOPPED else _('Resume reading')))
127 bar.appendChild(cb('slower', 'slower', _('Slow down speech')))
128 bar.appendChild(cb('faster', 'faster', _('Speed up speech')))
129 bar.appendChild(cb('configure', 'cogs', _('Configure Read aloud')))
130 bar.appendChild(cb('hide', 'off', _('Close Read aloud')))
131
132 def build_bar(self):
133 if self.state is HIDDEN:
134 return
135 bar_container = self.bar
136 bar_container.classList.remove('floating')
137 bar_container.classList.remove('docked')
138 iclass, bp = bar_class_and_position()
139 bar_container.classList.add(iclass)
140 bar_container.style.maxWidth = 'min(40rem, 80vw)'
141 bar_container.style.backgroundColor = get_color('window-background')
142 if self.state is PLAYING:
143 bar_container.classList.add('speaking')
144 else:
145 bar_container.classList.remove('speaking')
146 clear(bar_container)
147
148 if iclass is not 'floating':
149 return self.build_docked_bar(bar_container, bp)
150
151 container = self.container
152 container.style.alignItems = 'flex-end' if is_flow_mode() else 'flex-start'
153 container.style.justifyContent = 'flex-end'
154 for x in [
155 E.div(style='height: 4ex; display: flex; align-items: center; padding: 5px; justify-content: center'),
156 E.hr(style='border-top: solid 1px; margin: 0; padding: 0; display: none'),
157 E.div(
158 style='display: none; padding: 5px; font-size: smaller',
159 E.div()
160 )
161 ]:
162 bar_container.appendChild(x)
163 self.create_buttons(bar_container.firstChild)
164
165 if self.state is not WAITING_FOR_PLAY_TO_START:
166 notes_container = bar_container.lastChild
167 notes_container.style.display = notes_container.previousSibling.style.display = 'block'
168 notes_container = notes_container.lastChild
169 if self.state is STOPPED:
170 notes_container.textContent = _('Tap/click on a word to start from there')
171 elif self.state is PLAYING:
172 notes_container.textContent = _('Tap/click on a word to skip to it')
173 else:
174 notes_container.textContent = _('Tap/click on a word to continue from there')
175
176 def configure(self):
177 self.pause()
178 self.waiting_for_configure = True
179 ui_operations.tts('configure')
180
181 def slower(self):
182 ui_operations.tts('slower')
183
184 def faster(self):
185 ui_operations.tts('faster')
186
187 def play(self):
188 if self.state is PAUSED:
189 ui_operations.tts('resume_after_configure' if self.waiting_for_configure else 'resume')
190 self.waiting_for_configure = False
191 self.state = PLAYING
192 elif self.state is STOPPED:
193 self.send_message('play')
194 self.state = WAITING_FOR_PLAY_TO_START
195
196 def pause(self):
197 if self.state is PLAYING:
198 ui_operations.tts('pause')
199 self.state = PAUSED
200
201 def stop(self):
202 if self.state is PLAYING or self.state is PAUSED:
203 ui_operations.tts('stop')
204 self.state = STOPPED
205
206 def toggle(self):
207 if self.state is PLAYING:
208 self.pause()
209 elif self.state is PAUSED or self.state is STOPPED:
210 self.play()
211
212 def container_clicked(self, ev):
213 if ev.button is not 0:
214 return
215 ev.stopPropagation(), ev.preventDefault()
216 margins = get_margins()
217 pos = {'x': ev.clientX, 'y': ev.clientY}
218 pos = map_to_iframe_coords(pos, margins)
219 self.send_message('play', pos=pos)
220
221 def on_keydown(self, ev):
222 ev.stopPropagation(), ev.preventDefault()
223 if ev.key is 'Escape':
224 self.hide()
225 return
226 if ev.key is ' ' or ev.key is 'MediaPlayPause' or ev.key is 'PlayPause':
227 self.toggle()
228 return
229 if ev.key is 'Play' or ev.key is 'MediaPlay':
230 self.play()
231 return
232 if ev.key is 'Pause' or ev.key is 'MediaPause':
233 self.pause()
234 return
235 if ev.key is 'MediaStop':
236 self.stop()
237 return
238 sc_name = shortcut_for_key_event(ev, self.view.keyboard_shortcut_map)
239 if not sc_name:
240 return
241 if sc_name is 'show_chrome':
242 self.hide()
243 elif sc_name is 'quit':
244 self.hide()
245 ui_operations.quit()
246
247 def handle_tts_event(self, which, data):
248 if which is 'mark':
249 self.send_message('mark', num=data)
250 elif which is 'begin':
251 self.state = PLAYING
252 elif which is 'pause':
253 self.state = PAUSED
254 elif which is 'resume':
255 self.state = PLAYING
256 elif which is 'end':
257 self.state = STOPPED
258 self.view.show_next_spine_item()
259 elif which is 'configured':
260 self.focus()
261 if self.waiting_for_configure:
262 self.play()
263 if data is not None:
264 pass
265
266 def send_message(self, type, **kw):
267 self.view.iframe_wrapper.send_message('tts', type=type, **kw)
268
269 def handle_message(self, msg):
270 if msg.type is 'text-extracted':
271 if msg.pos:
272 self.stop()
273 has_words = False
274 for x in msg.marked_text:
275 if jstype(x) is 'number':
276 has_words = True
277 break
278 if has_words:
279 ui_operations.tts('play', {'marked_text': msg.marked_text})
280 else:
281 # nothing to speak in this file (e.g. a full page image) so
282 # skip to the next spine item, see https://bugs.launchpad.net/bugs/2165923
283 self.state = STOPPED
284 self.view.show_next_spine_item()