Skip to content

Commit

Permalink
GD-353: Redesign GdUnit inspector context menu (#364)
Browse files Browse the repository at this point in the history
# Why
There were several problems with the context menu, which needs to be
redesigned
- The context menu was not placed correctly when using multiple screens.
- the highlighting of context menu items did not work
- the implementation was based on a lack of knowledge

# What
Complete redesign of the context menu
- respect the screen position
- uses `PopupMenu` instead of `PopupPanel `
- add icons to the context menu

![image](https://github.com/MikeSchulze/gdUnit4/assets/347037/23995dbc-49d2-4ab5-9dc3-99d82bee9963)
  • Loading branch information
Nullpointer committed Mar 9, 2024
1 parent 55e9cfa commit 600fccd
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 46 deletions.
31 changes: 21 additions & 10 deletions addons/gdUnit4/src/ui/parts/InspectorTreeMainPanel.gd
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@ extends VSplitContainer
signal run_testcase(test_suite_resource_path, test_case, test_param_index, run_debug)
signal run_testsuite

const CONTEXT_MENU_RUN_ID = 0
const CONTEXT_MENU_DEBUG_ID = 1

@onready var _tree :Tree = $Panel/Tree
@onready var _report_list :Node = $report/ScrollContainer/list
@onready var _report_template :RichTextLabel = $report/report_template
@onready var _context_menu :PopupMenu = $contextMenu

@onready var _context_menu :PopupPanel = $contextMenu
@onready var _context_menu_run := $contextMenu/items/run
@onready var _context_menu_debug := $contextMenu/items/debug

# tree icons
@onready var ICON_SPINNER = load("res://addons/gdUnit4/src/ui/assets/spinner.tres")
Expand All @@ -22,6 +22,7 @@ signal run_testsuite
@onready var ICON_TEST_SUCCESS_ORPHAN = load("res://addons/gdUnit4/src/ui/assets/TestCase_success_orphan.tres")
@onready var ICON_TEST_FAILED_ORPHAN = load("res://addons/gdUnit4/src/ui/assets/TestCase_failed_orphan.tres")
@onready var ICON_TEST_ERRORS_ORPHAN = load("res://addons/gdUnit4/src/ui/assets/TestCase_error_orphan.tres")
@onready var debug_icon_image :Texture2D = load("res://addons/gdUnit4/src/ui/assets/PlayDebug.svg")

enum GdUnitType {
TEST_SUITE,
Expand Down Expand Up @@ -128,6 +129,9 @@ func is_test_suite(item :TreeItem) -> bool:
func _ready():
if Engine.is_editor_hint():
_editor = Engine.get_meta("GdUnitEditorPlugin")
var editior_control := _editor.get_editor_interface().get_base_control()
_context_menu.set_item_icon(CONTEXT_MENU_RUN_ID, GodotVersionFixures.get_icon(editior_control, "Play"))
_context_menu.set_item_icon(CONTEXT_MENU_DEBUG_ID, debug_icon_image)
init_tree()
GdUnitSignals.instance().gdunit_add_test_suite.connect(_on_gdunit_add_test_suite)
GdUnitSignals.instance().gdunit_event.connect(_on_gdunit_event)
Expand All @@ -136,7 +140,6 @@ func _ready():
command_handler.gdunit_runner_stop.connect(_on_gdunit_runner_stop)



# we need current to manually redraw bacause of the animation bug
# https://github.com/godotengine/godot/issues/69330
func _process(_delta):
Expand Down Expand Up @@ -477,7 +480,7 @@ func add_test_cases(parent :TreeItem, test_case_names :Array) -> void:
################################################################################
func _on_tree_item_mouse_selected(mouse_position :Vector2, mouse_button_index :int):
if mouse_button_index == MOUSE_BUTTON_RIGHT:
_context_menu.position = mouse_position + _tree.get_global_position()
_context_menu.position = get_screen_position() + mouse_position
_context_menu.popup()


Expand All @@ -501,7 +504,7 @@ func _on_run_pressed(run_debug :bool) -> void:
func _on_Tree_item_selected() -> void:
# only show report checked manual item selection
# we need to check the run mode here otherwise it will be called every selection
if not _context_menu_run.disabled:
if not _context_menu.is_item_disabled(CONTEXT_MENU_RUN_ID):
var selected_item :TreeItem = _tree.get_selected()
show_failed_report(selected_item)

Expand Down Expand Up @@ -531,14 +534,14 @@ func _on_Tree_item_activated() -> void:
# external signal receiver
################################################################################
func _on_gdunit_runner_start():
_context_menu_run.disabled = true
_context_menu_debug.disabled = true
_context_menu.set_item_disabled(CONTEXT_MENU_RUN_ID, true)
_context_menu.set_item_disabled(CONTEXT_MENU_DEBUG_ID, true)
clear_failures()


func _on_gdunit_runner_stop(_client_id :int):
_context_menu_run.disabled = false
_context_menu_debug.disabled = false
_context_menu.set_item_disabled(CONTEXT_MENU_RUN_ID, false)
_context_menu.set_item_disabled(CONTEXT_MENU_DEBUG_ID, false)
abort_running()
clear_failures()
collect_failures_and_errors()
Expand Down Expand Up @@ -576,3 +579,11 @@ func _on_StatusBar_failure_next():

func _on_StatusBar_failure_prevous():
select_previous_failure()


func _on_context_m_index_pressed(index):
match index:
CONTEXT_MENU_DEBUG_ID:
_on_run_pressed(true)
CONTEXT_MENU_RUN_ID:
_on_run_pressed(false)
46 changes: 10 additions & 36 deletions addons/gdUnit4/src/ui/parts/InspectorTreePanel.tscn
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ use_parent_material = true
layout_mode = 2
size_flags_horizontal = 3
size_flags_vertical = 3
theme_override_constants/icon_max_width = 16
allow_rmb_select = true
hide_root = true
select_mode = 1
Expand Down Expand Up @@ -58,43 +59,16 @@ layout_mode = 2
size_flags_horizontal = 3
size_flags_vertical = 3

[node name="contextMenu" type="PopupPanel" parent="."]
size = Vector2i(103, 80)

[node name="items" type="VBoxContainer" parent="contextMenu"]
anchors_preset = 15
anchor_right = 1.0
anchor_bottom = 1.0
offset_left = 4.0
offset_top = 4.0
offset_right = -4.0
offset_bottom = -4.0
grow_horizontal = 2
grow_vertical = 2
size_flags_horizontal = 3
size_flags_vertical = 3

[node name="debug" type="MenuButton" parent="contextMenu/items"]
layout_mode = 2
size_flags_horizontal = 11
size_flags_vertical = 9
keep_pressed_outside = true
text = "Debug Test"
switch_on_hover = true

[node name="run" type="MenuButton" parent="contextMenu/items"]
layout_mode = 2
size_flags_horizontal = 11
size_flags_vertical = 9
keep_pressed_outside = true
text = "Run Test"
switch_on_hover = true
[node name="contextMenu" type="PopupMenu" parent="."]
size = Vector2i(120, 60)
auto_translate = false
item_count = 2
item_0/text = "Run"
item_0/id = 0
item_1/text = "Debug"
item_1/id = 1

[connection signal="item_activated" from="Panel/Tree" to="." method="_on_Tree_item_activated"]
[connection signal="item_mouse_selected" from="Panel/Tree" to="." method="_on_tree_item_mouse_selected"]
[connection signal="item_selected" from="Panel/Tree" to="." method="_on_Tree_item_selected"]
[connection signal="focus_exited" from="contextMenu" to="." method="_on_contextMenu_focus_exited"]
[connection signal="popup_hide" from="contextMenu" to="." method="_on_contextMenu_popup_hide"]
[connection signal="mouse_exited" from="contextMenu/items" to="." method="_on_items_mouse_exited"]
[connection signal="pressed" from="contextMenu/items/debug" to="." method="_on_run_pressed" binds= [true]]
[connection signal="pressed" from="contextMenu/items/run" to="." method="_on_run_pressed" binds= [false]]
[connection signal="index_pressed" from="contextMenu" to="." method="_on_context_m_index_pressed"]

0 comments on commit 600fccd

Please sign in to comment.