Adjusted Control and Window for the new system.
This commit is contained in:
parent
403ca88316
commit
942490e1aa
|
@ -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.
|
// 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 {
|
type Control interface {
|
||||||
apply() error
|
apply(window *sysData) error
|
||||||
setParent(c Control)
|
setParent(c Control)
|
||||||
parentWindow() *Window
|
|
||||||
}
|
}
|
||||||
|
|
57
window.go
57
window.go
|
@ -16,7 +16,6 @@ type Window struct {
|
||||||
|
|
||||||
lock sync.Mutex
|
lock sync.Mutex
|
||||||
created bool
|
created bool
|
||||||
control Control
|
|
||||||
sysData *sysData
|
sysData *sysData
|
||||||
initTitle string
|
initTitle string
|
||||||
initWidth int
|
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.
|
// SetTitle sets the window's title.
|
||||||
func (w *Window) SetTitle(title string) (err error) {
|
func (w *Window) SetTitle(title string) (err error) {
|
||||||
w.lock.Lock()
|
w.lock.Lock()
|
||||||
|
@ -75,43 +61,46 @@ func (w *Window) SetSize(width int, height int) (err error) {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// Open opens the window. If the OS window has not been created yet, this function will.
|
// 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.
|
||||||
func (w *Window) Open() (err error) {
|
// TODO rename?
|
||||||
|
func (w *Window) Open(control Control) (err error) {
|
||||||
w.lock.Lock()
|
w.lock.Lock()
|
||||||
defer w.lock.Unlock()
|
defer w.lock.Unlock()
|
||||||
|
|
||||||
// If the window has already been created, show it.
|
if w.created {
|
||||||
if !w.created {
|
// TODO return an error instead?
|
||||||
w.sysData.closing = w.Closing
|
panic("window already open")
|
||||||
err = w.sysData.make(w.initTitle, w.initWidth, w.initHeight)
|
}
|
||||||
|
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 {
|
if err != nil {
|
||||||
return err
|
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()
|
return w.sysData.show()
|
||||||
}
|
}
|
||||||
|
|
||||||
// Close closes the window. The window is not destroyed; it is merely hidden.
|
// Show shows the window.
|
||||||
// TODO don't send on w.Closing
|
func (w *Window) Show() (err error) {
|
||||||
func (w *Window) Close() (err error) {
|
return w.sysData.show()
|
||||||
|
}
|
||||||
|
|
||||||
|
// Hide hides the window.
|
||||||
|
func (w *Window) Hide() (err error) {
|
||||||
return w.sysData.hide()
|
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.
|
// 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).
|
// 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")
|
panic("Window.apply() should never be called")
|
||||||
}
|
}
|
||||||
func (w *Window) setParent(c Control) {
|
func (w *Window) setParent(c Control) {
|
||||||
panic("Window.setParent() should never be called")
|
panic("Window.setParent() should never be called")
|
||||||
}
|
}
|
||||||
func (w *Window) parentWindow() *Window {
|
|
||||||
panic("Window.parentWindow() should never be called")
|
|
||||||
}
|
|
||||||
|
|
Loading…
Reference in New Issue