Vertical stack example works

This commit is contained in:
Sasha Koshka 2023-01-31 18:04:12 -05:00
parent 2f9504b1e4
commit b0ff1ca0af
8 changed files with 25 additions and 36 deletions

View File

@ -63,6 +63,7 @@ func (canvas BasicCanvas) Buffer () (data []color.RGBA, stride int) {
// Cut returns a sub-canvas of a given canvas.
func Cut (canvas Canvas, bounds image.Rectangle) (reduced BasicCanvas) {
// println(canvas.Bounds().String(), bounds.String())
bounds = bounds.Intersect(canvas.Bounds())
if bounds.Empty() { return }
reduced.rect = bounds

View File

@ -137,7 +137,7 @@ func (element *Button) draw () {
textBounds := element.drawer.LayoutBounds()
offset := image.Point {
X: innerBounds.Min.X + (innerBounds.Dx() - textBounds.Dx()) / 2,
Y: innerBounds.Min.X + (innerBounds.Dy() - textBounds.Dy()) / 2,
Y: innerBounds.Min.Y + (innerBounds.Dy() - textBounds.Dy()) / 2,
}
// account for the fact that the bounding rectangle will be shifted over

View File

@ -207,7 +207,7 @@ func (element *Container) redoAll () {
})
artist.FillRectangle(element, pattern, bounds)
// resize all elements, having them draw onto us
// cut our canvas up and give peices to child elements
for _, entry := range element.children {
entry.DrawTo(tomo.Cut(element, entry.Bounds))
}
@ -217,31 +217,27 @@ func (element *Container) HandleMouseDown (x, y int, button tomo.Button) {
child, handlesMouse := element.ChildAt(image.Pt(x, y)).(tomo.MouseTarget)
if !handlesMouse { return }
element.drags[button] = child
childPosition := element.childPosition(child)
child.HandleMouseDown(x - childPosition.X, y - childPosition.Y, button)
child.HandleMouseDown(x, y, button)
}
func (element *Container) HandleMouseUp (x, y int, button tomo.Button) {
child := element.drags[button]
if child == nil { return }
element.drags[button] = nil
childPosition := element.childPosition(child)
child.HandleMouseUp(x - childPosition.X, y - childPosition.Y, button)
child.HandleMouseUp(x, y, button)
}
func (element *Container) HandleMouseMove (x, y int) {
for _, child := range element.drags {
if child == nil { continue }
childPosition := element.childPosition(child)
child.HandleMouseMove(x - childPosition.X, y - childPosition.Y)
child.HandleMouseMove(x, y)
}
}
func (element *Container) HandleMouseScroll (x, y int, deltaX, deltaY float64) {
child, handlesMouse := element.ChildAt(image.Pt(x, y)).(tomo.MouseTarget)
if !handlesMouse { return }
childPosition := element.childPosition(child)
child.HandleMouseScroll(x - childPosition.X, y - childPosition.Y, deltaX, deltaY)
child.HandleMouseScroll(x, y, deltaX, deltaY)
}
func (element *Container) HandleKeyDown (key tomo.Key, modifiers tomo.Modifiers) {
@ -475,6 +471,5 @@ func (element *Container) updateMinimumSize () {
}
func (element *Container) recalculate () {
bounds := element.Bounds()
element.layout.Arrange(element.children, bounds.Dx(), bounds.Dy())
element.layout.Arrange(element.children, element.Bounds())
}

View File

@ -1,6 +1,5 @@
package basic
import "image"
import "git.tebibyte.media/sashakoshka/tomo/theme"
import "git.tebibyte.media/sashakoshka/tomo/artist"
import "git.tebibyte.media/sashakoshka/tomo/elements/core"
@ -119,8 +118,5 @@ func (element *Label) draw () {
foreground, _ := theme.ForegroundPattern (theme.PatternState {
Case: labelCase,
})
element.drawer.Draw (element, foreground, image.Point {
X: 0 - textBounds.Min.X,
Y: 0 - textBounds.Min.Y,
})
element.drawer.Draw (element, foreground, bounds.Min.Sub(textBounds.Min))
}

View File

@ -17,7 +17,7 @@ type Layout interface {
// and changes the position of the entiries in the slice so that they
// are properly laid out. The given width and height should not be less
// than what is returned by MinimumSize.
Arrange (entries []LayoutEntry, width, height int)
Arrange (entries []LayoutEntry, bounds image.Rectangle)
// MinimumSize returns the minimum width and height that the layout
// needs to properly arrange the given slice of layout entries.

View File

@ -96,7 +96,7 @@ func (layout Dialog) Arrange (entries []tomo.LayoutEntry, width, height int) {
entryBounds := entry.Bounds
if entryBounds.Dy() != controlRowHeight ||
entryBounds.Dx() != entryWidth {
entry.Bounds.Max = entryBounds.Min.Add (
entries[index].Bounds.Max = entryBounds.Min.Add (
image.Pt(entryWidth, controlRowHeight))
}
}

View File

@ -45,7 +45,7 @@ func (layout Horizontal) Arrange (entries []tomo.LayoutEntry, width, height int)
x += entryWidth
entryBounds := entry.Bounds
if entryBounds.Dy() != height || entryBounds.Dx() != entryWidth {
entry.Bounds.Max = entryBounds.Min.Add (
entries[index].Bounds.Max = entryBounds.Min.Add (
image.Pt(entryWidth, height))
}
}

View File

@ -17,22 +17,21 @@ type Vertical struct {
}
// Arrange arranges a list of entries vertically.
func (layout Vertical) Arrange (entries []tomo.LayoutEntry, width, height int) {
func (layout Vertical) Arrange (entries []tomo.LayoutEntry, bounds image.Rectangle) {
if layout.Pad {
width -= theme.Margin() * 2
height -= theme.Margin() * 2
bounds = bounds.Inset(theme.Margin())
}
freeSpace := height
expandingElements := 0
// count the number of expanding elements and the amount of free space
// for them to collectively occupy, while gathering minimum heights.
freeSpace := bounds.Dy()
minimumHeights := make([]int, len(entries))
expandingElements := 0
for index, entry := range entries {
var entryMinHeight int
if child, flexible := entry.Element.(tomo.Flexible); flexible {
entryMinHeight = child.FlexibleHeightFor(width)
entryMinHeight = child.FlexibleHeightFor(bounds.Dx())
} else {
_, entryMinHeight = entry.MinimumSize()
}
@ -47,34 +46,32 @@ func (layout Vertical) Arrange (entries []tomo.LayoutEntry, width, height int) {
freeSpace -= theme.Margin()
}
}
expandingElementHeight := 0
if expandingElements > 0 {
expandingElementHeight = freeSpace / expandingElements
}
x, y := 0, 0
if layout.Pad {
x += theme.Margin()
y += theme.Margin()
}
dot := bounds.Min
// set the size and position of each element
for index, entry := range entries {
if index > 0 && layout.Gap { y += theme.Margin() }
if index > 0 && layout.Gap { dot.Y += theme.Margin() }
entries[index].Bounds.Min = image.Pt(x, y)
entry.Bounds.Min = dot
entryHeight := 0
if entry.Expand {
entryHeight = expandingElementHeight
} else {
entryHeight = minimumHeights[index]
}
y += entryHeight
dot.Y += entryHeight
entryBounds := entry.Bounds
if entryBounds.Dx() != width || entryBounds.Dy() != entryHeight {
if entryBounds.Dx() != bounds.Dx() || entryBounds.Dy() != entryHeight {
entry.Bounds.Max = entryBounds.Min.Add (
image.Pt(width, entryHeight))
image.Pt(bounds.Dx(), entryHeight))
}
entries[index] = entry
}
}