StatusBar
示例代码Go · 可复制
这是上方组件库展示案例的完整源码。examples/components/status_bar.go
在脚手架项目中复用组件构造和 Render 写法;示例的 registerSection 与共享辅助函数由组件库提供。
package main
import (
"fmt"
"github.com/dyike/keel/ui/core"
"github.com/dyike/keel/ui/el"
"github.com/dyike/keel/ui/kit"
"github.com/dyike/keel/ui/theme"
)
func init() {
registerSection("status_bar", "shell", func() core.Widget { return el.Embed(newStatusBarGallery()) })
}
// The bars live in the view: an overflowing bar remembers its item widths.
type statusBarGallery struct {
wide, narrow, editor *kit.StatusBarView
width *kit.SliderView
last string
}
func newStatusBarGallery() *statusBarGallery {
g := &statusBarGallery{}
text := func(s string) el.View { return el.ViewFunc(func(cx *el.Context) el.Element { return el.Text(s) }) }
g.wide = kit.StatusBar().Left(text(demoText("Connected", "已连接"))).Right(text(demoText("123 records", "共 123 条记录")))
g.narrow = kit.StatusBar().Left(text(demoText("Offline; waiting to reconnect", "离线,等待重新连接"))).Right(text(demoText("3 tasks awaiting delivery", "3 个任务待发送")))
act := func(s string) func() { return func() { g.last = s } }
g.editor = kit.StatusBar().Left(text(demoText("Ready", "就绪"))).
Add(kit.StatusItem{Label: demoText("main branch", "main 分支"), Action: act(demoText("Switch branch", "切换分支")), Priority: 3},
kit.StatusItem{Label: demoText("0 errors, 2 warnings", "0 个错误 2 个警告"), Action: act(demoText("Open problems panel", "打开问题面板")), Priority: 2}).
AddRight(kit.StatusItem{Label: demoText("Line 12, column 4", "行 12,列 4"), Action: act(demoText("Go to line", "跳到行")), Priority: 2},
kit.StatusItem{Label: demoText("Spaces: 4", "空格: 4"), Action: act(demoText("Indentation settings", "缩进设置")), Priority: 1},
kit.StatusItem{Label: "UTF-8", Action: act(demoText("Choose encoding", "选择编码"))},
kit.StatusItem{Label: "Go", Action: act(demoText("Choose language", "选择语言")), Priority: 1})
g.width = kit.Slider(demoText("Status bar width", "状态栏宽度"), 240, 760).Step(20)
g.width.SetValue(640)
return g
}
func (g *statusBarGallery) Render(cx *el.Context) el.Element {
note := demoText("Drag the slider to narrow the status bar. Items that do not fit move into the … menu by priority.", "拖动滑块缩窄状态栏,放不下的项按优先级收进 … 菜单")
if g.last != "" {
note = fmt.Sprintf(demoText("Clicked: %s", "点了:%s"), g.last)
}
return el.Div().W(el.Full).Gap(16).Child(
g.wide.Render(cx),
el.Div().W(el.Dp(180)).Child(g.narrow.Render(cx)),
el.Div().W(el.Dp(320)).Child(g.width.Render(cx)),
el.Div().W(el.Dp(float32(g.width.Value()))).Child(g.editor.Render(cx)),
el.Text(note).TextSize(theme.TextSm).TextColor(theme.Muted),
)
}
kit.StatusBar().Left(status).Right(position) 创建固定 24dp 状态栏,顶部 1dp 边框,默认 12sp Muted 文本。左右内容分组,中间空间由 Grow 撑开,长文本限制单行。父视图决定状态栏位置。
Left/Right 接受 el.View,替换各自的视图组,每帧调用子视图的 Render。简单内容可用 el.ViewFunc 包装:
status := el.ViewFunc(func(cx *el.Context) el.Element {
return el.Text("就绪").TextColor(theme.Muted)
})
溢出菜单
窄窗口里放不下所有内容时,用 Add(左组)和 AddRight(右组)加入可以收起的项:
bar := kit.StatusBar().Left(ready).
Add(kit.StatusItem{Label: "main 分支", Action: switchBranch, Priority: 3}).
AddRight(
kit.StatusItem{Label: "行 12,列 4", Action: gotoLine, Priority: 2},
kit.StatusItem{Label: "UTF-8", Action: pickEncoding},
)
- 放不下时,
Priority低的先收进右侧的 “…” 菜单;同优先级时排在后面的先收。收起一个宽项后空出的位置,会让之前收起的窄项回来。 - 窗口变宽后,收起的项自动回到状态栏。
Left、Right传入的视图始终显示,不会收起,文字过长时截断。View为空时状态栏显示Label;有Action时它是可点的按钮。菜单里总是显示Label,点击运行Action。- 状态栏要按宽度记住每一项的尺寸,所以要保存在视图里复用,不要每帧新建。
Hidden()返回当前收起的项的Label。
Agent 角色 status,子元素单独列出;“…” 是名为“更多”的按钮。
验证:go run ./examples/components -section status_bar -theme dark,省略 theme 查看浅色。