[DictQuickLookup] NT: fix utf8 byte drift during text selection#15015
Conversation
|
Good that you found the reason ! Did you get joy and a wonderful sleep ? But it's a bit ugly that you have to do that and know about the internals. koreader/frontend/ui/widget/dictquicklookup.lua Lines 2006 to 2010 in 7de4f4c You pass nil as the onHoldReleaseText callback, but if you passed something, it would get the selected_text as its first arg, and you wouldn't have to do that yourself. I have no idea if this would work with the html widget, but logically, you could have something cleaner with: - selection_widget:onHoldReleaseText(nil, self:_createTextSelectionGesture("hold_release"))
- self:_performLookupOnSelection(selection_widget, false)
+ local selected_text
+ selection_widget:onHoldReleaseText(function(text) selected_text = text end, self:_createTextSelectionGesture("hold_release"))
- self:_performLookupOnSelection(selected_text, false)
-function DictQuickLookup:_performLookupOnSelection(selection_widget, switch_domain)
+function DictQuickLookup:_performLookupOnSelection(selected_text, switch_domain)
+ -- use selected_text directly instead of re-fetching it |
| function(text) selected_text = text end, | ||
| self:_createTextSelectionGesture("hold_release") | ||
| ) | ||
| self:_performLookupOnSelection(selected_text, true) -- switch_domain, "i'm master of my domain" ¯\_(ツ)_/¯ |
There was a problem hiding this comment.
lol, I don't want him to go... leave him alone
What ?! It actually worked ? That's unexpected, I just wanted to send you down the rabbit hole so you'd be busy for a while. |
so you want a somewhat obsequious response in a rather loquacious manner? "you absolute legend, they should make you archbishop of Canterbury or something." |
|
@poire-z, all this could be simply: args = function(text, hold_duration)
- -- do this lookup in the same domain (dict/wikipedia)
- local lookup_wikipedia = self.is_wiki
- if hold_duration >= time.s(3) then
- -- but allow switching domain with a long hold
- lookup_wikipedia = not lookup_wikipedia
- end
-
- local new_dict_close_callback = function()
- self:clearDictionaryHighlight()
- end
-
- -- We don't pass self.highlight to subsequent lookup, we want the
- -- first to be the only one to unhighlight selection when closed
- if lookup_wikipedia then
- self:lookupWikipedia(false, text, nil, nil, new_dict_close_callback)
- else
- self.ui:handleEvent(Event:new("LookupWord", text, nil, nil, nil, nil, new_dict_close_callback))
- end
+ -- short hold: same domain; long hold (>=3s): switch domain
+ self:_performLookupOnSelection(text, hold_duration >= time.s(3))
end
Do you want me to add it here? |
|
Nothing against. |
| function DictQuickLookup:searchDictionaryOrWikipedia(selected_text, switch_domain) | ||
| if not selected_text then return false end | ||
| local new_dict_close_callback = function() self:clearDictionaryHighlight() end | ||
| local use_wiki = switch_domain and not self.is_wiki or not switch_domain and self.is_wiki |
There was a problem hiding this comment.
Convoluted and hard to grasp, the way it was done in the code you removed was a lot clearer.
There was a problem hiding this comment.
happy with
local use_wiki = self.is_wiki
if switch_domain then use_wiki = not use_wiki end?
poire-z
left a comment
There was a problem hiding this comment.
Fine with me.
(I would still like a better name for searchDictionaryOrWikipedia - we don't really use the word "search" in there, mostly "lookup". :lookup() might be too simple/obvious for something that is not the real thing?, :doLookup ? :doNewLookup ?)
|
do you still need a new name or was that just a 'hopefully we could...'? it is a little contradictory to give an "approved" but still sort of ask for changes... |
|
= I have no more comment about the logic and I'm fine with it, so I approved so you don't feel like I'm harrassing you every day :) |
|
What/where was the question exactly? |
|
|
lernaLookup 👼 But yes, just a simple |
|
|
|
I suppose. |
|
It's still quite verbose. |
|
they are all new lookups, what matters is the idea the the true will switch you from one to the other |
So, if that's the main thing it always does, then
So, the idea should be in that parameter name: Anyway, if you don't "feel" it, I don't really care, go ahead. |
…ader#15015) * refactor handling of faked event HoldRelease for text selection on NT, make sure we close the cycle properly so we don't get any byte drift when dealing with non-ascii characters.
In
TextBoxWidget,highlight_start_idx/highlight_end_idxare character indices.In
DictQuickLookup:_performLookupOnSelection, we use:but
string.subuses byte indices. So with UTF-8 content (the IPA/mænˈkaɪnd/etc.), extraction shifts left and returns the wrong token (e.g. "with Sc").bug fix
onTextSelectorPressandonTextSelectorModifierPressto extract the selected text via a callback fromonHoldReleaseText, and pass it directly to_performLookupOnSelection._performLookupOnSelectionto accept the selected text as its argument, rather than the selection widget, simplifying its interface and removing internal logic for extracting text from the widget.This change is