Documentation
¶
Overview ¶
Package kit provides the small building blocks used to create custom components: color state shading, hover tracking and overlay positioning (used by elements.RegisterPortal to keep click/hover popups inside the window).
See the "Extending the library" documentation for a full recipe.
Index ¶
- Constants
- Variables
- func Alpha(c properties.Color, a float32) properties.Color
- func BlockDrag(gtx layout.Context, size image.Point)
- func BlockDragHere(gtx layout.Context)
- func ClampPos(win, size, pos f32.Point, margin float32) f32.Point
- func ClickablePass(c *widget.Clickable, update func(gtx layout.Context) bool) func(gtx layout.Context) bool
- func DragArea(gtx layout.Context, size image.Point)
- func DrawGlyph(gtx layout.Context, g Glyph, size unit.Dp, col color.NRGBA) layout.Dimensions
- func FocusRing(gtx layout.Context, tag any, size image.Point, radius int)
- func FocusVisible(gtx layout.Context, tag any) bool
- func HandleInput(pass func(gtx layout.Context) bool) bool
- func Hovered(c properties.Color) properties.Color
- func LastFocused() any
- func LayoutClickable(gtx layout.Context, click *widget.Clickable, w layout.Widget) layout.Dimensions
- func MarkLive()
- func MirrorRTL(gtx layout.Context, width int) (pop func())
- func MotionStep(step float32) float32
- func NextFrame()
- func NoteFocusable(tag any)
- func NotePointerPress()
- func NoteScrolling()
- func OpaqueInput()
- func PaintFocusRing(gtx layout.Context, size image.Point, radius int)
- func Pressed(c properties.Color) properties.Color
- func PushFocusRecord(dst *[]any) (pop func())
- func PushFocusScope(s *FocusScope) (pop func())
- func PushInputScope(s *InputScope) (pop func())
- func PushLiveScope(flag *bool) (pop func())
- func RTL(gtx layout.Context) bool
- func ReducedMotion() bool
- func RequestFrame(gtx layout.Context)
- func RequestFrameAt(gtx layout.Context, t time.Time)
- func RunPasses(gtx layout.Context, passes []func(gtx layout.Context) bool) bool
- func Scale(c properties.Color, f float32) properties.Color
- func Scrolling() bool
- func SetReducedMotion(on bool)
- func TextSelectable() bool
- func Unselectable(w layout.Widget) layout.Widget
- func WriteClipboard(gtx layout.Context, text string)
- type FocusScope
- type Glyph
- type HoverTracker
- type InputScope
- type ScrollGesture
- type Scrollbar
Constants ¶
const Margin = 8
Margin is the default distance kept between an overlay and the window edges.
const PopupBorder = "theme.border"
PopupBorder and PopupShadow outline floating panels - menus, select and autocomplete lists - so they stand out over panels of their own color.
Variables ¶
var PopupShadow = properties.BoxShadow{Y: 4, Blur: 16, Color: "#00000059"}
var Screen func() (width int, breakpoint string)
Screen reports the window's width in pixels and the name of the breakpoint the innermost Responsive matched, for scrollbars hidden on some screens. The elements package sets it.
var SetAutofill func(kind int)
SetAutofill tells the platform what the focused text field holds, for password managers and AutoFill (see input.Autofill's kinds): giode sets it with the giode CLI's Gio patches. Text fields call it while focused.
var SetReturnKeyDone func(done bool)
SetReturnKeyDone makes the software keyboard's return key read Done (on false, its default), where the platform and Gio allow it: giode sets it with the giode CLI's Gio patches. Text fields call it while focused.
Functions ¶
func Alpha ¶
func Alpha(c properties.Color, a float32) properties.Color
Alpha returns c with its alpha multiplied by a.
func BlockDrag ¶
BlockDrag is BlockDragHere for a widget that doesn't already have a clip scope open at the point its own area needs to be marked (most widget.Clickable-based components - Button, Checkbox, Tabs, Menu rows, Dropdown's field and options, ...): it pushes its own clip.Rect matching size, at the current paint position, marks it, then pops. Call right after laying the widget out, passing its own measured size.
func BlockDragHere ¶
BlockDragHere marks the current clip area as not draggable, so a press inside it is delivered as a normal click instead of being hijacked into a native window-move by an ancestor's whole-window system.ActionInputOp(system.ActionMove) registration (see the root package's frameless-drag support). Call it from inside a clip scope that's already open for the widget's own pointer/gesture registration (Slider, Scroll's viewport and thumb, ...).
func ClampPos ¶
ClampPos adjusts the position of an overlay of the given size so it stays inside a window of the given size. Overlays that overflow the bottom edge flip above the anchor point, like a context menu.
func ClickablePass ¶
func ClickablePass(c *widget.Clickable, update func(gtx layout.Context) bool) func(gtx layout.Context) bool
ClickablePass returns an input pass for a widget built on a widget.Clickable. update must process the clickable's clicks exactly as the widget's Layout does (calling its handler) and report whether it handled one; the pass reports that, or a change in hover or press state.
func DragArea ¶
DragArea marks a size-d area at the current paint position as a window drag handle: pressing it and moving drags a frameless window, like an OS title bar. On Windows the area also behaves as the system caption, so double-clicking it maximizes and dragging it to a screen edge snaps the window. Register it before laying out the handle's contents: widgets inside carve themselves out with BlockDrag/BlockDragHere, which only wins when registered later.
func DrawGlyph ¶
DrawGlyph paints g in col at size dp, with round caps and joins, and returns its dimensions.
func FocusRing ¶
FocusRing draws the focus ring around a size control at the current position, rounded by radius pixels, when FocusVisible.
func FocusVisible ¶
FocusVisible reports whether tag's focus should show: it's focused, and the focus came from the keyboard.
func HandleInput ¶
HandleInput registers pass as the input pass of the widget being laid out; see the package comment. pass must process the same events the widget's Layout processes (asking for them with the same filters), and return true when the widget must be laid out again to show a change. It reports whether a cache will run pass: when it does, the widget doesn't need MarkLive just to keep receiving input.
func Hovered ¶
func Hovered(c properties.Color) properties.Color
Hovered returns the hover highlight of c: slightly lighter.
func LastFocused ¶
func LastFocused() any
LastFocused returns the tag last seen with keyboard focus.
func LayoutClickable ¶
func LayoutClickable(gtx layout.Context, click *widget.Clickable, w layout.Widget) layout.Dimensions
LayoutClickable lays w out with click's input area under it, like click.Layout(gtx, w) but without clipping what w paints to its size: a box shadow, which falls outside the box, stays whole instead of showing only where it's under the box's rounded corners. w's own interactive children stay on top of the click area.
func MarkLive ¶
func MarkLive()
MarkLive reports that the widget being laid out changed, or will change without further input, so cached output enclosing it must not be reused on the next frame.
func MirrorRTL ¶
MirrorRTL flips what's drawn next, and the pointer positions it receives, horizontally across an area width pixels wide when gtx reads right to left, until the returned function runs. Drawing without text - a slider's track, a progress bar, a switch's knob - then runs from the right with no other change. Text drawn under it would read backwards.
func MotionStep ¶
MotionStep is an animation's step toward its target - a fraction of the whole - or the whole of it when motion is reduced.
func NextFrame ¶
func NextFrame()
NextFrame advances the frame counter. The app calls it once per frame (elements.BeginFrame does).
func NoteFocusable ¶
func NoteFocusable(tag any)
NoteFocusable reports a focusable control (its focus tag) to the enclosing focus scopes, and to caches recording it.
func NotePointerPress ¶
func NotePointerPress()
NotePointerPress records a pointer press anywhere in the window: focus that follows it came from the pointer. The app calls it.
func NoteScrolling ¶
func NoteScrolling()
NoteScrolling records that a scroll gesture is moving content this frame: a touch drag past the slop, or a fling. Scrolling components call it.
func OpaqueInput ¶
func OpaqueInput()
OpaqueInput reports that the widget being laid out handles input without an input pass, so enclosing caches must lay it out whenever the pointer is active over them.
func PaintFocusRing ¶
PaintFocusRing draws a focus ring around a size area at the current position: 2 dp in the theme's primary color, just outside it.
func Pressed ¶
func Pressed(c properties.Color) properties.Color
Pressed returns the pressed shading of c: slightly darker.
func PushFocusRecord ¶
func PushFocusRecord(dst *[]any) (pop func())
PushFocusRecord collects into dst the focusable controls reported until pop. A cache (elements.Memo) records them so it can report them again when it replays the subtree without laying it out.
func PushFocusScope ¶
func PushFocusScope(s *FocusScope) (pop func())
PushFocusScope collects the focusable controls laid out until pop into s.
func PushInputScope ¶
func PushInputScope(s *InputScope) (pop func())
PushInputScope collects the passes registered until pop is called into s. Scopes nest; a pass goes to the innermost one. Elements that cache output (elements.Memo) use it; components don't need to.
func PushLiveScope ¶
func PushLiveScope(flag *bool) (pop func())
PushLiveScope opens a change-tracking scope: until the returned pop function runs, MarkLive sets *flag. Scopes nest; MarkLive marks every open scope, since a change inside a nested memo is also a change inside each memo enclosing it. Elements that cache output (elements.Memo) use it; ordinary components don't need to.
func ReducedMotion ¶
func ReducedMotion() bool
ReducedMotion reports whether animations should finish at once.
func RequestFrame ¶
RequestFrame asks for another frame as soon as possible and marks the enclosing caches live. Call it from an animating widget, or after a handler changed state the current frame already read.
func RequestFrameAt ¶
RequestFrameAt asks for a frame at t and marks the enclosing caches live. Use it for timed changes (a tooltip delay, a caret blink) instead of redrawing every frame until then.
func RunPasses ¶
RunPasses runs passes, without marking enclosing caches live (the caller decides from the results), and reports whether any changed. Every pass runs, so each widget keeps receiving its events.
func Scale ¶
func Scale(c properties.Color, f float32) properties.Color
Scale multiplies each channel of c by f, clamping to the valid range.
func Scrolling ¶
func Scrolling() bool
Scrolling reports whether a scroll gesture moved content this frame or the last - the last, since a component handling touches may run before the list it's in.
func SetReducedMotion ¶
func SetReducedMotion(on bool)
SetReducedMotion turns reduced motion on or off.
func TextSelectable ¶
func TextSelectable() bool
TextSelectable reports whether text being laid out may join a SelectionArea's selection: it's not inside an Unselectable widget.
func Unselectable ¶
Unselectable wraps w so text laid out in it isn't part of an enclosing SelectionArea's selection: a button's label, a menu row. Interactive components wrap their content with it.
func WriteClipboard ¶
WriteClipboard copies text to the system clipboard from inside a Layout. Outside layout, use App.SetClipboard.
Types ¶
type FocusScope ¶
type FocusScope struct {
// contains filtered or unexported fields
}
FocusScope collects the focusable controls laid out inside it, in order. Focusable components report themselves (NoteFocusable; FocusVisible does it).
func (*FocusScope) First ¶
func (s *FocusScope) First() any
First returns the focus tag of the first control laid out in s, or nil when it has none.
func (*FocusScope) Release ¶
func (s *FocusScope) Release(gtx layout.Context)
Release gives focus back to what had it before the modal opened: call it when the modal closes.
func (*FocusScope) Trap ¶
func (s *FocusScope) Trap(gtx layout.Context)
Trap keeps the focus inside s while it's the topmost trap: call it after laying out the scope's content, every frame the modal is open. The first time, it moves focus to the first control inside. When focus leaves - Tab past the last control, Shift+Tab before the first, or a click outside - it goes back to the first or the last.
type Glyph ¶
type Glyph int
Glyph is one of the few small indicator shapes components draw themselves (chevrons, a check mark) so they don't depend on any icon set. Apps bring their own icons through the icon package.
const ( GlyphChevronDown Glyph = iota GlyphChevronUp GlyphChevronLeft GlyphChevronRight GlyphCheck GlyphClose GlyphMinus GlyphPlus // GlyphMaximize is a window's maximize button: a square. GlyphMaximize // GlyphRestore is a maximized window's restore button: two // overlapping squares. GlyphRestore // GlyphCopy is a copy-to-clipboard indicator: two stacked pages. GlyphCopy // GlyphChevronForward points in the reading direction: right, or // left in a right-to-left locale. Submenus, collapsed rows and // "next" use it. GlyphChevronForward // GlyphChevronBack points against the reading direction. GlyphChevronBack // GlyphCalendar is a date field's button: a calendar page. GlyphCalendar // GlyphClock is a time field's button: a clock face. GlyphClock )
func GlyphForward ¶
GlyphForward resolves GlyphChevronForward for gtx: the chevron pointing right, or left right to left.
type HoverTracker ¶
type HoverTracker struct {
// contains filtered or unexported fields
}
HoverTracker is the hover-tracking building block behind every stateless element's OnHover (Box, Text, Divider, Image, ...). Embed it in a custom stateless element to add the same capability: the zero value has hover tracking disabled and costs nothing until Enable is called.
func (*HoverTracker) Enable ¶
func (h *HoverTracker) Enable()
Enable turns on hover tracking. Call from the element's own OnHover.
func (*HoverTracker) Enabled ¶
func (h *HoverTracker) Enabled() bool
Enabled reports whether Enable was called.
func (*HoverTracker) Hovered ¶
func (h *HoverTracker) Hovered() bool
Hovered reports whether the pointer was over the element on the last frame.
func (*HoverTracker) RegisterArea ¶
func (h *HoverTracker) RegisterArea(gtx layout.Context, off, size image.Point, cur properties.Cursor)
RegisterArea registers a pass-through hover area of size, offset by off, for the region to be queried on the next frame, and applies cur if currently hovered. A pass-through region sees pointer events without blocking children painted above it. Call after painting. A no-op when Enable hasn't been called.
func (*HoverTracker) Update ¶
func (h *HoverTracker) Update(gtx layout.Context) bool
Update processes pending hover events, queued from the area RegisterArea added on a previous frame, and caches the result for Hovered. Call before deciding which styles to lay out and paint with - it reflects last frame's pointer position, not this one. A no-op, returning false, when Enable hasn't been called.
type InputScope ¶
type InputScope struct {
// Passes are the registered input passes, in layout order.
Passes []func(gtx layout.Context) bool
// Opaque is set when the subtree has input handling without a pass.
Opaque bool
}
InputScope collects the input passes of a subtree being laid out.
type ScrollGesture ¶
type ScrollGesture struct {
// contains filtered or unexported fields
}
ScrollGesture tells a scroll gesture moving content from a finger merely resting on it: Gio's gesture.Scroll reports dragging from the moment a finger touches down. Scrolling components embed one and call Track after each Update.
func (*ScrollGesture) Moving ¶
func (g *ScrollGesture) Moving() bool
Moving reports whether a touch drag or fling is moving content.
func (*ScrollGesture) Track ¶
func (g *ScrollGesture) Track(s *gesture.Scroll, dist int) (started, ended bool)
Track updates the state from the gesture and the distance it just returned. started is set when a touch drag starts moving content, ended when a drag or fling that moved content is over. While content moves it calls NoteScrolling.
type Scrollbar ¶
type Scrollbar struct {
// contains filtered or unexported fields
}
Scrollbar is the vertical scrollbar shared by scrolling components: a track and a draggable thumb along the right edge of a viewport. Embed one per scrolling component and lay it out after the content.
func (*Scrollbar) Layout ¶
func (s *Scrollbar) Layout(gtx layout.Context, viewport image.Point, contentH int, offset *float32, sb properties.ScrollBar)
Layout draws the scrollbar over a viewport of the given size showing contentH pixels of content scrolled by *offset pixels, and applies thumb drags to *offset (clamped to the content). sb customizes it in dp; zero fields use the defaults (8dp wide, 20dp minimum thumb). It draws nothing when the content fits, or when sb hides it on this screen (Hidden, HiddenMaxWidth, HiddenOn).