Skip to content

Commit

Permalink
[licenses] Add right click context menu
Browse files Browse the repository at this point in the history
  • Loading branch information
IceflowRE committed Nov 11, 2024
1 parent 5be524c commit 26ac528
Show file tree
Hide file tree
Showing 7 changed files with 47 additions and 31 deletions.
9 changes: 9 additions & 0 deletions addons/licenses/internal/components_tree.gd
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,15 @@ func reload(scroll_to: Component = null) -> void:
self.scroll_to_item(item)
item.select(0)

func select_component(comp: Component) -> void:
var tree_item: TreeItem = self.get_root().get_next_in_tree()
while tree_item != null:
if tree_item.has_meta("idx") && comp == self._li.get_at(tree_item.get_meta("idx")):
tree_item.select(0)
self.scroll_to_item(tree_item)
break
tree_item = tree_item.get_next_in_tree()

func _create_item_menu() -> void:
self._item_menu = PopupMenu.new()
self._item_menu.name = "item_menu"
Expand Down
12 changes: 6 additions & 6 deletions addons/licenses/internal/licenses.gd
Original file line number Diff line number Diff line change
Expand Up @@ -45,22 +45,22 @@ func _ready() -> void:
self._toolbar.add_component.connect(self._on_toolbar_add_component)
self._toolbar.show_engine_components.connect(self._on_toolbar_show_engine_components)

self.reload()
self._update_set_license_filepath_button()
self._li.components_changed.connect(self._on_components_changed)

func reload() -> void:
self._update_set_license_filepath_button()
var res: Licenses.LoadResult = Licenses.load(self._license_file_edit.text)
var res: Licenses.LoadResult = self._li.load_licenses(self._license_file_edit.text)
if res.err_msg == "":
self._license_file_edit.right_icon = null
self._license_file_edit.tooltip_text = ""
else:
self._license_file_edit.right_icon = self.get_theme_icon(&"NodeWarning", &"EditorIcons")
self._license_file_edit.tooltip_text = res.err_msg
self._li.set_components(res.components)
self._li.sort_custom(Licenses.compare_components_ascending)
self._li.emit_components_changed()

func show_component(comp: Component) -> void:
self._components_tree.select_component(comp)
self._component_detail_tree.set_component(comp)

func _update_set_license_filepath_button() -> void:
if Licenses.get_license_data_filepath() == self._license_file_edit.text:
Expand Down
5 changes: 1 addition & 4 deletions addons/licenses/internal/licenses_dialog.gd
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,8 @@ extends Window

const LicensesContainer := preload("res://addons/licenses/internal/licenses.gd")

@export var _licenses: LicensesContainer
@export var licenses: LicensesContainer

func _notification(what: int) -> void:
if (what == NOTIFICATION_WM_CLOSE_REQUEST):
self.hide()

func _on_about_to_popup() -> void:
self._licenses.reload()
4 changes: 2 additions & 2 deletions addons/licenses/internal/licenses_dialog.tscn
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@
[ext_resource type="Script" path="res://addons/licenses/internal/licenses_dialog.gd" id="1_0s46i"]
[ext_resource type="PackedScene" uid="uid://dfj2mhwrs1oss" path="res://addons/licenses/internal/licenses.tscn" id="1_f3ql8"]

[node name="license_dialog" type="Window" node_paths=PackedStringArray("_licenses")]
[node name="license_dialog" type="Window" node_paths=PackedStringArray("licenses")]
disable_3d = true
title = "Licenses"
size = Vector2i(1920, 1080)
visible = false
wrap_controls = true
transient = true
script = ExtResource("1_0s46i")
_licenses = NodePath("licenses")
licenses = NodePath("licenses")

[node name="panel" type="Panel" parent="."]
anchors_preset = 15
Expand Down
6 changes: 3 additions & 3 deletions addons/licenses/internal/plugin/context_menu_filesystem.gd
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ func _popup_menu(paths: PackedStringArray) -> void:
var comps: Array[Component] = self._li.get_components_in_path(paths[0])
if comps.size() > 0:
for comp: Component in comps:
self.add_context_menu_item("Show license", self._on_single_ctx_menu_clicked.bind(comp))
self.add_context_menu_item("Show license (" + comp.name + ")", self._on_single_ctx_menu_clicked.bind(comp))

