跳转到内容

网格(`gsp_grid`)

虚拟化的多列集合控件,通过可复用的单元模板展示数据。

当你需要以网格形式展示或选择一组重复条目时(例如图库、设置项网格),使用 Grid。

先按兼容性页的工具命令配置 gspc 和 gsp_sim_host,再在解压后的组件或公共仓库根目录运行:

grid-preview.sh
mkdir -p gsp-out/widget-preview
gspc pack \
examples/usage/widgets/grid/grid.json \
--deployable -o gsp-out/widget-preview/grid.gspb
gsp_sim_host \
--bundle gsp-out/widget-preview/grid.gspb --frames 0

这些命令会编译当前示例,并在 ESP-GSP 模拟器中打开浏览器预览。

虚拟化 Grid 使用 cell_template,必须用非空的应用单元格回调绑定一次,固定数据集也一样。 item_count 提供初始数量,Binder 填充每个复用单元的内容。保存 Grid Handle,并通过 set_total/refresh 更新。没有 cell_template 时,编译器只生成静态色块, 不形成绑定数据的虚拟化 Grid。

Grid 生成的单元格按场景中的声明顺序绘制。后面声明的前景对象(如 Drawer)会覆盖在单元格上方。如果你有一个需要始终显示在 Grid 上方的覆盖层,请把它放在场景中 Grid 之后的位置。

需要从应用代码读写的对象,请给它一个稳定的 name。GSPC 会为每个命名对象生成下方列出的类型化函数。

grid.json
{"screen":"widget_grid","w":480,"h":320,"screen_bg":"#101827","font":"../../../common/fonts/DejaVuSans.ttf","objects":[{"type":"layer","parent":-1,"x":0,"y":0,"w":480,"h":320},{"type":"container","parent":0,"x":0,"y":0,"w":116,"h":82,"template":"preview_cell","bg_color":"#1D2D44","border_color":"#334A68","border_width":1,"radius":10},{"type":"image","parent":1,"x":8,"y":8,"w":100,"h":66,"image":"../../../performance/benchmark/scenes/bench_industrial.png"},{"type":"grid","parent":0,"name":"gallery","x":44,"y":28,"w":392,"h":264,"column_count":3,"column_gap":14,"row_gap":12,"cell_template":"preview_cell","item_count":15,"scroll_snapshot":true}]}

规范示例来自 examples/usage/widgets/grid/grid.json。复制时请一并复制它引用的相对资源。

const gsp_component_directory_t *const * gsp_grid_docs_component_directories(uint16_t *out_count)
esp_err_t gsp_widget_grid_gallery_get_effective_visible(esp_gsp_handle_t gsp, bool *out_visible)
esp_err_t gsp_widget_grid_gallery_get_info(esp_gsp_handle_t gsp, esp_gsp_component_info_t *out_info)
esp_err_t gsp_widget_grid_gallery_refresh(esp_gsp_handle_t gsp, esp_gsp_grid_t grid)
esp_err_t gsp_widget_grid_gallery_set_total(esp_gsp_handle_t gsp, esp_gsp_grid_t grid, uint32_t total)
esp_gsp_config_t gsp_grid_docs_config(void)
esp_gsp_grid_t gsp_widget_grid_gallery_bind( esp_gsp_handle_t gsp, esp_gsp_grid_bind_cb_t bind_item, void *user_ctx)
esp_gsp_widget_t gsp_widget_grid_preview_cell_create( esp_gsp_handle_t gsp, int16_t x, int16_t y)
size_t gsp_grid_docs_dynamic_image_slots(void)

以上签名来自该 JSON 的实际编译产物。

