2017-08-29 19:11:16 +00:00
|
|
|
local Blitbuffer = require("ffi/blitbuffer")
|
|
|
|
local Button = require("ui/widget/button")
|
|
|
|
local Device = require("device")
|
|
|
|
local FocusManager = require("ui/widget/focusmanager")
|
2013-10-18 20:38:07 +00:00
|
|
|
local HorizontalGroup = require("ui/widget/horizontalgroup")
|
2017-08-29 19:11:16 +00:00
|
|
|
local LineWidget = require("ui/widget/linewidget")
|
2017-09-13 14:56:20 +00:00
|
|
|
local Size = require("ui/size")
|
2015-09-13 08:09:00 +00:00
|
|
|
local VerticalGroup = require("ui/widget/verticalgroup")
|
2013-10-18 20:38:07 +00:00
|
|
|
local VerticalSpan = require("ui/widget/verticalspan")
|
|
|
|
local Geom = require("ui/geometry")
|
2015-09-13 08:09:00 +00:00
|
|
|
local Screen = Device.screen
|
2013-06-15 15:13:19 +00:00
|
|
|
|
2015-09-13 08:09:00 +00:00
|
|
|
local ButtonTable = FocusManager:new{
|
2014-03-13 13:52:43 +00:00
|
|
|
width = Screen:getWidth(),
|
|
|
|
buttons = {
|
|
|
|
{
|
|
|
|
{text="OK", enabled=true, callback=nil},
|
|
|
|
{text="Cancel", enabled=false, callback=nil},
|
|
|
|
},
|
|
|
|
},
|
2017-09-13 14:56:20 +00:00
|
|
|
sep_width = Size.line.medium,
|
2018-07-06 19:05:12 +00:00
|
|
|
padding = Size.padding.default,
|
2013-10-24 17:47:22 +00:00
|
|
|
|
2014-03-13 13:52:43 +00:00
|
|
|
zero_sep = false,
|
|
|
|
button_font_face = "cfont",
|
|
|
|
button_font_size = 20,
|
2013-06-15 15:13:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function ButtonTable:init()
|
2017-10-07 10:11:00 +00:00
|
|
|
self.selected = { x = 1, y = 1 }
|
2016-05-26 06:09:49 +00:00
|
|
|
self.buttons_layout = {}
|
2018-08-06 19:16:30 +00:00
|
|
|
self.button_by_id = {}
|
2015-09-13 08:09:00 +00:00
|
|
|
self.container = VerticalGroup:new{ width = self.width }
|
|
|
|
table.insert(self, self.container)
|
2014-03-13 13:52:43 +00:00
|
|
|
if self.zero_sep then
|
2017-10-08 15:53:25 +00:00
|
|
|
-- If we're asked to add a first line, don't add a vspan before: caller
|
|
|
|
-- must do its own padding before.
|
|
|
|
-- Things look better when the first line is gray like the others.
|
|
|
|
self:addHorizontalSep(false, true, true)
|
|
|
|
else
|
|
|
|
self:addHorizontalSep(false, false, true)
|
2014-03-13 13:52:43 +00:00
|
|
|
end
|
2016-05-26 06:09:49 +00:00
|
|
|
local row_cnt = #self.buttons
|
|
|
|
for i = 1, row_cnt do
|
2018-03-18 10:42:35 +00:00
|
|
|
local buttons_layout_line = {}
|
2014-03-13 13:52:43 +00:00
|
|
|
local horizontal_group = HorizontalGroup:new{}
|
2016-05-26 06:09:49 +00:00
|
|
|
local row = self.buttons[i]
|
|
|
|
local column_cnt = #row
|
|
|
|
local sizer_space = self.sep_width * (column_cnt - 1) + 2
|
|
|
|
for j = 1, column_cnt do
|
|
|
|
local btn_entry = row[j]
|
2014-03-13 13:52:43 +00:00
|
|
|
local button = Button:new{
|
2016-05-26 06:09:49 +00:00
|
|
|
text = btn_entry.text,
|
2019-06-28 02:46:16 +00:00
|
|
|
text_func = btn_entry.text_func,
|
2016-05-26 06:09:49 +00:00
|
|
|
enabled = btn_entry.enabled,
|
|
|
|
callback = btn_entry.callback,
|
2019-02-15 23:42:27 +00:00
|
|
|
hold_callback = btn_entry.hold_callback,
|
2016-05-26 06:09:49 +00:00
|
|
|
width = (self.width - sizer_space)/column_cnt,
|
2017-08-29 19:11:16 +00:00
|
|
|
max_width = (self.width - sizer_space)/column_cnt - 2*self.sep_width - 2*self.padding,
|
2014-03-13 13:52:43 +00:00
|
|
|
bordersize = 0,
|
|
|
|
margin = 0,
|
2019-11-01 00:52:05 +00:00
|
|
|
padding = Size.padding.buttontable, -- a bit taller than standalone buttons, for easier tap
|
|
|
|
padding_h = 0, -- allow text to take more of the horizontal space
|
2014-03-13 13:52:43 +00:00
|
|
|
text_font_face = self.button_font_face,
|
|
|
|
text_font_size = self.button_font_size,
|
2014-05-01 10:37:12 +00:00
|
|
|
show_parent = self.show_parent,
|
2014-03-13 13:52:43 +00:00
|
|
|
}
|
2018-08-06 19:16:30 +00:00
|
|
|
if btn_entry.id then
|
|
|
|
self.button_by_id[btn_entry.id] = button
|
|
|
|
end
|
2014-03-13 13:52:43 +00:00
|
|
|
local button_dim = button:getSize()
|
|
|
|
local vertical_sep = LineWidget:new{
|
2019-11-11 09:05:28 +00:00
|
|
|
background = Blitbuffer.COLOR_GRAY,
|
2014-03-13 13:52:43 +00:00
|
|
|
dimen = Geom:new{
|
|
|
|
w = self.sep_width,
|
|
|
|
h = button_dim.h,
|
|
|
|
}
|
|
|
|
}
|
2018-03-18 10:42:35 +00:00
|
|
|
buttons_layout_line[j] = button
|
2014-03-13 13:52:43 +00:00
|
|
|
table.insert(horizontal_group, button)
|
2016-05-26 06:09:49 +00:00
|
|
|
if j < column_cnt then
|
2014-03-13 13:52:43 +00:00
|
|
|
table.insert(horizontal_group, vertical_sep)
|
|
|
|
end
|
|
|
|
end -- end for each button
|
2015-09-13 08:09:00 +00:00
|
|
|
table.insert(self.container, horizontal_group)
|
2016-05-26 06:09:49 +00:00
|
|
|
if i < row_cnt then
|
2017-10-08 15:53:25 +00:00
|
|
|
self:addHorizontalSep(true, true, true)
|
2014-03-13 13:52:43 +00:00
|
|
|
end
|
2018-03-18 10:42:35 +00:00
|
|
|
if column_cnt > 0 then
|
|
|
|
--Only add line that are not separator to the focusmanager
|
|
|
|
table.insert(self.buttons_layout, buttons_layout_line)
|
|
|
|
end
|
2014-03-13 13:52:43 +00:00
|
|
|
end -- end for each button line
|
2017-10-08 15:53:25 +00:00
|
|
|
self:addHorizontalSep(true, false, false)
|
A few graphics fixes after #4541 (#4554)
* Various FocusManager related tweaks to limit its usage to devices with a DPad, and prevent initial button highlights in Dialogs on devices where it makes no sense (i.e., those without a DPad. And even on DPad devices, I'm not even sure how we'd go about making one of those pop up anyway, because no Touch ;)!).
* One mysterious fix to text-only Buttons so that the flash_ui highlight always works, and always honors `FrameContainer`'s pill shape. (Before that, an unhighlight on a text button with a callback that didn't repaint anything [say, the find first/find last buttons in the Reader's search bar when you're already on the first/last match] would do a square black highlight, and a white pill-shaped unhighlight (leaving the black corners visible)).
The workaround makes *absolutely* no sense to me (as `self[1] -> self.frame`, AFAICT), but it works, and ensures all highlights/unhighlights are pill-shaped, so at least we're not doing maths for rounded corners for nothing ;).
2019-02-07 23:56:32 +00:00
|
|
|
if Device:hasDPad() then
|
2016-05-26 06:09:49 +00:00
|
|
|
self.layout = self.buttons_layout
|
2015-09-13 08:09:00 +00:00
|
|
|
self.layout[1][1]:onFocus()
|
2018-03-30 21:22:18 +00:00
|
|
|
self.key_events.SelectByKeyPress = { {{"Press"}} }
|
2015-09-13 08:09:00 +00:00
|
|
|
else
|
|
|
|
self.key_events = {} -- deregister all key press event listeners
|
|
|
|
end
|
2013-06-15 15:13:19 +00:00
|
|
|
end
|
|
|
|
|
2017-10-08 15:53:25 +00:00
|
|
|
function ButtonTable:addHorizontalSep(vspan_before, add_line, vspan_after, black_line)
|
|
|
|
if vspan_before then
|
|
|
|
table.insert(self.container,
|
|
|
|
VerticalSpan:new{ width = Size.span.vertical_default })
|
|
|
|
end
|
|
|
|
if add_line then
|
|
|
|
table.insert(self.container, LineWidget:new{
|
2019-11-11 09:05:28 +00:00
|
|
|
background = black_line and Blitbuffer.COLOR_BLACK or Blitbuffer.COLOR_GRAY,
|
2017-10-08 15:53:25 +00:00
|
|
|
dimen = Geom:new{
|
|
|
|
w = self.width,
|
|
|
|
h = self.sep_width,
|
|
|
|
}
|
|
|
|
})
|
|
|
|
end
|
|
|
|
if vspan_after then
|
|
|
|
table.insert(self.container,
|
|
|
|
VerticalSpan:new{ width = Size.span.vertical_default })
|
|
|
|
end
|
2015-09-13 08:09:00 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
function ButtonTable:onSelectByKeyPress()
|
2018-03-18 10:42:35 +00:00
|
|
|
local item = self:getFocusItem()
|
|
|
|
if item.enabled then
|
|
|
|
item.callback()
|
|
|
|
end
|
2013-06-15 15:13:19 +00:00
|
|
|
end
|
2013-10-18 20:38:07 +00:00
|
|
|
|
2018-08-06 19:16:30 +00:00
|
|
|
function ButtonTable:getButtonById(id)
|
|
|
|
return self.button_by_id[id] -- nil if not found
|
|
|
|
end
|
|
|
|
|
2013-10-18 20:38:07 +00:00
|
|
|
return ButtonTable
|