Godot-System-Tray.gd
· 1.8 KiB · GDScript3
Bruto
extends Node
var tray_icon: StatusIndicator
var tray_menu: PopupMenu
func _ready() -> void:
setup_tray()
func setup_tray() -> void:
# 1. Initialize the StatusIndicator
tray_icon = StatusIndicator.new()
tray_icon.icon = load("res://ship-icon-256.png") # Path to your 16x16 or 32x32 icon
tray_icon.tooltip = "My Godot App"
add_child(tray_icon)
# 2. Bind the click event to restore the window
tray_icon.pressed.connect(_on_tray_icon_pressed)
# 3. Optional: Create a right-click context menu
tray_menu = PopupMenu.new()
tray_menu.add_item("Show Window", 0)
tray_menu.add_item("Quit", 1)
tray_menu.id_pressed.connect(_on_menu_item_pressed)
add_child(tray_menu)
# Assign menu to the tray icon
tray_icon.menu = tray_menu.get_path()
# Handle window close interception
func _notification(what: int) -> void:
if what == NOTIFICATION_WM_CLOSE_REQUEST:
minimize_to_tray()
# Hides the application entirely from desktop view
func minimize_to_tray() -> void:
# Hide window from taskbar and ignore standard user inputs
DisplayServer.window_set_flag(DisplayServer.WINDOW_FLAG_NO_FOCUS, true)
# Hide the actual window frame
DisplayServer.window_set_mode(DisplayServer.WINDOW_MODE_MINIMIZED)
# Restores the application window back to normal
func restore_from_tray() -> void:
DisplayServer.window_set_flag(DisplayServer.WINDOW_FLAG_NO_FOCUS, false)
DisplayServer.window_set_mode(DisplayServer.WINDOW_MODE_WINDOWED)
# Handle clicking directly on the tray icon
func _on_tray_icon_pressed(mouse_button: int, _device_id: int) -> void:
if mouse_button == MOUSE_BUTTON_LEFT:
restore_from_tray()
# Handle context menu actions
func _on_menu_item_pressed(id: int) -> void:
match id:
0: restore_from_tray()
1: get_tree().quit() # Ensure clean termination when explicitly chosen
| 1 | extends Node |
| 2 | |
| 3 | var tray_icon: StatusIndicator |
| 4 | var tray_menu: PopupMenu |
| 5 | |
| 6 | func _ready() -> void: |
| 7 | setup_tray() |
| 8 | |
| 9 | func setup_tray() -> void: |
| 10 | # 1. Initialize the StatusIndicator |
| 11 | tray_icon = StatusIndicator.new() |
| 12 | tray_icon.icon = load("res://ship-icon-256.png") # 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 |
| 30 | func _notification(what: int) -> void: |
| 31 | if what == NOTIFICATION_WM_CLOSE_REQUEST: |
| 32 | minimize_to_tray() |
| 33 | |
| 34 | # Hides the application entirely from desktop view |
| 35 | func 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 |
| 42 | func 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 |
| 47 | func _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 |
| 52 | func _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 |