widget: [API] add content widget argument to Editor.Layout

To make the semantic relation between the editor and its content clear,
the editor clip operation must cover the content. This change adds an
explicit widget argument to editor, and lays it out inside the clip
rect.

This is an API change. Users of Editor.Layout must provide a content
widget.

Signed-off-by: Elias Naur <mail@eliasnaur.com>
This commit is contained in:
Elias Naur
2021-11-29 17:13:39 +01:00
parent 665e23693f
commit ac97b9d6e1
3 changed files with 36 additions and 31 deletions
+7 -4
View File
@@ -462,8 +462,8 @@ func (e *Editor) Focused() bool {
return e.focused
}
// Layout lays out the editor.
func (e *Editor) Layout(gtx layout.Context, sh text.Shaper, font text.Font, size unit.Value) layout.Dimensions {
// Layout lays out the editor. If content is not nil, it is laid out on top.
func (e *Editor) Layout(gtx layout.Context, sh text.Shaper, font text.Font, size unit.Value, content layout.Widget) layout.Dimensions {
textSize := fixed.I(gtx.Px(size))
if e.font != font || e.textSize != textSize {
e.invalidate()
@@ -497,10 +497,10 @@ func (e *Editor) Layout(gtx layout.Context, sh text.Shaper, font text.Font, size
}
e.makeValid()
return e.layout(gtx)
return e.layout(gtx, content)
}
func (e *Editor) layout(gtx layout.Context) layout.Dimensions {
func (e *Editor) layout(gtx layout.Context, content layout.Widget) layout.Dimensions {
// Adjust scrolling for new viewport and layout.
e.scrollRel(0, 0)
@@ -576,6 +576,9 @@ func (e *Editor) layout(gtx layout.Context) layout.Dimensions {
e.caret.on = e.focused && (!blinking || dt%timePerBlink < timePerBlink/2)
}
if content != nil {
content(gtx)
}
return layout.Dimensions{Size: e.viewSize, Baseline: e.dims.Baseline}
}