mirror of
https://git.sr.ht/~eliasnaur/gio
synced 2026-07-04 00:45:35 +00:00
text: simplify text layout and shaping API
First, replace LayoutOptions with an explicit maximum width parameter. The single-field option struct doesn't carry its weight, and I don't think we'll see more global layout options in the future. Rather, I expect options to cover spans of text or be part of a Font. Second, replace the unit.Converter with an scaled text size. It's simpler and allow the Editor and similar widgets to easily detect whether their cached layouts are stale. Package text no longer depends on package unit, which is now dealt with at the widget-level only. Finally, remove the Size field from Font. It was a design mistake: a Font is assumed to cover all sizes, as evidenced by the FontRegistry disregarding Size when looking up fonts. Signed-off-by: Elias Naur <mail@eliasnaur.com>
This commit is contained in:
+9
-7
@@ -36,6 +36,7 @@ type Editor struct {
|
||||
eventKey int
|
||||
scale int
|
||||
font text.Font
|
||||
textSize fixed.Int26_6
|
||||
blinkStart time.Time
|
||||
focused bool
|
||||
rr editBuffer
|
||||
@@ -213,14 +214,16 @@ func (e *Editor) Focus() {
|
||||
}
|
||||
|
||||
// Layout lays out the editor.
|
||||
func (e *Editor) Layout(gtx *layout.Context, sh text.Shaper, font text.Font) {
|
||||
func (e *Editor) Layout(gtx *layout.Context, sh text.Shaper, font text.Font, size unit.Value) {
|
||||
// Flush events from before the previous frame.
|
||||
copy(e.events, e.events[e.prevEvents:])
|
||||
e.events = e.events[:len(e.events)-e.prevEvents]
|
||||
e.prevEvents = len(e.events)
|
||||
if e.font != font {
|
||||
textSize := fixed.I(gtx.Px(size))
|
||||
if e.font != font || e.textSize != textSize {
|
||||
e.invalidate()
|
||||
e.font = font
|
||||
e.textSize = textSize
|
||||
}
|
||||
e.processEvents(gtx)
|
||||
e.layout(gtx, sh)
|
||||
@@ -245,7 +248,7 @@ func (e *Editor) layout(gtx *layout.Context, sh text.Shaper) {
|
||||
}
|
||||
|
||||
if !e.valid {
|
||||
e.lines, e.dims = e.layoutText(gtx, sh, e.font)
|
||||
e.lines, e.dims = e.layoutText(sh)
|
||||
e.valid = true
|
||||
}
|
||||
|
||||
@@ -277,7 +280,7 @@ func (e *Editor) layout(gtx *layout.Context, sh text.Shaper) {
|
||||
if !ok {
|
||||
break
|
||||
}
|
||||
path := sh.Shape(gtx, e.font, layout)
|
||||
path := sh.Shape(e.font, e.textSize, layout)
|
||||
e.shapes = append(e.shapes, line{off, path})
|
||||
}
|
||||
|
||||
@@ -430,10 +433,9 @@ func (e *Editor) moveCoord(c unit.Converter, pos image.Point) {
|
||||
e.moveToLine(x, carLine)
|
||||
}
|
||||
|
||||
func (e *Editor) layoutText(c unit.Converter, s text.Shaper, font text.Font) ([]text.Line, layout.Dimensions) {
|
||||
func (e *Editor) layoutText(s text.Shaper) ([]text.Line, layout.Dimensions) {
|
||||
e.rr.Reset()
|
||||
opts := text.LayoutOptions{MaxWidth: e.maxWidth}
|
||||
lines, _ := s.Layout(c, font, &e.rr, opts)
|
||||
lines, _ := s.Layout(e.font, e.textSize, e.maxWidth, &e.rr)
|
||||
dims := linesDimens(lines)
|
||||
for i := 0; i < len(lines)-1; i++ {
|
||||
// To avoid layout flickering while editing, assume a soft newline takes
|
||||
|
||||
+5
-3
@@ -12,6 +12,7 @@ import (
|
||||
"gioui.org/op"
|
||||
"gioui.org/op/paint"
|
||||
"gioui.org/text"
|
||||
"gioui.org/unit"
|
||||
|
||||
"golang.org/x/image/math/fixed"
|
||||
)
|
||||
@@ -83,9 +84,10 @@ func (l *lineIterator) Next() (int, int, []text.Glyph, f32.Point, bool) {
|
||||
return 0, 0, nil, f32.Point{}, false
|
||||
}
|
||||
|
||||
func (l Label) Layout(gtx *layout.Context, s text.Shaper, font text.Font, txt string) {
|
||||
func (l Label) Layout(gtx *layout.Context, s text.Shaper, font text.Font, size unit.Value, txt string) {
|
||||
cs := gtx.Constraints
|
||||
lines := s.LayoutString(gtx, font, txt, text.LayoutOptions{MaxWidth: cs.Width.Max})
|
||||
textSize := fixed.I(gtx.Px(size))
|
||||
lines := s.LayoutString(font, textSize, cs.Width.Max, txt)
|
||||
if max := l.MaxLines; max > 0 && len(lines) > max {
|
||||
lines = lines[:max]
|
||||
}
|
||||
@@ -109,7 +111,7 @@ func (l Label) Layout(gtx *layout.Context, s text.Shaper, font text.Font, txt st
|
||||
stack.Push(gtx.Ops)
|
||||
op.TransformOp{}.Offset(off).Add(gtx.Ops)
|
||||
str := txt[start:end]
|
||||
s.ShapeString(gtx, font, str, layout).Add(gtx.Ops)
|
||||
s.ShapeString(font, textSize, str, layout).Add(gtx.Ops)
|
||||
paint.PaintOp{Rect: lclip}.Add(gtx.Ops)
|
||||
stack.Pop()
|
||||
}
|
||||
|
||||
@@ -22,6 +22,7 @@ type Button struct {
|
||||
// Color is the text color.
|
||||
Color color.RGBA
|
||||
Font text.Font
|
||||
TextSize unit.Value
|
||||
Background color.RGBA
|
||||
CornerRadius unit.Value
|
||||
shaper text.Shaper
|
||||
@@ -40,10 +41,8 @@ func (t *Theme) Button(txt string) Button {
|
||||
Text: txt,
|
||||
Color: rgb(0xffffff),
|
||||
Background: t.Color.Primary,
|
||||
Font: text.Font{
|
||||
Size: t.TextSize.Scale(14.0 / 16.0),
|
||||
},
|
||||
shaper: t.Shaper,
|
||||
TextSize: t.TextSize.Scale(14.0 / 16.0),
|
||||
shaper: t.Shaper,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -83,7 +82,7 @@ func (b Button) Layout(gtx *layout.Context, button *widget.Button) {
|
||||
layout.Center.Layout(gtx, func() {
|
||||
layout.Inset{Top: unit.Dp(10), Bottom: unit.Dp(10), Left: unit.Dp(12), Right: unit.Dp(12)}.Layout(gtx, func() {
|
||||
paint.ColorOp{Color: col}.Add(gtx.Ops)
|
||||
widget.Label{}.Layout(gtx, b.shaper, b.Font, b.Text)
|
||||
widget.Label{}.Layout(gtx, b.shaper, b.Font, b.TextSize, b.Text)
|
||||
})
|
||||
})
|
||||
pointer.Rect(image.Rectangle{Max: gtx.Dimensions.Size}).Add(gtx.Ops)
|
||||
|
||||
@@ -18,6 +18,7 @@ type checkable struct {
|
||||
Label string
|
||||
Color color.RGBA
|
||||
Font text.Font
|
||||
TextSize unit.Value
|
||||
IconColor color.RGBA
|
||||
Size unit.Value
|
||||
shaper text.Shaper
|
||||
@@ -56,7 +57,7 @@ func (c *checkable) layout(gtx *layout.Context, checked bool) {
|
||||
layout.W.Layout(gtx, func() {
|
||||
layout.UniformInset(unit.Dp(2)).Layout(gtx, func() {
|
||||
paint.ColorOp{Color: c.Color}.Add(gtx.Ops)
|
||||
widget.Label{}.Layout(gtx, c.shaper, c.Font, c.Label)
|
||||
widget.Label{}.Layout(gtx, c.shaper, c.Font, c.TextSize, c.Label)
|
||||
})
|
||||
})
|
||||
}),
|
||||
|
||||
@@ -4,7 +4,6 @@ package material
|
||||
|
||||
import (
|
||||
"gioui.org/layout"
|
||||
"gioui.org/text"
|
||||
"gioui.org/unit"
|
||||
"gioui.org/widget"
|
||||
)
|
||||
@@ -16,12 +15,10 @@ type CheckBox struct {
|
||||
func (t *Theme) CheckBox(label string) CheckBox {
|
||||
return CheckBox{
|
||||
checkable{
|
||||
Label: label,
|
||||
Color: t.Color.Text,
|
||||
IconColor: t.Color.Primary,
|
||||
Font: text.Font{
|
||||
Size: t.TextSize.Scale(14.0 / 16.0),
|
||||
},
|
||||
Label: label,
|
||||
Color: t.Color.Text,
|
||||
IconColor: t.Color.Primary,
|
||||
TextSize: t.TextSize.Scale(14.0 / 16.0),
|
||||
Size: unit.Dp(26),
|
||||
shaper: t.Shaper,
|
||||
checkedStateIcon: t.checkBoxCheckedIcon,
|
||||
|
||||
@@ -9,11 +9,13 @@ import (
|
||||
"gioui.org/op"
|
||||
"gioui.org/op/paint"
|
||||
"gioui.org/text"
|
||||
"gioui.org/unit"
|
||||
"gioui.org/widget"
|
||||
)
|
||||
|
||||
type Editor struct {
|
||||
Font text.Font
|
||||
Font text.Font
|
||||
TextSize unit.Value
|
||||
// Color is the text color.
|
||||
Color color.RGBA
|
||||
// Hint contains the text displayed when the editor is empty.
|
||||
@@ -26,9 +28,7 @@ type Editor struct {
|
||||
|
||||
func (t *Theme) Editor(hint string) Editor {
|
||||
return Editor{
|
||||
Font: text.Font{
|
||||
Size: t.TextSize,
|
||||
},
|
||||
TextSize: t.TextSize,
|
||||
Color: t.Color.Text,
|
||||
shaper: t.Shaper,
|
||||
Hint: hint,
|
||||
@@ -43,7 +43,7 @@ func (e Editor) Layout(gtx *layout.Context, editor *widget.Editor) {
|
||||
macro.Record(gtx.Ops)
|
||||
paint.ColorOp{Color: e.HintColor}.Add(gtx.Ops)
|
||||
tl := widget.Label{Alignment: editor.Alignment}
|
||||
tl.Layout(gtx, e.shaper, e.Font, e.Hint)
|
||||
tl.Layout(gtx, e.shaper, e.Font, e.TextSize, e.Hint)
|
||||
macro.Stop()
|
||||
if w := gtx.Dimensions.Size.X; gtx.Constraints.Width.Min < w {
|
||||
gtx.Constraints.Width.Min = w
|
||||
@@ -51,7 +51,7 @@ func (e Editor) Layout(gtx *layout.Context, editor *widget.Editor) {
|
||||
if h := gtx.Dimensions.Size.Y; gtx.Constraints.Height.Min < h {
|
||||
gtx.Constraints.Height.Min = h
|
||||
}
|
||||
editor.Layout(gtx, e.shaper, e.Font)
|
||||
editor.Layout(gtx, e.shaper, e.Font, e.TextSize)
|
||||
if editor.Len() > 0 {
|
||||
paint.ColorOp{Color: e.Color}.Add(gtx.Ops)
|
||||
editor.PaintText(gtx)
|
||||
|
||||
@@ -22,6 +22,7 @@ type Label struct {
|
||||
// MaxLines limits the number of lines. Zero means no limit.
|
||||
MaxLines int
|
||||
Text string
|
||||
TextSize unit.Value
|
||||
|
||||
shaper text.Shaper
|
||||
}
|
||||
@@ -64,17 +65,15 @@ func (t *Theme) Caption(txt string) Label {
|
||||
|
||||
func (t *Theme) Label(size unit.Value, txt string) Label {
|
||||
return Label{
|
||||
Text: txt,
|
||||
Color: t.Color.Text,
|
||||
Font: text.Font{
|
||||
Size: size,
|
||||
},
|
||||
shaper: t.Shaper,
|
||||
Text: txt,
|
||||
Color: t.Color.Text,
|
||||
TextSize: size,
|
||||
shaper: t.Shaper,
|
||||
}
|
||||
}
|
||||
|
||||
func (l Label) Layout(gtx *layout.Context) {
|
||||
paint.ColorOp{Color: l.Color}.Add(gtx.Ops)
|
||||
tl := widget.Label{Alignment: l.Alignment, MaxLines: l.MaxLines}
|
||||
tl.Layout(gtx, l.shaper, l.Font, l.Text)
|
||||
tl.Layout(gtx, l.shaper, l.Font, l.TextSize, l.Text)
|
||||
}
|
||||
|
||||
@@ -4,7 +4,6 @@ package material
|
||||
|
||||
import (
|
||||
"gioui.org/layout"
|
||||
"gioui.org/text"
|
||||
"gioui.org/unit"
|
||||
"gioui.org/widget"
|
||||
)
|
||||
@@ -21,11 +20,9 @@ func (t *Theme) RadioButton(key, label string) RadioButton {
|
||||
checkable: checkable{
|
||||
Label: label,
|
||||
|
||||
Color: t.Color.Text,
|
||||
IconColor: t.Color.Primary,
|
||||
Font: text.Font{
|
||||
Size: t.TextSize.Scale(14.0 / 16.0),
|
||||
},
|
||||
Color: t.Color.Text,
|
||||
IconColor: t.Color.Primary,
|
||||
TextSize: t.TextSize.Scale(14.0 / 16.0),
|
||||
Size: unit.Dp(26),
|
||||
shaper: t.Shaper,
|
||||
checkedStateIcon: t.radioCheckedIcon,
|
||||
|
||||
Reference in New Issue
Block a user