Tour 漫游式引导
通过遮罩和分步浮层引导用户认识页面中的关键功能。
何时使用
在线体验
基本用法
import "github.com/tangoui/tango/ui"
var tourOpen bool
var tourCurrent int
func tourPage(c *ui.Context) {
upload := ui.Button(c, "上传").ID()
steps := []ui.TourStep{{
Target: upload, Title: "上传文件", Description: "从这里选择要导入的文件。",
}}
if ui.Button(c, "开始引导").Clicked() {
tourOpen = true
tourCurrent = 0
}
ui.Tour(c, &tourOpen, &tourCurrent, steps).Show()
}Tour 绑定打开状态、当前步骤和步骤切片。目标控件通过其 ID() 传入;Target 为零时显示居中面板。
常见用法
多步骤与位置
var tourOpen2 bool
var tourCurrent2 int
func tourSteps(c *ui.Context) {
first := ui.Button(c, "第一步").ID()
second := ui.Button(c, "第二步").ID()
steps := []ui.TourStep{
{Target: first, Title: "创建", Description: "先创建一条记录。"},
{Target: second, Title: "保存", Description: "完成后保存。", Placement: "right", PrevText: "返回"},
}
ui.Tour(c, &tourOpen2, &tourCurrent2, steps).Primary().Gap(8).Show()
}Placement 使用 Popover 的位置字符串;Primary 使用主色面板,Gap 设置目标洞口的留白。
自定义操作和指示器
func tourRender(c *ui.Context) {
target := ui.Button(c, "功能").ID()
steps := []ui.TourStep{{Target: target, Title: "功能", Description: "这里是关键入口。"}}
ui.Tour(c, &tourOpen2, &tourCurrent2, steps).
IndicatorsRender(func(current, total int) {
ui.Text(c, "步骤").Tertiary()
}).ActionsRender(func(a *ui.TourActions) {
a.Default()
}).Show()
}IndicatorsRender 和 ActionsRender 替换默认区域。自定义操作中可调用 a.Prev()、a.Next()、a.GoTo(i) 或 a.Close()。
每步覆盖选项
func tourStepOptions(c *ui.Context) {
target := ui.Button(c, "更多").ID()
steps := []ui.TourStep{{
Target: target, Title: "更多操作", Description: "这里没有遮罩。",
Mask: ui.TourMaskOff, Type: ui.TourTypePrimary,
HidePrev: true, NextText: "知道了",
}}
ui.Tour(c, &tourOpen2, &tourCurrent2, steps).Show()
}Mask、Type、按钮文案和隐藏按钮都可在单步设置,覆盖 Tour 的默认选项。
结束和关闭
func tourEvents(c *ui.Context) {
target := ui.Button(c, "目标").ID()
tour := ui.Tour(c, &tourOpen2, &tourCurrent2, []ui.TourStep{{Target: target, Title: "提示"}}).Show()
if tour.Closed() || tour.Finished() {
tourOpen2 = false
}
}Show() 返回 Tour 实例,可在同一帧读取 Closed() 或 Finished();打开状态和当前步骤必须放在函数外。Esc 和关闭按钮都会产生关闭事件。
主题与样式
Tour 使用 Popover 的定位语义,并由主题提供面板、遮罩、箭头和按钮样式。Primary()、NoMask()、NoArrow() 是页面级选项;不同主题会调整主色和阴影,但不改变步骤数据结构。
注意事项
目标控件必须在同一帧先构建并取得 ID(),再构建 Tour;动态列表目标变化时要重新绑定正确 ID。步骤过多会降低理解度,建议只覆盖关键路径。遮罩开启时应确保目标和操作按钮仍可键盘访问;目标被销毁时应关闭或跳过该步骤。滚动默认会把目标带入可视区域,可用 ScrollIntoView(false) 关闭。
示例代码
以下是上方在线演示运行的 Go 源码(来自官方 gallery),点击标题展开。函数接收的 c *ui.Context 由窗口的 UI 循环每帧传入,接入方式见 Go GUI 入门。
API
签名与说明由 site/tools/compdoc 从 Go 源码和文档注释提取;点击名称跳到 Go API 参考 的对应条目。
入口函数
ui.Tour
func Tour(c *Context, open *bool, current *int, steps []TourStep) *TourWTour starts a tour bound to *open and *current; call Show.
返回 *TourW,可链式调用的方法见 TourW 方法。
TourW 方法
ui.TourW:TourW is a guided tour: a mask with a hole around the current target and a panel with step indicators and prev/next buttons.
| 方法 | 说明 | 签名 |
|---|---|---|
ActionsRender | ActionsRender replaces the previous / next buttons with the content fn builds (Ant actionsRender); a.Default() builds the default buttons and a.Prev / Next / GoTo / Close drive the tour. | ActionsRender(fn func(a *TourActions)) *TourW |
Changed | Changed reports a step change. | Changed() bool |
Closable | Closable shows or hides the close button; without it Escape does not close the tour either (antd closable null; default shown). | Closable(on bool) *TourW |
Closed | Closed reports the tour was dismissed (close button or Escape). | Closed() bool |
DisabledInteraction | DisabledInteraction keeps the highlighted target from being clicked (antd disabledInteraction; by default the target stays interactive, Element target-area-clickable). | DisabledInteraction(on bool) *TourW |
Finished | Finished reports a click on the last step's finish button. | Finished() bool |
Gap | Gap sets the hole padding around the target (antd / Element gap.offset, default 6). | Gap(px float32) *TourW |
GapRadius | GapRadius sets the corner radius of the hole around the target (antd / Element gap.radius, default 2). | GapRadius(px float32) *TourW |
IndicatorsRender | IndicatorsRender replaces the step indicators (dots; Element: "1 / 3") with the content fn builds (Ant indicatorsRender). | IndicatorsRender(fn func(current, total int)) *TourW |
Keyboard | Keyboard sets whether Escape closes the tour and ←/→ move between steps (default true; antd keyboard, Element close-on-press-escape). | Keyboard(on bool) *TourW |
MaskColor | MaskColor sets the mask color (antd mask.color; default the theme's). | MaskColor(col theme.Color) *TourW |
NoArrow | NoArrow hides the panel arrow. | NoArrow() *TourW |
NoMask | NoMask hides the dimming mask. | NoMask() *TourW |
Placement | Placement sets the default panel placement of the steps, as Popover.Placement (default "bottom"); TourStep.Placement overrides it. | Placement(s string) *TourW |
Primary | Primary uses the brand-coloured panel. | Primary() *TourW |
ScrollIntoView | ScrollIntoView sets whether each step scrolls its target into the view of the scroll container holding it when the step is shown (default on, Ant scrollIntoViewOptions). | ScrollIntoView(on bool) *TourW |
Show | Show builds the tour layers when open. | Show() *TourW |
TourStep 参数类型
ui.TourStep:TourStep is one step of a Tour.
| 字段 | 说明 | 类型 |
|---|---|---|
Target | widget to highlight (0: centred panel, no hole) | ID |
Title | string | |
Description | string | |
Cover | optional picture above the title | func() |
Placement | as Popover.Placement; default "bottom" | string |
Mask | Per-step overrides of the tour's options. | TourMask |
Type | TourTypeDefault / TourTypePrimary; "": the tour's setting | TourType |
PrevText | label of the previous button ("": Locale) | string |
NextText | label of the next / finish button ("": Locale) | string |
HidePrev | no previous button on this step | bool |
HideNext | no next / finish button on this step | bool |
HideArrow | HideArrow hides the panel arrow on this step (antd steps[].arrow false). | bool |