mirror of https://github.com/koreader/koreader
add a demo version dictionary
parent
28d902f006
commit
695e095a1a
@ -1,24 +1,55 @@
|
||||
require "ui/device"
|
||||
require "ui/widget/dict"
|
||||
|
||||
ReaderDictionary = EventListener:new{}
|
||||
|
||||
function ReaderDictionary:init()
|
||||
local dev_mod = Device:getModel()
|
||||
if dev_mod == "KindlePaperWhite" or dev_mod == "KindleTouch" then
|
||||
require "liblipclua"
|
||||
require("liblipclua")
|
||||
JSON = require("JSON")
|
||||
self.lipc_handle = lipc.init("com.github.koreader.dictionary")
|
||||
end
|
||||
end
|
||||
|
||||
function ReaderDictionary:onLookupWord(word)
|
||||
DEBUG("lookup word:", word)
|
||||
if self.lipc_handle and word then
|
||||
-- start indicator depends on pillow being enabled
|
||||
--self:quickLookup()
|
||||
if self.lipc_handle and JSON and word then
|
||||
self.lipc_handle:set_string_property(
|
||||
"com.lab126.booklet.kpvbooklet.dict", "lookup", word)
|
||||
local definitions = self.lipc_handle:get_string_property(
|
||||
local results_str = self.lipc_handle:get_string_property(
|
||||
"com.lab126.booklet.kpvbooklet.word", word)
|
||||
DEBUG("definitions of word:", word, definitions)
|
||||
if results_str then
|
||||
--DEBUG("def str:", word, definitions)
|
||||
local ok, results_tab = pcall(JSON.decode, JSON, results_str)
|
||||
--DEBUG("lookup result table:", word, results_tab)
|
||||
if results_tab[1] then
|
||||
self:quickLookup(results_tab[1])
|
||||
end
|
||||
end
|
||||
end
|
||||
return true
|
||||
end
|
||||
|
||||
function ReaderDictionary:quickLookup(result)
|
||||
-- UIManager:show(DictQuickLookup:new{
|
||||
-- dict = "Oxford Dictionary of English",
|
||||
-- definition = "coordination n. [mass noun] 1 the organization of the different elements of a \
|
||||
-- complex body or activity so as to enable them to work together effectively: an important managerial \
|
||||
-- task is the control and coordination of activities. cooperative effort resulting in an effective \
|
||||
-- relationship: action groups work in coordination with local groups to end rainforest destruction. \
|
||||
-- the ability to use different parts of the body together smoothly and efficiently: changing from \
|
||||
-- one foot position to another requires coordination and balance.",
|
||||
-- id = "/mnt/us/documents/dictionaries/Oxford_Dictionary_of_English.azw",
|
||||
-- lang = "en",
|
||||
-- })
|
||||
if result then
|
||||
UIManager:show(DictQuickLookup:new{
|
||||
dict = result.dict,
|
||||
definition = result.definition,
|
||||
id = result.ID,
|
||||
lang = result.lang,
|
||||
})
|
||||
end
|
||||
end
|
||||
|
@ -0,0 +1,69 @@
|
||||
require "ui/widget/container"
|
||||
|
||||
--[[
|
||||
Display quick lookup word definition
|
||||
]]
|
||||
DictQuickLookup = InputContainer:new{
|
||||
dict = nil,
|
||||
definition = nil,
|
||||
id = nil,
|
||||
lang = nil,
|
||||
|
||||
title_face = Font:getFace("tfont", 20),
|
||||
content_face = Font:getFace("cfont", 18),
|
||||
width = Screen:getWidth() - 100
|
||||
}
|
||||
|
||||
function DictQuickLookup:init()
|
||||
if Device:hasKeyboard() then
|
||||
key_events = {
|
||||
AnyKeyPressed = { { Input.group.Any },
|
||||
seqtext = "any key", doc = _("close dialog") }
|
||||
}
|
||||
else
|
||||
self.ges_events.TapClose = {
|
||||
GestureRange:new{
|
||||
ges = "tap",
|
||||
range = Geom:new{
|
||||
x = 0, y = 0,
|
||||
w = Screen:getWidth(),
|
||||
h = Screen:getHeight(),
|
||||
}
|
||||
}
|
||||
}
|
||||
end
|
||||
-- we construct the actual content here because self.text is only available now
|
||||
self[1] = CenterContainer:new{
|
||||
dimen = Screen:getSize(),
|
||||
FrameContainer:new{
|
||||
margin = 2,
|
||||
background = 0,
|
||||
VerticalGroup:new{
|
||||
align = "center",
|
||||
-- title bar
|
||||
TextBoxWidget:new{
|
||||
text = self.dict,
|
||||
face = self.title_face,
|
||||
width = self.width,
|
||||
},
|
||||
VerticalSpan:new{ width = 20 },
|
||||
TextBoxWidget:new{
|
||||
text = self.definition,
|
||||
face = self.content_face,
|
||||
width = self.width,
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
end
|
||||
|
||||
function DictQuickLookup:onAnyKeyPressed()
|
||||
-- triggered by our defined key events
|
||||
UIManager:close(self)
|
||||
return true
|
||||
end
|
||||
|
||||
function DictQuickLookup:onTapClose()
|
||||
UIManager:close(self)
|
||||
return true
|
||||
end
|
Loading…
Reference in New Issue