Big change to Window: split Open()'s functionality into Create() and Open(); they no longer return errors.
This commit is contained in:
parent
59870e80f0
commit
427a0f3a74
5
doc.go
5
doc.go
|
@ -23,10 +23,7 @@ Here is a simple, complete program that asks the user for their name and greets
|
||||||
ui.AppQuit = w.Closing // treat quitting the application like closing the main window
|
ui.AppQuit = w.Closing // treat quitting the application like closing the main window
|
||||||
nameField := ui.NewLineEdit("Enter Your Name Here")
|
nameField := ui.NewLineEdit("Enter Your Name Here")
|
||||||
button := ui.NewButton("Click Here For a Greeting")
|
button := ui.NewButton("Click Here For a Greeting")
|
||||||
err := w.Open(ui.NewVerticalStack(nameField, button))
|
w.Open(ui.NewVerticalStack(nameField, button))
|
||||||
if err != nil {
|
|
||||||
panic(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
for {
|
for {
|
||||||
select {
|
select {
|
||||||
|
|
|
@ -78,8 +78,7 @@ func kbTest() {
|
||||||
wid, ht, ah := mkkbArea()
|
wid, ht, ah := mkkbArea()
|
||||||
a := NewArea(wid, ht, ah)
|
a := NewArea(wid, ht, ah)
|
||||||
w := NewWindow("Hi", wid, ht)
|
w := NewWindow("Hi", wid, ht)
|
||||||
err := w.Open(a)
|
w.Open(a)
|
||||||
if err != nil { panic(err) }
|
|
||||||
<-w.Closing
|
<-w.Closing
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
35
test/main.go
35
test/main.go
|
@ -15,15 +15,16 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
var prefsizetest = flag.Bool("prefsize", false, "")
|
var prefsizetest = flag.Bool("prefsize", false, "")
|
||||||
func listboxPreferredSizeTest() (*Window, error) {
|
func listboxPreferredSizeTest() *Window {
|
||||||
lb := NewListbox("xxxxx", "y", "zzz")
|
lb := NewListbox("xxxxx", "y", "zzz")
|
||||||
g := NewGrid(1, lb)
|
g := NewGrid(1, lb)
|
||||||
w := NewWindow("Listbox Preferred Size Test", 300, 300)
|
w := NewWindow("Listbox Preferred Size Test", 300, 300)
|
||||||
return w, w.Open(g)
|
w.Open(g)
|
||||||
|
return w
|
||||||
}
|
}
|
||||||
|
|
||||||
var gridtest = flag.Bool("grid", false, "")
|
var gridtest = flag.Bool("grid", false, "")
|
||||||
func gridWindow() (*Window, error) {
|
func gridWindow() *Window {
|
||||||
w := NewWindow("Grid Test", 400, 400)
|
w := NewWindow("Grid Test", 400, 400)
|
||||||
b00 := NewButton("0,0")
|
b00 := NewButton("0,0")
|
||||||
b01 := NewButton("0,1")
|
b01 := NewButton("0,1")
|
||||||
|
@ -39,7 +40,8 @@ func gridWindow() (*Window, error) {
|
||||||
l20, c21, l22)
|
l20, c21, l22)
|
||||||
g.SetFilling(1, 2)
|
g.SetFilling(1, 2)
|
||||||
g.SetStretchy(1, 1)
|
g.SetStretchy(1, 1)
|
||||||
return w, w.Open(g)
|
w.Open(g)
|
||||||
|
return w
|
||||||
}
|
}
|
||||||
|
|
||||||
var macCrashTest = flag.Bool("maccrash", false, "attempt crash on Mac OS X on deleting too far (debug lack of panic on 32-bit)")
|
var macCrashTest = flag.Bool("maccrash", false, "attempt crash on Mac OS X on deleting too far (debug lack of panic on 32-bit)")
|
||||||
|
@ -210,10 +212,7 @@ func areaTest() {
|
||||||
timedisp,
|
timedisp,
|
||||||
sizeStack)
|
sizeStack)
|
||||||
layout.SetStretchy(0)
|
layout.SetStretchy(0)
|
||||||
err = w.Open(layout)
|
w.Open(layout)
|
||||||
if err != nil {
|
|
||||||
panic(err)
|
|
||||||
}
|
|
||||||
for {
|
for {
|
||||||
select {
|
select {
|
||||||
case <-w.Closing:
|
case <-w.Closing:
|
||||||
|
@ -257,10 +256,7 @@ func areaboundsTest() {
|
||||||
r.Min.Y++
|
r.Min.Y++
|
||||||
draw.Draw(img, r, u(128, 0, 128), image.ZP, draw.Over)
|
draw.Draw(img, r, u(128, 0, 128), image.ZP, draw.Over)
|
||||||
w := NewWindow("Area Bounds Test", 320, 240)
|
w := NewWindow("Area Bounds Test", 320, 240)
|
||||||
err := w.Open(a)
|
w.Open(a)
|
||||||
if err != nil {
|
|
||||||
panic(err)
|
|
||||||
}
|
|
||||||
<-w.Closing
|
<-w.Closing
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -324,21 +320,12 @@ func myMain() {
|
||||||
if *invalidBefore {
|
if *invalidBefore {
|
||||||
invalidTest(cb1, lb1, s, NewGrid(1, Space()))
|
invalidTest(cb1, lb1, s, NewGrid(1, Space()))
|
||||||
}
|
}
|
||||||
err := w.Open(s)
|
w.Open(s)
|
||||||
if err != nil {
|
|
||||||
panic(err)
|
|
||||||
}
|
|
||||||
if *gridtest {
|
if *gridtest {
|
||||||
_, err := gridWindow()
|
gridWindow()
|
||||||
if err != nil {
|
|
||||||
panic(err)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
if *prefsizetest {
|
if *prefsizetest {
|
||||||
_, err = listboxPreferredSizeTest()
|
listboxPreferredSizeTest()
|
||||||
if err != nil {
|
|
||||||
panic(err)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
ticker := time.Tick(time.Second)
|
ticker := time.Tick(time.Second)
|
||||||
|
|
37
window.go
37
window.go
|
@ -20,9 +20,10 @@ type Window struct {
|
||||||
initTitle string
|
initTitle string
|
||||||
initWidth int
|
initWidth int
|
||||||
initHeight int
|
initHeight int
|
||||||
|
shownOnce bool
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewWindow creates a new window with the given title and size. The window is not constructed at the OS level until a call to Open().
|
// NewWindow allocates a new Window with the given title and size. The window is not created until a call to Create() or Open().
|
||||||
func NewWindow(title string, width int, height int) *Window {
|
func NewWindow(title string, width int, height int) *Window {
|
||||||
return &Window{
|
return &Window{
|
||||||
sysData: mksysdata(c_window),
|
sysData: mksysdata(c_window),
|
||||||
|
@ -62,40 +63,38 @@ func (w *Window) SetSize(width int, height int) (err error) {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// Open opens the window, setting its control to the given control, and then shows the window. This can only be called once per window, and finalizes all initialization of the control.
|
// Open creates the Window with Create and then shows the Window with Show. As with Create, you cannot call Open more than once per window.
|
||||||
// TODO rename?
|
func (w *Window) Open(control Control) {
|
||||||
func (w *Window) Open(control Control) (err error) {
|
w.Create(control)
|
||||||
|
w.Show()
|
||||||
|
}
|
||||||
|
|
||||||
|
// Create creates the Window, setting its control to the given control. It does not show the window. This can only be called once per window, and finalizes all initialization of the control.
|
||||||
|
func (w *Window) Create(control Control) {
|
||||||
w.lock.Lock()
|
w.lock.Lock()
|
||||||
defer w.lock.Unlock()
|
defer w.lock.Unlock()
|
||||||
|
|
||||||
if w.created {
|
if w.created {
|
||||||
// TODO return an error instead?
|
|
||||||
panic("window already open")
|
panic("window already open")
|
||||||
}
|
}
|
||||||
w.sysData.event = w.Closing
|
w.sysData.event = w.Closing
|
||||||
err = w.sysData.make(nil)
|
err := w.sysData.make(nil)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("error opening window: %v", err)
|
panic(fmt.Errorf("error opening window: %v", err))
|
||||||
}
|
}
|
||||||
if control != nil {
|
if control != nil {
|
||||||
w.sysData.resize = control.setRect
|
w.sysData.resize = control.setRect
|
||||||
err = control.make(w.sysData)
|
err = control.make(w.sysData)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("error adding window's control: %v", err)
|
panic(fmt.Errorf("error adding window's control: %v", err))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
err = w.sysData.setWindowSize(w.initWidth, w.initHeight)
|
err = w.sysData.setWindowSize(w.initWidth, w.initHeight)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("error setting window size (in Window.Open()): %v", err)
|
panic(fmt.Errorf("error setting window size (in Window.Open()): %v", err))
|
||||||
}
|
}
|
||||||
w.sysData.setText(w.initTitle)
|
w.sysData.setText(w.initTitle)
|
||||||
// TODO separate showing?
|
|
||||||
err = w.sysData.firstShow()
|
|
||||||
if err != nil {
|
|
||||||
return fmt.Errorf("error showing window (in Window.Open()): %v", err)
|
|
||||||
}
|
|
||||||
w.created = true
|
w.created = true
|
||||||
return nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Show shows the window.
|
// Show shows the window.
|
||||||
|
@ -103,6 +102,14 @@ func (w *Window) Show() {
|
||||||
w.lock.Lock()
|
w.lock.Lock()
|
||||||
defer w.lock.Unlock()
|
defer w.lock.Unlock()
|
||||||
|
|
||||||
|
if !w.shownOnce {
|
||||||
|
w.shownOnce = true
|
||||||
|
err := w.sysData.firstShow()
|
||||||
|
if err != nil {
|
||||||
|
panic(fmt.Errorf("error showing window for the first time: %v", err))
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
w.sysData.show()
|
w.sysData.show()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue