Draw draws the Button widget onto the canvas. Implements widgetapi.Widget.Draw.
(cvs *canvas.Canvas, meta *widgetapi.Meta)
| 175 | // Draw draws the Button widget onto the canvas. |
| 176 | // Implements widgetapi.Widget.Draw. |
| 177 | func (b *Button) Draw(cvs *canvas.Canvas, meta *widgetapi.Meta) error { |
| 178 | b.mu.Lock() |
| 179 | defer b.mu.Unlock() |
| 180 | |
| 181 | if b.keyTriggerTime != nil { |
| 182 | since := timeSince(*b.keyTriggerTime) |
| 183 | if since > b.opts.keyUpDelay { |
| 184 | b.state = button.Up |
| 185 | } |
| 186 | } |
| 187 | |
| 188 | cvsAr := cvs.Area() |
| 189 | b.mouseFSM.UpdateArea(cvsAr) |
| 190 | |
| 191 | sw := b.shadowWidth() |
| 192 | shadowAr := image.Rect(sw, sw, cvsAr.Dx(), cvsAr.Dy()) |
| 193 | if !b.opts.disableShadow { |
| 194 | if err := cvs.SetAreaCells(shadowAr, shadowRune, cell.BgColor(b.opts.shadowColor)); err != nil { |
| 195 | return err |
| 196 | } |
| 197 | } |
| 198 | |
| 199 | buttonAr := image.Rect(0, 0, cvsAr.Dx()-sw, cvsAr.Dy()-sw) |
| 200 | if b.state == button.Down && !b.opts.disableShadow { |
| 201 | buttonAr = shadowAr |
| 202 | } |
| 203 | |
| 204 | var fillColor cell.Color |
| 205 | switch { |
| 206 | case b.state == button.Down && b.opts.pressedFillColor != nil: |
| 207 | fillColor = *b.opts.pressedFillColor |
| 208 | case meta.Focused && b.opts.focusedFillColor != nil: |
| 209 | fillColor = *b.opts.focusedFillColor |
| 210 | default: |
| 211 | fillColor = b.opts.fillColor |
| 212 | } |
| 213 | |
| 214 | if err := cvs.SetAreaCells(buttonAr, buttonRune, cell.BgColor(fillColor)); err != nil { |
| 215 | return err |
| 216 | } |
| 217 | return b.drawText(cvs, meta, buttonAr) |
| 218 | } |
| 219 | |
| 220 | // drawText draws the text inside the button. |
| 221 | func (b *Button) drawText(cvs *canvas.Canvas, meta *widgetapi.Meta, buttonAr image.Rectangle) error { |