mirror of
https://github.com/koreader/koreader.git
synced 2025-08-10 00:52:38 +00:00
add multi-dictionary support
This commit is contained in:
@@ -1,16 +1,27 @@
|
||||
require "ui/widget/container"
|
||||
require "ui/widget/buttontable"
|
||||
|
||||
--[[
|
||||
Display quick lookup word definition
|
||||
]]
|
||||
DictQuickLookup = InputContainer:new{
|
||||
dict = nil,
|
||||
results = nil,
|
||||
lookupword = nil,
|
||||
dictionary = nil,
|
||||
definition = nil,
|
||||
id = nil,
|
||||
lang = nil,
|
||||
|
||||
dict_index = 1,
|
||||
title_face = Font:getFace("tfont", 20),
|
||||
word_face = Font:getFace("tfont", 18),
|
||||
content_face = Font:getFace("cfont", 18),
|
||||
width = Screen:getWidth() - scaleByDPI(100),
|
||||
|
||||
title_padding = scaleByDPI(5),
|
||||
title_margin = scaleByDPI(2),
|
||||
word_padding = scaleByDPI(5),
|
||||
word_margin = scaleByDPI(2),
|
||||
definition_padding = scaleByDPI(5),
|
||||
definition_margin = scaleByDPI(2),
|
||||
button_padding = scaleByDPI(14),
|
||||
}
|
||||
|
||||
function DictQuickLookup:init()
|
||||
@@ -20,7 +31,7 @@ function DictQuickLookup:init()
|
||||
seqtext = "any key", doc = _("close dialog") }
|
||||
}
|
||||
else
|
||||
self.ges_events.TapClose = {
|
||||
self.ges_events.TapCloseDict = {
|
||||
GestureRange:new{
|
||||
ges = "tap",
|
||||
range = Geom:new{
|
||||
@@ -31,29 +42,172 @@ function DictQuickLookup:init()
|
||||
}
|
||||
}
|
||||
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 = Screen:getWidth() - 100,
|
||||
self:changeToDefaultDict()
|
||||
end
|
||||
|
||||
function DictQuickLookup:update()
|
||||
-- dictionary title
|
||||
self.dict_title = FrameContainer:new{
|
||||
padding = self.title_padding,
|
||||
margin = self.title_margin,
|
||||
bordersize = 0,
|
||||
TextWidget:new{
|
||||
text = self.dictionary,
|
||||
face = self.title_face,
|
||||
width = self.width,
|
||||
}
|
||||
}
|
||||
-- lookup word
|
||||
local lookup_word = FrameContainer:new{
|
||||
padding = self.word_padding,
|
||||
margin = self.word_margin,
|
||||
bordersize = 0,
|
||||
TextBoxWidget:new{
|
||||
text = self.lookupword,
|
||||
face = self.word_face,
|
||||
width = self.width,
|
||||
},
|
||||
}
|
||||
-- word definition
|
||||
local definition = FrameContainer:new{
|
||||
padding = self.definition_padding,
|
||||
margin = self.definition_margin,
|
||||
bordersize = 0,
|
||||
TextBoxWidget:new{
|
||||
text = self.definition,
|
||||
face = self.content_face,
|
||||
width = self.width,
|
||||
},
|
||||
}
|
||||
local button_table = ButtonTable:new{
|
||||
width = math.max(self.width, definition:getSize().w),
|
||||
buttons = {
|
||||
{
|
||||
{
|
||||
text = _("<<"),
|
||||
enabled = self:isPrevDictAvaiable(),
|
||||
callback = function()
|
||||
self:changeToPrevDict()
|
||||
end,
|
||||
},
|
||||
VerticalSpan:new{ width = 20 },
|
||||
TextBoxWidget:new{
|
||||
text = self.definition,
|
||||
face = self.content_face,
|
||||
width = Screen:getWidth() - 100,
|
||||
}
|
||||
{
|
||||
text = _(">>"),
|
||||
enabled = self:isNextDictAvaiable(),
|
||||
callback = function()
|
||||
self:changeToNextDict()
|
||||
end,
|
||||
},
|
||||
},
|
||||
{
|
||||
{
|
||||
text = _("Highlight"),
|
||||
enabled = false,
|
||||
callback = function()
|
||||
self.ui:handleEvent(Event:new("Highlight"))
|
||||
end,
|
||||
},
|
||||
{
|
||||
text = _("Add Note"),
|
||||
enabled = false,
|
||||
callback = function()
|
||||
self.ui:handleEvent(Event:new("AddNote"))
|
||||
end,
|
||||
},
|
||||
},
|
||||
},
|
||||
zero_sep = true,
|
||||
}
|
||||
local title_bar = LineWidget:new{
|
||||
--background = 8,
|
||||
dimen = Geom:new{
|
||||
w = button_table:getSize().w + self.button_padding,
|
||||
h = scaleByDPI(2),
|
||||
}
|
||||
}
|
||||
|
||||
self.dict_frame = FrameContainer:new{
|
||||
radius = scaleByDPI(8),
|
||||
bordersize = scaleByDPI(3),
|
||||
padding = 0,
|
||||
margin = 0,
|
||||
background = 0,
|
||||
VerticalGroup:new{
|
||||
align = "left",
|
||||
self.dict_title,
|
||||
title_bar,
|
||||
-- word
|
||||
lookup_word,
|
||||
-- definition
|
||||
definition,
|
||||
-- buttons
|
||||
CenterContainer:new{
|
||||
dimen = Geom:new{
|
||||
w = title_bar:getSize().w,
|
||||
h = button_table:getSize().h,
|
||||
},
|
||||
button_table,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
self[1] = CenterContainer:new{
|
||||
dimen = Screen:getSize(),
|
||||
self.dict_frame,
|
||||
}
|
||||
end
|
||||
|
||||
function DictQuickLookup:isPrevDictAvaiable()
|
||||
return self.dict_index > 1
|
||||
end
|
||||
|
||||
function DictQuickLookup:isNextDictAvaiable()
|
||||
return self.dict_index < #self.results
|
||||
end
|
||||
|
||||
function DictQuickLookup:changeToPrevDict()
|
||||
self:changeDictionary(self.dict_index - 1)
|
||||
end
|
||||
|
||||
function DictQuickLookup:changeToNextDict()
|
||||
self:changeDictionary(self.dict_index + 1)
|
||||
end
|
||||
|
||||
function DictQuickLookup:changeDictionary(index)
|
||||
self.dict_index = index
|
||||
self.dictionary = self.results[index].dict
|
||||
self.lookupword = self.results[index].word
|
||||
self.definition = self.results[index].definition
|
||||
self:update()
|
||||
UIManager.repaint_all = true
|
||||
end
|
||||
|
||||
function DictQuickLookup:changeToDefaultDict()
|
||||
if self.dictionary then
|
||||
-- dictionaries that have definition of the first word(accurate word)
|
||||
-- excluding Fuzzy queries.
|
||||
local n_accurate_dicts = nil
|
||||
local default_word = self.results[1].word
|
||||
for i=1, #self.results do
|
||||
if self.results[i].word == default_word then
|
||||
n_accurate_dicts = i
|
||||
else
|
||||
break
|
||||
end
|
||||
end
|
||||
-- change to dictionary specified by self.dictionary
|
||||
for i=1, n_accurate_dicts do
|
||||
if self.results[i].dict == self.dictionary then
|
||||
self:changeDictionary(i)
|
||||
break
|
||||
end
|
||||
-- cannot find definition in default dictionary
|
||||
if i == n_accurate_dicts then
|
||||
self:changeDictionary(1)
|
||||
end
|
||||
end
|
||||
else
|
||||
self:changeDictionary(1)
|
||||
end
|
||||
end
|
||||
|
||||
function DictQuickLookup:onAnyKeyPressed()
|
||||
@@ -62,8 +216,14 @@ function DictQuickLookup:onAnyKeyPressed()
|
||||
return true
|
||||
end
|
||||
|
||||
function DictQuickLookup:onTapClose()
|
||||
UIManager:close(self)
|
||||
self.ui:handleEvent(Event:new("Tap"))
|
||||
function DictQuickLookup:onTapCloseDict(arg, ges_ev)
|
||||
if ges_ev.pos:notIntersectWith(self.dict_frame.dimen) then
|
||||
UIManager:close(self)
|
||||
self.ui:handleEvent(Event:new("Tap"))
|
||||
return true
|
||||
elseif not ges_ev.pos:notIntersectWith(self.dict_title.dimen) then
|
||||
self.ui:handleEvent(Event:new("UpdateDefaultDict", self.dictionary))
|
||||
return true
|
||||
end
|
||||
return true
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user