View 与导航
一个 ltv_view_t 表示一个页面实例。句柄由调用者持有,并且必须在 Runtime 关闭前通过 ltv_view_destroy() 销毁。
创建与 Client
ltv_view_t view = ltv_view_create();
需要在创建时注册回调:
ltv_view_client_t client = {};
client.struct_size = sizeof(client);
client.version = LTV_STRUCT_VERSION;
client.user_data = &app;
client.on_title_changed = &OnTitleChanged;
ltv_view_t view = ltv_view_create_with_client(&client);
ltv_view_set_client(view, nullptr) 可以清除当前 client。Runtime 会复制 struct_size 指定的结构体前缀;user_data 指针指向的对象必须覆盖回调使用周期。
加载内容
ltv_view_load_html(view, html, "https://app.local/");
ltv_view_navigate(view, "https://example.com/");
ltv_view_load_html() 的 base_url 用于解析相对 URL。ltv_view_navigate() 只启动导航,不等待文档加载完成;通过 client callback 观察 URL 和加载状态。
导航控制
ltv_view_go_back(view);
ltv_view_go_forward(view);
ltv_view_reload(view);
ltv_view_stop(view);
读取当前状态前必须初始化版本字段:
ltv_navigation_state_t state = {};
state.struct_size = sizeof(state);
state.version = LTV_STRUCT_VERSION;
if (ltv_view_get_navigation_state(view, &state) == LTV_OK) {
UpdateToolbar(state.can_go_back, state.can_go_forward, state.is_loading);
}
页面状态
char* title = nullptr;
char* url = nullptr;
char* html = nullptr;
ltv_view_get_title(view, &title);
ltv_view_get_url(view, &url);
ltv_view_get_html(view, &html);
ltv_free(title);
ltv_free(url);
ltv_free(html);
ltv_view_get_html() 返回当前 DOM 的序列化结果,包含页面加载后通过脚本产生的变更。
远程调试开启后,宿主可以为当前 View 获取 DevTools WebSocket 目标:
char* target_url = nullptr;
if (ltv_view_get_devtools_target_url(view, &target_url) == LTV_OK) {
OpenDevToolsFrontend(target_url);
}
ltv_free(target_url);
未通过 ltv_settings_t.remote_debugging_enabled 显式开启调试时,该调用返回
LTV_ERR_INVALID_STATE。
尺寸、焦点与编辑
ltv_view_resize()设置逻辑页面尺寸。- Windows 原生宿主应优先使用
ltv_win_view_resize()同时同步 DIP 和物理像素。 ltv_view_set_focus()同步宿主焦点状态。ltv_view_execute_edit_command()支持撤销、重做、剪切、复制、粘贴、删除和全选。
销毁
ltv_view_destroy(view);
view = nullptr;
on_closed 只表示页面关闭了 WebContents;View 句柄仍归调用者所有,依然需要显式销毁。