字段 类型 必填 默认值 / 范围 可运行时更新 编译器定义
type string 是 — — 控件类型
parent int 是 默认 -1; -1…65534 — 父对象索引(-1 表示屏幕根节点)
x int 是 默认 0; -32768…32767 场景: 支持; 模板: 不支持 相对于父对象的 x 坐标
y int 是 默认 0; -32768…32767 场景: 支持; 模板: 不支持 相对于父对象的 y 坐标
w int 是 0…65535 场景: 不支持; 模板: 不支持 宽度(像素)
h int 是 0…65535 场景: 不支持; 模板: 不支持 高度(像素)
name identifier — — — 稳定组件名称;生成 GSP_OBJ_KEY_<NAME>
cell_template identifier 是 — — 复用的网格单元模板
column_count int 是 1…32 — 固定网格列数
column_gap int — 默认 0; 0…4096 — 单元格水平间距
row_gap int — 默认 0; 0…4096 — 单元格垂直间距
scroll_snapshot bool — 默认 false — 滚动时缓存两个视口帧;内存不足时回退到实时渲染
item_count int — 默认 0; 0…4294967295 — 网格条目初始数量
查看此控件支持的其他字段
字段 类型 必填 默认值 / 范围 可运行时更新 编译器定义
min_width int — 0…65535 — 编译期最小宽度,单位像素
max_width int — 0…65535 — 编译期最大宽度,单位像素
min_height int — 0…65535 — 编译期最小高度,单位像素
max_height int — 0…65535 — 编译期最大高度,单位像素
layout enum — row, column, grid — 子对象自动布局:行/列/网格
layout_wrap bool — 默认 false — 将行/列子对象换行到其他行
grid_columns int — 1…65535 — 网格布局的等宽列数
grid_column_span int — 默认 1; 1…65535 — 父网格布局中的列跨度
gap int — 默认 0; 0…4096 — 自动布局间距(像素)
padding int — 默认 0; 0…4096 — 自动布局内边距(像素)
padding_left int — 0…4096 — 行布局起始内边距覆盖值
padding_right int — 0…4096 — 行布局末尾内边距覆盖值
padding_top int — 0…4096 — 列布局起始内边距覆盖值
padding_bottom int — 0…4096 — 列布局末尾内边距覆盖值
grow int — 默认 0; 0…100 — 自动布局扩展权重
margin int — 默认 0; 0…4096 — 子对象两侧的自动布局外边距
margin_left int — 0…4096 — 自动布局 x 轴起始外边距
margin_right int — 0…4096 — 自动布局 x 轴末尾外边距
margin_top int — 0…4096 — 自动布局 y 轴起始外边距
margin_bottom int — 0…4096 — 自动布局 y 轴末尾外边距
align_main enum — start, center, end, space_between — 自动布局主轴对齐方式
align_cross enum — start, center, end, stretch — 自动布局交叉轴对齐方式
dynamic_reflow bool — 默认 false — 将直接命名布局子对象自动提升为动态定位并生成 gsp_<stem>_<name>_reflow_ex 与 _scroll_attach/_scroll_reflow() 辅助函数
scrollable bool — 默认 false — 在 dynamic_reflow 容器上启用内置拖拽滚动辅助函数(自动开启 clip_children: true)
scroll enum — 默认 none; none, vertical, horizontal, auto — 在 dynamic_reflow 容器上启用内置拖拽滚动的简写(自动开启 clip_children: true)
hidden bool — 默认 false 是 初始隐藏(通过动作或 set_visible 显示)
fg_color color — — — 前景颜色(根据控件类型用于文字、旋钮、线条或标记)
opacity int — 默认 255; 0…255 场景: 不支持; 模板: 不支持 0–255 混合透明度
bg_color color — — 是 背景/填充颜色(#RRGGBB 或 #RRGGBBAA)
bg_gradient color — — — 第二个渐变色标(与 bg_color 配合)
gradient_dir enum — 默认 vertical; vertical, horizontal — 渐变方向
radius int — 默认 0; 0…65535 场景: 不支持; 模板: 不支持 圆角半径(像素)
shadow_color color — — — 静态硬阴影颜色
shadow_opacity int — 默认 96; 0…255 — 静态硬阴影透明度
shadow_offset_x int — -32768…32767 — 静态硬阴影 x 偏移
shadow_offset_y int — -32768…32767 — 静态硬阴影 y 偏移
shadow_spread int — 0…4096 — 静态硬阴影扩散范围(像素)
shadow_radius int — 0…65535 — 静态硬阴影圆角半径
border_color color — — — 边框描边颜色
border_width int — 0…65535 — 边框描边宽度(需要 border_color)
border_opacity int — 默认 255; 0…255 — 边框描边透明度
outline_color color — — — 外部轮廓颜色
outline_width int — 默认 0; 0…65535 — 外部轮廓宽度
outline_opacity int — 默认 255; 0…255 — 外部轮廓透明度
outline_pad int — 默认 0; 0…4096 — 元素与外部轮廓之间的间隔
text_line_space int — 默认 0; 0…4096 — 静态文字行之间的额外间距,单位像素
text_vertical_align enum — 默认 auto; auto, top, center, bottom — 静态文字块的垂直对齐;auto 保留单行居中、多行顶部对齐
text string — — 是 静态文字内容(UTF-8)
text_align enum — left, center, right — 文字对齐方式
overflow enum — 默认 clip; clip, ellipsis — 单行文字溢出方式
font path — — — 单个对象的 TTF/OTF 字体覆盖路径
font_size int — 1…255 — 单个对象的字体像素尺寸
font_charset string — — — 运行时绑定文字可使用的字形;静态文字会自动加入
font_charset_file path — — — 相对于场景的 UTF-8 字符集文件,与 font_charset 和静态文本合并
font_link enum — embedded, external, auto — 字体存储策略:内嵌/外部/自动
input bool — 默认 false — 文字输入字段:接入光标/键盘流程
animation_codec enum — lossless, eaf4, eaf4_bounded, jpeg, hardware_jpeg — 动画帧策略:无损增量帧、EAF4 4×4 帧间像素差分、完整 JPEG 帧,或仅在目标具备硬件解码能力时使用 JPEG
svg_layout enum — content, canvas — SVG 部件定位:裁紧内容或保留原画布布局
morph_to path — — — 路径结构和填充一致的 SVG 结束形状
morph int — 0…100 — SVG 形变或位图网格位移的插值进度,单位为百分比
svg_element string — — — SVG 元素 ID;按绘制边界导入为独立图片
tint color — — — SVG 轮廓颜色;生成运行时颜色设置接口
image path — — 是 图片、动图、视频(可选 FFmpeg)或编译 SVG 的文件路径
video_start_ms int — 0…4294967295 — 视频片段起点,单位毫秒
video_duration_ms int — 1…4294967295 — 视频片段时长,单位毫秒;省略则导入到结尾
video_fit enum — contain, cover, stretch — 构建期视频宽高比适配;contain 使用黑色补边
video_loop_count int — 0…65535 — 视频播放次数(默认 1;0 无限循环)
video_max_decoded_bytes int — 1…9223372036854775807 — 主机保留的 RGBA 像素预算(默认 256 MiB,不代表进程总内存)
video_max_frames int — 1…65535 — 最大视频解码帧数(默认 4096)
codec enum — raw, lossless, jpeg, auto, speed, size, store, qoi, png, rle16, rle16_a8, rle32, default, hardware_jpeg — 图片编码格式
dither bool — — — 减轻图片和动图的 RGB565 色阶;覆盖 Profile 的 image_dither
quality int — 1…100 — JPEG 质量 1–100(省略时使用 Profile 默认值)
jpeg_quality int — 1…100 — 旧版 JPEG 质量字段别名
compress bool — — — 图片压缩开关(兼容字段;优先使用 codec)
cache_policy enum — mmap_direct, mmap, decode_lru, lru, preload — 图片缓存策略:mmap_direct、decode_lru 或 preload
store_scale number — 0.05…1.0 — 编码时应用的预缩放比例
max_fps int — 0…120 — 动画导入帧率上限;视频默认 30,显式 0 保留源时间戳
fit enum — 默认 stretch; stretch, fill, contain, cover, native — 图像适配方式,native 以原始像素大小居中绘制,并按旋转范围计算脏区
position_x number — 默认 0.5; 0.0…1.0 — 图片适配的水平对齐位置
position_y number — 默认 0.5; 0.0…1.0 — 图片适配的垂直对齐位置
rotation int — 默认 0; -32768…32767 场景: 图片; 模板: 不支持 图片绕边界框中心顺时针旋转,支持源图透明度;超出边界框的部分会被裁剪
scalable bool — 默认 false — 启用运行时图片缩放
scale number — 默认 1.0; 0.0625…16.0 — 运行时图片初始缩放比例
min_scale number — 默认 0.5; 0.0625…16.0 — 运行时图片最小缩放比例
max_scale number — 默认 4.0; 0.0625…16.0 — 运行时图片最大缩放比例
enabled bool — — — 初始交互状态;设置后会公开可由后代控件继承的运行时 enabled 属性
disabled_color color — 默认 #808080 — 禁用态覆盖颜色
disabled_opacity int — 默认 112; 0…255 — 禁用态覆盖透明度
bind identifier — — — 公开状态名称;生成 GSP_BIND_<NAME>
bind_target enum — visible, value, color, text, resource, data — 显式绑定状态类型
dynamic string_list — — — 运行时可变属性列表(如 text、color、visible、resource、x、y、w、h、opacity)
callback identifier — — — 应用回调名称;生成按场景区分的事件辅助函数
events action_list — — — 输入绑定:[{event, action, ...}]
template identifier — — — 将此子树声明为渲染模板
max_instances int — 1…65535 — 同时存活模板实例的最大数量;计入自动推导的资源池需求
dynamic_color bool — — — 模板成员公开每实例独立颜色槽
dynamic_image bool — — — 模板图片公开每实例独立资源槽
查看所有组件共享字段
字段 类型 必填 默认值 / 范围 可运行时更新 编译器定义
parent_name string — — — 使用名称而不是索引指定父对象