E-book viewer: Add a toolbar icon for Read aloud

This commit is contained in:
Kovid Goyal 2020-12-12 09:04:22 +05:30
parent 66abe58c33
commit c8015e824c
No known key found for this signature in database
GPG Key ID: 06BC317B515ACE7C
7 changed files with 112 additions and 0 deletions

85
imgsrc/bullhorn.svg Normal file
View File

@ -0,0 +1,85 @@
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<svg
xmlns:dc="http://purl.org/dc/elements/1.1/"
xmlns:cc="http://creativecommons.org/ns#"
xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
xmlns:svg="http://www.w3.org/2000/svg"
xmlns="http://www.w3.org/2000/svg"
xmlns:xlink="http://www.w3.org/1999/xlink"
xmlns:sodipodi="http://sodipodi.sourceforge.net/DTD/sodipodi-0.dtd"
xmlns:inkscape="http://www.inkscape.org/namespaces/inkscape"
width="1792"
height="1792"
viewBox="0 0 1792 1792"
version="1.1"
id="svg4"
sodipodi:docname="bullhorn.svg"
inkscape:version="1.0.1 (3bc2e813f5, 2020-09-07)">
<metadata
id="metadata10">
<rdf:RDF>
<cc:Work
rdf:about="">
<dc:format>image/svg+xml</dc:format>
<dc:type
rdf:resource="http://purl.org/dc/dcmitype/StillImage" />
</cc:Work>
</rdf:RDF>
</metadata>
<defs
id="defs8">
<linearGradient
inkscape:collect="always"
id="linearGradient845">
<stop
style="stop-color:#2caf45;stop-opacity:1"
offset="0"
id="stop841" />
<stop
style="stop-color:#2caf45;stop-opacity:1;"
offset="0.43081748"
id="stop849" />
<stop
style="stop-color:#2271d5;stop-opacity:1"
offset="0.43215907"
id="stop851" />
<stop
style="stop-color:#2271d5;stop-opacity:1"
offset="1"
id="stop843" />
</linearGradient>
<linearGradient
inkscape:collect="always"
xlink:href="#linearGradient845"
id="linearGradient847"
x1="0"
y1="896.61688"
x2="1792"
y2="896.61688"
gradientUnits="userSpaceOnUse" />
</defs>
<sodipodi:namedview
pagecolor="#ffffff"
bordercolor="#666666"
borderopacity="1"
objecttolerance="10"
gridtolerance="10"
guidetolerance="10"
inkscape:pageopacity="0"
inkscape:pageshadow="2"
inkscape:window-width="2560"
inkscape:window-height="1400"
id="namedview6"
showgrid="false"
inkscape:zoom="0.5625"
inkscape:cx="381.33333"
inkscape:cy="912"
inkscape:window-x="0"
inkscape:window-y="40"
inkscape:window-maximized="0"
inkscape:current-layer="svg4" />
<path
d="M1664 640q53 0 90.5 37.5t37.5 90.5-37.5 90.5-90.5 37.5v384q0 52-38 90t-90 38q-417-347-812-380-58 19-91 66t-31 100.5 40 92.5q-20 33-23 65.5t6 58 33.5 55 48 50 61.5 50.5q-29 58-111.5 83t-168.5 11.5-132-55.5q-7-23-29.5-87.5t-32-94.5-23-89-15-101 3.5-98.5 22-110.5h-122q-66 0-113-47t-47-113v-192q0-66 47-113t113-47h480q435 0 896-384 52 0 90 38t38 90v384zm-128 604v-954q-394 302-768 343v270q377 42 768 341z"
id="path2"
style="stroke:none;stroke-opacity:0.40255955;fill:url(#linearGradient847);fill-opacity:1" />
</svg>

After

Width:  |  Height:  |  Size: 2.8 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.6 KiB

View File

