From d6847ad5f94d83c2ba7a94ea0e7074a7b8e3d75e Mon Sep 17 00:00:00 2001 From: Liu Date: Thu, 5 Sep 2024 00:01:41 +0800 Subject: [PATCH] fixup! feat: DPI awareness --- lib/platform/src/events.c | 238 ++++++++++++++++++------------------ lib/ui-server/src/server.c | 12 +- lib/ui/src/ui_tree.c | 8 +- src/lcui_ui.c | 6 +- tests/cases/test_mainloop.c | 32 +++-- tests/run_tests.c | 2 +- 6 files changed, 156 insertions(+), 142 deletions(-) diff --git a/lib/platform/src/events.c b/lib/platform/src/events.c index ac6ec61d2..4653e5d4a 100644 --- a/lib/platform/src/events.c +++ b/lib/platform/src/events.c @@ -15,173 +15,173 @@ #include "events.h" static struct app_events_t { - /** list_t */ - list_t queue; + /** list_t */ + list_t queue; - /** list_t */ - list_t listeners; + /** list_t */ + list_t listeners; - app_event_dispatcher_t dispatcher; + app_event_dispatcher_t dispatcher; } app_events; int app_touch_event_init(app_event_t *e, touch_point_t *points, int n_points) { - e->type = APP_EVENT_TOUCH; - e->touch.n_points = n_points; - e->touch.points = malloc(sizeof(touch_point_t) * n_points); - if (!e->touch.points) { - return -ENOMEM; - } - for (n_points -= 1; n_points >= 0; --n_points) { - e->touch.points[n_points] = points[n_points]; - } - return 0; + e->type = APP_EVENT_TOUCH; + e->touch.n_points = n_points; + e->touch.points = malloc(sizeof(touch_point_t) * n_points); + if (!e->touch.points) { + return -ENOMEM; + } + for (n_points -= 1; n_points >= 0; --n_points) { + e->touch.points[n_points] = points[n_points]; + } + return 0; } int app_composition_event_init(app_event_t *e, const wchar_t *text, size_t len) { - if (len == 0) { - len = wcslen(text); - } - e->type = APP_EVENT_COMPOSITION; - e->text.length = len; - e->text.text = malloc(sizeof(wchar_t) * (len + 1)); - if (!e->text.text) { - return -ENOMEM; - } - wcscpy(e->text.text, text); - return 0; + if (len == 0) { + len = wcslen(text); + } + e->type = APP_EVENT_COMPOSITION; + e->text.length = len; + e->text.text = malloc(sizeof(wchar_t) * (len + 1)); + if (!e->text.text) { + return -ENOMEM; + } + wcscpy(e->text.text, text); + return 0; } int app_event_copy(app_event_t *dest, app_event_t *src) { - switch (src->type) { - case APP_EVENT_TOUCH: - return app_touch_event_init(dest, src->touch.points, - src->touch.n_points); - case APP_EVENT_TEXTINPUT: - return app_composition_event_init(dest, src->text.text, 0); - default: - *dest = *src; - break; - } - return 0; + switch (src->type) { + case APP_EVENT_TOUCH: + return app_touch_event_init(dest, src->touch.points, + src->touch.n_points); + case APP_EVENT_TEXTINPUT: + return app_composition_event_init(dest, src->text.text, 0); + default: + *dest = *src; + break; + } + return 0; } void app_event_destroy(app_event_t *e) { - switch (e->type) { - case APP_EVENT_TOUCH: - if (e->touch.points) { - free(e->touch.points); - } - e->touch.points = NULL; - e->touch.n_points = 0; - break; - case APP_EVENT_COMPOSITION: - if (e->text.text) { - free(e->text.text); - } - e->text.text = NULL; - e->text.length = 0; - break; - default: - break; - } - e->type = APP_EVENT_NONE; + switch (e->type) { + case APP_EVENT_TOUCH: + if (e->touch.points) { + free(e->touch.points); + } + e->touch.points = NULL; + e->touch.n_points = 0; + break; + case APP_EVENT_COMPOSITION: + if (e->text.text) { + free(e->text.text); + } + e->text.text = NULL; + e->text.length = 0; + break; + default: + break; + } + e->type = APP_EVENT_NONE; } int app_post_event(app_event_t *e) { - app_event_t *ev; - - ev = malloc(sizeof(app_event_t)); - if (!ev) { - return -ENOMEM; - } - app_event_copy(ev, e); - list_append(&app_events.queue, ev); - return 0; + app_event_t *ev; + + ev = malloc(sizeof(app_event_t)); + if (!ev) { + return -ENOMEM; + } + app_event_copy(ev, e); + list_append(&app_events.queue, ev); + return 0; } int app_add_event_listener(int event_type, app_event_handler_t handler, - void *data) + void *data) { - app_event_listener_t *listener; - - listener = malloc(sizeof(app_event_listener_t)); - if (!listener) { - return -ENOMEM; - } - listener->handler = handler; - listener->data = data; - listener->type = event_type; - list_append(&app_events.listeners, listener); - return 0; + app_event_listener_t *listener; + + listener = malloc(sizeof(app_event_listener_t)); + if (!listener) { + return -ENOMEM; + } + listener->handler = handler; + listener->data = data; + listener->type = event_type; + list_append(&app_events.listeners, listener); + return 0; } int app_remove_event_listener(int event_type, app_event_handler_t handler) { - list_node_t *node, *prev; - app_event_listener_t *listener; - - for (list_each(node, &app_events.listeners)) { - prev = node->prev; - listener = node->data; - if (listener->handler == handler && - listener->type == event_type) { - list_delete_node(&app_events.listeners, node); - free(listener); - node = prev; - return 0; - } - } - return -1; + list_node_t *node, *prev; + app_event_listener_t *listener; + + for (list_each(node, &app_events.listeners)) { + prev = node->prev; + listener = node->data; + if (listener->handler == handler && + listener->type == event_type) { + list_delete_node(&app_events.listeners, node); + free(listener); + node = prev; + return 0; + } + } + return -1; } int app_process_event(app_event_t *e) { - int count = 0; - list_node_t *node; - app_event_listener_t *listener; - - for (list_each(node, &app_events.listeners)) { - listener = node->data; - if (listener->type == e->type) { - listener->handler(e, listener->data); - ++count; - } - } - if (app_events.dispatcher) { - app_events.dispatcher(e); - } - return count; + int count = 0; + list_node_t *node; + app_event_listener_t *listener; + + for (list_each(node, &app_events.listeners)) { + listener = node->data; + if (listener->type == e->type) { + listener->handler(e, listener->data); + ++count; + } + } + if (app_events.dispatcher) { + app_events.dispatcher(e); + } + return count; } void app_process_events(void) { - app_event_t *e; - - while ((e = list_get(&app_events.queue, 0)) != NULL) { - list_delete(&app_events.queue, 0); - app_process_event(e); - app_event_destroy(e); - free(e); - } + app_event_t *e; + + while ((e = list_get(&app_events.queue, 0)) != NULL) { + list_delete(&app_events.queue, 0); + app_process_event(e); + app_event_destroy(e); + free(e); + } } void app_set_event_dispatcher(app_event_dispatcher_t dispatcher) { - app_events.dispatcher = dispatcher; + app_events.dispatcher = dispatcher; } void app_init_events(void) { - list_create(&app_events.queue); + list_create(&app_events.queue); } void app_destroy_events(void) { - app_set_event_dispatcher(NULL); - list_destroy(&app_events.queue, free); + app_set_event_dispatcher(NULL); + list_destroy(&app_events.queue, free); } diff --git a/lib/ui-server/src/server.c b/lib/ui-server/src/server.c index 2d7e57cf1..f8d857975 100644 --- a/lib/ui-server/src/server.c +++ b/lib/ui-server/src/server.c @@ -720,10 +720,14 @@ void ui_server_update(void) list_node_t *node; float dpi = 1.f * ui_metrics.dpi; - for (list_each(node, &ui_server.connections)) { - conn = node->data; - ui_metrics.dpi = 1.f * app_window_get_dpi(conn->window); - ui_updater_update(conn->updater, conn->widget); + if (ui_server.connections.length > 0) { + for (list_each(node, &ui_server.connections)) { + conn = node->data; + ui_metrics.dpi = 1.f * app_window_get_dpi(conn->window); + ui_updater_update(conn->updater, conn->widget); + } + } else { + ui_update(); } ui_metrics.dpi = dpi; } diff --git a/lib/ui/src/ui_tree.c b/lib/ui/src/ui_tree.c index 75af34c7e..666572ae3 100644 --- a/lib/ui/src/ui_tree.c +++ b/lib/ui/src/ui_tree.c @@ -9,6 +9,8 @@ * LICENSE.TXT file in the root directory of this source tree. */ +#include +#include #include #include #include @@ -367,9 +369,6 @@ ui_widget_t *ui_widget_at(ui_widget_t *widget, int ix, int iy) return target == widget ? NULL : target; } -#include -#include - static void _ui_print_tree(ui_widget_t *w, int depth, const wchar_t *prefix) { ui_widget_t *child; @@ -421,7 +420,9 @@ static void _ui_print_tree(ui_widget_t *w, int depth, const wchar_t *prefix) void ui_print_tree(ui_widget_t *w) { css_selector_node_t *node; + char *ctype = setlocale(LC_CTYPE, NULL); + setlocale(LC_CTYPE, ""); w = w ? w : ui_root(); node = ui_widget_create_selector_node(w); logger_error("%s, xy:(%g,%g), size:(%g,%g), visible: %s\n", @@ -430,4 +431,5 @@ void ui_print_tree(ui_widget_t *w) ui_widget_is_visible(w) ? "true" : "false"); css_selector_node_destroy(node); _ui_print_tree(w, 0, L" "); + setlocale(LC_CTYPE, ctype); } diff --git a/src/lcui_ui.c b/src/lcui_ui.c index b446130af..f5dee0bac 100644 --- a/src/lcui_ui.c +++ b/src/lcui_ui.c @@ -89,8 +89,10 @@ static void lcui_dispatch_ui_touch_event(app_event_t *app_event) default: break; } - e.touch.points[i].x = (float)round(app_event->touch.points[i].x / scale); - e.touch.points[i].y = (float)round(app_event->touch.points[i].y / scale); + e.touch.points[i].x = + (float)round(app_event->touch.points[i].x / scale); + e.touch.points[i].y = + (float)round(app_event->touch.points[i].y / scale); } ui_dispatch_event(&e); ui_event_destroy(&e); diff --git a/tests/cases/test_mainloop.c b/tests/cases/test_mainloop.c index 20c56457b..64a850482 100644 --- a/tests/cases/test_mainloop.c +++ b/tests/cases/test_mainloop.c @@ -13,25 +13,28 @@ #include #include -static void OnRefreshScreen(void *arg) +static void handle_refresh(void *arg) { + logger_error("OnRefreshScreen\n"); ui_refresh_style(); } -static void OnQuit(void *arg) +static void handle_quit(void *arg) { + logger_error("OnQuit\n"); lcui_quit(); } -static void OnBtnClick(ui_widget_t* w, ui_event_t* ui_event, void *arg) +static void handle_btn_click(ui_widget_t* w, ui_event_t* ui_event, void *arg) { - lcui_set_timeout(10, OnRefreshScreen, NULL); - lcui_set_timeout(50, OnQuit, NULL); + logger_error("handle_btn_click\n"); + lcui_set_timeout(0, handle_refresh, NULL); + lcui_set_timeout(1, handle_quit, NULL); lcui_process_events(APP_PROCESS_EVENTS_UNTIL_QUIT); lcui_quit(); } -static void OnTriggerBtnClick(void *arg) +static void handle_trigger_btn_click(void *arg) { app_event_t e; @@ -42,17 +45,19 @@ static void OnTriggerBtnClick(void *arg) app_post_event(&e); e.type = APP_EVENT_MOUSEUP; app_post_event(&e); + ui_print_tree(NULL); + logger_error("handle_trigger_btn_click\n"); } -static void ObserverThread(void *arg) +static void observer_thread(void *arg) { int i; bool *exited = arg; - for (i = 0; i < 20 && !*exited; ++i) { - sleep_ms(100); + for (i = 0; i < 100 && !*exited; ++i) { + sleep_ms(1); } - ctest_equal_bool("main loop should exit within 2000ms", *exited, true); + ctest_equal_bool("main loop should exit within 100ms", *exited, true); if (!*exited) { exit(-ctest_finish()); return; @@ -69,12 +74,13 @@ void test_mainloop(void) lcui_init(); btn = ui_create_widget("button"); ui_button_set_text(btn, "button"); - ui_widget_on(btn, "click", OnBtnClick, NULL); + ui_widget_on(btn, "click", handle_btn_click, NULL); ui_root_append(btn); + ui_print_tree(NULL); /* Observe whether the main loop has exited in a new thread */ - thread_create(&tid, ObserverThread, &exited); + thread_create(&tid, observer_thread, &exited); /* Trigger the click event after the first frame is updated */ - lcui_set_timeout(50, OnTriggerBtnClick, btn); + lcui_set_timeout(50, handle_trigger_btn_click, btn); lcui_main(); exited = true; thread_join(tid, NULL); diff --git a/tests/run_tests.c b/tests/run_tests.c index 55e723ebb..eb40d5c84 100644 --- a/tests/run_tests.c +++ b/tests/run_tests.c @@ -18,7 +18,7 @@ int main(int argc, char **argv) { - logger_set_level(LOGGER_LEVEL_ALL); + logger_set_level(LOGGER_LEVEL_ERROR); ctest_describe("test settings", test_settings); ctest_describe("test thread", test_thread); ctest_describe("test font load", test_font_load);