Skip to content

Commit

Permalink
Merge pull request #28 from yohamta/fix-bug
Browse files Browse the repository at this point in the history
Fix margin bug
  • Loading branch information
yohamta authored Nov 26, 2022
2 parents 48bccd1 + 4c4a998 commit 04dc837
Show file tree
Hide file tree
Showing 2 changed files with 146 additions and 6 deletions.
8 changes: 5 additions & 3 deletions flex.go
Original file line number Diff line number Diff line change
Expand Up @@ -190,10 +190,11 @@ func (f *flexEmbed) layout(width, height int, container *containerEmbed) {
remFreeSpace := float64(f.mainSize(width, height))
unfrozenFlexFactor := 0.0
for _, child := range line.child {
mainMargin := child.mainMargin[0] + child.mainMargin[1]
if child.frozen {
remFreeSpace -= child.mainSize
remFreeSpace -= (child.mainSize + mainMargin)
} else {
remFreeSpace -= float64(f.mainSize(child.node.item.Width, child.node.item.Height))
remFreeSpace -= (float64(f.mainSize(child.node.item.Width, child.node.item.Height)) + mainMargin)
if grow {
unfrozenFlexFactor += child.node.item.Grow
} else {
Expand Down Expand Up @@ -307,7 +308,8 @@ func (f *flexEmbed) layout(width, height int, container *containerEmbed) {
if f.AlignItems == AlignItemStretch &&
f.crossSize(child.node.item.Width, child.node.item.Height) == 0 &&
child.crossSize < line.crossSize {
child.crossSize = line.crossSize
crossMargin := child.crossMargin[0] + child.crossMargin[1]
child.crossSize = line.crossSize - crossMargin
}
}
}
Expand Down
144 changes: 141 additions & 3 deletions flex_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -576,7 +576,8 @@ func TestMerginWithChild(t *testing.T) {
Justify: JustifyEnd,
}

mock := mockHandler{}
mock1 := mockHandler{}
mock2 := mockHandler{}

flex.AddChild(
(&View{
Expand All @@ -585,19 +586,156 @@ func TestMerginWithChild(t *testing.T) {
Direction: Column,
AlignItems: AlignItemEnd,
Justify: JustifyEnd,
Handler: &mock1,
}).AddChild(
&View{
Grow: 1,
Width: 100,
Height: 100,
Handler: &mock,
Handler: &mock2,
},
),
)

flex.Update()
flex.Draw(nil)

assert.Equal(t, image.Rect(850, 800, 950, 900), mock.Frame)
assert.Equal(t, image.Rect(950, 900, 950, 900), mock1.Frame)
assert.Equal(t, image.Rect(850, 800, 950, 900), mock2.Frame)
}

func TestStretchAndMargin(t *testing.T) {
flex := &View{
Width: 1000,
Height: 1000,
AlignItems: AlignItemStretch,
}

mock1 := mockHandler{}
mock2 := mockHandler{}

flex.AddChild(
(&View{
MarginRight: 50,
MarginBottom: 100,
Grow: 1,
Direction: Column,
AlignItems: AlignItemEnd,
Justify: JustifyEnd,
Handler: &mock1,
}).AddChild(
&View{
Width: 100,
Height: 100,
Handler: &mock2,
},
),
)

flex.Update()
flex.Draw(nil)

assert.Equal(t, image.Rect(0, 0, 950, 900), mock1.Frame)
assert.Equal(t, image.Rect(850, 800, 950, 900), mock2.Frame)
}

func TestStretchAndMarginItems(t *testing.T) {
flex := &View{
Width: 1000,
Height: 1000,
AlignItems: AlignItemStretch,
}

mock1 := mockHandler{}
mock2 := mockHandler{}

flex.AddChild(
&View{
MarginRight: 50,
Grow: 1,
Handler: &mock1,
},
&View{
MarginLeft: 50,
Grow: 1,
Handler: &mock2,
},
)

flex.Update()
flex.Draw(nil)

assert.Equal(t, image.Rect(0, 0, 450, 1000), mock1.Frame)
assert.Equal(t, image.Rect(550, 0, 1000, 1000), mock2.Frame)
}

func TestStretchAndMarginItemsMain(t *testing.T) {
flex := &View{
Width: 1000,
Height: 1000,
AlignItems: AlignItemStretch,
Wrap: Wrap,
Direction: Column,
}

mock1 := mockHandler{}
mock2 := mockHandler{}

flex.AddChild(
&View{
Width: 1000,
MarginBottom: 50,
Grow: 1,
Handler: &mock1,
},
&View{
Width: 1000,
MarginBottom: 50,
Grow: 1,
Handler: &mock2,
},
)

flex.Update()
flex.Draw(nil)

assert.Equal(t, image.Rect(0, 0, 1000, 450), mock1.Frame)
assert.Equal(t, image.Rect(0, 500, 1000, 950), mock2.Frame)
}

func TestStretchAndMarginItemsCross(t *testing.T) {
flex := &View{
Width: 1000,
Height: 1000,
AlignItems: AlignItemStretch,
AlignContent: AlignContentStretch,
Wrap: Wrap,
Direction: Row,
}

mock1 := mockHandler{}
mock2 := mockHandler{}

flex.AddChild(
&View{
Width: 1000,
MarginBottom: 50,
Grow: 1,
Handler: &mock1,
},
&View{
Width: 1000,
MarginBottom: 50,
Grow: 1,
Handler: &mock2,
},
)

flex.Update()
flex.Draw(nil)

assert.Equal(t, image.Rect(0, 0, 1000, 450), mock1.Frame)
assert.Equal(t, image.Rect(0, 500, 1000, 950), mock2.Frame)
}

func flexItemBounds(parent *View, child *View) image.Rectangle {
Expand Down

0 comments on commit 04dc837

Please sign in to comment.