Skip to content

Commit 93562cd

Browse files
committed
Implement shutdown/reboot buttons
1 parent 29d8b3d commit 93562cd

File tree

2 files changed

+118
-15
lines changed

2 files changed

+118
-15
lines changed

scenes/config/QuitSettings.gd

+46
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,55 @@ extends Control
44
@onready var n_shutdown := %Shutdown
55
@onready var n_restart := %Restart
66

7+
@onready var n_shutdown_confirm : ConfirmationDialog = n_shutdown.get_node("Confirmation")
8+
@onready var n_restart_confirm : ConfirmationDialog = n_restart.get_node("Confirmation")
9+
10+
@onready var n_extra_system_nodes := [
11+
%Separator, %SystemButtons
12+
]
13+
14+
func _ready() -> void:
15+
# macOS does not allow apps to issue arbitrary shutdown/reboot
16+
# commands; it requires root privileges, and the programmatic
17+
# method would require native code (https://developer.apple.com/library/archive/qa/qa1134/_index.html)
18+
# So, just disable it.
19+
if FileUtils.get_os_id() == FileUtils.OS_ID.MACOS:
20+
for node: Node in n_extra_system_nodes:
21+
node.queue_free()
22+
723
func grab_focus():
824
n_quit.grab_focus()
925

1026

1127
func _on_Quit_pressed():
1228
RetroHub.quit()
29+
30+
31+
func _on_shutdown_pressed() -> void:
32+
n_shutdown_confirm.popup_centered()
33+
34+
35+
func _on_restart_pressed() -> void:
36+
n_restart_confirm.popup_centered()
37+
38+
39+
func _on_shutdown_confirmed() -> void:
40+
match FileUtils.get_os_id():
41+
FileUtils.OS_ID.WINDOWS:
42+
OS.execute("shutdown", ["/s", "/t", "0"])
43+
FileUtils.OS_ID.LINUX:
44+
OS.execute("shutdown", ["-P", "now"])
45+
_:
46+
push_error("Unimplemented shutdown functionality!")
47+
_on_Quit_pressed()
48+
49+
50+
func _on_restart_confirmed() -> void:
51+
match FileUtils.get_os_id():
52+
FileUtils.OS_ID.WINDOWS:
53+
OS.execute("shutdown", ["/r", "/t", "0"])
54+
FileUtils.OS_ID.LINUX:
55+
OS.execute("shutdown", ["-r", "now"])
56+
_:
57+
push_error("Unimplemented shutdown functionality!")
58+
_on_Quit_pressed()

scenes/config/QuitSettings.tscn

+72-15
Original file line numberDiff line numberDiff line change
@@ -14,63 +14,120 @@ focus_mode = 2
1414
script = ExtResource("1")
1515

1616
[node name="VBoxContainer" type="VBoxContainer" parent="."]
17-
layout_mode = 0
17+
layout_mode = 1
18+
anchors_preset = 14
1819
anchor_top = 0.5
1920
anchor_right = 1.0
2021
anchor_bottom = 0.5
21-
offset_top = -47.0
22-
offset_bottom = 47.0
22+
offset_top = -15.5
23+
offset_bottom = 15.5
24+
grow_horizontal = 2
25+
grow_vertical = 2
2326
size_flags_vertical = 4
2427
theme_override_constants/separation = 20
2528

2629
[node name="Quit" type="Button" parent="VBoxContainer"]
2730
unique_name_in_owner = true
2831
layout_mode = 2
2932
size_flags_horizontal = 4
30-
focus_neighbor_top = NodePath("../HBoxContainer2/Shutdown")
33+
focus_neighbor_top = NodePath("../SystemButtons/Shutdown")
3134
text = "Quit Retrohub"
3235

3336
[node name="AccessibilityFocus" type="Node" parent="VBoxContainer/Quit"]
3437
script = ExtResource("2")
35-
previous = NodePath("../../HBoxContainer2/Restart")
38+
previous = NodePath("../../SystemButtons/Restart")
3639

37-
[node name="HBoxContainer" type="HBoxContainer" parent="VBoxContainer"]
40+
[node name="Separator" type="HBoxContainer" parent="VBoxContainer"]
41+
unique_name_in_owner = true
3842
layout_mode = 2
3943

40-
[node name="HSeparator" type="HSeparator" parent="VBoxContainer/HBoxContainer"]
44+
[node name="HSeparator" type="HSeparator" parent="VBoxContainer/Separator"]
4145
layout_mode = 2
4246
size_flags_horizontal = 3
4347

44-
[node name="Label" type="Label" parent="VBoxContainer/HBoxContainer"]
48+
[node name="Label" type="Label" parent="VBoxContainer/Separator"]
4549
layout_mode = 2
4650
text = "or"
4751

