github.com/Seikaijyu/gio@v0.0.1/widget/material/label.go (about)

     1  // SPDX-License-Identifier: Unlicense OR MIT
     2  
     3  package material
     4  
     5  import (
     6  	"image/color"
     7  
     8  	"github.com/Seikaijyu/gio/font"
     9  	"github.com/Seikaijyu/gio/internal/f32color"
    10  	"github.com/Seikaijyu/gio/layout"
    11  	"github.com/Seikaijyu/gio/op"
    12  	"github.com/Seikaijyu/gio/op/paint"
    13  	"github.com/Seikaijyu/gio/text"
    14  	"github.com/Seikaijyu/gio/unit"
    15  	"github.com/Seikaijyu/gio/widget"
    16  )
    17  
    18  // LabelStyle configures the presentation of text. If the State field is set, the
    19  // label will be laid out as interactive (able to be selected and copied). Otherwise,
    20  // the label will be non-interactive.
    21  type LabelStyle struct {
    22  	// Face defines the text style.
    23  	Font font.Font
    24  	// Color is the text color.
    25  	Color color.NRGBA
    26  	// SelectionColor is the color of the background for selected text.
    27  	SelectionColor color.NRGBA
    28  	// Alignment specify the text alignment.
    29  	Alignment text.Alignment
    30  	// MaxLines limits the number of lines. Zero means no limit.
    31  	MaxLines int
    32  	// WrapPolicy configures how displayed text will be broken into lines.
    33  	WrapPolicy text.WrapPolicy
    34  	// Truncator is the text that will be shown at the end of the final
    35  	// line if MaxLines is exceeded. Defaults to "…" if empty.
    36  	Truncator string
    37  	// Text is the content displayed by the label.
    38  	Text string
    39  	// TextSize determines the size of the text glyphs.
    40  	TextSize unit.Sp
    41  	// LineHeight controls the distance between the baselines of lines of text.
    42  	// If zero, a sensible default will be used.
    43  	LineHeight unit.Sp
    44  	// LineHeightScale applies a scaling factor to the LineHeight. If zero, a
    45  	// sensible default will be used.
    46  	LineHeightScale float32
    47  
    48  	// Shaper is the text shaper used to display this labe. This field is automatically
    49  	// set using by all constructor functions. If constructing a LabelStyle literal, you
    50  	// must provide a Shaper or displaying text will panic.
    51  	Shaper *text.Shaper
    52  	// State provides text selection state for the label. If not set, the label cannot
    53  	// be selected or copied interactively.
    54  	State *widget.Selectable
    55  }
    56  
    57  func H1(th *Theme, txt string) LabelStyle {
    58  	label := Label(th, th.TextSize*96.0/16.0, txt)
    59  	label.Font.Weight = font.Light
    60  	return label
    61  }
    62  
    63  func H2(th *Theme, txt string) LabelStyle {
    64  	label := Label(th, th.TextSize*60.0/16.0, txt)
    65  	label.Font.Weight = font.Light
    66  	return label
    67  }
    68  
    69  func H3(th *Theme, txt string) LabelStyle {
    70  	return Label(th, th.TextSize*48.0/16.0, txt)
    71  }
    72  
    73  func H4(th *Theme, txt string) LabelStyle {
    74  	return Label(th, th.TextSize*34.0/16.0, txt)
    75  }
    76  
    77  func H5(th *Theme, txt string) LabelStyle {
    78  	return Label(th, th.TextSize*24.0/16.0, txt)
    79  }
    80  
    81  func H6(th *Theme, txt string) LabelStyle {
    82  	label := Label(th, th.TextSize*20.0/16.0, txt)
    83  	label.Font.Weight = font.Medium
    84  	return label
    85  }
    86  
    87  func Subtitle1(th *Theme, txt string) LabelStyle {
    88  	return Label(th, th.TextSize*16.0/16.0, txt)
    89  }
    90  
    91  func Subtitle2(th *Theme, txt string) LabelStyle {
    92  	label := Label(th, th.TextSize*14.0/16.0, txt)
    93  	label.Font.Weight = font.Medium
    94  	return label
    95  }
    96  
    97  func Body1(th *Theme, txt string) LabelStyle {
    98  	return Label(th, th.TextSize, txt)
    99  }
   100  
   101  func Body2(th *Theme, txt string) LabelStyle {
   102  	return Label(th, th.TextSize*14.0/16.0, txt)
   103  }
   104  
   105  func Caption(th *Theme, txt string) LabelStyle {
   106  	return Label(th, th.TextSize*12.0/16.0, txt)
   107  }
   108  
   109  func Overline(th *Theme, txt string) LabelStyle {
   110  	return Label(th, th.TextSize*10.0/16.0, txt)
   111  }
   112  
   113  func Label(th *Theme, size unit.Sp, txt string) LabelStyle {
   114  	l := LabelStyle{
   115  		Text:           txt,
   116  		Color:          th.Palette.Fg,
   117  		SelectionColor: f32color.MulAlpha(th.Palette.ContrastBg, 0x60),
   118  		TextSize:       size,
   119  		Shaper:         th.Shaper,
   120  	}
   121  	l.Font.Typeface = th.Face
   122  	return l
   123  }
   124  
   125  func (l LabelStyle) Layout(gtx layout.Context) layout.Dimensions {
   126  	textColorMacro := op.Record(gtx.Ops)
   127  	paint.ColorOp{Color: l.Color}.Add(gtx.Ops)
   128  	textColor := textColorMacro.Stop()
   129  	selectColorMacro := op.Record(gtx.Ops)
   130  	paint.ColorOp{Color: l.SelectionColor}.Add(gtx.Ops)
   131  	selectColor := selectColorMacro.Stop()
   132  
   133  	if l.State != nil {
   134  		if l.State.Text() != l.Text {
   135  			l.State.SetText(l.Text)
   136  		}
   137  		l.State.Alignment = l.Alignment
   138  		l.State.MaxLines = l.MaxLines
   139  		l.State.Truncator = l.Truncator
   140  		l.State.WrapPolicy = l.WrapPolicy
   141  		l.State.LineHeight = l.LineHeight
   142  		l.State.LineHeightScale = l.LineHeightScale
   143  		return l.State.Layout(gtx, l.Shaper, l.Font, l.TextSize, textColor, selectColor)
   144  	}
   145  	tl := widget.Label{
   146  		Alignment:       l.Alignment,
   147  		MaxLines:        l.MaxLines,
   148  		Truncator:       l.Truncator,
   149  		WrapPolicy:      l.WrapPolicy,
   150  		LineHeight:      l.LineHeight,
   151  		LineHeightScale: l.LineHeightScale,
   152  	}
   153  	return tl.Layout(gtx, l.Shaper, l.Font, l.TextSize, l.Text, textColor)
   154  }