Files
Liu 093a6a1e35 feat: add explicit window lifecycle and per-window event routing
- add the window API and lifecycle design specs
- lcui_init no longer creates a window; settings loading is explicit
- add lcui_window_* create/bind/geometry/state/show/close api
- replace display modes with window options and quit-on-last-window
- add per-window event scopes and window-based pointer/keyboard routing
- keep pending updates for widgets appended during a frame
- migrate examples, tests, previews and internals docs
- remove default-scope focus and mouseover getters
2026-11-16 00:03:50 +08:00

138 lines
5.7 KiB
C

#include <router.h>
#include <ctest.h>
#include <LCUI/base.h>
#include <LCUI/widgets.h>
#include <LCUI/widgets/router.h>
void test_router_components(void)
{
router_t *router;
router_config_t *config;
ui_widget_t *root;
ui_widget_t *link_foo;
ui_widget_t *link_foobar;
ui_widget_t *link_bar;
ui_widget_t *view;
ui_widget_t *matched_widget;
ui_event_t e;
router = router_create(NULL);
config = router_config_create();
router_config_set_path(config, "/foo");
router_config_set_component(config, NULL, "foo");
router_add_route_record(router, config, NULL);
router_config_destroy(config);
config = router_config_create();
router_config_set_path(config, "/foo/bar");
router_config_set_component(config, NULL, "foobar");
router_add_route_record(router, config, NULL);
router_config_destroy(config);
config = router_config_create();
router_config_set_path(config, "/bar");
router_config_set_component(config, NULL, "bar");
router_add_route_record(router, config, NULL);
router_config_destroy(config);
lcui_init();
ui_create_widget_prototype("foo", NULL);
ui_create_widget_prototype("foobar", NULL);
ui_create_widget_prototype("bar", NULL);
root = ui_root();
view = ui_create_widget("router-view");
link_foo = ui_create_widget("router-link");
link_foobar = ui_create_widget("router-link");
link_bar = ui_create_widget("router-link");
ui_widget_set_attr(link_foo, "to", "/foo");
ui_widget_set_attr(link_foobar, "to", "/foo/bar");
ui_widget_set_attr(link_bar, "to", "/bar");
ui_widget_append(root, link_foo);
ui_widget_append(root, link_foobar);
ui_widget_append(root, link_bar);
ui_widget_append(root, view);
ui_update();
ui_event_init(&e, "click");
ui_widget_emit_event(link_foo, e, NULL);
matched_widget = ui_router_view_get_matched_widget(view);
ctest_equal_bool("[/foo] should load <foo> in <router-view>",
strcmp(matched_widget->type, "foo") == 0, true);
ctest_equal_bool(
"[/foo] should mark linkFoo with active classes",
(ui_widget_has_class(link_foo, "router-link-active") &&
ui_widget_has_class(link_foo, "router-link-exact-active")),
true);
ui_widget_emit_event(link_bar, e, NULL);
matched_widget = ui_router_view_get_matched_widget(view);
ctest_equal_bool("[/bar] should load <bar> in <router-view>",
strcmp(matched_widget->type, "bar") == 0, true);
ctest_equal_bool(
"[/bar] should remove active classes from linkFoo",
(!ui_widget_has_class(link_foo, "router-link-exact-active") &&
!ui_widget_has_class(link_foo, "router-link-active")),
true);
ui_widget_emit_event(link_foobar, e, NULL);
matched_widget = ui_router_view_get_matched_widget(view);
ctest_equal_bool("[/foo/bar] should load <foobar> in <router-view>",
strcmp(matched_widget->type, "foobar") == 0, true);
ctest_equal_bool(
"[/foo/bar] should mark linkFooBar with active classes",
(ui_widget_has_class(link_foobar, "router-link-exact-active") &&
ui_widget_has_class(link_foobar, "router-link-active")),
true);
ctest_equal_bool(
"[/foo/bar] should mark linkFoo with non-exact active class only",
(!ui_widget_has_class(link_foo, "router-link-exact-active") &&
ui_widget_has_class(link_foo, "router-link-active")),
true);
ui_widget_set_attr(link_foo, "exact", "exact");
ui_widget_emit_event(link_foobar, e, NULL);
ctest_equal_bool(
"[/foo/bar] should remove all active classes when exact is set",
(!ui_widget_has_class(link_foo, "router-link-exact-active") &&
!ui_widget_has_class(link_foo, "router-link-active")),
true);
/* Sibling routes that share the same component must reuse the mounted
* instance instead of rebuilding the subtree. */
config = router_config_create();
router_config_set_path(config, "/same/a");
router_config_set_component(config, NULL, "same");
router_add_route_record(router, config, NULL);
router_config_destroy(config);
config = router_config_create();
router_config_set_path(config, "/same/b");
router_config_set_component(config, NULL, "same");
router_add_route_record(router, config, NULL);
router_config_destroy(config);
ui_create_widget_prototype("same", NULL);
{
router_location_t *location =
router_location_create(NULL, "/same/a");
ui_widget_t *first;
router_push(router, location);
router_location_destroy(location);
ui_update();
first = ui_router_view_get_matched_widget(view);
ctest_equal_bool("[/same/a] should render <same>",
first && ui_check_widget_type(first, "same"),
true);
location = router_location_create(NULL, "/same/b");
router_push(router, location);
router_location_destroy(location);
ui_update();
ctest_equal_bool(
"[/same/b] should keep the mounted <same> instance",
ui_router_view_get_matched_widget(view) == first, true);
}
lcui_destroy();
router_destroy(router);
}