mirror of
https://github.com/koreader/koreader.git
synced 2026-11-28 08:17:00 +00:00
[ReaderKeySelection]: reset text selection state when document closes (#16070)
This commit is contained in:
1 parent
b8ca38d5e5
commit
9bbaecc2a8
1 file changed
+49
-23
@@ -403,6 +403,13 @@ function ReaderKeySelection:onStartOrMoveHighlightIndicator(args)
|
||||
return true
|
||||
end
|
||||
|
||||
function ReaderKeySelection:onCloseDocument()
|
||||
-- The document (self.ui.document) is about to go away, shred all our evidence ;)
|
||||
self:clearFlashHighlight()
|
||||
self._previous_indicator_pos = nil
|
||||
self:_resetIndicatorState()
|
||||
end
|
||||
|
||||
function ReaderKeySelection:isActive()
|
||||
return self._current_indicator_pos ~= nil
|
||||
end
|
||||
@@ -440,7 +447,6 @@ function ReaderKeySelection:startHighlightIndicator()
|
||||
rect.w = Size.item.height_default
|
||||
rect.h = rect.w
|
||||
end
|
||||
self._current_indicator_pos = rect
|
||||
|
||||
-- Compute padded saved region (match paintTo padding)
|
||||
local max_w = self.screen_w or Screen:getWidth()
|
||||
@@ -450,11 +456,7 @@ function ReaderKeySelection:startHighlightIndicator()
|
||||
if not save_r then
|
||||
save_r = Geom:new{ x = math.floor(rect.x), y = math.floor(rect.y), w = rect.w, h = rect.h }
|
||||
end
|
||||
self._indicator_overlay = IndicatorOverlay:new{
|
||||
dimen = Geom:new{ x = save_r.x, y = save_r.y, w = save_r.w, h = save_r.h },
|
||||
parent_ui = self.ui,
|
||||
}
|
||||
UIManager:show(self._indicator_overlay)
|
||||
self:_beginIndicator(rect, save_r)
|
||||
if self.ui.paging then
|
||||
self._last_indicator_move_args = {dx = 0, dy = 0, distance = 0, time = time:now()}
|
||||
self._indicator_overlay.indicator_rect = rect
|
||||
@@ -483,6 +485,35 @@ function ReaderKeySelection:startHighlightIndicator()
|
||||
return false
|
||||
end
|
||||
|
||||
function ReaderKeySelection:_beginIndicator(rect, save_r)
|
||||
-- The only place _current_indicator_pos and _indicator_overlay are created together,
|
||||
-- the same way _resetIndicatorState is the only place they're torn down together.
|
||||
self._current_indicator_pos = rect
|
||||
self._indicator_overlay = IndicatorOverlay:new{
|
||||
dimen = Geom:new{ x = save_r.x, y = save_r.y, w = save_r.w, h = save_r.h },
|
||||
parent_ui = self.ui,
|
||||
}
|
||||
UIManager:show(self._indicator_overlay)
|
||||
end
|
||||
|
||||
-- Resets the fields that must never be allowed to outlive the word/position they were
|
||||
-- computed against. Shared by stopHighlightIndicator and onCloseDocument teardown.
|
||||
function ReaderKeySelection:_resetIndicatorState()
|
||||
self._current_indicator_pos = nil
|
||||
self._previous_indicator_word = nil
|
||||
self._vertical_move_anchor_x = nil
|
||||
self._last_move_was_vertical = false
|
||||
self._start_indicator_highlight = false
|
||||
self._edge_dx, self._edge_dy = nil, nil
|
||||
self._last_move_was_quick_move = nil
|
||||
self._fast_dict_mode = nil
|
||||
if self._indicator_overlay then
|
||||
self._indicator_overlay:freeSavedBB()
|
||||
UIManager:close(self._indicator_overlay)
|
||||
self._indicator_overlay = nil
|
||||
end
|
||||
end
|
||||
|
||||
function ReaderKeySelection:stopHighlightIndicator(need_clear_selection)
|
||||
if not self._current_indicator_pos then return false end
|
||||
-- If we're in select mode and user presses back, end the selection
|
||||
@@ -497,20 +528,7 @@ function ReaderKeySelection:stopHighlightIndicator(need_clear_selection)
|
||||
end
|
||||
local rect = self._current_indicator_pos
|
||||
self._previous_indicator_pos = rect
|
||||
self._vertical_move_anchor_x = nil
|
||||
self._last_move_was_vertical = false
|
||||
self._start_indicator_highlight = false
|
||||
self._current_indicator_pos = nil
|
||||
self.view.highlight.indicator = nil
|
||||
self._edge_dx, self._edge_dy = nil, nil
|
||||
self._last_move_was_quick_move = nil
|
||||
self._previous_indicator_word = nil
|
||||
self._fast_dict_mode = nil
|
||||
if self._indicator_overlay then
|
||||
self._indicator_overlay:freeSavedBB()
|
||||
UIManager:close(self._indicator_overlay)
|
||||
self._indicator_overlay = nil
|
||||
end
|
||||
self:_resetIndicatorState()
|
||||
self._last_indicator_move_args = nil
|
||||
UIManager:setDirty(self.dialog, "ui", rect)
|
||||
if need_clear_selection then
|
||||
@@ -569,6 +587,7 @@ end
|
||||
|
||||
function ReaderKeySelection:moveHighlightIndicator(args)
|
||||
if not (self.view.visible_area and self._current_indicator_pos) then return false end
|
||||
if not self.ui.document then return false end -- document was closed out from under a queued key event
|
||||
self:clearFlashHighlight() -- delay may not have cleared it yet
|
||||
local dx, dy, quick_move = unpack(args)
|
||||
if dx == self._edge_dx and dy == self._edge_dy and self._last_move_was_quick_move == quick_move then
|
||||
@@ -816,12 +835,15 @@ function ReaderKeySelection:_setIndicatorToWord(word)
|
||||
end
|
||||
|
||||
function ReaderKeySelection:_setIndicatorRect(rect)
|
||||
local old_rect = self._current_indicator_pos
|
||||
self._current_indicator_pos = rect
|
||||
if not self._indicator_overlay then
|
||||
logger.warn("ReaderKeySelection: _setIndicatorRect: no overlay")
|
||||
-- Should be unreachable: _current_indicator_pos and _indicator_overlay are only
|
||||
-- ever created together (_beginIndicator) or torn down together (_resetIndicatorState).
|
||||
-- Bail before mutating state if that invariant has somehow broken.
|
||||
logger.warn("ReaderKeySelection: _setIndicatorRect called with no overlay - indicator state is out of sync.")
|
||||
return
|
||||
end
|
||||
local old_rect = self._current_indicator_pos
|
||||
self._current_indicator_pos = rect
|
||||
logger.dbg("ReaderKeySelection: _setIndicatorRect: dirtying overlay, rect=", rect)
|
||||
self._indicator_overlay.indicator_rect = rect
|
||||
local dirty = getIndicatorDirtyRect(old_rect, rect, self.screen_w, self.screen_h)
|
||||
@@ -910,6 +932,7 @@ function ReaderKeySelection:_getNearestWordFromScreenPoint(screen_x, screen_y)
|
||||
local probe = { x = screen_x, y = screen_y }
|
||||
local pos = self.view:screenToPageTransform(probe)
|
||||
local doc = self.ui.document
|
||||
if not doc then return nil end -- document was closed out from under a queued key event
|
||||
|
||||
local origin_word = doc:getWordFromPosition(pos, true)
|
||||
if origin_word and origin_word.sbox then
|
||||
@@ -953,6 +976,7 @@ end
|
||||
|
||||
function ReaderKeySelection:_getQuickVerticalWordRolling(anchor_x, target_y, dy, exclude_word, current_anchor_y)
|
||||
local doc = self.ui.document
|
||||
if not doc then return nil end -- document was closed out from under a queued key event
|
||||
-- Prevent wrapping off the current page.
|
||||
local safe_y = math_max(self.view.visible_area.y, math_min(target_y, self.view.visible_area.y + self.view.visible_area.h))
|
||||
|
||||
@@ -1007,6 +1031,7 @@ function ReaderKeySelection:_getAdjacentWordRolling(word, direction, lock_line_c
|
||||
end
|
||||
end
|
||||
local doc = self.ui.document
|
||||
if not doc then return nil end -- document was closed out from under a queued key event
|
||||
-- Map physical direction to logical XPointer direction
|
||||
local logical_dir = self.mirroredUI and -direction or direction
|
||||
|
||||
@@ -1082,6 +1107,7 @@ function ReaderKeySelection:_getAdjacentLineWordRolling(word, direction, preferr
|
||||
if not (word and word.pos0 and word.sbox) then return end
|
||||
|
||||
local doc = self.ui.document
|
||||
if not doc then return nil end -- document was closed out from under a queued key event
|
||||
local target_x = preferred_center_x or (word.sbox.x + word.sbox.w * 0.5)
|
||||
|
||||
-- Align to the physical top of the line to prevent offset drift
|
||||
|
||||
Reference in new issue
Block a user