diff --git a/frontend/ui/reader/readerdictionary.lua b/frontend/ui/reader/readerdictionary.lua index 6388dcd14..aebd6b797 100644 --- a/frontend/ui/reader/readerdictionary.lua +++ b/frontend/ui/reader/readerdictionary.lua @@ -1,19 +1,20 @@ local EventListener = require("ui/widget/eventlistener") local UIManager = require("ui/uimanager") local DictQuickLookup = require("ui/widget/dictquicklookup") +local Geom = require("ui/geometry") local Screen = require("ui/screen") local JSON = require("JSON") local DEBUG = require("dbg") local ReaderDictionary = EventListener:new{} -function ReaderDictionary:onLookupWord(highlight, word) +function ReaderDictionary:onLookupWord(highlight, word, box) self.highlight = highlight - self:stardictLookup(word) + self:stardictLookup(word, box) end -function ReaderDictionary:stardictLookup(word) - DEBUG("lookup word:", word) +function ReaderDictionary:stardictLookup(word, box) + DEBUG("lookup word:", word, box) if word then -- strip punctuation characters around selected word word = string.gsub(word, "^%p+", '') @@ -27,14 +28,25 @@ function ReaderDictionary:stardictLookup(word) --DEBUG("result str:", word, results_str) local ok, results = pcall(JSON.decode, JSON, results_str) --DEBUG("lookup result table:", word, results) - self:showDict(results) + self:showDict(results, box) end end end -function ReaderDictionary:showDict(results) +function ReaderDictionary:showDict(results, box) 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" + end UIManager:show(DictQuickLookup:new{ ui = self.ui, highlight = self.highlight, @@ -42,7 +54,9 @@ function ReaderDictionary:showDict(results) results = results, dictionary = self.default_dictionary, width = Screen:getWidth() - Screen:scaleByDPI(120), - height = Screen:getHeight()*0.43, + height = math.min(region.h*0.7, Screen:getHeight()*0.5), + region = region, + align = align, }) end end diff --git a/frontend/ui/reader/readerhighlight.lua b/frontend/ui/reader/readerhighlight.lua index 6f0b63f4a..a406decd2 100644 --- a/frontend/ui/reader/readerhighlight.lua +++ b/frontend/ui/reader/readerhighlight.lua @@ -225,12 +225,14 @@ end function ReaderHighlight:lookup(selected_word) -- if we extracted text directly if selected_word.word then - self.ui:handleEvent(Event:new("LookupWord", self, selected_word.word)) + local word_box = self.view:pageToScreenTransform(selected_word.page, selected_word.sbox) + self.ui:handleEvent(Event:new("LookupWord", self, selected_word.word, word_box)) -- or we will do OCR else local word = self.ui.document:getOCRWord(self.hold_pos.page, selected_word) DEBUG("OCRed word:", word) - self.ui:handleEvent(Event:new("LookupWord", self, word)) + local word_box = self.view:pageToScreenTransform(selected_word.page, selected_word.sbox) + self.ui:handleEvent(Event:new("LookupWord", self, word, word_box)) end end diff --git a/frontend/ui/widget/closebutton.lua b/frontend/ui/widget/closebutton.lua new file mode 100644 index 000000000..a1710fa8b --- /dev/null +++ b/frontend/ui/widget/closebutton.lua @@ -0,0 +1,45 @@ +local InputContainer = require("ui/widget/container/inputcontainer") +local FrameContainer = require("ui/widget/container/framecontainer") +local CenterContainer = require("ui/widget/container/centercontainer") +local TextWidget = require("ui/widget/textwidget") +local GestureRange = require("ui/gesturerange") +local UIManager = require("ui/uimanager") +local Geom = require("ui/geometry") +local Font = require("ui/font") + +--[[ +a button widget that shows an "×" and handles closing window when tapped +--]] +local CloseButton = InputContainer:new{ + align = "right", + window = nil, +} + +function CloseButton:init() + local text_widget = TextWidget:new{ + text = "×", + face = Font:getFace("cfont", 32), + } + self[1] = FrameContainer:new{ + bordersize = 0, + padding = 0, + text_widget + } + + self.dimen = text_widget:getSize():copy() + + self.ges_events.Close = { + GestureRange:new{ + ges = "tap", + range = self.dimen, + }, + doc = "Tap on close button", + } +end + +function CloseButton:onClose() + self.window:onClose() + return true +end + +return CloseButton diff --git a/frontend/ui/widget/dictquicklookup.lua b/frontend/ui/widget/dictquicklookup.lua index 056b0f20b..d1c23ddf6 100644 --- a/frontend/ui/widget/dictquicklookup.lua +++ b/frontend/ui/widget/dictquicklookup.lua @@ -1,10 +1,13 @@ 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 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") @@ -66,6 +69,10 @@ function DictQuickLookup:init() }, }, } + table.insert(self.dict_bar, + CloseButton:new{ + window = self, + }) end end @@ -101,7 +108,7 @@ function DictQuickLookup:update() text = self.definition, face = self.content_face, width = self.width, - height = self.height*0.8, + height = self.height*0.6, dialog = self, }, } @@ -153,6 +160,11 @@ function DictQuickLookup:update() } } + self.dict_bar = OverlapGroup:new{ + dimen = {w = button_table:getSize().w, h = self.dict_title:getSize().h}, + self.dict_title, + } + self.dict_frame = FrameContainer:new{ radius = 8, bordersize = 3, @@ -161,7 +173,7 @@ function DictQuickLookup:update() background = 0, VerticalGroup:new{ align = "left", - self.dict_title, + self.dict_bar, title_bar, -- word CenterContainer:new{ @@ -189,10 +201,14 @@ function DictQuickLookup:update() } } } - - self[1] = CenterContainer:new{ - dimen = Screen:getSize(), - self.dict_frame, + self[1] = WidgetContainer:new{ + align = self.align, + dimen = self.region:copy(), + FrameContainer:new{ + bordersize = 0, + padding = Screen:scaleByDPI(5), + self.dict_frame, + } } UIManager.repaint_all = true UIManager.full_refresh = true @@ -267,8 +283,7 @@ end function DictQuickLookup:onTapCloseDict(arg, ges_ev) if ges_ev.pos:notIntersectWith(self.dict_frame.dimen) then - UIManager:close(self) - self.highlight:handleEvent(Event:new("Tap")) + self:onClose() return true elseif not ges_ev.pos:notIntersectWith(self.dict_title.dimen) then self.ui:handleEvent(Event:new("UpdateDefaultDict", self.dictionary)) @@ -277,4 +292,10 @@ function DictQuickLookup:onTapCloseDict(arg, ges_ev) return true end +function DictQuickLookup:onClose() + UIManager:close(self) + self.highlight:handleEvent(Event:new("Tap")) + return true +end + return DictQuickLookup