From 8bd7109300bbd975467f20b483a2b595e4e75dcd Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Sun, 4 Oct 2020 11:23:25 +0530 Subject: [PATCH] Viewer: Fix selection popup bar not always close to mouse when ending select-to-drag --- src/pyj/read_book/iframe.pyj | 7 ++++++- src/pyj/read_book/selection_bar.pyj | 8 ++++++-- src/pyj/read_book/view.pyj | 3 ++- 3 files changed, 14 insertions(+), 4 deletions(-) diff --git a/src/pyj/read_book/iframe.pyj b/src/pyj/read_book/iframe.pyj index 1564fa46a8..93a0f4507e 100644 --- a/src/pyj/read_book/iframe.pyj +++ b/src/pyj/read_book/iframe.pyj @@ -536,15 +536,20 @@ class IframeBoss: text = '' annot_id = None collapsed = not sel or sel.isCollapsed + start_is_anchor = True if not collapsed: text = sel.toString() annot_id = highlight_associated_with_selection(sel, annot_id_uuid_map) + r = sel.getRangeAt(0) + start_is_anchor = r.startContainer is sel.anchorNode and r.startOffset is sel.anchorOffset by_search = window.performance.now() - self.last_search_at < 1000 self.send_message( 'selectionchange', text=text, empty=v'!!collapsed', annot_id=annot_id, drag_mouse_position=drag_mouse_position, selection_change_caused_by_search=by_search, selection_extents=selection_extents(current_layout_mode() is 'flow'), - rtl=scroll_viewport.rtl, vertical=scroll_viewport.vertical_writing_mode) + rtl=scroll_viewport.rtl, vertical=scroll_viewport.vertical_writing_mode, + start_is_anchor=start_is_anchor + ) def onresize_stage2(self): if scroll_viewport.width() is self.last_window_width and scroll_viewport.height() is self.last_window_height: diff --git a/src/pyj/read_book/selection_bar.pyj b/src/pyj/read_book/selection_bar.pyj index 5350f5eefc..6bb005813f 100644 --- a/src/pyj/read_book/selection_bar.pyj +++ b/src/pyj/read_book/selection_bar.pyj @@ -687,8 +687,12 @@ class SelectionBar: # We try to place the bar near the last dragged handle so it shows up # close to current mouse position. We assume it is the "end" handle. - if dragged_handle and dragged_handle is not self.end_handle_id: - start, end = end, start + if dragged_handle: + if dragged_handle is not self.end_handle_id: + start, end = end, start + else: + if not cs.start_is_anchor: + start, end = end, start if not end.onscreen and start.onscreen: start, end = end, start diff --git a/src/pyj/read_book/view.pyj b/src/pyj/read_book/view.pyj index c58326c9e6..996c4310d0 100644 --- a/src/pyj/read_book/view.pyj +++ b/src/pyj/read_book/view.pyj @@ -552,7 +552,8 @@ class View: 'end': data.selection_extents.end, 'annot_id': data.annot_id, 'drag_mouse_position': data.drag_mouse_position, 'selection_change_caused_by_search': data.selection_change_caused_by_search, - 'rtl': data.rtl, 'vertical': data.vertical + 'rtl': data.rtl, 'vertical': data.vertical, + 'start_is_anchor': data.start_is_anchor } if ui_operations.selection_changed: ui_operations.selection_changed(self.currently_showing.selection.text, self.currently_showing.selection.annot_id)