base

package
v0.1.9 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 9, 2026 License: MIT Imports: 5 Imported by: 0

README

ui/base

English | 简体中文

Component behavior, without appearance: keyboard navigation of lists, jump by first letter (typeahead), multi-selection with shift range, open/closed state. Doesn't draw anything, just normal Go state and functions.

  • Dependencies: Only relies on Gio's key name (gioui.org/io/key) and does not rely on any Keel module.
  • Used by it: kit's List, Tree, Menu, Select, Command, Sidebar, and Table use it to handle keyboard and selection; applications can also use it with el to write components with completely customized appearance.
File Responsibility
list.go List: ↑ ↓ Home End PageUp PageDown, skip disabled items, loopable
typeahead.go Typeahead: Enter letters continuously to jump to the item starting with it, and loop when the same letter is pressed repeatedly
selection.go Selection[K]: Multi-selection saved by key, click, Cmd/Ctrl switching, Shift range
disclosure.go Disclosure: Disableable on/off state, distinguishing user operations and program settings

Documentation: Unstyled Base Layer

Documentation

Overview

Package base holds the behavior of Keel's components without their look: keyboard navigation over a list, type-to-find, multi-selection with ranges, and open/closed state. It draws nothing and depends on no other Keel package. kit builds its components from it; build your own with base for the behavior and el for the look.

Index

Constants

View Source
const TypeaheadPause = time.Second

TypeaheadPause is how long a pause in typing starts a new search.

Variables

This section is empty.

Functions

func Text

func Text(name string, shortcut bool) (string, bool)

Text is the key text a typeahead takes from a key name: a single letter, digit or symbol pressed without Ctrl, Cmd or Alt. Gio names letter keys in upper case; the search ignores case.

Types

type Disclosure

type Disclosure struct {
	OnChange func(open bool)
	// contains filtered or unexported fields
}

Disclosure is open/closed state that can be disabled, for popovers, menus, collapsibles and dialogs. OnChange runs only for changes the user makes through Change, not for Set.

func (*Disclosure) Change

func (d *Disclosure) Change(open bool) bool

Change opens or closes as the user asked and calls OnChange if that changed anything. It reports whether it did.

func (*Disclosure) Disabled

func (d *Disclosure) Disabled() bool

func (*Disclosure) Open

func (d *Disclosure) Open() bool

func (*Disclosure) Set

func (d *Disclosure) Set(open bool)

Set opens or closes without calling OnChange. A disabled disclosure stays closed.

func (*Disclosure) SetDisabled

func (d *Disclosure) SetDisabled(on bool)

SetDisabled disables it, which also closes it.

func (*Disclosure) Toggle

func (d *Disclosure) Toggle() bool

Toggle is Change(!Open()).

type List

type List struct {
	Count    int
	Disabled func(i int) bool
	// Wrap moves from the last item to the first and back, as menus do.
	Wrap bool
	// Page is how far PageUp and PageDown move; 10 by default.
	Page int
}

List navigates Count items, some of which may be disabled. The zero Disabled enables every item. Indexes count from 0; -1 means none.

func (List) First

func (l List) First() int

First is the first enabled item, or -1.

func (List) Key

func (l List) Key(name string, i int) (int, bool)

Key moves from i for ↑ ↓ Home End PageUp PageDown and reports whether name was one of them. From no item (-1), ↓ goes to the first and ↑ to the last. Disabled items are skipped; a page move that lands on one goes on in the same direction, then back toward i. With nowhere to go it stays at i.

func (List) Last

func (l List) Last() int

Last is the last enabled item, or -1.

func (List) Next

func (l List) Next(i, dir int) int

Next is the nearest enabled item after i in direction dir (1 or -1). It wraps when Wrap is set; otherwise, and when no item is enabled, it returns i if i is enabled and -1 if not.

type Selection

type Selection[K comparable] struct {
	// contains filtered or unexported fields
}

Selection is a set of chosen items, keyed so it survives reordering and filtering, with an anchor for Shift ranges. The zero value is empty.

func (*Selection[K]) Anchor

func (s *Selection[K]) Anchor() (K, bool)

Anchor is where a Shift range starts: the item last clicked without Shift.

func (*Selection[K]) Click

func (s *Selection[K]) Click(order []K, i int, extend, toggle bool, disabled func(int) bool)

Click applies a click on item i of order, with the platform's rules: a plain click selects only i; toggle (Cmd, or Ctrl off macOS) adds or removes i; extend (Shift) selects from the anchor to i, added to the selection when toggle is also held. Disabled items in a range are skipped. A disabled i changes nothing.

func (*Selection[K]) Has

func (s *Selection[K]) Has(k K) bool

func (*Selection[K]) In

func (s *Selection[K]) In(order []K) []K

In returns the selected keys in the order of order.

func (*Selection[K]) Indexes

func (s *Selection[K]) Indexes(order []K) []int

Indexes returns the positions in order of the selected keys.

func (*Selection[K]) Keep

func (s *Selection[K]) Keep(keep func(K) bool)

Keep drops selected keys that keep rejects, such as items that were removed or disabled.

func (*Selection[K]) Len

func (s *Selection[K]) Len() int

func (*Selection[K]) Set

func (s *Selection[K]) Set(keys ...K)

Set replaces the selection with keys and anchors at the last, as if they had been clicked in order.

type Typeahead

type Typeahead struct {
	// contains filtered or unexported fields
}

Typeahead finds an item by the first letters of its label as they are typed, like a native list: typing "re" goes to "Red", and pressing the same letter again cycles through the items that start with it.

func (*Typeahead) Find

func (t *Typeahead) Find(now time.Time, text string, current int, l List, label func(int) string) (int, bool)

Find adds text to the search at time now and returns the enabled item whose label starts with it, searching from the item after current and wrapping. It returns current and false when nothing matches.

func (*Typeahead) Reset

func (t *Typeahead) Reset()

Reset forgets what was typed.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL