Added shorthand method for retrieving default font for the control.

This commit is contained in:
Sander Schobers 2020-12-12 14:47:25 +01:00
parent 46b3357635
commit f3ce48a1ba
6 changed files with 15 additions and 17 deletions

View File

@ -41,7 +41,7 @@ func BuildIconButton(icon, text string, fn func(b *Button)) *Button {
func (b *Button) desiredSize(ctx Context) geom.PointF32 {
var pad = ctx.Style().Dimensions.TextPadding
var font = ctx.Fonts().Font(b.FontName(ctx))
var font = b.Font_(ctx)
var w, h float32 = 0, font.Height()
icon, iconW, iconH := b.icon(ctx)
@ -225,8 +225,7 @@ func (b *Button) Render(ctx Context) {
pos.X += iconW + pad
}
if len(b.Text) != 0 {
fontName := b.FontName(ctx)
font := ctx.Fonts().Font(fontName)
font := b.Font_(ctx)
ctx.Renderer().Text(font, geom.PtF32(pos.X, pos.Y+.5*(boundsH-font.Height())), textColor, b.Text)
}

View File

@ -23,7 +23,7 @@ func BuildCheckbox(text string, fn func(c *Checkbox)) *Checkbox {
func (c *Checkbox) desiredSize(ctx Context) geom.PointF32 {
var pad = ctx.Style().Dimensions.TextPadding
var font = ctx.Fonts().Font(c.FontName(ctx))
font := c.Font_(ctx)
var w, h float32 = 0, font.Height()
if len(c.Text) != 0 {
w += pad + font.WidthOf(c.Text)
@ -128,8 +128,7 @@ func (c *Checkbox) Render(ctx Context) {
pos.X += iconWidth + pad
}
if len(c.Text) != 0 {
var fontName = c.FontName(ctx)
var font = ctx.Fonts().Font(fontName)
font := c.Font_(ctx)
ctx.Renderer().Text(font, geom.PtF32(pos.X, pos.Y+.5*(boundsH-font.Height())), fore, c.Text)
}
}

View File

@ -188,6 +188,11 @@ func (c *ControlBase) HandleNotify(ctx Context, e Event, notifier Notifier) bool
return false
}
func (c *ControlBase) Font_(ctx Context) Font {
name := c.FontName(ctx)
return ctx.Fonts().Font(name)
}
func (c *ControlBase) FontColor(ctx Context, color color.Color) color.Color {
if c.Disabled {
return ctx.Style().Palette.TextOnDisabled

View File

@ -28,8 +28,7 @@ func (l *Label) hashDesiredSize(ctx Context) string {
}
func (l *Label) desiredSize(ctx Context) interface{} {
fontName := l.FontName(ctx)
font := ctx.Fonts().Font(fontName)
font := l.Font_(ctx)
width := font.WidthOf(l.Text)
height := font.Height()
pad := ctx.Style().Dimensions.TextPadding

View File

@ -26,8 +26,7 @@ func BuildParagraph(text string, fn func(*Paragraph)) *Paragraph {
func fastFormatFloat32(f float32) string { return strconv.FormatFloat(float64(f), 'b', 32, 32) }
func (p *Paragraph) desiredSize(ctx Context) interface{} {
fontName := p.FontName(ctx)
font := ctx.Fonts().Font(fontName)
font := p.Font_(ctx)
pad := ctx.Style().Dimensions.TextPadding
lines := p.splitInLines(ctx, p.width-2*pad)
@ -43,8 +42,7 @@ func (p *Paragraph) hashTextDesired(ctx Context) string {
}
func (p *Paragraph) splitInLines(ctx Context, width float32) []string {
fontName := p.FontName(ctx)
font := ctx.Fonts().Font(fontName)
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

View File

@ -57,8 +57,7 @@ func (b *TextBox) Arrange(ctx Context, bounds geom.RectangleF32, offset geom.Poi
}
func (b *TextBox) DesiredSize(ctx Context, _ geom.PointF32) geom.PointF32 {
var fontName = b.FontName(ctx)
var font = ctx.Fonts().Font(fontName)
font := b.Font_(ctx)
var width = font.WidthOf(b.Text)
var height = font.Height()
var pad = b.pad(ctx)
@ -68,7 +67,7 @@ func (b *TextBox) DesiredSize(ctx Context, _ geom.PointF32) geom.PointF32 {
func (b *TextBox) mousePosToCaretPos(ctx Context, e MouseEvent) int {
p := b.ToControlPosition(e.Pos())
offset := p.X - b.box.bounds.Min.X
f := ctx.Fonts().Font(b.FontName(ctx))
f := b.Font_(ctx)
var carets = [3]int{0, 0, len(b.Text)}
var offsets = [3]float32{0, 0, f.WidthOf(b.Text)}
var updateCenter = func() {
@ -259,8 +258,7 @@ func (b *TextBox) Render(ctx Context) {
back = ctx.Style().Palette.Background
}
renderer.Clear(back)
fontName := b.FontName(ctx)
font := ctx.Fonts().Font(fontName)
font := b.Font_(ctx)
if b.Selection.Start != 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)