Labels may request an expanding height change
This commit is contained in:
parent
7754679710
commit
d9281b139f
@ -17,6 +17,7 @@ type wordLayout struct {
|
|||||||
position image.Point
|
position image.Point
|
||||||
width int
|
width int
|
||||||
spaceAfter int
|
spaceAfter int
|
||||||
|
breaksAfter int
|
||||||
text []characterLayout
|
text []characterLayout
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -167,13 +168,16 @@ func (drawer *TextDrawer) LineHeight () (height fixed.Int26_6) {
|
|||||||
func (drawer *TextDrawer) ReccomendedHeightFor (width int) (height int) {
|
func (drawer *TextDrawer) ReccomendedHeightFor (width int) (height int) {
|
||||||
if !drawer.layoutClean { drawer.recalculate() }
|
if !drawer.layoutClean { drawer.recalculate() }
|
||||||
metrics := drawer.face.Metrics()
|
metrics := drawer.face.Metrics()
|
||||||
dot := fixed.Point26_6 { 0, 0 }
|
dot := fixed.Point26_6 { 0, metrics.Height }
|
||||||
for _, word := range drawer.layout {
|
for _, word := range drawer.layout {
|
||||||
dot.X += fixed.Int26_6((word.width + word.spaceAfter) << 6)
|
if word.width + dot.X.Round() > width {
|
||||||
|
|
||||||
if word.width + word.position.X > width && word.position.X > 0 {
|
|
||||||
dot.Y += metrics.Height
|
dot.Y += metrics.Height
|
||||||
dot.X = fixed.Int26_6(word.width << 6)
|
dot.X = 0
|
||||||
|
}
|
||||||
|
dot.X += fixed.I(word.width + word.spaceAfter)
|
||||||
|
if word.breaksAfter > 0 {
|
||||||
|
dot.Y += fixed.I(word.breaksAfter).Mul(metrics.Height)
|
||||||
|
dot.X = 0
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -246,6 +250,7 @@ func (drawer *TextDrawer) recalculate () {
|
|||||||
if character == '\n' {
|
if character == '\n' {
|
||||||
dot.Y += metrics.Height
|
dot.Y += metrics.Height
|
||||||
dot.X = 0
|
dot.X = 0
|
||||||
|
word.breaksAfter ++
|
||||||
previousCharacter = character
|
previousCharacter = character
|
||||||
index ++
|
index ++
|
||||||
} else {
|
} else {
|
||||||
@ -290,7 +295,6 @@ func (drawer *TextDrawer) recalculate () {
|
|||||||
|
|
||||||
if drawer.wrap {
|
if drawer.wrap {
|
||||||
drawer.layoutBounds.Max.X = drawer.width
|
drawer.layoutBounds.Max.X = drawer.width
|
||||||
println("aaa")
|
|
||||||
} else {
|
} else {
|
||||||
drawer.layoutBounds.Max.X = horizontalExtent
|
drawer.layoutBounds.Max.X = horizontalExtent
|
||||||
}
|
}
|
||||||
|
|||||||
@ -89,6 +89,7 @@ func (window *Window) Adopt (child tomo.Element) {
|
|||||||
child.SetParentHooks (tomo.ParentHooks {
|
child.SetParentHooks (tomo.ParentHooks {
|
||||||
Draw: window.childDrawCallback,
|
Draw: window.childDrawCallback,
|
||||||
MinimumSizeChange: window.childMinimumSizeChangeCallback,
|
MinimumSizeChange: window.childMinimumSizeChangeCallback,
|
||||||
|
ExpandingHeightChange: window.resizeChildToFit,
|
||||||
SelectionRequest: window.childSelectionRequestCallback,
|
SelectionRequest: window.childSelectionRequestCallback,
|
||||||
})
|
})
|
||||||
|
|
||||||
@ -205,8 +206,25 @@ func (window *Window) resizeChildToFit () {
|
|||||||
if child, ok := window.child.(tomo.Expanding); ok {
|
if child, ok := window.child.(tomo.Expanding); ok {
|
||||||
minimumHeight := child.MinimumHeightFor(window.metrics.width)
|
minimumHeight := child.MinimumHeightFor(window.metrics.width)
|
||||||
_, minimumWidth := child.MinimumSize()
|
_, minimumWidth := child.MinimumSize()
|
||||||
window.childMinimumSizeChangeCallback (
|
|
||||||
minimumWidth, minimumHeight)
|
|
||||||
|
icccm.WmNormalHintsSet (
|
||||||
|
window.backend.connection,
|
||||||
|
window.xWindow.Id,
|
||||||
|
&icccm.NormalHints {
|
||||||
|
Flags: icccm.SizeHintPMinSize,
|
||||||
|
MinWidth: uint(minimumWidth),
|
||||||
|
MinHeight: uint(minimumHeight),
|
||||||
|
})
|
||||||
|
|
||||||
|
if window.metrics.height >= minimumHeight &&
|
||||||
|
window.metrics.width >= minimumWidth {
|
||||||
|
|
||||||
|
window.child.Resize (
|
||||||
|
window.metrics.width,
|
||||||
|
window.metrics.height)
|
||||||
|
window.redrawChildEntirely()
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
window.child.Resize (
|
window.child.Resize (
|
||||||
window.metrics.width,
|
window.metrics.width,
|
||||||
|
|||||||
12
element.go
12
element.go
@ -16,6 +16,10 @@ type ParentHooks struct {
|
|||||||
// event.
|
// event.
|
||||||
MinimumSizeChange func (width, height int)
|
MinimumSizeChange func (width, height int)
|
||||||
|
|
||||||
|
// ExpandingHeightChange is called when the parameters affecting the
|
||||||
|
// element's expanding height have changed.
|
||||||
|
ExpandingHeightChange func ()
|
||||||
|
|
||||||
// SelectionRequest is called when the child element element wants
|
// SelectionRequest is called when the child element element wants
|
||||||
// itself to be selected. If the parent element chooses to grant the
|
// itself to be selected. If the parent element chooses to grant the
|
||||||
// request, it must send the child element a selection event and return
|
// request, it must send the child element a selection event and return
|
||||||
@ -42,6 +46,14 @@ func (hooks ParentHooks) RunMinimumSizeChange (width, height int) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// RunExpandingHeightChange runs the ExpandingHeightChange hook if it is not
|
||||||
|
// nil. If it is nil, it does nothing.
|
||||||
|
func (hooks ParentHooks) RunExpandingHeightChange () {
|
||||||
|
if hooks.ExpandingHeightChange != nil {
|
||||||
|
hooks.ExpandingHeightChange()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// RunSelectionRequest runs the SelectionRequest hook if it is not nil. If it is
|
// RunSelectionRequest runs the SelectionRequest hook if it is not nil. If it is
|
||||||
// nil, it does nothing.
|
// nil, it does nothing.
|
||||||
func (hooks ParentHooks) RunSelectionRequest () (granted bool) {
|
func (hooks ParentHooks) RunSelectionRequest () (granted bool) {
|
||||||
|
|||||||
@ -27,6 +27,7 @@ func NewLabel (text string, wrap bool) (element *Label) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Resize resizes the label and re-wraps the text if wrapping is enabled.
|
||||||
func (element *Label) Resize (width, height int) {
|
func (element *Label) Resize (width, height int) {
|
||||||
element.core.AllocateCanvas(width, height)
|
element.core.AllocateCanvas(width, height)
|
||||||
if element.wrap {
|
if element.wrap {
|
||||||