widget

package
v0.0.6 Latest Latest
Warning

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

Go to latest
Published: May 16, 2021 License: MIT, Unlicense Imports: 29 Imported by: 0

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

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
}

func (*Bool) Changed

func (b *Bool) Changed() bool

Changed reports whether Value has changed since the last call to Changed.

func (*Bool) History

func (b *Bool) History() []Press

func (*Bool) Hovered

func (b *Bool) Hovered() bool

Hovered returns whether pointer is over the element.

func (*Bool) Layout

func (b *Bool) Layout(gtx layout.Context) layout.Dimensions

func (*Bool) Pressed

func (b *Bool) Pressed() bool

Pressed returns whether pointer is pressing the element.

type Border

type Border struct {
	Color        color.NRGBA
	CornerRadius unit.Value
	Width        unit.Value
}

Border lays out a widget and draws a border inside it.

func (Border) Layout

func (b Border) Layout(gtx layout.Context, w layout.Widget) layout.Dimensions

type ChangeEvent

type ChangeEvent struct{}

A ChangeEvent is generated for every user change to the text.

type Click

type Click struct {
	Modifiers key.Modifiers
	NumClicks int
}

Click represents a click.

type Clickable

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

Clickable represents a clickable area.

Example (Passthrough)
package main

import (
	"fmt"
	"image"

	"github.com/cybriq/gio/f32"
	"github.com/cybriq/gio/io/pointer"
	"github.com/cybriq/gio/io/router"
	"github.com/cybriq/gio/layout"
	"github.com/cybriq/gio/op"
	"github.com/cybriq/gio/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() {
		// button2 completely covers button1, but PassOp allows pointer
		// events to pass through to button1.
		button1.Layout(gtx)
		// PassOp is applied to the area defined by button1.
		pointer.PassOp{Pass: true}.Add(gtx.Ops)
		button2.Layout(gtx)
	}

	// 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) Click

func (b *Clickable) Click()

Click executes a simple programmatic click

func (*Clickable) Clicked

func (b *Clickable) Clicked() bool

Clicked reports whether there are pending clicks as would be reported by Clicks. If so, Clicked removes the earliest click.

func (*Clickable) Clicks

func (b *Clickable) Clicks() []Click

Clicks returns and clear the clicks since the last call to Clicks.

func (*Clickable) History

func (b *Clickable) History() []Press

History is the past pointer presses useful for drawing markers. History is retained for a short duration (about a second).

func (*Clickable) Hovered

func (b *Clickable) Hovered() bool

Hovered returns whether pointer is over the element.

func (*Clickable) Layout

func (b *Clickable) Layout(gtx layout.Context) layout.Dimensions

Layout and update the button state

func (*Clickable) Pressed

func (b *Clickable) Pressed() bool

Pressed returns whether pointer is pressing the element.

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
	// contains filtered or unexported fields
}

Editor implements an editable and scrollable text area.

func (*Editor) CaretCoords

func (e *Editor) CaretCoords() f32.Point

CaretCoords returns the coordinates of the caret, relative to the editor itself.

func (*Editor) CaretPos

func (e *Editor) CaretPos() (line, col int)

CaretPos returns the line & column numbers of the caret.

func (*Editor) ClearSelection

func (e *Editor) ClearSelection()

ClearSelection clears the selection, by setting the selection end equal to the selection start.

func (*Editor) Delete

func (e *Editor) Delete(runes int)

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) Focus

func (e *Editor) Focus()

Focus requests the input focus for the Editor.

func (*Editor) Focused

func (e *Editor) Focused() bool

Focused returns whether the editor is focused or not.

func (*Editor) Insert

func (e *Editor) Insert(s string)

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.Value) layout.Dimensions

Layout lays out the editor.

func (*Editor) Len

func (e *Editor) Len() int

Len is the length of the editor contents.

func (*Editor) MoveCaret

func (e *Editor) MoveCaret(startDelta, endDelta int)

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) NumLines

func (e *Editor) NumLines() int

NumLines returns the number of lines in the editor.

func (*Editor) PaintCaret

func (e *Editor) PaintCaret(gtx layout.Context)

func (*Editor) PaintSelection

func (e *Editor) PaintSelection(gtx layout.Context)

PaintSelection paints the contrasting background for selected text.

func (*Editor) PaintText

func (e *Editor) PaintText(gtx layout.Context)

func (*Editor) SelectedText

func (e *Editor) SelectedText() string

SelectedText returns the currently selected text (if any) from the editor.

func (*Editor) Selection

func (e *Editor) Selection() (start, end int)

Selection returns the start and end of the selection, as offsets into the editor text. start can be > end.

func (*Editor) SelectionLen

func (e *Editor) SelectionLen() int

SelectionLen returns the length of the selection, in bytes; it is equivalent to len(e.SelectedText()).

func (*Editor) SetCaret

func (e *Editor) SetCaret(start, end int)

SetCaret moves the caret to start, and sets the selection end to end. start and end are in bytes, and represent offsets into the editor text. start and end must be at a rune boundary.

func (*Editor) SetText

func (e *Editor) SetText(s string)

SetText replaces the contents of the editor, clearing any selection first.

func (*Editor) Text

func (e *Editor) Text() string

Text returns the contents of the editor.

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

func (e *Enum) Changed() bool

Changed reports whether Value has changed by user interaction since the last call to Changed.

func (*Enum) Hovered

func (e *Enum) Hovered() (string, bool)

Hovered returns the key that is highlighted, or false if none are.

func (*Enum) Layout

func (e *Enum) Layout(gtx layout.Context, key string) layout.Dimensions

Layout adds the event handler for key.

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

type Float struct {
	Value float32
	Axis  layout.Axis
	// contains filtered or unexported fields
}

Float is for selecting a value in a range.

func (*Float) Changed

func (f *Float) Changed() bool

Changed reports whether the value has changed since the last call to Changed.

func (*Float) Dragging

func (f *Float) Dragging() bool

Dragging returns whether the value is being interacted with.

func (*Float) Layout

func (f *Float) Layout(gtx layout.Context, pointerMargin int, min, max float32) layout.Dimensions

Layout updates the value according to drag events along the f's main axis.

The range of f is set by the minimum constraints main axis value.

func (*Float) Pos

func (f *Float) Pos() float32

Pos reports the selected position.

type Icon

type Icon struct {
	Color color.NRGBA
	// contains filtered or unexported fields
}

func NewIcon

func NewIcon(data []byte) (*Icon, error)

NewIcon returns a new Icon from IconVG data.

func (*Icon) Layout

func (ic *Icon) Layout(gtx layout.Context, sz unit.Value) layout.Dimensions

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.

func (Image) Layout

func (im Image) Layout(gtx layout.Context) layout.Dimensions

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.

func (Label) Layout

func (l Label) Layout(gtx layout.Context, s text.Shaper, font text.Font, size unit.Value, txt string) layout.Dimensions

type Press

type Press struct {
	// Position of the press.
	Position f32.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 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.

Directories

Path Synopsis
Package material implements the Material design.
Package material implements the Material design.

Jump to

Keyboard shortcuts

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