MCPcopy
hub / github.com/mum4k/termdash / drawText

Method drawText

widgets/button/button.go:221–267  ·  view source on GitHub ↗

drawText draws the text inside the button.

(cvs *canvas.Canvas, meta *widgetapi.Meta, buttonAr image.Rectangle)

Source from the content-addressed store, hash-verified

219
220// drawText draws the text inside the button.
221func (b *Button) drawText(cvs *canvas.Canvas, meta *widgetapi.Meta, buttonAr image.Rectangle) error {
222 pad := b.opts.textHorizontalPadding
223 textAr := image.Rect(buttonAr.Min.X+pad, buttonAr.Min.Y, buttonAr.Dx()-pad, buttonAr.Max.Y)
224 start, err := alignfor.Text(textAr, b.text.String(), align.HorizontalCenter, align.VerticalMiddle)
225 if err != nil {
226 return err
227 }
228
229 maxCells := buttonAr.Max.X - start.X
230 trimmed, err := draw.TrimText(b.text.String(), maxCells, draw.OverrunModeThreeDot)
231 if err != nil {
232 return err
233 }
234
235 optRange, err := b.tOptsTracker.ForPosition(0) // Text options for the current byte.
236 if err != nil {
237 return err
238 }
239
240 cur := start
241 for i, r := range trimmed {
242 if i >= optRange.High { // Get the next write options.
243 or, err := b.tOptsTracker.ForPosition(i)
244 if err != nil {
245 return err
246 }
247 optRange = or
248 }
249
250 tOpts := b.givenTOpts[optRange.AttrIdx]
251 var cellOpts []cell.Option
252 switch {
253 case b.state == button.Down && len(tOpts.pressedCellOpts) > 0:
254 cellOpts = tOpts.pressedCellOpts
255 case meta.Focused && len(tOpts.focusedCellOpts) > 0:
256 cellOpts = tOpts.focusedCellOpts
257 default:
258 cellOpts = tOpts.cellOpts
259 }
260 cells, err := cvs.SetCell(cur, r, cellOpts...)
261 if err != nil {
262 return err
263 }
264 cur = image.Point{cur.X + cells, cur.Y}
265 }
266 return nil
267}
268
269// activated asserts whether the keyboard event activated the button.
270func (b *Button) keyActivated(k *terminalapi.Keyboard, meta *widgetapi.EventMeta) bool {

Callers 1

DrawMethod · 0.95

Calls 5

TextFunction · 0.92
TrimTextFunction · 0.92
ForPositionMethod · 0.80
SetCellMethod · 0.65
StringMethod · 0.45

Tested by

no test coverage detected