rui_orig/viewStyle.go

922 lines
23 KiB
Go
Raw Normal View History

2021-09-07 17:36:50 +03:00
package rui
import (
"fmt"
2022-05-22 12:54:02 +03:00
"sort"
2021-09-07 17:36:50 +03:00
"strconv"
"strings"
)
// ViewStyle interface of the style of view
type ViewStyle interface {
Properties
2022-08-10 15:36:38 +03:00
// Transition returns the transition animation of the property. Returns nil is there is no transition animation.
2024-11-13 12:56:39 +03:00
Transition(tag PropertyName) Animation
2024-04-23 19:34:36 +03:00
2022-08-10 15:36:38 +03:00
// Transitions returns the map of transition animations. The result is always non-nil.
2024-11-13 12:56:39 +03:00
Transitions() map[PropertyName]Animation
2024-04-23 19:34:36 +03:00
2022-08-10 15:36:38 +03:00
// SetTransition sets the transition animation for the property if "animation" argument is not nil, and
// removes the transition animation of the property if "animation" argument is nil.
// The "tag" argument is the property name.
2024-11-13 12:56:39 +03:00
SetTransition(tag PropertyName, animation Animation)
2022-08-10 15:36:38 +03:00
cssViewStyle(buffer cssBuilder, session Session)
2021-09-07 17:36:50 +03:00
}
type viewStyle struct {
propertyList
2024-11-13 12:56:39 +03:00
//transitions map[PropertyName]Animation
2021-09-07 17:36:50 +03:00
}
2022-05-22 12:54:02 +03:00
type stringWriter interface {
writeString(buffer *strings.Builder, indent string)
}
2021-09-07 17:36:50 +03:00
func (style *viewStyle) init() {
style.propertyList.init()
2024-11-13 12:56:39 +03:00
style.normalize = normalizeViewStyleTag
2021-09-07 17:36:50 +03:00
}
// NewViewStyle create new ViewStyle object
func NewViewStyle(params Params) ViewStyle {
style := new(viewStyle)
style.init()
for tag, value := range params {
style.Set(tag, value)
}
return style
}
2024-11-13 12:56:39 +03:00
func textDecorationCSS(properties Properties, session Session) string {
2021-09-07 17:36:50 +03:00
buffer := allocStringBuilder()
defer freeStringBuilder(buffer)
noDecoration := false
2024-11-13 12:56:39 +03:00
if strikethrough, ok := boolProperty(properties, Strikethrough, session); ok {
2021-09-07 17:36:50 +03:00
if strikethrough {
buffer.WriteString("line-through")
}
noDecoration = true
}
2024-11-13 12:56:39 +03:00
if overline, ok := boolProperty(properties, Overline, session); ok {
2021-09-07 17:36:50 +03:00
if overline {
if buffer.Len() > 0 {
buffer.WriteRune(' ')
}
buffer.WriteString("overline")
}
noDecoration = true
}
2024-11-13 12:56:39 +03:00
if underline, ok := boolProperty(properties, Underline, session); ok {
2021-09-07 17:36:50 +03:00
if underline {
if buffer.Len() > 0 {
buffer.WriteRune(' ')
}
buffer.WriteString("underline")
}
noDecoration = true
}
if buffer.Len() == 0 && noDecoration {
return "none"
}
return buffer.String()
}
func split4Values(text string) []string {
values := strings.Split(text, ",")
count := len(values)
switch count {
case 1, 4:
return values
case 2:
if strings.Trim(values[1], " \t\r\n") == "" {
return values[:1]
}
case 5:
if strings.Trim(values[4], " \t\r\n") != "" {
return values[:4]
}
}
return []string{}
}
2024-11-13 12:56:39 +03:00
func backgroundCSS(properties Properties, session Session) string {
2024-11-21 18:50:58 +03:00
2024-11-13 12:56:39 +03:00
if value := properties.getRaw(Background); value != nil {
2024-11-21 18:50:58 +03:00
if backgrounds, ok := value.([]BackgroundElement); ok && len(backgrounds) > 0 {
2021-09-07 17:36:50 +03:00
buffer := allocStringBuilder()
defer freeStringBuilder(buffer)
for _, background := range backgrounds {
if value := background.cssStyle(session); value != "" {
2021-09-07 17:36:50 +03:00
if buffer.Len() > 0 {
buffer.WriteString(", ")
}
buffer.WriteString(value)
}
}
if buffer.Len() > 0 {
2024-11-21 18:50:58 +03:00
backgroundColor, _ := colorProperty(properties, BackgroundColor, session)
if backgroundColor != 0 {
buffer.WriteRune(' ')
buffer.WriteString(backgroundColor.cssString())
}
2021-09-07 17:36:50 +03:00
return buffer.String()
}
}
}
return ""
}
func (style *viewStyle) cssViewStyle(builder cssBuilder, session Session) {
2021-09-07 17:36:50 +03:00
2024-06-19 16:36:50 +03:00
if visibility, ok := enumProperty(style, Visibility, session, Visible); ok {
switch visibility {
case Invisible:
builder.add(`visibility`, `hidden`)
case Gone:
builder.add(`display`, `none`)
}
}
2024-11-13 12:56:39 +03:00
if margin, ok := getBounds(style, Margin, session); ok {
margin.cssValue(Margin, builder, session)
2021-09-07 17:36:50 +03:00
}
2024-11-13 12:56:39 +03:00
if padding, ok := getBounds(style, Padding, session); ok {
padding.cssValue(Padding, builder, session)
2021-09-07 17:36:50 +03:00
}
2024-11-13 12:56:39 +03:00
if border := getBorderProperty(style, Border); border != nil {
2021-09-07 17:36:50 +03:00
border.cssStyle(builder, session)
border.cssWidth(builder, session)
border.cssColor(builder, session)
}
radius := getRadius(style, session)
radius.cssValue(builder, session)
2021-09-07 17:36:50 +03:00
2024-11-13 12:56:39 +03:00
if outline := getOutlineProperty(style); outline != nil {
outline.ViewOutline(session).cssValue(builder, session)
2021-09-07 17:36:50 +03:00
}
2024-11-13 12:56:39 +03:00
for _, tag := range []PropertyName{ZIndex, Order} {
2022-12-18 18:37:36 +03:00
if value, ok := intProperty(style, tag, session, 0); ok {
2024-11-13 12:56:39 +03:00
builder.add(string(tag), strconv.Itoa(value))
2022-12-18 18:37:36 +03:00
}
2021-09-07 17:36:50 +03:00
}
if opacity, ok := floatProperty(style, Opacity, session, 1.0); ok && opacity >= 0 && opacity <= 1 {
2024-11-13 12:56:39 +03:00
builder.add(string(Opacity), strconv.FormatFloat(opacity, 'f', 3, 32))
2021-09-07 17:36:50 +03:00
}
2024-11-13 12:56:39 +03:00
for _, tag := range []PropertyName{ColumnCount, TabSize} {
2022-12-18 18:37:36 +03:00
if value, ok := intProperty(style, tag, session, 0); ok && value > 0 {
2024-11-13 12:56:39 +03:00
builder.add(string(tag), strconv.Itoa(value))
2022-12-18 18:37:36 +03:00
}
2021-09-07 17:36:50 +03:00
}
2024-11-13 12:56:39 +03:00
for _, tag := range []PropertyName{
2021-09-07 17:36:50 +03:00
Width, Height, MinWidth, MinHeight, MaxWidth, MaxHeight, Left, Right, Top, Bottom,
TextSize, TextIndent, LetterSpacing, WordSpacing, LineHeight, TextLineThickness,
2023-04-23 13:41:26 +03:00
ListRowGap, ListColumnGap, GridRowGap, GridColumnGap, ColumnGap, ColumnWidth, OutlineOffset} {
2021-09-07 17:36:50 +03:00
if size, ok := sizeProperty(style, tag, session); ok && size.Type != Auto {
cssTag, ok := sizeProperties[tag]
if !ok {
2024-11-13 12:56:39 +03:00
cssTag = string(tag)
2021-09-07 17:36:50 +03:00
}
builder.add(cssTag, size.cssString("", session))
2021-09-07 17:36:50 +03:00
}
}
2024-11-13 12:56:39 +03:00
type propertyCss struct {
property PropertyName
cssTag string
}
colorProperties := []propertyCss{
2024-11-21 18:50:58 +03:00
//{BackgroundColor, string(BackgroundColor)},
2021-09-07 17:36:50 +03:00
{TextColor, "color"},
{TextLineColor, "text-decoration-color"},
2024-11-13 12:56:39 +03:00
{CaretColor, string(CaretColor)},
{AccentColor, string(AccentColor)},
2021-09-07 17:36:50 +03:00
}
for _, p := range colorProperties {
if color, ok := colorProperty(style, p.property, session); ok && color != 0 {
builder.add(p.cssTag, color.cssString())
}
}
if value, ok := enumProperty(style, BackgroundClip, session, 0); ok {
2024-11-13 12:56:39 +03:00
builder.add(string(BackgroundClip), enumProperties[BackgroundClip].values[value])
2021-09-07 17:36:50 +03:00
}
2024-11-13 12:56:39 +03:00
if background := backgroundCSS(style, session); background != "" {
2021-09-07 17:36:50 +03:00
builder.add("background", background)
2024-11-21 18:50:58 +03:00
} else {
backgroundColor, _ := colorProperty(style, BackgroundColor, session)
if backgroundColor != 0 {
builder.add("background-color", backgroundColor.cssString())
}
2021-09-07 17:36:50 +03:00
}
if font, ok := stringProperty(style, FontName, session); ok && font != "" {
builder.add(`font-family`, font)
}
writingMode := 0
2024-11-13 12:56:39 +03:00
for _, tag := range []PropertyName{
2022-07-27 13:26:36 +03:00
Overflow, TextAlign, TextTransform, TextWeight, TextLineStyle, WritingMode, TextDirection,
VerticalTextOrientation, CellVerticalAlign, CellHorizontalAlign, GridAutoFlow, Cursor,
WhiteSpace, WordBreak, TextOverflow, Float, TableVerticalAlign, Resize, MixBlendMode, BackgroundBlendMode} {
2021-09-07 17:36:50 +03:00
if data, ok := enumProperties[tag]; ok {
if tag != VerticalTextOrientation || (writingMode != VerticalLeftToRight && writingMode != VerticalRightToLeft) {
if value, ok := enumProperty(style, tag, session, 0); ok {
cssValue := data.values[value]
if cssValue != "" {
builder.add(data.cssTag, cssValue)
}
if tag == WritingMode {
writingMode = value
}
}
}
}
}
2024-11-13 12:56:39 +03:00
type boolPropertyCss struct {
tag PropertyName
cssTag, off, on string
}
for _, prop := range []boolPropertyCss{
2021-09-07 17:36:50 +03:00
{tag: Italic, cssTag: "font-style", off: "normal", on: "italic"},
{tag: SmallCaps, cssTag: "font-variant", off: "normal", on: "small-caps"},
} {
if flag, ok := boolProperty(style, prop.tag, session); ok {
if flag {
builder.add(prop.cssTag, prop.on)
} else {
builder.add(prop.cssTag, prop.off)
}
}
}
2024-11-13 12:56:39 +03:00
if text := textDecorationCSS(style, session); text != "" {
2021-09-07 17:36:50 +03:00
builder.add("text-decoration", text)
}
if userSelect, ok := boolProperty(style, UserSelect, session); ok {
if userSelect {
builder.add("-webkit-user-select", "auto")
builder.add("user-select", "auto")
} else {
builder.add("-webkit-user-select", "none")
builder.add("user-select", "none")
}
}
2021-09-07 17:36:50 +03:00
if css := shadowCSS(style, Shadow, session); css != "" {
builder.add("box-shadow", css)
}
if css := shadowCSS(style, TextShadow, session); css != "" {
builder.add("text-shadow", css)
}
if value, ok := style.properties[ColumnSeparator]; ok {
if separator, ok := value.(ColumnSeparatorProperty); ok {
if css := separator.cssValue(session); css != "" {
builder.add("column-rule", css)
}
}
}
if avoid, ok := boolProperty(style, AvoidBreak, session); ok {
if avoid {
builder.add("break-inside", "avoid")
} else {
builder.add("break-inside", "auto")
}
}
wrap, _ := enumProperty(style, ListWrap, session, 0)
2021-11-11 13:23:41 +03:00
orientation, ok := valueToOrientation(style.Get(Orientation), session)
2021-09-07 17:36:50 +03:00
if ok || wrap > 0 {
cssText := enumProperties[Orientation].cssValues[orientation]
switch wrap {
case ListWrapOn:
2021-09-07 17:36:50 +03:00
cssText += " wrap"
case ListWrapReverse:
2021-09-07 17:36:50 +03:00
cssText += " wrap-reverse"
}
builder.add(`flex-flow`, cssText)
}
rows := (orientation == StartToEndOrientation || orientation == EndToStartOrientation)
var hAlignTag, vAlignTag string
if rows {
hAlignTag = `justify-content`
vAlignTag = `align-items`
} else {
hAlignTag = `align-items`
vAlignTag = `justify-content`
}
if align, ok := enumProperty(style, HorizontalAlign, session, LeftAlign); ok {
switch align {
case LeftAlign:
if (!rows && wrap == ListWrapReverse) || orientation == EndToStartOrientation {
2021-09-07 17:36:50 +03:00
builder.add(hAlignTag, `flex-end`)
} else {
builder.add(hAlignTag, `flex-start`)
}
case RightAlign:
if (!rows && wrap == ListWrapReverse) || orientation == EndToStartOrientation {
2021-09-07 17:36:50 +03:00
builder.add(hAlignTag, `flex-start`)
} else {
builder.add(hAlignTag, `flex-end`)
}
case CenterAlign:
builder.add(hAlignTag, `center`)
case StretchAlign:
if rows {
builder.add(hAlignTag, `space-between`)
} else {
builder.add(hAlignTag, `stretch`)
}
}
}
if align, ok := enumProperty(style, VerticalAlign, session, LeftAlign); ok {
switch align {
case TopAlign:
if (rows && wrap == ListWrapReverse) || orientation == BottomUpOrientation {
2021-09-07 17:36:50 +03:00
builder.add(vAlignTag, `flex-end`)
} else {
builder.add(vAlignTag, `flex-start`)
}
case BottomAlign:
if (rows && wrap == ListWrapReverse) || orientation == BottomUpOrientation {
2021-09-07 17:36:50 +03:00
builder.add(vAlignTag, `flex-start`)
} else {
builder.add(vAlignTag, `flex-end`)
}
case CenterAlign:
builder.add(vAlignTag, `center`)
case StretchAlign:
if rows {
2021-11-11 13:23:41 +03:00
builder.add(vAlignTag, `stretch`)
2021-09-07 17:36:50 +03:00
} else {
2021-11-11 13:23:41 +03:00
builder.add(vAlignTag, `space-between`)
2021-09-07 17:36:50 +03:00
}
}
}
if r, ok := rangeProperty(style, Row, session); ok {
2022-08-17 16:58:07 +03:00
builder.add("grid-row", fmt.Sprintf("%d / %d", r.First+1, r.Last+2))
2021-09-07 17:36:50 +03:00
}
if r, ok := rangeProperty(style, Column, session); ok {
2022-08-17 16:58:07 +03:00
builder.add("grid-column", fmt.Sprintf("%d / %d", r.First+1, r.Last+2))
2021-09-07 17:36:50 +03:00
}
2024-11-13 12:56:39 +03:00
if text := gridCellSizesCSS(style, CellWidth, session); text != "" {
2021-09-07 17:36:50 +03:00
builder.add(`grid-template-columns`, text)
}
2024-11-13 12:56:39 +03:00
if text := gridCellSizesCSS(style, CellHeight, session); text != "" {
2021-09-07 17:36:50 +03:00
builder.add(`grid-template-rows`, text)
}
style.writeViewTransformCSS(builder, session)
if clip := getClipShape(style, Clip, session); clip != nil && clip.valid(session) {
builder.add(`clip-path`, clip.cssStyle(session))
}
if clip := getClipShape(style, ShapeOutside, session); clip != nil && clip.valid(session) {
builder.add(`shape-outside`, clip.cssStyle(session))
}
if value := style.getRaw(Filter); value != nil {
if filter, ok := value.(ViewFilter); ok {
if text := filter.cssStyle(session); text != "" {
2024-11-13 12:56:39 +03:00
builder.add(string(Filter), text)
}
}
}
if value := style.getRaw(BackdropFilter); value != nil {
if filter, ok := value.(ViewFilter); ok {
if text := filter.cssStyle(session); text != "" {
builder.add(`-webkit-backdrop-filter`, text)
2024-11-13 12:56:39 +03:00
builder.add(string(BackdropFilter), text)
2021-09-07 17:36:50 +03:00
}
}
}
2024-11-13 12:56:39 +03:00
if transition := transitionCSS(style, session); transition != "" {
builder.add(`transition`, transition)
}
2021-09-07 17:36:50 +03:00
2024-11-13 12:56:39 +03:00
if animation := animationCSS(style, session); animation != "" {
builder.add(string(AnimationTag), animation)
}
2021-09-07 17:36:50 +03:00
if pause, ok := boolProperty(style, AnimationPaused, session); ok {
if pause {
builder.add(`animation-play-state`, `paused`)
} else {
builder.add(`animation-play-state`, `running`)
}
}
2023-01-03 14:56:57 +03:00
if spanAll, ok := boolProperty(style, ColumnSpanAll, session); ok {
if spanAll {
builder.add(`column-span`, `all`)
} else {
builder.add(`column-span`, `none`)
}
}
2021-09-07 17:36:50 +03:00
}
2022-05-22 12:54:02 +03:00
2022-07-26 18:36:00 +03:00
func valueToOrientation(value any, session Session) (int, bool) {
2022-05-22 12:54:02 +03:00
if value != nil {
switch value := value.(type) {
case int:
return value, true
case string:
text, ok := session.resolveConstants(value)
if !ok {
return 0, false
}
text = strings.ToLower(strings.Trim(text, " \t\n\r"))
switch text {
case "vertical":
return TopDownOrientation, true
case "horizontal":
return StartToEndOrientation, true
}
if result, ok := enumStringToInt(text, enumProperties[Orientation].values, true); ok {
return result, true
}
}
}
return 0, false
}
2024-11-13 12:56:39 +03:00
func normalizeViewStyleTag(tag PropertyName) PropertyName {
tag = defaultNormalize(tag)
switch tag {
case "top-margin":
return MarginTop
case "right-margin":
return MarginRight
case "bottom-margin":
return MarginBottom
case "left-margin":
return MarginLeft
case "top-padding":
return PaddingTop
case "right-padding":
return PaddingRight
case "bottom-padding":
return PaddingBottom
case "left-padding":
return PaddingLeft
case "origin-x":
return TransformOriginX
case "origin-y":
return TransformOriginY
case "origin-z":
return TransformOriginZ
2024-11-13 12:56:39 +03:00
}
return tag
}
func (style *viewStyle) Get(tag PropertyName) any {
return viewStyleGet(style, normalizeViewStyleTag(tag))
2022-05-22 12:54:02 +03:00
}
2024-11-13 12:56:39 +03:00
func viewStyleGet(style Properties, tag PropertyName) any {
2022-05-22 12:54:02 +03:00
switch tag {
case BorderLeft, BorderRight, BorderTop, BorderBottom,
BorderStyle, BorderLeftStyle, BorderRightStyle, BorderTopStyle, BorderBottomStyle,
BorderColor, BorderLeftColor, BorderRightColor, BorderTopColor, BorderBottomColor,
BorderWidth, BorderLeftWidth, BorderRightWidth, BorderTopWidth, BorderBottomWidth:
2024-11-13 12:56:39 +03:00
if border := getBorderProperty(style, Border); border != nil {
2022-05-22 12:54:02 +03:00
return border.Get(tag)
}
return nil
case CellBorderLeft, CellBorderRight, CellBorderTop, CellBorderBottom,
CellBorderStyle, CellBorderLeftStyle, CellBorderRightStyle, CellBorderTopStyle, CellBorderBottomStyle,
CellBorderColor, CellBorderLeftColor, CellBorderRightColor, CellBorderTopColor, CellBorderBottomColor,
CellBorderWidth, CellBorderLeftWidth, CellBorderRightWidth, CellBorderTopWidth, CellBorderBottomWidth:
2024-11-13 12:56:39 +03:00
if border := getBorderProperty(style, CellBorder); border != nil {
2022-05-22 12:54:02 +03:00
return border.Get(tag)
}
return nil
case RadiusX, RadiusY, RadiusTopLeft, RadiusTopLeftX, RadiusTopLeftY,
RadiusTopRight, RadiusTopRightX, RadiusTopRightY,
RadiusBottomLeft, RadiusBottomLeftX, RadiusBottomLeftY,
RadiusBottomRight, RadiusBottomRightX, RadiusBottomRightY:
return getRadiusElement(style, tag)
case ColumnSeparatorStyle, ColumnSeparatorWidth, ColumnSeparatorColor:
2024-11-13 12:56:39 +03:00
if val := style.getRaw(ColumnSeparator); val != nil {
2022-05-22 12:54:02 +03:00
separator := val.(ColumnSeparatorProperty)
return separator.Get(tag)
}
return nil
case RotateX, RotateY, RotateZ, Rotate, SkewX, SkewY, ScaleX, ScaleY, ScaleZ,
TranslateX, TranslateY, TranslateZ:
if transform := getTransformProperty(style, Transform); transform != nil {
return transform.Get(tag)
}
return nil
2022-05-22 12:54:02 +03:00
}
2024-11-13 12:56:39 +03:00
return style.getRaw(tag)
2022-05-22 12:54:02 +03:00
}
2022-07-26 18:36:00 +03:00
func supportedPropertyValue(value any) bool {
2022-05-22 12:54:02 +03:00
switch value.(type) {
case string:
case []string:
case bool:
case float32:
case float64:
case int:
case stringWriter:
case fmt.Stringer:
case []ViewShadow:
case []View:
2022-07-26 18:36:00 +03:00
case []any:
2024-04-22 19:14:58 +03:00
case []BackgroundElement:
case []BackgroundGradientPoint:
case []BackgroundGradientAngle:
2024-11-13 12:56:39 +03:00
case map[PropertyName]Animation:
2022-05-22 12:54:02 +03:00
default:
return false
}
return true
}
2024-11-13 12:56:39 +03:00
func writePropertyValue(buffer *strings.Builder, tag PropertyName, value any, indent string) {
2022-05-22 12:54:02 +03:00
writeString := func(text string) {
simple := (tag != Text && tag != Title && tag != Summary)
if simple {
if len(text) == 1 {
simple = (text[0] >= '0' && text[0] <= '9') || (text[0] >= 'A' && text[0] <= 'Z') || (text[0] >= 'a' && text[0] <= 'z')
} else {
for _, ch := range text {
if (ch >= '0' && ch <= '9') || (ch >= 'A' && ch <= 'Z') || (ch >= 'a' && ch <= 'z') ||
2024-07-06 13:04:12 +03:00
ch == '+' || ch == '-' || ch == '@' || ch == '/' || ch == '_' || ch == '.' ||
ch == ':' || ch == '#' || ch == '%' || ch == 'π' || ch == '°' {
2022-05-22 12:54:02 +03:00
} else {
simple = false
break
}
}
}
}
if !simple {
replace := []struct{ old, new string }{
{old: "\\", new: `\\`},
{old: "\t", new: `\t`},
{old: "\r", new: `\r`},
{old: "\n", new: `\n`},
{old: "\"", new: `\"`},
}
for _, s := range replace {
text = strings.Replace(text, s.old, s.new, -1)
}
buffer.WriteRune('"')
buffer.WriteString(text)
buffer.WriteRune('"')
} else {
buffer.WriteString(text)
}
}
switch value := value.(type) {
case string:
writeString(value)
case []string:
if len(value) == 0 {
buffer.WriteString("[]")
} else {
size := 0
for _, text := range value {
size += len(text) + 2
}
if size < 80 {
lead := "["
for _, text := range value {
buffer.WriteString(lead)
writeString(text)
lead = ", "
}
} else {
buffer.WriteString("[\n")
for _, text := range value {
buffer.WriteString(indent)
buffer.WriteRune('\t')
writeString(text)
buffer.WriteString(",\n")
}
}
buffer.WriteString(indent)
buffer.WriteRune(']')
}
case bool:
if value {
buffer.WriteString("true")
} else {
buffer.WriteString("false")
}
case float32:
buffer.WriteString(fmt.Sprintf("%g", float64(value)))
case float64:
buffer.WriteString(fmt.Sprintf("%g", value))
case int:
if prop, ok := enumProperties[tag]; ok && value >= 0 && value < len(prop.values) {
buffer.WriteString(prop.values[value])
} else {
buffer.WriteString(strconv.Itoa(value))
}
case stringWriter:
value.writeString(buffer, indent+"\t")
case fmt.Stringer:
writeString(value.String())
2022-05-22 12:54:02 +03:00
case []ViewShadow:
switch len(value) {
case 0:
// do nothing
case 1:
value[0].writeString(buffer, indent)
default:
buffer.WriteString("[")
indent2 := "\n" + indent + "\t"
for _, shadow := range value {
buffer.WriteString(indent2)
shadow.writeString(buffer, indent)
2024-04-22 20:03:40 +03:00
buffer.WriteRune(',')
2022-05-22 12:54:02 +03:00
}
buffer.WriteRune('\n')
buffer.WriteString(indent)
buffer.WriteRune(']')
}
case []View:
switch len(value) {
case 0:
2024-04-22 19:14:58 +03:00
buffer.WriteString("[]")
2022-05-22 12:54:02 +03:00
case 1:
writeViewStyle(value[0].Tag(), value[0], buffer, indent, value[0].exscludeTags())
2022-05-22 12:54:02 +03:00
default:
buffer.WriteString("[\n")
indent2 := indent + "\t"
for _, v := range value {
buffer.WriteString(indent2)
writeViewStyle(v.Tag(), v, buffer, indent2, v.exscludeTags())
2022-05-22 12:54:02 +03:00
buffer.WriteString(",\n")
}
buffer.WriteString(indent)
buffer.WriteRune(']')
}
2022-07-26 18:36:00 +03:00
case []any:
2022-05-22 12:54:02 +03:00
switch count := len(value); count {
case 0:
buffer.WriteString("[]")
case 1:
writePropertyValue(buffer, tag, value[0], indent)
default:
buffer.WriteString("[ ")
comma := false
for _, v := range value {
if comma {
buffer.WriteString(", ")
}
writePropertyValue(buffer, tag, v, indent)
comma = true
}
buffer.WriteString(" ]")
}
2024-04-22 19:14:58 +03:00
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('"')
2024-11-13 12:56:39 +03:00
case map[PropertyName]Animation:
2022-05-22 12:54:02 +03:00
switch count := len(value); count {
case 0:
buffer.WriteString("[]")
case 1:
for tag, animation := range value {
animation.writeTransitionString(tag, buffer)
break
}
default:
2024-11-13 12:56:39 +03:00
tags := make([]PropertyName, 0, len(value))
2022-05-22 12:54:02 +03:00
for tag := range value {
tags = append(tags, tag)
}
2024-11-13 12:56:39 +03:00
sort.Slice(tags, func(i, j int) bool {
return tags[i] < tags[j]
})
2022-05-22 12:54:02 +03:00
buffer.WriteString("[\n")
indent2 := indent + "\t"
for _, tag := range tags {
if animation := value[tag]; animation != nil {
buffer.WriteString(indent2)
animation.writeTransitionString(tag, buffer)
2022-08-10 15:36:38 +03:00
buffer.WriteString(",\n")
2022-05-22 12:54:02 +03:00
}
}
buffer.WriteString(indent)
buffer.WriteRune(']')
}
}
}
2024-11-13 12:56:39 +03:00
func writeViewStyle(name string, view Properties, buffer *strings.Builder, indent string, excludeTags []PropertyName) {
2022-05-22 12:54:02 +03:00
buffer.WriteString(name)
buffer.WriteString(" {\n")
indent += "\t"
2024-11-13 12:56:39 +03:00
writeProperty := func(tag PropertyName, value any) {
for _, exclude := range excludeTags {
if exclude == tag {
return
}
}
2022-05-22 12:54:02 +03:00
if supportedPropertyValue(value) {
buffer.WriteString(indent)
2024-11-13 12:56:39 +03:00
buffer.WriteString(string(tag))
2022-05-22 12:54:02 +03:00
buffer.WriteString(" = ")
writePropertyValue(buffer, tag, value, indent)
buffer.WriteString(",\n")
}
}
tags := view.AllTags()
2024-11-13 12:56:39 +03:00
removeTag := func(tag PropertyName) {
2022-05-22 12:54:02 +03:00
for i, t := range tags {
if t == tag {
if i == 0 {
tags = tags[1:]
} else if i == len(tags)-1 {
tags = tags[:i]
} else {
tags = append(tags[:i], tags[i+1:]...)
}
return
}
}
}
2024-11-13 12:56:39 +03:00
tagOrder := []PropertyName{
2022-05-22 12:54:02 +03:00
ID, Row, Column, Top, Right, Bottom, Left, Semantics, Cursor, Visibility,
Opacity, ZIndex, Width, Height, MinWidth, MinHeight, MaxWidth, MaxHeight,
Margin, Padding, BackgroundClip, BackgroundColor, Background, Border, Radius, Outline, Shadow,
Orientation, ListWrap, VerticalAlign, HorizontalAlign, CellWidth, CellHeight,
CellVerticalAlign, CellHorizontalAlign, ListRowGap, ListColumnGap, GridRowGap, GridColumnGap,
2022-05-22 12:54:02 +03:00
ColumnCount, ColumnWidth, ColumnSeparator, ColumnGap, AvoidBreak,
Current, Expanded, Side, ResizeBorderWidth, EditViewType, MaxLength, Hint, Text, EditWrap,
2022-05-22 12:54:02 +03:00
TextOverflow, FontName, TextSize, TextColor, TextWeight, Italic, SmallCaps,
Strikethrough, Overline, Underline, TextLineStyle, TextLineThickness,
TextLineColor, TextTransform, TextAlign, WhiteSpace, WordBreak, TextShadow, TextIndent,
LetterSpacing, WordSpacing, LineHeight, TextDirection, WritingMode, VerticalTextOrientation,
}
for _, tag := range tagOrder {
if value := view.Get(tag); value != nil {
removeTag(tag)
writeProperty(tag, value)
}
}
2024-11-13 12:56:39 +03:00
finalTags := []PropertyName{
PerspectiveOriginX, PerspectiveOriginY, BackfaceVisible,
TransformOriginX, TransformOriginY, TransformOriginZ,
Transform, Clip, Filter, BackdropFilter, Summary, Content, Transition}
2022-05-22 12:54:02 +03:00
for _, tag := range finalTags {
removeTag(tag)
}
for _, tag := range tags {
if value := view.Get(tag); value != nil {
writeProperty(tag, value)
}
}
for _, tag := range finalTags {
if value := view.Get(tag); value != nil {
writeProperty(tag, value)
}
}
indent = indent[:len(indent)-1]
buffer.WriteString(indent)
buffer.WriteString("}")
}
func runStringWriter(writer stringWriter) string {
buffer := allocStringBuilder()
defer freeStringBuilder(buffer)
writer.writeString(buffer, "")
return buffer.String()
}