48-
[node name="HSeparator2" type="HSeparator" parent="VBoxContainer/HBoxContainer"]
52+
[node name="HSeparator2" type="HSeparator" parent="VBoxContainer/Separator"]
4953
layout_mode = 2
5054
size_flags_horizontal = 3
5155

52-
[node name="HBoxContainer2" type="HBoxContainer" parent="VBoxContainer"]
56+
[node name="SystemButtons" type="HBoxContainer" parent="VBoxContainer"]
57+
unique_name_in_owner = true
5358
layout_mode = 2
5459
theme_override_constants/separation = 20
5560

56-
[node name="Shutdown" type="Button" parent="VBoxContainer/HBoxContainer2"]
61+
[node name="Shutdown" type="Button" parent="VBoxContainer/SystemButtons"]
5762
unique_name_in_owner = true
5863
layout_mode = 2
5964
size_flags_horizontal = 10
6065
focus_neighbor_bottom = NodePath("../../Quit")
61-
disabled = true
6266
text = "Shutdown system"
6367

64-
[node name="Restart" type="Button" parent="VBoxContainer/HBoxContainer2"]
68+
[node name="Confirmation" type="ConfirmationDialog" parent="VBoxContainer/SystemButtons/Shutdown"]
69+
title = "Shutdown"
70+
initial_position = 1
71+
size = Vector2i(350, 200)
72+
ok_button_text = "Shutdown system"
73+
74+
[node name="Label" type="Label" parent="VBoxContainer/SystemButtons/Shutdown/Confirmation"]
75+
anchors_preset = 15
76+
anchor_right = 1.0
77+
anchor_bottom = 1.0
78+
offset_left = 8.0
79+
offset_top = 8.0
80+
offset_right = -8.0
81+
offset_bottom = -49.0
82+
grow_horizontal = 2
83+
grow_vertical = 2
84+
text = "Do you really wish to shutdown your system?"
85+
horizontal_alignment = 1
86+
vertical_alignment = 1
87+
autowrap_mode = 2
88+
text_overrun_behavior = 2
89+
90+
[node name="AccessibilityFocus" type="Node" parent="VBoxContainer/SystemButtons/Shutdown/Confirmation/Label"]
91+
script = ExtResource("2")
92+
93+
[node name="Restart" type="Button" parent="VBoxContainer/SystemButtons"]
6594
unique_name_in_owner = true
6695
layout_mode = 2
6796
size_flags_horizontal = 2
6897
focus_neighbor_bottom = NodePath("../../Quit")
69-
disabled = true
7098
text = "Restart system"
7199

72-
[node name="AccessibilityFocus" type="Node" parent="VBoxContainer/HBoxContainer2/Restart"]
100+
[node name="AccessibilityFocus" type="Node" parent="VBoxContainer/SystemButtons/Restart"]
73101
script = ExtResource("2")
74102
next = NodePath("../../../Quit")
75103

104+
[node name="Confirmation" type="ConfirmationDialog" parent="VBoxContainer/SystemButtons/Restart"]
105+
title = "Reboot"
106+
initial_position = 1
107+
size = Vector2i(350, 200)
108+
ok_button_text = "Reboot system"
109+
110+
[node name="Label" type="Label" parent="VBoxContainer/SystemButtons/Restart/Confirmation"]
111+
anchors_preset = 15
112+
anchor_right = 1.0
113+
anchor_bottom = 1.0
114+
offset_left = 8.0
115+
offset_top = 8.0
116+
offset_right = -8.0
117+
offset_bottom = -49.0
118+
grow_horizontal = 2
119+
grow_vertical = 2
120+
text = "Do you really wish to reboot your system?"
121+
horizontal_alignment = 1
122+
vertical_alignment = 1
123+
autowrap_mode = 2
124+
text_overrun_behavior = 2
125+
126+
[node name="AccessibilityFocus" type="Node" parent="VBoxContainer/SystemButtons/Restart/Confirmation/Label"]
127+
script = ExtResource("2")
128+
76129
[connection signal="pressed" from="VBoxContainer/Quit" to="." method="_on_Quit_pressed"]
130+
[connection signal="pressed" from="VBoxContainer/SystemButtons/Shutdown" to="." method="_on_shutdown_pressed"]
131+
[connection signal="confirmed" from="VBoxContainer/SystemButtons/Shutdown/Confirmation" to="." method="_on_shutdown_confirmed"]
132+
[connection signal="pressed" from="VBoxContainer/SystemButtons/Restart" to="." method="_on_restart_pressed"]
133+
[connection signal="confirmed" from="VBoxContainer/SystemButtons/Restart/Confirmation" to="." method="_on_restart_confirmed"]

0 commit comments

Comments
 (0)