Made the sizing recursive chain idempotent and added a -spaced option to the test program to test spacing.

This commit is contained in:
Pietro Gagliardi 2014-07-26 14:11:03 -04:00
parent 22989c13da
commit d34ffa326c
5 changed files with 11 additions and 15 deletions

View File

@ -28,23 +28,17 @@ type controlSizing interface {
// on Windows, this is only embedded by window, as all other containers cannot have their own children; beginResize() points to an instance method literal (TODO get correct term) from window
// on GTK+ and Mac OS X, one is embedded by window and all containers; beginResize() points to a global function (TODO NOT GOOD; ideally the sizing data should be passed across size-allocate requests)
type container struct {
child Control
spaced bool
beginResize func() (d *sizing) // for the initial call
d *sizing // for recursive calls
child Control
spaced bool
d *sizing
}
func (c *container) resize(width, height int) {
if c.child == nil { // no children; nothing to do
return
}
if c.d == nil { // initial call
if c.beginResize == nil {
// should be a recursive call, but is not
// TODO get rid of this
return
}
c.d = c.beginResize()
if c.d == nil { // not ready (called early or out of the proper recursive call chain (such as by the underlying system when marking an unparented Tab as shown))
return
}
d := c.d
allocations := c.child.allocate(0, 0, width, height, d)
@ -54,6 +48,5 @@ func (c *container) resize(width, height int) {
allocations[i].this.commitResize(allocations[i], d)
}
// always set c.d to nil so it can be garbage-collected
// the c.endResize() above won't matter since the c.d there is evaluated then, not when c.endResize() is called
c.d = nil
}

View File

@ -32,7 +32,6 @@ func newWindow(title string, width int, height int, control Control) *window {
closing: newEvent(),
container: new(container),
}
w.container.beginResize = w.beginResize
C.windowSetDelegate(id, unsafe.Pointer(w))
w.child = control
w.child.setParent(C.windowContentView(w.id))
@ -78,6 +77,7 @@ func windowClosing(xw unsafe.Pointer) C.BOOL {
//export windowResized
func windowResized(xw unsafe.Pointer, width C.uintptr_t, height C.uintptr_t) {
w := (*window)(unsafe.Pointer(xw))
w.container.d = w.beginResize()
w.resize(int(width), int(height))
fmt.Printf("new size %d x %d\n", width, height)
}

View File

@ -49,7 +49,6 @@ func newWindow(title string, width int, height int, control Control) *window {
closing: newEvent(),
container: new(container),
}
w.container.beginResize = w.beginResize
C.gtk_window_set_title(w.window, ctitle)
g_signal_connect(
C.gpointer(unsafe.Pointer(w.window)),
@ -111,6 +110,7 @@ func windowClosing(wid *C.GtkWidget, e *C.GdkEvent, data C.gpointer) C.gboolean
//export windowResizing
func windowResizing(wid *C.GtkWidget, r *C.GdkRectangle, data C.gpointer) {
w := (*window)(unsafe.Pointer(data))
w.container.d = w.beginResize()
w.resize(int(r.width), int(r.height))
fmt.Printf("new size %d x %d\n", r.width, r.height)
}

View File

@ -43,7 +43,6 @@ func newWindow(title string, width int, height int, control Control) *window {
closing: newEvent(),
container: new(container),
}
w.container.beginResize = w.beginResize
hwnd := C.newWindow(toUTF16(title), C.int(width), C.int(height), unsafe.Pointer(w))
if hwnd != w.hwnd {
panic(fmt.Errorf("inconsistency: hwnd returned by CreateWindowEx() (%p) and hwnd stored in window (%p) differ", hwnd, w.hwnd))
@ -97,6 +96,7 @@ func storeWindowHWND(data unsafe.Pointer, hwnd C.HWND) {
//export windowResize
func windowResize(data unsafe.Pointer, r *C.RECT) {
w := (*window)(data)
w.container.d = w.beginResize()
w.resize(int(r.right - r.left), int(r.bottom - r.top))
}

View File

@ -11,6 +11,7 @@ import (
)
var closeOnClick = flag.Bool("close", false, "close on click")
var spaced = flag.Bool("spaced", false, "enable spacing")
// because Cocoa hates being run off the main thread, even if it's run exclusively off the main thread
func init() {
@ -20,6 +21,8 @@ func init() {
Do(func() {
t := NewTab()
w := NewWindow("Hello", 320, 240, t)
// TODO use a method here
w.(*window).spaced = *spaced
w.OnClosing(func() bool {
if *closeOnClick {
panic("window closed normally in close on click mode (should not happen)")