rui_orig/gridLayout.go

378 lines
9.9 KiB
Go
Raw Normal View History

2021-09-07 17:36:50 +03:00
package rui
import (
"fmt"
"strings"
)
// GridLayout - grid-container of View
type GridLayout interface {
ViewsContainer
}
type gridLayoutData struct {
viewsContainerData
}
// NewGridLayout create new GridLayout object and return it
func NewGridLayout(session Session, params Params) GridLayout {
view := new(gridLayoutData)
view.init(session)
2021-09-07 17:36:50 +03:00
setInitParams(view, params)
return view
}
func newGridLayout(session Session) View {
return NewGridLayout(session, nil)
}
// Init initialize fields of GridLayout by default values
func (gridLayout *gridLayoutData) init(session Session) {
gridLayout.viewsContainerData.init(session)
2021-09-07 17:36:50 +03:00
gridLayout.tag = "GridLayout"
gridLayout.systemClass = "ruiGridLayout"
}
2022-05-22 12:54:02 +03:00
func (gridLayout *gridLayoutData) String() string {
return getViewString(gridLayout)
}
2022-07-26 18:36:00 +03:00
func (style *viewStyle) setGridCellSize(tag string, value any) bool {
2021-09-07 17:36:50 +03:00
setValues := func(values []string) bool {
count := len(values)
if count > 1 {
2022-07-26 18:36:00 +03:00
sizes := make([]any, count)
2021-09-07 17:36:50 +03:00
for i, val := range values {
val = strings.Trim(val, " \t\n\r")
if isConstantName(val) {
sizes[i] = val
2022-09-06 09:57:33 +03:00
} else if fn := parseSizeFunc(val); fn != nil {
sizes[i] = SizeUnit{Type: SizeFunction, Function: fn}
2022-05-01 13:27:04 +03:00
} else if size, err := stringToSizeUnit(val); err == nil {
2021-09-07 17:36:50 +03:00
sizes[i] = size
} else {
invalidPropertyValue(tag, value)
return false
}
}
style.properties[tag] = sizes
} else if isConstantName(values[0]) {
style.properties[tag] = values[0]
2022-05-01 13:27:04 +03:00
} else if size, err := stringToSizeUnit(values[0]); err == nil {
2021-09-07 17:36:50 +03:00
style.properties[tag] = size
} else {
invalidPropertyValue(tag, value)
return false
}
return true
}
switch tag {
case CellWidth, CellHeight:
switch value := value.(type) {
case SizeUnit, []SizeUnit:
style.properties[tag] = value
case string:
if !setValues(strings.Split(value, ",")) {
return false
}
case []string:
if !setValues(value) {
return false
}
case []DataValue:
count := len(value)
if count == 0 {
invalidPropertyValue(tag, value)
return false
}
values := make([]string, count)
for i, val := range value {
if val.IsObject() {
invalidPropertyValue(tag, value)
return false
}
values[i] = val.Value()
}
if !setValues(values) {
return false
}
2022-07-26 18:36:00 +03:00
case []any:
2021-09-07 17:36:50 +03:00
count := len(value)
if count == 0 {
invalidPropertyValue(tag, value)
return false
}
2022-07-26 18:36:00 +03:00
sizes := make([]any, count)
2021-09-07 17:36:50 +03:00
for i, val := range value {
switch val := val.(type) {
case SizeUnit:
sizes[i] = val
case string:
if isConstantName(val) {
sizes[i] = val
2022-05-01 13:27:04 +03:00
} else if size, err := stringToSizeUnit(val); err == nil {
2021-09-07 17:36:50 +03:00
sizes[i] = size
} else {
invalidPropertyValue(tag, value)
return false
}
default:
invalidPropertyValue(tag, value)
return false
}
}
style.properties[tag] = sizes
default:
notCompatibleType(tag, value)
return false
}
return true
}
return false
}
func (style *viewStyle) gridCellSizesCSS(tag string, session Session) string {
switch cellSize := gridCellSizes(style, tag, session); len(cellSize) {
case 0:
case 1:
if cellSize[0].Type != Auto {
return `repeat(auto-fill, ` + cellSize[0].cssString(`auto`, session) + `)`
2021-09-07 17:36:50 +03:00
}
default:
allAuto := true
allEqual := true
for i, size := range cellSize {
if size.Type != Auto {
allAuto = false
}
if i > 0 && !size.Equal(cellSize[0]) {
allEqual = false
}
}
if !allAuto {
if allEqual {
return fmt.Sprintf(`repeat(%d, %s)`, len(cellSize), cellSize[0].cssString(`auto`, session))
2021-09-07 17:36:50 +03:00
}
buffer := allocStringBuilder()
defer freeStringBuilder(buffer)
for _, size := range cellSize {
buffer.WriteRune(' ')
buffer.WriteString(size.cssString(`auto`, session))
2021-09-07 17:36:50 +03:00
}
return buffer.String()
}
}
return ""
}
func (gridLayout *gridLayoutData) normalizeTag(tag string) string {
tag = strings.ToLower(tag)
switch tag {
case VerticalAlign:
return CellVerticalAlign
case HorizontalAlign:
return CellHorizontalAlign
case "row-gap":
return GridRowGap
case ColumnGap:
return GridColumnGap
}
return tag
}
2022-07-26 18:36:00 +03:00
func (gridLayout *gridLayoutData) Get(tag string) any {
2021-09-07 17:36:50 +03:00
return gridLayout.get(gridLayout.normalizeTag(tag))
}
2022-07-26 18:36:00 +03:00
func (gridLayout *gridLayoutData) get(tag string) any {
2021-09-07 17:36:50 +03:00
if tag == Gap {
rowGap := GetGridRowGap(gridLayout)
columnGap := GetGridColumnGap(gridLayout)
2021-09-07 17:36:50 +03:00
if rowGap.Equal(columnGap) {
return rowGap
}
return AutoSize()
}
return gridLayout.viewsContainerData.get(tag)
}
func (gridLayout *gridLayoutData) Remove(tag string) {
gridLayout.remove(gridLayout.normalizeTag(tag))
}
func (gridLayout *gridLayoutData) remove(tag string) {
if tag == Gap {
gridLayout.remove(GridRowGap)
gridLayout.remove(GridColumnGap)
gridLayout.propertyChangedEvent(Gap)
2021-09-07 17:36:50 +03:00
return
}
gridLayout.viewsContainerData.remove(tag)
if gridLayout.created {
switch tag {
case CellWidth:
2022-10-30 17:22:33 +03:00
gridLayout.session.updateCSSProperty(gridLayout.htmlID(), `grid-template-columns`,
gridLayout.gridCellSizesCSS(CellWidth, gridLayout.session))
2021-09-07 17:36:50 +03:00
case CellHeight:
2022-10-30 17:22:33 +03:00
gridLayout.session.updateCSSProperty(gridLayout.htmlID(), `grid-template-rows`,
gridLayout.gridCellSizesCSS(CellHeight, gridLayout.session))
2021-09-07 17:36:50 +03:00
}
2021-09-07 17:36:50 +03:00
}
}
2022-07-26 18:36:00 +03:00
func (gridLayout *gridLayoutData) Set(tag string, value any) bool {
2021-09-07 17:36:50 +03:00
return gridLayout.set(gridLayout.normalizeTag(tag), value)
}
2022-07-26 18:36:00 +03:00
func (gridLayout *gridLayoutData) set(tag string, value any) bool {
2021-09-07 17:36:50 +03:00
if value == nil {
gridLayout.remove(tag)
return true
}
if tag == Gap {
return gridLayout.set(GridRowGap, value) && gridLayout.set(GridColumnGap, value)
2021-09-07 17:36:50 +03:00
}
if gridLayout.viewsContainerData.set(tag, value) {
if gridLayout.created {
switch tag {
case CellWidth:
2022-10-30 17:22:33 +03:00
gridLayout.session.updateCSSProperty(gridLayout.htmlID(), `grid-template-columns`,
gridLayout.gridCellSizesCSS(CellWidth, gridLayout.session))
2021-09-07 17:36:50 +03:00
case CellHeight:
2022-10-30 17:22:33 +03:00
gridLayout.session.updateCSSProperty(gridLayout.htmlID(), `grid-template-rows`,
gridLayout.gridCellSizesCSS(CellHeight, gridLayout.session))
2021-09-07 17:36:50 +03:00
}
2021-09-07 17:36:50 +03:00
}
return true
}
return false
}
func gridCellSizes(properties Properties, tag string, session Session) []SizeUnit {
if value := properties.Get(tag); value != nil {
switch value := value.(type) {
case []SizeUnit:
return value
case SizeUnit:
return []SizeUnit{value}
2022-07-26 18:36:00 +03:00
case []any:
2021-09-07 17:36:50 +03:00
result := make([]SizeUnit, len(value))
for i, val := range value {
result[i] = AutoSize()
switch val := val.(type) {
case SizeUnit:
result[i] = val
case string:
if text, ok := session.resolveConstants(val); ok {
2022-05-01 13:27:04 +03:00
result[i], _ = stringToSizeUnit(text)
2021-09-07 17:36:50 +03:00
}
}
}
return result
case string:
if text, ok := session.resolveConstants(value); ok {
values := strings.Split(text, ",")
result := make([]SizeUnit, len(values))
for i, val := range values {
2022-05-01 13:27:04 +03:00
result[i], _ = stringToSizeUnit(val)
2021-09-07 17:36:50 +03:00
}
return result
}
}
}
return []SizeUnit{}
}
2021-11-20 16:53:45 +03:00
/*
2021-09-07 17:36:50 +03:00
func (gridLayout *gridLayoutData) cssStyle(self View, builder cssBuilder) {
gridLayout.viewsContainerData.cssStyle(self, builder)
}
2021-11-20 16:53:45 +03:00
*/
2021-09-07 17:36:50 +03:00
// GetCellVerticalAlign returns the vertical align of a GridLayout cell content: TopAlign (0), BottomAlign (1), CenterAlign (2), StretchAlign (3)
// If the second argument (subviewID) is not specified or it is "" then a value from the first argument (view) is returned.
func GetCellVerticalAlign(view View, subviewID ...string) int {
2022-07-28 12:41:50 +03:00
return enumStyledProperty(view, subviewID, CellVerticalAlign, StretchAlign, false)
2021-09-07 17:36:50 +03:00
}
// GetCellHorizontalAlign returns the vertical align of a GridLayout cell content: LeftAlign (0), RightAlign (1), CenterAlign (2), StretchAlign (3)
// If the second argument (subviewID) is not specified or it is "" then a value from the first argument (view) is returned.
func GetCellHorizontalAlign(view View, subviewID ...string) int {
2022-07-28 12:41:50 +03:00
return enumStyledProperty(view, subviewID, CellHorizontalAlign, StretchAlign, false)
2021-09-07 17:36:50 +03:00
}
// GetGridAutoFlow returns the value of the "grid-auto-flow" property
// If the second argument (subviewID) is not specified or it is "" then a value from the first argument (view) is returned.
func GetGridAutoFlow(view View, subviewID ...string) int {
2022-07-28 12:41:50 +03:00
return enumStyledProperty(view, subviewID, GridAutoFlow, 0, false)
}
2021-09-07 17:36:50 +03:00
// GetCellWidth returns the width of a GridLayout cell. If the result is an empty array, then the width is not set.
// If the result is a single value array, then the width of all cell is equal.
// If the second argument (subviewID) is not specified or it is "" then a value from the first argument (view) is returned.
func GetCellWidth(view View, subviewID ...string) []SizeUnit {
if len(subviewID) > 0 && subviewID[0] != "" {
view = ViewByID(view, subviewID[0])
2021-09-07 17:36:50 +03:00
}
if view != nil {
return gridCellSizes(view, CellWidth, view.Session())
}
return []SizeUnit{}
}
// GetCellHeight returns the height of a GridLayout cell. If the result is an empty array, then the height is not set.
// If the result is a single value array, then the height of all cell is equal.
// If the second argument (subviewID) is not specified or it is "" then a value from the first argument (view) is returned.
func GetCellHeight(view View, subviewID ...string) []SizeUnit {
if len(subviewID) > 0 && subviewID[0] != "" {
view = ViewByID(view, subviewID[0])
2021-09-07 17:36:50 +03:00
}
if view != nil {
return gridCellSizes(view, CellHeight, view.Session())
}
return []SizeUnit{}
}
// GetGridRowGap returns the gap between GridLayout rows.
// If the second argument (subviewID) is not specified or it is "" then a value from the first argument (view) is returned.
func GetGridRowGap(view View, subviewID ...string) SizeUnit {
2022-07-28 12:53:50 +03:00
return sizeStyledProperty(view, subviewID, GridRowGap, false)
2021-09-07 17:36:50 +03:00
}
// GetGridColumnGap returns the gap between GridLayout columns.
// If the second argument (subviewID) is not specified or it is "" then a value from the first argument (view) is returned.
func GetGridColumnGap(view View, subviewID ...string) SizeUnit {
2022-07-28 12:53:50 +03:00
return sizeStyledProperty(view, subviewID, GridColumnGap, false)
2021-09-07 17:36:50 +03:00
}