add folder browser

This commit is contained in:
Lera Elvoé 2023-05-09 21:08:49 +03:00
parent 85cdaca6f8
commit 7bab90995d
Signed by: yagich
SSH Key Fingerprint: SHA256:6xjGb6uA7lAVcULa7byPEN//rQ0wPoG+UzYVMfZnbvc
3 changed files with 124 additions and 1 deletions

105
main.gd Normal file
View File

@ -0,0 +1,105 @@
extends Control
@onready var select_folder_button: Button = %SelectFolderButton
@onready var start_server_button: Button = %StartServerButton
@onready var open_in_browser_button: Button = %OpenInBrowserButton
@onready var file_tree: Tree = %FileTree
@onready var dnd_help_label: Label = %DnDHelpLabel
@onready var article_name_line_edit: LineEdit = %ArticleNameLineEdit
@onready var article_date_line_edit: LineEdit = %ArticleDateLineEdit
@onready var content_text_edit: TextEdit = %ContentTextEdit
@onready var server_folder_dialog: FileDialog = %ServerFolderDialog
const SERVER_FILE := "res://server_files/index.html"
var working_folder: String
var server: HTTPServer
var server_port: int = 3001
func _ready() -> void:
select_folder_button.pressed.connect(
func():
server_folder_dialog.popup_centered()
)
server_folder_dialog.dir_selected.connect(on_server_folder_dialog_dir_selected)
func on_server_folder_dialog_dir_selected(folder: String) -> void:
working_folder = folder
var dirs := list_files_recursive(working_folder)
print(dirs)
refresh_file_tree(dirs)
dnd_help_label.visible = false
func refresh_file_tree(dirs: Array[String]) -> void:
file_tree.clear()
var root := file_tree.create_item()
for i in dirs:
prints(i, ":", i.get_slice_count("/"))
if i.get_slice_count("/") == 1:
var item := root.create_child()
item.set_text(0, i)
else:
var base := find_or_create_subfolder_item(i.get_base_dir())
var item := base.create_child()
item.set_text(0, i.get_file())
func list_files_recursive(folder: String, prefix: String = "") -> Array[String]:
# var res := [folder.trim_prefix(working_folder).trim_prefix("/")]
var res: Array[String] = []
var dir := DirAccess.open(folder)
if dir:
dir.list_dir_begin()
var file_name := dir.get_next()
while file_name != "":
if dir.current_is_dir():
res.append_array(list_files_recursive(folder.path_join(file_name), folder))
else:
res.append(folder.path_join(file_name).trim_prefix(working_folder + "/"))
file_name = dir.get_next()
res.sort_custom(
func(a: String, b: String):
return b.get_slice_count("/") == 1
)
return res
else:
return []
func find_or_create_subfolder_item(subfolder: String, from_item: TreeItem = file_tree.get_root()) -> TreeItem:
var search_res: TreeItem = null
for item in from_item.get_children():
if item.get_meta("subfolder_name", "") == subfolder:
search_res = item
return search_res
if item.get_child_count() > 0:
search_res = find_or_create_subfolder_item(subfolder, item)
if is_instance_valid(search_res):
return search_res
# early optimization. if there is only one slash, the item is a child of root.
if subfolder.get_slice_count("/") == 1:
var item := file_tree.get_root().create_child()
item.set_meta("subfolder_name", subfolder)
item.set_text(0, subfolder.get_slice("/", subfolder.get_slice_count("/") - 1))
return item
if subfolder.get_slice_count("/") > 1:
# need more searches to find items with the base dir(s)
# var base_paths := subfolder.get_base_dir().split("/", false)
var base: TreeItem = find_or_create_subfolder_item(subfolder.get_base_dir())
var item := base.create_child()
item.set_meta("subfolder_name", subfolder)
item.set_text(0, subfolder.get_slice("/", subfolder.get_slice_count("/") - 1))
return item
# i dont know at this point
print("cant find item for subfolder, ", subfolder)
return null

View File

@ -1,4 +1,6 @@
[gd_scene load_steps=2 format=3 uid="uid://w4airtd0kofc"] [gd_scene load_steps=3 format=3 uid="uid://w4airtd0kofc"]
[ext_resource type="Script" path="res://main.gd" id="1_t6a3l"]
[sub_resource type="LabelSettings" id="LabelSettings_6csy6"] [sub_resource type="LabelSettings" id="LabelSettings_6csy6"]
font_color = Color(1, 1, 1, 0.752941) font_color = Color(1, 1, 1, 0.752941)
@ -11,6 +13,7 @@ anchor_right = 0.960069
anchor_bottom = 0.919753 anchor_bottom = 0.919753
grow_horizontal = 2 grow_horizontal = 2
grow_vertical = 2 grow_vertical = 2
script = ExtResource("1_t6a3l")
metadata/_edit_use_anchors_ = true metadata/_edit_use_anchors_ = true
[node name="VBoxContainer" type="VBoxContainer" parent="."] [node name="VBoxContainer" type="VBoxContainer" parent="."]
@ -74,6 +77,7 @@ text = "Files"
unique_name_in_owner = true unique_name_in_owner = true
layout_mode = 2 layout_mode = 2
size_flags_vertical = 3 size_flags_vertical = 3
hide_root = true
metadata/_edit_group_ = true metadata/_edit_group_ = true
[node name="DnDHelpLabel" type="Label" parent="VBoxContainer/HSplitContainer/VBoxContainer/FileTree"] [node name="DnDHelpLabel" type="Label" parent="VBoxContainer/HSplitContainer/VBoxContainer/FileTree"]
@ -124,4 +128,14 @@ text = "Content"
unique_name_in_owner = true unique_name_in_owner = true
layout_mode = 2 layout_mode = 2
size_flags_vertical = 3 size_flags_vertical = 3
editable = false
caret_blink = true caret_blink = true
[node name="ServerFolderDialog" type="FileDialog" parent="."]
unique_name_in_owner = true
initial_position = 2
title = "Open a Directory"
size = Vector2i(768, 469)
ok_button_text = "Select Curent Folder"
file_mode = 2
access = 2

View File

@ -14,3 +14,7 @@ config/name="Tickle App"
run/main_scene="res://main.tscn" run/main_scene="res://main.tscn"
config/features=PackedStringArray("4.0", "Forward Plus") config/features=PackedStringArray("4.0", "Forward Plus")
config/icon="res://icon.svg" config/icon="res://icon.svg"
[display]
window/subwindows/embed_subwindows=false