Command palette
Searchable commands in a dialog or any bounded host, opened from a shortcut.
Loading the live example…
import (
"github.com/egoist/mygo/ui"
"github.com/oneo-me/onevra/source/components"
"github.com/oneo-me/onevra/source/components/command"
"github.com/oneo-me/onevra/source/icons/lucide"
)
type commandPalette struct {
open bool
state command.State
last string
}
var paletteCommands = []command.Item{
{ID: "new", Title: "New document", Group: "File", Shortcut: "⌘N", Icon: lucide.FilePlus()},
{ID: "open", Title: "Open…", Group: "File", Shortcut: "⌘O", Icon: lucide.FolderOpen()},
{ID: "export", Title: "Export as PDF", Group: "File", Keywords: []string{"print"}, Icon: lucide.FileDown()},
{ID: "theme", Title: "Toggle dark mode", Group: "View", Keywords: []string{"appearance"}, Icon: lucide.Moon()},
{ID: "zoom", Title: "Zoom in", Group: "View", Shortcut: "⌘+", Icon: lucide.ZoomIn()},
{ID: "settings", Title: "Settings", Group: "App", Shortcut: "⌘,", Keywords: []string{"preferences"}, Icon: lucide.Settings()},
}
func (p *commandPalette) View(c *ui.Context) {
t := c.Theme()
if c.Shortcut(ui.Cmd, ui.KeyK) {
p.open = true
}
ui.Column(c).Fill().Center().Gap(t.Space(3)).Children(func() {
if components.Button(c, "Open command palette", components.ButtonSecondary).Clicked() {
p.open = true
}
text := "Or press ⌘K (Ctrl+K)"
if p.last != "" {
text = "Ran " + p.last
}
components.Text(c, text).TextColor(t.TextMuted)
})
command.CommandDialog(c, &p.open, &p.state, paletteCommands, command.Options{
OnActivate: func(id string) {
p.last, p.open = id, false
p.state.Query = ""
},
}, components.DialogOptions{})
}command.CommandDialog hosts a search field and grouped results in a modal panel. The arrow keys move through results while focus stays in search, and Enter or a click activates a command through OnActivate. The host registers the opening shortcut and decides what each command does.
command.CommandView is the same view without the dialog, for a panel window such as a launcher. Filtering is local by default; Filter replaces it, and PreFiltered with Loading and Error support asynchronous search the host runs.
API
CommandDialog
func CommandDialog(c *ui.Context, open *bool, s *State, items []Item, options Options, dialog components.DialogOptions) ui.ElementCommandDialog hosts CommandView in Onevra's modal surface. Call every frame with retained open and state. The host owns opening shortcuts, query resets and closing on activation. Dialog dismissal and focus restoration follow components.Dialog. Its returned panel may be resized in this frame. With ExpandOnSearch, its height fits the input until a query is committed.
pkg.go.devCommandView
func CommandView(c *ui.Context, s *State, items []Item, options Options) ui.ElementCommandView builds search and grouped virtual results in a bounded parent, independently of any dialog. Expanded results fill that parent; collapsed search uses only the input's height. The host supplies the available size. Up/Down keep focus in the search field and skip disabled commands, stopping at either end. MyGo owns text editing and IME handling. Tab follows normal focus navigation. Stable keyed parents distinguish independently built views. The view paints no background, so the host's surface or window material shows through.
pkg.go.devOptions
Item
Item describes a command. IDs must be nonempty and unique, independent of localized titles. Shortcut is display text; the host registers actual keys. Items in the same Group are displayed together in first-appearance order.
Prop
Type
State
State is retained by the host for one command view; do not copy it after use. Query and ActiveID may be updated by the host. When the query or results change, the first available command becomes active and the results scroll to the top, unless the host also set ActiveID. The zero value is ready to use. Closing a dialog preserves the query; hosts choose when to reset it.
Prop
Type
Methods
func (s *State) Focus()Focus moves keyboard focus to the search field when the view is next built, for example after the host shows a hidden window. CommandDialog focuses search on opening without it.
func (s *State) ReleaseInputSource() errorReleaseInputSource ends this view's input-source session. CommandDialog calls it when closed. Embedded hosts must call it when removing the view; all hosts should also call it before destroying the owning window. Call on the UI thread. It is safe to call repeatedly. A manual input-source change or a different active native context is never overwritten during restoration.
Filter
func Filter(items []Item, query string) []ItemFilter returns matching items without modifying the input. All whitespace- separated query terms must match the title or keywords, case-insensitively. Exact titles rank before title prefixes, title substrings, then keywords; ties retain input order. An empty query returns a copy in original order.
pkg.go.dev