Skip to content

Commit

Permalink
fix search order and restore bug
Browse files Browse the repository at this point in the history
  • Loading branch information
Bauumm committed Jan 23, 2024
1 parent ff2790e commit 7be8998
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 14 deletions.
2 changes: 1 addition & 1 deletion ui/screens/levelselect/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ state.top_bar = flex:new({
expand = true,
no_text_text = "Search a level",
change_handler = function(text)
search.create_result_layout(text, state.levels.element.elements, state.levels.element)
search.create_result_layout(text, state.levels.element.elements)
end,
}),
quad:new({
Expand Down
4 changes: 2 additions & 2 deletions ui/screens/levelselect/packs.lua
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ function pack_elements.make_pack_element(pack, sort)
if state.levels.element then
search_pattern = state.levels.element.search_pattern
-- undo search in current pack
search.create_result_layout("", state.levels.element.elements, state.levels.element)
search.create_result_layout("", state.levels.element.elements)
end
for j = 1, #pack_elements.elements do
pack_elements.elements[j].style.background_color = theme.get("contrast_background_color")
Expand Down Expand Up @@ -59,7 +59,7 @@ function pack_elements.make_pack_element(pack, sort)
end
-- restore search pattern in newly selected pack
if search_pattern then
search.create_result_layout(search_pattern, state.levels.element.elements, state.levels.element)
search.create_result_layout(search_pattern, state.levels.element.elements)
end
end,
})
Expand Down
27 changes: 16 additions & 11 deletions ui/search.lua
Original file line number Diff line number Diff line change
Expand Up @@ -39,16 +39,11 @@ function search.find(pattern, elements)
for i = 1, #elements do
local matches = search.match(elements[i], pattern)
if matches > 0 then
if #results == 0 then
results[1] = elements[i]
matches_per_result[1] = matches
else
for j = 1, #results + 1 do
if (matches_per_result[j] or 0) < matches then
table.insert(results, j, elements[i])
table.insert(matches_per_result, j, matches)
break
end
for j = 1, #results + 1 do
if matches_per_result[j] == matches or matches_per_result[j] == nil then
table.insert(results, j, elements[i])
table.insert(matches_per_result, j, matches)
break
end
end
end
Expand All @@ -66,6 +61,7 @@ local function remove_highlights(elem)
remove_highlights(elem.elements[i])
end
end
elem.changed = true
end

local old_layouts = {}
Expand All @@ -76,8 +72,17 @@ local old_parents = {}
---also restores the original layout if the pattern is a empty string
---@param pattern string
---@param elements table
---@param flex_container flex
---@param flex_container flex?
function search.create_result_layout(pattern, elements, flex_container)
if #elements == 0 then
return
end
if flex_container == nil then
flex_container = elements[1].parent
if old_layouts[flex_container] then
elements = old_layouts[flex_container]
end
end
if old_layouts[flex_container] then
-- remove old highlights
remove_highlights(flex_container)
Expand Down

0 comments on commit 7be8998

Please sign in to comment.