Viewer: Add a keyboard shortcut ; to go to a book location or position

This commit is contained in:
Kovid Goyal 2019-11-03 18:23:41 +05:30
parent 95adbb3093
commit c6c7400105
No known key found for this signature in database
GPG Key ID: 06BC317B515ACE7C
4 changed files with 36 additions and 42 deletions

View File

@ -58,16 +58,12 @@ def create_location_overlay(current_position_data, overlay, container):
container_id = ensure_id(container) container_id = ensure_id(container)
container.appendChild(E.div(style='margin: 0 1rem')) container.appendChild(E.div(style='margin: 0 1rem'))
container = container.lastChild container = container.lastChild
container.appendChild(E.div(
style='padding-top: 1rem',
E.h4(_('Using book location (most accurate)'))
))
current_cfi = current_position_data.cfi current_cfi = current_position_data.cfi
if current_cfi: if current_cfi:
container.lastChild.appendChild(E.div( container.appendChild(E.div(
style='margin: 1rem 0; display: flex; align-items: baseline', style='margin: 1rem; display: flex; align-items: baseline',
E.input( E.input(
type='text', readonly='readonly', value=_('Currently at: {}').format(current_cfi), name='current_location', type='text', readonly='readonly', value=_('Current location: {}').format(current_cfi), name='current_location',
style='border-width: 0; background-color: transparent; outline: none; flex-grow: 10; font-family: inherit' style='border-width: 0; background-color: transparent; outline: none; flex-grow: 10; font-family: inherit'
), ),
create_button(_('Copy'), action=def(): create_button(_('Copy'), action=def():
@ -84,52 +80,42 @@ def create_location_overlay(current_position_data, overlay, container):
) )
)) ))
def goto_loc(): def goto_cfi(cfi):
src = document.querySelector(f'#{container_id} [name=newloc]') if ui_operations.goto_cfi(cfi):
if src.value:
if ui_operations.goto_cfi(src.value):
overlay.hide() overlay.hide()
else: else:
error_dialog(_('No such location'), _( error_dialog(_('No such location'), _(
'No location {} found').format(src.value)) 'No location {} found').format(cfi))
container.lastChild.appendChild(E.div(
style='margin: 1rem 0;',
E.div(
style='display: flex; align-items: baseline',
E.label(_('Go to:'), style='margin-right: 1rem'),
E.input(name='newloc', type='text', style='flex-grow: 10; margin-right: 1rem', onkeydown=def(ev):
if ev.key is 'Enter':
goto_loc()
),
E.span(' '),
create_button(_('Go'), action=goto_loc)
)
))
container.appendChild(E.div(
style='margin-top: 1rem; border-top: solid 1px; padding-top: 1rem',
E.h4(_('Using book position'))
))
if current_position_data.book_length > 0: if current_position_data.book_length > 0:
container.lastChild.appendChild( container.appendChild(
E.div(style='margin: 1rem 0', _('Currently at: {}').format( E.div(style='margin: 1rem', _('Current position: {}').format(
format_pos(current_position_data.progress_frac, current_position_data.book_length)))) format_pos(current_position_data.progress_frac, current_position_data.book_length))))
def goto_pos(): def goto_pos():
src = document.querySelector(f'#{container_id} [name=newpos]') src = document.querySelector(f'#{container_id} [name=newpos]').value
ui_operations.goto_book_position(float(src.value)) if not src:
return
if src.indexOf('epubcfi(') is 0:
return goto_cfi(src)
try:
ui_operations.goto_book_position(float(src))
except:
error_dialog(_('Not a valid book position'), _(
'{} is not a valid book position').format(src))
else:
overlay.hide() overlay.hide()
container.lastChild.appendChild(E.div( container.appendChild(E.div(
style='margin: 1rem 0;', style='margin: 1rem;',
E.div( E.div(
style='display: flex; align-items: baseline', style='display: flex; align-items: baseline',
E.label(_('Go to:'), style='margin-right: 1rem'), E.label(_('Go to:'), style='margin-right: 1rem'),
E.input(name='newpos', type='number', min='0', max=str(current_position_data.book_length), step='0.1', style='flex-grow: 10; margin-right: 1rem', onkeydown=def(ev): E.input(name='newpos', type='text', min='0', max=str(current_position_data.book_length), step='0.1', style='flex-grow: 10; margin-right: 1rem', onkeydown=def(ev):
if ev.key is 'Enter': if ev.key is 'Enter':
goto_pos() goto_pos()
), E.span(' '), ), E.span(' '),
create_button(_('Go'), action=goto_pos) create_button(_('Go'), action=goto_pos)
) )
)) ))
container.querySelector('[name=newpos]').focus()

View File

@ -628,7 +628,7 @@ class Overlay:
def show_ask_for_location(self): def show_ask_for_location(self):
self.hide_current_panel() self.hide_current_panel()
self.panels.push(SimpleOverlay( self.panels.push(SimpleOverlay(
self, create_location_overlay.bind(None, self.view.current_position_data), _('Go to location…'))) self, create_location_overlay.bind(None, self.view.current_position_data), _('Go to location or position…')))
self.show_current_panel() self.show_current_panel()
def show_search(self): def show_search(self):

View File

@ -246,6 +246,12 @@ def shortcuts_definition():
_('Show the viewer controls'), _('Show the viewer controls'),
), ),
'goto_location': desc(
v"[';', ':', 'Shift+:', 'Shift+;', 'Ctrl+g']",
'ui',
_('Go to a specified book location or position'),
),
} }
return ans return ans

View File

@ -388,6 +388,8 @@ class View:
self.overlay.show_prefs() self.overlay.show_prefs()
elif data.name is 'metadata': elif data.name is 'metadata':
self.overlay.show_metadata() self.overlay.show_metadata()
elif data.name is 'goto_location':
self.overlay.show_ask_for_location()
def on_selection_change(self, data): def on_selection_change(self, data):