2021-09-07 17:36:50 +03:00
|
|
|
package rui
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
2024-09-12 14:05:11 +03:00
|
|
|
// 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
|
2024-09-12 14:05:11 +03:00
|
|
|
|
|
|
|
// 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 {
|
|
|
|
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
|
2021-10-04 18:05:57 +03:00
|
|
|
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)
|
|
|
|
|
2021-10-04 18:05:57 +03:00
|
|
|
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)
|
2021-10-04 18:05:57 +03:00
|
|
|
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 {
|
2024-09-12 14:05:11 +03:00
|
|
|
// Style of the outline line
|
2021-09-07 17:36:50 +03:00
|
|
|
Style int
|
2024-09-12 14:05:11 +03:00
|
|
|
|
|
|
|
// Color of the outline line
|
2021-09-07 17:36:50 +03:00
|
|
|
Color Color
|
2024-09-12 14:05:11 +03:00
|
|
|
|
|
|
|
// Width of the outline line
|
2021-09-07 17:36:50 +03:00
|
|
|
Width SizeUnit
|
|
|
|
}
|
|
|
|
|
2022-09-05 14:00:07 +03:00
|
|
|
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 {
|
2022-09-05 14:00:07 +03:00
|
|
|
builder.addValues("outline", " ", outline.Width.cssString("0", session), values[outline.Style], outline.Color.cssString())
|
2021-09-07 17:36:50 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-05 14:00:07 +03:00
|
|
|
func (outline ViewOutline) cssString(session Session) string {
|
2021-09-07 17:36:50 +03:00
|
|
|
var builder cssValueBuilder
|
2022-09-05 14:00:07 +03:00
|
|
|
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:
|
2021-10-04 18:05:57 +03:00
|
|
|
style.properties[Outline] = NewOutlineProperty(Params{Style: value.Style, Width: value.Width, ColorTag: value.Color})
|
2021-09-07 17:36:50 +03:00
|
|
|
|
|
|
|
case ViewBorder:
|
2021-10-04 18:05:57 +03:00
|
|
|
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)
|
2021-10-04 18:05:57 +03:00
|
|
|
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
|
|
|
|
}
|