Utoljára aktív 1789267469

Systray app behavior: Go to Project > Project Settings > Engine > Config > Uncheck Auto Accept Quit

App

Revízió 75a776f2d6ed678a470d355de0403d189d563380

Godot-System-Tray.gd Eredeti
1extends Node
2
3var tray_icon: StatusIndicator
4var tray_menu: PopupMenu
5
6func _ready() -> void:
7 setup_tray()
8
9func setup_tray() -> void:
10 # 1. Initialize the StatusIndicator
11 tray_icon = StatusIndicator.new()
12 tray_icon.icon = load("res://icon.svg") # Path to your 16x16 or 32x32 icon
13 tray_icon.tooltip = "My Godot App"
14 add_child(tray_icon)
15
16 # 2. Bind the click event to restore the window
17 tray_icon.pressed.connect(_on_tray_icon_pressed)
18
19 # 3. Optional: Create a right-click context menu
20 tray_menu = PopupMenu.new()
21 tray_menu.add_item("Show Window", 0)
22 tray_menu.add_item("Quit", 1)
23 tray_menu.id_pressed.connect(_on_menu_item_pressed)
24 add_child(tray_menu)
25
26 # Assign menu to the tray icon
27 tray_icon.menu = tray_menu.get_path()
28
29# Handle window close interception
30func _notification(what: int) -> void:
31 if what == NOTIFICATION_WM_CLOSE_REQUEST:
32 minimize_to_tray()
33
34# Hides the application entirely from desktop view
35func minimize_to_tray() -> void:
36 # Hide window from taskbar and ignore standard user inputs
37 DisplayServer.window_set_flag(DisplayServer.WINDOW_FLAG_NO_FOCUS, true)
38 # Hide the actual window frame
39 DisplayServer.window_set_mode(DisplayServer.WINDOW_MODE_MINIMIZED)
40
41# Restores the application window back to normal
42func restore_from_tray() -> void:
43 DisplayServer.window_set_flag(DisplayServer.WINDOW_FLAG_NO_FOCUS, false)
44 DisplayServer.window_set_mode(DisplayServer.WINDOW_MODE_WINDOWED)
45
46# Handle clicking directly on the tray icon
47func _on_tray_icon_pressed(mouse_button: int, _device_id: int) -> void:
48 if mouse_button == MOUSE_BUTTON_LEFT:
49 restore_from_tray()
50
51# Handle context menu actions
52func _on_menu_item_pressed(id: int) -> void:
53 match id:
54 0: restore_from_tray()
55 1: get_tree().quit() # Ensure clean termination when explicitly chosen
56