Compare commits
No commits in common. "12a9f5ee393aa453f0b87f8eb71837a2b1dc825f" and "bcd32f83725c98e73ec05b60e6abb769df2a4764" have entirely different histories.
12a9f5ee39
...
bcd32f8372
@ -1,24 +0,0 @@
|
|||||||
package embedres
|
|
||||||
|
|
||||||
import (
|
|
||||||
"embed"
|
|
||||||
"io"
|
|
||||||
|
|
||||||
"opslag.de/schobers/zntg/ui"
|
|
||||||
)
|
|
||||||
|
|
||||||
var _ ui.Resources = &resources{}
|
|
||||||
|
|
||||||
type resources struct {
|
|
||||||
fs embed.FS
|
|
||||||
}
|
|
||||||
|
|
||||||
func New(fs embed.FS) ui.Resources {
|
|
||||||
return &resources{fs}
|
|
||||||
}
|
|
||||||
|
|
||||||
func (r resources) Destroy() error { return nil }
|
|
||||||
|
|
||||||
func (r resources) OpenResource(name string) (io.ReadCloser, error) {
|
|
||||||
return r.fs.Open(name)
|
|
||||||
}
|
|
20
ui/button.go
20
ui/button.go
@ -40,30 +40,30 @@ func BuildIconButton(icon, text string, fn func(b *Button)) *Button {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (b *Button) desiredSize(ctx Context) geom.PointF32 {
|
func (b *Button) desiredSize(ctx Context) geom.PointF32 {
|
||||||
var pad = b.ActualTextPadding(ctx)
|
var pad = ctx.Style().Dimensions.TextPadding
|
||||||
var font = b.ActualFont(ctx)
|
var font = b.Font_(ctx)
|
||||||
var w, h float32 = 0, font.Height()
|
var w, h float32 = 0, font.Height()
|
||||||
|
|
||||||
icon, iconW, iconH := b.icon(ctx)
|
icon, iconW, iconH := b.icon(ctx)
|
||||||
if len(b.Text) == 0 {
|
if len(b.Text) == 0 {
|
||||||
if icon != nil && iconH > 0 {
|
if icon != nil && iconH > 0 {
|
||||||
w = pad.Left + iconW + pad.Right
|
w = pad + iconW + pad
|
||||||
h = iconH
|
h = iconH
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
w += pad.Left + font.WidthOf(b.Text) + pad.Right
|
w += pad + font.WidthOf(b.Text) + pad
|
||||||
if icon != nil && iconH > 0 {
|
if icon != nil && iconH > 0 {
|
||||||
if b.IconHeight == 0 {
|
if b.IconHeight == 0 {
|
||||||
iconW = iconW * h / iconH
|
iconW = iconW * h / iconH
|
||||||
// iconH = h
|
// iconH = h
|
||||||
}
|
}
|
||||||
w += iconW + pad.Right
|
w += iconW + pad
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if w == 0 {
|
if w == 0 {
|
||||||
return geom.ZeroPtF32
|
return geom.ZeroPtF32
|
||||||
}
|
}
|
||||||
return geom.PtF32(w, pad.Top+h+pad.Bottom)
|
return geom.PtF32(w, pad+h+pad)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (b *Button) icon(ctx Context) (Texture, float32, float32) {
|
func (b *Button) icon(ctx Context) (Texture, float32, float32) {
|
||||||
@ -203,8 +203,8 @@ func (b *Button) Render(ctx Context) {
|
|||||||
bounds.Min.X += .5 * deltaX
|
bounds.Min.X += .5 * deltaX
|
||||||
bounds.Min.Y += .5 * deltaY
|
bounds.Min.Y += .5 * deltaY
|
||||||
|
|
||||||
pad := b.ActualTextPadding(ctx)
|
var pad = style.Dimensions.TextPadding
|
||||||
bounds = pad.InsetRect(bounds)
|
bounds = bounds.Inset(pad)
|
||||||
boundsH := bounds.Dy()
|
boundsH := bounds.Dy()
|
||||||
pos := bounds.Min
|
pos := bounds.Min
|
||||||
icon, iconW, iconH := b.icon(ctx)
|
icon, iconW, iconH := b.icon(ctx)
|
||||||
@ -222,10 +222,10 @@ func (b *Button) Render(ctx Context) {
|
|||||||
iconOffsetY = .5 * (boundsH - iconH)
|
iconOffsetY = .5 * (boundsH - iconH)
|
||||||
}
|
}
|
||||||
ctx.Renderer().DrawTextureOptions(icon, geom.RectRelF32(pos.X, pos.Y+iconOffsetY, iconW, iconH), DrawOptions{Tint: textColor})
|
ctx.Renderer().DrawTextureOptions(icon, geom.RectRelF32(pos.X, pos.Y+iconOffsetY, iconW, iconH), DrawOptions{Tint: textColor})
|
||||||
pos.X += iconW + pad.Right
|
pos.X += iconW + pad
|
||||||
}
|
}
|
||||||
if len(b.Text) != 0 {
|
if len(b.Text) != 0 {
|
||||||
font := b.ActualFont(ctx)
|
font := b.Font_(ctx)
|
||||||
ctx.Renderer().Text(font, geom.PtF32(pos.X, pos.Y+.5*(boundsH-font.Height())), textColor, b.Text)
|
ctx.Renderer().Text(font, geom.PtF32(pos.X, pos.Y+.5*(boundsH-font.Height())), textColor, b.Text)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -22,16 +22,16 @@ func BuildCheckbox(text string, fn func(c *Checkbox)) *Checkbox {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (c *Checkbox) desiredSize(ctx Context) geom.PointF32 {
|
func (c *Checkbox) desiredSize(ctx Context) geom.PointF32 {
|
||||||
pad := c.ActualTextPadding(ctx)
|
var pad = ctx.Style().Dimensions.TextPadding
|
||||||
font := c.ActualFont(ctx)
|
font := c.Font_(ctx)
|
||||||
var w, h float32 = 0, font.Height()
|
var w, h float32 = 0, font.Height()
|
||||||
if len(c.Text) != 0 {
|
if len(c.Text) != 0 {
|
||||||
w += pad.Left + font.WidthOf(c.Text)
|
w += pad + font.WidthOf(c.Text)
|
||||||
}
|
}
|
||||||
icon := c.getOrCreateNormalIcon(ctx)
|
icon := c.getOrCreateNormalIcon(ctx)
|
||||||
_, iconWidth := ScaleToHeight(SizeOfTexture(icon).ToF32(), h)
|
_, iconWidth := ScaleToHeight(SizeOfTexture(icon).ToF32(), h)
|
||||||
w += pad.Left + iconWidth
|
w += pad + iconWidth
|
||||||
return geom.PtF32(w+pad.Right, pad.Top+h+pad.Bottom)
|
return geom.PtF32(w+pad, pad+h+pad)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *Checkbox) icon(ctx Context) Texture {
|
func (c *Checkbox) icon(ctx Context) Texture {
|
||||||
@ -108,8 +108,8 @@ func (c *Checkbox) Render(ctx Context) {
|
|||||||
fore := c.TextColor(ctx)
|
fore := c.TextColor(ctx)
|
||||||
bounds := c.bounds
|
bounds := c.bounds
|
||||||
|
|
||||||
pad := c.ActualTextPadding(ctx)
|
var pad = style.Dimensions.TextPadding
|
||||||
bounds = pad.InsetRect(bounds)
|
bounds = bounds.Inset(pad)
|
||||||
boundsH := bounds.Dy()
|
boundsH := bounds.Dy()
|
||||||
pos := bounds.Min
|
pos := bounds.Min
|
||||||
icon := c.icon(ctx)
|
icon := c.icon(ctx)
|
||||||
@ -125,10 +125,10 @@ func (c *Checkbox) Render(ctx Context) {
|
|||||||
_, iconWidth := ScaleToHeight(SizeOfTexture(scaledIcon).ToF32(), boundsH)
|
_, iconWidth := ScaleToHeight(SizeOfTexture(scaledIcon).ToF32(), boundsH)
|
||||||
rect := geom.RectRelF32(pos.X, pos.Y, iconWidth, boundsH)
|
rect := geom.RectRelF32(pos.X, pos.Y, iconWidth, boundsH)
|
||||||
ctx.Renderer().DrawTextureOptions(scaledIcon, rect, DrawOptions{Tint: iconColor})
|
ctx.Renderer().DrawTextureOptions(scaledIcon, rect, DrawOptions{Tint: iconColor})
|
||||||
pos.X += iconWidth + pad.Right
|
pos.X += iconWidth + pad
|
||||||
}
|
}
|
||||||
if len(c.Text) != 0 {
|
if len(c.Text) != 0 {
|
||||||
font := c.ActualFont(ctx)
|
font := c.Font_(ctx)
|
||||||
ctx.Renderer().Text(font, geom.PtF32(pos.X, pos.Y+.5*(boundsH-font.Height())), fore, c.Text)
|
ctx.Renderer().Text(font, geom.PtF32(pos.X, pos.Y+.5*(boundsH-font.Height())), fore, c.Text)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -103,7 +103,6 @@ type ControlBase struct {
|
|||||||
Background color.Color
|
Background color.Color
|
||||||
Font FontStyle
|
Font FontStyle
|
||||||
TextAlignment HorizontalAlignment
|
TextAlignment HorizontalAlignment
|
||||||
TextPadding SideLengths
|
|
||||||
|
|
||||||
Disabled bool
|
Disabled bool
|
||||||
|
|
||||||
@ -189,15 +188,11 @@ func (c *ControlBase) HandleNotify(ctx Context, e Event, notifier Notifier) bool
|
|||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *ControlBase) ActualFont(ctx Context) Font {
|
func (c *ControlBase) Font_(ctx Context) Font {
|
||||||
name := c.FontName(ctx)
|
name := c.FontName(ctx)
|
||||||
return ctx.Fonts().Font(name)
|
return ctx.Fonts().Font(name)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *ControlBase) ActualTextPadding(ctx Context) Sides {
|
|
||||||
return c.TextPadding.Zero(ctx.Style().Dimensions.TextPadding)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (c *ControlBase) FontColor(ctx Context, color color.Color) color.Color {
|
func (c *ControlBase) FontColor(ctx Context, color color.Color) color.Color {
|
||||||
if c.Disabled {
|
if c.Disabled {
|
||||||
return ctx.Style().Palette.TextOnDisabled
|
return ctx.Style().Palette.TextOnDisabled
|
||||||
|
22
ui/debug.go
22
ui/debug.go
@ -77,9 +77,8 @@ func NewDebugOverlay(root Control) *debugOverlay {
|
|||||||
|
|
||||||
func (o *debugOverlay) renderControl(ctx Context, control Control) {
|
func (o *debugOverlay) renderControl(ctx Context, control Control) {
|
||||||
renderer := ctx.Renderer()
|
renderer := ctx.Renderer()
|
||||||
|
// bounds := control.Bounds()
|
||||||
currentColor := zntg.MustHexColor("#FF0000")
|
// renderer.Rectangle(bounds, o.boundsColor, 1)
|
||||||
parentColor := zntg.MustHexColor("#0000FF")
|
|
||||||
|
|
||||||
var maxY float32
|
var maxY float32
|
||||||
var renderHoverNode func(pos geom.PointF32, node *controlNode)
|
var renderHoverNode func(pos geom.PointF32, node *controlNode)
|
||||||
@ -97,10 +96,6 @@ func (o *debugOverlay) renderControl(ctx Context, control Control) {
|
|||||||
renderer.FillRectangle(pos.RectRel2D(nameTextureWidth, nameTextureHeight), color.Black)
|
renderer.FillRectangle(pos.RectRel2D(nameTextureWidth, nameTextureHeight), color.Black)
|
||||||
renderer.DrawTexturePoint(nameTexture, pos)
|
renderer.DrawTexturePoint(nameTexture, pos)
|
||||||
childPos := pos.Add2D(nameTextureWidth+ctx.Style().Dimensions.Margin, 0)
|
childPos := pos.Add2D(nameTextureWidth+ctx.Style().Dimensions.Margin, 0)
|
||||||
if len(node.Children) == 0 {
|
|
||||||
renderer.Rectangle(node.Parent.Bounds, parentColor, 1)
|
|
||||||
renderer.Rectangle(node.Bounds, currentColor, 1)
|
|
||||||
}
|
|
||||||
for _, child := range node.Children {
|
for _, child := range node.Children {
|
||||||
if childPos.Y == maxY {
|
if childPos.Y == maxY {
|
||||||
childPos.Y = maxY + nameTextureHeight
|
childPos.Y = maxY + nameTextureHeight
|
||||||
@ -119,18 +114,15 @@ func (o *debugOverlay) renderControl(ctx Context, control Control) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func createHoverNodes(hover geom.PointF32, control Control) *controlNode {
|
func createHoverNodes(hover geom.PointF32, control Control) *controlNode {
|
||||||
bounds := control.Bounds()
|
if !hover.In(control.Bounds()) {
|
||||||
if !hover.In(bounds) {
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
node := &controlNode{Name: controlName(control), Bounds: bounds}
|
node := &controlNode{Name: controlName(control)}
|
||||||
for _, child := range controlChildren(control) {
|
for _, child := range controlChildren(control) {
|
||||||
childNode := createHoverNodes(hover, child)
|
childNode := createHoverNodes(hover, child)
|
||||||
if childNode == nil {
|
if childNode != nil {
|
||||||
continue
|
node.Children = append(node.Children, childNode)
|
||||||
}
|
}
|
||||||
childNode.Parent = node
|
|
||||||
node.Children = append(node.Children, childNode)
|
|
||||||
}
|
}
|
||||||
return node
|
return node
|
||||||
}
|
}
|
||||||
@ -155,7 +147,5 @@ func (o *debugOverlay) Shown() {}
|
|||||||
|
|
||||||
type controlNode struct {
|
type controlNode struct {
|
||||||
Name string
|
Name string
|
||||||
Bounds geom.RectangleF32
|
|
||||||
Parent *controlNode
|
|
||||||
Children []*controlNode
|
Children []*controlNode
|
||||||
}
|
}
|
||||||
|
33
ui/label.go
33
ui/label.go
@ -6,19 +6,11 @@ import (
|
|||||||
"opslag.de/schobers/geom"
|
"opslag.de/schobers/geom"
|
||||||
)
|
)
|
||||||
|
|
||||||
type TextOverflow int
|
|
||||||
|
|
||||||
const (
|
|
||||||
TextOverflowClip TextOverflow = iota
|
|
||||||
TextOverflowEllipsis
|
|
||||||
)
|
|
||||||
|
|
||||||
type Label struct {
|
type Label struct {
|
||||||
ControlBase
|
ControlBase
|
||||||
|
|
||||||
Text string
|
Text string
|
||||||
TextOverflow TextOverflow
|
DropShadow color.Color
|
||||||
DropShadow color.Color
|
|
||||||
|
|
||||||
desired CachedValue
|
desired CachedValue
|
||||||
}
|
}
|
||||||
@ -36,11 +28,11 @@ func (l *Label) hashDesiredSize(ctx Context) string {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (l *Label) desiredSize(ctx Context) interface{} {
|
func (l *Label) desiredSize(ctx Context) interface{} {
|
||||||
font := l.ActualFont(ctx)
|
font := l.Font_(ctx)
|
||||||
width := font.WidthOf(l.Text)
|
width := font.WidthOf(l.Text)
|
||||||
height := font.Height()
|
height := font.Height()
|
||||||
pad := l.ActualTextPadding(ctx)
|
pad := ctx.Style().Dimensions.TextPadding
|
||||||
return geom.PtF32(pad.Left+width+pad.Right, pad.Top+height+pad.Bottom)
|
return geom.PtF32(width+pad*2, height+pad*2)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (l *Label) DesiredSize(ctx Context, _ geom.PointF32) geom.PointF32 {
|
func (l *Label) DesiredSize(ctx Context, _ geom.PointF32) geom.PointF32 {
|
||||||
@ -48,8 +40,8 @@ func (l *Label) DesiredSize(ctx Context, _ geom.PointF32) geom.PointF32 {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (l *Label) getLabelTopLeft(ctx Context) geom.PointF32 {
|
func (l *Label) getLabelTopLeft(ctx Context) geom.PointF32 {
|
||||||
pad := l.ActualTextPadding(ctx)
|
pad := ctx.Style().Dimensions.TextPadding
|
||||||
bounds := pad.InsetRect(l.bounds)
|
bounds := l.bounds.Inset(pad)
|
||||||
switch l.TextAlignment {
|
switch l.TextAlignment {
|
||||||
case AlignRight:
|
case AlignRight:
|
||||||
return geom.PtF32(bounds.Max.X, bounds.Min.Y)
|
return geom.PtF32(bounds.Max.X, bounds.Min.Y)
|
||||||
@ -63,15 +55,10 @@ func (l *Label) getLabelTopLeft(ctx Context) geom.PointF32 {
|
|||||||
func (l *Label) Render(ctx Context) {
|
func (l *Label) Render(ctx Context) {
|
||||||
l.RenderBackground(ctx)
|
l.RenderBackground(ctx)
|
||||||
fontColor := l.TextColor(ctx)
|
fontColor := l.TextColor(ctx)
|
||||||
font := l.ActualFont(ctx)
|
fontName := l.FontName(ctx)
|
||||||
topLeft := l.getLabelTopLeft(ctx)
|
topLeft := l.getLabelTopLeft(ctx)
|
||||||
text := l.Text
|
|
||||||
availableWidth := l.bounds.Dx()
|
|
||||||
if l.TextOverflow == TextOverflowEllipsis {
|
|
||||||
text = fitTextEllipsis(font, text, availableWidth)
|
|
||||||
}
|
|
||||||
if l.DropShadow != nil {
|
if l.DropShadow != nil {
|
||||||
ctx.Renderer().TextAlign(font, topLeft.Add2D(1, 1), l.DropShadow, text, l.TextAlignment)
|
ctx.Fonts().TextAlign(fontName, topLeft.Add2D(1, 1), l.DropShadow, l.Text, l.TextAlignment)
|
||||||
}
|
}
|
||||||
ctx.Renderer().TextAlign(font, topLeft, fontColor, text, l.TextAlignment)
|
ctx.Fonts().TextAlign(fontName, topLeft, fontColor, l.Text, l.TextAlignment)
|
||||||
}
|
}
|
||||||
|
45
ui/length.go
45
ui/length.go
@ -24,54 +24,9 @@ func Fixed(l float32) *Length { return &Length{l} }
|
|||||||
|
|
||||||
func Infinite() *Length { return &Length{geom.NaN32()} }
|
func Infinite() *Length { return &Length{geom.NaN32()} }
|
||||||
|
|
||||||
func ZL() *Length { return &Length{0} }
|
|
||||||
|
|
||||||
type SideLengths struct {
|
type SideLengths struct {
|
||||||
Left *Length
|
Left *Length
|
||||||
Top *Length
|
Top *Length
|
||||||
Right *Length
|
Right *Length
|
||||||
Bottom *Length
|
Bottom *Length
|
||||||
}
|
}
|
||||||
|
|
||||||
func (l SideLengths) InsetRect(r geom.RectangleF32) geom.RectangleF32 {
|
|
||||||
return Sides{
|
|
||||||
Left: l.Left.Value(),
|
|
||||||
Top: l.Top.Value(),
|
|
||||||
Right: l.Right.Value(),
|
|
||||||
Bottom: l.Bottom.Value(),
|
|
||||||
}.InsetRect(r)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (l SideLengths) Zero(value float32) Sides {
|
|
||||||
return Sides{
|
|
||||||
Left: l.Left.Zero(value),
|
|
||||||
Top: l.Top.Zero(value),
|
|
||||||
Right: l.Right.Zero(value),
|
|
||||||
Bottom: l.Bottom.Zero(value),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
type Sides struct {
|
|
||||||
Left float32
|
|
||||||
Top float32
|
|
||||||
Right float32
|
|
||||||
Bottom float32
|
|
||||||
}
|
|
||||||
|
|
||||||
func (s Sides) InsetRect(r geom.RectangleF32) geom.RectangleF32 {
|
|
||||||
if r.Dx() < (s.Left + s.Right) {
|
|
||||||
r.Min.X += r.Dx() * s.Left / (s.Left + s.Right)
|
|
||||||
r.Max.X = r.Min.X
|
|
||||||
} else {
|
|
||||||
r.Min.X += s.Left
|
|
||||||
r.Max.X -= s.Right
|
|
||||||
}
|
|
||||||
if r.Dy() < (s.Top + s.Bottom) {
|
|
||||||
r.Min.Y += r.Dy() * s.Top / (s.Top + s.Bottom)
|
|
||||||
r.Max.Y = r.Min.Y
|
|
||||||
} else {
|
|
||||||
r.Min.Y += s.Top
|
|
||||||
r.Max.Y -= s.Bottom
|
|
||||||
}
|
|
||||||
return r
|
|
||||||
}
|
|
||||||
|
@ -11,9 +11,6 @@ type overflow struct {
|
|||||||
|
|
||||||
Background color.Color
|
Background color.Color
|
||||||
|
|
||||||
ClipHorizontal bool
|
|
||||||
ClipVertical bool
|
|
||||||
|
|
||||||
barWidth float32
|
barWidth float32
|
||||||
desired geom.PointF32
|
desired geom.PointF32
|
||||||
bounds geom.RectangleF32
|
bounds geom.RectangleF32
|
||||||
@ -29,30 +26,18 @@ type overflow struct {
|
|||||||
type ScrollControl interface {
|
type ScrollControl interface {
|
||||||
Control
|
Control
|
||||||
|
|
||||||
SetBackgroundColor(color.Color)
|
|
||||||
SetClipHorizontal(bool)
|
|
||||||
SetClipVertical(bool)
|
|
||||||
SetScrollbarColor(bar color.Color, hover color.Color)
|
SetScrollbarColor(bar color.Color, hover color.Color)
|
||||||
}
|
}
|
||||||
|
|
||||||
func BuildOverflow(content Control, build func(c ScrollControl)) ScrollControl {
|
|
||||||
o := &overflow{Proxy: Proxy{Content: content}}
|
|
||||||
o.hor = BuildScrollbar(OrientationHorizontal, func(*Scrollbar) {})
|
|
||||||
o.ver = BuildScrollbar(OrientationVertical, func(*Scrollbar) {})
|
|
||||||
if build != nil {
|
|
||||||
build(o)
|
|
||||||
}
|
|
||||||
return o
|
|
||||||
}
|
|
||||||
|
|
||||||
func Overflow(content Control) ScrollControl {
|
func Overflow(content Control) ScrollControl {
|
||||||
return OverflowBackground(content, nil)
|
return OverflowBackground(content, nil)
|
||||||
}
|
}
|
||||||
|
|
||||||
func OverflowBackground(content Control, back color.Color) ScrollControl {
|
func OverflowBackground(content Control, back color.Color) ScrollControl {
|
||||||
return BuildOverflow(content, func(c ScrollControl) {
|
var o = &overflow{Proxy: Proxy{Content: content}, Background: back}
|
||||||
c.SetBackgroundColor(back)
|
o.hor = BuildScrollbar(OrientationHorizontal, func(*Scrollbar) {})
|
||||||
})
|
o.ver = BuildScrollbar(OrientationVertical, func(*Scrollbar) {})
|
||||||
|
return o
|
||||||
}
|
}
|
||||||
|
|
||||||
func (o *overflow) shouldScroll(bounds geom.RectangleF32) (hor bool, ver bool) {
|
func (o *overflow) shouldScroll(bounds geom.RectangleF32) (hor bool, ver bool) {
|
||||||
@ -68,8 +53,6 @@ func (o *overflow) shouldScroll(bounds geom.RectangleF32) (hor bool, ver bool) {
|
|||||||
if hor && !ver {
|
if hor && !ver {
|
||||||
ver = scroll(size.Y+o.barWidth, bounds.Dy())
|
ver = scroll(size.Y+o.barWidth, bounds.Dy())
|
||||||
}
|
}
|
||||||
hor = hor && !o.ClipHorizontal
|
|
||||||
ver = ver && !o.ClipVertical
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -175,17 +158,6 @@ func (o *overflow) Render(ctx Context) {
|
|||||||
bar.Render(ctx)
|
bar.Render(ctx)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
func (o *overflow) SetBackgroundColor(c color.Color) {
|
|
||||||
o.Background = c
|
|
||||||
}
|
|
||||||
|
|
||||||
func (o *overflow) SetClipHorizontal(clip bool) {
|
|
||||||
o.ClipHorizontal = clip
|
|
||||||
}
|
|
||||||
|
|
||||||
func (o *overflow) SetClipVertical(clip bool) {
|
|
||||||
o.ClipVertical = clip
|
|
||||||
}
|
|
||||||
|
|
||||||
func (o *overflow) SetScrollbarColor(bar color.Color, hover color.Color) {
|
func (o *overflow) SetScrollbarColor(bar color.Color, hover color.Color) {
|
||||||
o.hor.BarColor = bar
|
o.hor.BarColor = bar
|
||||||
|
@ -26,11 +26,11 @@ func BuildParagraph(text string, fn func(*Paragraph)) *Paragraph {
|
|||||||
func fastFormatFloat32(f float32) string { return strconv.FormatFloat(float64(f), 'b', 32, 32) }
|
func fastFormatFloat32(f float32) string { return strconv.FormatFloat(float64(f), 'b', 32, 32) }
|
||||||
|
|
||||||
func (p *Paragraph) desiredSize(ctx Context) interface{} {
|
func (p *Paragraph) desiredSize(ctx Context) interface{} {
|
||||||
font := p.ActualFont(ctx)
|
font := p.Font_(ctx)
|
||||||
|
|
||||||
pad := p.ActualTextPadding(ctx)
|
pad := ctx.Style().Dimensions.TextPadding
|
||||||
lines := p.splitInLines(ctx, p.width-pad.Left-pad.Right)
|
lines := p.splitInLines(ctx, p.width-2*pad)
|
||||||
return geom.PtF32(p.width, float32(len(lines))*font.Height()+pad.Top+pad.Bottom)
|
return geom.PtF32(p.width, float32(len(lines))*font.Height()+2*pad)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *Paragraph) hashTextArranged(ctx Context) string {
|
func (p *Paragraph) hashTextArranged(ctx Context) string {
|
||||||
@ -42,7 +42,38 @@ func (p *Paragraph) hashTextDesired(ctx Context) string {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (p *Paragraph) splitInLines(ctx Context, width float32) []string {
|
func (p *Paragraph) splitInLines(ctx Context, width float32) []string {
|
||||||
font := p.ActualFont(ctx)
|
font := p.Font_(ctx)
|
||||||
|
|
||||||
|
spaces := func(s string) []int { // creates a slice with indices where spaces can be found in string s
|
||||||
|
var spaces []int
|
||||||
|
offset := 0
|
||||||
|
for {
|
||||||
|
space := strings.Index(s[offset:], " ")
|
||||||
|
if space == -1 {
|
||||||
|
return spaces
|
||||||
|
}
|
||||||
|
offset += space
|
||||||
|
spaces = append(spaces, offset)
|
||||||
|
offset++
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fit := func(s string) string { // tries to fit as much of string s in width space.
|
||||||
|
if font.WidthOf(s) < width {
|
||||||
|
return s
|
||||||
|
}
|
||||||
|
spaces := spaces(s)
|
||||||
|
// removes one word (delimited by spaces) at a time and tries until the result fits.
|
||||||
|
for split := len(spaces) - 1; split >= 0; split-- {
|
||||||
|
clipped := s[:spaces[split]]
|
||||||
|
if font.WidthOf(clipped) < width {
|
||||||
|
return clipped
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// nothing fits (returns the whole string)...
|
||||||
|
return s
|
||||||
|
}
|
||||||
|
|
||||||
var lines []string
|
var lines []string
|
||||||
for _, line := range strings.Split(p.Text, "\n") {
|
for _, line := range strings.Split(p.Text, "\n") {
|
||||||
if len(line) == 0 {
|
if len(line) == 0 {
|
||||||
@ -51,7 +82,7 @@ func (p *Paragraph) splitInLines(ctx Context, width float32) []string {
|
|||||||
}
|
}
|
||||||
|
|
||||||
for len(line) > 0 {
|
for len(line) > 0 {
|
||||||
clipped := fitTextWord(font, line, width)
|
clipped := fit(line)
|
||||||
lines = append(lines, clipped)
|
lines = append(lines, clipped)
|
||||||
line = strings.TrimLeft(line[len(clipped):], " ")
|
line = strings.TrimLeft(line[len(clipped):], " ")
|
||||||
}
|
}
|
||||||
@ -60,8 +91,8 @@ func (p *Paragraph) splitInLines(ctx Context, width float32) []string {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (p *Paragraph) updateLines(ctx Context) interface{} {
|
func (p *Paragraph) updateLines(ctx Context) interface{} {
|
||||||
pad := p.ActualTextPadding(ctx)
|
pad := ctx.Style().Dimensions.TextPadding
|
||||||
return p.splitInLines(ctx, p.Bounds().Dx()-pad.Left-pad.Right)
|
return p.splitInLines(ctx, p.Bounds().Dx()-2*pad)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *Paragraph) DesiredSize(ctx Context, size geom.PointF32) geom.PointF32 {
|
func (p *Paragraph) DesiredSize(ctx Context, size geom.PointF32) geom.PointF32 {
|
||||||
@ -73,14 +104,14 @@ func (p *Paragraph) DesiredSize(ctx Context, size geom.PointF32) geom.PointF32 {
|
|||||||
func (p *Paragraph) Render(ctx Context) {
|
func (p *Paragraph) Render(ctx Context) {
|
||||||
p.RenderBackground(ctx)
|
p.RenderBackground(ctx)
|
||||||
|
|
||||||
pad := p.ActualTextPadding(ctx)
|
pad := ctx.Style().Dimensions.TextPadding
|
||||||
width := p.Bounds().Dx() - pad.Left - pad.Right
|
width := p.Bounds().Dx() - 2*pad
|
||||||
lines := p.lines.GetContext(ctx, p.updateLines, p.hashTextArranged).([]string)
|
lines := p.lines.GetContext(ctx, p.updateLines, p.hashTextArranged).([]string)
|
||||||
|
|
||||||
fontColor := p.TextColor(ctx)
|
fontColor := p.TextColor(ctx)
|
||||||
fontName := p.FontName(ctx)
|
fontName := p.FontName(ctx)
|
||||||
fontHeight := ctx.Fonts().Font(fontName).Height()
|
fontHeight := ctx.Fonts().Font(fontName).Height()
|
||||||
bounds := pad.InsetRect(p.bounds)
|
bounds := p.bounds.Inset(pad)
|
||||||
|
|
||||||
left := bounds.Min.X
|
left := bounds.Min.X
|
||||||
switch p.TextAlignment {
|
switch p.TextAlignment {
|
||||||
|
@ -42,9 +42,7 @@ func (p *StackPanel) Arrange(ctx Context, bounds geom.RectangleF32, offset geom.
|
|||||||
height = remainder / float32(stretch)
|
height = remainder / float32(stretch)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
minY := geom.Min32(bounds.Max.Y, bounds.Min.Y+childOffset)
|
var child = geom.RectF32(bounds.Min.X, bounds.Min.Y+childOffset, bounds.Max.X, bounds.Min.Y+childOffset+height)
|
||||||
maxY := geom.Min32(bounds.Max.Y, bounds.Min.Y+childOffset+height)
|
|
||||||
var child = geom.RectF32(bounds.Min.X, minY, bounds.Max.X, maxY)
|
|
||||||
p.Children[i].Arrange(ctx, p.Orientation.FlipRect(child), offset, p)
|
p.Children[i].Arrange(ctx, p.Orientation.FlipRect(child), offset, p)
|
||||||
childOffset += height
|
childOffset += height
|
||||||
}
|
}
|
||||||
|
68
ui/text.go
68
ui/text.go
@ -1,68 +0,0 @@
|
|||||||
package ui
|
|
||||||
|
|
||||||
import "strings"
|
|
||||||
|
|
||||||
// findOptimalFit tries to find the optimal (largest) fit for the interval [0..n] by doing a binary search.
|
|
||||||
func findOptimalFit(n int, fits func(i int) bool) int {
|
|
||||||
if n < 0 || fits(n) {
|
|
||||||
return n
|
|
||||||
}
|
|
||||||
min, max := 0, n
|
|
||||||
for {
|
|
||||||
if min == max {
|
|
||||||
if min == 0 && !fits(min) {
|
|
||||||
return -1
|
|
||||||
}
|
|
||||||
return min
|
|
||||||
}
|
|
||||||
middle := (max + min + 1) / 2
|
|
||||||
if fits(middle) {
|
|
||||||
min = middle
|
|
||||||
} else {
|
|
||||||
max = middle - 1
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// findSpaces creates a slice with indices where spaces can be found in string s
|
|
||||||
func findSpaces(s string) []int {
|
|
||||||
var spaces []int
|
|
||||||
offset := 0
|
|
||||||
for {
|
|
||||||
space := strings.Index(s[offset:], " ")
|
|
||||||
if space == -1 {
|
|
||||||
return spaces
|
|
||||||
}
|
|
||||||
offset += space
|
|
||||||
spaces = append(spaces, offset)
|
|
||||||
offset++
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func fitTextEllipsis(font Font, text string, availableWidth float32) string {
|
|
||||||
if font.WidthOf(text) <= availableWidth {
|
|
||||||
return text
|
|
||||||
}
|
|
||||||
ellipsis := "..."
|
|
||||||
availableWidth -= font.WidthOf(ellipsis)
|
|
||||||
cut := findOptimalFit(len(text), func(i int) bool { return font.WidthOf(text[:i]) <= availableWidth })
|
|
||||||
if cut == -1 {
|
|
||||||
return ""
|
|
||||||
}
|
|
||||||
return text[:cut] + ellipsis
|
|
||||||
}
|
|
||||||
|
|
||||||
// fitTextWord tries to fit as much of string s in width space.
|
|
||||||
func fitTextWord(font Font, s string, availableWidth float32) string {
|
|
||||||
if font.WidthOf(s) < availableWidth {
|
|
||||||
return s
|
|
||||||
}
|
|
||||||
spaces := findSpaces(s)
|
|
||||||
split := findOptimalFit(len(spaces)-1, func(i int) bool {
|
|
||||||
return font.WidthOf(s[:spaces[i]]) <= availableWidth
|
|
||||||
})
|
|
||||||
if split == -1 {
|
|
||||||
return s
|
|
||||||
}
|
|
||||||
return s[:spaces[split]]
|
|
||||||
}
|
|
@ -49,18 +49,21 @@ func BuildTextBox(fn func(*TextBox)) *TextBox {
|
|||||||
return b
|
return b
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (b *TextBox) pad(ctx Context) float32 {
|
||||||
|
return ctx.Style().Dimensions.TextPadding
|
||||||
|
}
|
||||||
|
|
||||||
func (b *TextBox) Arrange(ctx Context, bounds geom.RectangleF32, offset geom.PointF32, parent Control) {
|
func (b *TextBox) Arrange(ctx Context, bounds geom.RectangleF32, offset geom.PointF32, parent Control) {
|
||||||
b.ControlBase.Arrange(ctx, bounds, offset, parent)
|
b.ControlBase.Arrange(ctx, bounds, offset, parent)
|
||||||
pad := b.ActualTextPadding(ctx)
|
b.box.Arrange(ctx, bounds.Inset(b.pad(ctx)), offset, b)
|
||||||
b.box.Arrange(ctx, pad.InsetRect(bounds), offset, b)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (b *TextBox) DesiredSize(ctx Context, _ geom.PointF32) geom.PointF32 {
|
func (b *TextBox) DesiredSize(ctx Context, _ geom.PointF32) geom.PointF32 {
|
||||||
font := b.ActualFont(ctx)
|
font := b.Font_(ctx)
|
||||||
var width = font.WidthOf(b.Text)
|
var width = font.WidthOf(b.Text)
|
||||||
var height = font.Height()
|
var height = font.Height()
|
||||||
pad := b.ActualTextPadding(ctx)
|
var pad = b.pad(ctx)
|
||||||
return geom.PtF32(pad.Left+width+pad.Right, pad.Top+height+pad.Bottom)
|
return geom.PtF32(width+pad*2, height+pad*2)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (b *TextBox) TextChanged() *Events { return &b.textChanged }
|
func (b *TextBox) TextChanged() *Events { return &b.textChanged }
|
||||||
@ -68,7 +71,7 @@ func (b *TextBox) TextChanged() *Events { return &b.textChanged }
|
|||||||
func (b *TextBox) mousePosToCaretPos(ctx Context, e MouseEvent) int {
|
func (b *TextBox) mousePosToCaretPos(ctx Context, e MouseEvent) int {
|
||||||
p := b.ToControlPosition(e.Pos())
|
p := b.ToControlPosition(e.Pos())
|
||||||
offset := p.X - b.box.bounds.Min.X
|
offset := p.X - b.box.bounds.Min.X
|
||||||
f := b.ActualFont(ctx)
|
f := b.Font_(ctx)
|
||||||
var carets = [3]int{0, 0, len(b.Text)}
|
var carets = [3]int{0, 0, len(b.Text)}
|
||||||
var offsets = [3]float32{0, 0, f.WidthOf(b.Text)}
|
var offsets = [3]float32{0, 0, f.WidthOf(b.Text)}
|
||||||
var updateCenter = func() {
|
var updateCenter = func() {
|
||||||
@ -259,7 +262,7 @@ func (b *TextBox) Render(ctx Context) {
|
|||||||
back = ctx.Style().Palette.Background
|
back = ctx.Style().Palette.Background
|
||||||
}
|
}
|
||||||
renderer.Clear(back)
|
renderer.Clear(back)
|
||||||
font := b.ActualFont(ctx)
|
font := b.Font_(ctx)
|
||||||
if b.Selection.Start != b.Selection.End {
|
if b.Selection.Start != b.Selection.End {
|
||||||
left, right := font.WidthOf(b.Text[:b.Selection.Start]), font.WidthOf(b.Text[:b.Selection.End])
|
left, right := font.WidthOf(b.Text[:b.Selection.Start]), font.WidthOf(b.Text[:b.Selection.End])
|
||||||
renderer.FillRectangle(geom.RectF32(left, 0, right, size.Y), style.Palette.PrimaryHighlight)
|
renderer.FillRectangle(geom.RectF32(left, 0, right, size.Y), style.Palette.PrimaryHighlight)
|
||||||
|
Loading…
Reference in New Issue
Block a user