2017-09-11 08:32:39 +00:00
|
|
|
--[[--
|
|
|
|
Displays a button that toggles between states. Used in bottom configuration panel.
|
|
|
|
|
|
|
|
@usage
|
|
|
|
local ToggleSwitch = require("ui/widget/toggleswitch")
|
|
|
|
]]
|
|
|
|
|
2019-12-06 21:55:39 +00:00
|
|
|
local BD = require("ui/bidi")
|
2017-09-11 08:32:39 +00:00
|
|
|
local Blitbuffer = require("ffi/blitbuffer")
|
2013-10-18 20:38:07 +00:00
|
|
|
local CenterContainer = require("ui/widget/container/centercontainer")
|
2017-09-11 08:32:39 +00:00
|
|
|
local Device = require("device")
|
2022-01-25 00:32:46 +00:00
|
|
|
local FocusManager = require("ui/widget/focusmanager")
|
2013-10-18 20:38:07 +00:00
|
|
|
local Font = require("ui/font")
|
|
|
|
local Geom = require("ui/geometry")
|
2017-09-11 08:32:39 +00:00
|
|
|
local GestureRange = require("ui/gesturerange")
|
|
|
|
local HorizontalGroup = require("ui/widget/horizontalgroup")
|
|
|
|
local FrameContainer = require("ui/widget/container/framecontainer")
|
2021-05-20 21:14:11 +00:00
|
|
|
local Notification = require("ui/widget/notification")
|
2017-09-13 14:56:20 +00:00
|
|
|
local Size = require("ui/size")
|
2017-09-11 08:32:39 +00:00
|
|
|
local TextWidget = require("ui/widget/textwidget")
|
2013-10-18 20:38:07 +00:00
|
|
|
local UIManager = require("ui/uimanager")
|
2017-09-11 08:32:39 +00:00
|
|
|
local VerticalGroup = require("ui/widget/verticalgroup")
|
2013-10-18 20:38:07 +00:00
|
|
|
local _ = require("gettext")
|
2017-09-11 08:32:39 +00:00
|
|
|
local Screen = Device.screen
|
2013-03-16 11:48:32 +00:00
|
|
|
|
Clarify our OOP semantics across the codebase (#9586)
Basically:
* Use `extend` for class definitions
* Use `new` for object instantiations
That includes some minor code cleanups along the way:
* Updated `Widget`'s docs to make the semantics clearer.
* Removed `should_restrict_JIT` (it's been dead code since https://github.com/koreader/android-luajit-launcher/pull/283)
* Minor refactoring of LuaSettings/LuaData/LuaDefaults/DocSettings to behave (mostly, they are instantiated via `open` instead of `new`) like everything else and handle inheritance properly (i.e., DocSettings is now a proper LuaSettings subclass).
* Default to `WidgetContainer` instead of `InputContainer` for stuff that doesn't actually setup key/gesture events.
* Ditto for explicit `*Listener` only classes, make sure they're based on `EventListener` instead of something uselessly fancier.
* Unless absolutely necessary, do not store references in class objects, ever; only values. Instead, always store references in instances, to avoid both sneaky inheritance issues, and sneaky GC pinning of stale references.
* ReaderUI: Fix one such issue with its `active_widgets` array, with critical implications, as it essentially pinned *all* of ReaderUI's modules, including their reference to the `Document` instance (i.e., that was a big-ass leak).
* Terminal: Make sure the shell is killed on plugin teardown.
* InputText: Fix Home/End/Del physical keys to behave sensibly.
* InputContainer/WidgetContainer: If necessary, compute self.dimen at paintTo time (previously, only InputContainers did, which might have had something to do with random widgets unconcerned about input using it as a baseclass instead of WidgetContainer...).
* OverlapGroup: Compute self.dimen at *init* time, because for some reason it needs to do that, but do it directly in OverlapGroup instead of going through a weird WidgetContainer method that it was the sole user of.
* ReaderCropping: Under no circumstances should a Document instance member (here, self.bbox) risk being `nil`ed!
* Kobo: Minor code cleanups.
2022-10-06 00:14:48 +00:00
|
|
|
local ToggleLabel = TextWidget:extend{
|
2014-03-13 13:52:43 +00:00
|
|
|
bold = true,
|
2014-10-22 13:34:11 +00:00
|
|
|
bgcolor = Blitbuffer.COLOR_WHITE,
|
|
|
|
fgcolor = Blitbuffer.COLOR_BLACK,
|
2013-03-16 11:48:32 +00:00
|
|
|
}
|
|
|
|
|
Clarify our OOP semantics across the codebase (#9586)
Basically:
* Use `extend` for class definitions
* Use `new` for object instantiations
That includes some minor code cleanups along the way:
* Updated `Widget`'s docs to make the semantics clearer.
* Removed `should_restrict_JIT` (it's been dead code since https://github.com/koreader/android-luajit-launcher/pull/283)
* Minor refactoring of LuaSettings/LuaData/LuaDefaults/DocSettings to behave (mostly, they are instantiated via `open` instead of `new`) like everything else and handle inheritance properly (i.e., DocSettings is now a proper LuaSettings subclass).
* Default to `WidgetContainer` instead of `InputContainer` for stuff that doesn't actually setup key/gesture events.
* Ditto for explicit `*Listener` only classes, make sure they're based on `EventListener` instead of something uselessly fancier.
* Unless absolutely necessary, do not store references in class objects, ever; only values. Instead, always store references in instances, to avoid both sneaky inheritance issues, and sneaky GC pinning of stale references.
* ReaderUI: Fix one such issue with its `active_widgets` array, with critical implications, as it essentially pinned *all* of ReaderUI's modules, including their reference to the `Document` instance (i.e., that was a big-ass leak).
* Terminal: Make sure the shell is killed on plugin teardown.
* InputText: Fix Home/End/Del physical keys to behave sensibly.
* InputContainer/WidgetContainer: If necessary, compute self.dimen at paintTo time (previously, only InputContainers did, which might have had something to do with random widgets unconcerned about input using it as a baseclass instead of WidgetContainer...).
* OverlapGroup: Compute self.dimen at *init* time, because for some reason it needs to do that, but do it directly in OverlapGroup instead of going through a weird WidgetContainer method that it was the sole user of.
* ReaderCropping: Under no circumstances should a Document instance member (here, self.bbox) risk being `nil`ed!
* Kobo: Minor code cleanups.
2022-10-06 00:14:48 +00:00
|
|
|
local ToggleSwitch = FocusManager:extend{
|
2014-11-20 22:07:39 +00:00
|
|
|
width = Screen:scaleBySize(216),
|
2017-09-13 14:56:20 +00:00
|
|
|
height = Size.item.height_default,
|
2017-09-11 08:32:39 +00:00
|
|
|
bgcolor = Blitbuffer.COLOR_WHITE, -- unfocused item color
|
2019-03-14 19:58:45 +00:00
|
|
|
fgcolor = Blitbuffer.COLOR_DARK_GRAY, -- focused item color
|
2014-11-23 08:52:08 +00:00
|
|
|
font_face = "cfont",
|
|
|
|
font_size = 16,
|
2015-03-16 13:49:53 +00:00
|
|
|
enabled = true,
|
2016-04-26 02:45:55 +00:00
|
|
|
row_count = 1,
|
2013-03-16 11:48:32 +00:00
|
|
|
}
|
2013-02-19 03:44:44 +00:00
|
|
|
|
|
|
|
function ToggleSwitch:init()
|
2022-01-25 00:32:46 +00:00
|
|
|
self.layout = {{}}
|
2016-04-26 02:45:55 +00:00
|
|
|
-- Item count per row
|
|
|
|
self.n_pos = math.ceil(#self.toggle / self.row_count)
|
2014-03-13 13:52:43 +00:00
|
|
|
self.position = nil
|
2013-04-08 07:29:23 +00:00
|
|
|
|
2014-10-22 13:34:11 +00:00
|
|
|
self.toggle_frame = FrameContainer:new{
|
|
|
|
background = Blitbuffer.COLOR_WHITE,
|
2019-03-14 19:58:45 +00:00
|
|
|
color = Blitbuffer.COLOR_DARK_GRAY,
|
2017-09-13 14:56:20 +00:00
|
|
|
radius = Size.radius.window,
|
2018-08-19 18:21:03 +00:00
|
|
|
bordersize = Size.border.thin,
|
2017-09-13 14:56:20 +00:00
|
|
|
padding = Size.padding.small,
|
2015-03-16 13:49:53 +00:00
|
|
|
dim = not self.enabled,
|
2014-10-22 13:34:11 +00:00
|
|
|
}
|
2013-04-08 07:29:23 +00:00
|
|
|
|
2016-04-26 02:45:55 +00:00
|
|
|
self.toggle_content = VerticalGroup:new{}
|
|
|
|
for i = 1, self.row_count do
|
|
|
|
table.insert(self.toggle_content, HorizontalGroup:new{})
|
|
|
|
end
|
|
|
|
|
2018-08-19 18:21:03 +00:00
|
|
|
local item_padding = Size.padding.default -- only used to check if text truncate needed
|
|
|
|
local item_border_size = Size.border.thin
|
|
|
|
local frame_inner_width = self.width - 2*self.toggle_frame.padding - 2* self.toggle_frame.bordersize
|
2019-09-25 17:44:57 +00:00
|
|
|
-- We'll need to adjust items width and distribute the accumulated fractional part to some
|
|
|
|
-- of them for proper visual alignment
|
|
|
|
local item_width_real = frame_inner_width / self.n_pos - 2*item_border_size
|
|
|
|
local item_width = math.floor(item_width_real)
|
|
|
|
local item_width_adjust = item_width_real - item_width
|
2018-08-19 18:21:03 +00:00
|
|
|
-- Note: the height provided by ConfigDialog might be smaller than needed,
|
|
|
|
-- it gets too thin if we account for padding & border
|
2019-09-25 17:44:57 +00:00
|
|
|
local item_height = self.height / self.row_count
|
|
|
|
local item_width_to_add = 0
|
2016-04-26 02:45:55 +00:00
|
|
|
for i = 1, #self.toggle do
|
2019-09-25 17:44:57 +00:00
|
|
|
local real_item_width = item_width
|
|
|
|
item_width_to_add = item_width_to_add + item_width_adjust
|
|
|
|
if item_width_to_add >= 1 then
|
|
|
|
-- One pixel wider to better align the entire widget
|
|
|
|
real_item_width = item_width + math.floor(item_width_to_add)
|
|
|
|
item_width_to_add = item_width_to_add - math.floor(item_width_to_add)
|
|
|
|
end
|
2017-10-28 19:28:41 +00:00
|
|
|
local text = self.toggle[i]
|
|
|
|
local face = Font:getFace(self.font_face, self.font_size)
|
2014-03-13 13:52:43 +00:00
|
|
|
local label = ToggleLabel:new{
|
2017-10-28 19:28:41 +00:00
|
|
|
text = text,
|
|
|
|
face = face,
|
2019-10-21 13:20:40 +00:00
|
|
|
max_width = real_item_width - item_padding,
|
2014-03-13 13:52:43 +00:00
|
|
|
}
|
|
|
|
local content = CenterContainer:new{
|
2019-09-25 17:44:57 +00:00
|
|
|
dimen = Geom:new{
|
|
|
|
w = real_item_width,
|
|
|
|
h = item_height,
|
|
|
|
},
|
2014-03-13 13:52:43 +00:00
|
|
|
label,
|
|
|
|
}
|
|
|
|
local button = FrameContainer:new{
|
2014-10-22 13:34:11 +00:00
|
|
|
background = Blitbuffer.COLOR_WHITE,
|
2019-03-14 19:58:45 +00:00
|
|
|
color = Blitbuffer.COLOR_DARK_GRAY,
|
2014-03-13 13:52:43 +00:00
|
|
|
margin = 0,
|
2017-09-13 14:56:20 +00:00
|
|
|
radius = Size.radius.window,
|
2018-08-19 18:21:03 +00:00
|
|
|
bordersize = item_border_size,
|
2014-03-13 13:52:43 +00:00
|
|
|
padding = 0,
|
2022-01-25 00:32:46 +00:00
|
|
|
focusable = true,
|
|
|
|
focus_border_size = item_border_size,
|
|
|
|
focus_border_color = Blitbuffer.COLOR_BLACK,
|
2014-03-13 13:52:43 +00:00
|
|
|
content,
|
|
|
|
}
|
2016-04-26 02:45:55 +00:00
|
|
|
table.insert(self.toggle_content[math.ceil(i / self.n_pos)], button)
|
2022-01-25 00:32:46 +00:00
|
|
|
table.insert(self.layout[1], button)
|
2014-03-13 13:52:43 +00:00
|
|
|
end
|
|
|
|
self.toggle_frame[1] = self.toggle_content
|
|
|
|
self[1] = self.toggle_frame
|
|
|
|
self.dimen = Geom:new(self.toggle_frame:getSize())
|
2022-01-25 00:32:46 +00:00
|
|
|
self.ges_events = {
|
|
|
|
TapSelect = {
|
|
|
|
GestureRange:new{
|
|
|
|
ges = "tap",
|
|
|
|
range = self.dimen,
|
2014-03-13 13:52:43 +00:00
|
|
|
},
|
2022-01-25 00:32:46 +00:00
|
|
|
doc = "Toggle switch",
|
|
|
|
},
|
|
|
|
HoldSelect = {
|
|
|
|
GestureRange:new{
|
|
|
|
ges = "hold",
|
|
|
|
range = self.dimen,
|
2014-07-03 08:30:24 +00:00
|
|
|
},
|
2022-01-25 00:32:46 +00:00
|
|
|
doc = "Hold switch",
|
|
|
|
},
|
|
|
|
}
|
2013-02-19 03:44:44 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
function ToggleSwitch:update()
|
2014-03-13 13:52:43 +00:00
|
|
|
local pos = self.position
|
2016-04-26 02:45:55 +00:00
|
|
|
for i = 1, #self.toggle_content do
|
|
|
|
local row = self.toggle_content[i]
|
|
|
|
for j = 1, #row do
|
|
|
|
local cell = row[j]
|
|
|
|
if pos == (i - 1) * self.n_pos + j then
|
|
|
|
cell.color = self.fgcolor
|
|
|
|
cell.background = self.fgcolor
|
|
|
|
cell[1][1].fgcolor = Blitbuffer.COLOR_WHITE
|
|
|
|
else
|
|
|
|
cell.color = self.bgcolor
|
|
|
|
cell.background = self.bgcolor
|
|
|
|
cell[1][1].fgcolor = Blitbuffer.COLOR_BLACK
|
|
|
|
end
|
2014-03-13 13:52:43 +00:00
|
|
|
end
|
|
|
|
end
|
2013-02-19 03:44:44 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
function ToggleSwitch:setPosition(position)
|
2014-03-13 13:52:43 +00:00
|
|
|
self.position = position
|
|
|
|
self:update()
|
2013-02-19 03:44:44 +00:00
|
|
|
end
|
|
|
|
|
2019-09-27 10:58:40 +00:00
|
|
|
function ToggleSwitch:togglePosition(position, update)
|
2014-03-13 13:52:43 +00:00
|
|
|
if self.n_pos == 2 and self.alternate ~= false then
|
|
|
|
self.position = (self.position+1)%self.n_pos
|
|
|
|
self.position = self.position == 0 and self.n_pos or self.position
|
|
|
|
elseif self.n_pos == 1 then
|
|
|
|
self.position = self.position == 1 and 0 or 1
|
|
|
|
else
|
|
|
|
self.position = position
|
|
|
|
end
|
2019-09-27 10:58:40 +00:00
|
|
|
if update then
|
|
|
|
self:update()
|
|
|
|
end
|
2013-02-19 03:44:44 +00:00
|
|
|
end
|
|
|
|
|
2018-03-22 20:01:38 +00:00
|
|
|
function ToggleSwitch:circlePosition()
|
|
|
|
if self.position then
|
|
|
|
self.position = (self.position+1)%self.n_pos
|
|
|
|
self.position = self.position == 0 and self.n_pos or self.position
|
|
|
|
self:update()
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-04-26 02:45:55 +00:00
|
|
|
function ToggleSwitch:calculatePosition(gev)
|
|
|
|
local x = (gev.pos.x - self.dimen.x) / self.dimen.w * self.n_pos
|
2019-12-06 21:55:39 +00:00
|
|
|
if BD.mirroredUILayout() then
|
|
|
|
x = self.n_pos - x
|
|
|
|
end
|
2016-04-26 02:45:55 +00:00
|
|
|
local y = (gev.pos.y - self.dimen.y) / self.dimen.h * self.row_count
|
2017-09-06 10:56:50 +00:00
|
|
|
return math.max(1, math.ceil(x)) + math.min(self.row_count-1, math.floor(y)) * self.n_pos
|
2016-04-26 02:45:55 +00:00
|
|
|
end
|
|
|
|
|
2013-02-19 03:44:44 +00:00
|
|
|
function ToggleSwitch:onTapSelect(arg, gev)
|
2017-12-17 17:27:24 +00:00
|
|
|
if not self.enabled then
|
|
|
|
if self.readonly ~= true then
|
|
|
|
return true
|
|
|
|
else
|
|
|
|
return
|
|
|
|
end
|
|
|
|
end
|
2018-03-22 20:01:38 +00:00
|
|
|
if gev then
|
|
|
|
local position = self:calculatePosition(gev)
|
2019-09-30 09:18:39 +00:00
|
|
|
if self.toggle[position] ~= "⋮" then
|
2019-09-27 10:58:40 +00:00
|
|
|
self:togglePosition(position, true)
|
|
|
|
else
|
|
|
|
self:togglePosition(position, false)
|
|
|
|
end
|
2018-03-22 20:01:38 +00:00
|
|
|
else
|
|
|
|
self:circlePosition()
|
|
|
|
end
|
|
|
|
|
2014-03-13 13:52:43 +00:00
|
|
|
--[[
|
|
|
|
if self.values then
|
|
|
|
self.values = self.values or {}
|
|
|
|
self.config:onConfigChoice(self.name, self.values[self.position])
|
|
|
|
end
|
|
|
|
if self.event then
|
|
|
|
self.args = self.args or {}
|
|
|
|
self.config:onConfigEvent(self.event, self.args[self.position])
|
|
|
|
end
|
|
|
|
--]]
|
2019-10-04 21:06:54 +00:00
|
|
|
if self.callback then
|
|
|
|
self.callback(self.position)
|
|
|
|
end
|
2019-09-30 09:18:39 +00:00
|
|
|
if self.toggle[self.position] ~= "⋮" then
|
2021-05-20 21:14:11 +00:00
|
|
|
if #self.values == 0 then -- this is a toggle which is not selectable (eg. increase, decrease)
|
|
|
|
Notification:setNotifySource(Notification.SOURCE_BOTTOM_MENU_FINE)
|
|
|
|
else
|
|
|
|
Notification:setNotifySource(Notification.SOURCE_BOTTOM_MENU_TOGGLE)
|
|
|
|
end
|
2019-09-27 10:58:40 +00:00
|
|
|
self.config:onConfigChoose(self.values, self.name,
|
2021-08-30 15:21:50 +00:00
|
|
|
self.event, self.args, self.position, self.hide_on_apply)
|
2021-05-20 21:14:11 +00:00
|
|
|
|
2019-09-27 10:58:40 +00:00
|
|
|
UIManager:setDirty(self.config, function()
|
|
|
|
return "ui", self.dimen
|
|
|
|
end)
|
2021-05-20 21:14:11 +00:00
|
|
|
|
|
|
|
UIManager:tickAfterNext(function()
|
|
|
|
Notification:setNotifySource(Notification.SOURCE_OTHER) -- only allow events, if they are activated
|
|
|
|
end)
|
2019-09-27 10:58:40 +00:00
|
|
|
end
|
2014-03-13 13:52:43 +00:00
|
|
|
return true
|
2013-02-19 03:44:44 +00:00
|
|
|
end
|
|
|
|
|
2014-07-03 08:30:24 +00:00
|
|
|
function ToggleSwitch:onHoldSelect(arg, gev)
|
2016-04-26 02:45:55 +00:00
|
|
|
local position = self:calculatePosition(gev)
|
2019-09-30 09:18:39 +00:00
|
|
|
if self.toggle[position] == "⋮" then
|
2019-09-27 10:58:40 +00:00
|
|
|
return true
|
|
|
|
end
|
2019-09-03 11:41:15 +00:00
|
|
|
if self.name == "font_fine_tune" then
|
|
|
|
--- @note Ugly hack for the only widget that uses a dual toggle for fine-tuning (others prefer a buttonprogress)
|
|
|
|
self.config:onMakeFineTuneDefault("font_size", _("Font Size"),
|
|
|
|
self.values or self.args, self.toggle, position == 1 and "-" or "+")
|
|
|
|
else
|
|
|
|
self.config:onMakeDefault(self.name, self.name_text,
|
|
|
|
self.values or self.args, self.toggle, position)
|
|
|
|
end
|
2014-07-03 08:30:24 +00:00
|
|
|
return true
|
|
|
|
end
|
|
|
|
|
2013-10-18 20:38:07 +00:00
|
|
|
return ToggleSwitch
|