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

reviewable/pr10778/r1
zwim 9 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
option_text = nil,
option_callback = nil,
unit = nil,
unit = nil, -- unit to show or nil
}
function SpinWidget:init()
@ -226,30 +226,30 @@ function SpinWidget:update(numberpicker_value, numberpicker_value_index)
show_parent = self,
}
self:mergeLayoutInVertical(ok_cancel_buttons)
local vgroup = VerticalGroup:new{
self.vgroup = VerticalGroup:new{
align = "left",
title_bar,
}
table.insert(vgroup, CenterContainer:new{
table.insert(self.vgroup, CenterContainer:new{
dimen = Geom:new{
w = self.width,
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{
w = self.width,
h = ok_cancel_buttons:getSize().h,
},
ok_cancel_buttons
ok_cancel_buttons,
})
self.spin_frame = FrameContainer:new{
radius = Size.radius.window,
padding = 0,
margin = 0,
background = Blitbuffer.COLOR_WHITE,
vgroup,
self.vgroup,
}
self.movable = MovableContainer:new{
alpha = prev_movable_alpha,
@ -264,6 +264,13 @@ function SpinWidget:update(numberpicker_value, numberpicker_value_index)
},
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
self.movable:setMovedOffset(prev_movable_offset)
end
@ -273,6 +280,31 @@ function SpinWidget:update(numberpicker_value, numberpicker_value_index)
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()
local offset = self.movable:getMovedOffset()
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
--]]--
local CheckButton = require("ui/widget/checkbutton")
local ConfirmBox = require("ui/widget/confirmbox")
local Device = require("device")
local DateTimeWidget = require("ui/widget/datetimewidget")
@ -489,10 +490,10 @@ end
-- Set warmth and schedule the next warmth change
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 self.control_nightmode then
DeviceListener:onSetNightMode(val > 100)
end
DeviceListener:onSetNightMode(self.control_nightmode and val > 100)
if self.control_warmth and Device:hasNaturalLight() then
val = math.min(val, 100) -- "mask" night mode
@ -950,10 +951,18 @@ function AutoWarmth:getWarmthMenu()
mode = mode,
text_func = function()
if Device:hasNaturalLight() and self.control_warmth then
if self.warmth[num] <= 100 then
return T(_("%1: %2 %"), text, self.warmth[num])
if self.control_nightmode then
if self.warmth[num] <= 100 then
return T(_("%1: %2 %"), text, self.warmth[num])
else
return T(_("%1: 100 % + ☾"), text)
end
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
else
if self.warmth[num] <= 100 then
@ -965,10 +974,10 @@ function AutoWarmth:getWarmthMenu()
end,
callback = function(touchmenu_instance)
if Device:hasNaturalLight() and self.control_warmth then
UIManager:show(SpinWidget:new{
local warmth_spinner = SpinWidget:new{
title_text = text,
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_max = 100,
wrap = false,
@ -976,40 +985,56 @@ function AutoWarmth:getWarmthMenu()
value_hold_step = 10,
unit = "%",
ok_text = _("Set"),
ok_always_enabled = true,
callback = function(spin)
self.warmth[num] = spin.value
self.warmth[#self.warmth - num + 1] = spin.value
G_reader_settings:saveSetting("autowarmth_warmth", self.warmth)
self:scheduleMidnightUpdate()
if touchmenu_instance then self:updateItems(touchmenu_instance) end
end,
extra_text = self.control_nightmode and _("Use night mode"),
extra_callback = self.control_nightmode and function()
self.warmth[num] = 110
self.warmth[#self.warmth - num + 1] = 110
if self.control_nightmode and self.night_mode_check_box.checked then
if self.warmth[num] <= 100 then
self.warmth[num] = self.warmth[num] + 1000 -- add night mode
end
else
if self.warmth[num] > 100 then
self.warmth[num] = math.max(self.warmth[num] - 1000, 0) -- delete night mode
end
end
self.warmth[#self.warmth - num + 1] = self.warmth[num]
G_reader_settings:saveSetting("autowarmth_warmth", self.warmth)
self:scheduleMidnightUpdate()
if touchmenu_instance then self:updateItems(touchmenu_instance) 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
UIManager:show(ConfirmBox:new{
text = _("Night mode"),
ok_text = _("Turn on"),
ok_callback = function()
self.warmth[num] = 110
self.warmth[#self.warmth - num + 1] = 110
G_reader_settings:saveSetting("autowarmth_warmth", self.warmth)
self:scheduleMidnightUpdate()
if touchmenu_instance then self:updateItems(touchmenu_instance) end
if self.warmth[num] <= 100 then
self.warmth[num] = self.warmth[num] + 1000
self.warmth[#self.warmth - num + 1] = self.warmth[num]
G_reader_settings:saveSetting("autowarmth_warmth", self.warmth)
self:scheduleMidnightUpdate()
if touchmenu_instance then self:updateItems(touchmenu_instance) end
end
end,
cancel_text = _("Turn off"),
cancel_callback = function()
self.warmth[num] = 0
self.warmth[#self.warmth - num + 1] = 0
G_reader_settings:saveSetting("autowarmth_warmth", self.warmth)
self:scheduleMidnightUpdate()
if touchmenu_instance then self:updateItems(touchmenu_instance) end
if self.warmth[num] > 100 then
self.warmth[num] = math.max(self.warmth[num] - 1000, 0) -- delete night mode
self.warmth[#self.warmth - num + 1] = self.warmth[num]
G_reader_settings:saveSetting("autowarmth_warmth", self.warmth)
self:scheduleMidnightUpdate()
if touchmenu_instance then self:updateItems(touchmenu_instance) end
end
end,
other_buttons = {{
{

Loading…
Cancel
Save