[AutoWarmth] use CheckButton for night mode toggle (#10762)

reviewable/pr10778/r1
zwim 10 months ago committed by GitHub
parent 724db0b924
commit 2ba3353d8a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -49,7 +49,7 @@ local SpinWidget = FocusManager:extend{
-- Optional extra button above ok/cancel buttons row -- Optional extra button above ok/cancel buttons row
option_text = nil, option_text = nil,
option_callback = nil, option_callback = nil,
unit = nil, unit = nil, -- unit to show or nil
} }
function SpinWidget:init() function SpinWidget:init()
@ -226,30 +226,30 @@ function SpinWidget:update(numberpicker_value, numberpicker_value_index)
show_parent = self, show_parent = self,
} }
self:mergeLayoutInVertical(ok_cancel_buttons) self:mergeLayoutInVertical(ok_cancel_buttons)
local vgroup = VerticalGroup:new{ self.vgroup = VerticalGroup:new{
align = "left", align = "left",
title_bar, title_bar,
} }
table.insert(vgroup, CenterContainer:new{ table.insert(self.vgroup, CenterContainer:new{
dimen = Geom:new{ dimen = Geom:new{
w = self.width, w = self.width,
h = value_group:getSize().h + 4 * Size.padding.large, h = value_group:getSize().h + 4 * Size.padding.large,
}, },
value_group value_group,
}) })
table.insert(vgroup, CenterContainer:new{ table.insert(self.vgroup, CenterContainer:new{
dimen = Geom:new{ dimen = Geom:new{
w = self.width, w = self.width,
h = ok_cancel_buttons:getSize().h, h = ok_cancel_buttons:getSize().h,
}, },
ok_cancel_buttons ok_cancel_buttons,
}) })
self.spin_frame = FrameContainer:new{ self.spin_frame = FrameContainer:new{
radius = Size.radius.window, radius = Size.radius.window,
padding = 0, padding = 0,
margin = 0, margin = 0,
background = Blitbuffer.COLOR_WHITE, background = Blitbuffer.COLOR_WHITE,
vgroup, self.vgroup,
} }
self.movable = MovableContainer:new{ self.movable = MovableContainer:new{
alpha = prev_movable_alpha, alpha = prev_movable_alpha,
@ -264,6 +264,13 @@ function SpinWidget:update(numberpicker_value, numberpicker_value_index)
}, },
self.movable, self.movable,
} }
if self._added_widgets then
for _, widget in ipairs(self._added_widgets) do
self:addWidget(widget, true)
end
end
if prev_movable_offset then if prev_movable_offset then
self.movable:setMovedOffset(prev_movable_offset) self.movable:setMovedOffset(prev_movable_offset)
end end
@ -273,6 +280,31 @@ function SpinWidget:update(numberpicker_value, numberpicker_value_index)
end) end)
end end
-- This is almost the same functionality as in InputDialog, except positioning
function SpinWidget:addWidget(widget, re_init)
table.insert(self.layout, #self.layout, {widget})
if not re_init then -- backup widget for re-init
widget = CenterContainer:new{
dimen = Geom:new{
w = self.width,
h = widget:getSize().h,
},
widget,
}
if not self._added_widgets then
self._added_widgets = {}
end
table.insert(self._added_widgets, widget)
end
-- Insert widget before the bottom buttons and their previous vspan.
-- This is different to InputDialog.
table.insert(self.vgroup, #self.vgroup, widget)
end
function SpinWidget:getAddedWidgetAvailableWidth()
return self.width - 2 * Size.padding.large
end
function SpinWidget:hasMoved() function SpinWidget:hasMoved()
local offset = self.movable:getMovedOffset() local offset = self.movable:getMovedOffset()
return offset.x ~= 0 or offset.y ~= 0 return offset.x ~= 0 or offset.y ~= 0

@ -4,6 +4,7 @@ Plugin for setting screen warmth based on the sun position and/or a time schedul
@module koplugin.autowarmth @module koplugin.autowarmth
--]]-- --]]--
local CheckButton = require("ui/widget/checkbutton")
local ConfirmBox = require("ui/widget/confirmbox") local ConfirmBox = require("ui/widget/confirmbox")
local Device = require("device") local Device = require("device")
local DateTimeWidget = require("ui/widget/datetimewidget") local DateTimeWidget = require("ui/widget/datetimewidget")
@ -489,10 +490,10 @@ end
-- Set warmth and schedule the next warmth change -- Set warmth and schedule the next warmth change
function AutoWarmth:setWarmth(val, force_warmth) function AutoWarmth:setWarmth(val, force_warmth)
-- A value > 100 means to set night mode and set warmth to maximum.
-- We use an offset of 1000 to "flag", that night mode is on.
if val then if val then
if self.control_nightmode then DeviceListener:onSetNightMode(self.control_nightmode and val > 100)
DeviceListener:onSetNightMode(val > 100)
end
if self.control_warmth and Device:hasNaturalLight() then if self.control_warmth and Device:hasNaturalLight() then
val = math.min(val, 100) -- "mask" night mode val = math.min(val, 100) -- "mask" night mode
@ -950,10 +951,18 @@ function AutoWarmth:getWarmthMenu()
mode = mode, mode = mode,
text_func = function() text_func = function()
if Device:hasNaturalLight() and self.control_warmth then if Device:hasNaturalLight() and self.control_warmth then
if self.warmth[num] <= 100 then if self.control_nightmode then
return T(_("%1: %2 %"), text, self.warmth[num]) if self.warmth[num] <= 100 then
return T(_("%1: %2 %"), text, self.warmth[num])
else
return T(_("%1: 100 % + ☾"), text)
end
else else
return T(_("%1: 100 % %2"), text, self.control_nightmode and "+ ☾" or "") if self.warmth[num] <= 100 then
return T(_("%1: %2 %"), text, self.warmth[num])
else
return T(_("%1: %2 %"), text, math.max(self.warmth[num] - 1000, 0))
end
end end
else else
if self.warmth[num] <= 100 then if self.warmth[num] <= 100 then
@ -965,10 +974,10 @@ function AutoWarmth:getWarmthMenu()
end, end,
callback = function(touchmenu_instance) callback = function(touchmenu_instance)
if Device:hasNaturalLight() and self.control_warmth then if Device:hasNaturalLight() and self.control_warmth then
UIManager:show(SpinWidget:new{ local warmth_spinner = SpinWidget:new{
title_text = text, title_text = text,
info_text = _("Enter percentage of warmth."), info_text = _("Enter percentage of warmth."),
value = self.warmth[num], value = self.warmth[num] <= 100 and self.warmth[num] or math.max(self.warmth[num] - 1000, 0), -- mask nightmode
value_min = 0, value_min = 0,
value_max = 100, value_max = 100,
wrap = false, wrap = false,
@ -976,40 +985,56 @@ function AutoWarmth:getWarmthMenu()
value_hold_step = 10, value_hold_step = 10,
unit = "%", unit = "%",
ok_text = _("Set"), ok_text = _("Set"),
ok_always_enabled = true,
callback = function(spin) callback = function(spin)
self.warmth[num] = spin.value self.warmth[num] = spin.value
self.warmth[#self.warmth - num + 1] = spin.value if self.control_nightmode and self.night_mode_check_box.checked then
G_reader_settings:saveSetting("autowarmth_warmth", self.warmth) if self.warmth[num] <= 100 then
self:scheduleMidnightUpdate() self.warmth[num] = self.warmth[num] + 1000 -- add night mode
if touchmenu_instance then self:updateItems(touchmenu_instance) end end
end, else
extra_text = self.control_nightmode and _("Use night mode"), if self.warmth[num] > 100 then
extra_callback = self.control_nightmode and function() self.warmth[num] = math.max(self.warmth[num] - 1000, 0) -- delete night mode
self.warmth[num] = 110 end
self.warmth[#self.warmth - num + 1] = 110 end
self.warmth[#self.warmth - num + 1] = self.warmth[num]
G_reader_settings:saveSetting("autowarmth_warmth", self.warmth) G_reader_settings:saveSetting("autowarmth_warmth", self.warmth)
self:scheduleMidnightUpdate() self:scheduleMidnightUpdate()
if touchmenu_instance then self:updateItems(touchmenu_instance) end if touchmenu_instance then self:updateItems(touchmenu_instance) end
end, end,
}) }
if self.control_nightmode then
self.night_mode_check_box = CheckButton:new{
text = _("Night mode"),
checked = self.warmth[num] > 100,
parent = warmth_spinner,
}
warmth_spinner:addWidget(self.night_mode_check_box)
end
UIManager:show(warmth_spinner)
else else
UIManager:show(ConfirmBox:new{ UIManager:show(ConfirmBox:new{
text = _("Night mode"), text = _("Night mode"),
ok_text = _("Turn on"), ok_text = _("Turn on"),
ok_callback = function() ok_callback = function()
self.warmth[num] = 110 if self.warmth[num] <= 100 then
self.warmth[#self.warmth - num + 1] = 110 self.warmth[num] = self.warmth[num] + 1000
G_reader_settings:saveSetting("autowarmth_warmth", self.warmth) self.warmth[#self.warmth - num + 1] = self.warmth[num]
self:scheduleMidnightUpdate() G_reader_settings:saveSetting("autowarmth_warmth", self.warmth)
if touchmenu_instance then self:updateItems(touchmenu_instance) end self:scheduleMidnightUpdate()
if touchmenu_instance then self:updateItems(touchmenu_instance) end
end
end, end,
cancel_text = _("Turn off"), cancel_text = _("Turn off"),
cancel_callback = function() cancel_callback = function()
self.warmth[num] = 0 if self.warmth[num] > 100 then
self.warmth[#self.warmth - num + 1] = 0 self.warmth[num] = math.max(self.warmth[num] - 1000, 0) -- delete night mode
G_reader_settings:saveSetting("autowarmth_warmth", self.warmth) self.warmth[#self.warmth - num + 1] = self.warmth[num]
self:scheduleMidnightUpdate() G_reader_settings:saveSetting("autowarmth_warmth", self.warmth)
if touchmenu_instance then self:updateItems(touchmenu_instance) end self:scheduleMidnightUpdate()
if touchmenu_instance then self:updateItems(touchmenu_instance) end
end
end, end,
other_buttons = {{ other_buttons = {{
{ {

Loading…
Cancel
Save