diff --git a/background.go b/background.go index 692c73d..a315e9f 100644 --- a/background.go +++ b/background.go @@ -1,6 +1,9 @@ package rui -import "strings" +import ( + "fmt" + "strings" +) const ( // NoRepeat is value of the Repeat property of an background image: @@ -61,6 +64,8 @@ const ( // BackgroundElement describes the background element. type BackgroundElement interface { Properties + fmt.Stringer + stringWriter cssStyle(session Session) string Tag() string Clone() BackgroundElement @@ -239,3 +244,23 @@ func (image *backgroundImage) cssStyle(session Session) string { return "" } + +func (image *backgroundImage) writeString(buffer *strings.Builder, indent string) { + image.writeToBuffer(buffer, indent, image.Tag(), []string{ + Source, + Width, + Height, + ImageHorizontalAlign, + ImageVerticalAlign, + backgroundFit, + Repeat, + Attachment, + }) +} + +func (image *backgroundImage) String() string { + buffer := allocStringBuilder() + defer freeStringBuilder(buffer) + image.writeString(buffer, "") + return buffer.String() +} diff --git a/backgroundConicGradient.go b/backgroundConicGradient.go index c2b646b..27bd01f 100644 --- a/backgroundConicGradient.go +++ b/backgroundConicGradient.go @@ -336,3 +336,19 @@ func (gradient *backgroundConicGradient) cssStyle(session Session) string { return buffer.String() } + +func (gradient *backgroundConicGradient) writeString(buffer *strings.Builder, indent string) { + gradient.writeToBuffer(buffer, indent, gradient.Tag(), []string{ + Gradient, + CenterX, + CenterY, + Repeating, + }) +} + +func (gradient *backgroundConicGradient) String() string { + buffer := allocStringBuilder() + defer freeStringBuilder(buffer) + gradient.writeString(buffer, "") + return buffer.String() +} diff --git a/backgroundGradient.go b/backgroundGradient.go index 9373841..d6ae2f4 100644 --- a/backgroundGradient.go +++ b/backgroundGradient.go @@ -224,6 +224,33 @@ func (point *BackgroundGradientPoint) color(session Session) (Color, bool) { return 0, false } +func (point *BackgroundGradientPoint) String() string { + result := "black" + if point.Color != nil { + switch color := point.Color.(type) { + case string: + result = color + + case Color: + result = color.String() + } + } + + if point.Pos != nil { + switch value := point.Pos.(type) { + case string: + result += " " + value + + case SizeUnit: + if value.Type != Auto { + result += " " + value.String() + } + } + } + + return result +} + func (gradient *backgroundGradient) writeGradient(session Session, buffer *strings.Builder) bool { value, ok := gradient.properties[Gradient] @@ -370,6 +397,21 @@ func (gradient *backgroundLinearGradient) cssStyle(session Session) string { return buffer.String() } +func (gradient *backgroundLinearGradient) writeString(buffer *strings.Builder, indent string) { + gradient.writeToBuffer(buffer, indent, gradient.Tag(), []string{ + Gradient, + Repeating, + Direction, + }) +} + +func (gradient *backgroundLinearGradient) String() string { + buffer := allocStringBuilder() + defer freeStringBuilder(buffer) + gradient.writeString(buffer, "") + return buffer.String() +} + func (gradient *backgroundRadialGradient) Tag() string { return "radial-gradient" } @@ -610,3 +652,20 @@ func (gradient *backgroundRadialGradient) cssStyle(session Session) string { return buffer.String() } +func (gradient *backgroundRadialGradient) writeString(buffer *strings.Builder, indent string) { + gradient.writeToBuffer(buffer, indent, gradient.Tag(), []string{ + Gradient, + CenterX, + CenterY, + Repeating, + RadialGradientShape, + RadialGradientRadius, + }) +} + +func (gradient *backgroundRadialGradient) String() string { + buffer := allocStringBuilder() + defer freeStringBuilder(buffer) + gradient.writeString(buffer, "") + return buffer.String() +} diff --git a/properties.go b/properties.go index a53ef8e..32240f1 100644 --- a/properties.go +++ b/properties.go @@ -68,6 +68,28 @@ func (properties *propertyList) AllTags() []string { return tags } +func (properties *propertyList) writeToBuffer(buffer *strings.Builder, + indent string, objectTag string, tags []string) { + + buffer.WriteString(objectTag) + buffer.WriteString(" {\n") + + indent2 := indent + "\t" + + for _, tag := range tags { + if value, ok := properties.properties[tag]; ok { + buffer.WriteString(indent2) + buffer.WriteString(tag) + buffer.WriteString(" = ") + writePropertyValue(buffer, tag, value, indent2) + buffer.WriteString(",\n") + } + } + + buffer.WriteString(indent) + buffer.WriteString("}") +} + func parseProperties(properties Properties, object DataObject) { count := object.PropertyCount() for i := 0; i < count; i++ { diff --git a/viewStyle.go b/viewStyle.go index 71b261f..94a86d2 100644 --- a/viewStyle.go +++ b/viewStyle.go @@ -573,6 +573,9 @@ func supportedPropertyValue(value any) bool { case []ViewShadow: case []View: case []any: + case []BackgroundElement: + case []BackgroundGradientPoint: + case []BackgroundGradientAngle: case map[string]Animation: default: return false @@ -701,7 +704,7 @@ func writePropertyValue(buffer *strings.Builder, tag string, value any, indent s case []View: switch len(value) { case 0: - buffer.WriteString("[]\n") + buffer.WriteString("[]") case 1: writeViewStyle(value[0].Tag(), value[0], buffer, indent) @@ -740,6 +743,47 @@ func writePropertyValue(buffer *strings.Builder, tag string, value any, indent s buffer.WriteString(" ]") } + case []BackgroundElement: + switch len(value) { + case 0: + buffer.WriteString("[]\n") + + case 1: + value[0].writeString(buffer, indent) + + default: + buffer.WriteString("[\n") + indent2 := indent + "\t" + for _, element := range value { + buffer.WriteString(indent2) + element.writeString(buffer, indent2) + buffer.WriteString(",\n") + } + + buffer.WriteString(indent) + buffer.WriteRune(']') + } + + case []BackgroundGradientPoint: + buffer.WriteRune('"') + for i, point := range value { + if i > 0 { + buffer.WriteString(",") + } + buffer.WriteString(point.String()) + } + buffer.WriteRune('"') + + case []BackgroundGradientAngle: + buffer.WriteRune('"') + for i, point := range value { + if i > 0 { + buffer.WriteString(",") + } + buffer.WriteString(point.String()) + } + buffer.WriteRune('"') + case map[string]Animation: switch count := len(value); count { case 0: diff --git a/viewStyleSet.go b/viewStyleSet.go index ad499a8..15f44ce 100644 --- a/viewStyleSet.go +++ b/viewStyleSet.go @@ -32,42 +32,52 @@ func (style *viewStyle) setRange(tag string, value any) bool { } func (style *viewStyle) setBackground(value any) bool { + background := []BackgroundElement{} + switch value := value.(type) { case BackgroundElement: - style.properties[Background] = []BackgroundElement{value} - return true + background = []BackgroundElement{value} case []BackgroundElement: - style.properties[Background] = value - return true + background = value + + case []DataValue: + for _, el := range value { + if el.IsObject() { + if element := createBackground(el.Object()); element != nil { + background = append(background, element) + } + } else if obj := ParseDataText(el.Value()); obj != nil { + if element := createBackground(obj); element != nil { + background = append(background, element) + } + } + } case DataObject: if element := createBackground(value); element != nil { - style.properties[Background] = []BackgroundElement{element} - return true + background = []BackgroundElement{element} } case []DataObject: for _, obj := range value { - background := []BackgroundElement{} if element := createBackground(obj); element != nil { background = append(background, element) } - if len(background) > 0 { - style.properties[Background] = background - return true - } } case string: if obj := ParseDataText(value); obj != nil { if element := createBackground(obj); element != nil { - style.properties[Background] = []BackgroundElement{element} - return true + background = []BackgroundElement{element} } } } + if len(background) > 0 { + style.properties[Background] = background + return true + } return false }