Wrote the new Stack.setRect().
This commit is contained in:
parent
a174fbebbd
commit
1c540117d3
66
stack.go
66
stack.go
|
@ -23,8 +23,7 @@ type Stack struct {
|
||||||
orientation Orientation
|
orientation Orientation
|
||||||
controls []Control
|
controls []Control
|
||||||
stretchy []bool
|
stretchy []bool
|
||||||
xpos, ypos []int // caches to avoid reallocating these each time
|
width, height []int // caches to avoid reallocating these each time
|
||||||
width, height []int
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewStack creates a new Stack with the specified orientation.
|
// NewStack creates a new Stack with the specified orientation.
|
||||||
|
@ -33,8 +32,6 @@ func NewStack(o Orientation, controls ...Control) *Stack {
|
||||||
orientation: o,
|
orientation: o,
|
||||||
controls: controls,
|
controls: controls,
|
||||||
stretchy: make([]bool, len(controls)),
|
stretchy: make([]bool, len(controls)),
|
||||||
xpos: make([]int, len(controls)),
|
|
||||||
ypos: make([]int, len(controls)),
|
|
||||||
width: make([]int, len(controls)),
|
width: make([]int, len(controls)),
|
||||||
height: make([]int, len(controls)),
|
height: make([]int, len(controls)),
|
||||||
}
|
}
|
||||||
|
@ -61,25 +58,60 @@ func (s *Stack) make(window *sysData) error {
|
||||||
|
|
||||||
func (s *Stack) setRect(x int, y int, width int, height int) error {
|
func (s *Stack) setRect(x int, y int, width int, height int) error {
|
||||||
var dx, dy int
|
var dx, dy int
|
||||||
|
var stretchywid, stretchyht int
|
||||||
|
|
||||||
if len(s.controls) == 0 { // do nothing if there's nothing to do
|
if len(s.controls) == 0 { // do nothing if there's nothing to do
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
switch s.orientation {
|
// 1) get height and width of non-stretchy controls; figure out how much space is alloted to stretchy controls
|
||||||
case Horizontal:
|
stretchywid = width
|
||||||
dx = width / len(s.controls)
|
stretchyht = height
|
||||||
width = dx
|
nStretchy := 0
|
||||||
case Vertical:
|
|
||||||
dy = height / len(s.controls)
|
|
||||||
height = dy
|
|
||||||
}
|
|
||||||
for i, c := range s.controls {
|
for i, c := range s.controls {
|
||||||
err := c.setRect(x, y, width, height)
|
if s.stretchy[i] {
|
||||||
if err != nil {
|
nStretchy++
|
||||||
return fmt.Errorf("error setting size of control %d: %v", i, err)
|
continue
|
||||||
|
}
|
||||||
|
w, h, err := c.preferredSize()
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("error getting preferred size of control %d in Stack.setRect(): %v", i, err)
|
||||||
|
}
|
||||||
|
if s.orientation == Horizontal { // all controls have same height
|
||||||
|
s.width[i] = w
|
||||||
|
s.height[i] = height
|
||||||
|
stretchywid -= w
|
||||||
|
} else { // all controls have same width
|
||||||
|
s.width[i] = width
|
||||||
|
s.height[i] = h
|
||||||
|
stretchyht -= h
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// 2) figure out size of stretchy controls
|
||||||
|
if nStretchy != 0 {
|
||||||
|
if s.orientation == Horizontal { // split rest of width
|
||||||
|
stretchywid /= nStretchy
|
||||||
|
} else { // split rest of height
|
||||||
|
stretchyht /= nStretchy
|
||||||
|
}
|
||||||
|
}
|
||||||
|
for i, c := range controls {
|
||||||
|
if !s.stretchy[i] {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
c.width[i] = stretchywid
|
||||||
|
c.height[i] = stretchyht
|
||||||
|
}
|
||||||
|
// 3) now actually place controls
|
||||||
|
for i, c := range s.controls {
|
||||||
|
err := c.setRect(x, y, s.width[i], s.height[i])
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("error setting size of control %d in Stack.setRect(): %v", i, err)
|
||||||
|
}
|
||||||
|
if s.orientation == Horizontal {
|
||||||
|
x += s.width[i]
|
||||||
|
} else {
|
||||||
|
y += s.height[i]
|
||||||
}
|
}
|
||||||
x += dx
|
|
||||||
y += dy
|
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue