From 942490e1aa1fab8978fdb0b70e30ee78793d0fd4 Mon Sep 17 00:00:00 2001 From: Pietro Gagliardi Date: Wed, 12 Feb 2014 21:23:53 -0500 Subject: [PATCH] Adjusted Control and Window for the new system. --- control.go | 3 +-- window.go | 57 ++++++++++++++++++++++-------------------------------- 2 files changed, 24 insertions(+), 36 deletions(-) diff --git a/control.go b/control.go index 54f92d1..4e1243e 100644 --- a/control.go +++ b/control.go @@ -8,7 +8,6 @@ import ( // A Control represents an UI control. Note that Control contains unexported members; this has the consequence that you can't build custom controls that interface directly with the system-specific code (fo rinstance, to import an unsupported control), or at least not without some hackery. If you want to make your own controls, embed Area and provide its necessities. type Control interface { - apply() error + apply(window *sysData) error setParent(c Control) - parentWindow() *Window } diff --git a/window.go b/window.go index cdb346b..0e4f7aa 100644 --- a/window.go +++ b/window.go @@ -16,7 +16,6 @@ type Window struct { lock sync.Mutex created bool - control Control sysData *sysData initTitle string initWidth int @@ -37,19 +36,6 @@ func NewWindow(title string, width int, height int) *Window { } } -// SetControl sets the window's central control to control. This function cannot be called once the window has been opened. -func (w *Window) SetControl(control Control) (err error) { - w.lock.Lock() - defer w.lock.Unlock() - - if w.created { - panic("cannot set window control after window has been opened") - } - w.control = control - w.control.setParent(w) - return nil -} - // SetTitle sets the window's title. func (w *Window) SetTitle(title string) (err error) { w.lock.Lock() @@ -75,43 +61,46 @@ func (w *Window) SetSize(width int, height int) (err error) { return nil } -// Open opens the window. If the OS window has not been created yet, this function will. -func (w *Window) Open() (err error) { +// 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) { w.lock.Lock() defer w.lock.Unlock() - // If the window has already been created, show it. - if !w.created { - w.sysData.closing = w.Closing - err = w.sysData.make(w.initTitle, w.initWidth, w.initHeight) + if w.created { + // TODO return an error instead? + panic("window already open") + } + w.sysData.event = w.Closing + err = w.sysData.make(w.initTitle, w.initWidth, w.initHeight, nil) + if err != nil { + return err + } + if control != nil { + err = control.apply(w.sysData) if err != nil { return err } - if w.control != nil { - err = w.control.apply() - if err != nil { - return err - } - } - w.created = true } + w.created = true return w.sysData.show() } -// Close closes the window. The window is not destroyed; it is merely hidden. -// TODO don't send on w.Closing -func (w *Window) Close() (err error) { +// Show shows the window. +func (w *Window) Show() (err error) { + return w.sysData.show() +} + +// Hide hides the window. +func (w *Window) Hide() (err error) { return w.sysData.hide() } // These satisfy the Control interface, allowing a window to own a control. As a consequence, Windows are themselves Controls. THIS IS UNDOCUMENTED AND UNSUPPORTED. I can make it supported in the future, but for now, no. You shouldn't be depending on the internals of the library to develop your programs: if the documentation is incomplete and/or wrong, get the person responsible to fix it, as the documentation, not the implementation, is your contract to what you can or cannot do. Don't worry, this package is in good company: Go itself was designed spec-first for this reason. // If I decide not to support windows as controls, a better way to deal with controls would be in order. Perhaps separate interfaces...? Making Windows Controls seems the cleanest option for now (and with correct usage of the library costs nothing). -func (w *Window) apply() error { +func (w *Window) apply(window *sysData) error { panic("Window.apply() should never be called") } func (w *Window) setParent(c Control) { panic("Window.setParent() should never be called") } -func (w *Window) parentWindow() *Window { - panic("Window.parentWindow() should never be called") -}