mirror of
https://github.com/koreader/koreader
synced 2024-10-31 21:20:20 +00:00
Merge pull request #416 from chrox/master
add Kindle-like dictionary window follow-up
This commit is contained in:
commit
1b3a28bd0c
@ -350,6 +350,16 @@ function GestureDetector:handleNonTap(tev)
|
||||
return self:switchState("holdState", tev, true)
|
||||
end
|
||||
end, deadline)
|
||||
DEBUG("handle non-tap", tev)
|
||||
return {
|
||||
ges = "touch",
|
||||
pos = Geom:new{
|
||||
x = tev.x,
|
||||
y = tev.y,
|
||||
w = 0, h = 0,
|
||||
},
|
||||
time = tev.timev,
|
||||
}
|
||||
else
|
||||
-- it is not end of touch event, see if we need to switch to
|
||||
-- other states
|
||||
|
@ -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
|
||||
|
@ -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
|
||||
|
||||
|
45
frontend/ui/widget/closebutton.lua
Normal file
45
frontend/ui/widget/closebutton.lua
Normal file
@ -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
|
@ -40,7 +40,20 @@ end
|
||||
function WidgetContainer:paintTo(bb, x, y)
|
||||
-- default to pass request to first child widget
|
||||
if self[1] then
|
||||
return self[1]:paintTo(bb, x, y)
|
||||
x = x + (self.dimen.x or 0)
|
||||
y = y + (self.dimen.y or 0)
|
||||
if self.align == "top" then
|
||||
local contentSize = self[1]:getSize()
|
||||
self[1]:paintTo(bb,
|
||||
x + math.floor((self.dimen.w - contentSize.w)/2), y)
|
||||
elseif self.align == "bottom" then
|
||||
local contentSize = self[1]:getSize()
|
||||
self[1]:paintTo(bb,
|
||||
x + math.floor((self.dimen.w - contentSize.w)/2),
|
||||
y + (self.dimen.h - contentSize.h))
|
||||
else
|
||||
return self[1]:paintTo(bb, x, y)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
|
@ -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
|
||||
|
@ -1 +1 @@
|
||||
Subproject commit 1f45c486f7ddbe43156ec15633f2b188aba126cb
|
||||
Subproject commit ffec35674a11d13750224ce1b00704dea0f3f762
|
Loading…
Reference in New Issue
Block a user