Misc selection bar work

Use an object for tracking selections
Track mouse position while selecting
Remove copy selection action from chrome
This commit is contained in:
Kovid Goyal 2020-07-24 15:29:36 +05:30
parent 2078309b52
commit 6f919231f1
No known key found for this signature in database
GPG Key ID: 06BC317B515ACE7C
4 changed files with 29 additions and 29 deletions

View File

@ -77,9 +77,13 @@ def layout_style():
return 'scrolling' if current_layout_mode() is 'flow' else 'paginated'
drag_mouse_position = {'x': None, 'y': None}
def cancel_drag_scroll():
cancel_drag_scroll_flow()
cancel_drag_scroll_paged()
drag_mouse_position.x = drag_mouse_position.y = None
class EPUBReadingSystem:
@ -459,17 +463,16 @@ class IframeBoss:
def get_current_cfi(self, data):
cfi = current_cfi()
selected_text = window.getSelection().toString()
if cfi:
index = current_spine_item().index
if index > -1:
cfi = 'epubcfi(/{}{})'.format(2*(index+1), cfi)
self.send_message(
'report_cfi', cfi=cfi, progress_frac=self.calculate_progress_frac(),
file_progress_frac=progress_frac(), request_id=data.request_id, selected_text=selected_text)
file_progress_frac=progress_frac(), request_id=data.request_id)
return
self.send_message(
'report_cfi', cfi=None, progress_frac=0, file_progress_frac=0, selected_text=selected_text, request_id=data.request_id)
'report_cfi', cfi=None, progress_frac=0, file_progress_frac=0, request_id=data.request_id)
def update_cfi(self):
cfi = current_cfi()
@ -481,10 +484,9 @@ class IframeBoss:
fpf = progress_frac()
if cfi is not self.last_cfi:
self.last_cfi = cfi
selected_text = window.getSelection().toString()
self.send_message(
'update_cfi', cfi=cfi, replace_history=self.replace_history_on_next_cfi_update,
progress_frac=pf, file_progress_frac=fpf, selected_text=selected_text)
progress_frac=pf, file_progress_frac=fpf)
self.replace_history_on_next_cfi_update = True
else:
self.send_message(
@ -529,6 +531,7 @@ class IframeBoss:
annot_id = highlight_associated_with_selection(sel, annot_id_uuid_map)
self.send_message(
'selectionchange', text=text, empty=v'!!collapsed', annot_id=annot_id,
drag_mouse_position=drag_mouse_position,
selection_extents=selection_extents(current_layout_mode() is 'flow', True))
def onresize_stage2(self):
@ -565,6 +568,8 @@ class IframeBoss:
def onmousemove(self, evt):
if evt.buttons is not 1:
return
drag_mouse_position.x = evt.clientX
drag_mouse_position.y = evt.clientY
if 0 <= evt.clientY <= window.innerHeight:
cancel_drag_scroll()
return

View File

@ -328,10 +328,6 @@ class MainOverlay: # {{{
def(): self.overlay.hide(), ui_operations.toggle_highlights();, 'image')
)
copy_actions = E.ul()
if self.overlay.view.currently_showing.selected_text:
copy_actions.appendChild(ac(_('Copy selection'), _('Copy the current selection'), def():
self.overlay.hide(), ui_operations.copy_selection()
, 'copy'))
if self.elements.link:
copy_actions.appendChild(ac(_('Copy link'), _('Copy the current link'), def():
self.overlay.hide(), ui_operations.copy_selection(self.elements.link)

View File

@ -75,8 +75,8 @@ class SelectionBar:
return self.container.style.display is not 'none'
def copy_to_clipboard(self):
if self.view.currently_showing.selected_text and ui_operations.copy_selection:
ui_operations.copy_selection(self.view.currently_showing.selected_text)
if self.view.currently_showing.selection.text and ui_operations.copy_selection:
ui_operations.copy_selection(self.view.currently_showing.selection.text)
def lookup(self):
ui_operations.toggle_lookup(True)
@ -120,11 +120,11 @@ class SelectionBar:
def update_position(self):
container = self.container
clear(container)
cs = self.view.currently_showing
if not cs.has_selection:
cs = self.view.currently_showing.selection
if cs.empty:
return self.hide()
if not cs.selection_start.onscreen and not cs.selection_end.onscreen:
if not cs.start.onscreen and not cs.end.onscreen:
return self.hide()
margins = {
@ -137,9 +137,9 @@ class SelectionBar:
def map_boundary(x):
return {'x': (x.x or 0) + margins.left, 'y': (x.y or 0) + margins.top, 'height': x.height or 0, 'onscreen': x.onscreen}
bar = self.build_bar(self.view.annotations_manager.notes_for_highlight(cs.selection_annot_id))
start = map_boundary(cs.selection_start)
end = map_boundary(cs.selection_end)
bar = self.build_bar(self.view.annotations_manager.notes_for_highlight(cs.annot_id))
start = map_boundary(cs.start)
end = map_boundary(cs.end)
self.show()
end_after_start = start.y < end.y or (start.y is end.y and start.x < end.x)

View File

@ -471,8 +471,8 @@ class View:
elif data.name is 'reload_book':
ui_operations.reload_book()
elif data.name is 'search_for_selection':
if self.currently_showing.selected_text:
self.search_overlay.set_text(self.currently_showing.selected_text)
if self.currently_showing.selection.text:
self.search_overlay.set_text(self.currently_showing.selection.text)
self.search_overlay.find_next()
elif data.name is 'next_section':
self.on_next_section({'forward': True})
@ -518,18 +518,19 @@ class View:
self.iframe_wrapper.send_message('handle_navigation_shortcut', name=data.name)
def on_selection_change(self, data):
self.currently_showing.selected_text = data.text
self.currently_showing.has_selection = not data.empty
self.currently_showing.selection_start = data.selection_extents.start
self.currently_showing.selection_end = data.selection_extents.end
self.currently_showing.selection_annot_id = data.annot_id
self.currently_showing.selection = {
'text': data.text, 'empty': data.empty, 'start': data.selection_extents.start,
'end': data.selection_extents.end, 'annot_id': data.annot_id,
'drag_mouse_position': data.drag_mouse_position
}
if ui_operations.selection_changed:
ui_operations.selection_changed(self.currently_showing.selected_text)
ui_operations.selection_changed(self.currently_showing.selection.text)
self.selection_bar.update_position()
def update_selection_position(self, data):
self.currently_showing.selection_start = data.selection_extents.start
self.currently_showing.selection_end = data.selection_extents.end
sel = self.currently_showing.selection
sel.start = data.selection_extents.start
sel.end = data.selection_extents.end
self.selection_bar.update_position()
def on_columns_per_screen_changed(self, data):
@ -1045,7 +1046,6 @@ class View:
def update_cfi_data(self, data):
self.currently_showing.bookpos = data.cfi
self.currently_showing.selected_text = data.selected_text
username = get_interface_data().username
unkey = username_key(username)
if not self.book.last_read_position:
@ -1064,7 +1064,6 @@ class View:
'cfi': data.cfi,
'progress_frac': data.progress_frac,
'file_progress_frac': data.file_progress_frac,
'selected_text': data.selected_text,
})
v'delete self.report_cfi_callbacks[data.request_id]'