From f0abc7593a2e7ac13f3ff106811962a93bede137 Mon Sep 17 00:00:00 2001 From: mguinhos Date: Wed, 21 Oct 2026 11:03:01 -0300 Subject: [PATCH] editor: explorador de arquivos (tinyblocks-editor-files) para Abrir/Salvar MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Nova library privada do bundle do editor (dlopen + hot reload) com um diálogo modal no lugar da barra de caminho: locais, trilha clicável, lista filtrada por extensão, campo de nome e confirmação ao substituir. - start_path() abre o explorador em todos os modos (projeto, exportar, imagem, código, .so); sem o módulo volta para a barra de caminho. - O switch do Enter virou path_commit(), usado pelos dois caminhos. - Estado em ed->fdlg para sobreviver à recarga do módulo. - Empacotado no AppImage; 6 cenários novos no harness headless. Co-Authored-By: Claude Opus 5.5 --- Makefile | 5 +- libraries/tinyblocks-editor/Makefile | 10 +- .../internal/tb_editor_internal.h | 57 ++ .../internal/tb_editor_modules.h | 12 + .../tinyblocks-editor-files/Makefile | 43 + .../include/tb_editor_files.h | 43 + .../tinyblocks-editor-files/library.toml | 17 + .../src/tb_editor_files.c | 962 ++++++++++++++++++ .../tinyblocks-editor-image/Makefile | 1 + libraries/tinyblocks-editor/library.toml | 4 +- libraries/tinyblocks-editor/src/tb_editor.c | 89 +- .../tinyblocks-editor/src/tb_editor_modules.c | 34 +- tests/editor_harness.c | 55 + 13 files changed, 1286 insertions(+), 46 deletions(-) create mode 100644 libraries/tinyblocks-editor/libraries/tinyblocks-editor-files/Makefile create mode 100644 libraries/tinyblocks-editor/libraries/tinyblocks-editor-files/include/tb_editor_files.h create mode 100644 libraries/tinyblocks-editor/libraries/tinyblocks-editor-files/library.toml create mode 100644 libraries/tinyblocks-editor/libraries/tinyblocks-editor-files/src/tb_editor_files.c diff --git a/Makefile b/Makefile index 25a62ac..3eb6518 100644 --- a/Makefile +++ b/Makefile @@ -270,6 +270,7 @@ EW_DIR := $(ED_DIR)/libraries/tinyblocks-editor-widgets EIMG_DIR := $(ED_DIR)/libraries/tinyblocks-editor-image EW_A := $(EW_DIR)/build/libtinyblocks-editor-widgets.a EIMG_A := $(EIMG_DIR)/build/libtinyblocks-editor-image.a +EFILES_DIR := $(ED_DIR)/libraries/tinyblocks-editor-files EDITOR_HDRS := $(EDITOR_SRC)/tb_stage_widget.h $(ED_DIR)/include/tb_editor.h $(GFX_HDR) $(HDRS) $(BUILD)/editor: examples/editor/main.c static libs @@ -339,6 +340,7 @@ appdir: editor ## Monta o AppDir (build/TinyBlocks.AppDir) para o AppImage @mkdir -p $(APPDIR)/usr/lib/tinyblocks-editor @cp $(EW_DIR)/build/libtinyblocks-editor-widgets.so $(APPDIR)/usr/lib/tinyblocks-editor/ 2>/dev/null || true @cp $(EIMG_DIR)/build/libtinyblocks-editor-image.so $(APPDIR)/usr/lib/tinyblocks-editor/ 2>/dev/null || true + @cp $(EFILES_DIR)/build/libtinyblocks-editor-files.so $(APPDIR)/usr/lib/tinyblocks-editor/ 2>/dev/null || true @echo "Empacotando libraries de extensao (.so + library.toml)..." @mkdir -p $(APPDIR)/usr/share/tinyblocks/libraries @for l in $(APPIMAGE_LIBS); do \ @@ -435,10 +437,11 @@ $(BUILD)/editor_harness: tests/editor_harness.c $(SRCS) \ $(EDITOR_SRC)/tb_editor_modules.c \ $(EW_DIR)/include/tb_editor_widgets.h \ $(EIMG_DIR)/include/tb_editor_image.h \ + $(EFILES_DIR)/include/tb_editor_files.h \ $(GX_DIR)/src/tb_graphics.c lib-blueprint ed-modules @mkdir -p $(BUILD) $(CC) $(CFLAGS_DBG) \ - -I$(ED_DIR)/include -I$(EDITOR_SRC) -I$(ED_DIR)/internal -I$(EW_DIR)/include -I$(EIMG_DIR)/include -I$(GFX_INC) $(CAT_INCS) -I$(BP_DIR)/include \ + -I$(ED_DIR)/include -I$(EDITOR_SRC) -I$(ED_DIR)/internal -I$(EW_DIR)/include -I$(EIMG_DIR)/include -I$(EFILES_DIR)/include -I$(GFX_INC) $(CAT_INCS) -I$(BP_DIR)/include \ -I$(API_DIR)/include -I$(LIBM_DIR)/include -I$(EN_DIR)/include -I$(TOML_DIR)/include -I$(CV_DIR)/include -I$(B64_DIR)/include -I$(PPM_DIR)/include -I$(SYN_DIR)/include \ $(SRCS) \ $(EDITOR_SRC)/tb_editor.c $(EDITOR_SRC)/tb_stage_widget.c \ diff --git a/libraries/tinyblocks-editor/Makefile b/libraries/tinyblocks-editor/Makefile index 8c42573..3951a80 100644 --- a/libraries/tinyblocks-editor/Makefile +++ b/libraries/tinyblocks-editor/Makefile @@ -16,12 +16,15 @@ CFLAGS := -std=c11 -Wall -Wextra -pedantic -fvisibility=hidden \ -I../tinyblocks-libman/include \ -I../tinyblocks-syntax/include \ -Ilibraries/tinyblocks-editor-widgets/include \ - -Ilibraries/tinyblocks-editor-image/include + -Ilibraries/tinyblocks-editor-image/include \ + -Ilibraries/tinyblocks-editor-files/include WIDGETS_DIR := libraries/tinyblocks-editor-widgets WIDGETS_A := $(WIDGETS_DIR)/build/libtinyblocks-editor-widgets.a IMG_DIR := libraries/tinyblocks-editor-image IMG_A := $(IMG_DIR)/build/libtinyblocks-editor-image.a +FILES_DIR := libraries/tinyblocks-editor-files +FILES_A := $(FILES_DIR)/build/libtinyblocks-editor-files.a OBJ := $(BUILD)/tb_editor.o $(BUILD)/tb_stage_widget.o $(BUILD)/tb_editor_modules.o LIB_A := $(BUILD)/libtinyblocks-editor.a @@ -36,6 +39,7 @@ $(BUILD)/tb_editor.o: src/tb_editor.c include/tb_editor.h internal/tb_editor_int src/tb_stage_widget.h \ $(WIDGETS_DIR)/include/tb_editor_widgets.h \ $(IMG_DIR)/include/tb_editor_image.h \ + $(FILES_DIR)/include/tb_editor_files.h \ ../tinyblocks-graphics/include/tb_graphics.h \ ../tinyblocks-variables/include/tb_variables.h \ ../tinyblocks-blueprint/include/tb_blueprint.h \ @@ -65,8 +69,10 @@ $(WIDGETS_A): FORCE $(MAKE) -C $(WIDGETS_DIR) $(IMG_A): FORCE $(MAKE) -C $(IMG_DIR) +$(FILES_A): FORCE + $(MAKE) -C $(FILES_DIR) -$(LIB_A): $(OBJ) $(WIDGETS_A) $(IMG_A) +$(LIB_A): $(OBJ) $(WIDGETS_A) $(IMG_A) $(FILES_A) ar rcs $@ $(OBJ) $(LIB_SO): $(OBJ) diff --git a/libraries/tinyblocks-editor/internal/tb_editor_internal.h b/libraries/tinyblocks-editor/internal/tb_editor_internal.h index 4f458fc..04d3c58 100644 --- a/libraries/tinyblocks-editor/internal/tb_editor_internal.h +++ b/libraries/tinyblocks-editor/internal/tb_editor_internal.h @@ -27,6 +27,7 @@ #include #include #include +#include #include "../internal/tb_editor_modules.h" #ifndef TB_EW_UNUSED @@ -482,6 +483,40 @@ enum { CM_DUPLICATE, CM_DELETE, CM_EDIT_DEF, CM_CLEANUP, CM_NEW_COMPOSITE, enum { PM_NONE, PM_LIB, PM_LOAD, PM_SAVE, PM_EXP_JSON, PM_EXP_XML, PM_EXP_CSV, PM_IMGLOAD, PM_IMSAVE, PM_IMADDR, PM_IMADDV, PM_CODELOAD, PM_CODESAVE }; +/* Explorador de arquivos (library privada tinyblocks-editor-files). + O estado vive aqui para sobreviver ao hot reload do modulo. */ +#define FD_PLACES 8 +typedef struct { + char name[256]; + int is_dir; /* 0 = arquivo, 1 = pasta, 2 = ".." (pasta pai) */ + long long size; + long long mtime; +} FileEnt; + +typedef struct { + bool open; + int mode; /* PM_* a executar ao confirmar */ + bool save; /* dialogo de salvar (pede substituir) */ + bool confirm; /* "ja existe, substituir?" visivel */ + bool all_files; /* ignora o filtro de extensoes */ + bool show_hidden; + char dir[1024]; /* pasta atual (lembrada entre aberturas) */ + char name[256]; /* campo "Nome" */ + char def_name[256]; /* nome sugerido (volta apos entrar numa pasta) */ + char filter[96]; /* ".json .xml" - vazio = todos */ + char title[64]; + char ok_label[24]; + char err[192]; + FileEnt* ents; /* listagem (alocada pelo modulo) */ + int n, cap; + int sel, scroll; + int last_click_i; + unsigned last_click_ms; + char place_path[FD_PLACES][512]; + char place_label[FD_PLACES][40]; + int place_n; +} FileDlg; + /* Formatos de exportacao */ enum { EX_JSON, EX_XML, EX_CSV }; @@ -580,6 +615,7 @@ typedef struct Ed { char path_input_buf[512]; int path_input_len; int path_mode; /* PM_* - o que fazer no Enter */ + FileDlg fdlg; /* explorador de arquivos (Abrir/Salvar) */ /* Gestor de libraries (tinyblocks-libman); a UI e desenhada aqui */ bool libman_open; @@ -670,6 +706,8 @@ TB_ED_API void clip_set(Ed* ed, TinyBlocks_RenderableBlock* rb, float x, float y TB_ED_API void clip_clear(Ed* ed, TinyBlocks_RenderableBlock* rb); TB_ED_API void clip_restore(Ed* ed, TinyBlocks_RenderableBlock* rb); TB_ED_API void start_path(Ed* ed, int mode, const char* def); +/* Executa a acao do modo PM_* sobre o caminho escolhido (barra ou explorador) */ +TB_ED_API void path_commit(Ed* ed, int mode, const char* path); /* Aba de imagem: library privada carregada por dlopen (hot reload). As chamadas passam pela tabela tb_ed_img, entao trocar o .so em execucao @@ -699,6 +737,25 @@ TB_ED_API void start_path(Ed* ed, int mode, const char* def); # define image_zoom tb_ed_img.zoom #endif +/* Explorador de arquivos: library privada carregada por dlopen */ +#ifdef TB_EDITOR_FILES_BUILD +# define files_open tb_editor_files_open +# define files_close tb_editor_files_close +# define files_view tb_editor_files_view +# define files_click tb_editor_files_click +# define files_key tb_editor_files_key +# define files_text tb_editor_files_text +# define files_scroll tb_editor_files_scroll +#else +# define files_open tb_ed_files.open +# define files_close tb_ed_files.close +# define files_view tb_ed_files.view +# define files_click tb_ed_files.click +# define files_key tb_ed_files.key +# define files_text tb_ed_files.text +# define files_scroll tb_ed_files.scroll +#endif + /* Configuracoes (settings) - UI desenhada pelo editor */ void settings_show(Ed* ed); void settings_click(Ed* ed, float mx, float my); diff --git a/libraries/tinyblocks-editor/internal/tb_editor_modules.h b/libraries/tinyblocks-editor/internal/tb_editor_modules.h index 03405b3..189b1da 100644 --- a/libraries/tinyblocks-editor/internal/tb_editor_modules.h +++ b/libraries/tinyblocks-editor/internal/tb_editor_modules.h @@ -57,6 +57,17 @@ typedef struct { void (*zoom) (Ed*, float, float, int); /* roda do mouse = zoom */ } TbEdImageApi; +/* API da library privada tinyblocks-editor-files (explorador de arquivos) */ +typedef struct { + void (*open) (Ed*, int, const char*); + void (*close) (Ed*); + void (*view) (Ed*, TinyBlocks_RenderableBlock*); + bool (*click) (Ed*, float, float); + void (*key) (Ed*, int, bool); + void (*text) (Ed*, const char*); + void (*scroll)(Ed*, float); +} TbEdFilesApi; + typedef struct { const char* name; /* tinyblocks-editor-image */ const char* file; /* libtinyblocks-editor-image.so */ @@ -79,6 +90,7 @@ typedef struct { /* Tabelas vivas: o editor chama por aqui, entao a troca e instantanea */ TB_ED_API extern TbEdImageApi tb_ed_img; TB_ED_API extern TbEdWidgetsApi tb_ed_w; +TB_ED_API extern TbEdFilesApi tb_ed_files; /* Carrega (ou recarrega) os modulos. Sempre que um .so muda de mtime recarrega todos os modulos, na ordem. Retorna true se algo foi carregado. */ diff --git a/libraries/tinyblocks-editor/libraries/tinyblocks-editor-files/Makefile b/libraries/tinyblocks-editor/libraries/tinyblocks-editor-files/Makefile new file mode 100644 index 0000000..82c167b --- /dev/null +++ b/libraries/tinyblocks-editor/libraries/tinyblocks-editor-files/Makefile @@ -0,0 +1,43 @@ +# Makefile da library privada tinyblocks-editor-files +# Bundle "tinyblocks-editor". Constroi .a e .so. +CC := gcc +BUILD := build + +CFLAGS := -std=c11 -Wall -Wextra -pedantic \ + -DTB_EDITOR_FILES_BUILD \ + -D_POSIX_C_SOURCE=200809L \ + -I../../../../include -Iinclude \ + -I../../internal \ + -I../tinyblocks-editor-widgets/include \ + -I../tinyblocks-editor-image/include \ + -I../../../tinyblocks-graphics/include \ + -I../../../tinyblocks-variables/include \ + -I../../../tinyblocks-blueprint/include \ + -I../../../tinyblocks-environ/include \ + -I../../../tinyblocks-api/include \ + -I../../../tinyblocks-libman/include \ + -I../../../tinyblocks-syntax/include + +OBJS := $(BUILD)/tb_editor_files.o +LIB_A := $(BUILD)/libtinyblocks-editor-files.a +LIB_SO := $(BUILD)/libtinyblocks-editor-files.so + +.PHONY: all static shared clean +all: static shared +static: $(LIB_A) +shared: $(LIB_SO) + +$(BUILD)/tb_editor_files.o: src/tb_editor_files.c include/tb_editor_files.h \ + ../../internal/tb_editor_internal.h \ + ../tinyblocks-editor-widgets/include/tb_editor_widgets.h + @mkdir -p $(BUILD) + $(CC) $(CFLAGS) -fPIC -c $< -o $@ + +$(LIB_A): $(OBJS) + ar rcs $@ $^ + +$(LIB_SO): $(OBJS) + $(CC) -shared -o $@ $^ -Wl,-z,lazy + +clean: + $(RM) -r $(BUILD) diff --git a/libraries/tinyblocks-editor/libraries/tinyblocks-editor-files/include/tb_editor_files.h b/libraries/tinyblocks-editor/libraries/tinyblocks-editor-files/include/tb_editor_files.h new file mode 100644 index 0000000..332a2c9 --- /dev/null +++ b/libraries/tinyblocks-editor/libraries/tinyblocks-editor-files/include/tb_editor_files.h @@ -0,0 +1,43 @@ +/* + * TinyBlocks - tb_editor_files.h + * Explorador de arquivos do editor (library privada do bundle + * tinyblocks-editor): dialogo modal usado por Abrir/Salvar projeto, + * Exportar, abrir/salvar imagem e codigo e carregar library (.so). + * + * O estado mora em Ed (ed->fdlg, ver tb_editor_internal.h), entao o .so + * pode ser recarregado em execucao sem perder a pasta ou a selecao. + * Ao confirmar, o dialogo chama path_commit(ed, mode, caminho) do editor. + */ +#ifndef TB_EDITOR_FILES_H +#define TB_EDITOR_FILES_H + +#include +#include + +#ifdef __cplusplus +extern "C" { +#endif + +struct Ed; +typedef struct Ed Ed; + +/* Abre o dialogo para o modo PM_* com o nome/caminho sugerido `def` + (pode trazer pasta: "dir/arquivo.png"). */ +void tb_editor_files_open (Ed* ed, int mode, const char* def); +/* Fecha sem confirmar (libera a listagem) */ +void tb_editor_files_close (Ed* ed); + +/* Desenha o dialogo (nada se estiver fechado) */ +void tb_editor_files_view (Ed* ed, TinyBlocks_RenderableBlock* rb); + +/* Entrada enquanto o dialogo esta aberto */ +bool tb_editor_files_click (Ed* ed, float mx, float my); +void tb_editor_files_key (Ed* ed, int sc, bool ctrl); +void tb_editor_files_text (Ed* ed, const char* typed); +void tb_editor_files_scroll(Ed* ed, float dy); + +#ifdef __cplusplus +} +#endif + +#endif /* TB_EDITOR_FILES_H */ diff --git a/libraries/tinyblocks-editor/libraries/tinyblocks-editor-files/library.toml b/libraries/tinyblocks-editor/libraries/tinyblocks-editor-files/library.toml new file mode 100644 index 0000000..bdbd26e --- /dev/null +++ b/libraries/tinyblocks-editor/libraries/tinyblocks-editor-files/library.toml @@ -0,0 +1,17 @@ +# Explorador de arquivos do editor (bundle "tinyblocks-editor"). +[library] +name = "tinyblocks-editor-files" +bundle = "tinyblocks-editor" +private = true +version = "1.0.0" +category = "Editor" +description = "Explorador de arquivos: dialogos de Abrir/Salvar/Exportar" +color = [76, 151, 255, 255] +dark = [61, 121, 204, 255] + +[build] +sources = ["src/tb_editor_files.c"] +includes = ["include"] +shared = "build/libtinyblocks-editor-files.so" +static = "build/libtinyblocks-editor-files.a" +depends = ["tinyblocks-editor-widgets"] diff --git a/libraries/tinyblocks-editor/libraries/tinyblocks-editor-files/src/tb_editor_files.c b/libraries/tinyblocks-editor/libraries/tinyblocks-editor-files/src/tb_editor_files.c new file mode 100644 index 0000000..6cb22a2 --- /dev/null +++ b/libraries/tinyblocks-editor/libraries/tinyblocks-editor-files/src/tb_editor_files.c @@ -0,0 +1,962 @@ +/* + * TinyBlocks - tb_editor_files.c + * Explorador de arquivos do editor: dialogo modal estilo Scratch com + * locais (pasta inicial, Documentos, ...), trilha da pasta atual clicavel, + * lista de pastas/arquivos filtrada por extensao, campo de nome e + * confirmacao de substituicao ao salvar. + * + * Library privada do bundle "tinyblocks-editor": o estado mora em + * ed->fdlg, aqui so ha codigo (hot reload troca o .so sem perder nada). + * Ao confirmar chama path_commit(ed, mode, caminho) do editor. + */ +#include +#include +#include +#include +#include +#include + +#include "../../../internal/tb_editor_internal.h" +#include "tb_editor_files.h" + +/* -- Geometria ------------------------------------------------------------ */ +#define FD_HEAD_H 46 +#define FD_BAR_H 44 +#define FD_SIDE_W 196 +#define FD_COLH_H 26 +#define FD_ROW_H 26 +#define FD_FOOT_H 100 +#define FD_PAD 14 +#define FD_BTN_W 104 +#define FD_BTN_H 32 + +static const Col COL_FD_FOLDER = C(255,191, 38); +static const Col COL_FD_FOLDER_DARK = C(222,150, 10); +static const Col COL_FD_SIDE = C(242,246,252); +static const Col COL_FD_JSON = C(255,140, 26); +static const Col COL_FD_IMAGE = C( 15,189,140); +static const Col COL_FD_LIB = C(153,102,255); +static const Col COL_FD_CODE = C( 76,151,255); +static const Col COL_FD_ERR = C(220, 60, 60); + +typedef struct { + float x, y, w, h; + float up_x, bar_y, crumb_x, crumb_w; + float side_x, side_y, side_h; + float list_x, list_y, list_w, list_h, colh_y; + int rows; + float name_x, name_y, name_w; + float row2_y; + float chk_all_x, chk_hid_x; + float ok_x, cancel_x; + float close_cx, close_cy; +} FdL; + +static void fd_layout(Ed* ed, FdL* L) { + float w = WIN_W - 48.f, h = WIN_H - 48.f; + if (w > 900.f) w = 900.f; + if (h > 580.f) h = 580.f; + L->w = w; L->h = h; + L->x = (WIN_W - w) * 0.5f; + L->y = (WIN_H - h) * 0.5f; + L->close_cx = L->x + w - 26.f; + L->close_cy = L->y + FD_HEAD_H * 0.5f; + L->bar_y = L->y + FD_HEAD_H + 7.f; + L->up_x = L->x + FD_PAD; + L->crumb_x = L->up_x + 40.f; + L->crumb_w = L->x + w - FD_PAD - L->crumb_x; + float top = L->y + FD_HEAD_H + FD_BAR_H; + float bot = L->y + h - FD_FOOT_H; + L->side_x = L->x; + L->side_y = top; + L->side_h = bot - top; + L->list_x = L->x + FD_SIDE_W; + L->list_w = w - FD_SIDE_W; + L->colh_y = top; + L->list_y = top + FD_COLH_H; + L->list_h = bot - L->list_y; + L->rows = (int)(L->list_h / FD_ROW_H); + if (L->rows < 1) L->rows = 1; + L->name_x = L->x + FD_PAD + 56.f; + L->name_y = bot + 14.f; + L->name_w = L->x + w - FD_PAD - L->name_x; + L->row2_y = bot + 56.f; + L->chk_all_x = L->x + FD_PAD; + L->chk_hid_x = L->x + FD_PAD + 210.f; + L->ok_x = L->x + w - FD_PAD - FD_BTN_W; + L->cancel_x = L->ok_x - 10.f - FD_BTN_W; +} + +/* -- Utilidades ----------------------------------------------------------- */ + +static unsigned fd_now_ms(void) { + struct timespec ts; + clock_gettime(CLOCK_MONOTONIC, &ts); + return (unsigned)(ts.tv_sec * 1000u + ts.tv_nsec / 1000000u); +} + +static bool fd_is_dir(const char* p) { + struct stat st; + return stat(p, &st) == 0 && S_ISDIR(st.st_mode); +} + +static bool fd_exists(const char* p) { + struct stat st; + return stat(p, &st) == 0; +} + +static void fd_join(char* out, size_t cap, const char* dir, const char* name) { + size_t n = strlen(dir); + if (n > 0 && dir[n - 1] == '/') snprintf(out, cap, "%s%s", dir, name); + else snprintf(out, cap, "%s/%s", dir, name); +} + +/* Normaliza um caminho absoluto: tira "//", "." e resolve ".." */ +static void fd_normalize(char* p) { + char out[1024]; + size_t o = 0; + const char* s = p; + out[o++] = '/'; + while (*s) { + while (*s == '/') s++; + if (!*s) break; + const char* e = s; + while (*e && *e != '/') e++; + size_t len = (size_t)(e - s); + if (len == 1 && s[0] == '.') { + /* ignora */ + } else if (len == 2 && s[0] == '.' && s[1] == '.') { + if (o > 1) { + o--; /* tira a '/' final */ + while (o > 1 && out[o - 1] != '/') o--; + } + } else if (o + len + 1 < sizeof(out)) { + if (o > 1) out[o++] = '/'; + memcpy(out + o, s, len); + o += len; + } + s = e; + } + out[o] = 0; + scopy(p, 1024, out); +} + +/* Resolve o texto do campo (relativo a pasta atual, "~" = HOME) */ +static void fd_resolve(Ed* ed, const char* in, char out[1024]) { + const char* home = getenv("HOME"); + if (in[0] == '/') scopy(out, 1024, in); + else if (in[0] == '~' && (in[1] == '/' || !in[1]) && home) + snprintf(out, 1024, "%s%s", home, in + 1); + else fd_join(out, 1024, ed->fdlg.dir, in); + fd_normalize(out); +} + +static const char* fd_ext(const char* name) { + const char* d = strrchr(name, '.'); + return (d && d != name) ? d : ""; +} + +/* O arquivo passa no filtro (".png .jpg")? */ +static bool fd_match(const FileDlg* F, const char* name) { + if (F->all_files || !F->filter[0]) return true; + const char* e = fd_ext(name); + if (!e[0]) return false; + size_t el = strlen(e); + const char* f = F->filter; + while (*f) { + while (*f == ' ') f++; + const char* fe = f; + while (*fe && *fe != ' ') fe++; + if ((size_t)(fe - f) == el && strncasecmp(f, e, el) == 0) return true; + f = fe; + } + return false; +} + +/* Primeira extensao do filtro (para completar o nome ao salvar) */ +static void fd_first_ext(const FileDlg* F, char* out, size_t cap) { + const char* f = F->filter; + while (*f == ' ') f++; + size_t n = 0; + while (f[n] && f[n] != ' ') n++; + if (n >= cap) n = cap - 1; + memcpy(out, f, n); + out[n] = 0; +} + +/* Corta o texto com "..." para caber em maxw */ +static void fd_fit(char* out, size_t cap, const char* s, float fs, float maxw) { + if (tw(s, fs) <= maxw) { scopy(out, cap, s); return; } + size_t n = strlen(s); + char tmp[512]; + tmp[0] = 0; + while (n > 0) { + n--; + while (n > 0 && ((unsigned char)s[n] & 0xC0) == 0x80) n--; + snprintf(tmp, sizeof(tmp), "%.*s...", (int)n, s); + if (tw(tmp, fs) <= maxw) break; + } + scopy(out, cap, tmp); +} + +static void fd_size_str(char* out, size_t cap, long long sz) { + if (sz < 1024) snprintf(out, cap, "%lld B", sz); + else if (sz < 1024LL * 1024) snprintf(out, cap, "%.1f KB", sz / 1024.0); + else if (sz < 1024LL * 1024 * 1024) snprintf(out, cap, "%.1f MB", sz / (1024.0 * 1024.0)); + else snprintf(out, cap, "%.1f GB", sz / (1024.0 * 1024.0 * 1024.0)); +} + +static void fd_time_str(char* out, size_t cap, long long t) { + time_t tt = (time_t)t; + struct tm tmv; + if (!localtime_r(&tt, &tmv)) { scopy(out, cap, ""); return; } + strftime(out, cap, "%d/%m/%Y %H:%M", &tmv); +} + +/* -- Listagem ------------------------------------------------------------- */ + +static int fd_cmp(const void* a, const void* b) { + const FileEnt* x = a; + const FileEnt* y = b; + if (x->is_dir == 2) return -1; + if (y->is_dir == 2) return 1; + if (x->is_dir != y->is_dir) return y->is_dir - x->is_dir; + return strcasecmp(x->name, y->name); +} + +static bool fd_push(FileDlg* F, const FileEnt* e) { + if (F->n == F->cap) { + int nc = F->cap ? F->cap * 2 : 64; + FileEnt* ne = realloc(F->ents, (size_t)nc * sizeof(FileEnt)); + if (!ne) return false; + F->ents = ne; + F->cap = nc; + } + F->ents[F->n++] = *e; + return true; +} + +static void fd_scan(Ed* ed) { + FileDlg* F = &ed->fdlg; + F->n = 0; + F->sel = -1; + F->scroll = 0; + F->last_click_i = -1; + DIR* d = opendir(F->dir); + if (!d) { + snprintf(F->err, sizeof(F->err), "Nao foi possivel abrir a pasta: %.120s", strerror(errno)); + return; + } + if (strcmp(F->dir, "/") != 0) { + FileEnt up; + memset(&up, 0, sizeof(up)); + scopy(up.name, sizeof(up.name), ".."); + up.is_dir = 2; + fd_push(F, &up); + } + struct dirent* de; + while ((de = readdir(d)) != NULL) { + const char* nm = de->d_name; + if (strcmp(nm, ".") == 0 || strcmp(nm, "..") == 0) continue; + if (nm[0] == '.' && !F->show_hidden) continue; + char full[1300]; + fd_join(full, sizeof(full), F->dir, nm); + struct stat st; + if (stat(full, &st) != 0) continue; /* link quebrado etc */ + FileEnt e; + memset(&e, 0, sizeof(e)); + scopy(e.name, sizeof(e.name), nm); + e.is_dir = S_ISDIR(st.st_mode) ? 1 : 0; + e.size = (long long)st.st_size; + e.mtime = (long long)st.st_mtime; + if (!e.is_dir && !fd_match(F, nm)) continue; + if (!fd_push(F, &e)) break; + } + closedir(d); + qsort(F->ents, (size_t)F->n, sizeof(FileEnt), fd_cmp); + + /* pre-seleciona o arquivo do campo de nome, se estiver na lista */ + for (int i = 0; i < F->n; i++) + if (F->name[0] && strcmp(F->ents[i].name, F->name) == 0) { F->sel = i; break; } +} + +static void fd_chdir(Ed* ed, const char* path) { + FileDlg* F = &ed->fdlg; + char p[1024]; + scopy(p, sizeof(p), path); + fd_normalize(p); + if (!fd_is_dir(p)) { + snprintf(F->err, sizeof(F->err), "Pasta nao encontrada: %.150s", p); + return; + } + scopy(F->dir, sizeof(F->dir), p); + F->err[0] = 0; + F->confirm = false; + fd_scan(ed); +} + +static void fd_parent(Ed* ed) { + char p[1024]; + fd_join(p, sizeof(p), ed->fdlg.dir, ".."); + fd_chdir(ed, p); +} + +static void fd_enter(Ed* ed, int i) { + FileDlg* F = &ed->fdlg; + if (i < 0 || i >= F->n || !F->ents[i].is_dir) return; + if (F->ents[i].is_dir == 2) { fd_parent(ed); return; } + char p[1024]; + fd_join(p, sizeof(p), F->dir, F->ents[i].name); + fd_chdir(ed, p); +} + +/* Locais da barra lateral (so os que existem) */ +static void fd_add_place(FileDlg* F, const char* label, const char* path) { + if (F->place_n >= FD_PLACES || !path || !path[0] || !fd_is_dir(path)) return; + for (int i = 0; i < F->place_n; i++) + if (strcmp(F->place_path[i], path) == 0) return; + scopy(F->place_label[F->place_n], sizeof(F->place_label[0]), label); + scopy(F->place_path[F->place_n], sizeof(F->place_path[0]), path); + F->place_n++; +} + +static void fd_add_home_place(FileDlg* F, const char* home, const char* label, + const char* a, const char* b) { + char p[512]; + snprintf(p, sizeof(p), "%s/%s", home, a); + if (fd_is_dir(p)) { fd_add_place(F, label, p); return; } + if (!b) return; + snprintf(p, sizeof(p), "%s/%s", home, b); + fd_add_place(F, label, p); +} + +static void fd_places(Ed* ed) { + FileDlg* F = &ed->fdlg; + F->place_n = 0; + const char* home = getenv("HOME"); + char cwd[512]; + if (home && home[0]) { + fd_add_place(F, "Pasta pessoal", home); + fd_add_home_place(F, home, "Área de Trabalho", "Área de Trabalho", "Desktop"); + fd_add_home_place(F, home, "Documentos", "Documentos", "Documents"); + fd_add_home_place(F, home, "Downloads", "Downloads", NULL); + fd_add_home_place(F, home, "Imagens", "Imagens", "Pictures"); + } + if (getcwd(cwd, sizeof(cwd))) fd_add_place(F, "Pasta de trabalho", cwd); + fd_add_place(F, "Raiz do sistema", "/"); +} + +/* -- Trilha da pasta atual (segmentos clicaveis) ------------------------- */ +#define FD_MAX_CRUMBS 48 +typedef struct { + int n, first; /* segmentos; primeiro visivel */ + int start[FD_MAX_CRUMBS], end[FD_MAX_CRUMBS]; + float x[FD_MAX_CRUMBS], w[FD_MAX_CRUMBS]; + bool ellipsis; +} FdCrumbs; + +static void fd_crumb_label(const FileDlg* F, const FdCrumbs* K, int i, char* out, size_t cap) { + if (i == 0) { scopy(out, cap, "/"); return; } + int len = K->end[i] - K->start[i]; + snprintf(out, cap, "%.*s", len, F->dir + K->start[i]); +} + +#define FD_CRUMB_SEP 14.f +static void fd_crumbs(Ed* ed, const FdL* L, FdCrumbs* K) { + FileDlg* F = &ed->fdlg; + K->n = 0; + K->start[0] = 0; K->end[0] = 1; /* raiz */ + K->n = 1; + const char* d = F->dir; + int i = 1; + while (d[i] && K->n < FD_MAX_CRUMBS) { + while (d[i] == '/') i++; + if (!d[i]) break; + int s = i; + while (d[i] && d[i] != '/') i++; + K->start[K->n] = s; + K->end[K->n] = i; + K->n++; + } + float avail = L->crumb_w - 12.f; + float wsum = 0; + K->first = K->n - 1; + for (int k = K->n - 1; k >= 0; k--) { + char lb[256]; + fd_crumb_label(F, K, k, lb, sizeof(lb)); + float w = tw(lb, FS_UI) + 16.f; + if (w > avail - 40.f) w = avail - 40.f; + K->w[k] = w; + float need = wsum + w + (k < K->n - 1 ? FD_CRUMB_SEP : 0); + if (k < K->n - 1 && need > avail - 40.f) break; + wsum = need; + K->first = k; + } + K->ellipsis = K->first > 0; + float x = L->crumb_x + 6.f + (K->ellipsis ? 30.f : 0.f); + for (int k = K->first; k < K->n; k++) { + K->x[k] = x; + x += K->w[k] + (k == 0 ? 2.f : FD_CRUMB_SEP); + } +} + +/* -- Acoes ---------------------------------------------------------------- */ + +static void fd_ensure_visible(Ed* ed) { + FileDlg* F = &ed->fdlg; + FdL L; + fd_layout(ed, &L); + if (F->sel < 0) return; + if (F->sel < F->scroll) F->scroll = F->sel; + if (F->sel >= F->scroll + L.rows) F->scroll = F->sel - L.rows + 1; + if (F->scroll < 0) F->scroll = 0; +} + +static void fd_free(FileDlg* F) { + free(F->ents); + F->ents = NULL; + F->n = F->cap = 0; +} + +void tb_editor_files_close(Ed* ed) { + FileDlg* F = &ed->fdlg; + F->open = false; + F->confirm = false; + fd_free(F); +} + +static void fd_commit(Ed* ed, const char* full) { + FileDlg* F = &ed->fdlg; + int mode = F->mode; + char path[1024]; + scopy(path, sizeof(path), full); + /* lembra a pasta do arquivo escolhido para a proxima vez */ + char* sl = strrchr(path, '/'); + if (sl) { + if (sl == path) scopy(F->dir, sizeof(F->dir), "/"); + else snprintf(F->dir, sizeof(F->dir), "%.*s", (int)(sl - path), path); + } + tb_editor_files_close(ed); + path_commit(ed, mode, path); +} + +/* Confirma: entra em pasta, ou abre/salva o arquivo do campo de nome. + force = ja confirmou "substituir". */ +static void fd_accept(Ed* ed, bool force) { + FileDlg* F = &ed->fdlg; + F->err[0] = 0; + if (!F->name[0]) { + if (F->sel >= 0 && F->sel < F->n && F->ents[F->sel].is_dir) fd_enter(ed, F->sel); + return; + } + char full[1024]; + fd_resolve(ed, F->name, full); + if (fd_is_dir(full)) { /* digitou uma pasta: entra nela */ + fd_chdir(ed, full); + scopy(F->name, sizeof(F->name), F->save ? F->def_name : ""); + return; + } + if (F->save) { + const char* base = strrchr(full, '/'); + base = base ? base + 1 : full; + if (!fd_ext(base)[0] && F->filter[0] && !F->all_files) { + char ext[24]; + fd_first_ext(F, ext, sizeof(ext)); + size_t n = strlen(full); + if (n + strlen(ext) < sizeof(full)) memcpy(full + n, ext, strlen(ext) + 1); + } + char parent[1024]; + scopy(parent, sizeof(parent), full); + char* sl = strrchr(parent, '/'); + if (sl) { if (sl == parent) sl[1] = 0; else *sl = 0; } + if (!fd_is_dir(parent)) { + snprintf(F->err, sizeof(F->err), "A pasta nao existe: %.150s", parent); + return; + } + if (fd_exists(full) && !force) { + const char* b = strrchr(full, '/'); + scopy(F->name, sizeof(F->name), F->name[0] == '/' ? full : (b ? b + 1 : full)); + F->confirm = true; + return; + } + } else if (!fd_exists(full)) { + snprintf(F->err, sizeof(F->err), "Arquivo nao encontrado: %.150s", F->name); + return; + } + fd_commit(ed, full); +} + +static void fd_mode_info(FileDlg* F, int mode) { + const char* title = "Abrir arquivo"; + const char* ok = "Abrir"; + const char* filter = ""; + bool save = false; + switch (mode) { + case PM_LIB: title = "Carregar library"; ok = "Carregar"; filter = ".so"; break; + case PM_LOAD: title = "Abrir projeto"; filter = ".json"; break; + case PM_SAVE: title = "Salvar projeto"; ok = "Salvar"; filter = ".json"; save = true; break; + case PM_EXP_JSON: title = "Exportar como JSON"; ok = "Exportar"; filter = ".json"; save = true; break; + case PM_EXP_XML: title = "Exportar como XML"; ok = "Exportar"; filter = ".xml"; save = true; break; + case PM_EXP_CSV: title = "Exportar como CSV"; ok = "Exportar"; filter = ".csv"; save = true; break; + case PM_IMGLOAD: title = "Abrir imagem"; filter = ".png .jpg .jpeg .bmp .ppm .svg"; break; + case PM_IMSAVE: title = "Salvar imagem"; ok = "Salvar"; filter = ".png .jpg .jpeg .bmp .ppm"; save = true; break; + case PM_IMADDR: title = "Adicionar camada raster"; ok = "Adicionar"; filter = ".png .jpg .jpeg .bmp .ppm .svg"; break; + case PM_IMADDV: title = "Adicionar camada vetorial"; ok = "Adicionar"; filter = ".svg"; break; + case PM_CODELOAD: title = "Abrir código"; break; + case PM_CODESAVE: title = "Salvar código"; ok = "Salvar"; save = true; break; + default: break; + } + scopy(F->title, sizeof(F->title), title); + scopy(F->ok_label, sizeof(F->ok_label), ok); + scopy(F->filter, sizeof(F->filter), filter); + F->save = save; +} + +void tb_editor_files_open(Ed* ed, int mode, const char* def) { + FileDlg* F = &ed->fdlg; + fd_free(F); + fd_mode_info(F, mode); + F->open = true; + F->mode = mode; + F->confirm = false; + F->all_files = false; + F->err[0] = 0; + + char cwd[1024]; + if (!getcwd(cwd, sizeof(cwd))) scopy(cwd, sizeof(cwd), "/"); + if (!F->dir[0] || !fd_is_dir(F->dir)) scopy(F->dir, sizeof(F->dir), cwd); + + /* o nome sugerido pode trazer a pasta ("imgs/saida.png") */ + const char* base = def ? def : ""; + const char* sl = strrchr(base, '/'); + if (sl) { + char d[2048]; + if (base[0] == '/') snprintf(d, sizeof(d), "%.*s", (int)(sl - base), base); + else snprintf(d, sizeof(d), "%s/%.*s", cwd, (int)(sl - base), base); + if (!d[0]) scopy(d, sizeof(d), "/"); + fd_normalize(d); + if (fd_is_dir(d)) scopy(F->dir, sizeof(F->dir), d); + base = sl + 1; + } + scopy(F->def_name, sizeof(F->def_name), base); + scopy(F->name, sizeof(F->name), base); + + fd_places(ed); + fd_scan(ed); + /* abrir: so sugere o nome se o arquivo estiver aqui */ + if (!F->save && F->sel < 0) F->name[0] = 0; + fd_ensure_visible(ed); +} + +/* -- Desenho -------------------------------------------------------------- */ + +static void fd_icon_folder(TinyBlocks_RenderableBlock* rb, float x, float y, bool dim) { + Col c = COL_FD_FOLDER, d = COL_FD_FOLDER_DARK; + if (dim) { c.a = 0.55f; d.a = 0.55f; } + rb_fill_rrect(rb, x, y + 1, 9, 6, 2, d); + rb_fill_rrect(rb, x, y + 4, 19, 13, 2, c); +} + +static Col fd_type_col(const char* name) { + const char* e = fd_ext(name); + if (!strcasecmp(e, ".json") || !strcasecmp(e, ".xml") || !strcasecmp(e, ".csv")) return COL_FD_JSON; + if (!strcasecmp(e, ".png") || !strcasecmp(e, ".jpg") || !strcasecmp(e, ".jpeg") || + !strcasecmp(e, ".bmp") || !strcasecmp(e, ".ppm") || !strcasecmp(e, ".svg")) return COL_FD_IMAGE; + if (!strcasecmp(e, ".so")) return COL_FD_LIB; + if (!strcasecmp(e, ".c") || !strcasecmp(e, ".h") || !strcasecmp(e, ".py") || + !strcasecmp(e, ".md") || !strcasecmp(e, ".toml")) return COL_FD_CODE; + return COL_TEXT_DIM; +} + +static void fd_icon_file(TinyBlocks_RenderableBlock* rb, float x, float y, Col band) { + rb_fill_rrect(rb, x + 2, y, 15, 18, 2, COL_BORDER); + rb_fill_rrect(rb, x + 3, y + 1, 13, 16, 1.5f, COL_WHITE); + rb_fill_rect(rb, x + 3, y + 11, 13, 5, band); + rb_line(rb, x + 6, y + 5, x + 13, y + 5, 1, COL_BORDER); + rb_line(rb, x + 6, y + 8, x + 13, y + 8, 1, COL_BORDER); +} + +static void fd_checkbox(TinyBlocks_RenderableBlock* rb, float x, float cy, bool on, const char* label) { + float s = 16; + if (on) { + rb_fill_rrect(rb, x, cy - s * 0.5f, s, s, 3, COL_MENU); + rb_line(rb, x + 4, cy, x + 7, cy + 4, 2, COL_WHITE); + rb_line(rb, x + 7, cy + 4, x + 12, cy - 4, 2, COL_WHITE); + } else { + rb_fill_rrect(rb, x, cy - s * 0.5f, s, s, 3, COL_BORDER); + rb_fill_rrect(rb, x + 1.5f, cy - s * 0.5f + 1.5f, s - 3, s - 3, 2, COL_WHITE); + } + rb_text_c(rb, x + s + 8, cy, label, COL_TEXT, FS_UI); +} + +static void fd_button(TinyBlocks_RenderableBlock* rb, float x, float y, float w, float h, + const char* label, bool primary, bool hov, Col accent) { + if (primary) { + rb_fill_rrect(rb, x, y, w, h, h * 0.5f, hov ? col_mul(accent, 0.86f) : accent); + rb_text_c(rb, x + (w - tw(label, FS_UI)) * 0.5f, y + h * 0.5f, label, COL_WHITE, FS_UI); + } else { + rb_fill_rrect(rb, x, y, w, h, h * 0.5f, COL_BORDER); + rb_fill_rrect(rb, x + 1, y + 1, w - 2, h - 2, h * 0.5f - 1, hov ? COL_HOVER : COL_WHITE); + rb_text_c(rb, x + (w - tw(label, FS_UI)) * 0.5f, y + h * 0.5f, label, COL_TEXT, FS_UI); + } +} + +/* Caixa de confirmacao "substituir?" (geometria compartilhada com o clique) */ +static void fd_confirm_geom(const FdL* L, float* bx, float* by, float* bw, float* bh) { + *bw = 420; *bh = 150; + *bx = L->x + (L->w - *bw) * 0.5f; + *by = L->y + (L->h - *bh) * 0.5f; +} + +void tb_editor_files_view(Ed* ed, TinyBlocks_RenderableBlock* rb) { + FileDlg* F = &ed->fdlg; + if (!F->open) return; + FdL L; + fd_layout(ed, &L); + float mx = ed->mouse_x, my = ed->mouse_y; + bool modal_free = !F->confirm; + char buf[512]; + + rb_fill_rect(rb, 0, 0, WIN_W, WIN_H, COLA(0, 0, 0, 120)); + rb_fill_rrect(rb, L.x + 3, L.y + 4, L.w, L.h, 12, COLA(0, 0, 0, 70)); + rb_fill_rrect(rb, L.x, L.y, L.w, L.h, 12, COL_PANEL); + + /* Cabecalho */ + rb_fill_rrect(rb, L.x, L.y, L.w, FD_HEAD_H, 12, COL_MENU); + rb_fill_rect(rb, L.x, L.y + FD_HEAD_H - 12, L.w, 12, COL_MENU); + rb_text_c(rb, L.x + (L.w - tw(F->title, 18)) * 0.5f, L.y + FD_HEAD_H * 0.5f, F->title, COL_WHITE, 18); + { + bool hov = modal_free && hypotf(mx - L.close_cx, my - L.close_cy) < 14.f; + rb_fill_circle(rb, L.close_cx, L.close_cy, 13, COLA(255, 255, 255, hov ? 110 : 60)); + rb_line(rb, L.close_cx - 5, L.close_cy - 5, L.close_cx + 5, L.close_cy + 5, 2, COL_WHITE); + rb_line(rb, L.close_cx + 5, L.close_cy - 5, L.close_cx - 5, L.close_cy + 5, 2, COL_WHITE); + } + + /* Barra: pasta acima + trilha */ + { + float by = L.bar_y, bh = 30; + bool hov = modal_free && in_rect(mx, my, L.up_x, by, 32, bh); + bool can_up = strcmp(F->dir, "/") != 0; + rb_fill_rrect(rb, L.up_x, by, 32, bh, 6, hov && can_up ? COL_HOVER : COL_UI_BG); + Col ac = can_up ? COL_TEXT : COL_TEXT_DIM; + float cx = L.up_x + 16, cy = by + bh * 0.5f; + rb_line(rb, cx, cy + 7, cx, cy - 7, 2, ac); + rb_line(rb, cx - 6, cy - 1, cx, cy - 7, 2, ac); + rb_line(rb, cx + 6, cy - 1, cx, cy - 7, 2, ac); + + rb_fill_rrect(rb, L.crumb_x, by, L.crumb_w, bh, 6, COL_UI_BG); + FdCrumbs K; + fd_crumbs(ed, &L, &K); + if (K.ellipsis) + rb_text_c(rb, L.crumb_x + 10, cy, "...", COL_TEXT_DIM, FS_UI); + for (int k = K.first; k < K.n; k++) { + char lb[256], fit[256]; + fd_crumb_label(F, &K, k, lb, sizeof(lb)); + fd_fit(fit, sizeof(fit), lb, FS_UI, K.w[k] - 16.f); + bool last = k == K.n - 1; + bool h = modal_free && !last && in_rect(mx, my, K.x[k], by + 3, K.w[k], bh - 6); + if (last) rb_fill_rrect(rb, K.x[k], by + 3, K.w[k], bh - 6, 5, COL_WHITE); + else if (h) rb_fill_rrect(rb, K.x[k], by + 3, K.w[k], bh - 6, 5, COL_HOVER); + rb_text_c(rb, K.x[k] + 8, cy, fit, last ? COL_TEXT : COL_MENU_DARK, FS_UI); + if (!last && k > 0) rb_text_c(rb, K.x[k] + K.w[k] + 3, cy, "/", COL_TEXT_DIM, FS_UI); + } + } + + /* Barra lateral: locais */ + rb_fill_rect(rb, L.side_x, L.side_y, FD_SIDE_W, L.side_h, COL_FD_SIDE); + rb_line(rb, L.side_x + FD_SIDE_W, L.side_y, L.side_x + FD_SIDE_W, L.side_y + L.side_h, 1, COL_BORDER); + rb_line(rb, L.x, L.side_y, L.x + L.w, L.side_y, 1, COL_BORDER); + rb_text_c(rb, L.side_x + FD_PAD, L.side_y + 16, "Locais", COL_TEXT_DIM, FS_SM); + for (int i = 0; i < F->place_n; i++) { + float iy = L.side_y + 30 + i * 32; + bool cur = strcmp(F->dir, F->place_path[i]) == 0; + bool h = modal_free && in_rect(mx, my, L.side_x + 6, iy, FD_SIDE_W - 12, 30); + if (cur || h) rb_fill_rrect(rb, L.side_x + 6, iy, FD_SIDE_W - 12, 30, 6, cur ? COL_HOVER : COLA(214, 228, 248, 140)); + if (cur) rb_fill_rrect(rb, L.side_x + 6, iy + 6, 3, 18, 1.5f, COL_MENU); + fd_icon_folder(rb, L.side_x + 16, iy + 6, false); + fd_fit(buf, sizeof(buf), F->place_label[i], FS_UI, FD_SIDE_W - 60); + rb_text_c(rb, L.side_x + 42, iy + 15, buf, cur ? COL_MENU_DARK : COL_TEXT, FS_UI); + } + + /* Cabecalho das colunas */ + float col_size = L.list_x + L.list_w - 250.f; + float col_time = L.list_x + L.list_w - 140.f; + rb_text_c(rb, L.list_x + 44, L.colh_y + FD_COLH_H * 0.5f, "Nome", COL_TEXT_DIM, FS_SM); + rb_text_c(rb, col_size, L.colh_y + FD_COLH_H * 0.5f, "Tamanho", COL_TEXT_DIM, FS_SM); + rb_text_c(rb, col_time, L.colh_y + FD_COLH_H * 0.5f, "Modificado", COL_TEXT_DIM, FS_SM); + rb_line(rb, L.list_x, L.list_y, L.list_x + L.list_w, L.list_y, 1, COL_BORDER); + + /* Linhas */ + int end = F->scroll + L.rows; + if (end > F->n) end = F->n; + for (int i = F->scroll; i < end; i++) { + FileEnt* e = &F->ents[i]; + float ry = L.list_y + (float)(i - F->scroll) * FD_ROW_H; + bool sel = i == F->sel; + bool h = modal_free && in_rect(mx, my, L.list_x, ry, L.list_w - 12, FD_ROW_H); + if (sel) rb_fill_rrect(rb, L.list_x + 4, ry + 1, L.list_w - 18, FD_ROW_H - 2, 5, COL_MENU); + else if (h) rb_fill_rrect(rb, L.list_x + 4, ry + 1, L.list_w - 18, FD_ROW_H - 2, 5, COL_HOVER); + Col tc = sel ? COL_WHITE : COL_TEXT; + Col dc = sel ? COLA(255, 255, 255, 210) : COL_TEXT_DIM; + if (e->is_dir) fd_icon_folder(rb, L.list_x + 14, ry + 4, e->is_dir == 2); + else fd_icon_file(rb, L.list_x + 14, ry + 4, fd_type_col(e->name)); + fd_fit(buf, sizeof(buf), e->is_dir == 2 ? ".. (pasta acima)" : e->name, FS_UI, + col_size - (L.list_x + 44) - 16); + rb_text_c(rb, L.list_x + 44, ry + FD_ROW_H * 0.5f, buf, tc, FS_UI); + if (e->is_dir != 2) { + char s[48]; + if (e->is_dir) scopy(s, sizeof(s), "pasta"); + else fd_size_str(s, sizeof(s), e->size); + rb_text_c(rb, col_size, ry + FD_ROW_H * 0.5f, s, dc, FS_SM); + fd_time_str(s, sizeof(s), e->mtime); + rb_text_c(rb, col_time, ry + FD_ROW_H * 0.5f, s, dc, FS_SM); + } + } + bool only_parent = F->n == 0 || (F->n == 1 && F->ents[0].is_dir == 2); + if (only_parent && !F->err[0]) { + const char* msg = F->filter[0] && !F->all_files ? "Nenhum arquivo compatível nesta pasta" + : "Pasta vazia"; + rb_text_c(rb, L.list_x + (L.list_w - tw(msg, FS_UI)) * 0.5f, + L.list_y + L.list_h * 0.5f, msg, COL_TEXT_DIM, FS_UI); + } + /* Barra de rolagem */ + if (F->n > L.rows) { + float tx = L.list_x + L.list_w - 10, th = L.list_h - 8; + float kh = th * (float)L.rows / (float)F->n; + if (kh < 24) kh = 24; + float ky = L.list_y + 4 + (th - kh) * (float)F->scroll / (float)(F->n - L.rows); + rb_fill_rrect(rb, tx, L.list_y + 4, 6, th, 3, COL_UI_BG); + rb_fill_rrect(rb, tx, ky, 6, kh, 3, COL_TEXT_DIM); + } + if (F->err[0]) { + float ey = L.list_y + L.list_h - 30; + rb_fill_rrect(rb, L.list_x + 8, ey, L.list_w - 24, 24, 6, COLA(220, 60, 60, 30)); + fd_fit(buf, sizeof(buf), F->err, FS_UI, L.list_w - 48); + rb_text_c(rb, L.list_x + 18, ey + 12, buf, COL_FD_ERR, FS_UI); + } + + /* Rodape: nome + opcoes + botoes */ + float foot = L.y + L.h - FD_FOOT_H; + rb_line(rb, L.x, foot, L.x + L.w, foot, 1, COL_BORDER); + rb_text_c(rb, L.x + FD_PAD, L.name_y + 15, "Nome:", COL_TEXT, FS_UI); + rb_fill_rrect(rb, L.name_x, L.name_y, L.name_w, 30, 6, COL_MENU); + rb_fill_rrect(rb, L.name_x + 1.5f, L.name_y + 1.5f, L.name_w - 3, 27, 5, COL_WHITE); + { + float maxw = L.name_w - 24; + const char* s = F->name; + while (*s && tw(s, FS_UI) > maxw) { /* mostra o fim do texto */ + s++; + while ((*s & 0xC0) == 0x80) s++; + } + if (F->name[0]) rb_text_c(rb, L.name_x + 10, L.name_y + 15, s, COL_TEXT, FS_UI); + else rb_text_c(rb, L.name_x + 10, L.name_y + 15, + F->save ? "nome do arquivo" : "escolha um arquivo ou digite o caminho", + COL_TEXT_DIM, FS_UI); + float cw = F->name[0] ? tw(s, FS_UI) : 0; + if ((fd_now_ms() / 530u) % 2u == 0u) + rb_fill_rect(rb, L.name_x + 11 + cw, L.name_y + 7, 1.5f, 16, COL_TEXT); + } + float r2 = L.row2_y + FD_BTN_H * 0.5f; + if (F->filter[0]) { + snprintf(buf, sizeof(buf), "Todos os arquivos"); + fd_checkbox(rb, L.chk_all_x, r2, F->all_files, buf); + if (!F->all_files) { + char f2[120]; + snprintf(f2, sizeof(f2), "(%s)", F->filter); + fd_fit(buf, sizeof(buf), f2, FS_SM, L.chk_hid_x - L.chk_all_x - 160); + rb_text_c(rb, L.chk_all_x + 24 + tw("Todos os arquivos", FS_UI) + 8, r2, buf, COL_TEXT_DIM, FS_SM); + } + } + fd_checkbox(rb, L.chk_hid_x, r2, F->show_hidden, "Mostrar ocultos"); + fd_button(rb, L.cancel_x, L.row2_y, FD_BTN_W, FD_BTN_H, "Cancelar", false, + modal_free && in_rect(mx, my, L.cancel_x, L.row2_y, FD_BTN_W, FD_BTN_H), COL_MENU); + fd_button(rb, L.ok_x, L.row2_y, FD_BTN_W, FD_BTN_H, F->ok_label, true, + modal_free && in_rect(mx, my, L.ok_x, L.row2_y, FD_BTN_W, FD_BTN_H), COL_MENU); + + /* Confirmacao de substituicao */ + if (F->confirm) { + rb_fill_rrect(rb, L.x, L.y, L.w, L.h, 12, COLA(0, 0, 0, 60)); + float bx, by, bw, bh; + fd_confirm_geom(&L, &bx, &by, &bw, &bh); + rb_fill_rrect(rb, bx + 2, by + 3, bw, bh, 10, COLA(0, 0, 0, 60)); + rb_fill_rrect(rb, bx, by, bw, bh, 10, COL_PANEL); + const char* b = strrchr(F->name, '/'); + b = b ? b + 1 : F->name; + char q[400]; + snprintf(q, sizeof(q), "\"%s\" já existe.", b); + fd_fit(buf, sizeof(buf), q, 15, bw - 40); + rb_text_c(rb, bx + 20, by + 34, buf, COL_TEXT, 15); + rb_text_c(rb, bx + 20, by + 60, "Deseja substituir o arquivo?", COL_TEXT_DIM, FS_UI); + float yb = by + bh - FD_BTN_H - 16; + float xo = bx + bw - 20 - FD_BTN_W, xc = xo - 10 - FD_BTN_W; + fd_button(rb, xc, yb, FD_BTN_W, FD_BTN_H, "Cancelar", false, + in_rect(mx, my, xc, yb, FD_BTN_W, FD_BTN_H), COL_MENU); + fd_button(rb, xo, yb, FD_BTN_W, FD_BTN_H, "Substituir", true, + in_rect(mx, my, xo, yb, FD_BTN_W, FD_BTN_H), COL_STOP); + } +} + +/* -- Entrada -------------------------------------------------------------- */ + +static void fd_rescan_keep(Ed* ed) { + FileDlg* F = &ed->fdlg; + char nm[256]; + scopy(nm, sizeof(nm), F->name); + fd_scan(ed); + scopy(F->name, sizeof(F->name), nm); + fd_ensure_visible(ed); +} + +bool tb_editor_files_click(Ed* ed, float mx, float my) { + FileDlg* F = &ed->fdlg; + if (!F->open) return false; + FdL L; + fd_layout(ed, &L); + + if (F->confirm) { + float bx, by, bw, bh; + fd_confirm_geom(&L, &bx, &by, &bw, &bh); + float yb = by + bh - FD_BTN_H - 16; + float xo = bx + bw - 20 - FD_BTN_W; + if (in_rect(mx, my, xo, yb, FD_BTN_W, FD_BTN_H)) fd_accept(ed, true); + else if (!in_rect(mx, my, bx, by, bw, bh) || in_rect(mx, my, xo - 10 - FD_BTN_W, yb, FD_BTN_W, FD_BTN_H)) + F->confirm = false; + return true; + } + if (!in_rect(mx, my, L.x, L.y, L.w, L.h)) return true; /* modal */ + F->err[0] = 0; + + if (hypotf(mx - L.close_cx, my - L.close_cy) < 14.f) { tb_editor_files_close(ed); return true; } + + /* barra: pasta acima / trilha */ + if (in_rect(mx, my, L.up_x, L.bar_y, 32, 30)) { fd_parent(ed); return true; } + if (in_rect(mx, my, L.crumb_x, L.bar_y, L.crumb_w, 30)) { + FdCrumbs K; + fd_crumbs(ed, &L, &K); + for (int k = K.first; k < K.n - 1; k++) { + if (in_rect(mx, my, K.x[k], L.bar_y, K.w[k], 30)) { + char p[1024]; + snprintf(p, sizeof(p), "%.*s", K.end[k], F->dir); + if (!p[0]) scopy(p, sizeof(p), "/"); + fd_chdir(ed, p); + return true; + } + } + return true; + } + + /* locais */ + if (in_rect(mx, my, L.side_x, L.side_y, FD_SIDE_W, L.side_h)) { + for (int i = 0; i < F->place_n; i++) { + float iy = L.side_y + 30 + i * 32; + if (in_rect(mx, my, L.side_x + 6, iy, FD_SIDE_W - 12, 30)) { + fd_chdir(ed, F->place_path[i]); + return true; + } + } + return true; + } + + /* lista */ + if (in_rect(mx, my, L.list_x, L.list_y, L.list_w - 12, L.list_h)) { + int i = F->scroll + (int)((my - L.list_y) / FD_ROW_H); + if (i < 0 || i >= F->n || i >= F->scroll + L.rows) return true; + unsigned t = fd_now_ms(); + bool dbl = i == F->last_click_i && (t - F->last_click_ms) <= 400u; + F->last_click_i = i; + F->last_click_ms = t; + F->sel = i; + FileEnt* e = &F->ents[i]; + if (e->is_dir) { + if (dbl) fd_enter(ed, i); + else if (!F->save) F->name[0] = 0; + return true; + } + scopy(F->name, sizeof(F->name), e->name); + if (dbl) fd_accept(ed, false); + return true; + } + + /* rodape */ + float r2 = L.row2_y; + if (F->filter[0] && in_rect(mx, my, L.chk_all_x, r2, 24 + tw("Todos os arquivos", FS_UI), FD_BTN_H)) { + F->all_files = !F->all_files; + fd_rescan_keep(ed); + return true; + } + if (in_rect(mx, my, L.chk_hid_x, r2, 24 + tw("Mostrar ocultos", FS_UI), FD_BTN_H)) { + F->show_hidden = !F->show_hidden; + fd_rescan_keep(ed); + return true; + } + if (in_rect(mx, my, L.cancel_x, r2, FD_BTN_W, FD_BTN_H)) { tb_editor_files_close(ed); return true; } + if (in_rect(mx, my, L.ok_x, r2, FD_BTN_W, FD_BTN_H)) { fd_accept(ed, false); return true; } + return true; +} + +static void fd_pop(char* s) { + size_t n = strlen(s); + if (!n) return; + n--; + while (n > 0 && ((unsigned char)s[n] & 0xC0) == 0x80) n--; + s[n] = 0; +} + +void tb_editor_files_key(Ed* ed, int sc, bool ctrl) { + FileDlg* F = &ed->fdlg; + if (!F->open) return; + if (F->confirm) { + if (sc == TB_KEY_ESCAPE) F->confirm = false; + else if (sc == TB_KEY_RETURN) fd_accept(ed, true); + return; + } + if (ctrl && sc == TB_KEY_A + ('h' - 'a')) { + F->show_hidden = !F->show_hidden; + fd_rescan_keep(ed); + return; + } + switch (sc) { + case TB_KEY_ESCAPE: tb_editor_files_close(ed); return; + case TB_KEY_RETURN: fd_accept(ed, false); return; + case TB_KEY_BACKSPACE: fd_pop(F->name); F->err[0] = 0; return; + case TB_KEY_UP: + case TB_KEY_DOWN: + case TB_KEY_HOME: + case TB_KEY_END: { + if (F->n == 0) return; + int s = F->sel; + if (sc == TB_KEY_UP) s = s < 0 ? F->n - 1 : s - 1; + else if (sc == TB_KEY_DOWN) s = s < 0 ? 0 : s + 1; + else if (sc == TB_KEY_HOME) s = 0; + else s = F->n - 1; + if (s < 0) s = 0; + if (s >= F->n) s = F->n - 1; + F->sel = s; + F->last_click_i = -1; + if (!F->ents[s].is_dir) scopy(F->name, sizeof(F->name), F->ents[s].name); + else if (!F->save) F->name[0] = 0; + fd_ensure_visible(ed); + return; + } + default: return; + } +} + +void tb_editor_files_text(Ed* ed, const char* typed) { + FileDlg* F = &ed->fdlg; + if (!F->open || F->confirm) return; + size_t n = strlen(F->name), a = strlen(typed); + if (n + a < sizeof(F->name)) memcpy(F->name + n, typed, a + 1); + F->err[0] = 0; +} + +void tb_editor_files_scroll(Ed* ed, float dy) { + FileDlg* F = &ed->fdlg; + if (!F->open) return; + FdL L; + fd_layout(ed, &L); + F->scroll -= (int)(dy * 3.f); + int mx = F->n - L.rows; + if (F->scroll > mx) F->scroll = mx; + if (F->scroll < 0) F->scroll = 0; +} diff --git a/libraries/tinyblocks-editor/libraries/tinyblocks-editor-image/Makefile b/libraries/tinyblocks-editor/libraries/tinyblocks-editor-image/Makefile index 20248ae..078ae92 100644 --- a/libraries/tinyblocks-editor/libraries/tinyblocks-editor-image/Makefile +++ b/libraries/tinyblocks-editor/libraries/tinyblocks-editor-image/Makefile @@ -9,6 +9,7 @@ CFLAGS := -std=c11 -Wall -Wextra -pedantic \ -I../../../../include -Iinclude \ -I../../internal \ -I../tinyblocks-editor-widgets/include \ + -I../tinyblocks-editor-files/include \ -I../../../tinyblocks-graphics/include \ -I../../../tinyblocks-variables/include \ -I../../../tinyblocks-blueprint/include \ diff --git a/libraries/tinyblocks-editor/library.toml b/libraries/tinyblocks-editor/library.toml index 26b5f18..5fc0cc0 100644 --- a/libraries/tinyblocks-editor/library.toml +++ b/libraries/tinyblocks-editor/library.toml @@ -8,11 +8,11 @@ color = [255, 102, 128, 255] dark = [255, 51, 85, 255] # O editor e um bundle: as libraries privadas em libraries/ vem junto. -bundle_members = ["tinyblocks-editor-widgets", "tinyblocks-editor-image"] +bundle_members = ["tinyblocks-editor-widgets", "tinyblocks-editor-image", "tinyblocks-editor-files"] [build] sources = ["src/tb_editor.c", "src/tb_stage_widget.c"] includes = ["include"] shared = "build/libtinyblocks-editor.so" static = "build/libtinyblocks-editor.a" -depends = ["tinyblocks-editor-widgets", "tinyblocks-editor-image"] +depends = ["tinyblocks-editor-widgets", "tinyblocks-editor-image", "tinyblocks-editor-files"] diff --git a/libraries/tinyblocks-editor/src/tb_editor.c b/libraries/tinyblocks-editor/src/tb_editor.c index 41509e2..c8e899b 100644 --- a/libraries/tinyblocks-editor/src/tb_editor.c +++ b/libraries/tinyblocks-editor/src/tb_editor.c @@ -2239,8 +2239,8 @@ static void render_menu(Ed* ed, TinyBlocks_RenderableBlock* rb) { float mx = ed->mouse_x, my = ed->mouse_y; bool active = ed->path_input_active; - menu_button(rb, OPEN_BTN_X, OPEN_BTN_W, "Abrir", (active && ed->path_mode == PM_LOAD) || hit_open_btn(mx, my)); - menu_button(rb, SAVE_BTN_X, SAVE_BTN_W, "Salvar", (active && ed->path_mode == PM_SAVE) || hit_save_btn(mx, my)); + menu_button(rb, OPEN_BTN_X, OPEN_BTN_W, "Abrir", (active && ed->path_mode == PM_LOAD) || (ed->fdlg.open && ed->fdlg.mode == PM_LOAD) || hit_open_btn(mx, my)); + menu_button(rb, SAVE_BTN_X, SAVE_BTN_W, "Salvar", (active && ed->path_mode == PM_SAVE) || (ed->fdlg.open && ed->fdlg.mode == PM_SAVE) || hit_save_btn(mx, my)); menu_button(rb, EXP_BTN_X, EXP_BTN_W, "Exportar", (ed->ctx_menu_open && ed->ctx_export) || hit_export_btn(mx, my)); menu_button(rb, LIBM_BTN_X, LIBM_BTN_W, "Bibliotecas", ed->libman_open || hit_libman_btn(mx, my)); menu_button(rb, CFG_BTN_X, CFG_BTN_W, "Config", ed->settings_open || hit_settings_btn(ed, mx, my)); @@ -3804,6 +3804,14 @@ static bool hit_libman_btn(float mx, float my) { return in_rect(mx, my, LIBM_B static bool hit_settings_btn(Ed* ed, float mx, float my) { return in_rect(mx, my, CFG_BTN_X, 0, CFG_BTN_W, MENU_H); } TB_ED_API void start_path(Ed* ed, int mode, const char* def) { + /* Explorador de arquivos (tinyblocks-editor-files); sem o modulo, cai + na barra de caminho do menu. */ + if (files_open) { + ed->path_input_active = false; + ed->path_mode = PM_NONE; + files_open(ed, mode, def); + return; + } ed->path_input_active = true; ed->path_mode = mode; snprintf(ed->path_input_buf, sizeof(ed->path_input_buf), "%s", def ? def : ""); @@ -3898,6 +3906,7 @@ static void on_lmb_down(Ed* ed, float mx, float my) { if (ed->settings_open) { settings_click(ed, mx, my); return; } if (ed->libman_open) { libman_click(ed, mx, my); return; } if (ed->comp_dialog_open) { dlg_click(ed, mx, my); return; } + if (ed->fdlg.open) { if (files_click) files_click(ed, mx, my); return; } if (ed->plus_menu_open) { float px, py; plus_menu_pos(ed, &px, &py); @@ -4043,7 +4052,7 @@ static void on_lmb_down(Ed* ed, float mx, float my) { } static void on_lmb_up(Ed* ed, float mx, float my) { - if (ed->comp_dialog_open) return; + if (ed->comp_dialog_open || ed->fdlg.open) return; if (CT.kind == TK_IMAGE && CT.img_drag) { image_release(ed, mx, my); ed->state = ES_IDLE; return; } if (ed->state == ES_DRAG && ed->drag_bi >= 0) { mark_drag(&CT, ed->drag_bi, false); @@ -4069,6 +4078,7 @@ static void on_lmb_up(Ed* ed, float mx, float my) { } static void on_rmb_down(Ed* ed, float mx, float my) { + if (ed->fdlg.open) return; if (ed->ctx_menu_open) { ed->ctx_menu_open = false; ed->ctx_export = false; return; } if (ed->state != ES_IDLE) return; if (my < WS_Y || mx < WS_X || mx >= WIN_W - STAGE_W) return; @@ -4090,48 +4100,54 @@ static void on_rmb_down(Ed* ed, float mx, float my) { ed->ctx_menu_hover = -1; } +/* Executa a acao do modo PM_* sobre o caminho escolhido (barra de caminho + do menu ou explorador de arquivos). */ +TB_ED_API void path_commit(Ed* ed, int mode, const char* p) { + if (!p || !p[0]) return; + switch (mode) { + case PM_LOAD: project_load(ed, p); break; + case PM_SAVE: project_save(ed, p); break; + case PM_EXP_JSON: project_export(ed, p, EX_JSON); break; + case PM_EXP_XML: project_export(ed, p, EX_XML); break; + case PM_EXP_CSV: project_export(ed, p, EX_CSV); break; + case PM_IMGLOAD: if (CT.kind == TK_IMAGE) img_load_file(ed, p, -1); break; + case PM_IMSAVE: if (CT.kind == TK_IMAGE) img_save_out(ed, p); break; + case PM_IMADDR: if (CT.kind == TK_IMAGE) img_load_file(ed, p, IMG_L_RASTER); break; + case PM_IMADDV: if (CT.kind == TK_IMAGE) img_load_file(ed, p, IMG_L_VECTOR); break; + case PM_CODELOAD: { + if (CT.kind == TK_CODE) { + scopy(CT.code_path, sizeof(CT.code_path), p); + const char* b = strrchr(p, '/'); b = b ? b + 1 : p; + scopy(CT.name, sizeof(CT.name), b); + scopy(CT.code_lang, sizeof(CT.code_lang), tb_syntax_normalize_lang(p)); + if (!code_read_file(&CT, p)) { + snprintf(ed->hot_status, sizeof(ed->hot_status), "Nao consegui abrir %.200s", p); + ed->hot_status_ok = false; + } + CT.code_hl_dirty = true; + } + break; + } + case PM_CODESAVE: if (CT.kind == TK_CODE) { code_write_file(&CT, p); snprintf(ed->hot_status, sizeof(ed->hot_status), "Salvo: %.200s", p); ed->hot_status_ok = true; } break; + default: hotlib_load(ed, p); break; +} +} + static void on_key(Ed* ed, int sc, bool ctrl) { bool enter = sc == TB_KEY_RETURN; if (ed->settings_open) { settings_key(ed, sc, ctrl); return; } if (ed->libman_open) { libman_key(ed, sc); return; } if (ed->comp_dialog_open) { dlg_key(ed, sc); return; } + if (ed->fdlg.open) { if (files_key) files_key(ed, sc, ctrl); else ed->fdlg.open = false; return; } if (ed->ctx_menu_open) { if (sc == TB_KEY_ESCAPE) ed->ctx_menu_open = false; return; } if (ed->path_input_active) { if (sc == TB_KEY_ESCAPE) { ed->path_input_active = false; ed->path_mode = PM_NONE; } else if (enter) { - const char* p = ed->path_input_buf; - if (p[0]) { - switch (ed->path_mode) { - case PM_LOAD: project_load(ed, p); break; - case PM_SAVE: project_save(ed, p); break; - case PM_EXP_JSON: project_export(ed, p, EX_JSON); break; - case PM_EXP_XML: project_export(ed, p, EX_XML); break; - case PM_EXP_CSV: project_export(ed, p, EX_CSV); break; - case PM_IMGLOAD: if (CT.kind == TK_IMAGE) img_load_file(ed, p, -1); break; - case PM_IMSAVE: if (CT.kind == TK_IMAGE) img_save_out(ed, p); break; - case PM_IMADDR: if (CT.kind == TK_IMAGE) img_load_file(ed, p, IMG_L_RASTER); break; - case PM_IMADDV: if (CT.kind == TK_IMAGE) img_load_file(ed, p, IMG_L_VECTOR); break; - case PM_CODELOAD: { - if (CT.kind == TK_CODE) { - scopy(CT.code_path, sizeof(CT.code_path), p); - const char* b = strrchr(p, '/'); b = b ? b + 1 : p; - scopy(CT.name, sizeof(CT.name), b); - scopy(CT.code_lang, sizeof(CT.code_lang), tb_syntax_normalize_lang(p)); - if (!code_read_file(&CT, p)) { - snprintf(ed->hot_status, sizeof(ed->hot_status), "Nao consegui abrir %.200s", p); - ed->hot_status_ok = false; - } - CT.code_hl_dirty = true; - } - break; - } - case PM_CODESAVE: if (CT.kind == TK_CODE) { code_write_file(&CT, p); snprintf(ed->hot_status, sizeof(ed->hot_status), "Salvo: %.200s", p); ed->hot_status_ok = true; } break; - default: hotlib_load(ed, p); break; - } - } + int mode = ed->path_mode; ed->path_input_active = false; ed->path_mode = PM_NONE; + if (ed->path_input_buf[0]) path_commit(ed, mode, ed->path_input_buf); } else if (sc == TB_KEY_BACKSPACE) { utf8_pop(ed->path_input_buf); ed->path_input_len = (int)strlen(ed->path_input_buf); @@ -4180,6 +4196,7 @@ static void on_text(Ed* ed, const char* typed) { if (ed->settings_open) { settings_text(ed, typed); return; } if (ed->libman_open) { libman_text(ed, typed); return; } if (ed->comp_dialog_open) { dlg_text(ed, typed); return; } + if (ed->fdlg.open) { if (files_text) files_text(ed, typed); return; } if (ed->path_input_active) { append_text(ed->path_input_buf, sizeof(ed->path_input_buf), typed); ed->path_input_len = (int)strlen(ed->path_input_buf); @@ -4325,7 +4342,9 @@ static void editor_eval(TinyBlocks_Block* self, TinyBlocks* rt) { double sdx = ev_real(evb, GFX_IN_SCROLL_DX); double sdy = ev_real(evb, GFX_IN_SCROLL_DY); if (sdx != 0.0 || sdy != 0.0) { - if (ed->settings_open) { + if (ed->fdlg.open) { + if (files_scroll) files_scroll(ed, (float)sdy); + } else if (ed->settings_open) { settings_scroll_by(ed, (float)sdy); } else if (ed->libman_open) { libman_scroll_by(ed, (float)sdy); @@ -4401,6 +4420,7 @@ static bool editor_on_draw(TinyBlocks_Block* self, TinyBlocks* rt, render_tooltip(ed, rb); render_ctx_menu(ed, rb); dlg_render(ed, rb); + if (ed->fdlg.open && files_view) files_view(ed, rb); libman_render(ed, rb); /* tela cheia: por cima de tudo */ settings_render(ed, rb); @@ -4515,6 +4535,7 @@ static void editor_instance_deinit(TinyBlocks_Block* self, TinyBlocks* rt) { * aqui só soltamos o que é do editor. */ bubble_clear(ed); for (int i = 0; i < ed->tab_n; i++) tab_doc_free(&ed->tabs[i]); + free(ed->fdlg.ents); free(ed->run_blocks); for (int u = 0; u < ed->user_block_n; u++) free(ed->user_blocks[u].snap); tb_libman_shutdown(); diff --git a/libraries/tinyblocks-editor/src/tb_editor_modules.c b/libraries/tinyblocks-editor/src/tb_editor_modules.c index ec89d9c..4efacf9 100644 --- a/libraries/tinyblocks-editor/src/tb_editor_modules.c +++ b/libraries/tinyblocks-editor/src/tb_editor_modules.c @@ -36,13 +36,16 @@ /* Tabelas vivas (preenchidas aqui, usadas pelo editor) */ TbEdImageApi tb_ed_img; TbEdWidgetsApi tb_ed_w; +TbEdFilesApi tb_ed_files; /* Ordem = dependencia: widgets e a base (a imagem usa as primitivas dela) */ -static const char* s_mod_name[2] = { "tinyblocks-editor-widgets", - "tinyblocks-editor-image" }; -static const char* s_mod_file[2] = { "libtinyblocks-editor-widgets.so", - "libtinyblocks-editor-image.so" }; -static const int s_mod_n = 2; +static const char* s_mod_name[3] = { "tinyblocks-editor-widgets", + "tinyblocks-editor-image", + "tinyblocks-editor-files" }; +static const char* s_mod_file[3] = { "libtinyblocks-editor-widgets.so", + "libtinyblocks-editor-image.so", + "libtinyblocks-editor-files.so" }; +static const int s_mod_n = 3; static char s_dir[512] = ""; @@ -138,6 +141,22 @@ static bool resolve_image(void) { return ok; } +/* Resolve as funcoes do explorador de arquivos */ +static bool resolve_files(void) { + TbEdFilesApi a; + memset(&a, 0, sizeof(a)); + sym_fn(&a.open, "tinyblocks-editor-files", "tb_editor_files_open"); + sym_fn(&a.close, "tinyblocks-editor-files", "tb_editor_files_close"); + sym_fn(&a.view, "tinyblocks-editor-files", "tb_editor_files_view"); + sym_fn(&a.click, "tinyblocks-editor-files", "tb_editor_files_click"); + sym_fn(&a.key, "tinyblocks-editor-files", "tb_editor_files_key"); + sym_fn(&a.text, "tinyblocks-editor-files", "tb_editor_files_text"); + sym_fn(&a.scroll, "tinyblocks-editor-files", "tb_editor_files_scroll"); + bool ok = a.open && a.close && a.view && a.click && a.key && a.text && a.scroll; + if (ok) tb_ed_files = a; + return ok; +} + /* -- carga --------------------------------------------------------------- */ bool tb_ed_modules_load(Ed* ed) { @@ -161,7 +180,7 @@ bool tb_ed_modules_load(Ed* ed) { ed->mods.list[ed->mods.n].ok = 1; ed->mods.n++; } - bool ok = resolve_widgets() && resolve_image(); + bool ok = resolve_widgets() && resolve_image() && resolve_files(); ed->mods.loaded = ok; if (ok) { snprintf(ed->mods.info, sizeof(ed->mods.info), @@ -174,6 +193,7 @@ void tb_ed_modules_clear(void) { tb_api_code_unload_all(); memset(&tb_ed_img, 0, sizeof(tb_ed_img)); memset(&tb_ed_w, 0, sizeof(tb_ed_w)); + memset(&tb_ed_files, 0, sizeof(tb_ed_files)); } bool tb_ed_modules_check(Ed* ed) { @@ -199,7 +219,7 @@ bool tb_ed_modules_check(Ed* ed) { ed->mods.list[ed->mods.n].ok = 1; ed->mods.n++; } - bool ok = resolve_widgets() && resolve_image(); + bool ok = resolve_widgets() && resolve_image() && resolve_files(); ed->mods.loaded = ok; ed->mods.reloads++; snprintf(ed->mods.info, sizeof(ed->mods.info), "Editor recarregado (%d)%s", diff --git a/tests/editor_harness.c b/tests/editor_harness.c index a3970d7..e5dc739 100644 --- a/tests/editor_harness.c +++ b/tests/editor_harness.c @@ -1,6 +1,8 @@ /* Harness headless do editor: dirige Editor.Scene com um bloco de eventos * falso e inspeciona os comandos de desenho produzidos por on_draw. */ #include +#include +#include #include #include #include @@ -410,6 +412,59 @@ int main(void) { CHECK(find("Oi do arquivo", R_WS, 0, &x, &y), "bloco do arquivo externo aparece no workspace"); remove(openjson); + /* 13b. Explorador de arquivos (tinyblocks-editor-files) */ + { + const char* fdir = "/tmp/tb_harness_fdlg"; + char fp[256]; + mkdir(fdir, 0755); + snprintf(fp, sizeof(fp), "%s/sub", fdir); + mkdir(fp, 0755); + snprintf(fp, sizeof(fp), "%s/nota.txt", fdir); + FILE* tf = fopen(fp, "wb"); + if (tf) { fputs("oi", tf); fclose(tf); } + snprintf(fp, sizeof(fp), "%s/proj.json", fdir); + tf = fopen(fp, "wb"); + if (tf) { + fputs("{\"format\":\"tinyblocks-project\",\"version\":1,\"cur_tab\":0," + "\"tabs\":[{\"name\":\"Explorado\",\"composite\":false,\"comp_id\":-1,\"color\":0," + "\"blocks\":[{\"i\":0,\"type\":\"Events.WhenRun\",\"x\":40,\"y\":40}]," + "\"top\":[0]}],\"user_blocks\":[],\"variables\":[]}", tf); + fclose(tf); + } + if (need("Abrir", R_FLY, 0, &x, &y)) click(x, y); + CHECK(find("Abrir projeto", R_ANY, 0, &x, &y), "Abrir mostra o explorador de arquivos"); + type(fdir); + key(TB_KEY_RETURN, 0, 0); + CHECK(find("proj.json", R_ANY, 0, &x, &y) && find("sub", R_ANY, 0, &x, &y) + && !find("nota.txt", R_ANY, 0, &x, &y), + "explorador lista pastas + .json e filtra o resto"); + if (need("proj.json", R_ANY, 0, &x, &y)) { click(x, y); click(x, y); } + frame(0, 0, 0, 0, 0, 0, NULL); + CHECK(any_has("Projeto aberto") && find("Explorado", R_ANY, 0, &x, &y), + "duplo clique no explorador abre o projeto"); + + if (need("Salvar", R_FLY, 0, &x, &y)) click(x, y); + CHECK(find("Salvar projeto", R_ANY, 0, &x, &y), "Salvar mostra o explorador de arquivos"); + for (int i = 0; i < 40; i++) key(TB_KEY_BACKSPACE, 0, 0); + type("proj"); + key(TB_KEY_RETURN, 0, 0); + CHECK(find("Substituir", R_ANY, 0, &x, &y), "salvar sobre arquivo existente pede confirmacao"); + if (need("Substituir", R_ANY, 0, &x, &y)) click(x, y); + frame(0, 0, 0, 0, 0, 0, NULL); + CHECK(any_has("Projeto salvo") && !find("Salvar projeto", R_ANY, 0, &x, &y), + "Substituir grava o .json (extensao completada) e fecha"); + + if (need("Abrir", R_FLY, 0, &x, &y)) click(x, y); + key(TB_KEY_ESCAPE, 0, 0); + frame(0, 0, 0, 0, 0, 0, NULL); + CHECK(!find("Abrir projeto", R_ANY, 0, &x, &y), "Esc fecha o explorador"); + + remove(fp); + snprintf(fp, sizeof(fp), "%s/nota.txt", fdir); remove(fp); + snprintf(fp, sizeof(fp), "%s/sub", fdir); rmdir(fp); + rmdir(fdir); + } + /* 14. Exportar pelo menu de contexto do botao Exportar */ if (need("Exportar", R_FLY, 0, &x, &y)) click(x, y); CHECK(find("Exportar JSON", R_FLY, 0, &x, &y) && find("Exportar XML", R_FLY, 0, &x, &y)