@ -65,6 +65,7 @@ def all_actions():
'print': Action('print.png', _('Print book'), 'print'),
'preferences': Action('config.png', _('Preferences'), 'preferences'),
'metadata': Action('metadata.png', _('Show book metadata'), 'metadata'),
'toggle_read_aloud': Action('bullhorn.png', _('Read aloud'), 'toggle_read_aloud'),
'toggle_highlights': Action('highlight_only_on.png', _('Browse highlights in book'), 'toggle_highlights'),
}
all_actions.ans = Actions(amap)
@ -136,6 +137,7 @@ class ActionsToolBar(ToolBar):
web_view.reference_mode_changed.connect(self.update_reference_mode_action)
web_view.standalone_misc_settings_changed.connect(self.update_visibility)
web_view.autoscroll_state_changed.connect(self.update_autoscroll_action)
web_view.read_aloud_state_changed.connect(self.update_read_aloud_action)
web_view.customize_toolbar.connect(self.customize, type=Qt.ConnectionType.QueuedConnection)
web_view.view_created.connect(self.on_view_created)
@ -171,6 +173,8 @@ class ActionsToolBar(ToolBar):
a.setCheckable(True)
self.toggle_highlights_action = self.highlights_action = a = shortcut_action('toggle_highlights')
a.setCheckable(True)
self.toggle_read_aloud_action = a = shortcut_action('toggle_read_aloud')
a.setCheckable(True)
self.lookup_action = a = shortcut_action('lookup')
a.setCheckable(True)
self.inspector_action = a = shortcut_action('inspector')
@ -178,6 +182,7 @@ class ActionsToolBar(ToolBar):
self.autoscroll_action = a = shortcut_action('autoscroll')
a.setCheckable(True)
self.update_autoscroll_action(False)
self.update_read_aloud_action(False)
self.chrome_action = shortcut_action('chrome')
self.mode_action = a = shortcut_action('mode')
@ -223,6 +228,11 @@ class ActionsToolBar(ToolBar):
self.autoscroll_action.setToolTip(
_('Turn off auto-scrolling') if active else _('Turn on auto-scrolling'))
def update_read_aloud_action(self, active):
self.toggle_read_aloud_action.setChecked(active)
self.autoscroll_action.setToolTip(
_('Stop reading') if active else _('Read the text of the book aloud'))
def update_reference_mode_action(self, enabled):
self.reference_action.setChecked(enabled)

View File

@ -251,6 +251,7 @@ class ViewerBridge(Bridge):
ask_for_open = from_js(object)
selection_changed = from_js(object, object)
autoscroll_state_changed = from_js(object)
read_aloud_state_changed = from_js(object)
copy_selection = from_js(object, object)
view_image = from_js(object)
copy_image = from_js(object)
@ -452,6 +453,7 @@ class WebView(RestartingWebEngineView):
ask_for_open = pyqtSignal(object)
selection_changed = pyqtSignal(object, object)
autoscroll_state_changed = pyqtSignal(object)
read_aloud_state_changed = pyqtSignal(object)
view_image = pyqtSignal(object)
copy_image = pyqtSignal(object)
overlay_visibility_changed = pyqtSignal(object)
@ -508,6 +510,7 @@ class WebView(RestartingWebEngineView):
self.bridge.ask_for_open.connect(self.ask_for_open)
self.bridge.selection_changed.connect(self.selection_changed)
self.bridge.autoscroll_state_changed.connect(self.autoscroll_state_changed)
self.bridge.read_aloud_state_changed.connect(self.read_aloud_state_changed)
self.bridge.view_image.connect(self.view_image)
self.bridge.copy_image.connect(self.copy_image)
self.bridge.overlay_visibility_changed.connect(self.overlay_visibility_changed)

View File

@ -69,12 +69,16 @@ class ReadAloud:
self.state = HIDDEN
self.container.style.display = 'none'
self.view.focus_iframe()
if ui_operations.read_aloud_state_changed:
ui_operations.read_aloud_state_changed(False)
def show(self):
if self.state is HIDDEN:
self.container.style.display = 'block'
self.state = STOPPED
self.focus()
if ui_operations.read_aloud_state_changed:
ui_operations.read_aloud_state_changed(True)
def focus(self):
self.container.focus()

View File

@ -507,6 +507,8 @@ class View:
self.toggle_reference_mode()
elif data.name is 'read_aloud':
self.start_read_aloud()
elif data.name is 'toggle_read_aloud':
self.toggle_read_aloud()
elif data.name is 'reload_book':
ui_operations.reload_book()
elif data.name is 'next_section':
@ -680,6 +682,12 @@ class View:
if not dont_start_talking:
self.read_aloud.play()
def toggle_read_aloud(self):
if self.read_aloud.is_visible:
self.read_aloud.hide()
else:
self.start_read_aloud()
def show_chrome(self, data):
elements = {}
if data and data.elements:

View File

@ -394,6 +394,8 @@ if window is window.top:
to_python.customize_toolbar()
ui_operations.autoscroll_state_changed = def(active):
to_python.autoscroll_state_changed(active)
ui_operations.read_aloud_state_changed = def(active):
to_python.read_aloud_state_changed(active)
ui_operations.search_result_not_found = def(sr):
to_python.search_result_not_found(sr)
ui_operations.scrollbar_context_menu = def(x, y, frac):