diff --git a/cstyle/plugins/flex/main.go b/cstyle/plugins/flex/main.go
index 67c7378..7862a13 100644
--- a/cstyle/plugins/flex/main.go
+++ b/cstyle/plugins/flex/main.go
@@ -6 +5,0 @@ import (
- "gui/cstyle/plugins/inline"
@@ -9 +7,0 @@ import (
- "sort"
@@ -27 +25 @@ func Init() cstyle.Plugin {
- Level: 3,
+ Level: 1,
@@ -57,5 +54,0 @@ func Init() cstyle.Plugin {
- textTotal := 0
- textCounts := []int{}
- widths := []float32{}
- innerSizes := [][]float32{}
- minWidths := []float32{}
@@ -63,47 +55,0 @@ func Init() cstyle.Plugin {
- // vState := s[v.Properties.Id]
- count := countText(v)
- textTotal += count
- textCounts = append(textCounts, count)
-
- minw := getMinWidth(&v, state)
- minWidths = append(minWidths, minw)
-
- w, h := getInnerSize(&v, state)
- innerSizes = append(innerSizes, []float32{w, h})
- }
- selfWidth := (self.Width - self.Padding.Left) - self.Padding.Right
- // If the elements are under the size of the container to need to resize
- if add2d(innerSizes, 0) < selfWidth {
- for _, v := range innerSizes {
- widths = append(widths, v[0])
- }
- } else {
- // Modifiy the widths so they aren't under the mins
- for i, v := range n.Children {
- vState := s[v.Properties.Id]
-
- w := ((selfWidth / float32(textTotal)) * float32(textCounts[i]))
- w -= vState.Margin.Left + vState.Margin.Right + (vState.Border.Width * 2)
-
- if w < minWidths[i] {
- selfWidth -= minWidths[i] + vState.Margin.Left + vState.Margin.Right + (vState.Border.Width * 2)
- textTotal -= textCounts[i]
- textCounts[i] = 0
- }
-
- }
- for i, v := range n.Children {
- vState := s[v.Properties.Id]
-
- w := ((selfWidth / float32(textTotal)) * float32(textCounts[i]))
- w -= vState.Margin.Left + vState.Margin.Right + (vState.Border.Width * 2)
- if w < minWidths[i] {
- w = minWidths[i]
- }
-
- widths = append(widths, w)
- }
- }
- // Apply the new widths
- fState := s[n.Children[0].Properties.Id]
- for i, v := range n.Children {
@@ -112,27 +58,10 @@ func Init() cstyle.Plugin {
- vState.Width = widths[i]
- xStore := vState.X
- if i > 0 {
- sState := s[n.Children[i-1].Properties.Id]
- vState.X = sState.X + sState.Width + sState.Margin.Right + vState.Margin.Left + sState.Border.Width + vState.Border.Width
- propagateOffsets(&v, xStore, vState.Y, vState.X, fState.Y, state)
- }
-
- vState.Y = fState.Y
-
- (*state)[v.Properties.Id] = vState
- deInline(&v, state)
- applyInline(&v, state)
- applyBlock(&v, state)
- }
- // Set the heights based on the tallest one
- innerSizes = [][]float32{}
- for _, v := range n.Children {
- w, h := getInnerSize(&v, state)
- innerSizes = append(innerSizes, []float32{w, h})
- }
- sort.Slice(innerSizes, func(i, j int) bool {
- return innerSizes[i][1] > innerSizes[j][1]
- })
- for _, v := range n.Children {
- vState := s[v.Properties.Id]
- vState.Height = innerSizes[0][1]
+ fmt.Println(getMinSize(&v, vState))
+ fmt.Println(getInnerSize(&v, s))
+ pw, ph := getMinSize(&v, vState)
+ w, h := getInnerSize(&v, s)
+ w += pw
+ h += ph
+ // !INSIGHT: if you change the width of the element then you need to re line the text...
+ vState.Width = 100
+ // vState.Width = w
+ vState.Height = h
@@ -143,2 +72 @@ func Init() cstyle.Plugin {
- _, h := getInnerSize(n, state)
- self.Height = h
+
@@ -150,118 +78,5 @@ func Init() cstyle.Plugin {
-func applyBlock(n *element.Node, state *map[string]element.State) {
- accum := float32(0)
- inlineOffset := float32(0)
- s := *state
- baseY := s[n.Children[0].Properties.Id].Y
- for i := 0; i < len(n.Children); i++ {
- v := &n.Children[i]
- vState := s[v.Properties.Id]
-
- if v.Style["display"] != "block" {
- accum = (vState.Y - baseY)
- vState.Y += inlineOffset
- } else if v.Style["position"] != "absolute" {
- vState.Y += accum
- // !ISSUE: need to account fo rmultiple block elements and margin padding border and non asbsoulute
- inlineOffset = ((vState.Y - baseY) + (vState.Height + (vState.Border.Width * 2) + vState.Margin.Top + vState.Margin.Bottom + vState.Padding.Top + vState.Padding.Bottom)) - accum
- accum = 0
- }
- (*state)[v.Properties.Id] = vState
- }
-}
-
-func deInline(n *element.Node, state *map[string]element.State) {
- s := *state
- // self := s[n.Properties.Id]
- baseX := float32(-1)
- baseY := float32(-1)
- for _, v := range n.Children {
- vState := s[v.Properties.Id]
-
- if v.Style["display"] == "inline" {
- if baseX < 0 && baseY < 0 {
- baseX = vState.X
- baseY = vState.Y
- } else {
- vState.X = baseX
- vState.Y = baseY
- (*state)[v.Properties.Id] = vState
-
- }
- } else {
- baseX = float32(-1)
- baseY = float32(-1)
- }
-
- if len(v.Children) > 0 {
- deInline(&v, state)
- }
- }
-
-}
-
-func applyInline(n *element.Node, state *map[string]element.State) {
- pl := inline.Init()
- for i := 0; i < len(n.Children); i++ {
- v := &n.Children[i]
-
- if len(v.Children) > 0 {
- applyInline(v, state)
- }
-
- if pl.Selector(v) {
- pl.Handler(v, state)
- }
- }
-}
-
-func propagateOffsets(n *element.Node, prevx, prevy, newx, newy float32, state *map[string]element.State) {
- s := *state
- for _, v := range n.Children {
- vState := s[v.Properties.Id]
- xStore := (vState.X - prevx) + newx
- yStore := (vState.Y - prevy) + newy
-
- if len(v.Children) > 0 {
- propagateOffsets(&v, vState.X, vState.Y, xStore, yStore, state)
- }
- vState.X = xStore
- vState.Y = yStore
- (*state)[v.Properties.Id] = vState
- }
-
-}
-
-func countText(n element.Node) int {
- count := 0
- groups := []int{}
- for _, v := range n.Children {
- if v.TagName == "notaspan" {
- count += 1
- }
- if v.Style["display"] == "block" {
- groups = append(groups, count)
- count = 0
- }
- if len(v.Children) > 0 {
- count += countText(v)
- }
- }
- groups = append(groups, count)
-
- sort.Slice(groups, func(i, j int) bool {
- return groups[i] > groups[j]
- })
- return groups[0]
-}
-
-func getMinWidth(n *element.Node, state *map[string]element.State) float32 {
- s := *state
- self := s[n.Properties.Id]
- selfWidth := float32(0)
-
- if len(n.Children) > 0 {
- for _, v := range n.Children {
- selfWidth = utils.Max(selfWidth, getNodeWidth(&v, state))
- }
- } else {
- selfWidth = self.Width
+func getMinSize(n *element.Node, s element.State) (float32, float32) {
+ minW := s.Padding.Left + s.Padding.Right
+ minH := s.Padding.Top + s.Padding.Bottom
+ if n.Style["width"] != "" {
+ minW = s.Width
@@ -269,21 +84,2 @@ func getMinWidth(n *element.Node, state *map[string]element.State) float32 {
-
- selfWidth += self.Padding.Left + self.Padding.Right
- return selfWidth
-}
-
-func getNodeWidth(n *element.Node, state *map[string]element.State) float32 {
- s := *state
- self := s[n.Properties.Id]
- w := float32(0)
- w += self.Padding.Left
- w += self.Padding.Right
-
- w += self.Margin.Left
- w += self.Margin.Right
-
- w += self.Width
-
- w += self.Border.Width * 2
-
- for _, v := range n.Children {
- w = utils.Max(w, getNodeWidth(&v, state))
+ if n.Style["height"] != "" {
+ minH = s.Height
@@ -291,2 +87 @@ func getNodeWidth(n *element.Node, state *map[string]element.State) float32 {
-
- return w
+ return minW, minH
@@ -295,4 +90 @@ func getNodeWidth(n *element.Node, state *map[string]element.State) float32 {
-func getInnerSize(n *element.Node, state *map[string]element.State) (float32, float32) {
- s := *state
- self := s[n.Properties.Id]
-
+func getInnerSize(n *element.Node, s map[string]element.State) (float32, float32) {
@@ -307,12 +98,0 @@ func getInnerSize(n *element.Node, state *map[string]element.State) (float32, fl
- hOffset := (vState.Border.Width * 2) + vState.Margin.Top + vState.Margin.Bottom
- wOffset := (vState.Border.Width * 2) + vState.Margin.Left + vState.Margin.Right
- maxw = utils.Max(vState.X+vState.Width+wOffset, maxw)
- maxh = utils.Max(vState.Y+vState.Height+hOffset, maxh)
- }
- w := maxw - minx
- h := maxh - miny
-
- w += self.Padding.Left + self.Padding.Right
- h += self.Padding.Top + self.Padding.Bottom
- return w, h
-}
@@ -320,4 +100,2 @@ func getInnerSize(n *element.Node, state *map[string]element.State) (float32, fl
-func add2d(arr [][]float32, index int) float32 {
- var sum float32
- if len(arr) == 0 {
- return sum
+ maxw = utils.Max(vState.X+vState.Width, maxw)
+ maxh = utils.Max(vState.Y+vState.Height, maxh)
@@ -325,9 +103 @@ func add2d(arr [][]float32, index int) float32 {
-
- for i := 0; i < len(arr); i++ {
- if len(arr[i]) <= index {
- return sum
- }
- sum += arr[i][index]
- }
-
- return sum
+ return maxw - minx, maxh - miny
@@ -335,4 +104,0 @@ func add2d(arr [][]float32, index int) float32 {
-
-// getMinHeight(n,state)
-// calcHeight(n,width,state)
-// calcWidth(n,height,state)
package flex
import (
"fmt"
"gui/cstyle"
"gui/cstyle/plugins/inline"
"gui/element"
"gui/utils"
"sort"
"strings"
)
func Init() cstyle.Plugin {
return cstyle.Plugin{
Selector: func(n *element.Node) bool {
styles := map[string]string{
"display": "flex",
}
matches := true
for name, value := range styles {
if n.Style[name] != value && !(value == "*") && n.Style[name] != "" {
matches = false
}
}
return matches
},
Level: 3,
Handler: func(n *element.Node, state *map[string]element.State) {
s := *state
self := s[n.Properties.Id]
verbs := strings.Split(n.Style["flex-direction"], "-")
flexDirection := verbs[0]
flexReversed := false
if len(verbs) > 1 {
flexReversed = true
}
flexWrapped := !(n.Style["flex-wrap"] != "nowrap")
hAlign := n.Style["align-content"]
if hAlign == "" {
hAlign = "normal"
}
vAlign := n.Style["align-items"]
if vAlign == "" {
vAlign = "normal"
}
justify := n.Style["justify-items"]
if justify == "" {
justify = "normal"
}
fmt.Println(flexDirection, flexReversed, flexWrapped, hAlign, vAlign, justify)
if flexDirection == "row" && !flexReversed && !flexWrapped {
textTotal := 0
textCounts := []int{}
widths := []float32{}
innerSizes := [][]float32{}
minWidths := []float32{}
for _, v := range n.Children {
// vState := s[v.Properties.Id]
count := countText(v)
textTotal += count
textCounts = append(textCounts, count)
minw := getMinWidth(&v, state)
minWidths = append(minWidths, minw)
w, h := getInnerSize(&v, state)
innerSizes = append(innerSizes, []float32{w, h})
}
selfWidth := (self.Width - self.Padding.Left) - self.Padding.Right
// If the elements are under the size of the container to need to resize
if add2d(innerSizes, 0) < selfWidth {
for _, v := range innerSizes {
widths = append(widths, v[0])
}
} else {
// Modifiy the widths so they aren't under the mins
for i, v := range n.Children {
vState := s[v.Properties.Id]
w := ((selfWidth / float32(textTotal)) * float32(textCounts[i]))
w -= vState.Margin.Left + vState.Margin.Right + (vState.Border.Width * 2)
if w < minWidths[i] {
selfWidth -= minWidths[i] + vState.Margin.Left + vState.Margin.Right + (vState.Border.Width * 2)
textTotal -= textCounts[i]
textCounts[i] = 0
}
}
for i, v := range n.Children {
vState := s[v.Properties.Id]
w := ((selfWidth / float32(textTotal)) * float32(textCounts[i]))
w -= vState.Margin.Left + vState.Margin.Right + (vState.Border.Width * 2)
if w < minWidths[i] {
w = minWidths[i]
}
widths = append(widths, w)
}
}
// Apply the new widths
fState := s[n.Children[0].Properties.Id]
for i, v := range n.Children {
vState := s[v.Properties.Id]
vState.Width = widths[i]
xStore := vState.X
if i > 0 {
sState := s[n.Children[i-1].Properties.Id]
vState.X = sState.X + sState.Width + sState.Margin.Right + vState.Margin.Left + sState.Border.Width + vState.Border.Width
propagateOffsets(&v, xStore, vState.Y, vState.X, fState.Y, state)
}
vState.Y = fState.Y
(*state)[v.Properties.Id] = vState
deInline(&v, state)
applyInline(&v, state)
applyBlock(&v, state)
}
// Set the heights based on the tallest one
innerSizes = [][]float32{}
for _, v := range n.Children {
w, h := getInnerSize(&v, state)
innerSizes = append(innerSizes, []float32{w, h})
}
sort.Slice(innerSizes, func(i, j int) bool {
return innerSizes[i][1] > innerSizes[j][1]
})
for _, v := range n.Children {
vState := s[v.Properties.Id]
vState.Height = innerSizes[0][1]
(*state)[v.Properties.Id] = vState
}
}
_, h := getInnerSize(n, state)
self.Height = h
(*state)[n.Properties.Id] = self
},
}
}
func applyBlock(n *element.Node, state *map[string]element.State) {
accum := float32(0)
inlineOffset := float32(0)
s := *state
baseY := s[n.Children[0].Properties.Id].Y
for i := 0; i < len(n.Children); i++ {
v := &n.Children[i]
vState := s[v.Properties.Id]
if v.Style["display"] != "block" {
accum = (vState.Y - baseY)
vState.Y += inlineOffset
} else if v.Style["position"] != "absolute" {
vState.Y += accum
// !ISSUE: need to account fo rmultiple block elements and margin padding border and non asbsoulute
inlineOffset = ((vState.Y - baseY) + (vState.Height + (vState.Border.Width * 2) + vState.Margin.Top + vState.Margin.Bottom + vState.Padding.Top + vState.Padding.Bottom)) - accum
accum = 0
}
(*state)[v.Properties.Id] = vState
}
}
func deInline(n *element.Node, state *map[string]element.State) {
s := *state
// self := s[n.Properties.Id]
baseX := float32(-1)
baseY := float32(-1)
for _, v := range n.Children {
vState := s[v.Properties.Id]
if v.Style["display"] == "inline" {
if baseX < 0 && baseY < 0 {
baseX = vState.X
baseY = vState.Y
} else {
vState.X = baseX
vState.Y = baseY
(*state)[v.Properties.Id] = vState
}
} else {
baseX = float32(-1)
baseY = float32(-1)
}
if len(v.Children) > 0 {
deInline(&v, state)
}
}
}
func applyInline(n *element.Node, state *map[string]element.State) {
pl := inline.Init()
for i := 0; i < len(n.Children); i++ {
v := &n.Children[i]
if len(v.Children) > 0 {
applyInline(v, state)
}
if pl.Selector(v) {
pl.Handler(v, state)
}
}
}
func propagateOffsets(n *element.Node, prevx, prevy, newx, newy float32, state *map[string]element.State) {
s := *state
for _, v := range n.Children {
vState := s[v.Properties.Id]
xStore := (vState.X - prevx) + newx
yStore := (vState.Y - prevy) + newy
if len(v.Children) > 0 {
propagateOffsets(&v, vState.X, vState.Y, xStore, yStore, state)
}
vState.X = xStore
vState.Y = yStore
(*state)[v.Properties.Id] = vState
}
}
func countText(n element.Node) int {
count := 0
groups := []int{}
for _, v := range n.Children {
if v.TagName == "notaspan" {
count += 1
}
if v.Style["display"] == "block" {
groups = append(groups, count)
count = 0
}
if len(v.Children) > 0 {
count += countText(v)
}
}
groups = append(groups, count)
sort.Slice(groups, func(i, j int) bool {
return groups[i] > groups[j]
})
return groups[0]
}
func getMinWidth(n *element.Node, state *map[string]element.State) float32 {
s := *state
self := s[n.Properties.Id]
selfWidth := float32(0)
if len(n.Children) > 0 {
for _, v := range n.Children {
selfWidth = utils.Max(selfWidth, getNodeWidth(&v, state))
}
} else {
selfWidth = self.Width
}
selfWidth += self.Padding.Left + self.Padding.Right
return selfWidth
}
func getNodeWidth(n *element.Node, state *map[string]element.State) float32 {
s := *state
self := s[n.Properties.Id]
w := float32(0)
w += self.Padding.Left
w += self.Padding.Right
w += self.Margin.Left
w += self.Margin.Right
w += self.Width
w += self.Border.Width * 2
for _, v := range n.Children {
w = utils.Max(w, getNodeWidth(&v, state))
}
return w
}
func getInnerSize(n *element.Node, state *map[string]element.State) (float32, float32) {
s := *state
self := s[n.Properties.Id]
minx := float32(10e10)
maxw := float32(0)
miny := float32(10e10)
maxh := float32(0)
for _, v := range n.Children {
vState := s[v.Properties.Id]
minx = utils.Min(vState.X, minx)
miny = utils.Min(vState.Y, miny)
hOffset := (vState.Border.Width * 2) + vState.Margin.Top + vState.Margin.Bottom
wOffset := (vState.Border.Width * 2) + vState.Margin.Left + vState.Margin.Right
maxw = utils.Max(vState.X+vState.Width+wOffset, maxw)
maxh = utils.Max(vState.Y+vState.Height+hOffset, maxh)
}
w := maxw - minx
h := maxh - miny
w += self.Padding.Left + self.Padding.Right
h += self.Padding.Top + self.Padding.Bottom
return w, h
}
func add2d(arr [][]float32, index int) float32 {
var sum float32
if len(arr) == 0 {
return sum
}
for i := 0; i < len(arr); i++ {
if len(arr[i]) <= index {
return sum
}
sum += arr[i][index]
}
return sum
}
// getMinHeight(n,state)
// calcHeight(n,width,state)
// calcWidth(n,height,state)