Skip to content

Command 命令面板 ​

提供可搜索、可分组、支持键盘上下选择的命令面板,适合应用内操作和快速跳转。

何时使用 ​

  • 命令数量较多,需要按标签、值或关键词快速过滤时使用。
  • 用户熟悉快捷键并希望用键盘完成选择时使用。
  • 只有少量固定选项的表单选择,使用 Select 选择器;需要层级导航时使用 Menu 导航菜单。
  • 命令需要在模态层中打开时,先用 Modal 对话框 管理打开状态,再在内容中放置 Command。

在线体验 ​

Command 命令面板 · 在线体验真实 Go 组件
在独立页面打开 ↗

查看此场景的 Go 源码 ↗

基本用法 ​

Command 接收 []ui.CommandItem,默认在 Label、Value 和 Keywords 中做不区分大小写的匹配,并按匹配程度排序。按 Enter 或点击条目后,用 Selected() 读取本帧选中的项目。

go
import (
    "github.com/tangoui/tango/ui"
    "github.com/tangoui/tango/ui/layout"
)

var commandPicked string

func commandPage(c *ui.Context) {
    items := []ui.CommandItem{
        {Value: "new", Label: "新建文件"},
        {Value: "open", Label: "打开项目"},
        {Value: "settings", Label: "设置"},
    }
    palette := ui.Command(c, items).Layout(layout.W(360))
    if item, ok := palette.Selected(); ok {
        commandPicked = item.Value
    }
    ui.Text(c, "已选择:"+commandPicked)
}

常见用法 ​

分组、图标和快捷键 ​

相同 Group 的条目会归在一起。没有查询时按组首次出现的顺序排列,搜索时按各组排名最高的条目排列。Icon 放置前置图标,Shortcut 是右侧的快捷键提示;执行逻辑由 Selected() 处理。

go
import (
    "github.com/tangoui/tango/ui"
    "github.com/tangoui/tango/ui/icon"
)

func groupedCommandPage(c *ui.Context) {
    ui.Command(c, []ui.CommandItem{
        {Value: "calendar", Label: "日历", Group: "建议", Icon: icon.Calendar},
        {Value: "profile", Label: "个人资料", Group: "设置", Icon: icon.User, Shortcut: "⌘P"},
        {Value: "settings", Label: "设置", Group: "设置", Icon: icon.Setting, Shortcut: "⌘S"},
    }).Shadow()
}

关键词搜索和禁用项 ​

Keywords 用于补充搜索词;Disabled 项仍可展示但不会被键盘或点击选中。

go
import "github.com/tangoui/tango/ui"

func searchableCommandPage(c *ui.Context) {
    ui.Command(c, []ui.CommandItem{
        {Value: "docs", Label: "打开文档", Keywords: []string{"帮助", "指南"}},
        {Value: "cache", Label: "清理缓存", Disabled: true, Keywords: []string{"维护"}},
    }).Placeholder("搜索命令").Empty("没有匹配的命令")
}

限制面板高度 ​

MaxHeight 只限制结果列表的高度;结果超过高度时由面板内部的 Scroll 处理,面板宽度和位置仍可用 Layout 设置。

go
import (
    "fmt"

    "github.com/tangoui/tango/ui"
    "github.com/tangoui/tango/ui/layout"
)

func tallCommandPage(c *ui.Context) {
    items := make([]ui.CommandItem, 40)
    for i := range items {
        items[i] = ui.CommandItem{Value: fmt.Sprintf("item-%d", i), Label: fmt.Sprintf("命令 %d", i+1)}
    }
    ui.Command(c, items).MaxHeight(220).Layout(layout.W(420))
}

读取查询文本 ​

Query() 返回当前搜索框内容,适合在面板旁显示调试信息或将选择结果映射到应用状态。

go
import "github.com/tangoui/tango/ui"

func queryCommandPage(c *ui.Context) {
    palette := ui.Command(c, []ui.CommandItem{{Value: "build", Label: "构建项目"}})
    ui.Text(c, "查询:"+palette.Query()).Tertiary()
}

主题与样式 ​

  • Ant Design:面板使用悬浮容器、分隔线和主题的选中背景。
  • Element Plus:仍保留分组、禁用和键盘选择行为,颜色来自 Element 主题令牌。
  • shadcn:Command 是 shadcn 风格的搜索框、列表项和选中背景;Shadow() 只增加浮层阴影。
  • ConfigProvider.Defaults("Command", ui.Props{"placeholder": "搜索", "emptyText": "无结果"}) 可设置默认提示文案。还可设置 loop(默认 false)、shouldFilter(默认 true)和 vimBindings(默认 true)。

