Documentation ¶
Overview ¶
Package widget implements state tracking and event handling of common user interface controls. To draw widgets, use a theme packages such as package gioui.org/widget/material.
Index ¶
- type Bool
- type Border
- type ChangeEvent
- type Click
- type Clickable
- func (b *Clickable) Click()
- func (b *Clickable) Clicked() bool
- func (b *Clickable) Clicks() []Click
- func (b *Clickable) Focused() bool
- func (b *Clickable) History() []Press
- func (b *Clickable) Hovered() bool
- func (b *Clickable) Layout(gtx layout.Context, w layout.Widget) layout.Dimensions
- func (b *Clickable) Pressed() bool
- type Decorations
- type Draggable
- type Editor
- func (e *Editor) CaretCoords() f32.Point
- func (e *Editor) CaretPos() (line, col int)
- func (e *Editor) ClearSelection()
- func (e *Editor) Delete(runes int)
- func (e *Editor) Events() []EditorEvent
- func (e *Editor) Focus()
- func (e *Editor) Focused() bool
- func (e *Editor) Insert(s string)
- func (e *Editor) Layout(gtx layout.Context, sh text.Shaper, font text.Font, size unit.Sp, ...) layout.Dimensions
- func (e *Editor) Len() int
- func (e *Editor) MoveCaret(startDelta, endDelta int)
- func (e *Editor) NumLines() int
- func (e *Editor) PaintCaret(gtx layout.Context)
- func (e *Editor) PaintSelection(gtx layout.Context)
- func (e *Editor) PaintText(gtx layout.Context)
- func (e *Editor) Read(p []byte) (int, error)
- func (e *Editor) Seek(offset int64, whence int) (int64, error)
- func (e *Editor) SelectedText() string
- func (e *Editor) Selection() (start, end int)
- func (e *Editor) SelectionLen() int
- func (e *Editor) SetCaret(start, end int)
- func (e *Editor) SetText(s string)
- func (e *Editor) Text() string
- func (e *Editor) WriteTo(w io.Writer) (int64, error)
- type EditorEvent
- type Enum
- type Fit
- type Float
- type Icon
- type Image
- type Label
- type List
- type Press
- type Scrollbar
- func (s *Scrollbar) AddDrag(ops *op.Ops)
- func (s *Scrollbar) AddIndicator(ops *op.Ops)
- func (s *Scrollbar) AddTrack(ops *op.Ops)
- func (s *Scrollbar) Dragging() bool
- func (s *Scrollbar) IndicatorHovered() bool
- func (s *Scrollbar) Layout(gtx layout.Context, axis layout.Axis, viewportStart, viewportEnd float32) layout.Dimensions
- func (s *Scrollbar) ScrollDistance() float32
- func (s *Scrollbar) TrackHovered() bool
- type SelectEvent
- type SubmitEvent
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Bool ¶
type Bool struct { Value bool // contains filtered or unexported fields }
type ChangeEvent ¶
type ChangeEvent struct{}
A ChangeEvent is generated for every user change to the text.
type Clickable ¶
type Clickable struct {
// contains filtered or unexported fields
}
Clickable represents a clickable area.
Example (Passthrough) ¶
package main import ( "fmt" "image" "github.com/xiaoshengduan/gio-fly/f32" "github.com/xiaoshengduan/gio-fly/io/pointer" "github.com/xiaoshengduan/gio-fly/io/router" "github.com/xiaoshengduan/gio-fly/layout" "github.com/xiaoshengduan/gio-fly/op" "github.com/xiaoshengduan/gio-fly/widget" ) func main() { // When laying out clickable widgets on top of each other, // pointer events can be passed down for the underlying // widgets to pick them up. var button1, button2 widget.Clickable var r router.Router gtx := layout.Context{ Ops: new(op.Ops), Constraints: layout.Exact(image.Pt(100, 100)), Queue: &r, } // widget lays out two buttons on top of each other. widget := func() { content := func(gtx layout.Context) layout.Dimensions { return layout.Dimensions{Size: gtx.Constraints.Min} } button1.Layout(gtx, content) // button2 completely covers button1, but pass-through allows pointer // events to pass through to button1. defer pointer.PassOp{}.Push(gtx.Ops).Pop() button2.Layout(gtx, content) } // The first layout and call to Frame declare the Clickable handlers // to the input router, so the following pointer events are propagated. widget() r.Frame(gtx.Ops) // Simulate one click on the buttons by sending a Press and Release event. r.Queue( pointer.Event{ Source: pointer.Mouse, Buttons: pointer.ButtonPrimary, Type: pointer.Press, Position: f32.Pt(50, 50), }, pointer.Event{ Source: pointer.Mouse, Buttons: pointer.ButtonPrimary, Type: pointer.Release, Position: f32.Pt(50, 50), }, ) // The second layout ensures that the click event is registered by the buttons. widget() if button1.Clicked() { fmt.Println("button1 clicked!") } if button2.Clicked() { fmt.Println("button2 clicked!") } }
Output: button1 clicked! button2 clicked!
func (*Clickable) Clicked ¶
Clicked reports whether there are pending clicks as would be reported by Clicks. If so, Clicked removes the earliest click.
func (*Clickable) History ¶
History is the past pointer presses useful for drawing markers. History is retained for a short duration (about a second).
type Decorations ¶
type Decorations struct {
// contains filtered or unexported fields
}
Decorations handles the states of window decorations.
func (*Decorations) Actions ¶
func (d *Decorations) Actions() system.Action
Actions returns the set of actions activated by the user.
func (*Decorations) Clickable ¶
func (d *Decorations) Clickable(action system.Action) *Clickable
Clickable returns the clickable for the given single action.
func (*Decorations) LayoutMove ¶
func (d *Decorations) LayoutMove(gtx layout.Context, w layout.Widget) layout.Dimensions
LayoutMove lays out the widget that makes a window movable.
func (*Decorations) Maximized ¶
func (d *Decorations) Maximized() bool
Maximized returns whether the window is maximized.
func (*Decorations) Perform ¶
func (d *Decorations) Perform(actions system.Action)
Perform updates the decorations as if the specified actions were performed by the user.
type Draggable ¶
type Draggable struct { // Type contains the MIME type and matches transfer.SourceOp. Type string // contains filtered or unexported fields }
Draggable makes a widget draggable.
func (*Draggable) Layout ¶
Example ¶
package main import ( "fmt" "image" "github.com/xiaoshengduan/gio-fly/f32" "github.com/xiaoshengduan/gio-fly/io/pointer" "github.com/xiaoshengduan/gio-fly/io/router" "github.com/xiaoshengduan/gio-fly/io/transfer" "github.com/xiaoshengduan/gio-fly/layout" "github.com/xiaoshengduan/gio-fly/op" "github.com/xiaoshengduan/gio-fly/op/clip" "github.com/xiaoshengduan/gio-fly/widget" ) func main() { var r router.Router gtx := layout.Context{ Ops: new(op.Ops), Constraints: layout.Exact(image.Pt(100, 100)), Queue: &r, } // mime is the type used to match drag and drop operations. // It could be left empty in this example. mime := "MyMime" drag := &widget.Draggable{Type: mime} var drop int // widget lays out the drag and drop handlers and processes // the transfer events. widget := func() { // Setup the draggable widget. w := func(gtx layout.Context) layout.Dimensions { sz := image.Pt(10, 10) // drag area return layout.Dimensions{Size: sz} } drag.Layout(gtx, w, w) // drag must respond with an Offer event when requested. // Use the drag method for this. if m, ok := drag.Requested(); ok { drag.Offer(gtx.Ops, m, offer{Data: "hello world"}) } // Setup the area for drops. ds := clip.Rect{ Min: image.Pt(20, 20), Max: image.Pt(40, 40), }.Push(gtx.Ops) transfer.TargetOp{ Tag: &drop, Type: mime, // this must match the drag Type for the drop to succeed }.Add(gtx.Ops) ds.Pop() // Check for the received data. for _, ev := range gtx.Events(&drop) { switch e := ev.(type) { case transfer.DataEvent: data := e.Open() fmt.Println(data.(offer).Data) } } } // Register and lay out the widget. widget() r.Frame(gtx.Ops) // Send drag and drop gesture events. r.Queue( pointer.Event{ Type: pointer.Press, Position: f32.Pt(5, 5), // in the drag area }, pointer.Event{ Type: pointer.Move, Position: f32.Pt(5, 5), // in the drop area }, pointer.Event{ Type: pointer.Release, Position: f32.Pt(30, 30), // in the drop area }, ) // Let the widget process the events. widget() r.Frame(gtx.Ops) // Process the transfer.DataEvent. widget() } type offer struct { Data string } func (offer) Read([]byte) (int, error) { return 0, nil } func (offer) Close() error { return nil }
Output: hello world
func (*Draggable) Offer ¶
Offer the data ready for a drop. Must be called after being Requested. The mime must be one in the requested list.
type Editor ¶
type Editor struct { Alignment text.Alignment // SingleLine force the text to stay on a single line. // SingleLine also sets the scrolling direction to // horizontal. SingleLine bool // Submit enabled translation of carriage return keys to SubmitEvents. // If not enabled, carriage returns are inserted as newlines in the text. Submit bool // Mask replaces the visual display of each rune in the contents with the given rune. // Newline characters are not masked. When non-zero, the unmasked contents // are accessed by Len, Text, and SetText. Mask rune // InputHint specifies the type of on-screen keyboard to be displayed. InputHint key.InputHint // MaxLen limits the editor content to a maximum length. Zero means no limit. MaxLen int // Filter is the list of characters allowed in the Editor. If Filter is empty, // all characters are allowed. Filter string // contains filtered or unexported fields }
Editor implements an editable and scrollable text area.
func (*Editor) CaretCoords ¶
CaretCoords returns the coordinates of the caret, relative to the editor itself.
func (*Editor) ClearSelection ¶
func (e *Editor) ClearSelection()
ClearSelection clears the selection, by setting the selection end equal to the selection start.
func (*Editor) Delete ¶
Delete runes from the caret position. The sign of runes specifies the direction to delete: positive is forward, negative is backward.
If there is a selection, it is deleted and counts as a single rune.
func (*Editor) Events ¶
func (e *Editor) Events() []EditorEvent
Events returns available editor events.
func (*Editor) Insert ¶
Insert inserts text at the caret, moving the caret forward. If there is a selection, Insert overwrites it.
func (*Editor) Layout ¶
func (e *Editor) Layout(gtx layout.Context, sh text.Shaper, font text.Font, size unit.Sp, content layout.Widget) layout.Dimensions
Layout lays out the editor. If content is not nil, it is laid out on top.
func (*Editor) MoveCaret ¶
MoveCaret moves the caret (aka selection start) and the selection end relative to their current positions. Positive distances moves forward, negative distances moves backward. Distances are in runes.
func (*Editor) PaintCaret ¶
func (*Editor) PaintSelection ¶
PaintSelection paints the contrasting background for selected text.
func (*Editor) SelectedText ¶
SelectedText returns the currently selected text (if any) from the editor.
func (*Editor) Selection ¶
Selection returns the start and end of the selection, as rune offsets. start can be > end.
func (*Editor) SelectionLen ¶
SelectionLen returns the length of the selection, in runes; it is equivalent to utf8.RuneCountInString(e.SelectedText()).
func (*Editor) SetCaret ¶
SetCaret moves the caret to start, and sets the selection end to end. start and end are in runes, and represent offsets into the editor text.
type EditorEvent ¶
type EditorEvent interface {
// contains filtered or unexported methods
}
type Enum ¶
type Enum struct { Value string // contains filtered or unexported fields }
func (*Enum) Changed ¶
Changed reports whether Value has changed by user interaction since the last call to Changed.
type Fit ¶
type Fit uint8
Fit scales a widget to fit and clip to the constraints.
const ( // Unscaled does not alter the scale of a widget. Unscaled Fit = iota // Contain scales widget as large as possible without cropping // and it preserves aspect-ratio. Contain // Cover scales the widget to cover the constraint area and // preserves aspect-ratio. Cover // ScaleDown scales the widget smaller without cropping, // when it exceeds the constraint area. // It preserves aspect-ratio. ScaleDown // Fill stretches the widget to the constraints and does not // preserve aspect-ratio. Fill )
type Float ¶
Float is for selecting a value in a range.
func (*Float) Changed ¶
Changed reports whether the value has changed since the last call to Changed.
type Icon ¶
type Icon struct {
// contains filtered or unexported fields
}
type Image ¶
type Image struct { // Src is the image to display. Src paint.ImageOp // Fit specifies how to scale the image to the constraints. // By default it does not do any scaling. Fit Fit // Position specifies where to position the image within // the constraints. Position layout.Direction // Scale is the ratio of image pixels to // dps. If Scale is zero Image falls back to // a scale that match a standard 72 DPI. Scale float32 }
Image is a widget that displays an image.
type Label ¶
type Label struct { // Alignment specify the text alignment. Alignment text.Alignment // MaxLines limits the number of lines. Zero means no limit. MaxLines int }
Label is a widget for laying out and drawing text.
type Press ¶
type Press struct { // Position of the press. Position image.Point // Start is when the press began. Start time.Time // End is when the press was ended by a release or cancel. // A zero End means it hasn't ended yet. End time.Time // Cancelled is true for cancelled presses. Cancelled bool }
Press represents a past pointer press.
type Scrollbar ¶
type Scrollbar struct {
// contains filtered or unexported fields
}
Scrollbar holds the persistent state for an area that can display a scrollbar. In particular, it tracks the position of a viewport along a one-dimensional region of content. The viewport's position can be adjusted by drag operations along the display area, or by clicks within the display area.
Scrollbar additionally detects when a scroll indicator region is hovered.
func (*Scrollbar) AddDrag ¶
AddDrag configures the drag listener for the scrollbar to use the current clip area.
func (*Scrollbar) AddIndicator ¶
AddIndicator configures the indicator click listener for the scrollbar to use the current clip area.
func (*Scrollbar) AddTrack ¶
AddTrack configures the track click listener for the scrollbar to use the current clip area.
func (*Scrollbar) Dragging ¶
Dragging reports whether the user is currently performing a drag gesture on the indicator. Note that this can return false while ScrollDistance is nonzero if the user scrolls using a different control than the scrollbar (like a mouse wheel).
func (*Scrollbar) IndicatorHovered ¶
IndicatorHovered reports whether the scroll indicator is currently being hovered by the pointer.
func (*Scrollbar) Layout ¶
func (s *Scrollbar) Layout(gtx layout.Context, axis layout.Axis, viewportStart, viewportEnd float32) layout.Dimensions
Layout updates the internal state of the scrollbar based on events since the previous call to Layout. The provided axis will be used to normalize input event coordinates and constraints into an axis- independent format. viewportStart is the position of the beginning of the scrollable viewport relative to the underlying content expressed as a value in the range [0,1]. viewportEnd is the position of the end of the viewport relative to the underlying content, also expressed as a value in the range [0,1]. For example, if viewportStart is 0.25 and viewportEnd is .5, the viewport described by the scrollbar is currently showing the second quarter of the underlying content.
func (*Scrollbar) ScrollDistance ¶
ScrollDistance returns the normalized distance that the scrollbar moved during the last call to Layout as a value in the range [-1,1].
func (*Scrollbar) TrackHovered ¶
TrackHovered reports whether the scroll track is being hovered by the pointer.
type SelectEvent ¶
type SelectEvent struct{}
A SelectEvent is generated when the user selects some text, or changes the selection (e.g. with a shift-click), including if they remove the selection. The selected text is not part of the event, on the theory that it could be a relatively expensive operation (for a large editor), most applications won't actually care about it, and those that do can call Editor.SelectedText() (which can be empty).
type SubmitEvent ¶
type SubmitEvent struct {
Text string
}
A SubmitEvent is generated when Submit is set and a carriage return key is pressed.