Debian Codegraph @

shiny/widget/label.go

raw symbols SCIP
1 // Copyright 2016 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
4
5 package widget
6
7 import (
8 "image"
9
10 "golang.org/x/exp/shiny/widget/node"
11 "golang.org/x/exp/shiny/widget/theme"
12 "golang.org/x/image/font"
13 "golang.org/x/image/math/fixed"
14 )
15
16 // Label is a leaf widget that holds a text label.
17 type Label struct {
18 node.LeafEmbed
19 Text string
20 ThemeColor theme.Color
21 }
22
23 // NewLabel returns a new Label widget.
24 func NewLabel(text string) *Label {
25 w := &Label{
26 Text: text,
27 }
28 w.Wrapper = w
29 return w
30 }
31
32 func (w *Label) Measure(t *theme.Theme, widthHint, heightHint int) {
33 face := t.AcquireFontFace(theme.FontFaceOptions{})
34 defer t.ReleaseFontFace(theme.FontFaceOptions{}, face)
35 m := face.Metrics()
36
37 // TODO: padding, to match a Text widget?
38
39 w.MeasuredSize.X = font.MeasureString(face, w.Text).Ceil()
40 w.MeasuredSize.Y = m.Ascent.Ceil() + m.Descent.Ceil()
41 }
42
43 func (w *Label) PaintBase(ctx *node.PaintBaseContext, origin image.Point) error {
44 w.Marks.UnmarkNeedsPaintBase()
45 dst := ctx.Dst.SubImage(w.Rect.Add(origin)).(*image.RGBA)
46 if dst.Bounds().Empty() {
47 return nil
48 }
49
50 face := ctx.Theme.AcquireFontFace(theme.FontFaceOptions{})
51 defer ctx.Theme.ReleaseFontFace(theme.FontFaceOptions{}, face)
52 m := face.Metrics()
53 ascent := m.Ascent.Ceil()
54
55 tc := w.ThemeColor
56 if tc == nil {
57 tc = theme.Foreground
58 }
59
60 d := font.Drawer{
61 Dst: dst,
62 Src: tc.Uniform(ctx.Theme),
63 Face: face,
64 Dot: fixed.Point26_6{
65 X: fixed.I(origin.X + w.Rect.Min.X),
66 Y: fixed.I(origin.Y + w.Rect.Min.Y + ascent),
67 },
68 }
69 d.DrawString(w.Text)
70 return nil
71 }