Skip to content

Commit

Permalink
E-book viewer: Add a button to directly open the viewer help section …
Browse files Browse the repository at this point in the history
…in the calibre user manual to the viewer controls
  • Loading branch information
kovidgoyal committed Oct 24, 2021
1 parent 6c45f43 commit ae43785
Show file tree
Hide file tree
Showing 8 changed files with 45 additions and 7 deletions.
1 change: 1 addition & 0 deletions imgsrc/srv/help.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
7 changes: 7 additions & 0 deletions src/calibre/gui2/viewer/web_view.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
secure_webengine, to_js
)
from calibre.srv.code import get_translations_data
from calibre.utils.localization import localize_user_manual_link
from calibre.utils.serialize import json_loads
from calibre.utils.shared_file import share_open
from polyglot.builtins import as_bytes, iteritems
Expand Down Expand Up @@ -275,6 +276,7 @@ class ViewerBridge(Bridge):
tts = from_js(object, object)
edit_book = from_js(object, object, object)
show_book_folder = from_js()
show_help = from_js(object)

create_view = to_js()
start_book_load = to_js()
Expand Down Expand Up @@ -538,6 +540,7 @@ def __init__(self, parent=None):
self.bridge.highlights_changed.connect(self.highlights_changed)
self.bridge.edit_book.connect(self.edit_book)
self.bridge.show_book_folder.connect(self.show_book_folder)
self.bridge.show_help.connect(self.show_help)
self.bridge.open_url.connect(safe_open_url)
self.bridge.speak_simple_text.connect(self.tts.speak_simple_text)
self.bridge.tts.connect(self.tts.action)
Expand Down Expand Up @@ -744,5 +747,9 @@ def show_book_folder(self):
path = os.path.dirname(os.path.abspath(set_book_path.pathtoebook))
safe_open_url(QUrl.fromLocalFile(path))

def show_help(self, which):
if which == 'viewer':
safe_open_url(localize_user_manual_link('https://manual.calibre-ebook.com/viewer.html'))

def repair_after_fullscreen_switch(self):
self.execute_when_ready('repair_after_fullscreen_switch')
5 changes: 3 additions & 2 deletions src/calibre/srv/code.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
from calibre.utils.config import prefs, tweaks
from calibre.utils.icu import numeric_sort_key, sort_key
from calibre.utils.localization import (
get_lang, lang_map_for_ui, localize_website_link
get_lang, lang_map_for_ui, localize_website_link, lang_code_for_user_manual
)
from calibre.utils.search_query_parser import ParseException
from calibre.utils.serialize import json_dumps
Expand Down Expand Up @@ -156,7 +156,8 @@ def basic_interface_data(ctx, rd):
'search_the_net_urls': getattr(ctx, 'search_the_net_urls', None) or [],
'num_per_page': rd.opts.num_per_page,
'default_book_list_mode': rd.opts.book_list_mode,
'donate_link': localize_website_link('https://calibre-ebook.com/donate')
'donate_link': localize_website_link('https://calibre-ebook.com/donate'),
'lang_code_for_user_manual': lang_code_for_user_manual(),
}
ans['library_map'], ans['default_library_id'] = ctx.library_info(rd)
return ans
Expand Down
11 changes: 9 additions & 2 deletions src/calibre/utils/localization.py
Original file line number Diff line number Diff line change
Expand Up @@ -528,12 +528,19 @@ def user_manual_stats():
return stats


def localize_user_manual_link(url):
def lang_code_for_user_manual():
lc = lang_as_iso639_1(get_lang())
if lc == 'en':
return url
return ''
stats = user_manual_stats()
if stats.get(lc, 0) < 0.3:
return ''
return lc


def localize_user_manual_link(url):
lc = lang_code_for_user_manual()
if not lc:
return url
from polyglot.urllib import urlparse, urlunparse
parts = urlparse(url)
Expand Down
14 changes: 11 additions & 3 deletions src/pyj/read_book/overlay.pyj
Original file line number Diff line number Diff line change
Expand Up @@ -431,9 +431,17 @@ class MainOverlay: # {{{
style='position: fixed; width: 100%; bottom: 0; display: flex; justify-content: space-between; align-items: center;'
'user-select: none; background-color: {}'.format(get_color('window-background')),
E.div(
style='display: flex; align-items: center; cursor: pointer; padding: 0.5ex 1rem', class_='main-overlay-button',
title=_('Close the viewer controls'),
svgicon('close', icon_size, icon_size), '\xa0', _('Close')
style='display: flex; justify-content: space-between; align-items: center;',
E.div(
style='display: flex; align-items: center; cursor: pointer; padding: 0.5ex 1rem', class_='main-overlay-button',
title=_('Close the viewer controls'),
svgicon('close', icon_size, icon_size), '\xa0', _('Close')
),
E.div(
style='display: flex; align-items: center; cursor: pointer; padding: 0.5ex 1rem', class_='main-overlay-button',
svgicon('help', icon_size, icon_size), '\xa0', _('Help'), onclick=def(ev):
ui_operations.show_help('viewer')
)
),
E.div(style='padding: 0.5ex 1rem', '\xa0'), E.div(style='padding: 0.5ex 1rem', '\xa0'),
),
Expand Down
11 changes: 11 additions & 0 deletions src/pyj/read_book/ui.pyj
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ from read_book.db import get_db
from read_book.globals import ui_operations
from read_book.tts import Client
from read_book.view import View
from session import get_interface_data
from utils import debounce, full_screen_element, human_readable, request_full_screen
from widgets import create_button

Expand Down Expand Up @@ -92,6 +93,16 @@ class ReadUI:
ui_operations.find_next = self.view.search_overlay.find_next
ui_operations.open_url = def(url):
window.open(url, '_blank')
ui_operations.show_help = def(which):
if which is 'viewer':
path = '/viewer.html'
else:
return
if get_interface_data().lang_code_for_user_manual:
path = f'/{get_interface_data().lang_code_for_user_manual}{path}'
url = 'https://manual.calibre-ebook.com' + path
window.open(url, '_blank')

ui_operations.copy_selection = def(text, html):
# try using document.execCommand which on chrome allows the
# copy on non-secure origin if this is close to a user
Expand Down
1 change: 1 addition & 0 deletions src/pyj/session.pyj
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,7 @@ default_interface_data = {
'icon_path': '',
'custom_list_template': None,
'num_per_page': 50,
'lang_code_for_user_manual': '',
}

def get_interface_data():
Expand Down
2 changes: 2 additions & 0 deletions src/pyj/viewer-main.pyj
Original file line number Diff line number Diff line change
Expand Up @@ -429,6 +429,8 @@ if window is window.top:
to_python.edit_book(spine_name, frac, selected_text or '')
ui_operations.show_book_folder = def():
to_python.show_book_folder()
ui_operations.show_help = def(which):
to_python.show_help(which)

document.body.appendChild(E.div(id='view'))
window.onerror = onerror
Expand Down

0 comments on commit ae43785

Please sign in to comment.