rui_orig/outline.go

166 lines
4.4 KiB
Go
Raw Normal View History

2021-09-07 17:36:50 +03:00
package rui
import (
"fmt"
"strings"
)
// OutlineProperty defines a view's outside border
2021-09-07 17:36:50 +03:00
type OutlineProperty interface {
Properties
2022-05-22 12:54:02 +03:00
stringWriter
2021-09-07 17:36:50 +03:00
fmt.Stringer
// ViewOutline returns style color and line width of an outline
2021-09-07 17:36:50 +03:00
ViewOutline(session Session) ViewOutline
}
type outlinePropertyData struct {
2024-11-13 12:56:39 +03:00
dataProperty
2021-09-07 17:36:50 +03:00
}
2024-10-21 18:37:35 +03:00
// NewOutlineProperty creates the new OutlineProperty.
// The following properties can be used:
//
// "color" (ColorTag). Determines the line color (Color);
//
// "width" (Width). Determines the line thickness (SizeUnit).
2021-09-07 17:36:50 +03:00
func NewOutlineProperty(params Params) OutlineProperty {
outline := new(outlinePropertyData)
2024-11-13 12:56:39 +03:00
outline.init()
2021-09-07 17:36:50 +03:00
for tag, value := range params {
outline.Set(tag, value)
}
return outline
}
2024-11-13 12:56:39 +03:00
func (outline *outlinePropertyData) init() {
outline.propertyList.init()
outline.normalize = normalizeOutlineTag
outline.set = outlineSet
outline.supportedProperties = []PropertyName{Style, Width, ColorTag}
}
2022-05-22 12:54:02 +03:00
func (outline *outlinePropertyData) writeString(buffer *strings.Builder, indent string) {
buffer.WriteString("_{ ")
comma := false
2024-11-13 12:56:39 +03:00
for _, tag := range []PropertyName{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(", ")
}
2024-11-13 12:56:39 +03:00
buffer.WriteString(string(tag))
2022-05-22 12:54:02 +03:00
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
}
2024-11-13 12:56:39 +03:00
func normalizeOutlineTag(tag PropertyName) PropertyName {
tag = defaultNormalize(tag)
return PropertyName(strings.TrimPrefix(string(tag), "outline-"))
2021-09-07 17:36:50 +03:00
}
2024-11-13 12:56:39 +03:00
func outlineSet(properties Properties, tag PropertyName, value any) []PropertyName {
2021-09-07 17:36:50 +03:00
switch tag {
case Style:
2024-11-13 12:56:39 +03:00
return setEnumProperty(properties, Style, value, enumProperties[BorderStyle].values)
2021-09-07 17:36:50 +03:00
case Width:
if width, ok := value.(SizeUnit); ok {
switch width.Type {
case SizeInFraction, SizeInPercent:
notCompatibleType(tag, value)
2024-11-13 12:56:39 +03:00
return nil
2021-09-07 17:36:50 +03:00
}
}
2024-11-13 12:56:39 +03:00
return setSizeProperty(properties, Width, value)
2021-09-07 17:36:50 +03:00
case ColorTag:
2024-11-13 12:56:39 +03:00
return setColorProperty(properties, ColorTag, value)
2021-09-07 17:36:50 +03:00
default:
ErrorLogF(`"%s" property is not compatible with the OutlineProperty`, tag)
}
2024-11-13 12:56:39 +03:00
return nil
2021-09-07 17:36:50 +03:00
}
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 of the outline line
2021-09-07 17:36:50 +03:00
Style int
// Color of the outline line
2021-09-07 17:36:50 +03:00
Color Color
// Width of the outline line
2021-09-07 17:36:50 +03:00
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()
}
2024-11-13 12:56:39 +03:00
func getOutlineProperty(properties Properties) OutlineProperty {
2021-09-07 17:36:50 +03:00
if value := properties.Get(Outline); value != nil {
if outline, ok := value.(OutlineProperty); ok {
return outline
}
}
return nil
}
2024-11-13 12:56:39 +03:00
func setOutlineProperty(properties Properties, value any) []PropertyName {
2021-09-07 17:36:50 +03:00
switch value := value.(type) {
case OutlineProperty:
2024-11-13 12:56:39 +03:00
properties.setRaw(Outline, value)
2021-09-07 17:36:50 +03:00
case ViewOutline:
2024-11-13 12:56:39 +03:00
properties.setRaw(Outline, NewOutlineProperty(Params{Style: value.Style, Width: value.Width, ColorTag: value.Color}))
2021-09-07 17:36:50 +03:00
case ViewBorder:
2024-11-13 12:56:39 +03:00
properties.setRaw(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)
2024-11-13 12:56:39 +03:00
for _, tag := range []PropertyName{Style, Width, ColorTag} {
if text, ok := value.PropertyValue(string(tag)); ok && text != "" {
2021-09-07 17:36:50 +03:00
outline.Set(tag, text)
}
}
2024-11-13 12:56:39 +03:00
properties.setRaw(Outline, outline)
2021-09-07 17:36:50 +03:00
default:
notCompatibleType(Outline, value)
2024-11-13 12:56:39 +03:00
return nil
2021-09-07 17:36:50 +03:00
}
2024-11-13 12:56:39 +03:00
return []PropertyName{Outline}
2021-09-07 17:36:50 +03:00
}