Track the selection position in the chrome

This commit is contained in:
Kovid Goyal 2020-07-21 14:15:49 +05:30
parent 23fb259c42
commit 05516bfd54
No known key found for this signature in database
GPG Key ID: 06BC317B515ACE7C
2 changed files with 22 additions and 3 deletions

View File

@ -505,6 +505,9 @@ class IframeBoss:
fpf = progress_frac()
self.send_message(
'update_progress_frac', progress_frac=pf, file_progress_frac=fpf)
sel = window.getSelection()
if sel and not sel.isCollapsed:
self.send_message('update_selection_position', selection_extents=selection_extents(current_layout_mode() is 'flow'))
def onresize(self):
self.send_message('request_size')
@ -516,8 +519,16 @@ class IframeBoss:
self.onresize_stage2()
def onselectionchange(self):
if self.content_ready:
self.send_message('selectionchange', text=document.getSelection().toString())
if not self.content_ready:
return
sel = window.getSelection()
text = ''
collapsed = not sel or sel.isCollapsed
if not collapsed:
text = sel.toString()
self.send_message(
'selectionchange', text=text, empty=v'!!collapsed',
selection_extents=selection_extents(current_layout_mode() is 'flow'))
def onresize_stage2(self):
if scroll_viewport.width() is self.last_window_width and scroll_viewport.height() is self.last_window_height:

View File

@ -261,6 +261,7 @@ class View:
'request_size': self.on_request_size,
'scroll_to_anchor': self.on_scroll_to_anchor,
'selectionchange': self.on_selection_change,
'update_selection_position': self.update_selection_position,
'columns_per_screen_changed': self.on_columns_per_screen_changed,
'show_chrome': self.show_chrome,
'show_footnote': self.on_show_footnote,
@ -513,8 +514,15 @@ class View:
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
if ui_operations.selection_changed:
ui_operations.selection_changed(data.text)
ui_operations.selection_changed(self.currently_showing.selected_text)
def update_selection_position(self, data):
self.currently_showing.selection_start = data.selection_extents.start
self.currently_showing.selection_end = data.selection_extents.end
def on_columns_per_screen_changed(self, data):
sd = get_session_data()