Files
MeowOS-SDK/sdk/test_meow_ui_dispatch.c
AskaEth 4b094d62a5 sdk: 增量同步主仓新模块(box/anim/scroll/dispatch/listvirt/str/sb_icons/img)
subtree pull sdk 来自主仓 pub/sdk(主仓 commit 68ce46a 等)。新增:
- 布局引擎 meow_ui_box / 动画基座 meow_ui_anim / 滚动物理 meow_ui_scroll
- §6.4 事件分发形式化 / W_LISTVIEW 虚拟化多行 / meow_str 字符串资源 i18n
- sb_icons 动作图标扩充 + topbar 位图图标 / meow_img_downscale
- tools/ 图标生成脚本与 SVG 源
- 对应宿主 test_*.c 与 demo_box_scroll.c

build_sdk.sh 冲突解析:保留公开仓自包含版(vendored meow_font + -Iinclude -I.),
OBJS 手工并入 4 个新模块(meow_ui_box/anim/scroll/str)。保留 vendored meow_font.c/
meow_ipc.h/meow_font.h/meow_notif.h,剔除宿主测试二进制与编译产物。
2026-09-02 09:08:51 +08:00

146 lines
8.2 KiB
C
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
/* test_meow_ui_dispatch.c — §6.4 事件分发形式化宿主验证(离屏 480x720,16bpp)。
*
* 覆盖(新穿透矩阵,全部直接驱动 SDK §6.4 仲裁者 API + 真实注册控件):
* M1 模态打开点下层不触发:modal_begin 后 meow_ui_hit_topdown 恒返回模态哨兵(不再落到下层
* 同坐标控件),gesture_begin 捕获的是模态(哨兵)而非下层按钮——下层绝不消费;
* M2 弹层叠开两层不叠黑/层隔离:modal_begin 两次 → depth=2;end(LIFO)逐层递减;期间
* hit_topdown 始终返回哨兵,下层收不到任何触摸;
* M3 捕获手势滑出不误触:gesture_begin 命中按钮 A 后捕获;任凭概念滑动到远处(不重新命中),
* meow_ui_gesture_owner 恒为 A(绝不滑出误触下层/邻控件);end 释放;
* M4 list 焦点点外释放:meow_list_tap_at/meow_listview_tap_at 使列表成为唯一焦点;
* focus_tap 落在列表内 → 保位;落在列表外(页面内容/空白)→ 释放焦点并触发 lost 回调。
*
* 编译(宿主 gcc,链真实 widget/draw/input/activity/gfx/sys/theme/vol/log/dialog/ime/notify/ipc +
* 打桩 font/global;完整超集便于链接):
* gcc -Wall -Wextra -o test_meow_ui_dispatch test_meow_ui_dispatch.c test_host_stub.c \
* meow_ui_gfx.c meow_ui_sys.c meow_ui_theme.c meow_ui_vol.c meow_ui_log.c \
* meow_ui_widget.c meow_ui_input.c meow_ui_activity.c meow_ui_dialog.c meow_ui_ime.c meow_ui_str.c \
* meow_pinyin.c meow_ui_draw.c meow_ui_view.c meow_ui_box.c meow_ui_anim.c meow_ui_scroll.c \
* meow_ui_img.c meow_ui_media.c meow_notify_ipc.c stb_image.c \
* -I. -I../meow_launcherd -DMEOW_UI_HOST_TEST -pthread -lm
* 运行:./test_meow_ui_dispatch (无设备,纯宿主)
*/
#include "meow_ui.h"
#include "meow_ui_internal.h"
#include "meow_ui_view.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
static int g_fail = 0;
#define CHECK(cond, name) do { \
if (cond) { printf(" PASS %s\n", name); } \
else { printf(" FAIL %s\n", name); g_fail++; } } while (0)
/* 焦点丢失回调计数 */
static int g_lost_calls = 0;
static void *g_lost_last = NULL;
static void focus_lost(void *owner, void *ctx)
{ (void)ctx; g_lost_calls++; g_lost_last = owner; }
/* 列表数据源(最小 W_LIST) */
static int list_src(int idx, char *label, int len, void *ctx)
{
(void)ctx;
if (idx < 0 || idx >= 8) return 0;
snprintf(label, (size_t)len, "item %d", idx);
return 1;
}
int main(void)
{
setenv("MEOW_UI_DUMP", "/tmp/dispatch.ppm", 1);
meow_ui_opts o = { 480, 720 };
if (meow_ui_init(&o) != 0) { printf("init failed\n"); return 1; }
meow_ui_activity_push(5); /* 页面控件 owner=5,命中/绘制对齐 */
/* 场景控件:两个不重叠按钮 A/B + 一个 LazyColumn 列表 */
meow_widget *btnA = meow_btn_create(20, 100, 200, 60, "A");
meow_widget *btnB = meow_btn_create(240, 100, 200, 60, "B");
meow_widget *list = meow_lazycolumn_create(20, 300, 400, 150, 44, 8, list_src, NULL);
/* ================= M1:模态打开点下层不触发 =================
* 无模态:hit_topdown(按钮A中心) == 按钮A;modal_begin 后同一落点返回模态哨兵(≠按钮A),
* gesture_begin 捕获的是模态而非下层按钮——下层绝不消费(穿透根治)。 */
printf("M1 modal open -> lower not triggered\n");
void *h0 = meow_ui_hit_topdown(120, 130);
printf(" no-modal hit_topdown(120,130) = %s\n",
h0 == (void *)btnA ? "btnA" : (h0 ? "other" : "NULL"));
CHECK(h0 == (void *)btnA, "no-modal topdown hits btnA (z-top)");
meow_ui_modal *m = meow_ui_modal_begin();
void *hm = meow_ui_hit_topdown(120, 130);
printf(" modal-open hit_topdown(120,130) = %s (sentinel=%d)\n",
hm == (void *)btnA ? "btnA(LEAK!)" : "modal-sentinel", hm != NULL && hm != (void *)btnA);
CHECK(meow_ui_modal_active(), "modal active");
CHECK(hm != NULL && hm != (void *)btnA, "modal present -> hit_topdown never reaches btnA");
void *g0 = meow_ui_gesture_begin(120, 130); /* 下层同坐标:捕获的是模态而非按钮 */
CHECK(g0 == hm, "gesture_begin during modal captures modal (sentinel), not btnA");
meow_ui_gesture_end();
meow_ui_modal_end(m);
CHECK(meow_ui_hit_topdown(120, 130) == (void *)btnA, "after modal_end topdown returns btnA again");
CHECK(!meow_ui_modal_active(), "modal released");
/* ================= M2:弹层叠开两层(层隔离,depth 语义) =================
* 两层弹开:depth 1→2;期间 hit_topdown 恒返回哨兵;LIFO 关内层→1,关外层→0。
* 叠黑由 Dialog/Sheet 的「打开前整帧快照 + 关闭还原 + 高亮仅重绘卡片不叠遮罩」保证
* (meow_ui_dialog.c,M2.71-bug3),本处验证层隔离 + 栈深。 */
printf("M2 stacked modals (two layers)\n");
meow_ui_modal *m1 = meow_ui_modal_begin();
meow_ui_modal *m2 = meow_ui_modal_begin();
CHECK(meow_ui_modal_depth() == 2, "two modals -> depth 2");
CHECK(meow_ui_hit_topdown(120, 130) != (void *)btnA, "either modal -> lower btnA blocked");
meow_ui_modal_end(m2);
CHECK(meow_ui_modal_depth() == 1, "close inner -> depth 1");
CHECK(meow_ui_hit_topdown(120, 130) != (void *)btnA, "outer modal still blocks lower");
meow_ui_modal_end(m1);
CHECK(meow_ui_modal_depth() == 0, "close outer -> depth 0");
CHECK(meow_ui_hit_topdown(120, 130) == (void *)btnA, "both closed -> lower reachable");
/* ================= M3:捕获手势滑出不误触 =================
* touch-down 命中按钮 A → 捕获。此后 move(即使滑到按钮 B / 空白)不重新命中,owner 恒为 A;
* 绝不滑出误触 B 或下层。抬手(gesture_end)释放。 */
printf("M3 gesture capture: slide-out no false-trigger\n");
void *g1 = meow_ui_gesture_begin(120, 130);
CHECK(g1 == (void *)btnA, "down on A captures A");
CHECK(meow_ui_gesture_owner() == (void *)btnA, "owner == A after down");
/* 概念滑动到按钮 B 中心 (340,130):捕获不重命中 → 仍是 A(不误触 B) */
CHECK(meow_ui_gesture_owner() == (void *)btnA, "slide to B point: owner stays A (no re-hit)");
/* 滑出到空白 */
CHECK(meow_ui_gesture_owner() == (void *)btnA, "slide to blank: owner stays A");
meow_ui_gesture_end();
CHECK(meow_ui_gesture_owner() == NULL, "release -> no owner");
/* ================= M4:list 焦点点外释放 =================
* meow_list_tap_at 使列表成为唯一焦点;focus_tap 落在列表内→保位;落在列表外→释放并通知。 */
printf("M4 list focus release on tap outside\n");
meow_ui_focus_set_lost_cb(focus_lost, NULL);
CHECK(meow_ui_focus_owner() == NULL, "no focus initially");
int idx = meow_list_tap_at(list, 120, 320); /* 命中列表行0 */
CHECK(idx >= 0, "list tap hits a row");
CHECK(meow_ui_focus_owner() == (void *)list, "list tap -> list becomes focus owner");
meow_ui_focus_tap(120, 320); /* 点列表内:保位 */
CHECK(meow_ui_focus_owner() == (void *)list, "tap inside list -> focus retained");
CHECK(g_lost_calls == 0, "no lost callback while focus retained");
meow_ui_focus_tap(120, 130); /* 命中按钮A(非列表):释放焦点+通知 */
CHECK(meow_ui_focus_owner() == NULL, "tap outside list (on A) -> focus released");
CHECK(g_lost_calls == 1 && g_lost_last == (void *)list, "lost callback fired with list owner");
/* 再次聚焦后再点「空白」(无控件命中):同样释放 */
meow_list_tap_at(list, 120, 320);
meow_ui_focus_tap(400, 650); /* 列表外空白 */
CHECK(meow_ui_focus_owner() == NULL, "tap on blank -> focus released");
CHECK(g_lost_calls == 2, "lost callback fired again");
(void)btnB;
/* 清理:控件释放自动注销 z 序绘制物 + 焦点/捕获复位 */
meow_widget_free(list);
meow_widget_free(btnB);
meow_widget_free(btnA);
meow_ui_focus_release(NULL);
meow_ui_activity_pop();
meow_ui_shutdown();
printf(g_fail ? "\nRESULT: %d FAIL(S)\n" : "\nRESULT: ALL PASS\n", g_fail);
return g_fail;
}