fix scroll bug

This commit is contained in:
ruki
2020-11-29 22:14:41 +08:00
parent 75b1d2e4c7
commit b014d789ff
2 changed files with 58 additions and 27 deletions

View File

@@ -60,7 +60,7 @@ function choicebox:load(values, selected)
-- insert top-n items
local startindex = self._STARTINDEX
for idx = startindex, startindex + self:height() do
for idx = startindex, startindex + self:height() - 1 do
local item = items[idx]
if item then
self:insert(item)
@@ -97,7 +97,7 @@ function choicebox:scroll(count)
end
self._STARTINDEX = startindex
self:clear()
for idx = startindex, startindex + self:height() do
for idx = startindex, startindex + self:height() - 1 do
local item = items[idx]
if item then
item:bounds():move2(0, idx - startindex)
@@ -115,21 +115,46 @@ function choicebox:scroll(count)
end
end
-- on resize
function choicebox:on_resize()
local items = self:_items()
local totalcount = #items
local startindex = self._STARTINDEX
for idx = 1, totalcount do
local item = items[idx]
if item then
if idx >= startindex and idx < startindex + self:height() then
if not self:view(item:name()) then
item:bounds():move2(0, idx - startindex)
self:insert(item)
end
else
if self:view(item:name()) then
self:remove(item)
end
end
end
end
panel.on_resize(self)
end
-- on event
function choicebox:on_event(e)
if e.type == event.ev_keyboard then
if e.key_name == "Down" then
if self:current() == self:last() then
self:scroll(self:height())
end
else
self:select_next()
end
self:_notify_scrolled()
return true
elseif e.key_name == "Up" then
if self:current() == self:first() then
self:scroll(-self:height())
end
else
self:select_prev()
end
self:_notify_scrolled()
return true
elseif e.key_name == "Enter" or e.key_name == " " then

View File

@@ -46,25 +46,6 @@ function menuconf:init(name, bounds)
-- init start index
self._STARTINDEX = 1
-- on resize for panel
self:action_add(action.ac_on_resized, function (v)
local items = self:_items()
local totalcount = #items
local startindex = self._STARTINDEX
self:clear()
for idx = startindex, startindex + self:height() do
local item = items[idx]
if item then
item:bounds():move2(0, idx - startindex)
self:insert(item)
else
break
end
end
self:select(self:first())
self:invalidate()
end)
end
-- load configs
@@ -93,7 +74,7 @@ function menuconf:load(configs)
-- insert top-n items
local startindex = self._STARTINDEX
for idx = startindex, startindex + self:height() do
for idx = startindex, startindex + self:height() - 1 do
local item = items[idx]
if item then
self:insert(item)
@@ -130,7 +111,7 @@ function menuconf:scroll(count)
end
self._STARTINDEX = startindex
self:clear()
for idx = startindex, startindex + self:height() do
for idx = startindex, startindex + self:height() - 1 do
local item = items[idx]
if item then
item:bounds():move2(0, idx - startindex)
@@ -148,6 +129,29 @@ function menuconf:scroll(count)
end
end
-- on resize
function menuconf:on_resize()
local items = self:_items()
local totalcount = #items
local startindex = self._STARTINDEX
for idx = 1, totalcount do
local item = items[idx]
if item then
if idx >= startindex and idx < startindex + self:height() then
if not self:view(item:name()) then
item:bounds():move2(0, idx - startindex)
self:insert(item)
end
else
if self:view(item:name()) then
self:remove(item)
end
end
end
end
panel.on_resize(self)
end
-- on event
function menuconf:on_event(e)
local back = false
@@ -155,15 +159,17 @@ function menuconf:on_event(e)
if e.key_name == "Down" then
if self:current() == self:last() then
self:scroll(self:height())
end
else
self:select_next()
end
self:_notify_scrolled()
return true
elseif e.key_name == "Up" then
if self:current() == self:first() then
self:scroll(-self:height())
end
else
self:select_prev()
end
self:_notify_scrolled()
return true
elseif e.key_name == "Enter" or e.key_name == " " then