From d83de472040ebcd892cb792f50ed724b386e2e6d Mon Sep 17 00:00:00 2001 From: Hzj_jie Date: Mon, 19 Jun 2017 19:00:36 -0700 Subject: [PATCH] Add onFrontlightStateChanged event (#2966) So we do not need the hacky solution in KoboLight plugin anymore. PageFooter can receive and process the event itself. --- frontend/apps/reader/modules/readerfooter.lua | 6 ++++++ frontend/device/generic/powerd.lua | 21 ++++++++++++++----- plugins/kobolight.koplugin/main.lua | 6 ------ 3 files changed, 22 insertions(+), 11 deletions(-) diff --git a/frontend/apps/reader/modules/readerfooter.lua b/frontend/apps/reader/modules/readerfooter.lua index 03d8005e0..d8196afcc 100644 --- a/frontend/apps/reader/modules/readerfooter.lua +++ b/frontend/apps/reader/modules/readerfooter.lua @@ -668,4 +668,10 @@ function ReaderFooter:onSuspend() end end +function ReaderFooter:onFrontlightStateChanged() + if self.settings.frontlight then + self:updateFooter() + end +end + return ReaderFooter diff --git a/frontend/device/generic/powerd.lua b/frontend/device/generic/powerd.lua index 82d734fbe..d2a4cf2e4 100644 --- a/frontend/device/generic/powerd.lua +++ b/frontend/device/generic/powerd.lua @@ -35,8 +35,8 @@ function BasePowerD:setIntensityHW(intensity) end function BasePowerD:getCapacityHW() return 0 end function BasePowerD:isChargingHW() return false end function BasePowerD:frontlightIntensityHW() return 0 end -function BasePowerD:turnOffFrontlightHW() self:setIntensityHW(self.fl_min) end -function BasePowerD:turnOnFrontlightHW() self:setIntensityHW(self.fl_intensity) end +function BasePowerD:turnOffFrontlightHW() self:_setIntensity(self.fl_min) end +function BasePowerD:turnOnFrontlightHW() self:_setIntensity(self.fl_intensity) end -- Anything needs to be done before do a real hardware suspend. Such as turn off -- front light. function BasePowerD:beforeSuspend() end @@ -80,8 +80,8 @@ function BasePowerD:turnOffFrontlight() assert(self ~= nil) if not self.device.hasFrontlight() then return end if self:isFrontlightOff() then return false end - self:turnOffFrontlightHW() self.is_fl_on = false + self:turnOffFrontlightHW() return true end @@ -90,8 +90,8 @@ function BasePowerD:turnOnFrontlight() if not self.device.hasFrontlight() then return end if self:isFrontlightOn() then return false end if self.fl_intensity == self.fl_min then return false end - self:turnOnFrontlightHW() self.is_fl_on = true + self:turnOnFrontlightHW() return true end @@ -128,7 +128,7 @@ function BasePowerD:setIntensity(intensity) self.fl_intensity = self:normalizeIntensity(intensity) self:_decideFrontlightState() logger.dbg("set light intensity", self.fl_intensity) - self:setIntensityHW(self.fl_intensity) + self:_setIntensity(self.fl_intensity) return true end @@ -144,4 +144,15 @@ function BasePowerD:isCharging() return self:isChargingHW() end +function BasePowerD:_setIntensity(intensity) + self:setIntensityHW(intensity) + -- BasePowerD is loaded before UIManager. So we cannot broadcast events before UIManager has + -- been loaded. + if package.loaded["ui/uimanager"] ~= nil then + local Event = require("ui/event") + local UIManager = require("ui/uimanager") + UIManager:broadcastEvent(Event:new("FrontlightStateChanged")) + end +end + return BasePowerD diff --git a/plugins/kobolight.koplugin/main.lua b/plugins/kobolight.koplugin/main.lua index 6ff30ac74..dde5aee01 100644 --- a/plugins/kobolight.koplugin/main.lua +++ b/plugins/kobolight.koplugin/main.lua @@ -122,9 +122,6 @@ 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 @@ -153,9 +150,6 @@ function KoboLight:onSwipe(_, ges) powerd:setIntensity(new_intensity) self:onShowIntensity() end - if self.view.footer_visible and self.view.footer.settings.frontlight then - self.view.footer:updateFooter() - end return true end