Skip to content

Instantly share code, notes, and snippets.

@andlabs
Created July 7, 2014 13:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save andlabs/c092c4ad285d2ce00eb6 to your computer and use it in GitHub Desktop.
Save andlabs/c092c4ad285d2ce00eb6 to your computer and use it in GitHub Desktop.

ui

-- import "github.com/andlabs/ui/redo"

Usage

var Do = make(Doer)

Do is the main way to issue requests to package ui. Requests returned by the various functions and methods of package ui should be sent across Do to have them performed. When an event is dispatched to an event handler, that event handler will receive a new Doer which is active for the life of the event handler, and any requests made to Do will block until the event handler returns.

func Bool

func Bool(c Doer, r *Request) bool

Bool is a convenience function that performs a Request that returns a bool, waits for that request to be processed, and returns the result. For example:

if ui.Bool(ui.Do, checkbox.Checked()) { /* do stuff */ }

func Int

func Int(c Doer, r *Request) int

Int is like Bool, but for int.

func IntSlice

func IntSlice(c Doer, r *Request) []int

IntSlice is like Bool, but for []int.

func String

func String(c Doer, r *Request) string

String is like Bool, but for string.

func StringSlice

func StringSlice(c Doer, r *Request) []string

StringSlice is like Bool, but for []string.

func Wait

func Wait(c Doer, r *Request)

Wait is a convenience function that performs a Request and waits for that request to be processed. If the request returns a value, it is discarded. You should generally use Wait on functions that do not return a value. See the documentation of Bool for an example.

type Button

type Button interface {
	Control

	// OnClicked sets the event handler for when the Button is clicked.
	OnClicked(func(d Doer))

	// Text and SetText are Requests that get and set the Button's label text.
	Text() *Request
	SetText(text string) *Request
}

Button is a clickable button that performs some task.

func NewButton

func NewButton(text string) Button

NewButton creates a new Button with the given label text.

type Checkbox

type Checkbox interface {
	Control

	// OnClicked sets the event handler for when the Checkbox is clicked (to change its toggle state).
	// TODO change to OnCheckChanged or OnToggled?
	OnClicked(func(d Doer))

	// Text and SetText are Requests that get and set the Checkbox's label text.
	Text() *Request
	SetText(text string) *Request

	// Checked and SetChecked are Requests that get and set the Checkbox's check state.
	Checked() *Request
	SetChecked(checked bool) *Request
}

Checkbox is a clickable box that indicates some Boolean value.

func NewCheckbox

func NewCheckbox(text string) Checkbox

NewCheckbox creates a new Checkbox with the given label text. The Checkbox will be initially unchecked.

type Combobox

type Combobox interface {
	Control

	// Append, InsertBefore, and Delete are Requests that change the Combobox's list.
	// InsertBefore and Delete panic if the index passed in is out of range.
	Append(item string) *Request
	InsertBefore(item string, before int) *Request
	Delete(index int) *Request

	// SelectedIndex and SelectedText are Requests that return the current Combobox selection, either as the index into the list or as its label.
	// SelectedIndex returns -1 and SelectedText returns an empty string if no selection has been made.
	// If the Combobox is editable, SelectedIndex returns -1 if the user has entered their own string, in which case SelectedText will return that string.
	SelectedIndex() *Request
	SelectedText() *Request

	// SelectIndex is a Request that selects an index from the list.
	// SelectIndex panics if the given index is out of range.
	// [TODO SelectText or SetCustomText]
	SelectIndex(index int) *Request

	// Len is a Request that returns the number of items in the list.
	// At is a Request that returns a given item's text.
	// At panics if the given index is out of range.
	Len() *Request
	At(index int) *Request
}

Combobox is a drop-down list from which one item can be selected. Each item of a Combobox is a text string. The Combobox can optionally be editable, in which case the user can type in a selection not in the list. [TODO If an item is selected in an editable Combobox, the edit field will be changed ot reflect the selection.]

func NewCombobox

func NewCombobox(items ...string) Combobox

NewCombobox creates a new Combobox with the given items. The Checkbox will have nothing selected initially.

func NewEditableCombobox

func NewEditableCombobox(items ...string) Combobox

NewEditableCombobox creates a new editable Combobox with the given items. The Combobox will have nothing selected initially and no custom text initially.

type Control

type Control interface {
}

Control represents a control. All Controls have event handlers that take a single argument (the Doer active during the event) and return nothing.

type Doer

type Doer chan *Request

Doer is a channel that takes Requests returned by the various functions and methods of package ui. There are two main Doers: ui.Do, which is for outside event handlers, and the Doer passed into an event handler function. You should not create or use your own Doers; these are meaningless.

type Request

type Request struct {
}

Request represents a request issued to the package. These are returned by the various functions and methods of package ui and are sent to either Do or to the currently active event handler channel. There are also several convenience functions that perfrom common operations with requests.

func (*Request) Response

func (r *Request) Response() <-chan interface{}

Response returns a channel which is pulsed exactly once, then immeidately closed, with the response from the function that issued the request. If the function does not return a value, this value will be the zero value of struct{}. Otherwise, the type of this value depends on the function that created the Request.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment