Files
MeowOS-SDK/examples/hello/app.c
T
MeowOS 4f66da66ad Add 'examples/hello/' from commit 'e88e5a6ff3b0f55671cb64c0a506e552360010e9'
git-subtree-dir: examples/hello
git-subtree-mainline: f44e3bc6d0
git-subtree-split: e88e5a6ff3
2026-09-01 21:08:44 +08:00

141 lines
5.5 KiB
C
Raw 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.
/* app.c — hello 应用装配层:main + 组装 + 事件分发。
*
* 职责(SDK_VIEW_FRAMEWORK_DESIGN.md §B.2):main 只做初始化/建页/跑事件循环/清理;把
* meow_ui_run 的平铺输入回调(key/tap/swipe/tick/touch)路由到当前活动页(home/full/demo)。
* 页面切换统一走 meow_view_page_push/pop(活动栈 + on_enter/on_leave 的唯一驱动,App 不再
* 分别调 activity_push/pop)。业务变化经 logic 监听器送回 UI(见 ui/home/home.c)。
*
* 契约(MEOW_APP_SDK.md §1/§3/§6):应用只调 meow_ui API。
*/
#include "hello_internal.h"
#include "meow_ui_nav.h" /* ①:App 侧 M3 导航上报封装(上报页面深度/顶层) */
#include "meow_ui_media.h" /* ②:媒体会话封装(demo 页按钮上报 META + 收 PLAYCTL) */
#include <signal.h>
/* Dialog / BottomSheet(阻塞模态;非控件,main() 创建一次,demo 页打开/关) */
meow_dialog *g_dlg;
meow_sheet *g_sheet;
static meow_view *g_home_view;
static meow_view *g_full_view;
static meow_view *g_demo_view;
static void on_sigterm(int s) { (void)s; meow_ui_request_exit(); }
/* ② 媒体会话播放控制回调(launcher 下发 MSG_PLAYCTL 到最活跃媒体会话)。 */
static void demo_media_playctl(int cmd, int64_t seek_ms)
{
meow_log(MEOW_LOG_INFO, "media playctl cmd=%d seek=%lld", cmd, (long long)seek_ms);
}
/* ① 导航上报:把当前 Activity 栈深/顶层报给 launcher(M3 导航栈)。每个页面 on_enter 时调用
* (view_page_push/pop 会同步触发新页 on_enter),保证 pop/push 后 launcher 的 nav_stack 跟随。
* depth 取 meow_ui_activity_depth()(实际 push 页数,≥1);top 取 activity id 映射的字符串。 */
void hello_nav_report(void)
{
int depth = meow_ui_activity_depth();
if (depth <= 0) depth = 1;
int id = meow_ui_activity_active();
const char *top = (id == ACT_FULL) ? "full" : (id == ACT_DEMO) ? "demo" : "home";
meow_ui_nav_report((uint32_t)depth, top);
}
/* ============================================================ */
/* 事件分发:meow_ui_run 平铺回调 → 当前页 */
/* ============================================================ */
static void app_on_key(int keycode, int is_down)
{
(void)keycode; (void)is_down;
meow_log(MEOW_LOG_DEBUG, "key");
}
static void app_on_tap(int x, int y)
{
if (demo_is_active()) { demo_on_tap(x, y); return; }
if (full_is_active()) { meow_view_page_pop(); return; } /* 全屏:点任意处回 home */
home_on_tap(x, y);
}
static void app_on_swipe(int dir, int dx, int dy)
{
(void)dy;
if (demo_is_active()) { demo_swipe(dir, dx); return; }
if (dir == MEOW_SWIPE_LEFT) {
if (full_is_active()) { meow_view_page_pop(); }
else { meow_log(MEOW_LOG_INFO, "exit:swipe"); meow_ui_request_exit(); }
}
}
/* M2.71-bug2/6:拖动过程回调(只对 demo 页有意义;home/全屏不拖拽)。 */
static void app_on_touch_down(int x, int y)
{
if (demo_is_active()) demo_touch_down(x, y);
}
static void app_on_touch_move(int x, int y)
{
if (demo_is_active()) demo_touch_move(x, y);
}
static void app_on_swipe_progress(int x_start, int dx)
{
meow_ui_swipe_hint(x_start + dx);
}
static void app_on_tick(void)
{
uint64_t now = meow_ui_time_ms();
if (demo_is_active()) { demo_tick(now); return; }
static uint64_t last = 0; /* home/全屏计时:1s 一跳(demo 不计数,同旧) */
if (now - last >= 1000) { last = now; logic_secs_add(1); }
}
/* ============================================================ */
/* main */
/* ============================================================ */
int main(void)
{
meow_log(MEOW_LOG_INFO, "start");
meow_ui_opts o = { 480, 720 };
if (meow_ui_init(&o) != 0) { meow_log(MEOW_LOG_ERROR, "init failed"); return 1; }
meow_theme_add_listener(NULL);
meow_ui_nav_init(); /* ① 读 MEOW_ACTIVITY/MEOW_CHAIN 初始化导航栈 */
meow_ui_media_init(NULL); /* ② app_id=env MEOW_LOG_APP */
/* ② 注册播放控制回调(launcher 经媒体会话下发 MSG_PLAYCTL):hello 只打日志验证。 */
meow_ui_media_on_playctl(demo_media_playctl);
signal(SIGTERM, on_sigterm);
/* Dialog / BottomSheet 实例(阻塞模态;打开前排空残留触摸由 SDK 处理) */
g_dlg = meow_dialog_create(MEOW_DLG_SELECT, "Pick a fruit");
const char *items[] = { "Apple", "Banana", "Cherry" };
meow_dialog_set_items(g_dlg, items, 3, 1);
g_sheet = meow_sheet_create("Options", 0);
meow_sheet_add_item(g_sheet, "Copy");
meow_sheet_add_item(g_sheet, "Delete");
meow_sheet_add_item(g_sheet, "Share");
/* 建三页 view(on_create 建各自控件/状态)+ 弹层实例 */
g_home_view = home_view_create();
g_full_view = full_view_create();
g_demo_view = demo_view_create();
/* 进入根页(activity_push + home on_enter → home_redraw) */
meow_view_page_push(ACT_HOME);
meow_ui_events ev = { app_on_key, app_on_tap, app_on_swipe, app_on_tick, app_on_swipe_progress,
app_on_touch_down, app_on_touch_move };
meow_ui_run(&ev);
/* 清理:meow_view_free 触发各页 on_destroy(home 控件 / demo 内容+chrome) */
meow_view_free(g_full_view);
meow_view_free(g_demo_view);
meow_view_free(g_home_view);
meow_dialog_free(g_dlg);
meow_sheet_free(g_sheet);
meow_ui_shutdown();
meow_log(MEOW_LOG_INFO, "exit");
return 0;
}