From 703eb68f11873f94a5c2614be0975eee634b536f Mon Sep 17 00:00:00 2001 From: Leroy Hopson Date: Tue, 13 Jul 2021 16:43:51 +0700 Subject: [PATCH] More shortcuts - Copy - Paste - Kill --- .../settings/default_copy_shortcut.tres | 10 +++- .../default_kill_terminal_shortcut.tres | 11 +++++ .../settings/default_paste_shortcut.tres | 10 +++- .../terminal/settings/terminal_settings.gd | 1 + .../editor_plugins/terminal/terminal_panel.gd | 49 +++++++++++++++++++ .../terminal/terminal_panel.tscn | 2 +- 6 files changed, 80 insertions(+), 3 deletions(-) create mode 100644 addons/godot_xterm/editor_plugins/terminal/settings/default_kill_terminal_shortcut.tres diff --git a/addons/godot_xterm/editor_plugins/terminal/settings/default_copy_shortcut.tres b/addons/godot_xterm/editor_plugins/terminal/settings/default_copy_shortcut.tres index 5227ddc..09fcc09 100644 --- a/addons/godot_xterm/editor_plugins/terminal/settings/default_copy_shortcut.tres +++ b/addons/godot_xterm/editor_plugins/terminal/settings/default_copy_shortcut.tres @@ -1,3 +1,11 @@ -[gd_resource type="ShortCut" format=2] +[gd_resource type="ShortCut" load_steps=2 format=2] + +[sub_resource type="InputEventKey" id=1] +shift = true +control = true +command = true +pressed = true +scancode = 67 [resource] +shortcut = SubResource( 1 ) diff --git a/addons/godot_xterm/editor_plugins/terminal/settings/default_kill_terminal_shortcut.tres b/addons/godot_xterm/editor_plugins/terminal/settings/default_kill_terminal_shortcut.tres new file mode 100644 index 0000000..85a261b --- /dev/null +++ b/addons/godot_xterm/editor_plugins/terminal/settings/default_kill_terminal_shortcut.tres @@ -0,0 +1,11 @@ +[gd_resource type="ShortCut" load_steps=2 format=2] + +[sub_resource type="InputEventKey" id=1] +shift = true +control = true +command = true +pressed = true +scancode = 88 + +[resource] +shortcut = SubResource( 1 ) diff --git a/addons/godot_xterm/editor_plugins/terminal/settings/default_paste_shortcut.tres b/addons/godot_xterm/editor_plugins/terminal/settings/default_paste_shortcut.tres index 5227ddc..148d849 100644 --- a/addons/godot_xterm/editor_plugins/terminal/settings/default_paste_shortcut.tres +++ b/addons/godot_xterm/editor_plugins/terminal/settings/default_paste_shortcut.tres @@ -1,3 +1,11 @@ -[gd_resource type="ShortCut" format=2] +[gd_resource type="ShortCut" load_steps=2 format=2] + +[sub_resource type="InputEventKey" id=1] +shift = true +control = true +command = true +pressed = true +scancode = 86 [resource] +shortcut = SubResource( 1 ) diff --git a/addons/godot_xterm/editor_plugins/terminal/settings/terminal_settings.gd b/addons/godot_xterm/editor_plugins/terminal/settings/terminal_settings.gd index df4a7f9..e97a27a 100644 --- a/addons/godot_xterm/editor_plugins/terminal/settings/terminal_settings.gd +++ b/addons/godot_xterm/editor_plugins/terminal/settings/terminal_settings.gd @@ -13,6 +13,7 @@ enum CWDType { ### Shortcuts ### export (ShortCut) var new_terminal_shortcut = preload("./default_new_terminal_shortcut.tres") +export (ShortCut) var kill_terminal_shortcut = preload("./default_kill_terminal_shortcut.tres") export (ShortCut) var copy_shortcut = preload("./default_copy_shortcut.tres") export (ShortCut) var paste_shortcut = preload("./default_paste_shortcut.tres") diff --git a/addons/godot_xterm/editor_plugins/terminal/terminal_panel.gd b/addons/godot_xterm/editor_plugins/terminal/terminal_panel.gd index 0b8b7df..885182a 100644 --- a/addons/godot_xterm/editor_plugins/terminal/terminal_panel.gd +++ b/addons/godot_xterm/editor_plugins/terminal/terminal_panel.gd @@ -65,6 +65,24 @@ func _update_settings() -> void: tabs.tab_close_display_policy = Tabs.CLOSE_BUTTON_SHOW_ALWAYS + # Update shortcuts. + if _settings.new_terminal_shortcut: + terminal_popup_menu.set_item_shortcut( + TerminalPopupMenuOptions.NEW_TERMINAL, _settings.new_terminal_shortcut, true + ) + if _settings.kill_terminal_shortcut: + terminal_popup_menu.set_item_shortcut( + TerminalPopupMenuOptions.KILL_TERMINAL, _settings.kill_terminal_shortcut, false + ) + if _settings.copy_shortcut: + terminal_popup_menu.set_item_shortcut( + TerminalPopupMenuOptions.COPY, _settings.copy_shortcut, false + ) + if _settings.paste_shortcut: + terminal_popup_menu.set_item_shortcut( + TerminalPopupMenuOptions.PASTE, _settings.paste_shortcut, false + ) + _update_terminal_tabs() @@ -119,6 +137,11 @@ func _on_Tabs_tab_changed(tab_index): func _on_Tabs_tab_close(tab_index): tabs.remove_tab(tab_index) tab_container.get_child(tab_index).queue_free() + + # Switch focus to the next active tab. + if tabs.get_tab_count() > 0: + tab_container.get_child(tabs.current_tab).grab_focus() + _update_terminal_tabs() @@ -140,12 +163,36 @@ func _input(event: InputEvent) -> void: if not _settings or not event.is_pressed(): return + # Global shortcut to open new terminal and make terminal panel visible. if _settings.new_terminal_shortcut and _settings.new_terminal_shortcut.shortcut: if event.shortcut_match(_settings.new_terminal_shortcut.shortcut): get_tree().set_input_as_handled() editor_plugin.make_bottom_panel_item_visible(self) _on_AddButton_pressed() + # Non-global shortcuts, only applied if terminal is active and focused. + if ( + tabs.get_tab_count() > 0 and tab_container.get_child(tabs.current_tab).has_focus() + or terminal_popup_menu.has_focus() + ): + # Kill terminal. + if _settings.kill_terminal_shortcut and _settings.kill_terminal_shortcut.shortcut: + if event.shortcut_match(_settings.kill_terminal_shortcut.shortcut): + get_tree().set_input_as_handled() + _on_TerminalPopupMenu_id_pressed(TerminalPopupMenuOptions.KILL_TERMINAL) + + # Copy. + if _settings.copy_shortcut and _settings.copy_shortcut.shortcut: + if event.shortcut_match(_settings.copy_shortcut.shortcut): + get_tree().set_input_as_handled() + _on_TerminalPopupMenu_id_pressed(TerminalPopupMenuOptions.COPY) + + # Paste. + if _settings.paste_shortcut and _settings.paste_shortcut.shortcut: + if event.shortcut_match(_settings.paste_shortcut.shortcut): + get_tree().set_input_as_handled() + _on_TerminalPopupMenu_id_pressed(TerminalPopupMenuOptions.PASTE) + func _on_TabContainer_gui_input(event): if event is InputEventMouseButton and event.button_index == BUTTON_RIGHT: @@ -161,6 +208,8 @@ func _on_TerminalPopupMenu_id_pressed(id): if tabs.get_tab_count() > 0: var terminal = tab_container.get_child(tab_container.current_tab) match id: + TerminalPopupMenuOptions.COPY: + OS.clipboard = terminal.copy_selection() TerminalPopupMenuOptions.PASTE: for i in OS.clipboard.length(): var event = InputEventKey.new() diff --git a/addons/godot_xterm/editor_plugins/terminal/terminal_panel.tscn b/addons/godot_xterm/editor_plugins/terminal/terminal_panel.tscn index 9bb2113..4fe7c0a 100644 --- a/addons/godot_xterm/editor_plugins/terminal/terminal_panel.tscn +++ b/addons/godot_xterm/editor_plugins/terminal/terminal_panel.tscn @@ -107,7 +107,7 @@ margin_right = 193.0 margin_bottom = 160.0 size_flags_horizontal = 0 size_flags_vertical = 0 -items = [ "New Terminal [Ctrl+Shift+T]", null, 0, false, false, 0, 0, null, "", false, "", null, 0, false, true, 1, 0, null, "", true, "Copy [Ctrl+Shift+C]", null, 0, false, false, 2, 0, null, "", false, "Paste [Ctrl+Shift+V]", null, 0, false, false, 3, 0, null, "", false, "Select All", null, 0, false, true, 4, 0, null, "", false, "", null, 0, false, false, 5, 0, null, "", true, "Clear", null, 0, false, false, 6, 0, null, "", false, "Kill Terminal", null, 0, false, false, 7, 0, null, "", false ] +items = [ "New Terminal", null, 0, false, false, 0, 0, null, "", false, "", null, 0, false, true, 1, 0, null, "", true, "Copy", null, 0, false, false, 2, 0, null, "", false, "Paste", null, 0, false, false, 3, 0, null, "", false, "Select All", null, 0, false, true, 4, 0, null, "", false, "", null, 0, false, false, 5, 0, null, "", true, "Clear", null, 0, false, false, 6, 0, null, "", false, "Kill Terminal", null, 0, false, false, 7, 0, null, "", false ] __meta__ = { "_edit_use_anchors_": false }