mirror of https://github.com/koreader/koreader
kobolight(refactor): move into plugin & only enable for kobo
parent
0c49b915de
commit
a6c506dc10
@ -1,132 +0,0 @@
|
|||||||
local InputContainer = require("ui/widget/container/inputcontainer")
|
|
||||||
local LeftContainer = require("ui/widget/container/leftcontainer")
|
|
||||||
local GestureRange = require("ui/gesturerange")
|
|
||||||
local Device = require("device")
|
|
||||||
local Geom = require("ui/geometry")
|
|
||||||
local Screen = Device.screen
|
|
||||||
local DEBUG = require("dbg")
|
|
||||||
local UIManager = require("ui/uimanager")
|
|
||||||
local Notification = require("ui/widget/notification")
|
|
||||||
local T = require("ffi/util").template
|
|
||||||
local _ = require("gettext")
|
|
||||||
|
|
||||||
local DTAP_ZONE_KOBOLIGHTTOGGLE = {x = 0, y = 0.9375, w = 0.1, h = 0.0625 }
|
|
||||||
local DTAP_ZONE_KOBOLIGHTSWIPE = {x = 0, y = 0.125, w = 0.1, h = 0.875 }
|
|
||||||
|
|
||||||
local ReaderKoboLight = InputContainer:new{
|
|
||||||
steps = {0,1,1,1,1,2,2,2,3,4,5,6,7,8,9,10},
|
|
||||||
gestureScale = Screen:getHeight() * DTAP_ZONE_KOBOLIGHTSWIPE.h * 0.8,
|
|
||||||
}
|
|
||||||
|
|
||||||
function ReaderKoboLight:init()
|
|
||||||
self[1] = LeftContainer:new{
|
|
||||||
dimen = Geom:new{w = nil, h = nil},
|
|
||||||
}
|
|
||||||
self:resetLayout()
|
|
||||||
end
|
|
||||||
|
|
||||||
function ReaderKoboLight:resetLayout()
|
|
||||||
local new_screen_width = Screen:getWidth()
|
|
||||||
if new_screen_width == self[1].dimen.w then return end
|
|
||||||
local new_screen_height = Screen:getHeight()
|
|
||||||
self[1].dimen.w = new_screen_width
|
|
||||||
self[1].dimen.h = new_screen_height
|
|
||||||
self.gestureScale = new_screen_height * DTAP_ZONE_KOBOLIGHTSWIPE.h * 0.8
|
|
||||||
|
|
||||||
if Device:isTouchDevice() then
|
|
||||||
self.ges_events = {
|
|
||||||
Tap = {
|
|
||||||
GestureRange:new{
|
|
||||||
ges = "tap",
|
|
||||||
range = Geom:new{
|
|
||||||
x = new_screen_width*DTAP_ZONE_KOBOLIGHTTOGGLE.x,
|
|
||||||
y = new_screen_height*DTAP_ZONE_KOBOLIGHTTOGGLE.y,
|
|
||||||
w = new_screen_width*DTAP_ZONE_KOBOLIGHTTOGGLE.w,
|
|
||||||
h = new_screen_height*DTAP_ZONE_KOBOLIGHTTOGGLE.h
|
|
||||||
}
|
|
||||||
}
|
|
||||||
},
|
|
||||||
Swipe = {
|
|
||||||
GestureRange:new{
|
|
||||||
ges = "swipe",
|
|
||||||
range = Geom:new{
|
|
||||||
x = new_screen_width*DTAP_ZONE_KOBOLIGHTSWIPE.x,
|
|
||||||
y = new_screen_height*DTAP_ZONE_KOBOLIGHTSWIPE.y,
|
|
||||||
w = new_screen_width*DTAP_ZONE_KOBOLIGHTSWIPE.w,
|
|
||||||
h = new_screen_height*DTAP_ZONE_KOBOLIGHTSWIPE.h
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
function ReaderKoboLight:onShowIntensity()
|
|
||||||
local powerd = Device:getPowerDevice()
|
|
||||||
if powerd.fl_intensity ~= nil then
|
|
||||||
UIManager:show(Notification:new{
|
|
||||||
text = T(_("Frontlight intensity is set to %1."), powerd.fl_intensity),
|
|
||||||
timeout = 1.0,
|
|
||||||
})
|
|
||||||
end
|
|
||||||
return true
|
|
||||||
end
|
|
||||||
|
|
||||||
function ReaderKoboLight:onShowOnOff()
|
|
||||||
local powerd = Device:getPowerDevice()
|
|
||||||
local new_text
|
|
||||||
if powerd.is_fl_on then
|
|
||||||
new_text = _("Frontlight is on.")
|
|
||||||
else
|
|
||||||
new_text = _("Frontlight is off.")
|
|
||||||
end
|
|
||||||
UIManager:show(Notification:new{
|
|
||||||
text = new_text,
|
|
||||||
timeout = 1.0,
|
|
||||||
})
|
|
||||||
return true
|
|
||||||
end
|
|
||||||
|
|
||||||
function ReaderKoboLight:onTap()
|
|
||||||
Device:getPowerDevice():toggleFrontlight()
|
|
||||||
self:onShowOnOff()
|
|
||||||
if self.view.footer_visible and self.view.footer.settings.frontlight then
|
|
||||||
self.view.footer:updateFooter()
|
|
||||||
end
|
|
||||||
return true
|
|
||||||
end
|
|
||||||
|
|
||||||
function ReaderKoboLight:onSwipe(arg, ges)
|
|
||||||
local powerd = Device:getPowerDevice()
|
|
||||||
if powerd.fl_intensity == nil then return true end
|
|
||||||
|
|
||||||
DEBUG("frontlight intensity", powerd.fl_intensity)
|
|
||||||
local step = math.ceil(#self.steps * ges.distance / self.gestureScale)
|
|
||||||
DEBUG("step = ", step)
|
|
||||||
local delta_int = self.steps[step] or self.steps[#self.steps]
|
|
||||||
DEBUG("delta_int = ", delta_int)
|
|
||||||
local new_intensity
|
|
||||||
if ges.direction == "north" then
|
|
||||||
new_intensity = powerd.fl_intensity + delta_int
|
|
||||||
elseif ges.direction == "south" then
|
|
||||||
new_intensity = powerd.fl_intensity - delta_int
|
|
||||||
end
|
|
||||||
if new_intensity ~= nil then
|
|
||||||
-- when new_intensity <=0, toggle light off
|
|
||||||
if new_intensity <=0 then
|
|
||||||
if powerd.is_fl_on then
|
|
||||||
powerd:toggleFrontlight()
|
|
||||||
end
|
|
||||||
self:onShowOnOff()
|
|
||||||
else -- general case
|
|
||||||
powerd:setIntensity(new_intensity)
|
|
||||||
self:onShowIntensity()
|
|
||||||
end
|
|
||||||
if self.view.footer_visible and self.view.footer.settings.frontlight then
|
|
||||||
self.view.footer:updateFooter()
|
|
||||||
end
|
|
||||||
end
|
|
||||||
return true
|
|
||||||
end
|
|
||||||
|
|
||||||
return ReaderKoboLight
|
|
@ -0,0 +1,142 @@
|
|||||||
|
local Device = require("device")
|
||||||
|
|
||||||
|
if not (Device:isKobo() and Device:hasFrontlight()) then
|
||||||
|
return { disabled = true, }
|
||||||
|
end
|
||||||
|
|
||||||
|
local WidgetContainer = require("ui/widget/container/widgetcontainer")
|
||||||
|
local Screen = Device.screen
|
||||||
|
local UIManager = require("ui/uimanager")
|
||||||
|
local Notification = require("ui/widget/notification")
|
||||||
|
local T = require("ffi/util").template
|
||||||
|
local _ = require("gettext")
|
||||||
|
|
||||||
|
local tap_touch_zone_ratio = { x = 0, y = 15/16, w = 1/10, h = 1/16, }
|
||||||
|
local swipe_touch_zone_ratio = { x = 0, y = 1/8, w = 1/10, h = 7/8, }
|
||||||
|
|
||||||
|
|
||||||
|
local KoboLight = WidgetContainer:new{
|
||||||
|
name = 'kobolight',
|
||||||
|
steps = { 0, 1, 1, 1, 1, 2, 2, 2, 3, 4, 5, 6, 7, 8, 9, 10, },
|
||||||
|
gestureScale = nil, -- initialized in self:resetLayout()
|
||||||
|
}
|
||||||
|
|
||||||
|
function KoboLight:onReaderReady()
|
||||||
|
self:setupTouchZones()
|
||||||
|
self:resetLayout()
|
||||||
|
end
|
||||||
|
|
||||||
|
function KoboLight:setupTouchZones()
|
||||||
|
if not Device:isTouchDevice() then return end
|
||||||
|
local swipe_zone = {
|
||||||
|
ratio_x = swipe_touch_zone_ratio.x, ratio_y = swipe_touch_zone_ratio.y,
|
||||||
|
ratio_w = swipe_touch_zone_ratio.w, ratio_h = swipe_touch_zone_ratio.h,
|
||||||
|
}
|
||||||
|
self.ui:registerTouchZones({
|
||||||
|
{
|
||||||
|
id = "plugin_kobolight_tap",
|
||||||
|
ges = "tap",
|
||||||
|
screen_zone = {
|
||||||
|
ratio_x = tap_touch_zone_ratio.x, ratio_y = tap_touch_zone_ratio.y,
|
||||||
|
ratio_w = tap_touch_zone_ratio.w, ratio_h = tap_touch_zone_ratio.h,
|
||||||
|
},
|
||||||
|
handler = function() return self:onTap() end,
|
||||||
|
overrides = { 'footer_tap' },
|
||||||
|
},
|
||||||
|
{
|
||||||
|
id = "plugin_kobolight_swipe",
|
||||||
|
ges = "swipe",
|
||||||
|
screen_zone = swipe_zone,
|
||||||
|
handler = function(ges) return self:onSwipe(nil, ges) end,
|
||||||
|
overrides = { 'paging_swipe', 'rolling_swipe', },
|
||||||
|
},
|
||||||
|
{
|
||||||
|
-- dummy zone to disable reader panning
|
||||||
|
id = "plugin_kobolight_pan",
|
||||||
|
ges = "pan",
|
||||||
|
screen_zone = swipe_zone,
|
||||||
|
handler = function(ges) return true end,
|
||||||
|
overrides = { 'paging_pan', 'rolling_pan', },
|
||||||
|
},
|
||||||
|
{
|
||||||
|
-- dummy zone to disable reader panning
|
||||||
|
id = "plugin_kobolight_pan_release",
|
||||||
|
ges = "pan_release",
|
||||||
|
screen_zone = swipe_zone,
|
||||||
|
handler = function(ges) return true end,
|
||||||
|
overrides = { 'paging_pan_release', },
|
||||||
|
},
|
||||||
|
})
|
||||||
|
end
|
||||||
|
|
||||||
|
function KoboLight:resetLayout()
|
||||||
|
local new_screen_height = Screen:getHeight()
|
||||||
|
self.gestureScale = new_screen_height * swipe_touch_zone_ratio.h * 0.8
|
||||||
|
end
|
||||||
|
|
||||||
|
function KoboLight:onShowIntensity()
|
||||||
|
local powerd = Device:getPowerDevice()
|
||||||
|
if powerd.fl_intensity ~= nil then
|
||||||
|
UIManager:show(Notification:new{
|
||||||
|
text = T(_("Frontlight intensity is set to %1."), powerd.fl_intensity),
|
||||||
|
timeout = 1.0,
|
||||||
|
})
|
||||||
|
end
|
||||||
|
return true
|
||||||
|
end
|
||||||
|
|
||||||
|
function KoboLight:onShowOnOff()
|
||||||
|
local powerd = Device:getPowerDevice()
|
||||||
|
local new_text
|
||||||
|
if powerd.is_fl_on then
|
||||||
|
new_text = _("Frontlight is on.")
|
||||||
|
else
|
||||||
|
new_text = _("Frontlight is off.")
|
||||||
|
end
|
||||||
|
UIManager:show(Notification:new{
|
||||||
|
text = new_text,
|
||||||
|
timeout = 1.0,
|
||||||
|
})
|
||||||
|
return true
|
||||||
|
end
|
||||||
|
|
||||||
|
function KoboLight:onTap()
|
||||||
|
Device:getPowerDevice():toggleFrontlight()
|
||||||
|
self:onShowOnOff()
|
||||||
|
if self.view.footer_visible and self.view.footer.settings.frontlight then
|
||||||
|
self.view.footer:updateFooter()
|
||||||
|
end
|
||||||
|
return true
|
||||||
|
end
|
||||||
|
|
||||||
|
function KoboLight:onSwipe(_, ges)
|
||||||
|
local powerd = Device:getPowerDevice()
|
||||||
|
if powerd.fl_intensity == nil then return true end
|
||||||
|
|
||||||
|
local step = math.ceil(#self.steps * ges.distance / self.gestureScale)
|
||||||
|
local delta_int = self.steps[step] or self.steps[#self.steps]
|
||||||
|
local new_intensity
|
||||||
|
if ges.direction == "north" then
|
||||||
|
new_intensity = powerd.fl_intensity + delta_int
|
||||||
|
elseif ges.direction == "south" then
|
||||||
|
new_intensity = powerd.fl_intensity - delta_int
|
||||||
|
end
|
||||||
|
if new_intensity ~= nil then
|
||||||
|
-- when new_intensity <=0, toggle light off
|
||||||
|
if new_intensity <=0 then
|
||||||
|
if powerd.is_fl_on then
|
||||||
|
powerd:toggleFrontlight()
|
||||||
|
end
|
||||||
|
self:onShowOnOff()
|
||||||
|
else -- general case
|
||||||
|
powerd:setIntensity(new_intensity)
|
||||||
|
self:onShowIntensity()
|
||||||
|
end
|
||||||
|
if self.view.footer_visible and self.view.footer.settings.frontlight then
|
||||||
|
self.view.footer:updateFooter()
|
||||||
|
end
|
||||||
|
end
|
||||||
|
return true
|
||||||
|
end
|
||||||
|
|
||||||
|
return KoboLight
|
Loading…
Reference in New Issue