Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tweak main menu server list behavior #15736

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 23 additions & 5 deletions builtin/mainmenu/tab_online.lua
Original file line number Diff line number Diff line change
Expand Up @@ -391,12 +391,16 @@ local function matches_query(server, query)
return name_matches and 50 or description_matches and 0
end

local pre_search_selection = nil
y5nw marked this conversation as resolved.
Show resolved Hide resolved
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

might be cleaner to move this into tabdata

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you elaborate on that?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

well it is only relevant to this tab, and global variables are best avoided.
or is there something that means it needs to persist between switching tabs?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, totally makes sense. I am just a bit confused on where/how to put it.


local function search_server_list(input)
menudata.search_result = nil
if #serverlistmgr.servers < 2 then
return
end

pre_search_selection = pre_search_selection or find_selected_server()

-- setup the search query
local query = parse_search_input(input)
if not query then
Expand All @@ -419,11 +423,23 @@ local function search_server_list(input)
return
end

local current_server = find_selected_server()

table.sort(search_result, function(a, b)
return a.points > b.points
end)
menudata.search_result = search_result

-- Keep current selection if it's in search results
if current_server then
for _, server in ipairs(search_result) do
if server.address == current_server.address and
server.port == current_server.port then
return
end
end
end

-- Find first compatible server (favorite or public)
for _, server in ipairs(search_result) do
if is_server_protocol_compat(server.proto_min, server.proto_max) then
Expand Down Expand Up @@ -471,6 +487,7 @@ local function main_button_handler(tabview, fields, name, tabdata)
end
if event.type == "CHG" then
set_selected_server(server)
pre_search_selection = nil
return true
end
end
Expand All @@ -484,11 +501,9 @@ local function main_button_handler(tabview, fields, name, tabdata)
if fields.btn_delete_favorite then
local idx = core.get_table_index("servers")
if not idx then return end
local server = tabdata.lookup[idx]
if not server then return end

serverlistmgr.delete_favorite(server)
set_selected_server(server)
serverlistmgr.delete_favorite(tabdata.lookup[idx])
set_selected_server(tabdata.lookup[idx+1])
return true
end

Expand Down Expand Up @@ -516,7 +531,10 @@ local function main_button_handler(tabview, fields, name, tabdata)
if fields.btn_mp_clear then
tabdata.search_for = ""
menudata.search_result = nil
set_selected_server(nil)
if pre_search_selection then
set_selected_server(pre_search_selection)
pre_search_selection = nil
end
return true
end

Expand Down
Loading