Toolbar
Example sourceGo · Copyable
This is the complete source for the gallery example above.examples/components/toolbar.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 (
"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("toolbar", "navigation", func() core.Widget {
msg := demoText("Narrow the window to move overflowing buttons into More; ←→ moves between buttons", "缩小窗口,放不下的按钮会移到「更多」里;←→ 在按钮间移动")
do := func(s string) func() { return func() { msg = demoText("Executed: ", "已执行:") + s } }
zoom := kit.Select("", "75%", "100%", "125%", "150%").OnChange(func(value string) { msg = demoText("Zoom: ", "缩放:") + value })
zoom.SetValue("100%")
bar := kit.Toolbar(
kit.ToolbarItem{Label: demoText("New", "新建"), Icon: kit.IconPlus, Action: do(demoText("New", "新建"))},
kit.ToolbarItem{Label: demoText("Search", "搜索"), Icon: kit.IconSearch, IconOnly: true, Action: do(demoText("Search", "搜索"))},
kit.ToolbarItem{Separator: true},
kit.ToolbarItem{Label: demoText("Zoom", "缩放"), Width: 150, Content: zoom},
kit.ToolbarItem{Label: demoText("Copy", "复制"), Action: do(demoText("Copy", "复制"))},
kit.ToolbarItem{Label: demoText("Export", "导出"), Action: do(demoText("Export", "导出"))},
kit.ToolbarItem{Label: demoText("Print", "打印"), Action: do(demoText("Print", "打印"))},
kit.ToolbarItem{Label: demoText("Share", "分享"), Action: do(demoText("Share", "分享"))},
kit.ToolbarItem{Label: demoText("Archive", "归档"), Action: do(demoText("Archive", "归档")), Disabled: true},
)
bar.Leading(el.ViewFunc(func(*el.Context) el.Element { return el.Div().Px(8).Child(el.Text(demoText("Files", "文件"))) })).Trailing(kit.Button(demoText("Help", "帮助"), do(demoText("Help", "帮助"))).Variant(kit.ButtonGhost))
return el.Root(el.ViewFunc(func(cx *el.Context) el.Element {
return el.Div().P(24).Gap(12).Items(el.Stretch).Child(
el.Div().Border(1, theme.Outline).Rounded(8).P(4).Items(el.Stretch).Child(bar.Render(cx)),
el.Text(msg).TextColor(theme.Muted))
}))
})
}
A row of command buttons, buttons that cannot fit will be automatically moved into the "More" menu.
bar := kit.Toolbar(
kit.ToolbarItem{Label: "New", Icon: kit.IconPlus, Action: create},
kit.ToolbarItem{Separator: true},
kit.ToolbarItem{Label: "Export", Action: export},
)
- Each item can be a button or a divider. When
Iconis set, the icon is displayed, and whenIconOnlyis set, only the icon is displayed with the name as a prompt; disabled buttons do not respond. - Overflow judgment uses the width given by the parent container, so the toolbar must be placed in a position with width constraints, such as the entire row, or a container with a set width. When the parent container is width-limited by content, all buttons will be displayed.
- The command area only occupies one Tab stop, ← → moves between available buttons and "More", loops from beginning to end, Home / End jumps to beginning and end, press Enter or space to execute. Return to trigger buttons after the More menu is closed; left and right append actions retain their respective tab stops.
Leading(el.View)/Trailing(el.View)fix the left and right areas, and the middle command overflows according to the remaining width.Size(dp)sets the uniform height, default is 32, minimum is 24.SetDisableddisables the entire toolbar and additional actions;SetItemDisabled(index, bool)disables individual items. Icon buttons are visible via hover or keyboard focus.SetItemsreplaces the button and closes the old menu; passing in a slice makes a copy, andItems()also returns a copy. The disabled state of the command with the same name is independent.
Agent: container role toolbar, the button is listed separately; the "More" button opens the ordinary menu.
Verify: go run ./examples/components -section toolbar, add -theme dark to check the dark theme.
When the width of the command area has not yet been measured in the first frame, the operation is to enter "More" first, and after the measurement, the buttons that can be accommodated are automatically expanded. The command area crops its own drawing and hits to avoid covering the right slot when it is first displayed or when the window is zoomed; there is a regression test for the first frame menu operation.
Custom group anywhere
ToolbarItem{Label: "Zoom", Content: zoomSelect, Width: 150} Place an interactive view at this location, and multiple controls can be combined inside the view. Content takes precedence over Action/Icon, Separator still takes precedence; Label is used for group semantics and overflow menus. Width refers to the group width. The legal range is greater than 0 and no more than 4096dp. If it is not set or illegal, 160dp is used.
For groups that cannot be placed, use Label to enter "More". After clicking, Content will be displayed in the modal overlay below the toolbar. OverflowContent can be used to provide another layout; the overlay is limited to the window, scrolled when the content is too high, Esc or click outside to close. Closes the overlay when the group is resized, disabled, or replaced by SetItems. The state of the custom view is held by the application; switching between the toolbar and the overlay will change the element tree path, and the internal state of the frame such as input selection is not guaranteed to be retained.
The custom group's child controls retain their respective tab stops and arrow key behaviors, and the toolbar's left and right/Home/End navigation still only manages command buttons. Disabling entire groups and entire toolbars is passed to child controls. The example adds a zoom selector between search and copy; automatic testing covers double overflow, nested Select, Esc, disabling, width change and replacement cleanup; the native vision is not accepted.