Main menu: Add server favorite button (#15486)

This adopts PR 13446.
This commit is contained in:
siliconsniffer 2024-12-08 20:28:37 +01:00 committed by GitHub
parent 480eb7d816
commit eb6731bdc6
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

@ -55,7 +55,20 @@ local function get_sorted_servers()
return servers return servers
end end
local function is_selected_fav(server)
local address = core.settings:get("address")
local port = tonumber(core.settings:get("remote_port"))
for _, fav in ipairs(serverlistmgr.get_favorites()) do
if address == fav.address and port == fav.port then
return true
end
end
return false
end
-- Persists the selected server in the "address" and "remote_port" settings -- Persists the selected server in the "address" and "remote_port" settings
local function set_selected_server(server) local function set_selected_server(server)
if server == nil then -- reset selection if server == nil then -- reset selection
core.settings:remove("address") core.settings:remove("address")
@ -66,14 +79,6 @@ local function set_selected_server(server)
local port = server.port local port = server.port
gamedata.serverdescription = server.description gamedata.serverdescription = server.description
gamedata.fav = false
for _, fav in ipairs(serverlistmgr.get_favorites()) do
if address == fav.address and port == fav.port then
gamedata.fav = true
break
end
end
if address and port then if address and port then
core.settings:set("address", address) core.settings:set("address", address)
core.settings:set("remote_port", port) core.settings:set("remote_port", port)
@ -165,15 +170,20 @@ local function get_formspec(tabview, name, tabdata)
fgettext("Clients:\n$1", table.concat(clients_list, "\n")) .. "]" fgettext("Clients:\n$1", table.concat(clients_list, "\n")) .. "]"
end end
retval = retval .. "style[btn_view_clients;padding=6]" retval = retval .. "style[btn_view_clients;padding=6]"
retval = retval .. "image_button[5,1.3;0.5,0.5;" .. core.formspec_escape(defaulttexturedir .. retval = retval .. "image_button[4.5,1.3;0.5,0.5;" .. core.formspec_escape(defaulttexturedir ..
"server_view_clients.png") .. ";btn_view_clients;]" "server_view_clients.png") .. ";btn_view_clients;]"
end end
if gamedata.fav then if is_selected_fav() then
retval = retval .. "tooltip[btn_delete_favorite;" .. fgettext("Remove favorite") .. "]" retval = retval .. "tooltip[btn_delete_favorite;" .. fgettext("Remove favorite") .. "]"
retval = retval .. "style[btn_delete_favorite;padding=6]" retval = retval .. "style[btn_delete_favorite;padding=6]"
retval = retval .. "image_button[" .. (can_view_clients_list and "4.5" or "5") .. ",1.3;0.5,0.5;" .. retval = retval .. "image_button[5,1.3;0.5,0.5;" ..
core.formspec_escape(defaulttexturedir .. "server_favorite_delete.png") .. ";btn_delete_favorite;]" core.formspec_escape(defaulttexturedir .. "server_favorite_delete.png") .. ";btn_delete_favorite;]"
else
retval = retval .. "tooltip[btn_add_favorite;" .. fgettext("Add favorite") .. "]"
retval = retval .. "style[btn_add_favorite;padding=6]"
retval = retval .. "image_button[5,1.3;0.5,0.5;" ..
core.formspec_escape(defaulttexturedir .. "server_favorite.png") .. ";btn_add_favorite;]"
end end
end end
@ -336,6 +346,11 @@ local function main_button_handler(tabview, fields, name, tabdata)
end end
end end
if fields.btn_add_favorite then
serverlistmgr.add_favorite(find_selected_server())
return true
end
if fields.btn_delete_favorite then if fields.btn_delete_favorite then
local idx = core.get_table_index("servers") local idx = core.get_table_index("servers")
if not idx then return end if not idx then return end