mirror of
https://github.com/koreader/koreader.git
synced 2025-12-13 20:36:53 +01:00
kobolight(refactor): move into plugin & only enable for kobo
This commit is contained in:
@@ -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
|
||||
@@ -23,9 +23,9 @@ function PluginLoader:loadPlugins()
|
||||
if not ok then
|
||||
DEBUG("Error when loading", mainfile, plugin_module)
|
||||
end
|
||||
package.path = package_path
|
||||
package.cpath = package_cpath
|
||||
if ok then
|
||||
if not plugin_module.disabled and ok then
|
||||
package.path = package_path
|
||||
package.cpath = package_cpath
|
||||
plugin_module.path = path
|
||||
plugin_module.name = plugin_module.name or path:match("/(.-)%.koplugin")
|
||||
table.insert(self.plugins, plugin_module)
|
||||
|
||||
@@ -100,6 +100,7 @@ function ReaderUI:init()
|
||||
self.doc_settings = DocSettings:open(self.document.file)
|
||||
|
||||
-- a view container (so it must be child #1!)
|
||||
-- all paintable widgets need to be a child of reader view
|
||||
self:registerModule("view", ReaderView:new{
|
||||
dialog = self.dialog,
|
||||
dimen = self.dimen,
|
||||
@@ -317,15 +318,6 @@ function ReaderUI:init()
|
||||
})
|
||||
end
|
||||
|
||||
local ReaderKoboLight = require("apps/reader/modules/readerkobolight")
|
||||
if (Device:isKobo() and Device:hasFrontlight()) then
|
||||
self:registerModule('kobolight', ReaderKoboLight:new{
|
||||
dialog = self.dialog,
|
||||
view = self.view,
|
||||
ui = self,
|
||||
})
|
||||
end
|
||||
|
||||
-- we only read settings after all the widgets are initialized
|
||||
self:handleEvent(Event:new("ReadSettings", self.doc_settings))
|
||||
|
||||
|
||||
@@ -419,4 +419,3 @@ function EvernoteExporter:exportBooknotesToHTML(title, booknotes)
|
||||
end
|
||||
|
||||
return EvernoteExporter
|
||||
|
||||
|
||||
142
plugins/kobolight.koplugin/main.lua
Normal file
142
plugins/kobolight.koplugin/main.lua
Normal file
@@ -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
|
||||
@@ -1,7 +1,8 @@
|
||||
describe("NetworkSetting module", function()
|
||||
local NetworkSetting, NetworkMgr
|
||||
local NetworkSetting, NetworkMgr, UIManager
|
||||
setup(function()
|
||||
require("commonrequire")
|
||||
UIManager = require("ui/uimanager")
|
||||
NetworkSetting = require("ui/widget/networksetting")
|
||||
NetworkMgr = require("ui/network/manager")
|
||||
end)
|
||||
@@ -15,6 +16,7 @@ describe("NetworkSetting module", function()
|
||||
stub(NetworkMgr, "disconnectNetwork")
|
||||
stub(NetworkMgr, "releaseIP")
|
||||
|
||||
UIManager:quit()
|
||||
local called = false
|
||||
local network_list = {
|
||||
{
|
||||
@@ -41,6 +43,7 @@ describe("NetworkSetting module", function()
|
||||
stub(NetworkMgr, "disconnectNetwork")
|
||||
stub(NetworkMgr, "releaseIP")
|
||||
|
||||
UIManager:quit()
|
||||
local network_list = {
|
||||
{
|
||||
ssid = "foo",
|
||||
|
||||
Reference in New Issue
Block a user