summaryrefslogtreecommitdiff
path: root/stack.go
diff options
context:
space:
mode:
authorPietro Gagliardi <[email protected]>2014-03-06 20:30:13 -0500
committerPietro Gagliardi <[email protected]>2014-03-06 20:30:13 -0500
commitd6f289bc86814a9a77eba0dd1740d2ee911a59f2 (patch)
treeebf121044150fe742a11a998ef2dff2ce4e0caf1 /stack.go
parent4630eaa84b782b7e3f4bb285925e0b59fe451e71 (diff)
Split NewStack() into NewHorizontalStack() and NewVerticalStack(). Unexported Orientation and its values accordingly.
Diffstat (limited to 'stack.go')
-rw-r--r--stack.go35
1 files changed, 22 insertions, 13 deletions
diff --git a/stack.go b/stack.go
index 171920b..8710594 100644
--- a/stack.go
+++ b/stack.go
@@ -6,11 +6,11 @@ import (
"sync"
)
-// Orientation defines the orientation of controls in a Stack.
-type Orientation bool
+type orientation bool
+
const (
- Horizontal Orientation = false
- Vertical Orientation = true
+ horizontal orientation = false
+ vertical orientation = true
)
// A Stack stacks controls horizontally or vertically within the Stack's parent.
@@ -21,14 +21,13 @@ const (
type Stack struct {
lock sync.Mutex
created bool
- orientation Orientation
+ orientation orientation
controls []Control
stretchy []bool
width, height []int // caches to avoid reallocating these each time
}
-// NewStack creates a new Stack with the specified orientation.
-func NewStack(o Orientation, controls ...Control) *Stack {
+func newStack(o orientation, controls ...Control) *Stack {
return &Stack{
orientation: o,
controls: controls,
@@ -38,6 +37,16 @@ func NewStack(o Orientation, controls ...Control) *Stack {
}
}
+// NewHorizontalStack creates a new Stack that arranges the given Controls horizontally.
+func NewHorizontalStack(controls ...Control) *Stack {
+ return newStack(horizontal, controls...)
+}
+
+// NewVerticalStack creates a new Stack that arranges the given Controls vertically.
+func NewVerticalStack(controls ...Control) *Stack {
+ return newStack(vertical, controls...)
+}
+
// SetStretchy marks a control in a Stack as stretchy. This cannot be called once the Window containing the Stack has been opened.
func (s *Stack) SetStretchy(index int) {
s.lock.Lock()
@@ -85,7 +94,7 @@ func (s *Stack) setRect(x int, y int, width int, height int, winheight int) erro
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
+ if s.orientation == horizontal { // all controls have same height
s.width[i] = w
s.height[i] = height
stretchywid -= w
@@ -97,7 +106,7 @@ func (s *Stack) setRect(x int, y int, width int, height int, winheight int) erro
}
// 2) figure out size of stretchy controls
if nStretchy != 0 {
- if s.orientation == Horizontal { // split rest of width
+ if s.orientation == horizontal { // split rest of width
stretchywid /= nStretchy
} else { // split rest of height
stretchyht /= nStretchy
@@ -116,7 +125,7 @@ func (s *Stack) setRect(x int, y int, width int, height int, winheight int) erro
if err != nil {
return fmt.Errorf("error setting size of control %d in Stack.setRect(): %v", i, err)
}
- if s.orientation == Horizontal {
+ if s.orientation == horizontal {
x += s.width[i]
} else {
y += s.height[i]
@@ -153,7 +162,7 @@ func (s *Stack) preferredSize() (width int, height int, err error) {
maxswid = max(maxswid, w)
maxsht = max(maxsht, h)
}
- if s.orientation == Horizontal { // max vertical size
+ if s.orientation == horizontal { // max vertical size
if !s.stretchy[i] {
width += w
}
@@ -165,7 +174,7 @@ func (s *Stack) preferredSize() (width int, height int, err error) {
}
}
}
- if s.orientation == Horizontal {
+ if s.orientation == horizontal {
width += nStretchy * maxswid
} else {
height += nStretchy * maxsht
@@ -181,5 +190,5 @@ func (s *Stack) preferredSize() (width int, height int, err error) {
// For a Grid, Space can be used to have an empty cell. (TODO stretching/sizing rules)
func Space() Control {
// As above, a Stack with no controls draws nothing and reports no errors; its parent will still size it properly if made stretchy.
- return NewStack(Horizontal)
+ return newStack(horizontal)
}