mirror of
https://github.com/koreader/koreader.git
synced 2025-08-10 00:52:38 +00:00
Initial Kindle PW5 support (#8856)
* Rejig frontlight warmth API to more closely match the existing API, and, hopefully, clarify some of its quirks, and reduce boilerplate and duplicate code in platform implementations. * Tweak Kindle:setDateTime to prefer using the platform's custom script, as in interacts better with the stock UI. And make the fallbacks handle old busybox versions better. * Add Kindle PW5 support ;). * Add warmth support to the Kindle platform. * Random TextBoxWidget cleanups: make sure we immediately free destroyed instances. * FrontLightWidget: Refactor to make it slightly less obnoxious to grok and update; i.e., separate layout from update, and properly separate brightness from warmth handling. Move to simpler widgets instead of reinventing the wheel. * TextBoxWidgets: Implement `setText` to match TextWidget's API, as some callers may be using the two interchangeably (i.e., Button). * NaturalLightWidget: Make sure we pass a string to InputText * InputText: Add debug guards to catch bad callers not passing strings ;).
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
local Blitbuffer = require("ffi/blitbuffer")
|
||||
local Button = require("ui/widget/button")
|
||||
local ButtonProgressWidget = require("ui/widget/buttonprogresswidget")
|
||||
local CenterContainer = require("ui/widget/container/centercontainer")
|
||||
local Device = require("device")
|
||||
local FocusManager = require("ui/widget/focusmanager")
|
||||
@@ -13,7 +14,7 @@ local Math = require("optmath")
|
||||
local NaturalLight = require("ui/widget/naturallightwidget")
|
||||
local ProgressWidget = require("ui/widget/progresswidget")
|
||||
local Size = require("ui/size")
|
||||
local TextBoxWidget = require("ui/widget/textboxwidget")
|
||||
local TextWidget = require("ui/widget/textwidget")
|
||||
local TimeVal = require("ui/timeval")
|
||||
local TitleBar = require("ui/widget/titlebar")
|
||||
local UIManager = require("ui/uimanager")
|
||||
@@ -33,50 +34,49 @@ local FrontLightWidget = FocusManager:new{
|
||||
}
|
||||
|
||||
function FrontLightWidget:init()
|
||||
-- Layout constants
|
||||
self.medium_font_face = Font:getFace("ffont")
|
||||
self.screen_width = Screen:getWidth()
|
||||
self.screen_height = Screen:getHeight()
|
||||
self.span = math.ceil(self.screen_height * 0.01)
|
||||
self.span = Math.round(self.screen_height * 0.01)
|
||||
self.width = math.floor(self.screen_width * 0.95)
|
||||
|
||||
-- State constants
|
||||
self.powerd = Device:getPowerDevice()
|
||||
self.fl_min = self.powerd.fl_min
|
||||
self.fl_max = self.powerd.fl_max
|
||||
self.fl_cur = self.powerd:frontlightIntensity()
|
||||
local steps_fl = self.fl_max - self.fl_min + 1
|
||||
self.one_step = math.ceil(steps_fl / 25)
|
||||
self.steps = math.ceil(steps_fl / self.one_step)
|
||||
if (self.steps - 1) * self.one_step < self.fl_max - self.fl_min then
|
||||
self.steps = self.steps + 1
|
||||
|
||||
-- Frontlight
|
||||
self.fl = {}
|
||||
self.fl.min = self.powerd.fl_min
|
||||
self.fl.max = self.powerd.fl_max
|
||||
self.fl.cur = self.powerd:frontlightIntensity()
|
||||
local fl_steps = self.fl.max - self.fl.min + 1
|
||||
self.fl.stride = math.ceil(fl_steps / 25)
|
||||
self.fl.steps = math.ceil(fl_steps / self.fl.stride)
|
||||
if (self.fl.steps - 1) * self.fl.stride < self.fl.max - self.fl.min then
|
||||
self.fl.steps = self.fl.steps + 1
|
||||
end
|
||||
self.steps = math.min(self.steps, steps_fl)
|
||||
self.natural_light = Device:hasNaturalLight()
|
||||
self.fl.steps = math.min(self.fl.steps, fl_steps)
|
||||
|
||||
-- Warmth
|
||||
self.has_nl = Device:hasNaturalLight()
|
||||
self.has_nl_mixer = Device:hasNaturalLightMixer()
|
||||
self.has_nl_api = Device:hasNaturalLightApi()
|
||||
-- Handle Warmth separately, because it may use a different scale
|
||||
if self.natural_light then
|
||||
self.nl_min = self.powerd.fl_warmth_min
|
||||
self.nl_max = self.powerd.fl_warmth_max
|
||||
-- NOTE: fl_warmth is always [0...100] even when internal scale is [0...10]
|
||||
self.nl_scale = (100 / self.nl_max)
|
||||
if self.has_nl then
|
||||
self.nl = {}
|
||||
self.nl.min = self.powerd.fl_warmth_min
|
||||
self.nl.max = self.powerd.fl_warmth_max
|
||||
self.nl.cur = self.powerd:toNativeWarmth(self.powerd:frontlightWarmth())
|
||||
|
||||
local nl_steps = self.nl.max - self.nl.min + 1
|
||||
self.nl.stride = math.ceil(nl_steps / 25)
|
||||
self.nl.steps = math.ceil(nl_steps / self.nl.stride)
|
||||
if (self.nl.steps - 1) * self.nl.stride < self.nl.max - self.nl.min then
|
||||
self.nl.steps = self.nl.steps + 1
|
||||
end
|
||||
self.nl.steps = math.min(self.nl.steps, nl_steps)
|
||||
end
|
||||
|
||||
-- button width to fit screen size
|
||||
local button_margin = Size.margin.tiny
|
||||
local button_padding = Size.padding.button
|
||||
local button_bordersize = Size.border.button
|
||||
self.button_width = math.floor(self.screen_width * 0.9 / self.steps) -
|
||||
2 * (button_margin + button_padding + button_bordersize)
|
||||
|
||||
self.fl_prog_button = Button:new{
|
||||
text = "",
|
||||
radius = 0,
|
||||
margin = button_margin,
|
||||
padding = button_padding,
|
||||
bordersize = button_bordersize,
|
||||
enabled = true,
|
||||
width = self.button_width,
|
||||
show_parent = self,
|
||||
}
|
||||
-- Input
|
||||
if Device:hasKeys() then
|
||||
self.key_events.Close = { {Device.input.group.Back}, doc = "close frontlight" }
|
||||
end
|
||||
@@ -104,102 +104,104 @@ function FrontLightWidget:init()
|
||||
},
|
||||
}
|
||||
end
|
||||
self:update()
|
||||
|
||||
-- Widget layout
|
||||
self:layout()
|
||||
end
|
||||
|
||||
function FrontLightWidget:generateProgressGroup(width, height, fl_level, step)
|
||||
self.fl_container = CenterContainer:new{
|
||||
dimen = Geom:new{ w = width, h = height },
|
||||
function FrontLightWidget:layout()
|
||||
self.layout = {}
|
||||
|
||||
local main_container = CenterContainer:new{
|
||||
dimen = Geom:new{
|
||||
w = self.width,
|
||||
h = math.floor(self.screen_height * 0.2),
|
||||
},
|
||||
}
|
||||
self:setProgress(fl_level, step)
|
||||
return self.fl_container
|
||||
end
|
||||
|
||||
function FrontLightWidget:setProgress(num, step, num_warmth)
|
||||
self.fl_container:clear()
|
||||
local padding_span = VerticalSpan:new{ width = self.span }
|
||||
local button_group_down = HorizontalGroup:new{ align = "center" }
|
||||
local button_group_up = HorizontalGroup:new{ align = "center" }
|
||||
local vertical_group = VerticalGroup:new{ align = "center" }
|
||||
local enable_button_plus = true
|
||||
local enable_button_minus = true
|
||||
if self.natural_light then
|
||||
num_warmth = num_warmth or self.powerd.fl_warmth
|
||||
end
|
||||
if num then
|
||||
--- @note Don't set the same value twice, to play nice with the update() sent by the swipe handler on the FL bar
|
||||
-- Except for fl_min, as that's how setFrontLightIntensity detects a toggle...
|
||||
if num == self.fl_min or num ~= self.fl_cur then
|
||||
self:setFrontLightIntensity(num)
|
||||
end
|
||||
|
||||
if self.fl_cur == self.fl_max then enable_button_plus = false end
|
||||
if self.fl_cur == self.fl_min then enable_button_minus = false end
|
||||
end
|
||||
-- Frontlight
|
||||
-- Bigger spans, as ProgressWidget appears to be ever so slightly smaller than ButtonProgressWidget ;).
|
||||
local fl_padding_span = VerticalSpan:new{ width = Math.round(self.span * 1.5) }
|
||||
local fl_group_above = HorizontalGroup:new{ align = "center" }
|
||||
local fl_group_below = HorizontalGroup:new{ align = "center" }
|
||||
local main_group = VerticalGroup:new{ align = "center" }
|
||||
|
||||
local ticks = {}
|
||||
for i = 1, self.steps-2 do
|
||||
table.insert(ticks, i*self.one_step)
|
||||
for i = 1, self.fl.steps - 2 do
|
||||
table.insert(ticks, i * self.fl.stride)
|
||||
end
|
||||
|
||||
self.fl_group = ProgressWidget:new{
|
||||
self.fl_progress = ProgressWidget:new{
|
||||
width = math.floor(self.screen_width * 0.9),
|
||||
height = Size.item.height_big,
|
||||
percentage = self.fl_cur / self.fl_max,
|
||||
percentage = self.fl.cur / self.fl.max,
|
||||
ticks = ticks,
|
||||
tick_width = Screen:scaleBySize(0.5),
|
||||
last = self.fl_max,
|
||||
last = self.fl.max,
|
||||
}
|
||||
local text_br = TextBoxWidget:new{
|
||||
local fl_header = TextWidget:new{
|
||||
text = _("Brightness"),
|
||||
face = self.medium_font_face,
|
||||
bold = true,
|
||||
alignment = "center",
|
||||
width = math.floor(self.screen_width * 0.95),
|
||||
max_width = math.floor(self.screen_width * 0.95),
|
||||
}
|
||||
local button_minus = Button:new{
|
||||
text = "-1",
|
||||
self.fl_minus = Button:new{
|
||||
text = "−",
|
||||
margin = Size.margin.small,
|
||||
radius = 0,
|
||||
enabled = enable_button_minus,
|
||||
enabled = self.fl.cur ~= self.fl.min,
|
||||
width = math.floor(self.screen_width * 0.2),
|
||||
show_parent = self,
|
||||
callback = function() self:setProgress(self.fl_cur - 1, step) end,
|
||||
callback = function()
|
||||
self:setBrightness(self.fl.cur - 1)
|
||||
end,
|
||||
}
|
||||
local button_plus = Button:new{
|
||||
text = "+1",
|
||||
self.fl_plus = Button:new{
|
||||
text = "+",
|
||||
margin = Size.margin.small,
|
||||
radius = 0,
|
||||
enabled = enable_button_plus,
|
||||
enabled = self.fl.cur ~= self.fl.max,
|
||||
width = math.floor(self.screen_width * 0.2),
|
||||
show_parent = self,
|
||||
callback = function() self:setProgress(self.fl_cur + 1, step) end,
|
||||
callback = function()
|
||||
self:setBrightness(self.fl.cur + 1)
|
||||
end,
|
||||
}
|
||||
local item_level = TextBoxWidget:new{
|
||||
text = self.fl_cur,
|
||||
self.fl_level = TextWidget:new{
|
||||
text = tostring(self.fl.cur),
|
||||
face = self.medium_font_face,
|
||||
alignment = "center",
|
||||
width = math.floor(self.screen_width * 0.95 - 1.275 * button_minus.width - 1.275 * button_plus.width),
|
||||
max_width = math.floor(self.screen_width * 0.95 - 1.275 * self.fl_minus.width - 1.275 * self.fl_plus.width),
|
||||
}
|
||||
local button_min = Button:new{
|
||||
local fl_level_container = CenterContainer:new{
|
||||
dimen = Geom:new{
|
||||
w = self.fl_level.max_width,
|
||||
h = self.fl_level:getSize().h
|
||||
},
|
||||
self.fl_level,
|
||||
}
|
||||
local fl_min = Button:new{
|
||||
text = _("Min"),
|
||||
margin = Size.margin.small,
|
||||
radius = 0,
|
||||
enabled = true,
|
||||
width = math.floor(self.screen_width * 0.2),
|
||||
show_parent = self,
|
||||
callback = function() self:setProgress(self.fl_min+1, step) end, -- min is 1 (use toggle for 0)
|
||||
callback = function()
|
||||
self:setBrightness(self.fl.min + 1)
|
||||
end, -- min is 1 (We use 0 to mean "toggle")
|
||||
}
|
||||
local button_max = Button:new{
|
||||
local fl_max = Button:new{
|
||||
text = _("Max"),
|
||||
margin = Size.margin.small,
|
||||
radius = 0,
|
||||
enabled = true,
|
||||
width = math.floor(self.screen_width * 0.2),
|
||||
show_parent = self,
|
||||
callback = function() self:setProgress(self.fl_max, step) end,
|
||||
callback = function()
|
||||
self:setBrightness(self.fl.max)
|
||||
end,
|
||||
}
|
||||
local button_toggle = Button:new{
|
||||
local fl_toggle = Button:new{
|
||||
text = _("Toggle"),
|
||||
margin = Size.margin.small,
|
||||
radius = 0,
|
||||
@@ -207,48 +209,161 @@ function FrontLightWidget:setProgress(num, step, num_warmth)
|
||||
width = math.floor(self.screen_width * 0.2),
|
||||
show_parent = self,
|
||||
callback = function()
|
||||
self:setProgress(self.fl_min, step)
|
||||
self:setBrightness(self.fl.min)
|
||||
end,
|
||||
}
|
||||
local empty_space = HorizontalSpan:new{
|
||||
width = math.floor((self.screen_width * 0.95 - 1.2 * button_minus.width - 1.2 * button_plus.width - 1.2 * button_toggle.width) / 2),
|
||||
local fl_spacer = HorizontalSpan:new{
|
||||
width = math.floor((self.screen_width * 0.95 - 1.2 * self.fl_minus.width - 1.2 * self.fl_plus.width - 1.2 * fl_toggle.width) / 2),
|
||||
}
|
||||
local button_table_up = HorizontalGroup:new{
|
||||
local fl_buttons_above = HorizontalGroup:new{
|
||||
align = "center",
|
||||
button_minus,
|
||||
item_level,
|
||||
button_plus,
|
||||
self.fl_minus,
|
||||
fl_level_container,
|
||||
self.fl_plus,
|
||||
}
|
||||
self.layout[1] = {button_minus, button_plus}
|
||||
local button_table_down = HorizontalGroup:new{
|
||||
self.layout[1] = {self.fl_minus, self.fl_plus}
|
||||
local fl_buttons_below = HorizontalGroup:new{
|
||||
align = "center",
|
||||
button_min,
|
||||
empty_space,
|
||||
button_toggle,
|
||||
empty_space,
|
||||
button_max,
|
||||
fl_min,
|
||||
fl_spacer,
|
||||
fl_toggle,
|
||||
fl_spacer,
|
||||
fl_max,
|
||||
}
|
||||
self.layout[2] = {button_min, button_toggle, button_max}
|
||||
if self.natural_light then
|
||||
-- Only insert 'brightness' caption if we also add 'warmth'
|
||||
-- widgets below.
|
||||
table.insert(vertical_group, text_br)
|
||||
self.layout[2] = {fl_min, fl_toggle, fl_max}
|
||||
|
||||
if self.has_nl then
|
||||
-- Only insert a "Brightness" caption if we also add the full set of warmth widgets below,
|
||||
-- otherwise, it's implied by the title bar ;).
|
||||
table.insert(main_group, fl_header)
|
||||
end
|
||||
table.insert(button_group_up, button_table_up)
|
||||
table.insert(button_group_down, button_table_down)
|
||||
table.insert(vertical_group, padding_span)
|
||||
table.insert(vertical_group, button_group_up)
|
||||
table.insert(vertical_group, padding_span)
|
||||
table.insert(vertical_group, self.fl_group)
|
||||
table.insert(vertical_group, padding_span)
|
||||
table.insert(vertical_group, button_group_down)
|
||||
table.insert(vertical_group, padding_span)
|
||||
if self.natural_light then
|
||||
-- If the device supports natural light, add the widgets for 'warmth',
|
||||
-- as well as a 'Configure' button for devices *without* a mixer
|
||||
self:addWarmthWidgets(num_warmth, step, vertical_group)
|
||||
table.insert(fl_group_above, fl_buttons_above)
|
||||
table.insert(fl_group_below, fl_buttons_below)
|
||||
table.insert(main_group, fl_padding_span)
|
||||
table.insert(main_group, fl_group_above)
|
||||
table.insert(main_group, fl_padding_span)
|
||||
table.insert(main_group, self.fl_progress)
|
||||
table.insert(main_group, fl_padding_span)
|
||||
table.insert(main_group, fl_group_below)
|
||||
table.insert(main_group, fl_padding_span)
|
||||
|
||||
-- Warmth
|
||||
if self.has_nl then
|
||||
-- Smaller spans, as ButtonProgressWidget appears to be ever so slightly taller than ProgressWidget ;).
|
||||
local nl_padding_span = VerticalSpan:new{ width = self.span }
|
||||
local nl_group_above = HorizontalGroup:new{ align = "center" }
|
||||
local nl_group_below = HorizontalGroup:new{ align = "center" }
|
||||
|
||||
self.nl_progress = ButtonProgressWidget:new{
|
||||
width = math.floor(self.screen_width * 0.9),
|
||||
font_size = 20, -- match Button's default
|
||||
padding = 0,
|
||||
thin_grey_style = false,
|
||||
num_buttons = self.nl.steps - 1, -- no button for step 0
|
||||
position = math.floor(self.nl.cur / self.nl.stride),
|
||||
default_position = math.floor(self.nl.cur / self.nl.stride),
|
||||
callback = function(i)
|
||||
self:setWarmth(i, false)
|
||||
end,
|
||||
show_parent = self,
|
||||
enabled = true,
|
||||
}
|
||||
-- We want a wider gap between the two sets of widgets
|
||||
local nl_span = VerticalSpan:new{ width = Size.span.vertical_large * 4 }
|
||||
local nl_header = TextWidget:new{
|
||||
text = _("Warmth"),
|
||||
face = self.medium_font_face,
|
||||
bold = true,
|
||||
max_width = math.floor(self.screen_width * 0.95),
|
||||
}
|
||||
self.nl_minus = Button:new{
|
||||
text = "−",
|
||||
margin = Size.margin.small,
|
||||
radius = 0,
|
||||
enabled = self.nl.cur ~= self.nl.min,
|
||||
width = math.floor(self.screen_width * 0.2),
|
||||
show_parent = self,
|
||||
callback = function()
|
||||
self:setWarmth(self.nl.cur - 1, true) end,
|
||||
}
|
||||
self.nl_plus = Button:new{
|
||||
text = "+",
|
||||
margin = Size.margin.small,
|
||||
radius = 0,
|
||||
enabled = self.nl.cur ~= self.nl.max,
|
||||
width = math.floor(self.screen_width * 0.2),
|
||||
show_parent = self,
|
||||
callback = function()
|
||||
self:setWarmth(self.nl.cur + 1, true) end,
|
||||
}
|
||||
self.nl_level = TextWidget:new{
|
||||
text = tostring(self.nl.cur),
|
||||
face = self.medium_font_face,
|
||||
max_width = math.floor(self.screen_width * 0.95 - 1.275 * self.nl_minus.width - 1.275 * self.nl_plus.width),
|
||||
}
|
||||
local nl_level_container = CenterContainer:new{
|
||||
dimen = Geom:new{
|
||||
w = self.nl_level.max_width,
|
||||
h = self.nl_level:getSize().h
|
||||
},
|
||||
self.nl_level,
|
||||
}
|
||||
local nl_min = Button:new{
|
||||
text = _("Min"),
|
||||
margin = Size.margin.small,
|
||||
radius = 0,
|
||||
enabled = true,
|
||||
width = math.floor(self.screen_width * 0.2),
|
||||
show_parent = self,
|
||||
callback = function()
|
||||
self:setWarmth(self.nl.min, true)
|
||||
end,
|
||||
}
|
||||
local nl_max = Button:new{
|
||||
text = _("Max"),
|
||||
margin = Size.margin.small,
|
||||
radius = 0,
|
||||
enabled = true,
|
||||
width = math.floor(self.screen_width * 0.2),
|
||||
show_parent = self,
|
||||
callback = function()
|
||||
self:setWarmth(self.nl.max, true)
|
||||
end,
|
||||
}
|
||||
local nl_spacer = HorizontalSpan:new{
|
||||
width = math.floor((self.screen_width * 0.95 - 1.2 * self.nl_minus.width - 1.2 * self.nl_plus.width) / 2),
|
||||
}
|
||||
local nl_buttons_above = HorizontalGroup:new{
|
||||
align = "center",
|
||||
self.nl_minus,
|
||||
nl_level_container,
|
||||
self.nl_plus,
|
||||
}
|
||||
self.layout[3] = {self.nl_minus, self.nl_plus}
|
||||
local nl_buttons_below = HorizontalGroup:new{
|
||||
align = "center",
|
||||
nl_min,
|
||||
nl_spacer,
|
||||
nl_max,
|
||||
}
|
||||
self.layout[4] = {nl_min, nl_max}
|
||||
|
||||
table.insert(main_group, nl_span)
|
||||
table.insert(main_group, nl_header)
|
||||
table.insert(nl_group_above, nl_buttons_above)
|
||||
table.insert(nl_group_below, nl_buttons_below)
|
||||
|
||||
table.insert(main_group, nl_padding_span)
|
||||
table.insert(main_group, nl_group_above)
|
||||
table.insert(main_group, nl_padding_span)
|
||||
table.insert(main_group, self.nl_progress)
|
||||
table.insert(main_group, nl_padding_span)
|
||||
table.insert(main_group, nl_group_below)
|
||||
table.insert(main_group, nl_padding_span)
|
||||
|
||||
-- Aura One R/G/B widget
|
||||
if not self.has_nl_mixer and not self.has_nl_api then
|
||||
self.configure_button = Button:new{
|
||||
local nl_setup = Button:new{
|
||||
text = _("Configure"),
|
||||
margin = Size.margin.small,
|
||||
radius = 0,
|
||||
@@ -258,177 +373,41 @@ function FrontLightWidget:setProgress(num, step, num_warmth)
|
||||
UIManager:show(NaturalLight:new{fl_widget = self})
|
||||
end,
|
||||
}
|
||||
table.insert(vertical_group, self.configure_button)
|
||||
self.layout[5] = {self.configure_button}
|
||||
table.insert(main_group, nl_setup)
|
||||
self.layout[5] = {nl_setup}
|
||||
end
|
||||
end
|
||||
table.insert(self.fl_container, vertical_group)
|
||||
|
||||
table.insert(main_container, main_group)
|
||||
-- Reset container height to what it actually contains
|
||||
self.fl_container.dimen.h = vertical_group:getSize().h
|
||||
self:refocusWidget()
|
||||
UIManager:setDirty(self, function()
|
||||
return "ui", self.light_frame.dimen
|
||||
end)
|
||||
return true
|
||||
end
|
||||
main_container.dimen.h = main_group:getSize().h
|
||||
|
||||
-- Currently, we are assuming the 'warmth' has the same min/max limits as 'brightness'.
|
||||
function FrontLightWidget:addWarmthWidgets(num_warmth, step, vertical_group)
|
||||
local button_group_down = HorizontalGroup:new{ align = "center" }
|
||||
local button_group_up = HorizontalGroup:new{ align = "center" }
|
||||
local warmth_group = HorizontalGroup:new{ align = "center" }
|
||||
local padding_span = VerticalSpan:new{ width = self.span }
|
||||
local enable_button_plus = true
|
||||
local enable_button_minus = true
|
||||
|
||||
if self[1] then
|
||||
--- @note Don't set the same value twice, to play nice with the update() sent by the swipe handler on the FL bar
|
||||
if num_warmth ~= self.powerd.fl_warmth then
|
||||
self.powerd:setWarmth(num_warmth)
|
||||
end
|
||||
end
|
||||
|
||||
if self.natural_light and num_warmth then
|
||||
local curr_warmth_step = math.floor(num_warmth / step)
|
||||
for i = 0, curr_warmth_step do
|
||||
table.insert(warmth_group, self.fl_prog_button:new{
|
||||
text = "",
|
||||
preselect = curr_warmth_step > 0 and true or false,
|
||||
callback = function()
|
||||
self:setProgress(self.fl_cur, step, i * step)
|
||||
end
|
||||
})
|
||||
end
|
||||
|
||||
for i = curr_warmth_step + 1, self.steps - 1 do
|
||||
table.insert(warmth_group, self.fl_prog_button:new{
|
||||
text = "",
|
||||
callback = function()
|
||||
self:setProgress(self.fl_cur, step, i * step)
|
||||
end
|
||||
})
|
||||
end
|
||||
end
|
||||
|
||||
if math.floor(num_warmth / self.nl_scale) <= self.nl_min then enable_button_minus = false end
|
||||
if math.ceil(num_warmth / self.nl_scale) >= self.nl_max then enable_button_plus = false end
|
||||
|
||||
local text_warmth = TextBoxWidget:new{
|
||||
text = "\n" .. _("Warmth"),
|
||||
face = self.medium_font_face,
|
||||
bold = true,
|
||||
alignment = "center",
|
||||
width = math.floor(self.screen_width * 0.95),
|
||||
}
|
||||
local button_minus = Button:new{
|
||||
text = "-" .. (1 * self.nl_scale),
|
||||
margin = Size.margin.small,
|
||||
radius = 0,
|
||||
enabled = enable_button_minus,
|
||||
width = math.floor(self.screen_width * 0.2),
|
||||
show_parent = self,
|
||||
callback = function() self:setProgress(self.fl_cur, step, (num_warmth - (1 * self.nl_scale))) end,
|
||||
}
|
||||
local button_plus = Button:new{
|
||||
text = "+" .. (1 * self.nl_scale),
|
||||
margin = Size.margin.small,
|
||||
radius = 0,
|
||||
enabled = enable_button_plus,
|
||||
width = math.floor(self.screen_width * 0.2),
|
||||
show_parent = self,
|
||||
callback = function() self:setProgress(self.fl_cur, step, (num_warmth + (1 * self.nl_scale))) end,
|
||||
}
|
||||
local item_level = TextBoxWidget:new{
|
||||
text = num_warmth,
|
||||
face = self.medium_font_face,
|
||||
alignment = "center",
|
||||
width = math.floor(self.screen_width * 0.95 - 1.275 * button_minus.width - 1.275 * button_plus.width),
|
||||
}
|
||||
local button_min = Button:new{
|
||||
text = _("Min"),
|
||||
margin = Size.margin.small,
|
||||
radius = 0,
|
||||
enabled = true,
|
||||
width = math.floor(self.screen_width * 0.2),
|
||||
show_parent = self,
|
||||
callback = function() self:setProgress(self.fl_cur, step, self.nl_min) end,
|
||||
}
|
||||
local button_max = Button:new{
|
||||
text = _("Max"),
|
||||
margin = Size.margin.small,
|
||||
radius = 0,
|
||||
enabled = true,
|
||||
width = math.floor(self.screen_width * 0.2),
|
||||
show_parent = self,
|
||||
callback = function() self:setProgress(self.fl_cur, step, (self.nl_max * self.nl_scale)) end,
|
||||
}
|
||||
local empty_space = HorizontalSpan:new{
|
||||
width = math.floor((self.screen_width * 0.95 - 1.2 * button_minus.width - 1.2 * button_plus.width) / 2),
|
||||
}
|
||||
local button_table_up = HorizontalGroup:new{
|
||||
align = "center",
|
||||
button_minus,
|
||||
item_level,
|
||||
button_plus,
|
||||
}
|
||||
self.layout[3] = {button_minus, button_plus}
|
||||
local button_table_down = HorizontalGroup:new{
|
||||
align = "center",
|
||||
button_min,
|
||||
empty_space,
|
||||
button_max,
|
||||
}
|
||||
self.layout[4] = {button_min, button_max}
|
||||
|
||||
table.insert(vertical_group, text_warmth)
|
||||
table.insert(button_group_up, button_table_up)
|
||||
table.insert(button_group_down, button_table_down)
|
||||
|
||||
table.insert(vertical_group, padding_span)
|
||||
table.insert(vertical_group, button_group_up)
|
||||
table.insert(vertical_group, padding_span)
|
||||
table.insert(vertical_group, warmth_group)
|
||||
table.insert(vertical_group, padding_span)
|
||||
table.insert(vertical_group, button_group_down)
|
||||
table.insert(vertical_group, padding_span)
|
||||
end
|
||||
|
||||
function FrontLightWidget:setFrontLightIntensity(num)
|
||||
self.fl_cur = num
|
||||
local set_fl = math.min(self.fl_cur, self.fl_max)
|
||||
-- Don't touch frontlight on first call (no self[1] means not yet out of update()),
|
||||
-- so that we don't untoggle light.
|
||||
if self[1] then
|
||||
if set_fl == self.fl_min then -- fl_min (which is always 0) means toggle
|
||||
self.powerd:toggleFrontlight()
|
||||
else
|
||||
self.powerd:setIntensity(set_fl)
|
||||
end
|
||||
|
||||
-- get back the real level (different from set_fl if untoggle)
|
||||
self.fl_cur = self.powerd:frontlightIntensity()
|
||||
end
|
||||
end
|
||||
|
||||
function FrontLightWidget:update()
|
||||
self.layout = {}
|
||||
-- Common
|
||||
local title_bar = TitleBar:new{
|
||||
title = _("Frontlight"),
|
||||
width = self.width,
|
||||
align = "left",
|
||||
with_bottom_line = true,
|
||||
bottom_v_padding = 0,
|
||||
close_callback = function() self:onClose() end,
|
||||
close_callback = function()
|
||||
self:onClose()
|
||||
end,
|
||||
show_parent = self,
|
||||
}
|
||||
local light_level = FrameContainer:new{
|
||||
local inner_frame = FrameContainer:new{
|
||||
padding = Size.padding.button,
|
||||
margin = Size.margin.small,
|
||||
bordersize = 0,
|
||||
self:generateProgressGroup(self.width, math.floor(self.screen_height * 0.2),
|
||||
self.fl_cur, self.one_step)
|
||||
main_container,
|
||||
}
|
||||
self.light_frame = FrameContainer:new{
|
||||
local center_container = CenterContainer:new{
|
||||
dimen = Geom:new{
|
||||
w = self.width,
|
||||
h = inner_frame:getSize().h,
|
||||
},
|
||||
inner_frame,
|
||||
}
|
||||
self.frame = FrameContainer:new{
|
||||
radius = Size.radius.window,
|
||||
bordersize = Size.border.window,
|
||||
padding = 0,
|
||||
@@ -437,39 +416,123 @@ function FrontLightWidget:update()
|
||||
VerticalGroup:new{
|
||||
align = "left",
|
||||
title_bar,
|
||||
CenterContainer:new{
|
||||
dimen = Geom:new{
|
||||
w = self.width,
|
||||
h = light_level:getSize().h,
|
||||
},
|
||||
light_level,
|
||||
},
|
||||
center_container,
|
||||
}
|
||||
}
|
||||
self[1] = WidgetContainer:new{
|
||||
align = "center",
|
||||
dimen =Geom:new{
|
||||
dimen = Geom:new{
|
||||
x = 0, y = 0,
|
||||
w = self.screen_width,
|
||||
h = self.screen_height,
|
||||
},
|
||||
FrameContainer:new{
|
||||
bordersize = 0,
|
||||
self.light_frame,
|
||||
self.frame,
|
||||
},
|
||||
}
|
||||
end
|
||||
|
||||
function FrontLightWidget:update()
|
||||
self:refocusWidget()
|
||||
|
||||
UIManager:setDirty(self, function()
|
||||
return "ui", self.frame.dimen
|
||||
end)
|
||||
return true
|
||||
end
|
||||
|
||||
function FrontLightWidget:updateBrightnessWidgets()
|
||||
self.fl_progress:setPercentage(self.fl.cur / self.fl.max)
|
||||
self.fl_level:setText(tostring(self.fl.cur))
|
||||
if self.fl.cur == self.fl.min then
|
||||
self.fl_minus:disable()
|
||||
else
|
||||
self.fl_minus:enable()
|
||||
end
|
||||
if self.fl.cur == self.fl.max then
|
||||
self.fl_plus:disable()
|
||||
else
|
||||
self.fl_plus:enable()
|
||||
end
|
||||
end
|
||||
|
||||
function FrontLightWidget:refreshBrightnessWidgets()
|
||||
self:updateBrightnessWidgets()
|
||||
self:update()
|
||||
end
|
||||
|
||||
function FrontLightWidget:setBrightness(intensity)
|
||||
-- Let fl.min through, as that's what we use for the Toggle button ;).
|
||||
if intensity ~= self.fl.min and intensity == self.fl.cur then
|
||||
return
|
||||
end
|
||||
|
||||
-- Set brightness
|
||||
self:setFrontLightIntensity(intensity)
|
||||
|
||||
-- Update the progress bar
|
||||
self:updateBrightnessWidgets()
|
||||
|
||||
-- Refresh widget
|
||||
self:update()
|
||||
end
|
||||
|
||||
function FrontLightWidget:setWarmth(warmth, update_position)
|
||||
if warmth == self.nl.cur then
|
||||
return
|
||||
end
|
||||
|
||||
-- Set warmth
|
||||
self.nl.cur = warmth
|
||||
self.powerd:setWarmth(self.powerd:fromNativeWarmth(self.nl.cur))
|
||||
|
||||
-- Update the progress bar, if we were called from outside ButtonProgressWidget
|
||||
-- (as it already handles that internally ;)).
|
||||
if update_position then
|
||||
self.nl_progress:setPosition(warmth, self.nl_progress.default_position)
|
||||
end
|
||||
|
||||
self.nl_level:setText(tostring(self.nl.cur))
|
||||
if self.nl.cur == self.nl.min then
|
||||
self.nl_minus:disable()
|
||||
else
|
||||
self.nl_minus:enable()
|
||||
end
|
||||
if self.nl.cur == self.nl.max then
|
||||
self.nl_plus:disable()
|
||||
else
|
||||
self.nl_plus:enable()
|
||||
end
|
||||
|
||||
-- Refresh widget
|
||||
self:update()
|
||||
end
|
||||
|
||||
function FrontLightWidget:setFrontLightIntensity(intensity)
|
||||
self.fl.cur = intensity
|
||||
|
||||
-- min (which is always 0) means toggle
|
||||
if self.fl.cur == self.fl.min then
|
||||
self.powerd:toggleFrontlight()
|
||||
else
|
||||
self.powerd:setIntensity(self.fl.cur)
|
||||
end
|
||||
|
||||
-- Retrieve the real level (different from intensity on toggle)
|
||||
self.fl.cur = self.powerd:frontlightIntensity()
|
||||
end
|
||||
|
||||
function FrontLightWidget:onCloseWidget()
|
||||
UIManager:setDirty(nil, function()
|
||||
return "flashui", self.light_frame.dimen
|
||||
return "flashui", self.frame.dimen
|
||||
end)
|
||||
end
|
||||
|
||||
function FrontLightWidget:onShow()
|
||||
-- NOTE: Keep this one as UI, it'll get coalesced...
|
||||
UIManager:setDirty(self, function()
|
||||
return "ui", self.light_frame.dimen
|
||||
return "ui", self.frame.dimen
|
||||
end)
|
||||
return true
|
||||
end
|
||||
@@ -487,19 +550,19 @@ end
|
||||
function FrontLightWidget:onTapProgress(arg, ges_ev)
|
||||
-- The throttling has a tendency to wreak a bit of a havoc,
|
||||
-- so, if the widget hasn't been repainted yet, go away.
|
||||
if not self.fl_group.dimen or not self.light_frame.dimen then
|
||||
if not self.fl_progress.dimen or not self.frame.dimen then
|
||||
return true
|
||||
end
|
||||
|
||||
if ges_ev.pos:intersectWith(self.fl_group.dimen) then
|
||||
if ges_ev.pos:intersectWith(self.fl_progress.dimen) then
|
||||
-- Unschedule any pending updates.
|
||||
UIManager:unschedule(self.update)
|
||||
UIManager:unschedule(self.refreshBrightnessWidgets)
|
||||
|
||||
local perc = self.fl_group:getPercentageFromPosition(ges_ev.pos)
|
||||
local perc = self.fl_progress:getPercentageFromPosition(ges_ev.pos)
|
||||
if not perc then
|
||||
return true
|
||||
end
|
||||
local num = Math.round(perc * self.fl_max)
|
||||
local num = Math.round(perc * self.fl.max)
|
||||
|
||||
-- Always set the frontlight intensity.
|
||||
self:setFrontLightIntensity(num)
|
||||
@@ -512,17 +575,17 @@ function FrontLightWidget:onTapProgress(arg, ges_ev)
|
||||
self.last_time = current_time
|
||||
else
|
||||
-- Schedule a final update after we stop panning.
|
||||
UIManager:scheduleIn(0.075, self.update, self)
|
||||
UIManager:scheduleIn(0.075, self.refreshBrightnessWidgets, self)
|
||||
return true
|
||||
end
|
||||
end
|
||||
|
||||
self:update()
|
||||
elseif not ges_ev.pos:intersectWith(self.light_frame.dimen) and ges_ev.ges == "tap" then
|
||||
-- close if tap outside
|
||||
self:refreshBrightnessWidgets()
|
||||
elseif not ges_ev.pos:intersectWith(self.frame.dimen) and ges_ev.ges == "tap" then
|
||||
-- Close when tapping outside.
|
||||
self:onClose()
|
||||
end
|
||||
-- otherwise, do nothing (it's easy missing taping a button)
|
||||
-- Otherwise, do nothing (it's easy to miss a button).
|
||||
return true
|
||||
end
|
||||
|
||||
|
||||
Reference in New Issue
Block a user