From 600c4fc520d23f364f9d2e32ca3419beeecef801 Mon Sep 17 00:00:00 2001 From: mguinhos Date: Fri, 23 Oct 2026 07:24:19 -0300 Subject: [PATCH] Interface de acessibilidade do editor (tinyblocks-a11y) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Library nova, no papel do ATK/GtkAccessible: cada quadro o editor monta uma árvore acessível (papel, nome, descrição, valor, estados, ações, retângulo), com ids estáveis, eventos entre quadros (foco, estado, nome, valor, componente novo, aviso) e uma ponte para o narrador (TINYBLOCKS_A11Y=log liga uma ponte de texto no stderr). Cada parte da tela se descreve onde é desenhada: menu, áreas, categorias, paleta, abas, blocos (rótulo falado, campos, bocas, parâmetros do defina), grupos, palco, saída, menus e diálogos. As ações agem pelo mesmo caminho do mouse e rolam até o componente se ele estiver fora da vista. API: tb_editor_accessible(scene). O harness acha componentes pela árvore (categorias, cenários 8 e 27) e tem um cenário só de acessibilidade; test-a11y testa a library. Co-Authored-By: Claude Opus 5.5 --- Makefile | 32 +- libraries/tinyblocks-a11y/Makefile | 29 + libraries/tinyblocks-a11y/include/tb_a11y.h | 203 +++++++ libraries/tinyblocks-a11y/library.toml | 18 + libraries/tinyblocks-a11y/src/tb_a11y.c | 458 ++++++++++++++++ libraries/tinyblocks-editor/Makefile | 4 +- .../tinyblocks-editor/include/tb_editor.h | 7 + .../include/tb_editor_internal.h | 9 + libraries/tinyblocks-editor/library.toml | 2 +- libraries/tinyblocks-editor/src/tb_editor.c | 503 ++++++++++++++++-- tests/a11y_test.c | 127 +++++ tests/editor_harness.c | 268 ++++++++-- 12 files changed, 1557 insertions(+), 103 deletions(-) create mode 100644 libraries/tinyblocks-a11y/Makefile create mode 100644 libraries/tinyblocks-a11y/include/tb_a11y.h create mode 100644 libraries/tinyblocks-a11y/library.toml create mode 100644 libraries/tinyblocks-a11y/src/tb_a11y.c create mode 100644 tests/a11y_test.c diff --git a/Makefile b/Makefile index fb50ecf..68752bb 100644 --- a/Makefile +++ b/Makefile @@ -45,6 +45,7 @@ GX_DIR := $(LIBDIR)/tinyblocks-graphics SD_DIR := $(LIBDIR)/tinyblocks-sdl2 PY_DIR := $(LIBDIR)/tinyblocks-python MC_DIR := $(LIBDIR)/tinyblocks-mcp +A11Y_DIR := $(LIBDIR)/tinyblocks-a11y GFX_INC := $(GX_DIR)/include GFX_HDR := $(GFX_INC)/tb_graphics.h @@ -94,13 +95,14 @@ API_A := $(API_DIR)/build/libtinyblocks-api.a LIBM_A := $(LIBM_DIR)/build/libtinyblocks-libman.a SYN_A := $(SYN_DIR)/build/libtinyblocks-syntax.a SB_A := $(SB_DIR)/build/libtinyblocks-scratchblocks.a +A11Y_A := $(A11Y_DIR)/build/libtinyblocks-a11y.a LIB_LDFLAGS := -L$(ED_DIR)/build -L$(GX_DIR)/build -L$(SD_DIR)/build \ $(foreach l,$(CAT_LIBS),-L$(LIBDIR)/tinyblocks-$(l)/build) -L$(BUILD) .PHONY: all help debug release static shared example sdl2 clean run python-lib mcp-lib json \ editor run-editor run-sdl2 mylib vector customappearance graphics sdl2-backend test-editor \ appdir appimage run-appimage icon \ - test-toml test-plugin test-blueprint test-base64 test-curves test-meta test-ppm test-bmp test-png test-image test-vg test-svg test-jpg \ + test-toml test-plugin test-blueprint test-a11y test-base64 test-curves test-meta test-ppm test-bmp test-png test-image test-vg test-svg test-jpg \ test-syntax test-os test-value test-vars test-scratchblocks test-input test-stdlib test-regex test-markdown test-opentype \ category-libs category-sos libs \ lib-events lib-control lib-errors lib-input lib-operators lib-number lib-string lib-variables lib-vector lib-appearance lib-pen lib-fs lib-os \ @@ -164,9 +166,12 @@ lib-$(1): endef $(foreach l,$(CAT_LIBS),$(eval $(call CAT_LIB_TARGET,$(l)))) -lib-editor: +lib-editor: lib-a11y $(MAKE) -C $(ED_DIR) +lib-a11y: + $(MAKE) -C $(A11Y_DIR) + lib-toml: $(MAKE) -C $(TOML_DIR) @@ -244,8 +249,8 @@ lib-mcp: libs: static lib-events lib-control lib-errors lib-operators lib-variables \ lib-appearance lib-pen lib-fs lib-os lib-input lib-number lib-string lib-vector lib-environ lib-graphics lib-sdl2 lib-toml \ - lib-json lib-xml lib-csv lib-blueprint lib-base64 lib-curves lib-ppm lib-bmp lib-png lib-image lib-vg lib-svg lib-jpg lib-syntax lib-scratchblocks lib-regex lib-markdown lib-opentype lib-api lib-libman lib-editor - @echo "Libraries OK: category + graphics + sdl2 + toml + json + xml + csv + blueprint + base64 + curves + ppm + bmp + png + image + vg + svg + jpg + syntax + regex + markdown + opentype + api + libman + editor" + lib-json lib-xml lib-csv lib-blueprint lib-base64 lib-curves lib-ppm lib-bmp lib-png lib-image lib-vg lib-svg lib-jpg lib-syntax lib-scratchblocks lib-regex lib-markdown lib-opentype lib-api lib-libman lib-a11y lib-editor + @echo "Libraries OK: category + graphics + sdl2 + toml + json + xml + csv + blueprint + base64 + curves + ppm + bmp + png + image + vg + svg + jpg + syntax + regex + markdown + opentype + api + libman + a11y + editor" category-libs: static $(foreach l,$(CAT_LIBS),lib-$(l)) ## Compila as libraries de categoria (.a/.so) @echo "Build category libs OK" @@ -287,9 +292,9 @@ $(BUILD)/editor: examples/editor/main.c static libs -I$(ED_DIR)/include -I$(GFX_INC) $(CAT_INCS) \ -I$(FS_DIR)/include -I$(EN_DIR)/include -I$(TOML_DIR)/include \ -I$(JSON_DIR)/include -I$(XML_DIR)/include -I$(CSV_DIR)/include -I$(BP_DIR)/include \ - -I$(API_DIR)/include -I$(LIBM_DIR)/include -I$(SYN_DIR)/include \ + -I$(API_DIR)/include -I$(LIBM_DIR)/include -I$(SYN_DIR)/include -I$(A11Y_DIR)/include \ $< -o $@ \ - -Wl,--start-group $(ED_A) $(GX_A) $(SD_A) $(TOML_A) $(JSON_A) $(XML_A) $(CSV_A) $(BP_A) $(SB_A) $(API_A) $(LIBM_A) $(SYN_A) $(CAT_A) $(LIB_A) -Wl,--end-group \ + -Wl,--start-group $(ED_A) $(A11Y_A) $(GX_A) $(SD_A) $(TOML_A) $(JSON_A) $(XML_A) $(CSV_A) $(BP_A) $(SB_A) $(API_A) $(LIBM_A) $(SYN_A) $(CAT_A) $(LIB_A) -Wl,--end-group \ $(SDL2_LIBS) $(SDL2TTF_LIBS) -ldl -lm -rdynamic \ '-Wl,-rpath,$$ORIGIN' @@ -447,15 +452,16 @@ $(BUILD)/editor_harness: tests/editor_harness.c $(SRCS) \ $(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 + $(GX_DIR)/src/tb_graphics.c $(A11Y_DIR)/src/tb_a11y.c $(A11Y_DIR)/include/tb_a11y.h \ + lib-blueprint ed-modules @mkdir -p $(BUILD) $(CC) $(CFLAGS_DBG) \ - -I$(ED_DIR)/include -DTB_EDITOR_PRIVATE_API -I$(EW_DIR)/include -I$(EIMG_DIR)/include -I$(EFILES_DIR)/include -I$(GFX_INC) $(CAT_INCS) -I$(BP_DIR)/include \ + -I$(ED_DIR)/include -I$(A11Y_DIR)/include -DTB_EDITOR_PRIVATE_API -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 -I$(SB_DIR)/include \ $(SRCS) \ $(EDITOR_SRC)/tb_editor.c $(EDITOR_SRC)/tb_stage_widget.c \ $(EDITOR_SRC)/tb_editor_modules.c \ - $(GX_DIR)/src/tb_graphics.c \ + $(GX_DIR)/src/tb_graphics.c $(A11Y_DIR)/src/tb_a11y.c \ $(BP_DIR)/src/blueprint.c $(BP_DIR)/src/serialize.c \ $(API_DIR)/src/tb_api.c $(LIBM_DIR)/src/tb_libman.c \ $(TOML_LIB_SRCS) $(TOML_DIR)/src/tb_toml.c \ @@ -513,6 +519,14 @@ $(BUILD)/environ_test: tests/environ_test.c $(EN_DIR)/src/tb_environ.c static test-environ: $(BUILD)/environ_test ## Roda o teste da environ (ASan/UBSan) ./$(BUILD)/environ_test +# -- Teste da library a11y (arvore acessivel) --- +$(BUILD)/a11y_test: tests/a11y_test.c $(A11Y_DIR)/src/tb_a11y.c $(A11Y_DIR)/include/tb_a11y.h static + @mkdir -p $(BUILD) + $(CC) $(CFLAGS_DBG) -I$(A11Y_DIR)/include $(A11Y_DIR)/src/tb_a11y.c tests/a11y_test.c -o $@ + +test-a11y: $(BUILD)/a11y_test ## Roda o teste da interface de acessibilidade (ASan/UBSan) + ./$(BUILD)/a11y_test + # -- Teste da library base64 --- $(BUILD)/base64_test: tests/base64_test.c $(B64_DIR)/src/tb_base64.c static @mkdir -p $(BUILD) diff --git a/libraries/tinyblocks-a11y/Makefile b/libraries/tinyblocks-a11y/Makefile new file mode 100644 index 0000000..59a9dc0 --- /dev/null +++ b/libraries/tinyblocks-a11y/Makefile @@ -0,0 +1,29 @@ +# Makefile da library a11y (interface de acessibilidade: arvore acessivel) +# Constroi build/libtinyblocks-a11y.a e build/libtinyblocks-a11y.so +CC := gcc +BUILD := build + +CFLAGS := -std=c11 -Wall -Wextra -pedantic -fvisibility=hidden \ + -D_POSIX_C_SOURCE=200809L -I../../include -Iinclude + +OBJ := $(BUILD)/tb_a11y.o +LIB_A := $(BUILD)/libtinyblocks-a11y.a +LIB_SO := $(BUILD)/libtinyblocks-a11y.so + +.PHONY: all static shared clean +all: static shared +static: $(LIB_A) +shared: $(LIB_SO) + +$(OBJ): src/tb_a11y.c include/tb_a11y.h + @mkdir -p $(BUILD) + $(CC) $(CFLAGS) -fPIC -c $< -o $@ + +$(LIB_A): $(OBJ) + ar rcs $@ $^ + +$(LIB_SO): $(OBJ) + $(CC) -shared -o $@ $^ + +clean: + $(RM) -r $(BUILD) diff --git a/libraries/tinyblocks-a11y/include/tb_a11y.h b/libraries/tinyblocks-a11y/include/tb_a11y.h new file mode 100644 index 0000000..9e2ebad --- /dev/null +++ b/libraries/tinyblocks-a11y/include/tb_a11y.h @@ -0,0 +1,203 @@ +/* + * TinyBlocks - tb_a11y.h + * Interface de acessibilidade (no espirito do ATK/GtkAccessible do GTK). + * + * Uma interface grafica descreve a cada quadro uma ARVORE ACESSIVEL: cada + * componente (botao, aba, bloco, campo...) vira um no com papel, nome, + * descricao, valor, estados, acoes e retangulo na tela. Quem consome: + * - um narrador / leitor de tela, por uma ponte (TinyBlocks_A11yBridge) + * que recebe os eventos (foco mudou, estado mudou, aviso...); + * - a automacao de testes, que acha componentes por papel + nome e os + * aciona por tb_a11y_do_action, sem depender de texto desenhado nem de + * coordenadas fixas. + * + * Construcao (modo imediato, a cada quadro): + * + * tb_a11y_begin(tree, "TinyBlocks", w, h); + * int bar = tb_a11y_push(tree, TB_A11Y_TOOLBAR, "Menu", 0, 0, w, 44); + * int b = tb_a11y_add(tree, TB_A11Y_BUTTON, "Abrir", x, y, bw, bh); + * tb_a11y_set_actions(tree, b, TB_A11Y_ACT_ACTIVATE); + * tb_a11y_pop(tree); + * tb_a11y_end(tree); // compara com o quadro anterior e gera eventos + * + * Os ids dos nos sao estaveis entre quadros (derivados do caminho: papel + + * nome + ordem entre irmaos iguais), entao foco e selecao podem ser seguidos. + */ +#ifndef TINYBLOCKS_TB_A11Y_H +#define TINYBLOCKS_TB_A11Y_H + +#include +#include +#include +#include + +/* == Papeis (o "tipo" do componente, como ATK_ROLE_* / GTK_ACCESSIBLE_ROLE_*) */ +typedef enum TinyBlocks_A11yRole { + TB_A11Y_NONE = 0, + TB_A11Y_APPLICATION, /* raiz */ + TB_A11Y_PANE, /* area da janela (paleta, scripts, palco) */ + TB_A11Y_TOOLBAR, /* barra de botoes */ + TB_A11Y_BUTTON, + TB_A11Y_CHECKBOX, + TB_A11Y_RADIO, /* uma opcao entre varias (ex.: cor) */ + TB_A11Y_TEXT_FIELD, /* campo de texto editavel */ + TB_A11Y_LABEL, /* texto estatico */ + TB_A11Y_LIST, + TB_A11Y_LIST_ITEM, + TB_A11Y_TAB_LIST, + TB_A11Y_TAB, + TB_A11Y_TAB_PANEL, + TB_A11Y_MENU, + TB_A11Y_MENU_ITEM, + TB_A11Y_DIALOG, + TB_A11Y_GROUP, /* agrupamento (ex.: grupo colorido) */ + TB_A11Y_CANVAS, /* area de desenho (palco, imagem) */ + TB_A11Y_STATUS, /* aviso / mensagem (regiao "viva") */ + TB_A11Y_BLOCK, /* um bloco de programacao */ + TB_A11Y_SLOT, /* encaixe vazio de um bloco (ex.: booleano) */ + TB_A11Y_ROLE_COUNT +} TinyBlocks_A11yRole; + +/* == Estados (bits) */ +enum { + TB_A11Y_FOCUSED = 1u << 0, /* recebe o teclado */ + TB_A11Y_SELECTED = 1u << 1, /* aba/categoria/opcao atual */ + TB_A11Y_CHECKED = 1u << 2, + TB_A11Y_EXPANDED = 1u << 3, /* menu aberto */ + TB_A11Y_DISABLED = 1u << 4, + TB_A11Y_EDITABLE = 1u << 5, + TB_A11Y_EDITING = 1u << 6, /* texto sendo digitado agora */ + TB_A11Y_MODAL = 1u << 7, + TB_A11Y_HOVERED = 1u << 8, + TB_A11Y_BUSY = 1u << 9, /* rodando */ + TB_A11Y_OFFSCREEN = 1u << 10, /* existe, mas fora da area visivel */ +}; + +/* == Acoes (bits), executadas por tb_a11y_do_action */ +typedef enum TinyBlocks_A11yAction { + TB_A11Y_ACT_ACTIVATE = 1u << 0, /* clicar / pressionar */ + TB_A11Y_ACT_MENU = 1u << 1, /* abrir o menu de contexto */ + TB_A11Y_ACT_EDIT = 1u << 2, /* comecar a editar o texto */ + TB_A11Y_ACT_DRAG = 1u << 3, /* pode ser arrastado */ + TB_A11Y_ACT_DROP = 1u << 4, /* aceita algo solto em cima */ +} TinyBlocks_A11yAction; + +#define TB_A11Y_NAME_MAX 128 +#define TB_A11Y_DESC_MAX 192 +#define TB_A11Y_VALUE_MAX 128 + +typedef struct TinyBlocks_A11yNode { + uint32_t id; /* estavel entre quadros */ + uint32_t key; /* hash da chave (o nome, se nao * + * houver set_key); entra no id */ + int parent; /* indice do pai (-1 na raiz) */ + int first_child, last_child, next_sibling; + int child_count; + int depth; + TinyBlocks_A11yRole role; + uint32_t states; + uint32_t actions; + float x, y, w, h; /* retangulo na janela */ + float px, py; /* onde agir (clique); centro se nao * + * houver set_point */ + char name[TB_A11Y_NAME_MAX]; + char description[TB_A11Y_DESC_MAX]; + char value[TB_A11Y_VALUE_MAX]; +} TinyBlocks_A11yNode; + +/* == Eventos (o que muda entre quadros), para o narrador */ +typedef enum TinyBlocks_A11yEventKind { + TB_A11Y_EV_FOCUS, /* `node` ganhou o foco */ + TB_A11Y_EV_STATE, /* estados de `node` mudaram (old_states) */ + TB_A11Y_EV_NAME, /* nome mudou */ + TB_A11Y_EV_VALUE, /* valor mudou */ + TB_A11Y_EV_SHOWN, /* no novo (dialogos, menus, avisos) */ + TB_A11Y_EV_ANNOUNCE, /* aviso para ler em voz alta (`text`) */ +} TinyBlocks_A11yEventKind; + +typedef struct TinyBlocks_A11yEvent { + TinyBlocks_A11yEventKind kind; + int node; /* indice no quadro atual (-1 em ANNOUNCE) */ + uint32_t id; + uint32_t old_states; + char text[TB_A11Y_DESC_MAX]; +} TinyBlocks_A11yEvent; + +typedef struct TinyBlocks_A11yTree TinyBlocks_A11yTree; + +/* == Ponte para um narrador (interface implementada por cada plataforma: + * AT-SPI no Linux, UI Automation no Windows, um sintetizador de voz...) */ +typedef struct TinyBlocks_A11yBridge { + const char* name; + void (*on_event)(void* user, const TinyBlocks_A11yTree* tree, const TinyBlocks_A11yEvent* ev); + void (*on_frame)(void* user, const TinyBlocks_A11yTree* tree); /* arvore nova (pode ser NULL) */ + void* user; +} TinyBlocks_A11yBridge; + +/* == Quem desenha a interface executa as acoes (ex.: clicar no centro) */ +typedef bool (*TinyBlocks_A11yPerform)(void* user, const TinyBlocks_A11yTree* tree, + int node, TinyBlocks_A11yAction action); + +/* -- Ciclo de vida --- */ +TB_API TinyBlocks_A11yTree* tb_a11y_tree_new(void); +TB_API void tb_a11y_tree_free(TinyBlocks_A11yTree* tree); +TB_API void tb_a11y_set_perform(TinyBlocks_A11yTree* tree, TinyBlocks_A11yPerform fn, void* user); +TB_API void tb_a11y_set_bridge(TinyBlocks_A11yTree* tree, const TinyBlocks_A11yBridge* bridge); + +/* -- Construcao (a cada quadro) --- */ +TB_API void tb_a11y_begin(TinyBlocks_A11yTree* tree, const char* app_name, float w, float h); +TB_API int tb_a11y_push(TinyBlocks_A11yTree* tree, TinyBlocks_A11yRole role, const char* name, + float x, float y, float w, float h); /* abre um container */ +TB_API int tb_a11y_add (TinyBlocks_A11yTree* tree, TinyBlocks_A11yRole role, const char* name, + float x, float y, float w, float h); /* folha */ +TB_API void tb_a11y_pop (TinyBlocks_A11yTree* tree); +TB_API int tb_a11y_current(const TinyBlocks_A11yTree* tree); /* container aberto */ +TB_API void tb_a11y_end (TinyBlocks_A11yTree* tree); + +TB_API void tb_a11y_set_states (TinyBlocks_A11yTree* tree, int node, uint32_t states); +TB_API void tb_a11y_add_states (TinyBlocks_A11yTree* tree, int node, uint32_t states); +TB_API void tb_a11y_set_actions (TinyBlocks_A11yTree* tree, int node, uint32_t actions); +TB_API void tb_a11y_set_value (TinyBlocks_A11yTree* tree, int node, const char* value); +TB_API void tb_a11y_set_description(TinyBlocks_A11yTree* tree, int node, const char* desc); +TB_API void tb_a11y_set_name (TinyBlocks_A11yTree* tree, int node, const char* name); +/* Chave estavel para o id quando o nome muda com o conteudo (ex.: um bloco + * "repita (10) vezes" usa o tipo como chave). Chame antes dos filhos. */ +TB_API void tb_a11y_set_key (TinyBlocks_A11yTree* tree, int node, const char* key); +/* Ponto onde as acoes agem (o centro pode cair num filho) */ +TB_API void tb_a11y_set_point (TinyBlocks_A11yTree* tree, int node, float x, float y); +/* Aviso para o narrador ler (mensagens de status, erros) */ +TB_API void tb_a11y_announce(TinyBlocks_A11yTree* tree, const char* text); + +/* -- Consulta --- */ +TB_API int tb_a11y_count(const TinyBlocks_A11yTree* tree); +TB_API const TinyBlocks_A11yNode* tb_a11y_node(const TinyBlocks_A11yTree* tree, int node); +TB_API int tb_a11y_root(const TinyBlocks_A11yTree* tree); +TB_API int tb_a11y_focus(const TinyBlocks_A11yTree* tree); /* -1 se nenhum */ +TB_API int tb_a11y_by_id(const TinyBlocks_A11yTree* tree, uint32_t id); +/* n-esimo descendente de `from` com o papel (NONE = qualquer) e o nome + * (NULL = qualquer). Devolve o indice ou -1. */ +TB_API int tb_a11y_find(const TinyBlocks_A11yTree* tree, int from, TinyBlocks_A11yRole role, + const char* name, int nth); +/* Como find, mas o nome so precisa conter `part` */ +TB_API int tb_a11y_find_part(const TinyBlocks_A11yTree* tree, int from, TinyBlocks_A11yRole role, + const char* part, int nth); +/* Caminho separado por " > " (ex.: "Paleta > Meus Blocos > Criar um Bloco") */ +TB_API char* tb_a11y_path(const TinyBlocks_A11yTree* tree, int node, char* out, size_t cap); +TB_API void tb_a11y_center(const TinyBlocks_A11yTree* tree, int node, float* x, float* y); +TB_API void tb_a11y_point (const TinyBlocks_A11yTree* tree, int node, float* x, float* y); +TB_API const char* tb_a11y_role_name(TinyBlocks_A11yRole role); /* "botão", "aba"... */ +/* Arvore em texto, um no por linha (para depuracao e testes) */ +TB_API void tb_a11y_dump(const TinyBlocks_A11yTree* tree, int from, FILE* out); + +/* -- Acoes e eventos --- */ +TB_API bool tb_a11y_do_action(TinyBlocks_A11yTree* tree, int node, TinyBlocks_A11yAction action); +/* Eventos do ultimo tb_a11y_end (tambem enviados a ponte) */ +TB_API int tb_a11y_event_count(const TinyBlocks_A11yTree* tree); +TB_API const TinyBlocks_A11yEvent* tb_a11y_event(const TinyBlocks_A11yTree* tree, int i); + +/* Ponte simples que escreve os eventos num FILE* (um "narrador" de texto) */ +TB_API TinyBlocks_A11yBridge tb_a11y_log_bridge(FILE* out); +/* Frase que um narrador diria para o no: "Abrir, botão" */ +TB_API char* tb_a11y_describe(const TinyBlocks_A11yTree* tree, int node, char* out, size_t cap); + +#endif /* TINYBLOCKS_TB_A11Y_H */ diff --git a/libraries/tinyblocks-a11y/library.toml b/libraries/tinyblocks-a11y/library.toml new file mode 100644 index 0000000..e76478e --- /dev/null +++ b/libraries/tinyblocks-a11y/library.toml @@ -0,0 +1,18 @@ +# Metadados da library a11y. Lido pela UI/build sem recompilar o .so. +# Nao tem blocos: e a interface de acessibilidade usada pelo editor (narrador +# e automacao de testes); por isso e privada (fora da lista de extensoes). +[library] +name = "tinyblocks-a11y" +private = true +version = "1.0.0" +category = "Acessibilidade" +description = "Interface de acessibilidade: arvore acessivel, acoes e eventos" +color = [76, 151, 255, 255] +dark = [51, 115, 230, 255] + +[build] +sources = ["src/tb_a11y.c"] +includes = ["include"] +shared = "build/libtinyblocks-a11y.so" +static = "build/libtinyblocks-a11y.a" +depends = [] diff --git a/libraries/tinyblocks-a11y/src/tb_a11y.c b/libraries/tinyblocks-a11y/src/tb_a11y.c new file mode 100644 index 0000000..9d35ed8 --- /dev/null +++ b/libraries/tinyblocks-a11y/src/tb_a11y.c @@ -0,0 +1,458 @@ +/* + * TinyBlocks - tb_a11y.c + * Arvore acessivel em modo imediato (ver tb_a11y.h). + */ +#include +#include + +#include "tb_a11y.h" + +#define STACK_MAX 64 + +typedef struct PrevNode { /* o que importa do quadro anterior, por id */ + uint32_t id; + uint32_t states; + uint32_t name_h, value_h; +} PrevNode; + +struct TinyBlocks_A11yTree { + TinyBlocks_A11yNode* nodes; + int n, cap; + int stack[STACK_MAX]; + int sp; + int focus; + + PrevNode* prev; /* tabela hash (aberta) do quadro anterior */ + int prev_cap; + uint32_t prev_focus; + bool has_prev; + + TinyBlocks_A11yEvent* events; + int ev_n, ev_cap; + char announce[8][TB_A11Y_DESC_MAX]; + int announce_n; + + TinyBlocks_A11yPerform perform; + void* perform_user; + TinyBlocks_A11yBridge bridge; + bool has_bridge; +}; + +/* -- Utilitarios --- */ +static uint32_t fnv(uint32_t h, const void* data, size_t n) { + const unsigned char* p = data; + for (size_t i = 0; i < n; i++) { h ^= p[i]; h *= 16777619u; } + return h; +} +static uint32_t str_hash(const char* s) { return fnv(2166136261u, s, strlen(s)); } + +static void scopy(char* dst, size_t cap, const char* src) { + if (!src) src = ""; + size_t n = strlen(src); + if (n >= cap) { /* corta sem partir letra UTF-8 */ + n = cap - 1; + while (n > 0 && ((unsigned char)src[n] & 0xC0) == 0x80) n--; + } + memcpy(dst, src, n); + dst[n] = 0; +} + +static bool valid(const TinyBlocks_A11yTree* t, int node) { return t && node >= 0 && node < t->n; } + +/* -- Ciclo de vida --- */ +TinyBlocks_A11yTree* tb_a11y_tree_new(void) { + TinyBlocks_A11yTree* t = calloc(1, sizeof(*t)); + if (t) t->focus = -1; + return t; +} + +void tb_a11y_tree_free(TinyBlocks_A11yTree* t) { + if (!t) return; + free(t->nodes); + free(t->prev); + free(t->events); + free(t); +} + +void tb_a11y_set_perform(TinyBlocks_A11yTree* t, TinyBlocks_A11yPerform fn, void* user) { + if (!t) return; + t->perform = fn; + t->perform_user = user; +} + +void tb_a11y_set_bridge(TinyBlocks_A11yTree* t, const TinyBlocks_A11yBridge* b) { + if (!t) return; + t->has_bridge = b != NULL; + if (b) t->bridge = *b; +} + +/* -- Construcao --- */ +/* id = hash(pai, papel, chave, ordem entre os irmaos com o mesmo papel e chave) */ +static uint32_t node_id(const TinyBlocks_A11yTree* t, int parent, TinyBlocks_A11yRole role, + uint32_t key, int self) { + uint32_t h = parent >= 0 ? t->nodes[parent].id : 2166136261u; + int ord = 0; + if (parent >= 0) + for (int c = t->nodes[parent].first_child; c >= 0 && c != self; c = t->nodes[c].next_sibling) + if (t->nodes[c].role == role && t->nodes[c].key == key) ord++; + h = fnv(h, &role, sizeof(role)); + h = fnv(h, &key, sizeof(key)); + h = fnv(h, &ord, sizeof(ord)); + return h ? h : 1; +} + +static int new_node(TinyBlocks_A11yTree* t, TinyBlocks_A11yRole role, const char* name, + float x, float y, float w, float h) { + if (t->n == t->cap) { + int cap = t->cap ? t->cap * 2 : 256; + TinyBlocks_A11yNode* nn = realloc(t->nodes, (size_t)cap * sizeof(*nn)); + if (!nn) return -1; + t->nodes = nn; + t->cap = cap; + } + int i = t->n++; + TinyBlocks_A11yNode* nd = &t->nodes[i]; + memset(nd, 0, sizeof(*nd)); + int parent = t->sp > 0 ? t->stack[t->sp - 1] : -1; + nd->parent = parent; + nd->first_child = nd->last_child = nd->next_sibling = -1; + nd->role = role; + nd->x = x; nd->y = y; nd->w = w; nd->h = h; + nd->px = x + w * 0.5f; nd->py = y + h * 0.5f; + scopy(nd->name, sizeof(nd->name), name); + if (parent >= 0) { + TinyBlocks_A11yNode* p = &t->nodes[parent]; + nd->depth = p->depth + 1; + if (p->last_child >= 0) t->nodes[p->last_child].next_sibling = i; + else p->first_child = i; + p->last_child = i; + p->child_count++; + } + nd->key = str_hash(nd->name); + nd->id = node_id(t, parent, role, nd->key, i); + return i; +} + +void tb_a11y_begin(TinyBlocks_A11yTree* t, const char* app_name, float w, float h) { + if (!t) return; + t->n = 0; + t->sp = 0; + t->focus = -1; + t->announce_n = 0; + int r = new_node(t, TB_A11Y_APPLICATION, app_name, 0, 0, w, h); + if (r >= 0) t->stack[t->sp++] = r; +} + +int tb_a11y_push(TinyBlocks_A11yTree* t, TinyBlocks_A11yRole role, const char* name, + float x, float y, float w, float h) { + if (!t) return -1; + int i = new_node(t, role, name, x, y, w, h); + if (i >= 0 && t->sp < STACK_MAX) t->stack[t->sp++] = i; + return i; +} + +int tb_a11y_add(TinyBlocks_A11yTree* t, TinyBlocks_A11yRole role, const char* name, + float x, float y, float w, float h) { + return t ? new_node(t, role, name, x, y, w, h) : -1; +} + +void tb_a11y_pop(TinyBlocks_A11yTree* t) { + if (t && t->sp > 1) t->sp--; /* a raiz fica */ +} + +int tb_a11y_current(const TinyBlocks_A11yTree* t) { + return t && t->sp > 0 ? t->stack[t->sp - 1] : -1; +} + +void tb_a11y_set_states(TinyBlocks_A11yTree* t, int node, uint32_t s) { + if (!valid(t, node)) return; + t->nodes[node].states = s; + if (s & TB_A11Y_FOCUSED) t->focus = node; +} +void tb_a11y_add_states(TinyBlocks_A11yTree* t, int node, uint32_t s) { + if (valid(t, node)) tb_a11y_set_states(t, node, t->nodes[node].states | s); +} +void tb_a11y_set_actions(TinyBlocks_A11yTree* t, int node, uint32_t a) { + if (valid(t, node)) t->nodes[node].actions = a; +} +void tb_a11y_set_value(TinyBlocks_A11yTree* t, int node, const char* v) { + if (valid(t, node)) scopy(t->nodes[node].value, sizeof(t->nodes[node].value), v); +} +void tb_a11y_set_description(TinyBlocks_A11yTree* t, int node, const char* d) { + if (valid(t, node)) scopy(t->nodes[node].description, sizeof(t->nodes[node].description), d); +} +void tb_a11y_set_name(TinyBlocks_A11yTree* t, int node, const char* name) { + if (valid(t, node)) scopy(t->nodes[node].name, sizeof(t->nodes[node].name), name); +} + +/* Muda o id: so antes de o no ganhar filhos (o id deles depende do dele) */ +void tb_a11y_set_key(TinyBlocks_A11yTree* t, int node, const char* key) { + if (!valid(t, node) || !key) return; + TinyBlocks_A11yNode* nd = &t->nodes[node]; + nd->key = str_hash(key); + if (nd->child_count == 0) nd->id = node_id(t, nd->parent, nd->role, nd->key, node); +} + +void tb_a11y_set_point(TinyBlocks_A11yTree* t, int node, float x, float y) { + if (!valid(t, node)) return; + t->nodes[node].px = x; + t->nodes[node].py = y; +} + +void tb_a11y_announce(TinyBlocks_A11yTree* t, const char* text) { + if (!t || !text || !text[0] || t->announce_n >= 8) return; + scopy(t->announce[t->announce_n++], TB_A11Y_DESC_MAX, text); +} + +/* -- Eventos (diferenca para o quadro anterior) --- */ +static void ev_push(TinyBlocks_A11yTree* t, TinyBlocks_A11yEventKind kind, int node, + uint32_t old_states, const char* text) { + if (t->ev_n == t->ev_cap) { + int cap = t->ev_cap ? t->ev_cap * 2 : 32; + TinyBlocks_A11yEvent* ne = realloc(t->events, (size_t)cap * sizeof(*ne)); + if (!ne) return; + t->events = ne; + t->ev_cap = cap; + } + TinyBlocks_A11yEvent* e = &t->events[t->ev_n++]; + memset(e, 0, sizeof(*e)); + e->kind = kind; + e->node = node; + e->id = node >= 0 ? t->nodes[node].id : 0; + e->old_states = old_states; + if (text) scopy(e->text, sizeof(e->text), text); +} + +static const PrevNode* prev_find(const TinyBlocks_A11yTree* t, uint32_t id) { + if (!t->prev_cap) return NULL; + for (int k = 0, i = (int)(id & (uint32_t)(t->prev_cap - 1)); k < t->prev_cap; + k++, i = (i + 1) & (t->prev_cap - 1)) { + if (t->prev[i].id == 0) return NULL; + if (t->prev[i].id == id) return &t->prev[i]; + } + return NULL; +} + +static void prev_store(TinyBlocks_A11yTree* t) { + int cap = 64; + while (cap < t->n * 2) cap *= 2; + if (cap != t->prev_cap) { + PrevNode* np = realloc(t->prev, (size_t)cap * sizeof(*np)); + if (!np) { t->has_prev = false; return; } + t->prev = np; + t->prev_cap = cap; + } + memset(t->prev, 0, (size_t)t->prev_cap * sizeof(*t->prev)); + for (int n = 0; n < t->n; n++) { + const TinyBlocks_A11yNode* nd = &t->nodes[n]; + int i = (int)(nd->id & (uint32_t)(cap - 1)); + while (t->prev[i].id != 0 && t->prev[i].id != nd->id) i = (i + 1) & (cap - 1); + t->prev[i].id = nd->id; + t->prev[i].states = nd->states; + t->prev[i].name_h = str_hash(nd->name); + t->prev[i].value_h = str_hash(nd->value); + } + t->prev_focus = t->focus >= 0 ? t->nodes[t->focus].id : 0; + t->has_prev = true; +} + +/* estados que interessam ao narrador (passar o mouse nao) */ +#define STATE_EV_MASK (TB_A11Y_SELECTED | TB_A11Y_CHECKED | TB_A11Y_EXPANDED | TB_A11Y_DISABLED | \ + TB_A11Y_EDITING | TB_A11Y_BUSY) + +static bool shows_up(TinyBlocks_A11yRole r) { + return r == TB_A11Y_DIALOG || r == TB_A11Y_MENU || r == TB_A11Y_STATUS; +} + +void tb_a11y_end(TinyBlocks_A11yTree* t) { + if (!t) return; + t->ev_n = 0; + if (t->has_prev) { + uint32_t fid = t->focus >= 0 ? t->nodes[t->focus].id : 0; + if (fid && fid != t->prev_focus) ev_push(t, TB_A11Y_EV_FOCUS, t->focus, 0, NULL); + for (int n = 0; n < t->n; n++) { + const TinyBlocks_A11yNode* nd = &t->nodes[n]; + const PrevNode* p = prev_find(t, nd->id); + if (!p) { + if (shows_up(nd->role)) ev_push(t, TB_A11Y_EV_SHOWN, n, 0, nd->name); + continue; + } + if ((p->states ^ nd->states) & STATE_EV_MASK) ev_push(t, TB_A11Y_EV_STATE, n, p->states, NULL); + if (p->name_h != str_hash(nd->name)) ev_push(t, TB_A11Y_EV_NAME, n, 0, nd->name); + if (p->value_h != str_hash(nd->value)) ev_push(t, TB_A11Y_EV_VALUE, n, 0, nd->value); + } + } + for (int i = 0; i < t->announce_n; i++) ev_push(t, TB_A11Y_EV_ANNOUNCE, -1, 0, t->announce[i]); + prev_store(t); + + if (t->has_bridge) { + if (t->bridge.on_frame) t->bridge.on_frame(t->bridge.user, t); + if (t->bridge.on_event) + for (int i = 0; i < t->ev_n; i++) t->bridge.on_event(t->bridge.user, t, &t->events[i]); + } +} + +int tb_a11y_event_count(const TinyBlocks_A11yTree* t) { return t ? t->ev_n : 0; } +const TinyBlocks_A11yEvent* tb_a11y_event(const TinyBlocks_A11yTree* t, int i) { + return t && i >= 0 && i < t->ev_n ? &t->events[i] : NULL; +} + +/* -- Consulta --- */ +int tb_a11y_count(const TinyBlocks_A11yTree* t) { return t ? t->n : 0; } +const TinyBlocks_A11yNode* tb_a11y_node(const TinyBlocks_A11yTree* t, int node) { + return valid(t, node) ? &t->nodes[node] : NULL; +} +int tb_a11y_root(const TinyBlocks_A11yTree* t) { return t && t->n > 0 ? 0 : -1; } +int tb_a11y_focus(const TinyBlocks_A11yTree* t) { return t ? t->focus : -1; } + +int tb_a11y_by_id(const TinyBlocks_A11yTree* t, uint32_t id) { + for (int i = 0; t && i < t->n; i++) if (t->nodes[i].id == id) return i; + return -1; +} + +/* Os nos sao gravados em pre-ordem: os descendentes de `from` vem depois dele */ +static int find_impl(const TinyBlocks_A11yTree* t, int from, TinyBlocks_A11yRole role, + const char* name, bool part, int nth) { + if (!t || t->n == 0 || from >= t->n) return -1; + if (from < 0) from = 0; + for (int i = from + 1; i < t->n; i++) { + const TinyBlocks_A11yNode* nd = &t->nodes[i]; + if (nd->depth <= t->nodes[from].depth) break; /* saiu da subarvore */ + if (role != TB_A11Y_NONE && nd->role != role) continue; + if (name && (part ? !strstr(nd->name, name) : strcmp(nd->name, name) != 0)) continue; + if (nth-- > 0) continue; + return i; + } + return -1; +} + +int tb_a11y_find(const TinyBlocks_A11yTree* t, int from, TinyBlocks_A11yRole role, + const char* name, int nth) { + return find_impl(t, from, role, name, false, nth); +} + +int tb_a11y_find_part(const TinyBlocks_A11yTree* t, int from, TinyBlocks_A11yRole role, + const char* part, int nth) { + return find_impl(t, from, role, part, true, nth); +} + +void tb_a11y_center(const TinyBlocks_A11yTree* t, int node, float* x, float* y) { + if (!valid(t, node)) { *x = *y = 0; return; } + const TinyBlocks_A11yNode* nd = &t->nodes[node]; + *x = nd->x + nd->w * 0.5f; + *y = nd->y + nd->h * 0.5f; +} + +void tb_a11y_point(const TinyBlocks_A11yTree* t, int node, float* x, float* y) { + if (!valid(t, node)) { *x = *y = 0; return; } + *x = t->nodes[node].px; + *y = t->nodes[node].py; +} + +char* tb_a11y_path(const TinyBlocks_A11yTree* t, int node, char* out, size_t cap) { + if (!cap) return out; + out[0] = 0; + if (!valid(t, node)) return out; + int chain[64], n = 0; + for (int p = node; p > 0 && n < 64; p = t->nodes[p].parent) chain[n++] = p; /* sem a raiz */ + size_t len = 0; + for (int i = n - 1; i >= 0 && len + 1 < cap; i--) { + const char* nm = t->nodes[chain[i]].name[0] ? t->nodes[chain[i]].name + : tb_a11y_role_name(t->nodes[chain[i]].role); + int w = snprintf(out + len, cap - len, "%s%s", i == n - 1 ? "" : " > ", nm); + if (w < 0) break; + len += (size_t)w; + } + return out; +} + +const char* tb_a11y_role_name(TinyBlocks_A11yRole r) { + static const char* names[TB_A11Y_ROLE_COUNT] = { + [TB_A11Y_NONE] = "", + [TB_A11Y_APPLICATION] = "aplicação", + [TB_A11Y_PANE] = "área", + [TB_A11Y_TOOLBAR] = "barra de ferramentas", + [TB_A11Y_BUTTON] = "botão", + [TB_A11Y_CHECKBOX] = "caixa de seleção", + [TB_A11Y_RADIO] = "opção", + [TB_A11Y_TEXT_FIELD] = "campo de texto", + [TB_A11Y_LABEL] = "texto", + [TB_A11Y_LIST] = "lista", + [TB_A11Y_LIST_ITEM] = "item", + [TB_A11Y_TAB_LIST] = "lista de abas", + [TB_A11Y_TAB] = "aba", + [TB_A11Y_TAB_PANEL] = "painel", + [TB_A11Y_MENU] = "menu", + [TB_A11Y_MENU_ITEM] = "item de menu", + [TB_A11Y_DIALOG] = "diálogo", + [TB_A11Y_GROUP] = "grupo", + [TB_A11Y_CANVAS] = "tela de desenho", + [TB_A11Y_STATUS] = "aviso", + [TB_A11Y_BLOCK] = "bloco", + [TB_A11Y_SLOT] = "encaixe", + }; + return r >= 0 && r < TB_A11Y_ROLE_COUNT ? names[r] : ""; +} + +/* "Abrir, botão" / "x, campo de texto, 10, editando" */ +char* tb_a11y_describe(const TinyBlocks_A11yTree* t, int node, char* out, size_t cap) { + if (!cap) return out; + out[0] = 0; + if (!valid(t, node)) return out; + const TinyBlocks_A11yNode* nd = &t->nodes[node]; + size_t len = 0; +#define ADD(...) do { int w_ = snprintf(out + len, cap - len, __VA_ARGS__); \ + if (w_ > 0) len = len + (size_t)w_ < cap ? len + (size_t)w_ : cap - 1; } while (0) + if (nd->name[0]) ADD("%s, ", nd->name); + ADD("%s", tb_a11y_role_name(nd->role)); + if (nd->value[0]) ADD(", %s", nd->value); + static const struct { uint32_t bit; const char* txt; } st[] = { + { TB_A11Y_SELECTED, "selecionado" }, { TB_A11Y_CHECKED, "marcado" }, + { TB_A11Y_EXPANDED, "aberto" }, { TB_A11Y_DISABLED, "desativado" }, + { TB_A11Y_EDITING, "editando" }, { TB_A11Y_BUSY, "rodando" }, + }; + for (size_t i = 0; i < sizeof(st) / sizeof(st[0]); i++) + if (nd->states & st[i].bit) ADD(", %s", st[i].txt); + if (nd->description[0]) ADD(". %s", nd->description); +#undef ADD + return out; +} + +void tb_a11y_dump(const TinyBlocks_A11yTree* t, int from, FILE* out) { + if (!t || t->n == 0 || !out || from >= t->n) return; + if (from < 0) from = 0; + int base = t->nodes[from].depth; + for (int i = from; i < t->n; i++) { + const TinyBlocks_A11yNode* nd = &t->nodes[i]; + if (i > from && nd->depth <= base) break; + char d[400]; + fprintf(out, "%*s%s [%.0f,%.0f %.0fx%.0f]\n", (nd->depth - base) * 2, "", + tb_a11y_describe(t, i, d, sizeof(d)), nd->x, nd->y, nd->w, nd->h); + } +} + +/* -- Acoes --- */ +bool tb_a11y_do_action(TinyBlocks_A11yTree* t, int node, TinyBlocks_A11yAction a) { + if (!valid(t, node) || !t->perform || !(t->nodes[node].actions & (uint32_t)a)) return false; + return t->perform(t->perform_user, t, node, a); +} + +/* -- Ponte de texto --- */ +static void log_event(void* user, const TinyBlocks_A11yTree* t, const TinyBlocks_A11yEvent* ev) { + FILE* out = user; + char d[400]; + switch (ev->kind) { + case TB_A11Y_EV_FOCUS: fprintf(out, "[a11y] foco: %s\n", tb_a11y_describe(t, ev->node, d, sizeof(d))); break; + case TB_A11Y_EV_STATE: fprintf(out, "[a11y] estado: %s\n", tb_a11y_describe(t, ev->node, d, sizeof(d))); break; + case TB_A11Y_EV_NAME: fprintf(out, "[a11y] nome: %s\n", ev->text); break; + case TB_A11Y_EV_VALUE: fprintf(out, "[a11y] valor: %s\n", tb_a11y_describe(t, ev->node, d, sizeof(d))); break; + case TB_A11Y_EV_SHOWN: fprintf(out, "[a11y] abriu: %s\n", tb_a11y_describe(t, ev->node, d, sizeof(d))); break; + case TB_A11Y_EV_ANNOUNCE: fprintf(out, "[a11y] aviso: %s\n", ev->text); break; + } + fflush(out); +} + +TinyBlocks_A11yBridge tb_a11y_log_bridge(FILE* out) { + TinyBlocks_A11yBridge b = { "texto", log_event, NULL, out }; + return b; +} diff --git a/libraries/tinyblocks-editor/Makefile b/libraries/tinyblocks-editor/Makefile index 035f01f..2c4b249 100644 --- a/libraries/tinyblocks-editor/Makefile +++ b/libraries/tinyblocks-editor/Makefile @@ -16,6 +16,7 @@ CFLAGS := -std=c11 -Wall -Wextra -pedantic -fvisibility=hidden \ -I../tinyblocks-libman/include \ -I../tinyblocks-syntax/include \ -I../tinyblocks-scratchblocks/include \ + -I../tinyblocks-a11y/include \ -Ilibraries/tinyblocks-editor-widgets/include \ -Ilibraries/tinyblocks-editor-image/include \ -Ilibraries/tinyblocks-editor-files/include @@ -48,7 +49,8 @@ $(BUILD)/tb_editor.o: src/tb_editor.c include/tb_editor.h include/tb_editor_inte ../tinyblocks-api/include/tb_api.h \ ../tinyblocks-libman/include/tb_libman.h \ ../tinyblocks-syntax/include/tb_syntax.h \ - ../tinyblocks-scratchblocks/include/tb_scratchblocks.h + ../tinyblocks-scratchblocks/include/tb_scratchblocks.h \ + ../tinyblocks-a11y/include/tb_a11y.h @mkdir -p $(BUILD) $(CC) $(CFLAGS) -fPIC -c $< -o $@ diff --git a/libraries/tinyblocks-editor/include/tb_editor.h b/libraries/tinyblocks-editor/include/tb_editor.h index d628419..80c4ca5 100644 --- a/libraries/tinyblocks-editor/include/tb_editor.h +++ b/libraries/tinyblocks-editor/include/tb_editor.h @@ -34,4 +34,11 @@ TB_API const TinyBlocks_LibraryDescriptor* tinyblocks_get_library(void); /* Enfileira um .so para ser carregado automaticamente ao abrir o editor */ TB_API void tb_editor_preload(const char* path); +/* Interface de acessibilidade (tb_a11y.h, da tinyblocks-a11y): a arvore + * acessivel do ultimo quadro desenhado do bloco "Editor.Scene". Serve ao + * narrador (tb_a11y_set_bridge) e aos testes (tb_a11y_find + do_action). + * NULL se a acessibilidade estiver desligada (TINYBLOCKS_A11Y=0). */ +typedef struct TinyBlocks_A11yTree TinyBlocks_A11yTree; +TB_API TinyBlocks_A11yTree* tb_editor_accessible(TinyBlocks_Block* scene); + #endif /* TINYBLOCKS_EDITOR_H */ diff --git a/libraries/tinyblocks-editor/include/tb_editor_internal.h b/libraries/tinyblocks-editor/include/tb_editor_internal.h index 5a677d8..9caf9de 100644 --- a/libraries/tinyblocks-editor/include/tb_editor_internal.h +++ b/libraries/tinyblocks-editor/include/tb_editor_internal.h @@ -39,6 +39,9 @@ #include #include "tb_editor_modules.h" +/* Arvore acessivel (tb_a11y.h, da tinyblocks-a11y); so o tb_editor.c usa */ +typedef struct TinyBlocks_A11yTree TinyBlocks_A11yTree; + #ifndef TB_EW_UNUSED #define TB_EW_UNUSED __attribute__((unused)) #endif @@ -964,6 +967,11 @@ typedef struct Ed { AreaRect main_scripts; /* area de scripts da principal */ int drag_tab; /* aba de onde vem o bloco arrastado */ float drag_ws_x, drag_ws_y; /* origem da area de scripts dessa aba */ + + /* Acessibilidade: cada parte da tela se descreve ao se desenhar */ + TinyBlocks_A11yTree* ax_tree; + bool ax_enabled; /* TINYBLOCKS_A11Y=0 desliga */ + bool ax_on; /* montando a arvore neste desenho */ } Ed; #define CT (ed->tabs[ed->cur_tab]) @@ -1011,6 +1019,7 @@ typedef struct TabClass { bool (*text)(Ed* ed, const char* typed); /* true = tratou */ bool (*scroll)(Ed* ed, float mx, float my, double dx, double dy); bool (*icon)(TinyBlocks_RenderableBlock* rb, float x, float cy); /* icone na barra */ + void (*a11y)(Ed* ed); /* descreve o conteudo (acessivel) */ } TabClass; const TabClass* tab_class(const Tab* T); diff --git a/libraries/tinyblocks-editor/library.toml b/libraries/tinyblocks-editor/library.toml index d0a7acc..7190c69 100644 --- a/libraries/tinyblocks-editor/library.toml +++ b/libraries/tinyblocks-editor/library.toml @@ -17,4 +17,4 @@ includes = ["include"] defines = ["TB_EDITOR_PRIVATE_API"] shared = "build/libtinyblocks-editor.so" static = "build/libtinyblocks-editor.a" -depends = ["tinyblocks-editor-widgets", "tinyblocks-editor-image", "tinyblocks-editor-files"] +depends = ["tinyblocks-a11y", "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 a3a1708..285af2e 100644 --- a/libraries/tinyblocks-editor/src/tb_editor.c +++ b/libraries/tinyblocks-editor/src/tb_editor.c @@ -47,6 +47,7 @@ #include #include #include +#include #include "../include/tb_editor.h" @@ -877,6 +878,145 @@ static void draw_slot(TinyBlocks_RenderableBlock* rb, const EdBlock* b, int s, if (editing) rb_fill_rect(rb, end_x + 1, ly - SLOT_LINE_H - 7, 1.5f, 14, txt); } +/* == Acessibilidade (tb_a11y.h) === + * Como os widgets do GTK implementam GtkAccessible, cada parte da tela se + * descreve na arvore acessivel no mesmo lugar em que se desenha: papel, + * nome, valor, estados, acoes e retangulo. A arvore serve ao narrador (por + * uma ponte) e aos testes (acham componentes por papel + nome). So o + * desenho da janela principal monta a arvore (ed->ax_on). */ +static TinyBlocks_A11yTree* ax(Ed* ed) { return ed->ax_on ? ed->ax_tree : NULL; } + +static int ax_push(Ed* ed, TinyBlocks_A11yRole r, const char* name, float x, float y, float w, float h) { + return tb_a11y_push(ax(ed), r, name, x, y, w, h); +} +static int ax_add(Ed* ed, TinyBlocks_A11yRole r, const char* name, float x, float y, float w, float h) { + return tb_a11y_add(ax(ed), r, name, x, y, w, h); +} +static void ax_pop(Ed* ed) { tb_a11y_pop(ax(ed)); } + +/* Componente que se ativa com um clique (botao, aba, item de menu...) */ +static int ax_click(Ed* ed, TinyBlocks_A11yRole r, const char* name, float x, float y, float w, float h) { + int n = ax_add(ed, r, name, x, y, w, h); + tb_a11y_set_actions(ax(ed), n, TB_A11Y_ACT_ACTIVATE); + return n; +} +static int ax_button(Ed* ed, const char* name, float x, float y, float w, float h) { + return ax_click(ed, TB_A11Y_BUTTON, name, x, y, w, h); +} + +/* Campo de texto; `editing` = recebe o teclado agora */ +static int ax_field(Ed* ed, const char* name, const char* value, bool editing, + float x, float y, float w, float h) { + int n = ax_add(ed, TB_A11Y_TEXT_FIELD, name, x, y, w, h); + tb_a11y_set_value(ax(ed), n, value); + tb_a11y_set_actions(ax(ed), n, TB_A11Y_ACT_ACTIVATE | TB_A11Y_ACT_EDIT); + tb_a11y_set_states(ax(ed), n, TB_A11Y_EDITABLE | (editing ? TB_A11Y_FOCUSED | TB_A11Y_EDITING : 0)); + return n; +} + +static void ax_cat(char* out, size_t cap, size_t* len, const char* s) { + if (!s[0] || *len + 1 >= cap) return; + int w = snprintf(out + *len, cap - *len, "%s%s", *len ? " " : "", s); + if (w > 0) *len = *len + (size_t)w < cap ? *len + (size_t)w : cap - 1; +} + +/* Rotulo falado de um bloco, com o que esta nos campos: + * "repita (10) vezes", "se <(x) > (5)> então", "defina desenhe (x)" */ +static void ax_block_label(Tab* T, int bi, char* out, size_t cap, int depth) { + EdBlock* b = &T->ws[bi]; + size_t len = 0; + out[0] = 0; + for (int i = 0; i < b->part_n; i++) { + const Part* p = &b->parts[i]; + char piece[192], buf[96]; + piece[0] = 0; + if (p->arg == PART_PROTO) { + ProtoLayout L; + proto_layout(b, &L); + size_t m = 0; + for (int k = 0; k < L.n; k++) { + char it[80]; + snprintf(it, sizeof(it), L.param[k] ? "(%.60s)" : "%.60s", L.txt[k]); + ax_cat(piece, sizeof(piece), &m, it); + } + } else if (p->arg < 0) { + snprintf(piece, sizeof(piece), "%s", part_str(p, buf, sizeof(buf))); + } else { + bool bl = slot_is_bool(b->type, p->arg); + if (b->args[p->arg] >= 0 && depth < 4) { + char sub[160]; + ax_block_label(T, b->args[p->arg], sub, sizeof(sub), depth + 1); + snprintf(piece, sizeof(piece), bl ? "<%.150s>" : "(%.150s)", sub); + } else { + snprintf(piece, sizeof(piece), bl ? "<>" : "(%.150s)", slot_src(b, p->arg)); + } + } + ax_cat(out, cap, &len, piece); + } +} + +static void ax_chain(Ed* ed, Tab* T, int head, float ox, float oy, uint32_t acts); + +/* Um bloco: no BLOCK com os campos (TEXT_FIELD / SLOT), os reporteres + * encaixados, os parametros do "defina" e as bocas (GROUP com a pilha) */ +static void ax_block(Ed* ed, Tab* T, int bi, float ox, float oy, uint32_t acts) { + TinyBlocks_A11yTree* t = ax(ed); + if (!t) return; + EdBlock* b = &T->ws[bi]; + char label[TB_A11Y_NAME_MAX]; + ax_block_label(T, bi, label, sizeof(label), 0); + float x = b->ax + ox, y = b->ay + oy; + int n = tb_a11y_push(t, TB_A11Y_BLOCK, label, x, y, b->w, b->h); + tb_a11y_set_key(t, n, b->type_name); /* o id nao muda ao editar */ + tb_a11y_set_point(t, n, x + fminf(8.f, b->w * 0.5f), y + b->row_y + b->row_h * 0.5f); + tb_a11y_set_actions(t, n, acts); + if (T == &CT && (x + b->w < WS_X || x > WS_X + WS_W || y + b->h < WS_Y || y > WS_Y + WS_H)) + tb_a11y_add_states(t, n, TB_A11Y_OFFSCREEN); /* existe, mas a aba esta rolada */ + const TinyBlocks_BlockMeta* meta = tb_meta_find(b->type_name); + if (meta && meta->doc) tb_a11y_set_description(t, n, meta->doc); + + float cy = y + b->row_y + b->row_h * 0.5f; + for (int i = 0; i < b->part_n; i++) { + const Part* p = &b->parts[i]; + if (p->arg == PART_PROTO) { /* parametros arrastaveis */ + ProtoLayout L; + proto_layout(b, &L); + for (int k = 0; k < L.n; k++) { + if (!L.param[k]) continue; + int q = tb_a11y_add(t, TB_A11Y_BLOCK, L.txt[k], x + p->x + L.x[k], cy - PILL_H * 0.5f, + L.w[k], PILL_H); + tb_a11y_set_description(t, q, "parâmetro: arraste para usar no corpo"); + tb_a11y_set_actions(t, q, TB_A11Y_ACT_DRAG); + } + continue; + } + if (p->arg < 0) continue; + int s = p->arg; + if (b->args[s] >= 0) { ax_block(ed, T, b->args[s], ox, oy, acts); continue; } + const char* sname = b->type->input_descs[s].name ? b->type->input_descs[s].name : ""; + float sx = x + b->slot_x[s], sy = y + b->slot_y[s]; + if (slot_is_bool(b->type, s)) { + int q = tb_a11y_add(t, TB_A11Y_SLOT, sname, sx, sy, b->slot_w[s], b->slot_h[s]); + tb_a11y_set_actions(t, q, TB_A11Y_ACT_DROP); + } else { + bool editing = ed->editing && T == &CT && ed->edit_bi == bi && ed->edit_in == s; + int q = ax_field(ed, sname, slot_src(b, s), editing, sx, sy, b->slot_w[s], b->slot_h[s]); + tb_a11y_set_actions(t, q, TB_A11Y_ACT_ACTIVATE | TB_A11Y_ACT_EDIT | TB_A11Y_ACT_DROP); + } + } + for (int k = 0; k < shape_mouths(shape_of(b->type)); k++) { + const char* mn = k == 0 ? "dentro" : mouth_label(b->type, k - 1); + tb_a11y_push(t, TB_A11Y_GROUP, mn, x + ARM_W, y + b->sub_y[k], b->w - ARM_W, b->sub_h[k]); + if (b->sub[k] >= 0) ax_chain(ed, T, b->sub[k], ox, oy, acts); + tb_a11y_pop(t); + } + tb_a11y_pop(t); +} + +static void ax_chain(Ed* ed, Tab* T, int head, float ox, float oy, uint32_t acts) { + for (int c = head; c >= 0; c = T->ws[c].next) ax_block(ed, T, c, ox, oy, acts); +} + static void render_chain(Ed* ed, TinyBlocks_RenderableBlock* rb, Tab* T, int head, float ox, float oy); static void render_block(Ed* ed, TinyBlocks_RenderableBlock* rb, Tab* T, int bi, float ox, float oy) { @@ -1870,7 +2010,7 @@ static void lay_default(Ed* ed) { ed->lay_root = lay_node(ed, LN_HSPLIT, pal, mid, -1, 0, PALETTE_W); } -/* -- Layout salvo nas configuracoes (chave "editor.layout") --------------- +/* -- Layout salvo nas configuracoes (chave "editor.layout") --- * Texto curto: folha = letra da view (p paleta, s scripts, t palco, * o saida); divisao = H|V:(a,b). O padrao e * "H0:318(p,H1:360(s,V0:310(t,o)))". */ @@ -2018,7 +2158,7 @@ static void lay_place(Ed* ed, int n, float x, float y, float w, float h) { else { lay_place(ed, N->a, x, y, w, sa); lay_place(ed, N->b, x, y + sa, w, h - sa); } } -/* Abas destacadas -------------------------------------------------------- */ +/* Abas destacadas --- */ static bool tab_detached(Ed* ed, int i) { for (int k = 0; k < TABWIN_MAX; k++) if (ed->twin[k].win && ed->twin[k].tab == i) return true; return false; @@ -2914,7 +3054,7 @@ static bool sb_import_text(Ed* ed, const char* text, const char* name) { return true; } -/* -- Previa da exportacao: o texto que vai para o forum ----------------- */ +/* -- Previa da exportacao: o texto que vai para o forum --- */ #define SBP_HEAD_H 46 #define SBP_FOOT_H 58 #define SBP_LINE_H 19 @@ -2988,11 +3128,13 @@ static void sbp_render(Ed* ed, TinyBlocks_RenderableBlock* rb) { char title[96]; snprintf(title, sizeof(title), "Prévia scratchblocks - %s", CT.name); rb_text_c(rb, x + 18, y + SBP_HEAD_H * 0.5f, title, COL_WHITE, 17); + tb_a11y_set_states(ax(ed), ax_push(ed, TB_A11Y_DIALOG, title, x, y, w, h), TB_A11Y_MODAL); /* texto, numa area escura como a aba de codigo */ float cx = x + 14, cy = y + SBP_HEAD_H + 12, cw = w - 28, ch = h - SBP_HEAD_H - SBP_FOOT_H - 18; rb_fill_rrect(rb, cx, cy, cw, ch, 8, COLA(30, 30, 30, 255)); clip_set(ed, rb, cx + 4, cy + 4, cw - 8, ch - 8); + tb_a11y_set_value(ax(ed), ax_add(ed, TB_A11Y_TEXT_FIELD, "scratchblocks", cx, cy, cw, ch), ed->sbp_text); int total = sbp_lines(ed->sbp_text); int visible = (int)((ch - 16) / SBP_LINE_H); if (ed->sbp_scroll > total - visible) ed->sbp_scroll = total - visible; @@ -3033,7 +3175,9 @@ static void sbp_render(Ed* ed, TinyBlocks_RenderableBlock* rb) { float bx, by, bw; sbp_button_pos(ed, i, &bx, &by, &bw); sbp_button(rb, bx, by, bw, labels[i], i == 0, in_rect(ed->mouse_x, ed->mouse_y, bx, by, bw, 34)); + ax_button(ed, labels[i], bx, by, bw, 34); } + ax_pop(ed); } static void sbp_copy(Ed* ed) { @@ -3281,6 +3425,8 @@ void dlg_render(Ed* ed, TinyBlocks_RenderableBlock* rb) { rb_fill_rect(rb, dx, dy + DLG_HEAD_H - 12, DLG_W, 12, accent); const char* title = ed->comp_dialog_def >= 0 ? "Editar Bloco" : "Criar um Bloco"; rb_text_c(rb, dx + (DLG_W - tw(title, 18)) * 0.5f, dy + DLG_HEAD_H * 0.5f, title, COL_WHITE, 18); + tb_a11y_set_states(ax(ed), ax_push(ed, TB_A11Y_DIALOG, title, dx, dy, DLG_W, DLG_H), TB_A11Y_MODAL); + ax_button(ed, "Fechar", dx + DLG_W - 44, dy, 44, DLG_HEAD_H); { /* fechar: um X desenhado */ float qx = dx + DLG_W - 26, qy = dy + DLG_HEAD_H * 0.5f; rb_fill_circle(rb, qx, qy, 12, COLA(255, 255, 255, 60)); @@ -3306,6 +3452,9 @@ void dlg_render(Ed* ed, TinyBlocks_RenderableBlock* rb) { ed->comp_dialog_name[0] ? COL_TEXT : COL_TEXT_DIM, FS); if (ed->comp_dialog_editing && ed->comp_dialog_edit_part < 0) rb_fill_rect(rb, nfx + (nfw + lw) * 0.5f + 2, nfy + 4, 1.5f, nfh - 8, COL_TEXT); + ax_push(ed, TB_A11Y_BLOCK, "Protótipo", bx, by, bw, bh); + ax_field(ed, "Nome do bloco", ed->comp_dialog_name, + ed->comp_dialog_editing && ed->comp_dialog_edit_part < 0, nfx, nfy, nfw, nfh); } /* Partes do protótipo */ @@ -3329,15 +3478,27 @@ void dlg_render(Ed* ed, TinyBlocks_RenderableBlock* rb) { p->name[0] ? COL_TEXT : COL_TEXT_DIM, FS); } if (sel) rb_stroke_rect(rb, px - 2, py - 2, pw + 4, ph + 4, 2, COL_TEXT); + { /* cada parte: campo com o nome da entrada (ou do rotulo) */ + const char* kind = p->kind == PARAM_LABEL ? "Rótulo" : p->kind == PARAM_BOOL ? "Entrada booleana" + : "Entrada"; + bool ed_here = ed->comp_dialog_editing && ed->comp_dialog_edit_part == i; + int an = ax_field(ed, kind, p->name, ed_here, px, py, pw, ph); + if (sel) tb_a11y_add_states(ax(ed), an, TB_A11Y_SELECTED); + } if (ed->comp_dialog_editing && ed->comp_dialog_edit_part == i && p->kind != PARAM_LABEL) { float lw = tw(label, FS); rb_fill_rect(rb, px + (pw + lw) * 0.5f + 2, py + 5, 1.5f, ph - 10, COL_TEXT); } } + ax_pop(ed); /* prototipo */ + /* Controles - apagar - da parte selecionada */ if (ed->comp_dialog_sel >= 0) { float ctr = bx + bw * 0.5f, cty = by - 26; + ax_button(ed, "Mover para a esquerda", ctr - 60, cty - 16, 34, 32); + ax_button(ed, "Apagar a parte", ctr - 16, cty - 16, 32, 32); + ax_button(ed, "Mover para a direita", ctr + 26, cty - 16, 34, 32); /* setas: mover a parte para a esquerda/direita */ rb_line(rb, ctr - 38, cty - 6, ctr - 44, cty, 2.f, COL_TEXT); rb_line(rb, ctr - 44, cty, ctr - 38, cty + 6, 2.f, COL_TEXT); @@ -3360,6 +3521,11 @@ void dlg_render(Ed* ed, TinyBlocks_RenderableBlock* rb) { for (int i = 0; i < 3; i++) { float cx = cx0 + i * (cw + 16.f); bool hover = in_rect(ed->mouse_x, ed->mouse_y, cx, cy0, cw, ch); + { + static const char* names[3] = { "Adicionar entrada de número ou texto", + "Adicionar entrada booleana", "Adicionar rótulo" }; + ax_button(ed, names[i], cx, cy0, cw, ch); + } rb_fill_rrect(rb, cx, cy0, cw, ch, 8, hover ? COL_HOVER : COL_PANEL); rb_stroke_rect(rb, cx + 0.5f, cy0 + 0.5f, cw - 1, ch - 1, 1, hover ? COL_MENU : COL_BORDER); float iy = cy0 + 14.f; @@ -3383,12 +3549,19 @@ void dlg_render(Ed* ed, TinyBlocks_RenderableBlock* rb) { float sx = dx + 28.f + ci * 30.f; if (ed->comp_dialog_color == ci) rb_fill_rrect(rb, sx - 2, fy - 2, 26, 26, 6, COL_TEXT); rb_fill_rrect(rb, sx, fy, 22, 22, 5, s_user_colors[ci]); + char cn[16]; + snprintf(cn, sizeof(cn), "Cor %d", ci + 1); + tb_a11y_set_states(ax(ed), ax_click(ed, TB_A11Y_RADIO, cn, sx, fy, 22, 22), + ed->comp_dialog_color == ci ? TB_A11Y_CHECKED : 0); } float okx = dx + DLG_W - 28.f - 84.f, cax = okx - 96.f; rb_fill_rrect(rb, cax, fy - 8, 84, 36, 8, COL_BORDER); rb_text_c(rb, cax + (84 - tw("Cancelar", FS_UI)) * 0.5f, fy + 10, "Cancelar", COL_TEXT, FS_UI); rb_fill_rrect(rb, okx, fy - 8, 84, 36, 8, accent); rb_text_c(rb, okx + (84 - tw("OK", FS_UI)) * 0.5f, fy + 10, "OK", COL_WHITE, FS_UI); + ax_button(ed, "Cancelar", cax, fy - 8, 84, 36); + ax_button(ed, "OK", okx, fy - 8, 84, 36); + ax_pop(ed); } bool dlg_click(Ed* ed, float mx, float my) { @@ -3512,6 +3685,13 @@ static void render_menu(Ed* ed, TinyBlocks_RenderableBlock* rb) { 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)); + ax_push(ed, TB_A11Y_TOOLBAR, "Menu", 0, 0, WIN_W, MENU_H); + ax_button(ed, "Abrir", OPEN_BTN_X, 7, OPEN_BTN_W, MENU_H - 14); + ax_button(ed, "Salvar", SAVE_BTN_X, 7, SAVE_BTN_W, MENU_H - 14); + tb_a11y_set_states(ax(ed), ax_button(ed, "Exportar", EXP_BTN_X, 7, EXP_BTN_W, MENU_H - 14), + ed->ctx_menu_open && ed->ctx_export ? TB_A11Y_EXPANDED : 0); + ax_button(ed, "Bibliotecas", LIBM_BTN_X, 7, LIBM_BTN_W, MENU_H - 14); + ax_button(ed, "Config", CFG_BTN_X, 7, CFG_BTN_W, MENU_H - 14); /* views destacadas: um botao para cada (clicar recolhe de volta) */ float cx = CFG_BTN_X - 10; for (int v = V_COUNT - 1; v >= 0; v--) { @@ -3525,6 +3705,8 @@ static void render_menu(Ed* ed, TinyBlocks_RenderableBlock* rb) { rb_fill_rrect(rb, cx, 7, w, MENU_H - 14, 6, hov ? COL_MENU_DARK : COLA(255, 255, 255, 34)); draw_window_icon(rb, cx + 2, 7 + (MENU_H - 14 - 22) * 0.5f, 22, true, COL_WHITE); rb_text_c(rb, cx + 30, MENU_H * 0.5f, nm, COL_WHITE, FS_UI); + tb_a11y_set_description(ax(ed), ax_button(ed, nm, cx, 7, w, MENU_H - 14), + "janela destacada: ative para recolher"); cx -= 8; } for (int k = TABWIN_MAX - 1; k >= 0; k--) { /* abas destacadas */ @@ -3539,12 +3721,15 @@ static void render_menu(Ed* ed, TinyBlocks_RenderableBlock* rb) { rb_fill_rrect(rb, cx, 7, w, MENU_H - 14, 6, hov ? COL_MENU_DARK : COLA(255, 255, 255, 34)); draw_window_icon(rb, cx + 2, 7 + (MENU_H - 14 - 22) * 0.5f, 22, true, COL_WHITE); rb_text_c(rb, cx + 30, MENU_H * 0.5f, nm, COL_WHITE, FS_UI); + tb_a11y_set_description(ax(ed), ax_button(ed, nm, cx, 7, w, MENU_H - 14), + "aba destacada: ative para recolher"); cx -= 8; } if (ed->lib_hover || (active && ed->path_mode == PM_LIB)) rb_fill_rect(rb, LIB_BTN_X, 0, LIB_BTN_W, MENU_H, COL_MENU_DARK); rb_text_c(rb, LIB_BTN_X + 14, MENU_H*0.5f, "Carregar library...", COL_WHITE, FS_UI); + ax_button(ed, "Carregar library...", LIB_BTN_X, 0, LIB_BTN_W, MENU_H); if (ed->path_input_active) { float bx = LIB_BTN_X + LIB_BTN_W + 10, bw = WIN_W - bx - 16; @@ -3560,7 +3745,9 @@ static void render_menu(Ed* ed, TinyBlocks_RenderableBlock* rb) { rb_text_c(rb, bx + 10, MENU_H*0.5f, t, ed->path_input_buf[0] ? COL_TEXT : COL_TEXT_DIM, FS_UI); float cw = tw(ed->path_input_buf, FS_UI); rb_fill_rect(rb, bx + 11 + cw, MENU_H*0.5f - 8, 1.5f, 16, COL_TEXT); + ax_field(ed, prompt, ed->path_input_buf, true, bx, 7, bw, MENU_H - 14); } + ax_pop(ed); } /* == Notificacoes flutuantes (toasts) === @@ -3697,6 +3884,10 @@ static void render_toasts(Ed* ed, TinyBlocks_RenderableBlock* rb) { rb_line(rb, cx - 4, cy - 4, cx + 4, cy + 4, 1.6f, xh ? txt : dim); rb_line(rb, cx + 4, cy - 4, cx - 4, cy + 4, 1.6f, xh ? txt : dim); /* temporizador: barra que encolhe (para com o mouse em cima) */ + int an = ax_push(ed, TB_A11Y_STATUS, t->text, t->x, t->y, w, h); + tb_a11y_set_description(ax(ed), an, t->ok ? "aviso" : "erro"); + ax_button(ed, "Fechar aviso", cx - 11, cy - 11, 22, 22); + ax_pop(ed); float k = t->total_ms > 0 ? fmaxf(0.f, t->left_ms / t->total_ms) : 0.f; Col tb = bar; if (hov) tb.a *= 0.45f; @@ -3724,6 +3915,7 @@ static void render_tab_bar(Ed* ed, TinyBlocks_RenderableBlock* rb) { rb_fill_rect(rb, x0, y0, WS_W, TAB_H, COL_UI_BG); int dragged = ed->tab_drag ? ed->tab_press : -1; int vis[MAX_TABS], vn = tabs_visible_list(ed, vis); + ax_push(ed, TB_A11Y_TAB_LIST, "Abas", x0, y0, wbar, TAB_H); for (int pass = 0; pass < 2; pass++) /* 2a passada: a aba arrastada, por cima */ for (int sl = 0; sl < vn; sl++) { int i = vis[sl]; @@ -3748,18 +3940,28 @@ static void render_tab_bar(Ed* ed, TinyBlocks_RenderableBlock* rb) { lx = tx + 24; } rb_text_c(rb, lx, cy, ed->tabs[i].name, cur ? COL_MENU : COL_TEXT, FS_UI); + int an = ax_push(ed, TB_A11Y_TAB, ed->tabs[i].name, tx, y0 + 6, TAB_W - 4, TAB_H - 6); + tb_a11y_set_point(ax(ed), an, tx + 14, cy); + tb_a11y_set_states(ax(ed), an, cur ? TB_A11Y_SELECTED : 0); + tb_a11y_set_actions(ax(ed), an, TB_A11Y_ACT_ACTIVATE | TB_A11Y_ACT_MENU | TB_A11Y_ACT_DRAG); + tb_a11y_set_description(ax(ed), an, tab_class(&ed->tabs[i])->name); if (ed->tab_n > 1 && ed->tabctx < 0) { /* fechar: um X desenhado */ float qx = tx + TAB_W - 18; rb_line(rb, qx - 3.5f, cy - 3.5f, qx + 3.5f, cy + 3.5f, 1.5f, COL_TEXT_DIM); rb_line(rb, qx + 3.5f, cy - 3.5f, qx - 3.5f, cy + 3.5f, 1.5f, COL_TEXT_DIM); + ax_button(ed, "Fechar aba", tx + TAB_W - 28, y0 + 6, 24, TAB_H - 6); } + ax_pop(ed); } float px = WS_X + 8 + vn*TAB_W; if (ed->tabctx < 0 && px + PLUS_W <= x0 + wbar && ed->tab_n < MAX_TABS) { float pcy = y0 + 6 + (TAB_H-6)*0.5f; if (ed->plus_menu_open) rb_fill_circle(rb, px+13, pcy, 12, COL_HOVER); rb_text_c(rb, px+8, pcy, "+", ed->plus_menu_open ? COL_MENU : COL_TEXT, 18); + tb_a11y_set_states(ax(ed), ax_button(ed, "Nova aba", px, y0, PLUS_W, TAB_H), + ed->plus_menu_open ? TB_A11Y_EXPANDED : 0); } + ax_pop(ed); if (ed->tabctx < 0) draw_grip(ed, rb, V_SCRIPTS); /* na janela de uma aba nao ha area para mover */ } @@ -3786,11 +3988,16 @@ static void render_categories(Ed* ed, TinyBlocks_RenderableBlock* rb) { rb_fill_circle(rb, mid, cy, r - 1, ed->cats[c].col); float w = tw(ed->cats[c].name, fs); rb_text_c(rb, mid - w*0.5f, iy + ih*0.78f, ed->cats[c].name, COL_TEXT, fs); + if (c == 0) ax_push(ed, TB_A11Y_TAB_LIST, "Categorias", PAL_X, PAL_Y, CAT_W, PAL_B - PAL_Y); + tb_a11y_set_states(ax(ed), ax_click(ed, TB_A11Y_TAB, ed->cats[c].name, PAL_X, iy, CAT_W - 1, ih), + c == cur ? TB_A11Y_SELECTED : 0); + if (c == ed->cat_n - 1) ax_pop(ed); } /* Botao de buscar/adicionar library, no fim do painel (como o Scratch) */ float by = PAL_B - LIBMAN_BTN_H; bool hov = hit_libman_foot(ed, ed->mouse_x, ed->mouse_y); rb_fill_rect(rb, PAL_X, by, CAT_W-1, LIBMAN_BTN_H, hov ? COL_LIBBTN_DARK : COL_LIBBTN); + ax_button(ed, "Adicionar extensão", PAL_X, by, CAT_W - 1, LIBMAN_BTN_H); float cx = mid, cy = by + LIBMAN_BTN_H*0.5f; for (int i = 0; i < 3; i++) /* livrinhos */ rb_fill_rect(rb, cx-14, cy-6 + i*5, 16, 3, COL_WHITE); @@ -3804,9 +4011,26 @@ static void render_flyout(Ed* ed, TinyBlocks_RenderableBlock* rb) { rb_line(rb, FLY_R-1, PAL_Y, FLY_R-1, PAL_B, 1.f, COL_BORDER); clip_set(ed, rb, FLY_X, PAL_Y, FLY_R - FLY_X - 1, PAL_B-PAL_Y); float oy = PAL_Y - ed->fly_scroll; + ax_push(ed, TB_A11Y_LIST, "Paleta", FLY_X, PAL_Y, FLY_R - FLY_X, PAL_B - PAL_Y); + bool ax_grp = false; for (int i = 0; i < ed->fly_n; i++) { FlyItem* it = &ed->fly_items[i]; float iy = it->y + oy; + if (ax(ed)) { /* a arvore tem a paleta toda; o que esta fora fica OFFSCREEN */ + uint32_t off = (iy + it->h < PAL_Y || iy > PAL_B) ? TB_A11Y_OFFSCREEN : 0; + if (it->kind == FI_HEADER) { + if (ax_grp) ax_pop(ed); + tb_a11y_set_states(ax(ed), ax_push(ed, TB_A11Y_GROUP, ed->cats[it->cat].name, + FLY_X, iy, FLY_R - FLY_X, it->h), off); + ax_grp = true; + } else if (it->kind == FI_BUTTON) { + tb_a11y_set_states(ax(ed), ax_button(ed, "Criar um Bloco", FLY_X + FLY_PAD, iy + 4, FLY_BTN_W, 30), off); + } else { + int before = tb_a11y_count(ax(ed)); + ax_block(ed, &ed->fly, it->bi, FLY_X, oy, TB_A11Y_ACT_ACTIVATE | TB_A11Y_ACT_DRAG); + if (off) for (int k = before; k < tb_a11y_count(ax(ed)); k++) tb_a11y_add_states(ax(ed), k, off); + } + } if (iy + it->h < PAL_Y || iy > PAL_B) continue; switch (it->kind) { case FI_HEADER: @@ -3823,6 +4047,8 @@ static void render_flyout(Ed* ed, TinyBlocks_RenderableBlock* rb) { break; } } + if (ax_grp) ax_pop(ed); + ax_pop(ed); clip_clear(ed, rb); } @@ -3874,7 +4100,7 @@ static char* decode_entities(const char* s, int n) { return out; } -/* -- Buffer de texto da aba de codigo ---------------------------------- */ +/* -- Buffer de texto da aba de codigo --- */ static bool code_reserve(Tab* T, int extra) { if (T->code_len + extra + 1 <= T->code_cap) return true; int cap = T->code_cap ? T->code_cap : 4096; @@ -4100,7 +4326,7 @@ void open_code_at(Ed* ed, const char* file, int line) { if (line > 0) code_scroll_to(ed, T, line); } -/* -- Render da aba de codigo ------------------------------------------- */ +/* -- Render da aba de codigo --- */ void render_code_tab(Ed* ed, TinyBlocks_RenderableBlock* rb) { Tab* T = &CT; const float x0 = WS_X, y0 = WS_Y; @@ -4175,7 +4401,7 @@ void render_code_tab(Ed* ed, TinyBlocks_RenderableBlock* rb) { (void)ed; } -/* -- Entrada (mouse/teclado) da aba de codigo -------------------------- */ +/* -- Entrada (mouse/teclado) da aba de codigo --- */ bool code_click(Ed* ed, float mx, float my) { Tab* T = &CT; const float barh = 30; @@ -4269,7 +4495,7 @@ bool code_text(Ed* ed, const char* typed) { } /* A aba de imagem vive em libraries/tinyblocks-editor-image (bundle do editor). */ -/* -- Menu do botao "+" -------------------------------------------------- */ +/* -- Menu do botao "+" --- */ static void plus_menu_pos(Ed* ed, float* px, float* py) { int vis[MAX_TABS]; *px = WS_X + 8 + tabs_visible_list(ed, vis) * TAB_W; @@ -4291,17 +4517,21 @@ void render_plus_menu(Ed* ed, TinyBlocks_RenderableBlock* rb) { "editar SVG/imagem", "editar .c/.py/..." }; static const Col dots[PLUS_MENU_N] = {{0.30f,0.62f,1.f,1.f}, {1.f,0.40f,0.50f,1.f}, {0.98f,0.62f,0.25f,1.f}, {0.42f,0.72f,0.35f,1.f}}; + ax_push(ed, TB_A11Y_MENU, "Nova aba", px, py, PLUS_MENU_W, h); for (int i = 0; i < n; i++) { float iy = py + 26 + i * PLUS_MENU_ITEM_H; + tb_a11y_set_description(ax(ed), ax_click(ed, TB_A11Y_MENU_ITEM, labels[i], px, iy, + PLUS_MENU_W, PLUS_MENU_ITEM_H), subs[i]); bool hov = ed->plus_menu_hover == i; if (hov) rb_fill_rrect(rb, px + 4, iy, PLUS_MENU_W - 8, PLUS_MENU_ITEM_H - 2, 6, COL_HOVER); rb_fill_circle(rb, px + 20, iy + PLUS_MENU_ITEM_H * 0.5f - 1, 6, dots[i]); rb_text_c(rb, px + 36, iy + 12, labels[i], COL_TEXT, FS_UI - 1); rb_text_c(rb, px + 36, iy + 25, subs[i], COL_TEXT_DIM, 10.f); } + ax_pop(ed); } -/* -- Tooltip de metadados (Alt + hover) --------------------------------- */ +/* -- Tooltip de metadados (Alt + hover) --- */ void render_tooltip(Ed* ed, TinyBlocks_RenderableBlock* rb) { if (!ed->tip_open) return; float x = ed->tip_x, y = ed->tip_y, w = ed->tip_w, h = ed->tip_h; @@ -4395,13 +4625,37 @@ static bool image_tab_icon(TinyBlocks_RenderableBlock* rb, float x, float cy) { return true; } +/* Aba de codigo: um campo de texto; o valor e a linha do cursor */ +static void code_tab_a11y(Ed* ed) { + const char* txt = CT.code_text ? CT.code_text : ""; + int caret = CT.code_caret < 0 ? 0 : CT.code_caret > CT.code_len ? CT.code_len : CT.code_caret; + int st = caret, en = caret, line = 1; + while (st > 0 && txt[st - 1] != '\n') st--; + while (en < CT.code_len && txt[en] != '\n') en++; + for (int i = 0; i < st; i++) if (txt[i] == '\n') line++; + char val[TB_A11Y_VALUE_MAX], desc[TB_A11Y_DESC_MAX]; + snprintf(val, sizeof(val), "%.*s", en - st, txt + st); + snprintf(desc, sizeof(desc), "linha %d - %.150s", line, CT.code_path[0] ? CT.code_path : "sem arquivo"); + int n = ax_field(ed, "Código", val, true, WS_X, WS_Y, WS_W, WS_H); + tb_a11y_set_description(ax(ed), n, desc); +} + +/* Aba de imagem: a tela de desenho, com o tamanho como valor */ +static void image_tab_a11y(Ed* ed) { + char val[32]; + snprintf(val, sizeof(val), "%d x %d", CT.img_w, CT.img_h); + int n = ax_add(ed, TB_A11Y_CANVAS, "Imagem", WS_X, WS_Y, WS_W, WS_H); + tb_a11y_set_value(ax(ed), n, val); + tb_a11y_set_description(ax(ed), n, CT.img_path[0] ? CT.img_path : "sem arquivo"); +} + static const TabClass s_tab_classes[] = { - [TK_BLOCKS] = { "Blocos", true, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL }, + [TK_BLOCKS] = { "Blocos", true, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL }, [TK_CODE] = { "Código", false, NULL, render_code_tab, code_tab_click, NULL, NULL, - code_key, code_text, code_tab_scroll, code_tab_icon }, + code_key, code_text, code_tab_scroll, code_tab_icon, code_tab_a11y }, [TK_IMAGE] = { "Imagem", false, image_tab_init, image_tab_render, image_tab_click, image_tab_release, image_tab_motion, image_tab_key, NULL, - image_tab_scroll, image_tab_icon }, + image_tab_scroll, image_tab_icon, image_tab_a11y }, }; const TabClass* tab_class(const Tab* T) { @@ -4534,9 +4788,14 @@ static void group_motion(Ed* ed, float mx, float my) { layout_tab(T); } +static void ax_workspace(Ed* ed, float ox, float oy); + static void render_workspace(Ed* ed, TinyBlocks_RenderableBlock* rb) { const TabClass* tc = tab_class(&CT); - if (tc->render) { tc->render(ed, rb); return; } + int panel = ax_push(ed, TB_A11Y_TAB_PANEL, CT.name, WS_X, WS_Y, WS_W, WS_H); + tb_a11y_set_description(ax(ed), panel, tc->name); + if (tc->a11y) tc->a11y(ed); + if (tc->render) { tc->render(ed, rb); ax_pop(ed); return; } float ox = WS_X - CT.ws_sx, oy = WS_Y - CT.ws_sy; rb_fill_rect(rb, WS_X, WS_Y, WS_W, WS_H, COL_WS_BG); clip_set(ed, rb, WS_X, WS_Y, WS_W, WS_H); @@ -4562,6 +4821,14 @@ static void render_workspace(Ed* ed, TinyBlocks_RenderableBlock* rb) { render_chain(ed, rb, &CT, CT.top[i], ox, oy); } render_snap(ed, rb, ox, oy); + ax_workspace(ed, ox, oy); + if (ed->bubble_bi >= 0 && CT.ws[ed->bubble_bi].used) { /* balao com o valor do reporter */ + EdBlock* b = &CT.ws[ed->bubble_bi]; + int n = ax_add(ed, TB_A11Y_STATUS, "Resultado", b->ax + ox + 8, b->ay + oy - 44, 120, 38); + tb_a11y_set_value(ax(ed), n, ed->bubble_text); + tb_a11y_set_description(ax(ed), n, ed->bubble_type); + } + ax_pop(ed); if (ed->bubble_bi >= 0 && CT.ws[ed->bubble_bi].used) { EdBlock* b = &CT.ws[ed->bubble_bi]; @@ -4637,6 +4904,35 @@ static void render_workspace(Ed* ed, TinyBlocks_RenderableBlock* rb) { clip_clear(ed, rb); } +/* Grupos coloridos e pilhas da aba de blocos (cada pilha e uma LIST com os + * blocos de cima para baixo; o rotulo do 1o bloco da nome a pilha) */ +static void ax_workspace(Ed* ed, float ox, float oy) { + TinyBlocks_A11yTree* t = ax(ed); + if (!t) return; + for (int g = 0; g < CT.group_n; g++) { + WsGroup* G = &CT.groups[g]; + int n = tb_a11y_add(t, TB_A11Y_GROUP, G->title[0] ? G->title : "Grupo", + ox + G->x, oy + G->y, G->w, G->h); + tb_a11y_set_key(t, n, "grupo"); + tb_a11y_set_point(t, n, ox + G->x + 12, oy + G->y + GRP_HEAD_H * 0.5f); + tb_a11y_set_actions(t, n, TB_A11Y_ACT_DRAG | TB_A11Y_ACT_MENU); + if (ed->grp_edit == g) tb_a11y_set_states(t, n, TB_A11Y_FOCUSED | TB_A11Y_EDITING); + } + for (int i = 0; i < CT.top_n; i++) { + int head = CT.top[i]; + if (ed->state == ES_DRAG && head == ed->drag_bi) continue; + float x0, y0, x1, y1; + stack_bounds(&CT, head, &x0, &y0, &x1, &y1); + char label[TB_A11Y_NAME_MAX], name[TB_A11Y_NAME_MAX + 8]; + ax_block_label(&CT, head, label, sizeof(label), 0); + snprintf(name, sizeof(name), "Pilha: %s", label); + int n = tb_a11y_push(t, TB_A11Y_LIST, name, x0 + ox, y0 + oy, x1 - x0, y1 - y0); + tb_a11y_set_key(t, n, CT.ws[head].type_name); + ax_chain(ed, &CT, head, ox, oy, TB_A11Y_ACT_ACTIVATE | TB_A11Y_ACT_MENU | TB_A11Y_ACT_DRAG); + tb_a11y_pop(t); + } +} + /* == Barra do bloco composto (canto superior direito do workspace) === */ #define CP_W 440 #define CP_H 44 @@ -4664,6 +4960,10 @@ static void render_comp_panel(Ed* ed, TinyBlocks_RenderableBlock* rb) { bx -= 96; rb_fill_rrect(rb, bx, py+8, 88, 28, 6, COL_BORDER); rb_text_c(rb, bx + (88 - tw("Editar bloco", FS_UI))*0.5f, py+CP_H*0.5f, "Editar bloco", COL_TEXT, FS_UI); + ax_push(ed, TB_A11Y_TOOLBAR, nm, px, py, CP_W, CP_H); + ax_button(ed, "Editar bloco", bx, py + 8, 88, 28); + ax_button(ed, "Salvar", bx + 96, py + 8, 78, 28); + ax_pop(ed); } /* Altura da área do palco no painel (4:3 embutido; faixa curta se destacado) */ @@ -4684,12 +4984,15 @@ static void render_stage(Ed* ed, TinyBlocks_RenderableBlock* rb) { draw_hex(rb, tx+10, fy+6, 20, 20, COL_STOP_DARK); rb_fill_circle(rb, tx+20, fy+16, 9, COL_STOP); rb_text_c(rb, sx + 110, sy + STAGE_BAR_H*0.5f, "Palco", COL_TEXT_DIM, FS_UI); + tb_a11y_set_states(ax(ed), ax_button(ed, "Executar", fx, fy, 40, 32), ed->vm_running ? TB_A11Y_BUSY : 0); + ax_button(ed, "Parar", tx, fy, 40, 32); draw_grip(ed, rb, V_STAGE); /* Destacar / recolher o palco */ if (ed->stage) { float dx = sx + A.w - 50; if (ed->detach_hover) rb_fill_rrect(rb, dx, fy, 40, 32, 6, COL_HOVER); draw_window_icon(rb, dx, fy, 32, A.detached, COL_TEXT); + ax_button(ed, A.detached ? "Recolher o palco" : "Destacar o palco", dx, fy, 40, 32); } float bx = sx + 8, by = sy + STAGE_BAR_H, bw = A.w - 16; @@ -4698,6 +5001,7 @@ static void render_stage(Ed* ed, TinyBlocks_RenderableBlock* rb) { rb_fill_rrect(rb, bx, by, bw, ph, 8, COL_BORDER); TinyBlocks_RenderableBlock* sub = NULL; tb_stage_set_view(bw-2, ph-2); + ax_add(ed, TB_A11Y_CANVAS, "Palco", bx, by, bw, ph); clip_set(ed, rb, bx+1, by+1, bw-2, ph-2); if (ed->stage->type->on_draw(ed->stage, ed->rt, &sub) && sub) { tb_draw_merge_offset(rb, sub, bx+1, by+1); @@ -4720,6 +5024,7 @@ static void render_output(Ed* ed, TinyBlocks_RenderableBlock* rb) { /* Monitores de variáveis */ float y = by + 10; + ax_push(ed, TB_A11Y_LIST, "Variáveis", bx, by, bw, bh * 0.5f); for (int v = 0; v < tb_variables_count() && y < by + bh*0.5f; v++) { const char* vname = tb_variables_name(v); const TinyBlocks_Value* vv = tb_variables_value(v); @@ -4732,8 +5037,11 @@ static void render_output(Ed* ed, TinyBlocks_RenderableBlock* rb) { rb_text_c(rb, bx+18, y+13, vname, COL_TEXT, FS); rb_fill_rrect(rb, bx+18+nw+6, y+4, vw, 18, 4, COL_VAR); rb_text_c(rb, bx+18+nw+6 + (vw - tw(val, FS))*0.5f, y+13, val, COL_WHITE, FS); + tb_a11y_set_value(ax(ed), ax_add(ed, TB_A11Y_LABEL, vname, bx + 10, y, mw, 26), val); y += 32; } + ax_pop(ed); + ax_push(ed, TB_A11Y_LIST, "Console", bx, y, bw, by + bh - y); /* Console: mostra as últimas linhas que cabem */ const float lh = FS_SM + 5; @@ -4749,11 +5057,23 @@ static void render_output(Ed* ed, TinyBlocks_RenderableBlock* rb) { memcpy(line, p, m); line[m] = 0; bool err = strncmp(line, "[ERRO]", 6) == 0 || strncmp(line, "[AVISO]", 7) == 0; rb_text_c(rb, bx + 12, y + lh*0.5f, line, err ? COL_ERR_TEXT : COL_TEXT, FS_SM); + ax_add(ed, TB_A11Y_LABEL, line, bx + 12, y, bw - 24, lh); y += lh; } p += n; if (*p == '\n') p++; } + ax_pop(ed); + { /* a ultima linha escrita: o narrador le quando muda */ + const char* last = ed->stage_buf; + size_t L = strlen(last); + while (L > 0 && last[L - 1] == '\n') L--; + size_t st = L; + while (st > 0 && last[st - 1] != '\n') st--; + char line[TB_A11Y_VALUE_MAX]; + snprintf(line, sizeof(line), "%.*s", (int)(L - st), last + st); + tb_a11y_set_value(ax(ed), ax_add(ed, TB_A11Y_STATUS, "Última saída", bx, by, bw, bh), line); + } clip_clear(ed, rb); draw_grip(ed, rb, V_OUTPUT); } @@ -4801,11 +5121,14 @@ static void render_ctx_menu(Ed* ed, TinyBlocks_RenderableBlock* rb) { rb_fill_rrect(rb, x+2, y+3, CTX_W, mh, 6, COLA(0,0,0,40)); rb_fill_rrect(rb, x, y, CTX_W, mh, 6, COL_BORDER); rb_fill_rrect(rb, x+1, y+1, CTX_W-2, mh-2, 5, COL_PANEL); + ax_push(ed, TB_A11Y_MENU, "Menu de contexto", x, y, CTX_W, mh); for (int i = 0; i < ed->ctx_n; i++) { float iy = y + 4 + i*CTX_ITEM_H; if (ed->ctx_menu_hover == i) rb_fill_rect(rb, x+1, iy, CTX_W-2, CTX_ITEM_H, COL_UI_BG); rb_text_c(rb, x+14, iy + CTX_ITEM_H*0.5f, ctx_label(ed->ctx_items[i]), COL_TEXT, FS_UI); + ax_click(ed, TB_A11Y_MENU_ITEM, ctx_label(ed->ctx_items[i]), x, iy, CTX_W, CTX_ITEM_H); } + ax_pop(ed); } /* === @@ -4894,7 +5217,9 @@ void libman_render(Ed* ed, TinyBlocks_RenderableBlock* rb) { rb_line(rb, bx-5, by, bx+4, by+7, 2.f, COL_WHITE); rb_text_c(rb, bx + 28, by, "Voltar", COL_WHITE, FS_UI); { const char* t = "Escolha uma Extensão"; - rb_text_c(rb, (WIN_W - tw(t, 20))*0.5f, by, t, COL_WHITE, 20); } + rb_text_c(rb, (WIN_W - tw(t, 20))*0.5f, by, t, COL_WHITE, 20); + tb_a11y_set_states(ax(ed), ax_push(ed, TB_A11Y_DIALOG, t, 0, 0, WIN_W, WIN_H), TB_A11Y_MODAL); + ax_button(ed, "Voltar", 0, 0, 170, LM_HEAD_H); } /* Barra de filtros */ float fh = libman_chip_layout(ed, NULL); @@ -4910,6 +5235,7 @@ void libman_render(Ed* ed, TinyBlocks_RenderableBlock* rb) { float cw = tw(ed->libman_query, FS_UI); rb_fill_rect(rb, sx + 16 + cw, sy + 12, 1.5f, 20, COL_TEXT); } + ax_field(ed, "Buscar", ed->libman_query, ed->libman_search_focus, sx, sy, LM_SEARCH_W, 44); } { const char* labels[LM_MAX_CHIPS]; @@ -4922,12 +5248,15 @@ void libman_render(Ed* ed, TinyBlocks_RenderableBlock* rb) { sel ? COL_MENU : COL_UI_BG); rb_text_c(rb, rects[i][0] + 14, rects[i][1] + LM_CHIP_H*0.5f, labels[i], sel ? COL_WHITE : COL_TEXT, FS_UI); + tb_a11y_set_states(ax(ed), ax_click(ed, TB_A11Y_RADIO, labels[i], rects[i][0], rects[i][1], + rects[i][2], rects[i][3]), sel ? TB_A11Y_CHECKED : 0); } } /* Grade de cartoes (com recorte e rolagem) */ rb_fill_rect(rb, 0, gridY, WIN_W, WIN_H - gridY, COL_UI_BG); clip_set(ed, rb, 0, gridY, WIN_W, WIN_H - gridY); + ax_push(ed, TB_A11Y_LIST, "Libraries", 0, gridY, WIN_W, WIN_H - gridY); for (int k = 0; k < nh; k++) { int col = k % 2, row = k / 2; float x = LM_MARGIN + col*(LM_CARD_W + LM_CARD_GAP); @@ -4949,11 +5278,18 @@ void libman_render(Ed* ed, TinyBlocks_RenderableBlock* rb) { rb_text_c(rb, x + LM_CARD_W - 110, y + LM_CARD_H - 22, "carregada", COL_FLAG, FS_SM); } if (ed->libman_hover == k) rb_stroke_rect(rb, x-1, y-1, LM_CARD_W+2, LM_CARD_H+2, 2.f, COL_MENU); + int an = ax_click(ed, TB_A11Y_LIST_ITEM, e->name, x, y, LM_CARD_W, LM_CARD_H); + tb_a11y_set_description(ax(ed), an, e->description); + tb_a11y_set_value(ax(ed), an, tb_libman_loaded(hits[k]) ? "carregada" : ""); } + ax_pop(ed); clip_clear(ed, rb); - if (nh == 0) + if (nh == 0) { rb_text_c(rb, LM_MARGIN, gridY + 40, "Nenhuma library encontrada.", COL_TEXT_DIM, FS_UI); + ax_add(ed, TB_A11Y_LABEL, "Nenhuma library encontrada.", LM_MARGIN, gridY + 30, 300, 20); + } + ax_pop(ed); } void libman_click(Ed* ed, float mx, float my) { @@ -5128,7 +5464,9 @@ void settings_render(Ed* ed, TinyBlocks_RenderableBlock* rb) { rb_line(rb, bx-5, by, bx+4, by+7, 2.f, COL_WHITE); rb_text_c(rb, bx + 28, by, "Voltar", COL_WHITE, FS_UI); { const char* t = "Configurações"; - rb_text_c(rb, (WIN_W - tw(t, 20)) * 0.5f, by, t, COL_WHITE, 20); } + rb_text_c(rb, (WIN_W - tw(t, 20)) * 0.5f, by, t, COL_WHITE, 20); + tb_a11y_set_states(ax(ed), ax_push(ed, TB_A11Y_DIALOG, t, 0, 0, WIN_W, WIN_H), TB_A11Y_MODAL); + ax_button(ed, "Voltar", 0, 0, 170, ST_HEAD_H); } /* Opcoes: toggle de deteccao do caminho pela variavel de ambiente */ rb_fill_rect(rb, 0, ST_OPT_Y, WIN_W, ST_OPT_H, COL_FLY); @@ -5142,6 +5480,8 @@ void settings_render(Ed* ed, TinyBlocks_RenderableBlock* rb) { rb_line(rb, ST_MARGIN + 8, cb + 15, ST_MARGIN + 16, cb + 5, 2.5f, COL_WHITE); } rb_text_c(rb, ST_MARGIN + 30, ST_OPT_Y + ST_OPT_H*0.5f, ST_ENV_LABEL, COL_TEXT, FS_UI); + tb_a11y_set_states(ax(ed), ax_click(ed, TB_A11Y_CHECKBOX, ST_ENV_LABEL, ST_MARGIN, ST_OPT_Y, 540, ST_OPT_H), + ed->settings_use_env ? TB_A11Y_CHECKED : 0); const char* e = tb_environ_env_path(); const char* hint = e[0] ? e : "TINYBLOCKS_SETTINGS nao definida"; rb_text_c(rb, WIN_W - ST_MARGIN - tw(hint, FS_SM), ST_OPT_Y + ST_OPT_H*0.5f, @@ -5159,17 +5499,23 @@ void settings_render(Ed* ed, TinyBlocks_RenderableBlock* rb) { float cw = tw(ed->settings_path, FS_UI); rb_fill_rect(rb, px + 13 + cw, py + ph*0.5f - 9, 1.5f, 18, COL_TEXT); } + ax_field(ed, "Arquivo de configurações", ed->settings_path, ed->settings_path_focus, px, py, pw, ph); } for (int i = 0; i < 3; i++) { float x, y, w, h; settings_btn_rect(ed, i, &x, &y, &w, &h); bool hov = in_rect(ed->mouse_x, ed->mouse_y, x, y, w, h); st_button(rb, x, y, w, h, i == 0 ? "Salvar" : i == 1 ? "Exportar CSV" : "+ Nova", hov); + ax_button(ed, i == 0 ? "Salvar" : i == 1 ? "Exportar CSV" : "Nova configuração", x, y, w, h); } /* Lista */ rb_fill_rect(rb, 0, ST_LIST_Y, WIN_W, WIN_H - ST_LIST_Y, COL_UI_BG); clip_set(ed, rb, 0, ST_LIST_Y, WIN_W, WIN_H - ST_LIST_Y); + ax_push(ed, TB_A11Y_LIST, "Configurações", 0, ST_LIST_Y, WIN_W, WIN_H - ST_LIST_Y); + if (ed->settings_new_key) + ax_field(ed, "Nome da nova chave", ed->settings_key, true, + ST_MARGIN, ST_LIST_Y - ed->settings_scroll, WIN_W - 2*ST_MARGIN, ST_ROW_H); if (ed->settings_new_key) { float y = ST_LIST_Y - ed->settings_scroll; @@ -5201,12 +5547,19 @@ void settings_render(Ed* ed, TinyBlocks_RenderableBlock* rb) { } else { rb_text_c(rb, vx, y + h*0.5f, tb_environ_value_at(i), COL_TEXT_DIM, FS_UI); } + bool editing = sel && ed->settings_value_edit; + int an = ax_click(ed, TB_A11Y_LIST_ITEM, tb_environ_key(i), x, y, w, h); + tb_a11y_set_value(ax(ed), an, editing ? ed->settings_edit : tb_environ_value_at(i)); + tb_a11y_set_states(ax(ed), an, (sel ? TB_A11Y_SELECTED : 0) | TB_A11Y_EDITABLE | + (editing ? TB_A11Y_FOCUSED | TB_A11Y_EDITING : 0)); } + ax_pop(ed); clip_clear(ed, rb); if (n == 0 && !ed->settings_new_key) rb_text_c(rb, ST_MARGIN, ST_LIST_Y + 24, "Nenhuma configuracao. Use \"+ Nova\".", COL_TEXT_DIM, FS_UI); + ax_pop(ed); } void settings_click(Ed* ed, float mx, float my) { @@ -5832,7 +6185,14 @@ static void libman_scroll_m(Ed* ed, float dy) { libman_scroll_by(ed, dy); } /* o explorador vem do modulo do editor (pode faltar) */ static bool files_is_open(Ed* ed) { return ed->fdlg.open; } -static void files_render_m(Ed* ed, TinyBlocks_RenderableBlock* rb) { if (files_view) files_view(ed, rb); } +static void files_render_m(Ed* ed, TinyBlocks_RenderableBlock* rb) { + if (!files_view) return; + files_view(ed, rb); + /* o modulo desenha; aqui so o dialogo e o caminho (o conteudo fica para o modulo) */ + tb_a11y_set_states(ax(ed), ax_push(ed, TB_A11Y_DIALOG, ed->fdlg.mode == PM_LOAD ? "Abrir" : "Salvar", + 0, 0, WIN_W, WIN_H), TB_A11Y_MODAL); + ax_pop(ed); +} static void files_click_m(Ed* ed, float mx, float my) { if (files_click) files_click(ed, mx, my); } static void files_key_m(Ed* ed, int sc, bool ctrl) { if (files_key) files_key(ed, sc, ctrl); else ed->fdlg.open = false; } static void files_text_m(Ed* ed, const char* t) { if (files_text) files_text(ed, t); } @@ -6415,6 +6775,72 @@ static bool any_window_detached(Ed* ed) { /* Entrada de uma janela do editor: a principal (from = -1) ou a de uma view destacada (from = V_*). Pontos viram coordenadas da janela principal. */ +/* O que esta sob o mouse (botoes realcados, item de menu) */ +static void hover_update(Ed* ed, float mx, float my) { + ed->flag_hover = hit_flag(ed, mx, my); + ed->stop_hover = hit_stop(ed, mx, my); + ed->lib_hover = hit_lib_btn(mx, my); + ed->detach_hover = hit_detach(ed, mx, my); + + ed->ctx_menu_hover = ed->ctx_menu_open ? ctx_hit(ed, mx, my) : -1; + + ed->plus_menu_hover = -1; + if (ed->plus_menu_open) { + float px, py; + plus_menu_pos(ed, &px, &py); + for (int i = 0; i < PLUS_MENU_N; i++) + if (in_rect(mx, my, px, py + 26 + i * PLUS_MENU_ITEM_H, PLUS_MENU_W, PLUS_MENU_ITEM_H)) + ed->plus_menu_hover = i; + } +} + +/* Acoes da arvore acessivel: agem como o mouse no ponto do componente + * (o mesmo caminho do clique, entao narrador, testes e mouse nao divergem) */ +static bool ax_perform(void* user, const TinyBlocks_A11yTree* t, int node, TinyBlocks_A11yAction a) { + Ed* ed = user; + float x, y; + tb_a11y_point(t, node, &x, &y); + g_active_ed = ed; + lay_compute(ed); + if (tb_a11y_node(t, node)->states & TB_A11Y_OFFSCREEN) { + /* fora da vista: rola ate ele antes (como o GTK faz) */ + TinyBlocks_A11yRole area = TB_A11Y_NONE; + for (int p = node; p >= 0 && area == TB_A11Y_NONE; p = tb_a11y_node(t, p)->parent) { + const TinyBlocks_A11yNode* nd = tb_a11y_node(t, p); + if (nd->role == TB_A11Y_TAB_PANEL || (nd->role == TB_A11Y_LIST && !strcmp(nd->name, "Paleta"))) + area = nd->role; + } + if (area == TB_A11Y_TAB_PANEL) { + float dx = x - (WS_X + WS_W * 0.5f), dy = y - (WS_Y + WS_H * 0.5f); + CT.ws_sx += dx; CT.ws_sy += dy; + x -= dx; y -= dy; + } else if (area == TB_A11Y_LIST) { + float before = ed->fly_scroll; + ed->fly_scroll += y - (PAL_Y + (PAL_B - PAL_Y) * 0.5f); + if (ed->fly_scroll > fly_max_scroll(ed)) ed->fly_scroll = fly_max_scroll(ed); + if (ed->fly_scroll < 0) ed->fly_scroll = 0; + y -= ed->fly_scroll - before; + } + } + ed->mouse_x = x; ed->mouse_y = y; + layout_tab(&CT); + hover_update(ed, x, y); + switch (a) { + case TB_A11Y_ACT_ACTIVATE: + case TB_A11Y_ACT_EDIT: + ed->capture_view = -1; + on_lmb_down(ed, x, y); + on_lmb_up(ed, x, y); + ed->capture_view = -2; + return true; + case TB_A11Y_ACT_MENU: + on_rmb_down(ed, x, y); + return true; + default: + return false; /* arrastar precisa de um destino: use o retangulo */ + } +} + static void ed_handle_input(Ed* ed, TinyBlocks_Block* evb, int from) { g_active_ed = ed; if (from < 0) { @@ -6479,22 +6905,7 @@ static void ed_handle_input(Ed* ed, TinyBlocks_Block* evb, int from) { if (from < 0) toasts_update(ed); layout_tab(&CT); - /* == Hover === */ - ed->flag_hover = hit_flag(ed, mx, my); - ed->stop_hover = hit_stop(ed, mx, my); - ed->lib_hover = hit_lib_btn(mx, my); - ed->detach_hover = hit_detach(ed, mx, my); - - ed->ctx_menu_hover = ed->ctx_menu_open ? ctx_hit(ed, mx, my) : -1; - - ed->plus_menu_hover = -1; - if (ed->plus_menu_open) { - float px, py; - plus_menu_pos(ed, &px, &py); - for (int i = 0; i < PLUS_MENU_N; i++) - if (in_rect(mx, my, px, py + 26 + i * PLUS_MENU_ITEM_H, PLUS_MENU_W, PLUS_MENU_ITEM_H)) - ed->plus_menu_hover = i; - } + hover_update(ed, mx, my); update_tooltip(ed, (ev_int(evb, GFX_IN_MOD_STATE) & TB_MOD_ALT) != 0); @@ -6762,7 +7173,14 @@ static bool editor_on_draw(TinyBlocks_Block* self, TinyBlocks* rt, bool saved[V_COUNT]; areas_only(ed, -1, saved); /* as destacadas desenham na janela delas */ - for (int v = 0; v < V_COUNT; v++) view_class(v)->render(ed, rb); /* as escondidas nao desenham */ + ed->ax_on = ed->ax_enabled && ed->ax_tree; + tb_a11y_begin(ax(ed), "TinyBlocks", WIN_W, WIN_H); + for (int v = 0; v < V_COUNT; v++) { /* as escondidas nao desenham */ + bool shown = AREA(v).shown; + if (shown) ax_push(ed, TB_A11Y_PANE, view_name(v), AREA(v).x, AREA(v).y, AREA(v).w, AREA(v).h); + view_class(v)->render(ed, rb); + if (shown) ax_pop(ed); + } render_menu(ed, rb); /* A pilha arrastada fica por cima de tudo (pode passar sobre a paleta) */ @@ -6775,6 +7193,8 @@ static bool editor_on_draw(TinyBlocks_Block* self, TinyBlocks* rt, render_ctx_menu(ed, rb); modals_render(ed, rb); /* de baixo para cima; configuracoes por cima de tudo */ render_toasts(ed, rb); /* avisos flutuam por cima de tudo */ + tb_a11y_end(ax(ed)); + ed->ax_on = false; areas_restore(ed, saved); *dst = rb; @@ -6828,6 +7248,16 @@ static bool editor_instance_init(TinyBlocks_Block* self, TinyBlocks* rt) { ed->split_hover = ed->split_drag = ed->area_drag = ed->area_drop = -1; ed->swap_pending[0] = ed->swap_pending[1] = -1; ed->mouse_owner = -1; + ed->ax_tree = tb_a11y_tree_new(); + tb_a11y_set_perform(ed->ax_tree, ax_perform, ed); + { /* TINYBLOCKS_A11Y=0 desliga; =log escreve os eventos no stderr (um narrador de texto) */ + const char* env = getenv("TINYBLOCKS_A11Y"); + ed->ax_enabled = !(env && strcmp(env, "0") == 0); + if (env && strcmp(env, "log") == 0) { + TinyBlocks_A11yBridge b = tb_a11y_log_bridge(stderr); + tb_a11y_set_bridge(ed->ax_tree, &b); + } + } ed->capture_view = -2; ed->tab_press = -1; ed->tabctx = -1; @@ -6905,11 +7335,18 @@ static void editor_instance_deinit(TinyBlocks_Block* self, TinyBlocks* rt) { free(ed->run_blocks); for (int u = 0; u < ed->user_block_n; u++) free(ed->user_blocks[u].snap); tb_libman_shutdown(); + tb_a11y_tree_free(ed->ax_tree); if (g_active_ed == ed) g_active_ed = NULL; free(ed); *(Ed**)self->userdata = NULL; } +TB_API TinyBlocks_A11yTree* tb_editor_accessible(TinyBlocks_Block* scene) { + if (!scene || !scene->userdata) return NULL; + Ed* ed = *(Ed**)scene->userdata; + return ed && ed->ax_enabled ? ed->ax_tree : NULL; +} + /* === * Registra a library * === */ diff --git a/tests/a11y_test.c b/tests/a11y_test.c new file mode 100644 index 0000000..6df1a8a --- /dev/null +++ b/tests/a11y_test.c @@ -0,0 +1,127 @@ +/* Teste da tinyblocks-a11y: arvore acessivel, busca, ids, eventos e acoes */ +#include +#include +#include + +static int fails = 0; +#define CHECK(cond, msg) do { if (cond) printf("ok %s\n", msg); else { printf("FAIL %s\n", msg); fails++; } } while (0) + +static int performed = -1; +static bool perform(void* user, const TinyBlocks_A11yTree* t, int node, TinyBlocks_A11yAction a) { + (void)user; (void)t; + if (a != TB_A11Y_ACT_ACTIVATE) return false; + performed = node; + return true; +} + +static int bridge_events = 0; +static void on_event(void* user, const TinyBlocks_A11yTree* t, const TinyBlocks_A11yEvent* ev) { + (void)user; (void)t; (void)ev; + bridge_events++; +} + +/* Um quadro de uma interface pequena; `sel` = aba selecionada, `edit` = campo com foco */ +static void build(TinyBlocks_A11yTree* t, int sel, bool edit, const char* value, bool dialog) { + tb_a11y_begin(t, "App", 800, 600); + tb_a11y_push(t, TB_A11Y_TOOLBAR, "Menu", 0, 0, 800, 40); + int b = tb_a11y_add(t, TB_A11Y_BUTTON, "Abrir", 10, 5, 60, 30); + tb_a11y_set_actions(t, b, TB_A11Y_ACT_ACTIVATE); + tb_a11y_add(t, TB_A11Y_BUTTON, "Salvar", 80, 5, 60, 30); + tb_a11y_pop(t); + tb_a11y_push(t, TB_A11Y_TAB_LIST, "Abas", 0, 40, 800, 30); + for (int i = 0; i < 3; i++) { + char nm[16]; + snprintf(nm, sizeof(nm), "Aba %d", i + 1); + int a = tb_a11y_add(t, TB_A11Y_TAB, nm, 10 + i * 100, 40, 90, 30); + tb_a11y_set_states(t, a, i == sel ? TB_A11Y_SELECTED : 0); + } + tb_a11y_pop(t); + int blk = tb_a11y_push(t, TB_A11Y_BLOCK, value, 100, 100, 200, 40); /* nome muda com o valor */ + tb_a11y_set_key(t, blk, "Control.Repeat"); + tb_a11y_set_point(t, blk, 108, 120); + int f = tb_a11y_add(t, TB_A11Y_TEXT_FIELD, "vezes", 150, 108, 30, 24); + tb_a11y_set_value(t, f, value); + tb_a11y_set_states(t, f, TB_A11Y_EDITABLE | (edit ? TB_A11Y_FOCUSED | TB_A11Y_EDITING : 0)); + tb_a11y_pop(t); + if (dialog) { + tb_a11y_push(t, TB_A11Y_DIALOG, "Configurações", 100, 100, 600, 400); + tb_a11y_add(t, TB_A11Y_BUTTON, "Voltar", 100, 100, 80, 40); + tb_a11y_pop(t); + } + tb_a11y_end(t); +} + +static bool has_event(TinyBlocks_A11yTree* t, TinyBlocks_A11yEventKind k) { + for (int i = 0; i < tb_a11y_event_count(t); i++) if (tb_a11y_event(t, i)->kind == k) return true; + return false; +} + +int main(void) { + TinyBlocks_A11yTree* t = tb_a11y_tree_new(); + tb_a11y_set_perform(t, perform, NULL); + + build(t, 0, false, "repita (10) vezes", false); + CHECK(tb_a11y_count(t) == 10, "árvore com a raiz e 9 componentes"); + int menu = tb_a11y_find(t, 0, TB_A11Y_TOOLBAR, "Menu", 0); + int abrir = tb_a11y_find(t, menu, TB_A11Y_BUTTON, "Abrir", 0); + CHECK(menu > 0 && abrir > menu && tb_a11y_node(t, abrir)->parent == menu, "busca por papel + nome, dentro do pai"); + CHECK(tb_a11y_find(t, menu, TB_A11Y_TAB, NULL, 0) < 0, "a busca fica na subárvore"); + CHECK(tb_a11y_find(t, 0, TB_A11Y_TAB, NULL, 2) >= 0 && tb_a11y_find(t, 0, TB_A11Y_TAB, NULL, 3) < 0, + "n-ésimo componente"); + CHECK(tb_a11y_find_part(t, 0, TB_A11Y_BLOCK, "repita", 0) >= 0, "busca por parte do nome"); + CHECK(tb_a11y_event_count(t) == 0, "primeiro quadro não gera eventos"); + + char buf[256]; + CHECK(!strcmp(tb_a11y_path(t, abrir, buf, sizeof(buf)), "Menu > Abrir"), "caminho \"Menu > Abrir\""); + int aba1 = tb_a11y_find(t, 0, TB_A11Y_TAB, "Aba 1", 0); + CHECK(!strcmp(tb_a11y_describe(t, aba1, buf, sizeof(buf)), "Aba 1, aba, selecionado"), "frase do narrador com estado"); + + float x, y; + int blk = tb_a11y_find(t, 0, TB_A11Y_BLOCK, NULL, 0); + tb_a11y_point(t, blk, &x, &y); + CHECK(x == 108 && y == 120, "ponto de ação próprio"); + tb_a11y_center(t, abrir, &x, &y); + CHECK(x == 40 && y == 20, "centro do retângulo"); + + CHECK(tb_a11y_do_action(t, abrir, TB_A11Y_ACT_ACTIVATE) && performed == abrir, "ação executada por quem desenha"); + int salvar = tb_a11y_find(t, 0, TB_A11Y_BUTTON, "Salvar", 0); + CHECK(!tb_a11y_do_action(t, salvar, TB_A11Y_ACT_ACTIVATE), "ação não declarada é recusada"); + + uint32_t id_aba2 = tb_a11y_node(t, tb_a11y_find(t, 0, TB_A11Y_TAB, "Aba 2", 0))->id; + uint32_t id_blk = tb_a11y_node(t, blk)->id; + + TinyBlocks_A11yBridge br = { "teste", on_event, NULL, NULL }; + tb_a11y_set_bridge(t, &br); + + /* quadro 2: outra aba, campo editando com valor novo, dialogo aberto */ + build(t, 1, true, "repita (25) vezes", true); + int aba2 = tb_a11y_find(t, 0, TB_A11Y_TAB, "Aba 2", 0); + blk = tb_a11y_find(t, 0, TB_A11Y_BLOCK, NULL, 0); + CHECK(tb_a11y_node(t, aba2)->id == id_aba2, "id estável entre quadros"); + CHECK(tb_a11y_node(t, blk)->id == id_blk, "set_key: o id não muda quando o nome muda"); + CHECK(has_event(t, TB_A11Y_EV_STATE), "evento de estado (seleção mudou)"); + CHECK(has_event(t, TB_A11Y_EV_FOCUS), "evento de foco (campo editando)"); + CHECK(tb_a11y_focus(t) == tb_a11y_find(t, 0, TB_A11Y_TEXT_FIELD, "vezes", 0), "foco = campo editando"); + CHECK(has_event(t, TB_A11Y_EV_VALUE) && has_event(t, TB_A11Y_EV_NAME), "eventos de valor e de nome"); + CHECK(has_event(t, TB_A11Y_EV_SHOWN), "evento: diálogo apareceu"); + CHECK(bridge_events == tb_a11y_event_count(t), "a ponte recebe todos os eventos"); + + /* quadro 3: igual ao 2 -> nada muda */ + build(t, 1, true, "repita (25) vezes", true); + CHECK(tb_a11y_event_count(t) == 0, "quadro igual não gera eventos"); + + tb_a11y_announce(t, "ignorado fora de begin/end"); /* o begin seguinte limpa */ + tb_a11y_begin(t, "App", 800, 600); + tb_a11y_announce(t, "Projeto salvo"); + tb_a11y_end(t); + CHECK(tb_a11y_event_count(t) == 1 && tb_a11y_event(t, 0)->kind == TB_A11Y_EV_ANNOUNCE && + !strcmp(tb_a11y_event(t, 0)->text, "Projeto salvo"), "aviso para ler em voz alta"); + + /* robustez: arvore NULL e indices invalidos */ + CHECK(tb_a11y_push(NULL, TB_A11Y_BUTTON, "x", 0, 0, 1, 1) == -1 && tb_a11y_node(t, 999) == NULL && + tb_a11y_find(t, 999, TB_A11Y_NONE, NULL, 0) == -1, "NULL e índices inválidos não quebram"); + + tb_a11y_tree_free(t); + printf(fails ? "\n%d FALHA(S)\n" : "\nTUDO OK\n", fails); + return fails != 0; +} diff --git a/tests/editor_harness.c b/tests/editor_harness.c index 3e5baba..56874c3 100644 --- a/tests/editor_harness.c +++ b/tests/editor_harness.c @@ -23,6 +23,7 @@ #include #include #include +#include static TinyBlocks* rt; static TinyBlocks_Block *ev, *scene; @@ -80,8 +81,36 @@ static bool in_region(float x, int r) { } } +/* == Interface de acessibilidade: componentes por papel + nome === + * (nao depende do texto desenhado nem de coordenadas fixas) */ +static TinyBlocks_A11yTree* A(void) { return tb_editor_accessible(scene); } +static const TinyBlocks_A11yNode* N(int n) { return tb_a11y_node(A(), n); } +static int ax_in(int from, TinyBlocks_A11yRole r, const char* name) { return tb_a11y_find(A(), from, r, name, 0); } +static int ax_part(int from, TinyBlocks_A11yRole r, const char* part) { return tb_a11y_find_part(A(), from, r, part, 0); } +static int ax_need(int from, TinyBlocks_A11yRole r, const char* name) { + int n = ax_in(from, r, name); + if (n < 0) { printf("FAIL não achei o %s \"%s\" na árvore acessível\n", tb_a11y_role_name(r), name); fails++; } + return n; +} +static const char* ax_value(int n) { return n >= 0 ? N(n)->value : ""; } +static int ax_count(int from, TinyBlocks_A11yRole r, const char* name) { + int k = 0; + while (tb_a11y_find(A(), from, r, name, k) >= 0) k++; + return k; +} + +/* Categoria (aba da coluna "Categorias") */ +static bool find_cat(const char* s, int nth, float* x, float* y) { + int list = ax_in(0, TB_A11Y_TAB_LIST, "Categorias"); + int n = list >= 0 ? tb_a11y_find(A(), list, TB_A11Y_TAB, s, nth) : -1; + if (n < 0) return false; + tb_a11y_point(A(), n, x, y); + return true; +} + /* Centro (aprox.) do n-ésimo texto igual a s na região */ static bool find(const char* s, int region, int nth, float* x, float* y) { + if (region == R_CAT) return find_cat(s, nth, x, y); for (uint32_t i = 0; i < ncmd; i++) { TinyBlocks_DrawCmd* c = &cmds[i]; if (c->kind != TB_DRAW_TEXT || strcmp(c->text, s) != 0 || !in_region(c->x, region)) continue; @@ -210,6 +239,23 @@ static void type(const char* s) { frame(0, 0, 0, 0, 0, 0, s); } static void key(int sc, float x, float y) { frame(x, y, 0, 0, 0, sc, NULL); } static void flag(void) { click(920 + 30, 44 + 22); } +/* Executa uma acao acessivel e roda um quadro (a arvore e redesenhada) */ +static bool ax_do(int n, TinyBlocks_A11yAction a) { + if (n < 0) return false; + float x, y; + tb_a11y_point(A(), n, &x, &y); + bool ok = tb_a11y_do_action(A(), n, a); + frame(x, y, 0, 0, 0, 0, NULL); + return ok; +} +/* Arrasta o componente pelo seu ponto, levando o canto dele para (tx, ty) */ +static void ax_drag_to(int n, float tx, float ty) { + if (n < 0) return; + float px, py; + tb_a11y_point(A(), n, &px, &py); + drag(px, py, tx + (px - N(n)->x), ty + (py - N(n)->y)); +} + static bool need(const char* s, int region, int nth, float* x, float* y) { if (find(s, region, nth, x, y)) return true; printf("FAIL não achei \"%s\"\n", s); fails++; @@ -314,74 +360,82 @@ int main(void) { int after = 0; while (find("escreva", R_WS, after, &x2, &y2)) after++; CHECK(after == before - 1, "soltar bloco sobre a paleta apaga"); - /* 8. Bloco composto "dobro(n)" criado pelo diálogo; corpo: devolva n + 1 */ + /* 8. Bloco composto "dobro(n)" criado pelo diálogo; corpo: devolva n + 1. + * Tudo achado pela interface de acessibilidade (papel + nome). */ if (need("Meus Blocos", R_CAT, 0, &x, &y)) click(x, y); - if (need("Criar um Bloco", R_FLY, 0, &x, &y)) click(x, y); - if (need("nome do bloco", R_WS, 0, &x, &y)) click(x, y); + ax_do(ax_need(ax_in(0, TB_A11Y_LIST, "Paleta"), TB_A11Y_BUTTON, "Criar um Bloco"), TB_A11Y_ACT_ACTIVATE); + { + int dlg = ax_need(0, TB_A11Y_DIALOG, "Criar um Bloco"); + int nome = ax_in(dlg, TB_A11Y_TEXT_FIELD, "Nome do bloco"); + CHECK(dlg >= 0 && (N(dlg)->states & TB_A11Y_MODAL), "diálogo \"Criar um Bloco\" na árvore, modal"); + CHECK(nome >= 0 && tb_a11y_focus(A()) == nome, "o foco abre no campo \"Nome do bloco\""); + } type("dobro"); - if (need("número ou texto", R_WS, 0, &x, &y)) click(x, y); + CHECK(!strcmp(ax_value(ax_in(0, TB_A11Y_TEXT_FIELD, "Nome do bloco")), "dobro"), "o nome digitado é o valor do campo"); + ax_do(ax_need(ax_in(0, TB_A11Y_DIALOG, "Criar um Bloco"), TB_A11Y_BUTTON, "Adicionar entrada de número ou texto"), + TB_A11Y_ACT_ACTIVATE); type("n"); - key(TB_KEY_RETURN, 0, 0); /* OK no diálogo */ - /* como no Scratch: o "defina" aparece na própria aba, com o parâmetro ao lado */ - CHECK(ws_has("defina") && ws_count("n") == 1, "Criar um Bloco põe \"defina dobro\" na mesma aba (sem parâmetros soltos)"); - CHECK(ws_has("quando a bandeira for clicado"), "sem abrir aba nova: os scripts continuam ali"); - hx = 0; hy = 0; - need("defina", R_WS, 0, &hx, &hy); - { float nx, ny; CHECK(find_near("dobro", hx, hy, &nx, &ny) && find_near("n", nx, ny, &nx, &ny), - "o protótipo \"dobro (n)\" aparece dentro do defina"); } + CHECK(!strcmp(ax_value(tb_a11y_focus(A())), "n"), "a entrada nova recebe o foco e o nome n"); + ax_do(ax_need(ax_in(0, TB_A11Y_DIALOG, "Criar um Bloco"), TB_A11Y_BUTTON, "OK"), TB_A11Y_ACT_ACTIVATE); + CHECK(ax_in(0, TB_A11Y_DIALOG, "Criar um Bloco") < 0, "OK fecha o diálogo"); + int panel = ax_in(0, TB_A11Y_TAB_PANEL, NULL); + int def = ax_in(panel, TB_A11Y_BLOCK, "defina dobro (n)"); + /* como no Scratch: o "defina" aparece na própria aba, e o parâmetro só dentro dele */ + CHECK(def >= 0, "Criar um Bloco põe \"defina dobro (n)\" na mesma aba"); + CHECK(ax_count(panel, TB_A11Y_BLOCK, "n") == 1 && ax_in(def, TB_A11Y_BLOCK, "n") >= 0, + "sem parâmetros soltos: o \"n\" está no protótipo do defina"); + CHECK(ax_in(panel, TB_A11Y_BLOCK, "quando a bandeira for clicado") >= 0, "sem abrir aba nova: os scripts continuam ali"); + float dfx = def >= 0 ? N(def)->x : 0, dfy = def >= 0 ? N(def)->y : 0, dfh = def >= 0 ? N(def)->h : 0; + + /* devolva embaixo do "defina"; um "+" solto ao lado */ if (need("Meus Blocos", R_CAT, 0, &x, &y)) click(x, y); - if (need("devolva", R_FLY, 0, &x, &y)) drag(x, y, hx, hy + 44); /* embaixo do "defina" */ + ax_drag_to(ax_part(ax_in(0, TB_A11Y_LIST, "Paleta"), TB_A11Y_BLOCK, "devolva"), dfx, dfy + dfh); if (need("Operadores", R_CAT, 0, &x, &y)) click(x, y); - if (need("+", R_FLY, 0, &x, &y)) drag(x, y, hx + 300, hy + 110); - /* encaixa o parâmetro n no slot esquerdo do "+" */ { - float px, py, zx, zy; - /* o parâmetro sai arrastando de dentro do "defina" */ - float dx0, dy0; - if (find_near("dobro", hx, hy, &dx0, &dy0) && find_near("n", dx0, dy0, &px, &py) && - block_plus(&x, &y, hy + 20)) { - int k = 0; bool got = false; - while (find("0", R_WS, k++, &zx, &zy)) - if (zy > y - 4 && zy < y + 4 && zx < x) { got = true; break; } - if (!got) { printf("FAIL slot esquerdo do + (+ em %.0f,%.0f)\n", x, y); fails++; } - else drag(px, py, zx - 3, zy); - } - CHECK(ws_count("n") == 2, "arrastar o parâmetro do defina cria o reporter \"n\""); + int ops = ax_in(ax_in(0, TB_A11Y_LIST, "Paleta"), TB_A11Y_GROUP, "Operadores"); + ax_drag_to(ax_part(ops, TB_A11Y_BLOCK, ") + ("), dfx + 300, dfy + 110); } - /* digita 1 no slot direito do "+" */ - if (block_plus(&x, &y, hy + 20)) { - float zx, zy; int k = 0; - while (find("0", R_WS, k++, &zx, &zy)) - if (zy > y - 4 && zy < y + 4 && zx > x) { - click(zx, zy); type("1"); key(TB_KEY_RETURN, 0, 0); break; - } + /* o parâmetro sai arrastando de dentro do "defina" para o 1o campo do "+" */ + { + panel = ax_in(0, TB_A11Y_TAB_PANEL, NULL); + int plus = ax_in(ax_in(panel, TB_A11Y_LIST, "Pilha: (0) + (0)"), TB_A11Y_BLOCK, "(0) + (0)"); + int slot = ax_in(plus, TB_A11Y_TEXT_FIELD, NULL); + if (slot >= 0) ax_drag_to(ax_in(ax_in(panel, TB_A11Y_BLOCK, "defina dobro (n)"), TB_A11Y_BLOCK, "n"), + N(slot)->x, N(slot)->y); + panel = ax_in(0, TB_A11Y_TAB_PANEL, NULL); + CHECK(ax_in(panel, TB_A11Y_BLOCK, "(n) + (0)") >= 0, "arrastar o parâmetro do defina põe \"n\" no +"); } - if (block_plus(&x, &y, hy + 20) && need("=", R_WS, 0, &x2, &y2)) { - float rep_left = x - 3 - 10; - /* o reporter "n" (o outro "n" esta no defina) */ - { float px, py; for (int k = 0; find("n", R_WS, k, &px, &py); k++) - if (py > hy + 10) { rep_left = px - 3 - 10; break; } } - drag(x, y, x + ((x2 + 12) - rep_left), y + (y2 - y)); + /* 1 no outro campo do "+" */ + ax_do(ax_in(ax_in(0, TB_A11Y_BLOCK, "(n) + (0)"), TB_A11Y_TEXT_FIELD, NULL), TB_A11Y_ACT_EDIT); + key(TB_KEY_BACKSPACE, 0, 0); + type("1"); key(TB_KEY_RETURN, 0, 0); + /* o "+" no campo de valor do devolva (o 2o campo) */ + { + int dev = ax_part(ax_in(0, TB_A11Y_TAB_PANEL, NULL), TB_A11Y_BLOCK, "devolva"); + int val = dev >= 0 ? tb_a11y_find(A(), dev, TB_A11Y_TEXT_FIELD, NULL, 1) : -1; + if (val >= 0) ax_drag_to(ax_need(0, TB_A11Y_BLOCK, "(n) + (1)"), N(val)->x, N(val)->y); + CHECK(ax_part(ax_in(0, TB_A11Y_TAB_PANEL, NULL), TB_A11Y_BLOCK, "= ((n) + (1))") >= 0, + "corpo: devolva (n) + (1)"); } if (need("Meus Blocos", R_CAT, 0, &x, &y)) click(x, y); - CHECK(find("dobro", R_FLY, 0, &x, &y), "bloco dobro aparece em Meus Blocos"); - if (need("dobro", R_FLY, 0, &x, &y)) drag(x, y, hx + 330, hy - 30); { - float sx, sy; - float dx2 = 0, dy2 = 0; - for (int k = 0; find("dobro", R_WS, k, &x, &y); k++) if (y < hy) { dx2 = x; dy2 = y; } - if (dx2 > 0 && find_near("n", dx2, dy2, &sx, &sy)) { - click(sx, sy); - key(TB_KEY_BACKSPACE, 0, 0); - type("21"); key(TB_KEY_RETURN, 0, 0); - } + int mine = ax_in(ax_in(0, TB_A11Y_LIST, "Paleta"), TB_A11Y_GROUP, "Meus Blocos"); + int call = ax_in(mine, TB_A11Y_BLOCK, "dobro (n)"); + CHECK(call >= 0, "bloco dobro aparece em Meus Blocos"); + ax_drag_to(call, dfx + 330, dfy - 60); } - { int k = 0; float bx = 0, by = 0; - while (find("dobro", R_WS, k, &x, &y)) { if (y < hy) { bx = x; by = y; } k++; } /* a chamada, não o "defina" */ - if (bx > 0) { click(bx, by); click(bx, by); } } - CHECK(ws_has("22"), "bloco composto dobro(21) = 22"); - if (!ws_has("22")) { for (uint32_t i = 0; i < ncmd; i++) if (cmds[i].kind==TB_DRAW_TEXT && in_region(cmds[i].x,R_WS)) printf(" ws: %s (%.0f,%.0f)\n", cmds[i].text, cmds[i].x, cmds[i].y); } + { + panel = ax_in(0, TB_A11Y_TAB_PANEL, NULL); + int call = ax_in(panel, TB_A11Y_BLOCK, "dobro (n)"); + ax_do(ax_in(call, TB_A11Y_TEXT_FIELD, NULL), TB_A11Y_ACT_EDIT); + key(TB_KEY_BACKSPACE, 0, 0); + type("21"); key(TB_KEY_RETURN, 0, 0); + call = ax_in(ax_in(0, TB_A11Y_TAB_PANEL, NULL), TB_A11Y_BLOCK, "dobro (21)"); + ax_do(call, TB_A11Y_ACT_ACTIVATE); + ax_do(call, TB_A11Y_ACT_ACTIVATE); /* clique duplo: balão com o valor */ + } + CHECK(!strcmp(ax_value(ax_in(0, TB_A11Y_STATUS, "Resultado")), "22"), "bloco composto dobro(21) = 22"); /* 9. Laço infinito é interrompido */ if (need("Controle", R_CAT, 0, &x, &y)) click(x, y); @@ -1123,16 +1177,112 @@ int main(void) { const char* nomes[2] = { "render", "draw button" }; for (int k = 0; k < 2; k++) { if (need("Meus Blocos", R_CAT, 0, &x, &y)) click(x, y); - if (need("Criar um Bloco", R_FLY, 0, &x, &y)) click(x, y); - if (need("nome do bloco", R_WS, 0, &x, &y)) click(x, y); - type(nomes[k]); + ax_do(ax_need(ax_in(0, TB_A11Y_LIST, "Paleta"), TB_A11Y_BUTTON, "Criar um Bloco"), TB_A11Y_ACT_ACTIVATE); + type(nomes[k]); /* o foco abre no nome do bloco */ key(TB_KEY_RETURN, 0, 0); } if (need("Meus Blocos", R_CAT, 0, &x, &y)) click(x, y); - CHECK(find("render", R_FLY, 0, &x, &y) && find("draw button", R_FLY, 0, &x, &y), + int mine = ax_in(ax_in(0, TB_A11Y_LIST, "Paleta"), TB_A11Y_GROUP, "Meus Blocos"); + CHECK(ax_in(mine, TB_A11Y_BLOCK, "render") >= 0 && ax_in(mine, TB_A11Y_BLOCK, "draw button") >= 0, "dois blocos definidos aparecem na paleta"); } + /* 28. Interface de acessibilidade: estrutura, estados, eventos, acoes */ + { + frame(0, 0, 0, 0, 0, 0, NULL); + int menu = ax_in(0, TB_A11Y_TOOLBAR, "Menu"); + CHECK(menu >= 0 && ax_in(menu, TB_A11Y_BUTTON, "Abrir") >= 0 && ax_in(menu, TB_A11Y_BUTTON, "Salvar") >= 0 && + ax_in(menu, TB_A11Y_BUTTON, "Config") >= 0 && ax_in(menu, TB_A11Y_BUTTON, "Carregar library...") >= 0, + "a11y: barra de menu com os botões"); + CHECK(ax_in(0, TB_A11Y_PANE, "Blocos") >= 0 && ax_in(0, TB_A11Y_PANE, "Scripts") >= 0 && + ax_in(0, TB_A11Y_PANE, "Palco") >= 0 && ax_in(0, TB_A11Y_PANE, "Saída") >= 0, + "a11y: as quatro áreas são painéis"); + + int run = ax_in(ax_in(0, TB_A11Y_PANE, "Palco"), TB_A11Y_BUTTON, "Executar"); + uint32_t run_id = run >= 0 ? N(run)->id : 0; + char d[256]; + CHECK(run >= 0 && !strcmp(tb_a11y_describe(A(), run, d, sizeof(d)), "Executar, botão"), + "a11y: o narrador diz \"Executar, botão\""); + + /* categoria: ativar seleciona (e gera evento de estado) */ + int cats = ax_in(0, TB_A11Y_TAB_LIST, "Categorias"); + ax_do(ax_in(cats, TB_A11Y_TAB, "Controle"), TB_A11Y_ACT_ACTIVATE); + int ctl = ax_in(ax_in(0, TB_A11Y_TAB_LIST, "Categorias"), TB_A11Y_TAB, "Controle"); + bool ev_state = false; + for (int i = 0; i < tb_a11y_event_count(A()); i++) { + const TinyBlocks_A11yEvent* e = tb_a11y_event(A(), i); + if (e->kind == TB_A11Y_EV_STATE && e->node == ctl) ev_state = true; + } + CHECK(ctl >= 0 && (N(ctl)->states & TB_A11Y_SELECTED) && ev_state, + "a11y: ativar a categoria a seleciona (evento de estado)"); + int sel = 0; + for (int k = 0; tb_a11y_find(A(), cats, TB_A11Y_TAB, NULL, k) >= 0; k++) + if (N(tb_a11y_find(A(), cats, TB_A11Y_TAB, NULL, k))->states & TB_A11Y_SELECTED) sel++; + CHECK(sel == 1, "a11y: uma categoria selecionada por vez"); + run = ax_in(0, TB_A11Y_BUTTON, "Executar"); + CHECK(run >= 0 && N(run)->id == run_id, "a11y: ids estáveis entre quadros"); + + /* blocos da paleta fora da vista ficam OFFSCREEN, mas estão na árvore */ + int pal = ax_in(0, TB_A11Y_LIST, "Paleta"); + int far = ax_in(pal, TB_A11Y_GROUP, "Meus Blocos"); + CHECK(far >= 0 && (N(far)->states & TB_A11Y_OFFSCREEN) && ax_in(pal, TB_A11Y_GROUP, "Controle") >= 0 && + !(N(ax_in(pal, TB_A11Y_GROUP, "Controle"))->states & TB_A11Y_OFFSCREEN), + "a11y: a paleta toda está na árvore; o que não aparece é OFFSCREEN"); + + /* dialogo: abrir pelo botão, evento SHOWN, voltar fecha */ + ax_do(ax_in(0, TB_A11Y_BUTTON, "Config"), TB_A11Y_ACT_ACTIVATE); + int dlg = ax_in(0, TB_A11Y_DIALOG, "Configurações"); + bool shown = false; + for (int i = 0; i < tb_a11y_event_count(A()); i++) + if (tb_a11y_event(A(), i)->kind == TB_A11Y_EV_SHOWN && tb_a11y_event(A(), i)->node == dlg) shown = true; + CHECK(dlg >= 0 && (N(dlg)->states & TB_A11Y_MODAL) && shown, "a11y: Config abre um diálogo modal (evento)"); + ax_do(ax_in(dlg, TB_A11Y_BUTTON, "Voltar"), TB_A11Y_ACT_ACTIVATE); + CHECK(ax_in(0, TB_A11Y_DIALOG, "Configurações") < 0, "a11y: Voltar fecha o diálogo"); + + /* menu "+": o botão fica EXPANDED e o menu tem os itens */ + ax_do(ax_in(ax_in(0, TB_A11Y_TAB_LIST, "Abas"), TB_A11Y_BUTTON, "Nova aba"), TB_A11Y_ACT_ACTIVATE); + int pm = ax_in(0, TB_A11Y_MENU, "Nova aba"); + CHECK(pm >= 0 && ax_in(pm, TB_A11Y_MENU_ITEM, "Editor de Código") >= 0 && + (N(ax_in(0, TB_A11Y_BUTTON, "Nova aba"))->states & TB_A11Y_EXPANDED), + "a11y: o menu \"Nova aba\" abre com os itens"); + ax_do(ax_in(pm, TB_A11Y_MENU_ITEM, "Editor de Código"), TB_A11Y_ACT_ACTIVATE); + int code = ax_in(0, TB_A11Y_TEXT_FIELD, "Código"); + CHECK(code >= 0 && tb_a11y_focus(A()) == code, "a11y: aba de código é um campo de texto com o foco"); + type("int x;"); + CHECK(!strcmp(ax_value(ax_in(0, TB_A11Y_TEXT_FIELD, "Código")), "int x;"), "a11y: valor = linha do cursor"); + { /* fecha a aba de codigo (a selecionada) */ + int tabs = ax_in(0, TB_A11Y_TAB_LIST, "Abas"), cur = -1; + for (int k = 0, t; (t = tb_a11y_find(A(), tabs, TB_A11Y_TAB, NULL, k)) >= 0; k++) + if (N(t)->states & TB_A11Y_SELECTED) cur = t; + ax_do(ax_in(cur, TB_A11Y_BUTTON, "Fechar aba"), TB_A11Y_ACT_ACTIVATE); + CHECK(ax_in(0, TB_A11Y_TEXT_FIELD, "Código") < 0, "a11y: \"Fechar aba\" fecha a aba"); + } + + /* menu de contexto num bloco */ + int hat = ax_in(ax_in(0, TB_A11Y_TAB_PANEL, NULL), TB_A11Y_BLOCK, "quando a bandeira for clicado"); + CHECK(hat >= 0 && (N(hat)->states & TB_A11Y_OFFSCREEN), "a11y: bloco fora da vista é OFFSCREEN"); + ax_do(hat, TB_A11Y_ACT_MENU); /* rola até ele e abre o menu */ + int cm = ax_in(0, TB_A11Y_MENU, "Menu de contexto"); + CHECK(cm >= 0 && ax_in(cm, TB_A11Y_MENU_ITEM, "Duplicar") >= 0, "a11y: menu de contexto do bloco"); + key(TB_KEY_ESCAPE, 0, 0); + + /* ponte de texto (um narrador simples) */ + FILE* log = tmpfile(); + if (log) { + TinyBlocks_A11yBridge b = tb_a11y_log_bridge(log); + tb_a11y_set_bridge(A(), &b); + ax_do(ax_in(0, TB_A11Y_BUTTON, "Config"), TB_A11Y_ACT_ACTIVATE); + ax_do(ax_in(ax_in(0, TB_A11Y_DIALOG, "Configurações"), TB_A11Y_BUTTON, "Voltar"), TB_A11Y_ACT_ACTIVATE); + tb_a11y_set_bridge(A(), NULL); + char lb[2048] = ""; + rewind(log); + size_t n = fread(lb, 1, sizeof(lb) - 1, log); + lb[n] = 0; + fclose(log); + CHECK(strstr(lb, "[a11y] abriu: Configurações, diálogo") != NULL, "a11y: a ponte recebe os eventos"); + } + } + tinyblocks_deinit(&rt); printf(fails ? "\n%d FALHA(S)\n" : "\nTUDO OK\n", fails); return fails != 0;