StatusBar
Example sourceGo · Copyable
This is the complete source for the gallery example above.examples/components/status_bar.go
Reuse the component construction and Render methods in a scaffolded project. The example’s registerSection and shared helpers are provided by the component gallery.
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) Creates a fixed 24dp status bar with a top 1dp border and default 12sp Muted text. The left and right content are grouped, the middle space is opened by Grow, and long text is limited to a single line. The parent view determines the status bar position.
Left/Right accepts el.View, replaces the respective view group, and calls the subview's Render every frame. Simple content can be wrapped with el.ViewFunc:
status := el.ViewFunc(func(cx *el.Context) el.Element {
return el.Text("Ready").TextColor(theme.Muted)
})
Overflow menu
When all the content cannot fit in the narrow window, use Add (left group) and AddRight (right group) to add items that can be collapsed:
bar := kit.StatusBar().Left(ready).
Add(kit.StatusItem{Label: "main branch", Action: switchBranch, Priority: 3}).
AddRight(
kit.StatusItem{Label: "Line 12, column 4", Action: gotoLine, Priority: 2},
kit.StatusItem{Label: "UTF-8", Action: pickEncoding},
)
- When it cannot be put down, the one with the lowest
Prioritywill be put into the "..." menu on the right first; if it has the same priority, the one with the lowerPrioritywill be put first. The space vacated after folding a wide item will bring back the narrow item that was previously folded. - After the window is widened, the collapsed items automatically return to the status bar.
Left,RightThe incoming view is always displayed and will not be collapsed. If the text is too long, it will be truncated.- When
Viewis empty, the status bar displaysLabel; whenActionis present, it is a clickable button.Labelis always displayed in the menu, click to runAction. - The status bar must remember the size of each item according to its width, so it must be saved in the view for reuse instead of creating a new one every frame.
Hidden()returns theLabelof the currently collapsed item.
Agent role status, child elements are listed separately; "..." is a button named "More".
Verification: go run ./examples/components -section status_bar -theme dark, omit theme to see the light theme.