@@ -1,6 +1,7 @@
11package lipgloss
22
33import (
4+ "fmt"
45 "image"
56 "sort"
67
@@ -94,17 +95,14 @@ type Layer struct {
9495 zIndex int
9596 children []*Layer
9697 id string
97− content string
98+ content uv.Drawable
9899}
99100
100101// NewLayer creates a new Layer with the given content. It calculates the size
101102// based on the widest line and the number of lines in the content.
102−func NewLayer(content string) (l *Layer) {
103+func NewLayer(content any) (l *Layer) {
103104 l = new(Layer)
104− l.content = content
105− height := Height(content)
106− width := Width(content)
107− l.rect = image.Rect(0, 0, width, height)
105+ l.SetContent(content)
108106 return l
109107}
110108
@@ -216,23 +214,56 @@ func (l *Layer) AddLayers(layers ...*Layer) *Layer {
216214}
217215
218216// SetContent sets the content of the Layer.
219−func (l *Layer) SetContent(content string) *Layer {
220− l.content = content
217+func (l *Layer) SetContent(content any) *Layer {
218+ var drawable uv.Drawable
219+ var rect image.Rectangle
220+ switch c := content.(type) {
221+ case uv.Drawable:
222+ drawable = c
223+ switch c := c.(type) {
224+ case interface{ Bounds() image.Rectangle }:
225+ rect.Max.X = c.Bounds().Dx()
226+ rect.Max.Y = c.Bounds().Dy()
227+ case interface {
228+ Width() int
229+ Height() int
230+ }:
231+ rect.Max.X = c.Width()
232+ rect.Max.Y = c.Height()
233+ }
234+ case fmt.Stringer:
235+ s := c.String()
236+ drawable = uv.NewStyledString(s)
237+ rect = image.Rect(0, 0, Width(s), Height(s))
238+ case string:
239+ drawable = uv.NewStyledString(c)
240+ rect = image.Rect(0, 0, Width(c), Height(c))
241+ default:
242+ s := fmt.Sprint(content)
243+ drawable = uv.NewStyledString(s)
244+ rect = image.Rect(0, 0, Width(s), Height(s))
245+ }
246+ l.content = drawable
247+ l.rect = rect
221248 return l
222249}
223250
224251// Content returns the content of the Layer.
225−func (l *Layer) Content() string {
252+func (l *Layer) Content() any {
226253 return l.content
227254}
228255
229256// Draw draws the Layer onto the given screen buffer.
230−func (l *Layer) Draw(scr uv.Screen, _ image.Rectangle) {
231− ss := uv.NewStyledString(l.content)
232− ss.Draw(scr, l.Bounds())
257+func (l *Layer) Draw(scr uv.Screen, area image.Rectangle) {
258+ if l.content == nil {
259+ return
260+ }
261+ l.content.Draw(scr, area.Intersect(l.Bounds()))
233262 for _, child := range l.children {
234− ss := uv.NewStyledString(child.content)
235− ss.Draw(scr, child.Bounds())
263+ if child.content == nil {
264+ continue
265+ }
266+ child.content.Draw(scr, area.Intersect(child.Bounds()))
236267 }
237268}
238269