forked from mbk-lab/rui_orig
2
0
Fork 0
rui/outline.go

158 lines
4.0 KiB
Go
Raw Normal View History

2021-09-07 17:36:50 +03:00
package rui
import (
"fmt"
"strings"
)
type OutlineProperty interface {
Properties
2022-05-22 12:54:02 +03:00
stringWriter
2021-09-07 17:36:50 +03:00
fmt.Stringer
ViewOutline(session Session) ViewOutline
}
type outlinePropertyData struct {
propertyList
}
func NewOutlineProperty(params Params) OutlineProperty {
outline := new(outlinePropertyData)
2022-07-26 18:36:00 +03:00
outline.properties = map[string]any{}
2021-09-07 17:36:50 +03:00
for tag, value := range params {
outline.Set(tag, value)
}
return outline
}
2022-05-22 12:54:02 +03:00
func (outline *outlinePropertyData) writeString(buffer *strings.Builder, indent string) {
buffer.WriteString("_{ ")
comma := false
for _, tag := range []string{Style, Width, ColorTag} {
2021-09-07 17:36:50 +03:00
if value, ok := outline.properties[tag]; ok {
2022-05-22 12:54:02 +03:00
if comma {
buffer.WriteString(", ")
}
buffer.WriteString(tag)
buffer.WriteString(" = ")
writePropertyValue(buffer, BorderStyle, value, indent)
comma = true
2021-09-07 17:36:50 +03:00
}
}
2022-05-22 12:54:02 +03:00
buffer.WriteString(" }")
2021-09-07 17:36:50 +03:00
}
func (outline *outlinePropertyData) String() string {
2022-05-22 12:54:02 +03:00
return runStringWriter(outline)
2021-09-07 17:36:50 +03:00
}
func (outline *outlinePropertyData) normalizeTag(tag string) string {
return strings.TrimPrefix(strings.ToLower(tag), "outline-")
}
func (outline *outlinePropertyData) Remove(tag string) {
delete(outline.properties, outline.normalizeTag(tag))
}
2022-07-26 18:36:00 +03:00
func (outline *outlinePropertyData) Set(tag string, value any) bool {
2021-09-07 17:36:50 +03:00
if value == nil {
outline.Remove(tag)
return true
}
tag = outline.normalizeTag(tag)
switch tag {
case Style:
return outline.setEnumProperty(Style, value, enumProperties[BorderStyle].values)
case Width:
if width, ok := value.(SizeUnit); ok {
switch width.Type {
case SizeInFraction, SizeInPercent:
notCompatibleType(tag, value)
return false
}
}
return outline.setSizeProperty(Width, value)
case ColorTag:
return outline.setColorProperty(ColorTag, value)
2021-09-07 17:36:50 +03:00
default:
ErrorLogF(`"%s" property is not compatible with the OutlineProperty`, tag)
}
return false
}
2022-07-26 18:36:00 +03:00
func (outline *outlinePropertyData) Get(tag string) any {
2021-09-07 17:36:50 +03:00
return outline.propertyList.Get(outline.normalizeTag(tag))
}
func (outline *outlinePropertyData) ViewOutline(session Session) ViewOutline {
style, _ := valueToEnum(outline.getRaw(Style), BorderStyle, session, NoneLine)
width, _ := sizeProperty(outline, Width, session)
color, _ := colorProperty(outline, ColorTag, session)
2021-09-07 17:36:50 +03:00
return ViewOutline{Style: style, Width: width, Color: color}
}
// ViewOutline describes parameters of a view border
type ViewOutline struct {
Style int
Color Color
Width SizeUnit
}
func (outline ViewOutline) cssValue(builder cssBuilder, session Session) {
2021-09-07 17:36:50 +03:00
values := enumProperties[BorderStyle].cssValues
if outline.Style > 0 && outline.Style < len(values) && outline.Color.Alpha() > 0 &&
outline.Width.Type != Auto && outline.Width.Type != SizeInFraction &&
outline.Width.Type != SizeInPercent && outline.Width.Value > 0 {
builder.addValues("outline", " ", outline.Width.cssString("0", session), values[outline.Style], outline.Color.cssString())
2021-09-07 17:36:50 +03:00
}
}
func (outline ViewOutline) cssString(session Session) string {
2021-09-07 17:36:50 +03:00
var builder cssValueBuilder
outline.cssValue(&builder, session)
2021-09-07 17:36:50 +03:00
return builder.finish()
}
func getOutline(properties Properties) OutlineProperty {
if value := properties.Get(Outline); value != nil {
if outline, ok := value.(OutlineProperty); ok {
return outline
}
}
return nil
}
2022-07-26 18:36:00 +03:00
func (style *viewStyle) setOutline(value any) bool {
2021-09-07 17:36:50 +03:00
switch value := value.(type) {
case OutlineProperty:
style.properties[Outline] = value
case ViewOutline:
style.properties[Outline] = NewOutlineProperty(Params{Style: value.Style, Width: value.Width, ColorTag: value.Color})
2021-09-07 17:36:50 +03:00
case ViewBorder:
style.properties[Outline] = NewOutlineProperty(Params{Style: value.Style, Width: value.Width, ColorTag: value.Color})
2021-09-07 17:36:50 +03:00
case DataObject:
outline := NewOutlineProperty(nil)
for _, tag := range []string{Style, Width, ColorTag} {
2021-09-07 17:36:50 +03:00
if text, ok := value.PropertyValue(tag); ok && text != "" {
outline.Set(tag, text)
}
}
style.properties[Outline] = outline
default:
notCompatibleType(Outline, value)
return false
}
return true
}