Documentation
¶
Overview ¶
Package command provides application-owned command views and dialogs. CommandView embeds search and results in any bounded parent, such as a panel window; CommandDialog hosts the same view in Onevra's modal surface. Hosts register opening shortcuts, retain State, and execute activated IDs.
Index ¶
Examples ¶
Constants ¶
const InputSourceABC = "com.apple.keylayout.ABC"
InputSourceABC identifies Apple's ABC keyboard input source on macOS.
Variables ¶
ErrInputSourceUnavailable means there is no active native input context, the source is not enabled, or the platform does not support source selection.
Functions ¶
func CommandDialog ¶
func CommandDialog(c *ui.Context, open *bool, s *State, items []Item, options Options, dialog components.DialogOptions) ui.Element
CommandDialog 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.
Example ¶
package main
import (
"runtime"
"github.com/egoist/mygo/ui"
"github.com/oneo-me/onevra/source/components"
"github.com/oneo-me/onevra/source/components/command"
)
func main() {
var state command.State
var open bool
var destination string
inputSource := ""
if runtime.GOOS == "darwin" {
inputSource = command.InputSourceABC
}
items := []command.Item{{ID: "settings", Title: "Settings", Keywords: []string{"preferences"}}}
view := func(c *ui.Context) {
if c.Shortcut(ui.Cmd, ui.KeyF) {
open = true
}
command.CommandDialog(c, &open, &state, items, command.Options{
// Omit this to show results immediately, even with an empty query.
InputSource: inputSource,
ExpandOnSearch: true,
OnActivate: func(id string) { destination = id; open = false },
}, components.DialogOptions{})
}
// Also call state.ReleaseInputSource on the UI thread before destroying
// the host window. Normal dialog dismissal restores the source automatically.
_ = view // Supply to a MyGo window or Onevra Shell content builder.
_ = destination
}
Output:
func CommandView ¶ added in v0.2026.11
CommandView 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.
Example ¶
package main
import (
"github.com/egoist/mygo/ui"
"github.com/oneo-me/onevra/source/components/command"
)
func main() {
var state command.State
items := []command.Item{{ID: "settings", Title: "Settings"}}
view := func(c *ui.Context) {
// A bounded parent, such as a panel window's content, sizes the view.
ui.Column(c).Fill().Children(func() {
command.CommandView(c, &state, items, command.Options{
OnActivate: func(id string) { /* Run id; hide the window if needed. */ },
})
})
}
// After showing a hidden host window, call state.Focus so typing reaches
// search. Embedded hosts call state.ReleaseInputSource when removing the view.
_ = view
}
Output:
Types ¶
type Item ¶
type Item struct {
ID, Title, Description, Group, Shortcut string
Keywords []string
Icon *ui.SVG
// Leading replaces Icon with non-interactive content of the given size,
// such as an image. Trailing adds non-interactive content before Shortcut.
// Neither slot takes part in filtering.
Leading func(c *ui.Context, size float32)
Trailing func(c *ui.Context)
Disabled bool
}
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.
func Filter ¶
Filter 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.
type Options ¶
type Options struct {
// InputSource optionally selects an enabled macOS input source once when
// search receives focus, e.g. InputSourceABC. Empty keeps the current source.
// Selection waits for any marked-text composition to finish. Unsupported
// platforms and headless views report State.InputSourceError without switching.
InputSource string
// KeepInputSource skips restoration on close. By default restoration only
// occurs if the user has not manually selected a different source.
KeepInputSource bool
// ExpandOnSearch hides the results area until Query contains non-whitespace
// committed text, and collapses it again when cleared. Hidden results cannot
// be activated; loading, error and empty states are also hidden until search.
// The zero value shows the results area immediately.
ExpandOnSearch bool
// Filter replaces the package Filter for synchronous local matching. It
// receives this frame's query and must not modify items. Nil uses Filter.
Filter func(items []Item, query string) []Item
// PreFiltered uses host-supplied matches instead of applying a filter.
// For asynchronous search, the host owns cancellation and stale-result checks.
PreFiltered bool
// Loading and Error replace results and suppress activation. Error wins.
Loading bool
Error string
// Empty labels use Onevra's catalog in the window's language.
SearchLabel, Placeholder, ResultsLabel, EmptyText, LoadingText string
// OnActivate runs on the UI thread for Enter or a single click. The host
// owns execution, background work and whether to close the dialog or window.
OnActivate func(id string)
}
Options supplies application text, results and activation behavior.
type State ¶
type State struct {
// InputSourceError records the last selection or restoration failure.
// A failure never prevents searching. It is cleared on the next attempt.
InputSourceError error
Query string
ActiveID string
// contains filtered or unexported fields
}
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.
func (*State) Focus ¶ added in v0.2026.11
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 (*State) ReleaseInputSource ¶
ReleaseInputSource 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.