From f3ce48a1bad6854805ebc6585b6cbd0ffec6a25f Mon Sep 17 00:00:00 2001 From: Sander Schobers Date: Sat, 12 Dec 2020 14:47:25 +0100 Subject: [PATCH] Added shorthand method for retrieving default font for the control. --- ui/button.go | 5 ++--- ui/checkbox.go | 5 ++--- ui/controlbase.go | 5 +++++ ui/label.go | 3 +-- ui/paragraph.go | 6 ++---- ui/textbox.go | 8 +++----- 6 files changed, 15 insertions(+), 17 deletions(-) diff --git a/ui/button.go b/ui/button.go index 673cefe..7c2327e 100644 --- a/ui/button.go +++ b/ui/button.go @@ -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) } diff --git a/ui/checkbox.go b/ui/checkbox.go index 50d2bfb..7cfb5c9 100644 --- a/ui/checkbox.go +++ b/ui/checkbox.go @@ -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) } } diff --git a/ui/controlbase.go b/ui/controlbase.go index 45eb423..7493caf 100644 --- a/ui/controlbase.go +++ b/ui/controlbase.go @@ -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 diff --git a/ui/label.go b/ui/label.go index 66a7df1..40a3fb1 100644 --- a/ui/label.go +++ b/ui/label.go @@ -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 diff --git a/ui/paragraph.go b/ui/paragraph.go index a4cee92..9f63199 100644 --- a/ui/paragraph.go +++ b/ui/paragraph.go @@ -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 diff --git a/ui/textbox.go b/ui/textbox.go index 14d2c0b..697fdd6 100644 --- a/ui/textbox.go +++ b/ui/textbox.go @@ -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)