Mainmenu: Unify gamedata.errormessage and messagebox formspecs (#14716)

This commit is contained in:
grorp 2024-06-01 17:03:06 +02:00 committed by GitHub
parent 140b9e5a5a
commit 981d67324b
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 22 additions and 16 deletions

@ -78,15 +78,12 @@ function dialog_create(name,get_formspec,buttonhandler,eventhandler)
return self return self
end end
-- "message" must already be formspec-escaped, e.g. via fgettext or
-- core.formspec_escape.
function messagebox(name, message) function messagebox(name, message)
return dialog_create(name, return dialog_create(name,
function() function()
return ([[ return ui.get_message_formspec("", message, "ok")
formspec_version[3]
size[8,3]
textarea[0.375,0.375;7.25,1.2;;;%s]
button[3,1.825;2,0.8;ok;%s]
]]):format(message, fgettext("OK"))
end, end,
function(this, fields) function(this, fields)
if fields.ok then if fields.ok then

@ -50,6 +50,20 @@ function ui.find_by_name(name)
return ui.childlist[name] return ui.childlist[name]
end end
--------------------------------------------------------------------------------
-- "title" and "message" must already be formspec-escaped, e.g. via fgettext or
-- core.formspec_escape.
function ui.get_message_formspec(title, message, btn_id)
return table.concat({
"size[14,8]",
"real_coordinates[true]",
"set_focus[", btn_id, ";true]",
"box[0.5,1.2;13,5;#000]",
("textarea[0.5,1.2;13,5;;%s;%s]"):format(title, message),
"button[5,6.6;4,1;", btn_id, ";" .. fgettext("OK") .. "]",
})
end
-------------------------------------------------------------------------------- --------------------------------------------------------------------------------
-------------------------------------------------------------------------------- --------------------------------------------------------------------------------
-- Internal functions not to be called from user -- Internal functions not to be called from user
@ -76,6 +90,9 @@ function ui.update()
} }
ui.overridden = true ui.overridden = true
elseif gamedata ~= nil and gamedata.errormessage ~= nil then elseif gamedata ~= nil and gamedata.errormessage ~= nil then
-- Note to API users:
-- "gamedata.errormessage" must not be formspec-escaped yet.
-- For translations, fgettext_ne should be used.
local error_message = core.formspec_escape(gamedata.errormessage) local error_message = core.formspec_escape(gamedata.errormessage)
local error_title local error_title
@ -84,15 +101,7 @@ function ui.update()
else else
error_title = fgettext("An error occurred:") error_title = fgettext("An error occurred:")
end end
formspec = { formspec = {ui.get_message_formspec(error_title, error_message, "btn_error_confirm")}
"size[14,8]",
"real_coordinates[true]",
"set_focus[btn_error_confirm;true]",
"box[0.5,1.2;13,5;#000]",
("textarea[0.5,1.2;13,5;;%s;%s]"):format(
error_title, error_message),
"button[5,6.6;4,1;btn_error_confirm;" .. fgettext("OK") .. "]"
}
ui.overridden = true ui.overridden = true
else else
local active_toplevel_ui_elements = 0 local active_toplevel_ui_elements = 0

@ -117,7 +117,7 @@ local function resolve_auto_install_spec()
end end
if not resolved then if not resolved then
gamedata.errormessage = fgettext("The package $1 was not found.", auto_install_spec) gamedata.errormessage = fgettext_ne("The package $1 was not found.", auto_install_spec)
ui.update() ui.update()
auto_install_spec = nil auto_install_spec = nil