Fixed "background" property

This commit is contained in:
Alexei Anoshenko 2024-04-22 19:14:58 +03:00
parent 0740a48346
commit 856b09b04b
6 changed files with 191 additions and 15 deletions

View File

@ -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()
}

View File

@ -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()
}

View File

@ -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()
}

View File

@ -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++ {

View File

@ -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:

View File

@ -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
}