Half-working container flexibility propagation

This commit is contained in:
2023-01-16 23:34:17 -05:00
parent 76d50bb01a
commit e94e170a04
8 changed files with 91 additions and 34 deletions

View File

@@ -18,6 +18,7 @@ type Container struct {
warping bool
selected bool
selectable bool
flexible bool
}
// NewContainer creates a new container.
@@ -49,6 +50,7 @@ func (element *Container) Adopt (child tomo.Element, expand bool) {
MinimumSizeChange: func (int, int) {
element.updateMinimumSize()
},
FlexibleHeightChange: element.updateMinimumSize,
SelectionRequest: func () (granted bool) {
child, selectable := child.(tomo.Selectable)
if !selectable { return }
@@ -68,7 +70,7 @@ func (element *Container) Adopt (child tomo.Element, expand bool) {
})
element.updateMinimumSize()
element.updateSelectable()
element.reflectChildProperties()
if element.core.HasImage() && !element.warping {
element.recalculate()
element.draw()
@@ -113,7 +115,7 @@ func (element *Container) Disown (child tomo.Element) {
}
element.updateMinimumSize()
element.updateSelectable()
element.reflectChildProperties()
if element.core.HasImage() && !element.warping {
element.recalculate()
element.draw()
@@ -126,7 +128,7 @@ func (element *Container) DisownAll () {
element.children = nil
element.updateMinimumSize()
element.updateSelectable()
element.reflectChildProperties()
if element.core.HasImage() && !element.warping {
element.recalculate()
element.draw()
@@ -183,8 +185,6 @@ func (element *Container) Resize (width, height int) {
element.draw()
}
// TODO: implement KeyboardTarget
func (element *Container) HandleMouseDown (x, y int, button tomo.Button) {
child, handlesMouse := element.ChildAt(image.Pt(x, y)).(tomo.MouseTarget)
if !handlesMouse { return }
@@ -230,7 +230,7 @@ func (element *Container) HandleKeyDown (
})
}
func (element *Container) HandleKeyUp(key tomo.Key, modifiers tomo.Modifiers) {
func (element *Container) HandleKeyUp (key tomo.Key, modifiers tomo.Modifiers) {
element.forSelected (func (child tomo.Selectable) bool {
child0, handlesKeyboard := child.(tomo.KeyboardTarget)
if handlesKeyboard {
@@ -300,6 +300,11 @@ func (element *Container) HandleSelection (direction tomo.SelectionDirection) (o
return false
}
// TODO: fix this!
// func (element *Container) MinimumHeightFor (width int) (height int) {
// return element.layout.MinimumHeightFor(element.children, width)
// }
func (element *Container) HandleDeselection () {
element.selected = false
element.forSelected (func (child tomo.Selectable) bool {
@@ -326,6 +331,15 @@ func (element *Container) forSelectable (callback func (child tomo.Selectable) b
}
}
func (element *Container) forFlexible (callback func (child tomo.Flexible) bool) {
for _, entry := range element.children {
child, selectable := entry.Element.(tomo.Flexible)
if selectable {
if !callback(child) { break }
}
}
}
func (element *Container) forSelectableBackward (callback func (child tomo.Selectable) bool) {
for index := len(element.children) - 1; index >= 0; index -- {
child, selectable := element.children[index].Element.(tomo.Selectable)
@@ -345,12 +359,17 @@ func (element *Container) firstSelected () (index int) {
return -1
}
func (element *Container) updateSelectable () {
func (element *Container) reflectChildProperties () {
element.selectable = false
element.forSelectable (func (tomo.Selectable) bool {
element.selectable = true
return false
})
element.flexible = false
element.forFlexible (func (tomo.Flexible) bool {
element.flexible = true
return false
})
if !element.selectable {
element.selected = false
}
@@ -374,8 +393,11 @@ func (element *Container) childSelectionRequestCallback (
}
func (element *Container) updateMinimumSize () {
element.core.SetMinimumSize (
element.layout.MinimumSize(element.children, 1e9))
width, height := element.layout.MinimumSize(element.children)
if element.flexible {
height = element.layout.MinimumHeightFor(element.children, width)
}
element.core.SetMinimumSize(width, height)
}
func (element *Container) recalculate () {

View File

@@ -88,7 +88,7 @@ func (element *Label) updateMinimumSize () {
if em < 1 { em = theme.Padding() }
element.core.SetMinimumSize (
em, element.drawer.LineHeight().Round())
element.core.NotifyExpandingHeightChange()
element.core.NotifyFlexibleHeightChange()
} else {
bounds := element.drawer.LayoutBounds()
element.core.SetMinimumSize(bounds.Dx(), bounds.Dy())