widget,text: move Label and Editor from text to widget package

Signed-off-by: Elias Naur <mail@eliasnaur.com>
This commit is contained in:
Elias Naur
2019-10-10 12:46:30 +02:00
parent 607d853b58
commit ff3fc7a24a
4 changed files with 61 additions and 63 deletions
-44
View File
@@ -3,10 +3,6 @@
package text
import (
"fmt"
"image"
"gioui.org/layout"
"gioui.org/op/paint"
"gioui.org/unit"
"golang.org/x/image/math/fixed"
@@ -87,46 +83,6 @@ const (
Bold Weight = 700
)
func linesDimens(lines []Line) layout.Dimensions {
var width fixed.Int26_6
var h int
var baseline int
if len(lines) > 0 {
baseline = lines[0].Ascent.Ceil()
var prevDesc fixed.Int26_6
for _, l := range lines {
h += (prevDesc + l.Ascent).Ceil()
prevDesc = l.Descent
if l.Width > width {
width = l.Width
}
}
h += lines[len(lines)-1].Descent.Ceil()
}
w := width.Ceil()
return layout.Dimensions{
Size: image.Point{
X: w,
Y: h,
},
Baseline: baseline,
}
}
func align(align Alignment, width fixed.Int26_6, maxWidth int) fixed.Int26_6 {
mw := fixed.I(maxWidth)
switch align {
case Middle:
return fixed.I(((mw - width) / 2).Floor())
case End:
return fixed.I((mw - width).Floor())
case Start:
return 0
default:
panic(fmt.Errorf("unknown alignment %v", align))
}
}
func (a Alignment) String() string {
switch a {
case Start:
+1 -1
View File
@@ -1,6 +1,6 @@
// SPDX-License-Identifier: Unlicense OR MIT
package text
package widget
import (
"fmt"
+9 -8
View File
@@ -1,6 +1,6 @@
// SPDX-License-Identifier: Unlicense OR MIT
package text
package widget
import (
"image"
@@ -15,6 +15,7 @@ import (
"gioui.org/layout"
"gioui.org/op"
"gioui.org/op/paint"
"gioui.org/text"
"gioui.org/unit"
"golang.org/x/image/math/fixed"
@@ -22,7 +23,7 @@ import (
// Editor implements an editable and scrollable text area.
type Editor struct {
Alignment Alignment
Alignment text.Alignment
// SingleLine force the text to stay on a single line.
// SingleLine also sets the scrolling direction to
// horizontal.
@@ -33,14 +34,14 @@ type Editor struct {
eventKey int
scale int
font Font
font text.Font
blinkStart time.Time
focused bool
rr editBuffer
maxWidth int
viewSize image.Point
valid bool
lines []Line
lines []text.Line
shapes []line
dims layout.Dimensions
carWidth fixed.Int26_6
@@ -175,7 +176,7 @@ func (e *Editor) Focus() {
}
// Layout lays out the editor.
func (e *Editor) Layout(gtx *layout.Context, sh *Shaper, font Font) {
func (e *Editor) Layout(gtx *layout.Context, sh *text.Shaper, font text.Font) {
if e.font != font {
e.invalidate()
e.font = font
@@ -187,7 +188,7 @@ func (e *Editor) Layout(gtx *layout.Context, sh *Shaper, font Font) {
}
}
func (e *Editor) layout(gtx *layout.Context, sh *Shaper) {
func (e *Editor) layout(gtx *layout.Context, sh *text.Shaper) {
// Crude configuration change detection.
if scale := gtx.Px(unit.Sp(100)); scale != e.scale {
e.invalidate()
@@ -391,9 +392,9 @@ func (e *Editor) moveCoord(c unit.Converter, pos image.Point) {
e.moveToLine(x, carLine)
}
func (e *Editor) layoutText(c unit.Converter, s *Shaper, font Font) ([]Line, layout.Dimensions) {
func (e *Editor) layoutText(c unit.Converter, s *text.Shaper, font text.Font) ([]text.Line, layout.Dimensions) {
txt := e.rr.String()
opts := LayoutOptions{SingleLine: e.SingleLine, MaxWidth: e.maxWidth}
opts := text.LayoutOptions{SingleLine: e.SingleLine, MaxWidth: e.maxWidth}
textLayout := s.Layout(c, font, txt, opts)
lines := textLayout.Lines
dims := linesDimens(lines)
+51 -10
View File
@@ -1,9 +1,9 @@
// SPDX-License-Identifier: Unlicense OR MIT
// Package text implements text widgets.
package text
package widget
import (
"fmt"
"image"
"unicode/utf8"
@@ -11,6 +11,7 @@ import (
"gioui.org/layout"
"gioui.org/op"
"gioui.org/op/paint"
"gioui.org/text"
"golang.org/x/image/math/fixed"
)
@@ -18,15 +19,15 @@ import (
// Label is a widget for laying out and drawing text.
type Label struct {
// Alignment specify the text alignment.
Alignment Alignment
Alignment text.Alignment
// MaxLines limits the number of lines. Zero means no limit.
MaxLines int
}
type lineIterator struct {
Lines []Line
Lines []text.Line
Clip image.Rectangle
Alignment Alignment
Alignment text.Alignment
Width int
Offset image.Point
@@ -35,7 +36,7 @@ type lineIterator struct {
const inf = 1e6
func (l *lineIterator) Next() (String, f32.Point, bool) {
func (l *lineIterator) Next() (text.String, f32.Point, bool) {
for len(l.Lines) > 0 {
line := l.Lines[0]
l.Lines = l.Lines[1:]
@@ -78,12 +79,12 @@ func (l *lineIterator) Next() (String, f32.Point, bool) {
offf := f32.Point{X: float32(off.X) / 64, Y: float32(off.Y) / 64}
return str, offf, true
}
return String{}, f32.Point{}, false
return text.String{}, f32.Point{}, false
}
func (l Label) Layout(gtx *layout.Context, s *Shaper, font Font, txt string) {
func (l Label) Layout(gtx *layout.Context, s *text.Shaper, font text.Font, txt string) {
cs := gtx.Constraints
textLayout := s.Layout(gtx, font, txt, LayoutOptions{MaxWidth: cs.Width.Max})
textLayout := s.Layout(gtx, font, txt, text.LayoutOptions{MaxWidth: cs.Width.Max})
lines := textLayout.Lines
if max := l.MaxLines; max > 0 && len(lines) > max {
lines = lines[:max]
@@ -121,7 +122,7 @@ func toRectF(r image.Rectangle) f32.Rectangle {
}
}
func textPadding(lines []Line) (padding image.Rectangle) {
func textPadding(lines []text.Line) (padding image.Rectangle) {
if len(lines) == 0 {
return
}
@@ -141,3 +142,43 @@ func textPadding(lines []Line) (padding image.Rectangle) {
}
return
}
func linesDimens(lines []text.Line) layout.Dimensions {
var width fixed.Int26_6
var h int
var baseline int
if len(lines) > 0 {
baseline = lines[0].Ascent.Ceil()
var prevDesc fixed.Int26_6
for _, l := range lines {
h += (prevDesc + l.Ascent).Ceil()
prevDesc = l.Descent
if l.Width > width {
width = l.Width
}
}
h += lines[len(lines)-1].Descent.Ceil()
}
w := width.Ceil()
return layout.Dimensions{
Size: image.Point{
X: w,
Y: h,
},
Baseline: baseline,
}
}
func align(align text.Alignment, width fixed.Int26_6, maxWidth int) fixed.Int26_6 {
mw := fixed.I(maxWidth)
switch align {
case text.Middle:
return fixed.I(((mw - width) / 2).Floor())
case text.End:
return fixed.I((mw - width).Floor())
case text.Start:
return 0
default:
panic(fmt.Errorf("unknown alignment %v", align))
}
}