Keel v0.5.1 English GitHub

BubbleGroup

在线示例:这是真实的 Keel 程序编译成 WebAssembly,可以直接操作。首次加载约 10 MB。全屏打开
示例代码Go · 可复制

这是上方组件库展示案例的完整源码。examples/components/bubble_group.go

在脚手架项目中复用组件构造和 Render 写法;示例的 registerSection 与共享辅助函数由组件库提供。

package main

import (
	"github.com/dyike/keel/ui/core"
	"github.com/dyike/keel/ui/el"
	"github.com/dyike/keel/ui/kit"
)

func init() {
	registerSection("bubble_group", "chat", func() core.Widget {
		say := func(s string) el.View { return el.ViewFunc(func(*el.Context) el.Element { return el.Text(s) }) }
		received := kit.BubbleGroup(
			kit.Bubble(say(demoText("Order confirmed.", "订单已确认。"))),
			kit.Bubble(say(demoText("Shipping is scheduled for tomorrow morning. The tracking number will be sent after dispatch.", "明天上午安排发货,运单号会在出库后发送。"))),
		).Name(demoText("Consecutive support messages", "客服连续消息"))
		sent := kit.BubbleGroup(
			kit.Bubble(say(demoText("Got it, thanks. ", "收到,谢谢。 "))).Mine(),
			kit.Bubble(say(demoText("Please leave it at the reception desk. ", "请帮我备注放在门卫。 "))).Mine(),
		).Name(demoText("My consecutive messages", "我的连续消息"))
		return el.Embed(el.ViewFunc(func(cx *el.Context) el.Element {
			return el.Div().P(24).Gap(24).Child(received.Render(cx), sent.Render(cx))
		}))
	})
}

将连续气泡排成纵向组,默认间距为 theme.SpaceMd(8dp)。分组由应用决定;组件不推断发送人,也不改变各气泡的外观、左右对齐或反应槽。

group := kit.BubbleGroup(first, second).Name("客服消息").Gap(6)
group.SetItems(first, second, third)

SetItems 复制切片并忽略 nil,空参数清空;Items 返回切片副本。应复用 Bubble 实例,每个实例在组内只出现一次。插入、删除其他项或重排时,保留实例的内部输入状态与焦点继续保留;被删除的控件不再参与交互。普通自定义 View 需自行提供稳定 ID。

Gap 接受非负有限 dp,非法值忽略。Style(func(*el.DivEl)) 在默认布局后配置间距、边框、背景等,nil 恢复默认;回调只用于当帧元素。Name 设置组的语义名称,SetDisabled 禁用所有子项但不修改子组件自身状态。组件保持组身份、角色、名称和禁用状态。

分组占满可用宽度,普通 Bubble 仍按自己的 75% 上限布局,Ghost 使用整行。需要头像、作者或消息状态时使用 Message。

运行:go run ./examples/components -section bubble_group,加 -theme dark 检查深色。