From ff3fc7a24a9f28a1c378e74a20d711c4fcf2376b Mon Sep 17 00:00:00 2001 From: Elias Naur Date: Thu, 10 Oct 2019 12:46:30 +0200 Subject: [PATCH] widget,text: move Label and Editor from text to widget package Signed-off-by: Elias Naur --- text/text.go | 44 --------------------------- {text => widget}/buffer.go | 2 +- {text => widget}/editor.go | 17 ++++++----- {text => widget}/label.go | 61 +++++++++++++++++++++++++++++++------- 4 files changed, 61 insertions(+), 63 deletions(-) rename {text => widget}/buffer.go (99%) rename {text => widget}/editor.go (96%) rename {text => widget}/label.go (69%) diff --git a/text/text.go b/text/text.go index 01b5fd32..187d8314 100644 --- a/text/text.go +++ b/text/text.go @@ -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: diff --git a/text/buffer.go b/widget/buffer.go similarity index 99% rename from text/buffer.go rename to widget/buffer.go index 6151f6f9..22e84517 100644 --- a/text/buffer.go +++ b/widget/buffer.go @@ -1,6 +1,6 @@ // SPDX-License-Identifier: Unlicense OR MIT -package text +package widget import ( "fmt" diff --git a/text/editor.go b/widget/editor.go similarity index 96% rename from text/editor.go rename to widget/editor.go index f9db842a..b9038257 100644 --- a/text/editor.go +++ b/widget/editor.go @@ -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) diff --git a/text/label.go b/widget/label.go similarity index 69% rename from text/label.go rename to widget/label.go index c21eeb76..7b492f74 100644 --- a/text/label.go +++ b/widget/label.go @@ -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)) + } +}