mirror of
https://git.sr.ht/~eliasnaur/gio
synced 2026-07-04 00:45:35 +00:00
ui/text: rename Center Alignment to Middle and drop IsNewline
Middle matches the similar layout.Middle constant, and IsNewline is too simple to export. Add documentation while we're here. Signed-off-by: Elias Naur <mail@eliasnaur.com>
This commit is contained in:
@@ -179,7 +179,7 @@ func layoutText(ppem fixed.Int26_6, str string, f *opentype, opts text.LayoutOpt
|
||||
}
|
||||
for prev.idx < len(str) {
|
||||
c, s := utf8.DecodeRuneInString(str[prev.idx:])
|
||||
nl := text.IsNewline(c)
|
||||
nl := c == '\n'
|
||||
if opts.SingleLine && nl {
|
||||
nl = false
|
||||
c = ' '
|
||||
|
||||
+1
-1
@@ -334,7 +334,7 @@ func (e *Editor) layoutText() {
|
||||
// up all available space.
|
||||
if len(s) > 0 {
|
||||
r, _ := utf8.DecodeLastRuneInString(s)
|
||||
if !IsNewline(r) {
|
||||
if r != '\n' {
|
||||
dims.Size.X = e.maxWidth
|
||||
break
|
||||
}
|
||||
|
||||
+13
-4
@@ -1,5 +1,6 @@
|
||||
// SPDX-License-Identifier: Unlicense OR MIT
|
||||
|
||||
// Package text implements text widgets.
|
||||
package text
|
||||
|
||||
import (
|
||||
@@ -15,12 +16,20 @@ import (
|
||||
"golang.org/x/image/math/fixed"
|
||||
)
|
||||
|
||||
// Label is a widget for laying out and drawing text.
|
||||
type Label struct {
|
||||
Face Face
|
||||
Material ui.MacroOp
|
||||
// Face define the font and style of the text.
|
||||
Face Face
|
||||
// Material is a macro recording the material to draw the
|
||||
// text. Use a ColorOp for colored text.
|
||||
Material ui.MacroOp
|
||||
// Alignment specify the text alignment.
|
||||
Alignment Alignment
|
||||
Text string
|
||||
MaxLines int
|
||||
// Text is the string to draw.
|
||||
Text string
|
||||
// MaxLines specify the maximum number of lines the text
|
||||
// may fill.
|
||||
MaxLines int
|
||||
|
||||
it lineIterator
|
||||
}
|
||||
|
||||
+29
-10
@@ -11,6 +11,7 @@ import (
|
||||
"golang.org/x/image/math/fixed"
|
||||
)
|
||||
|
||||
// A Line contains the measurements of a line of text.
|
||||
type Line struct {
|
||||
Text String
|
||||
// Width is the width of the line.
|
||||
@@ -25,22 +26,31 @@ type Line struct {
|
||||
}
|
||||
|
||||
type String struct {
|
||||
String string
|
||||
String string
|
||||
// Advances contain the advance of each rune in String.
|
||||
Advances []fixed.Int26_6
|
||||
}
|
||||
|
||||
// A Layout contains the measurements of a body of text as
|
||||
// a list of Lines.
|
||||
type Layout struct {
|
||||
Lines []Line
|
||||
}
|
||||
|
||||
// LayoutOptions specify the constraints of a text layout.
|
||||
type LayoutOptions struct {
|
||||
MaxWidth int
|
||||
// MaxWidth set the maximum width of the layout.
|
||||
MaxWidth int
|
||||
// SingleLine specify that line breaks are ignored.
|
||||
SingleLine bool
|
||||
}
|
||||
|
||||
type Face interface {
|
||||
Layout(str string, opts LayoutOptions) *Layout
|
||||
Path(str String) ui.MacroOp
|
||||
// Layout returns the text layout for a string given a set of
|
||||
// options.
|
||||
Layout(s string, opts LayoutOptions) *Layout
|
||||
// Path returns the ClipOp outline of a text recorded in a macro.
|
||||
Path(s String) ui.MacroOp
|
||||
}
|
||||
|
||||
type Alignment uint8
|
||||
@@ -48,7 +58,7 @@ type Alignment uint8
|
||||
const (
|
||||
Start Alignment = iota
|
||||
End
|
||||
Center
|
||||
Middle
|
||||
)
|
||||
|
||||
func linesDimens(lines []Line) layout.Dimens {
|
||||
@@ -77,14 +87,10 @@ func linesDimens(lines []Line) layout.Dimens {
|
||||
}
|
||||
}
|
||||
|
||||
func IsNewline(r rune) bool {
|
||||
return r == '\n'
|
||||
}
|
||||
|
||||
func align(align Alignment, width fixed.Int26_6, maxWidth int) fixed.Int26_6 {
|
||||
mw := fixed.I(maxWidth)
|
||||
switch align {
|
||||
case Center:
|
||||
case Middle:
|
||||
return fixed.I(((mw - width) / 2).Floor())
|
||||
case End:
|
||||
return fixed.I((mw - width).Floor())
|
||||
@@ -94,3 +100,16 @@ func align(align Alignment, width fixed.Int26_6, maxWidth int) fixed.Int26_6 {
|
||||
panic(fmt.Errorf("unknown alignment %v", align))
|
||||
}
|
||||
}
|
||||
|
||||
func (a Alignment) String() string {
|
||||
switch a {
|
||||
case Start:
|
||||
return "Start"
|
||||
case End:
|
||||
return "End"
|
||||
case Middle:
|
||||
return "Middle"
|
||||
default:
|
||||
panic("unreachable")
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user