Skip to content

Commit

Permalink
GD-508: Revisit the EditorFileSystemContextMenuHandler added to the…
Browse files Browse the repository at this point in the history
… root node (#509)

# Why
see #508

# What
- simplify `EditorFileSystemContextMenuHandler`
- do preload and add as child to the GdUnit inspector
  • Loading branch information
MikeSchulze authored Jun 17, 2024
1 parent 627e85b commit d5079c6
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 61 deletions.
8 changes: 4 additions & 4 deletions addons/gdUnit4/src/network/GdUnitServer.gd
Original file line number Diff line number Diff line change
Expand Up @@ -17,20 +17,20 @@ func _ready() -> void:
GdUnitCommandHandler.instance().gdunit_runner_stop.connect(_on_gdunit_runner_stop)


func _on_client_connected(client_id :int) -> void:
func _on_client_connected(client_id: int) -> void:
GdUnitSignals.instance().gdunit_client_connected.emit(client_id)


func _on_client_disconnected(client_id :int) -> void:
func _on_client_disconnected(client_id: int) -> void:
GdUnitSignals.instance().gdunit_client_disconnected.emit(client_id)


func _on_gdunit_runner_stop(client_id :int) -> void:
func _on_gdunit_runner_stop(client_id: int) -> void:
if _server:
_server.disconnect_client(client_id)


func _receive_rpc_data(p_rpc :RPC) -> void:
func _receive_rpc_data(p_rpc: Variant) -> void:
if p_rpc is RPCMessage:
GdUnitSignals.instance().gdunit_message.emit(p_rpc.message())
return
Expand Down
25 changes: 0 additions & 25 deletions addons/gdUnit4/src/ui/EditorFileSystemControls.gd

This file was deleted.

10 changes: 3 additions & 7 deletions addons/gdUnit4/src/ui/GdUnitInspector.gd
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ class_name GdUnitInspecor
extends Panel

const ScriptEditorContextMenuHandler = preload("res://addons/gdUnit4/src/ui/menu/ScriptEditorContextMenuHandler.gd")
const EditorFileSystemContextMenuHandler = preload("res://addons/gdUnit4/src/ui/menu/EditorFileSystemContextMenuHandler.gd")

var _command_handler := GdUnitCommandHandler.instance()

Expand All @@ -21,11 +22,6 @@ func _ready() -> void:
add_file_system_dock_context_menu()


func _notification(what: int) -> void:
if what == NOTIFICATION_PREDELETE:
EditorFileSystemControls.unregister_context_menu()


func _process(_delta: float) -> void:
_command_handler._do_process()

Expand All @@ -52,13 +48,13 @@ func _getEditorThemes() -> void:
func add_file_system_dock_context_menu() -> void:
var is_test_suite := func is_visible(script: Script, is_ts: bool) -> bool:
if script == null:
return true
return false
return GdObjects.is_test_suite(script) == is_ts
var menu :Array[GdUnitContextMenuItem] = [
GdUnitContextMenuItem.new(GdUnitContextMenuItem.MENU_ID.TEST_RUN, "Run Testsuites", "Play", is_test_suite.bind(true), _command_handler.command(GdUnitCommandHandler.CMD_RUN_TESTSUITE)),
GdUnitContextMenuItem.new(GdUnitContextMenuItem.MENU_ID.TEST_DEBUG, "Debug Testsuites", "PlayStart", is_test_suite.bind(true), _command_handler.command(GdUnitCommandHandler.CMD_RUN_TESTSUITE_DEBUG)),
]
EditorFileSystemControls.register_context_menu(menu)
add_child(EditorFileSystemContextMenuHandler.new(menu))


func add_script_editor_context_menu() -> void:
Expand Down
33 changes: 8 additions & 25 deletions addons/gdUnit4/src/ui/menu/EditorFileSystemContextMenuHandler.gd
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
class_name EditorFileSystemContextMenuHandler
@tool
extends Control

var _context_menus := Dictionary()
Expand All @@ -8,25 +8,12 @@ func _init(context_menus: Array[GdUnitContextMenuItem]) -> void:
set_name("EditorFileSystemContextMenuHandler")
for menu in context_menus:
_context_menus[menu.id] = menu
var popup := EditorFileSystemContextMenuHandler._menu_popup()
var file_tree := EditorFileSystemContextMenuHandler._file_tree()
var popup := _menu_popup()
var file_tree := _file_tree()
popup.about_to_popup.connect(on_context_menu_show.bind(popup, file_tree))
popup.id_pressed.connect(on_context_menu_pressed.bind(file_tree))


static func dispose() -> void:
if Engine.get_main_loop().root == null:
return
var handler: EditorFileSystemContextMenuHandler = Engine.get_main_loop().root.find_child("EditorFileSystemContextMenuHandler*", false, false)
if handler:
var popup := _menu_popup()
if popup.about_to_popup.is_connected(Callable(handler, "on_context_menu_show")):
popup.about_to_popup.disconnect(Callable(handler, "on_context_menu_show"))
if popup.id_pressed.is_connected(Callable(handler, "on_context_menu_pressed")):
popup.id_pressed.disconnect(Callable(handler, "on_context_menu_pressed"))
GodotVersionFixures.free_fix(handler)


func on_context_menu_show(context_menu: PopupMenu, file_tree: Tree) -> void:
context_menu.add_separator()
var current_index := context_menu.get_item_count()
Expand Down Expand Up @@ -55,6 +42,7 @@ func collect_testsuites(_menu_item: GdUnitContextMenuItem, file_tree: Tree) -> P
var file_system := EditorInterface.get_resource_filesystem()
var selected_item := file_tree.get_selected()
var selected_test_suites := PackedStringArray()

while selected_item:
var resource_path: String = selected_item.get_metadata(0)
var file_type := file_system.get_file_type(resource_path)
Expand All @@ -71,14 +59,9 @@ func collect_testsuites(_menu_item: GdUnitContextMenuItem, file_tree: Tree) -> P
return selected_test_suites


# Returns the FileSystemDock instance
static func filesystem_dock() -> FileSystemDock:
return EditorInterface.get_file_system_dock()


static func _file_tree() -> Tree:
return GdObjects.find_nodes_by_class(filesystem_dock(), "Tree", true)[-1]
func _file_tree() -> Tree:
return GdObjects.find_nodes_by_class(EditorInterface.get_file_system_dock(), "Tree", true)[-1]


static func _menu_popup() -> PopupMenu:
return GdObjects.find_nodes_by_class(filesystem_dock(), "PopupMenu")[-1]
func _menu_popup() -> PopupMenu:
return GdObjects.find_nodes_by_class(EditorInterface.get_file_system_dock(), "PopupMenu")[-1]
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
@tool
extends Control

var _context_menus := Dictionary()
Expand Down

0 comments on commit d5079c6

Please sign in to comment.