From b014d789ffa84c0d8bc84a8e6bef3dba9e69a470 Mon Sep 17 00:00:00 2001 From: ruki Date: Sun, 29 Nov 2020 22:14:41 +0800 Subject: [PATCH] fix scroll bug --- src/ltui/choicebox.lua | 33 +++++++++++++++++++++++---- src/ltui/menuconf.lua | 52 +++++++++++++++++++++++------------------- 2 files changed, 58 insertions(+), 27 deletions(-) diff --git a/src/ltui/choicebox.lua b/src/ltui/choicebox.lua index 969cda6..bbb889f 100644 --- a/src/ltui/choicebox.lua +++ b/src/ltui/choicebox.lua @@ -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()) + else + self:select_next() end - self:select_next() self:_notify_scrolled() return true elseif e.key_name == "Up" then if self:current() == self:first() then self:scroll(-self:height()) + else + self:select_prev() end - self:select_prev() self:_notify_scrolled() return true elseif e.key_name == "Enter" or e.key_name == " " then diff --git a/src/ltui/menuconf.lua b/src/ltui/menuconf.lua index ac109c6..6955bbe 100644 --- a/src/ltui/menuconf.lua +++ b/src/ltui/menuconf.lua @@ -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()) + else + self:select_next() end - self:select_next() self:_notify_scrolled() return true elseif e.key_name == "Up" then if self:current() == self:first() then self:scroll(-self:height()) + else + self:select_prev() end - self:select_prev() self:_notify_scrolled() return true elseif e.key_name == "Enter" or e.key_name == " " then