add Kindle-like dictionary window follow-up

This commit is contained in:
chrox
2013-12-26 22:40:40 +08:00
parent f12d8eb9b4
commit 13d70eec9b
4 changed files with 99 additions and 17 deletions

View File

@@ -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