2021-07-11 17:32:13 +02:00
|
|
|
# Copyright (c) 2021, Leroy Hopson (MIT License).
|
|
|
|
#
|
|
|
|
# This file contains snippets of code derived from Godot's editor_node.cpp file.
|
|
|
|
# These snippets are copyright of their authors and released under the MIT license:
|
|
|
|
# - Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur (MIT License).
|
|
|
|
# - Copyright (c) 2014-2021 Godot Engine contributors (MIT License).
|
2022-11-09 21:57:46 +01:00
|
|
|
@tool
|
2021-07-11 17:32:13 +02:00
|
|
|
extends Control
|
|
|
|
|
|
|
|
const EditorTerminal := preload("./editor_terminal.tscn")
|
2021-07-12 18:11:28 +02:00
|
|
|
const TerminalSettings := preload("./settings/terminal_settings.gd")
|
|
|
|
|
|
|
|
const SETTINGS_FILE_PATH := "res://.gdxterm/settings.tres"
|
|
|
|
|
|
|
|
enum TerminalPopupMenuOptions {
|
|
|
|
NEW_TERMINAL = 0,
|
|
|
|
COPY = 2,
|
|
|
|
PASTE = 3,
|
2021-07-13 17:20:29 +02:00
|
|
|
COPY_ALL = 4,
|
2021-07-12 18:11:28 +02:00
|
|
|
CLEAR = 6,
|
|
|
|
KILL_TERMINAL = 7,
|
|
|
|
}
|
2021-07-11 17:32:13 +02:00
|
|
|
|
|
|
|
# Has access to the EditorSettings singleton so it can dynamically generate the
|
2022-11-09 22:52:54 +01:00
|
|
|
# terminal color scheme based on editor theme settings.
|
2021-07-12 18:11:28 +02:00
|
|
|
var editor_plugin: EditorPlugin
|
2021-07-11 17:32:13 +02:00
|
|
|
var editor_interface: EditorInterface
|
|
|
|
|
2022-11-09 21:57:46 +01:00
|
|
|
@onready var editor_settings: EditorSettings = editor_interface.get_editor_settings()
|
2023-01-08 10:41:48 +01:00
|
|
|
@onready var tabs: TabBar = $VBoxContainer/TabbarContainer/Tabs
|
2022-11-09 21:57:46 +01:00
|
|
|
@onready var tabbar_container: HBoxContainer = $VBoxContainer/TabbarContainer
|
2023-01-08 10:41:48 +01:00
|
|
|
@onready var add_button: Button = $VBoxContainer/TabbarContainer/AddButton
|
2022-11-09 21:57:46 +01:00
|
|
|
@onready var tab_container: TabContainer = $VBoxContainer/TabContainer
|
|
|
|
@onready var terminal_popup_menu: PopupMenu = $VBoxContainer/TerminalPopupMenu
|
2021-07-13 18:33:26 +02:00
|
|
|
|
|
|
|
# Size label.
|
|
|
|
# Used to show the size of the terminal (rows/cols) and panel (pixels) when resized.
|
2022-11-09 21:57:46 +01:00
|
|
|
@onready var size_label: Label = $SizeLabel
|
|
|
|
@onready var size_label_timer: Timer = $SizeLabel/SizeLabelTimer
|
2021-07-13 18:33:26 +02:00
|
|
|
|
2023-01-08 10:41:48 +01:00
|
|
|
@onready var is_ready := true
|
2021-07-11 17:32:13 +02:00
|
|
|
|
|
|
|
var _theme := Theme.new()
|
2021-07-12 18:11:28 +02:00
|
|
|
var _settings: TerminalSettings
|
2021-07-11 17:32:13 +02:00
|
|
|
var _tab_container_min_size
|
|
|
|
|
|
|
|
|
|
|
|
func _ready():
|
|
|
|
_update_settings()
|
|
|
|
|
|
|
|
|
2021-07-12 18:11:28 +02:00
|
|
|
func _load_or_create_settings() -> void:
|
2021-07-25 17:59:16 +02:00
|
|
|
# Use only default settings for now, until settings are properly defined
|
|
|
|
# and documented.
|
|
|
|
_settings = TerminalSettings.new()
|
2021-07-12 18:11:28 +02:00
|
|
|
|
|
|
|
|
2021-07-11 17:32:13 +02:00
|
|
|
func _update_settings() -> void:
|
2021-07-12 18:11:28 +02:00
|
|
|
_load_or_create_settings()
|
|
|
|
|
2021-07-20 08:25:12 +02:00
|
|
|
var editor_scale: float = 1.0
|
|
|
|
if editor_interface.has_method("get_editor_scale"):
|
|
|
|
editor_scale = editor_interface.get_editor_scale()
|
|
|
|
|
2023-01-08 10:41:48 +01:00
|
|
|
custom_minimum_size = Vector2(0, tabbar_container.size.y + 182) * editor_scale
|
2022-11-09 21:57:46 +01:00
|
|
|
size.y = 415
|
2021-07-11 17:32:13 +02:00
|
|
|
|
2022-11-09 21:57:46 +01:00
|
|
|
tabs.tab_close_display_policy = TabBar.CLOSE_BUTTON_SHOW_ALWAYS
|
2021-07-11 17:32:13 +02:00
|
|
|
|
2024-05-05 06:28:29 +02:00
|
|
|
# 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
|
|
|
|
)
|
2021-07-13 11:43:51 +02:00
|
|
|
|
2021-07-11 17:32:13 +02:00
|
|
|
_update_terminal_tabs()
|
|
|
|
|
|
|
|
|
|
|
|
func _update_terminal_tabs():
|
|
|
|
# Wait a couple of frames to allow everything to resize before updating.
|
2023-01-08 10:41:48 +01:00
|
|
|
await get_tree().process_frame
|
|
|
|
await get_tree().process_frame
|
2021-07-11 17:32:13 +02:00
|
|
|
|
|
|
|
if tabs.get_offset_buttons_visible():
|
2022-11-09 22:52:54 +01:00
|
|
|
# Move add button to fixed position on the tabbar.
|
2021-07-11 17:32:13 +02:00
|
|
|
if add_button.get_parent() == tabs:
|
2022-11-09 21:57:46 +01:00
|
|
|
add_button.position = Vector2.ZERO
|
2021-07-11 17:32:13 +02:00
|
|
|
tabs.remove_child(add_button)
|
|
|
|
tabbar_container.add_child(add_button)
|
|
|
|
tabbar_container.move_child(add_button, 0)
|
|
|
|
else:
|
|
|
|
# Move add button after last tab.
|
2023-01-08 10:41:48 +01:00
|
|
|
if tabs.tab_count > 0 and add_button.get_parent() == tabbar_container:
|
2021-07-11 17:32:13 +02:00
|
|
|
tabbar_container.remove_child(add_button)
|
|
|
|
tabs.add_child(add_button)
|
|
|
|
var last_tab := Rect2()
|
|
|
|
if tabs.get_tab_count() > 0:
|
|
|
|
last_tab = tabs.get_tab_rect(tabs.get_tab_count() - 1)
|
2022-11-09 21:57:46 +01:00
|
|
|
add_button.position = Vector2(
|
2021-07-11 17:32:13 +02:00
|
|
|
last_tab.position.x + last_tab.size.x + 3, last_tab.position.y
|
|
|
|
)
|
2023-01-08 10:41:48 +01:00
|
|
|
if tabs.tab_count == 0 and add_button.get_parent() == tabs:
|
|
|
|
tabs.remove_child(add_button)
|
|
|
|
tabbar_container.add_child(add_button)
|
2021-07-11 17:32:13 +02:00
|
|
|
|
|
|
|
# Make sure we still own the button, so it gets saved with our scene.
|
|
|
|
add_button.owner = self
|
|
|
|
|
|
|
|
|
|
|
|
func _on_AddButton_pressed():
|
|
|
|
var shell = OS.get_environment("SHELL") if OS.has_environment("SHELL") else "sh"
|
2022-11-09 21:57:46 +01:00
|
|
|
var terminal := EditorTerminal.instantiate()
|
2021-07-11 17:32:13 +02:00
|
|
|
tabs.add_tab(shell.get_file())
|
|
|
|
terminal.editor_settings = editor_settings
|
2023-01-08 10:41:48 +01:00
|
|
|
terminal.set_anchors_preset(PRESET_BOTTOM_WIDE)
|
|
|
|
terminal.connect("gui_input", Callable(self, "_on_TabContainer_gui_input"))
|
|
|
|
terminal.connect("exited", Callable(self, "_on_Terminal_exited").bind(terminal))
|
2021-07-11 17:32:13 +02:00
|
|
|
tab_container.add_child(terminal)
|
|
|
|
terminal.pty.fork(shell)
|
|
|
|
terminal.grab_focus()
|
|
|
|
tabs.current_tab = tabs.get_tab_count() - 1
|
|
|
|
tab_container.current_tab = tabs.current_tab
|
|
|
|
_update_terminal_tabs()
|
|
|
|
|
|
|
|
|
|
|
|
func _on_Tabs_tab_changed(tab_index):
|
2024-04-14 10:28:26 +02:00
|
|
|
tab_container.call_deferred("set_current_tab", tab_index)
|
|
|
|
await get_tree().process_frame
|
|
|
|
tab_container.get_current_tab_control().grab_focus()
|
2021-07-11 17:32:13 +02:00
|
|
|
|
|
|
|
|
|
|
|
func _on_Tabs_tab_close(tab_index):
|
|
|
|
tabs.remove_tab(tab_index)
|
|
|
|
tab_container.get_child(tab_index).queue_free()
|
2021-07-13 11:43:51 +02:00
|
|
|
|
|
|
|
# Switch focus to the next active tab.
|
|
|
|
if tabs.get_tab_count() > 0:
|
|
|
|
tab_container.get_child(tabs.current_tab).grab_focus()
|
|
|
|
|
2021-07-11 17:32:13 +02:00
|
|
|
_update_terminal_tabs()
|
|
|
|
|
|
|
|
|
|
|
|
func _notification(what):
|
2023-01-08 10:41:48 +01:00
|
|
|
if not is_ready:
|
2021-07-11 17:32:13 +02:00
|
|
|
return
|
|
|
|
|
|
|
|
match what:
|
|
|
|
EditorSettings.NOTIFICATION_EDITOR_SETTINGS_CHANGED:
|
|
|
|
_update_settings()
|
|
|
|
_update_terminal_tabs()
|
|
|
|
NOTIFICATION_RESIZED:
|
|
|
|
_update_terminal_tabs()
|
2022-11-09 21:57:46 +01:00
|
|
|
NOTIFICATION_APPLICATION_FOCUS_IN:
|
2021-07-11 17:32:13 +02:00
|
|
|
_update_terminal_tabs()
|
2021-07-12 18:11:28 +02:00
|
|
|
|
|
|
|
|
|
|
|
func _input(event: InputEvent) -> void:
|
|
|
|
if not _settings or not event.is_pressed():
|
|
|
|
return
|
|
|
|
|
2021-07-13 11:43:51 +02:00
|
|
|
# Global shortcut to open new terminal and make terminal panel visible.
|
2024-05-05 06:28:29 +02:00
|
|
|
if _settings.new_terminal_shortcut and _settings.new_terminal_shortcut.matches_event(event):
|
|
|
|
get_viewport().set_input_as_handled()
|
|
|
|
editor_plugin.make_bottom_panel_item_visible(self)
|
|
|
|
_on_AddButton_pressed()
|
2021-07-12 18:11:28 +02:00
|
|
|
|
2021-07-13 11:43:51 +02:00
|
|
|
# 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.
|
2024-05-05 06:28:29 +02:00
|
|
|
if (
|
|
|
|
_settings.kill_terminal_shortcut
|
|
|
|
and _settings.kill_terminal_shortcut.matches_event(event)
|
|
|
|
):
|
|
|
|
get_viewport().set_input_as_handled()
|
|
|
|
_on_TerminalPopupMenu_id_pressed(TerminalPopupMenuOptions.KILL_TERMINAL)
|
|
|
|
|
|
|
|
# Copy.
|
|
|
|
if _settings.copy_shortcut and _settings.copy_shortcut.matches_event(event):
|
|
|
|
get_viewport().set_input_as_handled()
|
|
|
|
_on_TerminalPopupMenu_id_pressed(TerminalPopupMenuOptions.COPY)
|
|
|
|
|
|
|
|
# Paste.
|
|
|
|
if _settings.paste_shortcut and _settings.paste_shortcut.matches_event(event):
|
|
|
|
get_viewport().set_input_as_handled()
|
|
|
|
_on_TerminalPopupMenu_id_pressed(TerminalPopupMenuOptions.PASTE)
|
|
|
|
|
|
|
|
# Next tab.
|
|
|
|
if _settings.next_tab_shortcut and _settings.next_tab_shortcut.matches_event(event):
|
|
|
|
get_viewport().set_input_as_handled()
|
|
|
|
tabs.current_tab = min(tabs.current_tab + 1, tabs.get_tab_count() - 1)
|
|
|
|
|
|
|
|
# Previous tab.
|
|
|
|
if _settings.previous_tab_shortcut and _settings.previous_tab_shortcut.matches_event(event):
|
|
|
|
get_viewport().set_input_as_handled()
|
|
|
|
tabs.current_tab = max(tabs.current_tab - 1, 0)
|
2021-07-13 17:48:02 +02:00
|
|
|
|
2021-07-12 18:11:28 +02:00
|
|
|
|
|
|
|
func _on_TabContainer_gui_input(event):
|
2022-11-09 21:57:46 +01:00
|
|
|
if event is InputEventMouseButton and event.button_index == MOUSE_BUTTON_RIGHT:
|
|
|
|
terminal_popup_menu.position = event.global_position
|
2021-07-12 18:11:28 +02:00
|
|
|
terminal_popup_menu.popup()
|
|
|
|
|
|
|
|
|
|
|
|
func _on_TerminalPopupMenu_id_pressed(id):
|
|
|
|
match id:
|
|
|
|
TerminalPopupMenuOptions.NEW_TERMINAL:
|
|
|
|
_on_AddButton_pressed()
|
2021-07-13 01:27:20 +02:00
|
|
|
|
|
|
|
if tabs.get_tab_count() > 0:
|
|
|
|
var terminal = tab_container.get_child(tab_container.current_tab)
|
|
|
|
match id:
|
2021-07-13 11:43:51 +02:00
|
|
|
TerminalPopupMenuOptions.COPY:
|
2023-01-08 10:41:48 +01:00
|
|
|
DisplayServer.clipboard_set(terminal.copy_selection())
|
2021-07-13 01:27:20 +02:00
|
|
|
TerminalPopupMenuOptions.PASTE:
|
2024-05-05 06:28:29 +02:00
|
|
|
terminal.write(DisplayServer.clipboard_get())
|
2021-07-13 17:20:29 +02:00
|
|
|
TerminalPopupMenuOptions.COPY_ALL:
|
2023-01-08 10:41:48 +01:00
|
|
|
DisplayServer.clipboard_set(terminal.copy_all())
|
2021-07-13 01:27:20 +02:00
|
|
|
TerminalPopupMenuOptions.CLEAR:
|
|
|
|
terminal.clear()
|
|
|
|
TerminalPopupMenuOptions.KILL_TERMINAL:
|
2021-07-12 18:11:28 +02:00
|
|
|
_on_Tabs_tab_close(tabs.current_tab)
|
2021-07-13 13:50:00 +02:00
|
|
|
|
|
|
|
|
|
|
|
func _on_Tabs_reposition_active_tab_request(idx_to):
|
|
|
|
var active = tab_container.get_child(tab_container.current_tab)
|
|
|
|
tab_container.move_child(active, idx_to)
|
2021-07-13 18:33:26 +02:00
|
|
|
|
|
|
|
|
|
|
|
func _on_Panel_resized():
|
|
|
|
if not size_label:
|
|
|
|
return
|
|
|
|
|
2022-11-09 21:57:46 +01:00
|
|
|
var size = tab_container.size
|
2021-07-13 18:33:26 +02:00
|
|
|
if tabs.get_tab_count() > 0:
|
|
|
|
var terminal = tab_container.get_child(tabs.current_tab)
|
2022-08-15 00:35:01 +02:00
|
|
|
var cols = terminal.get_cols()
|
|
|
|
var rows = terminal.get_rows()
|
2021-07-13 18:33:26 +02:00
|
|
|
size_label.text = "Size: %d cols; %d rows\n(%d x %d px)" % [cols, rows, size.x, size.y]
|
|
|
|
else:
|
|
|
|
size_label.text = "Size:\n(%d x %d px)" % [size.x, size.y]
|
|
|
|
|
|
|
|
size_label.visible = true
|
|
|
|
size_label_timer.wait_time = 1
|
|
|
|
size_label_timer.start()
|
|
|
|
|
|
|
|
|
|
|
|
func _on_SizeLabelTimer_timeout():
|
|
|
|
if size_label:
|
|
|
|
size_label.visible = false
|
2021-07-15 14:33:59 +02:00
|
|
|
|
|
|
|
|
|
|
|
func _on_Terminal_exited(exit_code, signum, terminal):
|
|
|
|
# Leave non-zero exit code terminals open in case they have some important
|
|
|
|
# error information.
|
|
|
|
if exit_code == 0:
|
|
|
|
_on_Tabs_tab_close(terminal.get_index())
|