~whereswaldon/gio-x

6e5ccb802ed109bb41ec5d47d1c850ec8d552e9e — Dominik Honnef 10 months ago a0b26f7
richtext: allow any value type in metadata

Users may want to associate complex metadata with spans, such as objects
that interactive spans refer to. Let users store them directly instead
of having to roundtrip through strings.

Performance wise, this makes storing string values more expensive,
because they have to be put in interface values. On the other hand,
users can now store pointer-typed values for "free". Additionally, if
users are concerned about performance, then they likely aren't
dynamically allocating strings and can use pointers to strings instead.

Signed-off-by: Dominik Honnef <dominik@honnef.co>
1 files changed, 7 insertions(+), 7 deletions(-)

M richtext/richtext.go
M richtext/richtext.go => richtext/richtext.go +7 -7
@@ 49,7 49,7 @@ type InteractiveSpan struct {
	longPressed  bool
	pressStarted time.Time
	contents     string
	metadata     map[string]string
	metadata     map[string]interface{}
	events       []Event
}



@@ 98,12 98,12 @@ func (i *InteractiveSpan) Events() []Event {

// Content returns the text content of the interactive span as well as the
// metadata associated with it.
func (i *InteractiveSpan) Content() (string, map[string]string) {
func (i *InteractiveSpan) Content() (string, map[string]interface{}) {
	return i.contents, i.metadata
}

// Get looks up a metadata property on the interactive span.
func (i *InteractiveSpan) Get(key string) string {
func (i *InteractiveSpan) Get(key string) interface{} {
	return i.metadata[key]
}



@@ 153,7 153,7 @@ type SpanStyle struct {
	Color       color.NRGBA
	Content     string
	Interactive bool
	metadata    map[string]string
	metadata    map[string]interface{}
}

// spanShape describes the text shaping of a single span.


@@ 166,7 166,7 @@ type spanShape struct {
// Set configures a metadata key-value pair on the span that can be
// retrieved if the span is interacted with. If the provided value
// is empty, the key will be deleted from the metadata.
func (ss *SpanStyle) Set(key, value string) {
func (ss *SpanStyle) Set(key string, value interface{}) {
	if value == "" {
		if ss.metadata != nil {
			delete(ss.metadata, key)


@@ 177,7 177,7 @@ func (ss *SpanStyle) Set(key, value string) {
		return
	}
	if ss.metadata == nil {
		ss.metadata = make(map[string]string)
		ss.metadata = make(map[string]interface{})
	}
	ss.metadata[key] = value
}


@@ 195,7 195,7 @@ func (ss SpanStyle) Layout(gtx layout.Context, s text.Shaper, shape spanShape) l
func (ss SpanStyle) DeepCopy() SpanStyle {
	out := ss
	if len(ss.metadata) > 0 {
		md := make(map[string]string)
		md := make(map[string]interface{})
		for k, v := range ss.metadata {
			md[k] = v
		}