注意事项 ​

  • 状态:Command 内部保留查询和活动行;业务选中值要在 Selected() 成功后写入页面外部状态。
  • 键盘。输入框聚焦后使用 ↑ / ↓ 移动、Home / End 跳到首尾、Enter 选择。默认也支持 Ctrl+N/J 向下和 Ctrl+P/K 向上,移动时跳过禁用项。
  • 性能:大量条目应提供简洁的 Label、稳定的 Value 和必要的关键词,避免在每帧生成昂贵的字符串或图标对象。
  • 大列表。显示的条目多于 100 项时自动虚拟化,只构建可见行和活动行,键盘移动时列表跟随活动行。无障碍树只暴露已构建的条目。用 Virtual(false) 构建所有行,或用 Virtual(true) 强制开启虚拟化。
  • 原地更新条目。虚拟化且未设置 Filter 时会复用匹配结果、顺序、布局和宽度。原地修改同一切片且长度不变时,给 DataVersion(version) 传入新版本,或替换切片,下一帧会重新计算。
  • 无障碍:组件提供 combobox、listbox 和 option 语义;Shortcut 只是可读提示,真正的全局快捷键需要单独注册。
  • RTL:列表和快捷键提示会按主题方向布局,CommandItem.Value 不应依赖视觉顺序。

示例代码 ​

以下是上方在线演示运行的 Go 源码(来自官方 gallery),点击标题展开。函数接收的 c *ui.Context 由窗口的 UI 循环每帧传入,接入方式见 Go GUI 入门。

Command 命令面板

gallery 演示 Command 的 Render 函数,源码 page_shadcn.go#L45。

加载中…

Command 排序、受控值与键盘

gallery 演示 CommandAlign 的 Render 函数,源码 page_b3i.go#L86。

加载中…

API ​

签名与说明由 site/tools/compdoc 从 Go 源码和文档注释提取;点击名称跳到 Go API 参考 的对应条目。

入口函数 ​

ui.Command ​

go
func Command(c *Context, items []CommandItem) *CommandW

Command adds a command palette over items. Typing filters and ranks the list like cmdk: an item matches when the search letters appear in order in its label, value or keywords (case-insensitive); whole-word and prefix matches rank first and groups follow their best item. Up/Down (and Ctrl+N/J, Ctrl+P/K) move the active row, Home/End jump to the first / last, Enter or a click picks it (Selected).

ConfigProvider.Defaults("Command", ...) keys: "placeholder", "emptyText" (string), "loop" (bool, default false), "shouldFilter" (bool, default true), "vimBindings" (bool, default true).

返回 *CommandW,可链式调用的方法见 CommandW 方法。

相关类型与成员 ​

ui.CommandItem ​

CommandItem is one entry of a Command palette.

CommandW 方法 ​

ui.CommandW:CommandW is a command palette under construction (shadcn/ui Command): a search field over a filtered, grouped list navigated with the arrow keys. It renders in every design system with the active theme's tokens.

方法说明签名
DataVersionDataVersion tells the palette that the items slice was modified in place (a virtual list caches its matches, layout and width by slice identity, length and this number): after editing items of the same slice without changing its length, change v (or pass a new slice) so they are recomputed on the next frame.DataVersion(v int) *CommandW
DisablePointerSelectionDisablePointerSelection stops hovering from moving the active item (cmdk disablePointerSelection); clicks still pick.DisablePointerSelection(on bool) *CommandW
EmptyEmpty replaces the "no results" text.Empty(s string) *CommandW
FilterFilter replaces the matching and ranking (cmdk filter): fn returns 0 to hide an item, else its rank (higher first) for the item's value (Label when Value is empty), the trimmed search and its keywords.Filter(fn func(value, search string, keywords []string) float64) *CommandW
IDID returns the palette box ID.ID() ID
LayoutLayout applies layout options to the palette box.Layout(opts ...layout.Option) *CommandW
LoopLoop lets the arrow keys wrap from the last item to the first and back (cmdk loop; default false).Loop(on bool) *CommandW
MaxHeightMaxHeight limits the list height (default 300).MaxHeight(h float32) *CommandW
PlaceholderPlaceholder replaces the search placeholder.Placeholder(s string) *CommandW
QueryQuery returns the current search text.Query() string
SearchSearch binds the search text (cmdk CommandInput value / onValueChange).Search(q *string) *CommandW
SelectedSelected returns the item picked this frame.Selected() (CommandItem, bool)
ShadowShadow gives the palette a floating shadow (as in a dialog).Shadow() *CommandW
ShouldFilterShouldFilter(false) lists every item whatever the search (cmdk shouldFilter): the app filters items itself, for example from a server, reading Query.ShouldFilter(on bool) *CommandW
ValueValue binds the value of the active item (cmdk value / onValueChange): the palette highlights the item whose Value (Label when empty) equals *v and writes the active item's value back as it moves.Value(v *string) *CommandW
ValueChangedValueChanged reports that the active item changed this frame.ValueChanged() bool
VimBindingsVimBindings(false) turns off Ctrl+N/J (next) and Ctrl+P/K (previous) (cmdk vimBindings; default true).VimBindings(on bool) *CommandW
VirtualVirtual forces the virtual list on or off; by default lists showing more than 100 items are virtual: only the rows in view and the active item are built (groups only for their visible part), the list keeps the width, height and scroll range of the full list and follows the active item when it moves with the keyboard, but items out of view are not in the accessibility tree.Virtual(on bool) *CommandW

CommandItem 类型 ​

ui.CommandItem:CommandItem is one entry of a Command palette.

字段说明类型
Valuestring
Labelstring
Groupheading the item is listed under ("" = none)string
Iconoptional leading icon*icon.Icon
Shortcutright-aligned hint such as "⌘P"string
Keywordsextra search terms[]string
Disabledbool

相关链接 ​

以 MIT 许可发布