Skip to content

Commit b4aa73d

Browse files
Add exit confirmation dialog on ESC in main menu (#16164)
1 parent aff1abd commit b4aa73d

File tree

4 files changed

+69
-1
lines changed

4 files changed

+69
-1
lines changed
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
-- Luanti
2+
-- Copyright (C) 2025 siliconsniffer
3+
-- SPDX-License-Identifier: LGPL-2.1-or-later
4+
5+
6+
local function exit_dialog_formspec()
7+
local show_dialog = core.settings:get_bool("enable_esc_dialog", true)
8+
local formspec = {
9+
"formspec_version[10]" ..
10+
"size[10,3.6]" ..
11+
"style_type[label;font=bold]" ..
12+
"style[btn_quit_confirm_yes;bgcolor=red]" ..
13+
"label[0.5,0.5;" .. fgettext("Are you sure you want to quit?") .. "]" ..
14+
"checkbox[0.5,1.4;cb_show_dialog;" .. fgettext("Always show this dialog.") .. ";" .. tostring(show_dialog) .. "]" ..
15+
"button[0.5,2.3;3,0.8;btn_quit_confirm_cancel;" .. fgettext("Cancel") .. "]" ..
16+
"button[6.5,2.3;3,0.8;btn_quit_confirm_yes;" .. fgettext("Quit") .. "]" ..
17+
"set_focus[btn_quit_confirm_yes]"
18+
}
19+
return table.concat(formspec, "")
20+
end
21+
22+
23+
local function exit_dialog_buttonhandler(this, fields)
24+
if fields.cb_show_dialog ~= nil then
25+
core.settings:set_bool("enable_esc_dialog", core.is_yes(fields.cb_show_dialog))
26+
return false
27+
elseif fields.btn_quit_confirm_yes then
28+
this:delete()
29+
core.close()
30+
return true
31+
elseif fields.btn_quit_confirm_cancel then
32+
this:delete()
33+
this:show()
34+
return true
35+
end
36+
end
37+
38+
39+
local function event_handler(event)
40+
if event == "DialogShow" then
41+
mm_game_theme.set_engine(true) -- hide the menu header
42+
return true
43+
end
44+
return false
45+
end
46+
47+
48+
function create_exit_dialog()
49+
local retval = dialog_create("dlg_exit",
50+
exit_dialog_formspec,
51+
exit_dialog_buttonhandler,
52+
event_handler)
53+
return retval
54+
end

builtin/mainmenu/init.lua

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ dofile(menupath .. DIR_DELIM .. "content" .. DIR_DELIM .. "init.lua")
2828

2929
dofile(menupath .. DIR_DELIM .. "dlg_config_world.lua")
3030
dofile(basepath .. "common" .. DIR_DELIM .. "settings" .. DIR_DELIM .. "init.lua")
31+
dofile(menupath .. DIR_DELIM .. "dlg_confirm_exit.lua")
3132
dofile(menupath .. DIR_DELIM .. "dlg_create_world.lua")
3233
dofile(menupath .. DIR_DELIM .. "dlg_delete_content.lua")
3334
dofile(menupath .. DIR_DELIM .. "dlg_delete_world.lua")
@@ -49,7 +50,16 @@ local tabs = {
4950
--------------------------------------------------------------------------------
5051
local function main_event_handler(tabview, event)
5152
if event == "MenuQuit" then
52-
core.close()
53+
local show_dialog = core.settings:get_bool("enable_esc_dialog")
54+
if not ui.childlist["mainmenu_quit_confirm"] and show_dialog then
55+
tabview:hide()
56+
local dlg = create_exit_dialog()
57+
dlg:set_parent(tabview)
58+
dlg:show()
59+
else
60+
core.close()
61+
end
62+
return true
5363
end
5464
return true
5565
end

builtin/settingtypes.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,9 @@ autojump (Automatic jumping) bool false
162162
# On touchscreens, this only affects digging.
163163
safe_dig_and_place (Safe digging and placing) bool false
164164

165+
# Enable a confirmation dialog before closing.
166+
enable_esc_dialog (Confirmation dialog before closing) bool true
167+
165168
[*Keyboard and Mouse]
166169

167170
# Invert vertical mouse movement.

src/defaultsettings.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -370,6 +370,7 @@ void set_default_settings()
370370
settings->setDefault("toggle_sneak_key", "false");
371371
settings->setDefault("toggle_aux1_key", "false");
372372
settings->setDefault("autojump", bool_to_cstr(has_touch));
373+
settings->setDefault("enable_esc_dialog", "true");
373374
settings->setDefault("continuous_forward", "false");
374375
settings->setDefault("enable_joysticks", "false");
375376
settings->setDefault("joystick_id", "0");

0 commit comments

Comments
 (0)