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

Method Draw

widgets/fx/framed.go:116–178  ·  view source on GitHub ↗

Draw implements widgetapi.Widget.

(cvs *canvas.Canvas, meta *widgetapi.Meta)

Source from the content-addressed store, hash-verified

114
115// Draw implements widgetapi.Widget.
116func (fw *FramedWidget) Draw(cvs *canvas.Canvas, meta *widgetapi.Meta) error {
117 fw.mu.Lock()
118 defer fw.mu.Unlock()
119
120 size := cvs.Size()
121
122 // Need at least 3×3: a 1-cell border on each side plus a 1×1 inner area.
123 if size.X < 3 || size.Y < 3 {
124 return nil
125 }
126
127 innerSize := image.Point{X: size.X - 2, Y: size.Y - 2}
128
129 // (Re)create the scratch canvas on resize.
130 if fw.tmp == nil || fw.lastSize != innerSize {
131 var err error
132 fw.tmp, err = canvas.New(image.Rect(0, 0, innerSize.X, innerSize.Y))
133 if err != nil {
134 return err
135 }
136 fw.lastSize = innerSize
137 }
138
139 // Clear and draw the inner widget onto the scratch canvas.
140 if err := fw.tmp.Clear(); err != nil {
141 return err
142 }
143 if err := fw.inner.Draw(fw.tmp, meta); err != nil {
144 return err
145 }
146
147 // Copy the inner canvas onto the outer canvas at offset (1, 1).
148 innerAr := fw.tmp.Area()
149 for y := innerAr.Min.Y; y < innerAr.Max.Y; y++ {
150 for x := innerAr.Min.X; x < innerAr.Max.X; x++ {
151 p := image.Point{X: x, Y: y}
152 c, err := fw.tmp.Cell(p)
153 if err != nil {
154 continue
155 }
156 _, _ = cvs.SetCell(image.Point{X: x + 1, Y: y + 1}, c.Rune, c.Opts)
157 }
158 }
159
160 // Assemble border draw options.
161 var cellOpts []cell.Option
162 cellOpts = append(cellOpts, fw.borderOpts...)
163 if fw.colorOpt != nil {
164 cellOpts = append(cellOpts, fw.colorOpt)
165 }
166
167 borderDrawOpts := []draw.BorderOption{
168 draw.BorderLineStyle(fw.lineStyle),
169 }
170 if len(cellOpts) > 0 {
171 borderDrawOpts = append(borderDrawOpts, draw.BorderCellOpts(cellOpts...))
172 }
173 if fw.title != "" {

Callers

nothing calls this directly

Calls 11

NewFunction · 0.92
BorderLineStyleFunction · 0.92
BorderCellOptsFunction · 0.92
BorderTitleFunction · 0.92
BorderFunction · 0.92
CellMethod · 0.80
SizeMethod · 0.65
ClearMethod · 0.65
DrawMethod · 0.65
SetCellMethod · 0.65
AreaMethod · 0.45

Tested by

no test coverage detected