From 427a0f3a74a1feed3cfbef8894719221e4f8f27b Mon Sep 17 00:00:00 2001 From: Pietro Gagliardi Date: Tue, 3 Jun 2014 23:08:51 -0400 Subject: [PATCH] Big change to Window: split Open()'s functionality into Create() and Open(); they no longer return errors. --- doc.go | 5 +---- test/kbtest.go | 3 +-- test/main.go | 35 +++++++++++------------------------ window.go | 37 ++++++++++++++++++++++--------------- 4 files changed, 35 insertions(+), 45 deletions(-) diff --git a/doc.go b/doc.go index 7487d5e..0a2637a 100644 --- a/doc.go +++ b/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 nameField := ui.NewLineEdit("Enter Your Name Here") button := ui.NewButton("Click Here For a Greeting") - err := w.Open(ui.NewVerticalStack(nameField, button)) - if err != nil { - panic(err) - } + w.Open(ui.NewVerticalStack(nameField, button)) for { select { diff --git a/test/kbtest.go b/test/kbtest.go index 6765c49..df00613 100644 --- a/test/kbtest.go +++ b/test/kbtest.go @@ -78,8 +78,7 @@ func kbTest() { wid, ht, ah := mkkbArea() a := NewArea(wid, ht, ah) w := NewWindow("Hi", wid, ht) - err := w.Open(a) - if err != nil { panic(err) } + w.Open(a) <-w.Closing } diff --git a/test/main.go b/test/main.go index d5786f7..cb17466 100644 --- a/test/main.go +++ b/test/main.go @@ -15,15 +15,16 @@ import ( ) var prefsizetest = flag.Bool("prefsize", false, "") -func listboxPreferredSizeTest() (*Window, error) { +func listboxPreferredSizeTest() *Window { lb := NewListbox("xxxxx", "y", "zzz") g := NewGrid(1, lb) w := NewWindow("Listbox Preferred Size Test", 300, 300) - return w, w.Open(g) + w.Open(g) + return w } var gridtest = flag.Bool("grid", false, "") -func gridWindow() (*Window, error) { +func gridWindow() *Window { w := NewWindow("Grid Test", 400, 400) b00 := NewButton("0,0") b01 := NewButton("0,1") @@ -39,7 +40,8 @@ func gridWindow() (*Window, error) { l20, c21, l22) g.SetFilling(1, 2) 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)") @@ -210,10 +212,7 @@ func areaTest() { timedisp, sizeStack) layout.SetStretchy(0) - err = w.Open(layout) - if err != nil { - panic(err) - } + w.Open(layout) for { select { case <-w.Closing: @@ -257,10 +256,7 @@ func areaboundsTest() { r.Min.Y++ draw.Draw(img, r, u(128, 0, 128), image.ZP, draw.Over) w := NewWindow("Area Bounds Test", 320, 240) - err := w.Open(a) - if err != nil { - panic(err) - } + w.Open(a) <-w.Closing } @@ -324,21 +320,12 @@ func myMain() { if *invalidBefore { invalidTest(cb1, lb1, s, NewGrid(1, Space())) } - err := w.Open(s) - if err != nil { - panic(err) - } + w.Open(s) if *gridtest { - _, err := gridWindow() - if err != nil { - panic(err) - } + gridWindow() } if *prefsizetest { - _, err = listboxPreferredSizeTest() - if err != nil { - panic(err) - } + listboxPreferredSizeTest() } ticker := time.Tick(time.Second) diff --git a/window.go b/window.go index 58f0aca..e65d55d 100644 --- a/window.go +++ b/window.go @@ -20,9 +20,10 @@ type Window struct { initTitle string initWidth 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 { return &Window{ sysData: mksysdata(c_window), @@ -62,40 +63,38 @@ func (w *Window) SetSize(width int, height int) (err error) { 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. -// TODO rename? -func (w *Window) Open(control Control) (err error) { +// 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. +func (w *Window) Open(control Control) { + 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() defer w.lock.Unlock() if w.created { - // TODO return an error instead? panic("window already open") } w.sysData.event = w.Closing - err = w.sysData.make(nil) + err := w.sysData.make(nil) if err != nil { - return fmt.Errorf("error opening window: %v", err) + panic(fmt.Errorf("error opening window: %v", err)) } if control != nil { w.sysData.resize = control.setRect err = control.make(w.sysData) 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) 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) - // TODO separate showing? - err = w.sysData.firstShow() - if err != nil { - return fmt.Errorf("error showing window (in Window.Open()): %v", err) - } w.created = true - return nil } // Show shows the window. @@ -103,6 +102,14 @@ func (w *Window) Show() { w.lock.Lock() 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() }