handle uneven disribution of space
This commit is contained in:
		
							
								
								
									
										17
									
								
								ui/ui.go
									
									
									
									
									
								
							
							
						
						
									
										17
									
								
								ui/ui.go
									
									
									
									
									
								
							| @@ -56,22 +56,35 @@ func (c *Container) Draw(s tcell.Screen) { | |||||||
|  |  | ||||||
| func (c *Container) SetSize(x, y, h, w int) { | func (c *Container) SetSize(x, y, h, w int) { | ||||||
| 	c.x, c.y, c.h, c.w = x, y, h, w | 	c.x, c.y, c.h, c.w = x, y, h, w | ||||||
|  | 	carry := 0 | ||||||
| 	if c.layoutMethod == LayoutVerticalEven { | 	if c.layoutMethod == LayoutVerticalEven { | ||||||
| 		num := len(c.contents) | 		num := len(c.contents) | ||||||
|  | 		extra := c.h % num | ||||||
| 		for r := range c.contents { | 		for r := range c.contents { | ||||||
| 			w := c.w | 			w := c.w | ||||||
| 			h := c.h / num | 			h := c.h / num | ||||||
| 			x := c.x | 			x := c.x | ||||||
| 			y := c.y + (h * r) | 			y := c.y + (h * r) + carry | ||||||
|  | 			if extra > 0 { // distribute "extra" space to containers as we have some left | ||||||
|  | 				h++ | ||||||
|  | 				extra-- | ||||||
|  | 				carry++ | ||||||
|  | 			} | ||||||
| 			c.contents[r].Container.SetSize(x, y, h, w) | 			c.contents[r].Container.SetSize(x, y, h, w) | ||||||
| 		} | 		} | ||||||
| 	} else if c.layoutMethod == LayoutHorizontalEven { | 	} else if c.layoutMethod == LayoutHorizontalEven { | ||||||
| 		num := len(c.contents) | 		num := len(c.contents) | ||||||
|  | 		extra := c.w % num | ||||||
| 		for r := range c.contents { | 		for r := range c.contents { | ||||||
| 			w := c.w / num | 			w := c.w / num | ||||||
| 			h := c.h | 			h := c.h | ||||||
| 			x := c.x + (w * r) | 			x := c.x + (w * r) + carry | ||||||
| 			y := c.y | 			y := c.y | ||||||
|  | 			if extra > 0 { // distribute "extra" space to containers as we have some left | ||||||
|  | 				w++ | ||||||
|  | 				extra-- | ||||||
|  | 				carry++ | ||||||
|  | 			} | ||||||
| 			c.contents[r].Container.SetSize(x, y, h, w) | 			c.contents[r].Container.SetSize(x, y, h, w) | ||||||
| 		} | 		} | ||||||
| 	} else { | 	} else { | ||||||
|   | |||||||
| @@ -55,6 +55,32 @@ func TestContainerTwoBoxesHStack(t *testing.T) { | |||||||
| 		t.Fail() | 		t.Fail() | ||||||
| 	} | 	} | ||||||
| } | } | ||||||
|  | func TestContainerThreeBoxesUnevenHStack(t *testing.T) { | ||||||
|  | 	expect := `┌─ one ──┐┌─ two ──┐┌─ three  | ||||||
|  | │        ││        ││       │ | ||||||
|  | │        ││        ││       │ | ||||||
|  | │        ││        ││       │ | ||||||
|  | └────────┘└────────┘└───────┘ | ||||||
|  | ` | ||||||
|  | 	m := &MockScreen{} | ||||||
|  | 	one := NewBox("one", nil, Contents{}) | ||||||
|  | 	two := NewBox("two", nil, Contents{}) | ||||||
|  | 	three := NewBox("three", nil, Contents{}) | ||||||
|  | 	container := NewContainer( | ||||||
|  | 		Contents{{Container: one}, {Container: two}, {Container: three}}, | ||||||
|  | 		LayoutHorizontalEven, | ||||||
|  | 	) | ||||||
|  | 	m.Init() | ||||||
|  | 	m.Resize(0, 0, 5, 29) | ||||||
|  | 	container.SetSize(0, 0, 5, 29) | ||||||
|  | 	container.Draw(m) | ||||||
|  | 	result := m.DumpContents() | ||||||
|  | 	if result != expect { | ||||||
|  | 		fmt.Printf("expected:\n%+v", expect) | ||||||
|  | 		fmt.Printf("actual:\n%+v", result) | ||||||
|  | 		t.Fail() | ||||||
|  | 	} | ||||||
|  | } | ||||||
|  |  | ||||||
| func TestContainerTwoBoxesVStack(t *testing.T) { | func TestContainerTwoBoxesVStack(t *testing.T) { | ||||||
| 	expect := `┌─ one ──┐ | 	expect := `┌─ one ──┐ | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user