Skip to content

Commit

Permalink
Merge pull request #1328 from andydotxyz/refactor/container
Browse files Browse the repository at this point in the history
 Container add Remove finally
  • Loading branch information
andydotxyz committed Sep 21, 2020
2 parents 5ba08c1 + d94e736 commit ded5a51
Show file tree
Hide file tree
Showing 2 changed files with 129 additions and 97 deletions.
133 changes: 78 additions & 55 deletions container.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,37 +14,48 @@ type Container struct {
Objects []CanvasObject // The set of CanvasObjects this container holds
}

func (c *Container) layout() {
if c.Layout != nil {
c.Layout.Layout(c.Objects, c.size)
return
}
// NewContainer returns a new Container instance holding the specified CanvasObjects.
//
// Deprecated: Use NewContainerWithoutLayout to create a container that uses manual layout.
func NewContainer(objects ...CanvasObject) *Container {
return NewContainerWithoutLayout(objects...)
}

// Size returns the current size of this container.
func (c *Container) Size() Size {
return c.size
// NewContainerWithoutLayout returns a new Container instance holding the specified CanvasObjects
// that are manually arranged.
func NewContainerWithoutLayout(objects ...CanvasObject) *Container {
ret := &Container{
Objects: objects,
}

ret.size = ret.MinSize()
return ret
}

// Resize sets a new size for the Container.
func (c *Container) Resize(size Size) {
if c.size == size {
return
// NewContainerWithLayout returns a new Container instance holding the specified
// CanvasObjects which will be laid out according to the specified Layout.
func NewContainerWithLayout(layout Layout, objects ...CanvasObject) *Container {
ret := &Container{
Objects: objects,
Layout: layout,
}

c.size = size
c.layout()
ret.size = layout.MinSize(objects)
ret.layout()
return ret
}

// Position gets the current position of this Container, relative to its parent.
func (c *Container) Position() Position {
return c.position
// Add appends the specified object to the items this container manages.
func (c *Container) Add(add CanvasObject) {
c.Objects = append(c.Objects, add)
c.layout()
}

// Move the container (and all its children) to a new position, relative to its parent.
func (c *Container) Move(pos Position) {
c.position = pos
c.layout()
// AddObject adds another CanvasObject to the set this Container holds.
//
// Deprecated: Use replacement Add() function
func (c *Container) AddObject(o CanvasObject) {
c.Add(o)
}

// MinSize calculates the minimum size of a Container.
Expand All @@ -62,9 +73,30 @@ func (c *Container) MinSize() Size {
return minSize
}

// Visible returns true if the container is currently visible, false otherwise.
func (c *Container) Visible() bool {
return !c.Hidden
// Move the container (and all its children) to a new position, relative to its parent.
func (c *Container) Move(pos Position) {
c.position = pos
c.layout()
}

// Position gets the current position of this Container, relative to its parent.
func (c *Container) Position() Position {
return c.position
}

// Resize sets a new size for the Container.
func (c *Container) Resize(size Size) {
if c.size == size {
return
}

c.size = size
c.layout()
}

// Size returns the current size of this container.
func (c *Container) Size() Size {
return c.size
}

// Show sets this container, and all its children, to be visible.
Expand All @@ -85,12 +117,6 @@ func (c *Container) Hide() {
c.Hidden = true
}

// AddObject adds another CanvasObject to the set this Container holds.
func (c *Container) AddObject(o CanvasObject) {
c.Objects = append(c.Objects, o)
c.layout()
}

// Refresh causes this object to be redrawn in it's current state
func (c *Container) Refresh() {
c.layout()
Expand All @@ -107,35 +133,32 @@ func (c *Container) Refresh() {
o.Refresh(c)
}

// NewContainer returns a new Container instance holding the specified CanvasObjects.
//
// Deprecated: Use NewContainerWithoutLayout to create a container that uses manual layout.
func NewContainer(objects ...CanvasObject) *Container {
return NewContainerWithoutLayout(objects...)
}

// NewContainerWithoutLayout returns a new Container instance holding the specified CanvasObjects
// that are manually arranged.
func NewContainerWithoutLayout(objects ...CanvasObject) *Container {
ret := &Container{
Objects: objects,
// Remove updates the contents of this container to no longer include the specified object.
func (c *Container) Remove(rem CanvasObject) {
if len(c.Objects) == 0 {
return
}

ret.size = ret.MinSize()
ret.layout()
for i, o := range c.Objects {
if o != rem {
continue
}

return ret
copy(c.Objects[i:], c.Objects[i+1:])
c.Objects[len(c.Objects)-1] = nil
c.Objects = c.Objects[:len(c.Objects)-1]
return
}
}

// NewContainerWithLayout returns a new Container instance holding the specified
// CanvasObjects which will be laid out according to the specified Layout.
func NewContainerWithLayout(layout Layout, objects ...CanvasObject) *Container {
ret := &Container{
Objects: objects,
Layout: layout,
}
// Visible returns true if the container is currently visible, false otherwise.
func (c *Container) Visible() bool {
return !c.Hidden
}

ret.size = layout.MinSize(objects)
ret.layout()
return ret
func (c *Container) layout() {
if c.Layout != nil {
c.Layout.Layout(c.Objects, c.size)
return
}
}
93 changes: 51 additions & 42 deletions container_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,46 @@ import (
"github.com/stretchr/testify/assert"
)

func TestMinSize(t *testing.T) {
func TestContainer_Add(t *testing.T) {
box := new(dummyObject)
minSize := box.MinSize()
container := NewContainerWithoutLayout()
assert.Equal(t, 0, len(container.Objects))

container.Add(box)
assert.Equal(t, 1, len(container.Objects))
}

func TestContainer_CustomLayout(t *testing.T) {
box := new(dummyObject)
layout := new(customLayout)
container := NewContainerWithLayout(layout, box)

size := layout.MinSize(container.Objects)
assert.Equal(t, size, container.MinSize())
assert.Equal(t, size, container.Size())
assert.Equal(t, size, box.Size())
}

func TestContainer_Hide(t *testing.T) {
box := new(dummyObject)
container := NewContainerWithoutLayout(box)
assert.Equal(t, minSize, container.MinSize())

container.AddObject(box)
assert.True(t, container.Visible())
assert.True(t, box.Visible())
container.Hide()
assert.False(t, container.Visible())
assert.True(t, box.Visible())
}

func TestContainer_MinSize(t *testing.T) {
box := new(dummyObject)
minSize := box.MinSize()

container := NewContainerWithoutLayout(box)
assert.Equal(t, minSize, container.MinSize())
}

func TestMove(t *testing.T) {
func TestContainer_Move(t *testing.T) {
box := new(dummyObject)
container := NewContainerWithoutLayout(box)

Expand All @@ -35,7 +63,7 @@ func TestMove(t *testing.T) {
assert.Equal(t, pos, box.Position())
}

func TestNilLayout(t *testing.T) {
func TestContainer_NilLayout(t *testing.T) {
box := new(dummyObject)
boxSize := box.size
container := NewContainerWithoutLayout(box)
Expand All @@ -44,47 +72,15 @@ func TestNilLayout(t *testing.T) {
container.Resize(size)
assert.Equal(t, size, container.Size())
assert.Equal(t, boxSize, box.Size())

container.AddObject(box)
assert.Equal(t, boxSize, box.Size())
}

type customLayout struct {
}

func (c *customLayout) Layout(objs []CanvasObject, size Size) {
for _, child := range objs {
child.Resize(size)
}
}

func (c *customLayout) MinSize(_ []CanvasObject) Size {
return NewSize(10, 10)
}

func TestCustomLayout(t *testing.T) {
box := new(dummyObject)
layout := new(customLayout)
container := NewContainerWithLayout(layout, box)

size := layout.MinSize(container.Objects)
assert.Equal(t, size, container.MinSize())
assert.Equal(t, size, container.Size())
assert.Equal(t, size, box.Size())

container.AddObject(box)
assert.Equal(t, size, box.Size())
}

func TestContainer_Hide(t *testing.T) {
func TestContainer_Remove(t *testing.T) {
box := new(dummyObject)
container := NewContainerWithoutLayout(box)
assert.Equal(t, 1, len(container.Objects))

assert.True(t, container.Visible())
assert.True(t, box.Visible())
container.Hide()
assert.False(t, container.Visible())
assert.True(t, box.Visible())
container.Remove(box)
assert.Equal(t, 0, len(container.Objects))
}

func TestContainer_Show(t *testing.T) {
Expand All @@ -100,6 +96,19 @@ func TestContainer_Show(t *testing.T) {
assert.True(t, container.Visible())
}

type customLayout struct {
}

func (c *customLayout) Layout(objs []CanvasObject, size Size) {
for _, child := range objs {
child.Resize(size)
}
}

func (c *customLayout) MinSize(_ []CanvasObject) Size {
return NewSize(10, 10)
}

type dummyObject struct {
size Size
pos Position
Expand Down

0 comments on commit ded5a51

Please sign in to comment.