func _on_single_ctx_menu_clicked(vals: PackedStringArray, comp: Component) -> void:
print(comp.name)
func _on_single_ctx_menu_clicked(_paths: PackedStringArray, comp: Component) -> void:
self._li.show_popup(comp)
26 changes: 23 additions & 3 deletions addons/licenses/internal/plugin/licenses_interface.gd
Original file line number Diff line number Diff line change
@@ -1,13 +1,36 @@
extends Node

const LicensesDialogScene: PackedScene = preload("res://addons/licenses/internal/licenses_dialog.tscn")
const Licenses := preload("res://addons/licenses/licenses.gd")
const Component := preload("res://addons/licenses/component.gd")
const LicensesDialog := preload("res://addons/licenses/internal/licenses_dialog.gd")

signal components_changed()
signal cfg_path_changed(new_path: String)

var _licenses_dialog: LicensesDialog

var _components: Array[Component] = []

func _ready() -> void:
self._licenses_dialog = LicensesDialogScene.instantiate()
self.add_child(self._licenses_dialog)

func show_popup(show_comp: Component = null) -> void:
if show_comp != null:
self._licenses_dialog.licenses.show_component(show_comp)
if self._licenses_dialog.visible:
self._licenses_dialog.grab_focus()
else:
self._licenses_dialog.popup_centered_ratio(0.4)

func load_licenses(license_path: String) -> Licenses.LoadResult:
var res: Licenses.LoadResult = Licenses.load(license_path)
self._components = res.components
self.sort_custom(Licenses.compare_components_ascending)
self.emit_components_changed()
return res

func emit_components_changed() -> void:
self.components_changed.emit()

Expand All @@ -19,9 +42,6 @@ func add_component(component: Component) -> void:
self._components.append(component)
self._components.sort_custom(Licenses.compare_components_ascending)

func set_components(components_: Array[Component]) -> void:
self._components = components_

func components() -> Array[Component]:
return self._components

Expand Down
16 changes: 3 additions & 13 deletions addons/licenses/plugin.gd
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
@tool
extends EditorPlugin

const LicensesDialogScene: PackedScene = preload("res://addons/licenses/internal/licenses_dialog.tscn")
const LicensesDialog := preload("res://addons/licenses/internal/licenses_dialog.gd")
const Licenses := preload("res://addons/licenses/licenses.gd")
const ExportPlugin := preload("res://addons/licenses/internal/plugin/export_plugin.gd")
const LicensesInterface := preload("res://addons/licenses/internal/plugin/licenses_interface.gd")
Expand All @@ -11,7 +9,6 @@ const FileSystemWatcher := preload("res://addons/licenses/internal/plugin/file_s

var _export_plugin: ExportPlugin
var _context_menu_filesystem: ContextMenuFilesystem
var _licenses_dialog: LicensesDialog
var _file_watcher: FileSystemWatcher

func _get_plugin_name() -> String:
Expand All @@ -20,30 +17,23 @@ func _get_plugin_name() -> String:
func _enter_tree() -> void:
set_project_setting(Licenses.DATA_FILE, "res://licenses.json", TYPE_STRING, PROPERTY_HINT_FILE)
LicensesInterface.create_interface()
LicensesInterface.get_interface().load_licenses(Licenses.get_license_data_filepath())
self._file_watcher = FileSystemWatcher.new()

self._export_plugin = ExportPlugin.new()
self.add_export_plugin(self._export_plugin)

self._licenses_dialog = LicensesDialogScene.instantiate()
EditorInterface.get_base_control().add_child(self._licenses_dialog)
self.add_tool_menu_item(self._get_plugin_name() + "...", self._show_popup)
self.add_tool_menu_item(self._get_plugin_name() + "...", LicensesInterface.get_interface().show_popup)
self._context_menu_filesystem = ContextMenuFilesystem.new()
self.add_context_menu_plugin(EditorContextMenuPlugin.CONTEXT_SLOT_FILESYSTEM, self._context_menu_filesystem)

func _exit_tree() -> void:
self.remove_context_menu_plugin(self._context_menu_filesystem)
self.remove_tool_menu_item(self._get_plugin_name() + "...")
self._licenses_dialog.queue_free()
self.remove_export_plugin(self._export_plugin)
self._file_watcher = null
LicensesInterface.remove_interface()

func _show_popup() -> void:
if _licenses_dialog.visible:
self._licenses_dialog.grab_focus()
else:
self._licenses_dialog.popup_centered_ratio(0.4)

static func set_project_setting(key: String, initial_value, type: int, type_hint: int) -> void:
if not ProjectSettings.has_setting(key):
ProjectSettings.set_setting(key, initial_value)
Expand Down

0 comments on commit 26ac528

Please sign in to comment.