Tooltip工具提示
查看源码components/tooltip.go悬停提示#
悬停或点击触发器显示顶部提示。
trigger := tree.ID(page, "trigger")
canvas.HitWithOptions(trigger, anchor, "Show tooltip", ui.HitOptions{Role: ui.RoleButton})
if canvas.Clicked(trigger) {
open = !open
}
if open || canvas.Hovered() == trigger {
components.BeginTooltip(canvas, theme, viewport, anchor,
components.TooltipOptions{Layer: 60, Side: components.TooltipTop, MaxWidth: 176}).
Paint("Native synchronous tooltip")
}四个方向#
同屏展示上、右、下、左四种放置方向。
labels := [...]string{"Top", "Right", "Bottom", "Left"}
sides := [...]components.TooltipSide{
components.TooltipTop, components.TooltipRight,
components.TooltipBottom, components.TooltipLeft,
}
for index, anchor := range anchors {
components.BeginTooltip(canvas, theme, viewport, anchor, components.TooltipOptions{
Layer: uint16(60 + index), Side: sides[index], MaxWidth: 96,
}).Paint(labels[index])
}视口约束#
靠近边缘时将提示内容限制在视口内。
anchor := f.Slot().Size(60, theme.Metrics.ControlHeight).Self(flex.Start).Rect()
anchor.X, anchor.Width = size.Width-76, 60 // pin the trigger to the window edge
components.BeginTooltip(canvas, theme, viewport, anchor, components.TooltipOptions{
Layer: 60, Side: components.TooltipBottom, MaxWidth: 176,
}).Paint("Clamped to the viewport")安装
将组件源码复制到自己的项目,直接修改。
go run ./cmd/viva add --out ./components/local tooltip源码组件用法#
此预览直接调用生产 Go 组件。稳定 ID、值与打开状态仍由应用持有;组件只负责绘制并声明交互语义。
- 标题旁的源码链接列出了全部选项与视觉决策。
- 请使用原生命令验证系统文本、焦点、无障碍与平台集成。
- 浏览器预览验证共享布局和场景行为,不代表原生性能。
验证边界#
示例覆盖当前 API 的代表性状态,但不表示每个桌面后端都已完成真机验证。
键盘操作
Tab 在控件间移动;Ctrl/⌘ K 搜索组件。进入预览后,键盘事件由 Go 运行时处理。