Removed ui.Event(); all event channels are initialized with their objects now.

This commit is contained in:
Pietro Gagliardi 2014-03-12 21:47:39 -04:00
parent eb48ed09c3
commit 26c6b97ce1
6 changed files with 8 additions and 12 deletions

View File

@ -7,8 +7,8 @@ import (
// A Button represents a clickable button with some text.
type Button struct {
// This channel gets a message when the button is clicked.
// Unlike other channels in this package, this channel is initialized to non-nil when creating a new button, and cannot be set to nil later.
// Clicked gets a message when the button is clicked.
// You cannot change it once the Window containing the Button has been opened.
Clicked chan struct{}
lock sync.Mutex
@ -22,7 +22,7 @@ func NewButton(text string) (b *Button) {
return &Button{
sysData: mksysdata(c_button),
initText: text,
Clicked: Event(),
Clicked: newEvent(),
}
}

3
doc.go
View File

@ -9,8 +9,6 @@ Building GUIs is as simple as creating a Window, populating it with Controls, an
Once a Window is open, you cannot make layout changes.
At present, you must also hook Window.Closing with ui.Event() to monitor for attempts to close the Window.
Once your Window is open, you can begin to handle events. Handling events is simple: because all events are channels exposed as exported members of the Window and Control types, simply select on them.
Here is a simple, complete program that asks the user for their name and greets them after clicking a button.
@ -22,7 +20,6 @@ Here is a simple, complete program that asks the user for their name and greets
func myMain() {
w := ui.NewWindow("Hello", 400, 100)
w.Closing = ui.Event()
nameField := ui.NewLineEdit("Enter Your Name Here")
button := ui.NewButton("Click Here For a Greeting")
err := w.Open(ui.NewVerticalStack(nameField, button))

View File

@ -7,8 +7,8 @@ import (
const eventbufsiz = 100 // suggested by skelterjohn
// Event returns a new channel suitable for listening for events.
func Event() chan struct{} {
// newEvent returns a new channel suitable for listening for events.
func newEvent() chan struct{} {
return make(chan struct{}, eventbufsiz)
}

View File

@ -112,7 +112,6 @@ var invalidBefore = flag.Bool("invalid", false, "run invalid test before opening
func myMain() {
w := NewWindow("Main Window", 320, 240)
w.Closing = Event()
b := NewButton("Click Me")
b2 := NewButton("Or Me")
s2 := NewHorizontalStack(b, b2)

View File

@ -86,4 +86,3 @@ maybe:
- rename Stack to Box?
- make Combobox and Listbox satisfy sort.Interface?
- should a noneditable Combobox be allowed to return to unselected mode by the user?
- since all events are dispatched without blocking uitask, don't bother requiring explicit dispatch? remove ui.Event() and make Window.Closing initialized by default; if we don't listen on the channel, nothing will happen

View File

@ -8,8 +8,8 @@ import (
// Window represents an on-screen window.
type Window struct {
// If this channel is non-nil, the event loop will receive on this when the user clicks the window's close button.
// This channel can only be set before initially opening the window.
// Closing gets a message when the user clicks the window's close button.
// You cannot change it once the Window has been opened.
Closing chan struct{}
lock sync.Mutex
@ -27,6 +27,7 @@ func NewWindow(title string, width int, height int) *Window {
initTitle: title,
initWidth: width,
initHeight: height,
Closing: newEvent(),
}
}