mirror of
https://github.com/koreader/koreader.git
synced 2025-08-10 00:52:38 +00:00
Merge pull request #813 from chrox/dict_lookup
lookup input word when holding on word in dict lookup window
This commit is contained in:
@@ -9,7 +9,7 @@ local _ = require("gettext")
|
||||
|
||||
local ReaderDictionary = EventListener:new{}
|
||||
|
||||
function ReaderDictionary:onLookupWord(highlight, word, box)
|
||||
function ReaderDictionary:onLookupWord(word, box, highlight)
|
||||
self.highlight = highlight
|
||||
self:stardictLookup(word, box)
|
||||
end
|
||||
@@ -46,18 +46,24 @@ function ReaderDictionary:stardictLookup(word, box)
|
||||
end
|
||||
|
||||
function ReaderDictionary:showDict(results, box)
|
||||
if results and results[1] and box then
|
||||
if results and results[1] then
|
||||
DEBUG("showing quick lookup dictionary window")
|
||||
local align = nil
|
||||
local region = Geom:new{x = 0, w = Screen:getWidth()}
|
||||
if box.y + box.h/2 < Screen:getHeight()/2 then
|
||||
region.y = box.y + box.h
|
||||
region.h = Screen:getHeight() - box.y - box.h
|
||||
align = "top"
|
||||
else
|
||||
region.y = 0
|
||||
region.h = box.y
|
||||
align = "bottom"
|
||||
local align = "center"
|
||||
local region = Geom:new{
|
||||
x = 0, y = 0,
|
||||
w = Screen:getWidth(),
|
||||
h = Screen:getHeight(),
|
||||
}
|
||||
if box then
|
||||
if box.y + box.h/2 < Screen:getHeight()/2 then
|
||||
region.y = box.y + box.h
|
||||
region.h = Screen:getHeight() - box.y - box.h
|
||||
align = "top"
|
||||
else
|
||||
region.y = 0
|
||||
region.h = box.y
|
||||
align = "bottom"
|
||||
end
|
||||
end
|
||||
UIManager:show(DictQuickLookup:new{
|
||||
ui = self.ui,
|
||||
|
||||
@@ -260,13 +260,13 @@ function ReaderHighlight:lookup(selected_word)
|
||||
-- if we extracted text directly
|
||||
if selected_word.word then
|
||||
local word_box = self.view:pageToScreenTransform(self.hold_pos.page, selected_word.sbox)
|
||||
self.ui:handleEvent(Event:new("LookupWord", self, selected_word.word, word_box))
|
||||
self.ui:handleEvent(Event:new("LookupWord", selected_word.word, word_box, self))
|
||||
-- or we will do OCR
|
||||
elseif selected_word.sbox and self.hold_pos then
|
||||
local word = self.ui.document:getOCRWord(self.hold_pos.page, selected_word)
|
||||
DEBUG("OCRed word:", word)
|
||||
local word_box = self.view:pageToScreenTransform(self.hold_pos.page, selected_word.sbox)
|
||||
self.ui:handleEvent(Event:new("LookupWord", self, word, word_box))
|
||||
self.ui:handleEvent(Event:new("LookupWord", word, word_box, self))
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
@@ -79,6 +79,13 @@ function Button:init()
|
||||
},
|
||||
doc = "Tap Button",
|
||||
},
|
||||
HoldSelect = {
|
||||
GestureRange:new{
|
||||
ges = "hold",
|
||||
range = self.dimen,
|
||||
},
|
||||
doc = "Hold Button",
|
||||
}
|
||||
}
|
||||
end
|
||||
end
|
||||
@@ -152,4 +159,11 @@ function Button:onTapSelect()
|
||||
return true
|
||||
end
|
||||
|
||||
function Button:onHoldSelect()
|
||||
if self.enabled and self.hold_callback then
|
||||
self.hold_callback()
|
||||
end
|
||||
return true
|
||||
end
|
||||
|
||||
return Button
|
||||
|
||||
@@ -51,6 +51,11 @@ function WidgetContainer:paintTo(bb, x, y)
|
||||
self[1]:paintTo(bb,
|
||||
x + math.floor((self.dimen.w - contentSize.w)/2),
|
||||
y + (self.dimen.h - contentSize.h))
|
||||
elseif self.align == "center" then
|
||||
local contentSize = self[1]:getSize()
|
||||
self[1]:paintTo(bb,
|
||||
x + math.floor((self.dimen.w - contentSize.w)/2),
|
||||
y + math.floor((self.dimen.h - contentSize.h)/2))
|
||||
else
|
||||
return self[1]:paintTo(bb, x, y)
|
||||
end
|
||||
|
||||
@@ -2,21 +2,24 @@ local InputContainer = require("ui/widget/container/inputcontainer")
|
||||
local WidgetContainer = require("ui/widget/container/widgetcontainer")
|
||||
local FrameContainer = require("ui/widget/container/framecontainer")
|
||||
local CenterContainer = require("ui/widget/container/centercontainer")
|
||||
local CloseButton = require("ui/widget/closebutton")
|
||||
local TextWidget = require("ui/widget/textwidget")
|
||||
local TextBoxWidget = require("ui/widget/textboxwidget")
|
||||
local LeftContainer = require("ui/widget/container/leftcontainer")
|
||||
local ScrollTextWidget = require("ui/widget/scrolltextwidget")
|
||||
local LineWidget = require("ui/widget/linewidget")
|
||||
local OverlapGroup = require("ui/widget/overlapgroup")
|
||||
local Screen = require("ui/screen")
|
||||
local GestureRange = require("ui/gesturerange")
|
||||
local Geom = require("ui/geometry")
|
||||
local Font = require("ui/font")
|
||||
local Event = require("ui/event")
|
||||
local UIManager = require("ui/uimanager")
|
||||
local ButtonTable = require("ui/widget/buttontable")
|
||||
local Device = require("ui/device")
|
||||
local VerticalGroup = require("ui/widget/verticalgroup")
|
||||
local TextBoxWidget = require("ui/widget/textboxwidget")
|
||||
local OverlapGroup = require("ui/widget/overlapgroup")
|
||||
local CloseButton = require("ui/widget/closebutton")
|
||||
local ButtonTable = require("ui/widget/buttontable")
|
||||
local InputDialog = require("ui/widget/inputdialog")
|
||||
local TextWidget = require("ui/widget/textwidget")
|
||||
local LineWidget = require("ui/widget/linewidget")
|
||||
local GestureRange = require("ui/gesturerange")
|
||||
local Button = require("ui/widget/button")
|
||||
local UIManager = require("ui/uimanager")
|
||||
local Screen = require("ui/screen")
|
||||
local Device = require("ui/device")
|
||||
local Geom = require("ui/geometry")
|
||||
local Event = require("ui/event")
|
||||
local Font = require("ui/font")
|
||||
local DEBUG = require("dbg")
|
||||
local _ = require("gettext")
|
||||
|
||||
@@ -37,7 +40,7 @@ local DictQuickLookup = InputContainer:new{
|
||||
|
||||
title_padding = Screen:scaleByDPI(5),
|
||||
title_margin = Screen:scaleByDPI(2),
|
||||
word_padding = Screen:scaleByDPI(2),
|
||||
word_padding = Screen:scaleByDPI(5),
|
||||
word_margin = Screen:scaleByDPI(2),
|
||||
definition_padding = Screen:scaleByDPI(2),
|
||||
definition_margin = Screen:scaleByDPI(2),
|
||||
@@ -95,16 +98,14 @@ function DictQuickLookup:update()
|
||||
}
|
||||
}
|
||||
-- lookup word
|
||||
local lookup_word = FrameContainer:new{
|
||||
local lookup_word = Button:new{
|
||||
padding = self.word_padding,
|
||||
margin = self.word_margin,
|
||||
bordersize = 0,
|
||||
TextBoxWidget:new{
|
||||
text = self.lookupword,
|
||||
face = self.word_face,
|
||||
bold = true,
|
||||
width = self.width,
|
||||
},
|
||||
text = self.lookupword,
|
||||
text_font_face = "tfont",
|
||||
text_font_size = 22,
|
||||
hold_callback = function() self:lookupInputWord(self.lookupword) end,
|
||||
}
|
||||
-- word definition
|
||||
local definition = FrameContainer:new{
|
||||
@@ -197,7 +198,7 @@ function DictQuickLookup:update()
|
||||
self.dict_bar,
|
||||
title_bar,
|
||||
-- word
|
||||
CenterContainer:new{
|
||||
LeftContainer:new{
|
||||
dimen = Geom:new{
|
||||
w = title_bar:getSize().w,
|
||||
h = lookup_word:getSize().h,
|
||||
@@ -316,7 +317,9 @@ end
|
||||
|
||||
function DictQuickLookup:onClose()
|
||||
UIManager:close(self)
|
||||
self.highlight:handleEvent(Event:new("Tap"))
|
||||
if self.highlight then
|
||||
self.highlight:handleEvent(Event:new("Tap"))
|
||||
end
|
||||
return true
|
||||
end
|
||||
|
||||
@@ -329,4 +332,50 @@ function DictQuickLookup:onSwipe(arg, ges)
|
||||
return true
|
||||
end
|
||||
|
||||
function DictQuickLookup:lookupInputWord(hint)
|
||||
self:onClose()
|
||||
self.input_dialog = InputDialog:new{
|
||||
title = _("Input lookup word"),
|
||||
input_hint = hint or "",
|
||||
input_type = "text",
|
||||
buttons = {
|
||||
{
|
||||
{
|
||||
text = _("Cancel"),
|
||||
callback = function()
|
||||
self:closeInputDialog()
|
||||
end,
|
||||
},
|
||||
{
|
||||
text = _("Lookup"),
|
||||
callback = function()
|
||||
self:closeInputDialog()
|
||||
self:inputLookup()
|
||||
end,
|
||||
},
|
||||
}
|
||||
},
|
||||
enter_callback = function()
|
||||
self:closeInputDialog()
|
||||
self:inputLookup()
|
||||
end,
|
||||
width = Screen:getWidth() * 0.8,
|
||||
height = Screen:getHeight() * 0.2,
|
||||
}
|
||||
self.input_dialog:onShowKeyboard()
|
||||
UIManager:show(self.input_dialog)
|
||||
end
|
||||
|
||||
function DictQuickLookup:inputLookup()
|
||||
local word = self.input_dialog:getInputText()
|
||||
if word and word ~= "" then
|
||||
self.ui:handleEvent(Event:new("LookupWord", word))
|
||||
end
|
||||
end
|
||||
|
||||
function DictQuickLookup:closeInputDialog()
|
||||
self.input_dialog:onClose()
|
||||
UIManager:close(self.input_dialog)
|
||||
end
|
||||
|
||||
return DictQuickLookup
|
||||
|
||||
Reference in New Issue
Block a user