diff --git a/cstyle/plugins/crop/main.go b/cstyle/plugins/crop/main.go
index b26128b..c9bc934 100644
--- a/cstyle/plugins/crop/main.go
+++ b/cstyle/plugins/crop/main.go
@@ -18 +18 @@ func Init() cstyle.Plugin {
- Handler: func(n *element.Node, c *cstyle.CSS) {
+ Handler: func(n *element.Node, state *map[string]element.State, c *cstyle.CSS) {
@@ -20 +20,2 @@ func Init() cstyle.Plugin {
- self := c.State[n.Properties.Id]
+ s := *state
+ self := s[n.Properties.Id]
@@ -28 +29 @@ func Init() cstyle.Plugin {
- cs := c.State[v.Properties.Id]
+ cs := s[v.Properties.Id]
@@ -42 +43 @@ func Init() cstyle.Plugin {
- p := c.State[v.Children[0].Properties.Id]
+ p := s[v.Children[0].Properties.Id]
@@ -48 +49 @@ func Init() cstyle.Plugin {
- c.State[v.Children[0].Properties.Id] = p
+ (*state)[v.Children[0].Properties.Id] = p
@@ -50 +51 @@ func Init() cstyle.Plugin {
- p := c.State[v.Properties.Id]
+ p := s[v.Properties.Id]
@@ -52,2 +53,2 @@ func Init() cstyle.Plugin {
- c.State[v.Properties.Id] = p
- p = c.State[v.Children[0].Properties.Id]
+ (*state)[v.Properties.Id] = p
+ p = s[v.Children[0].Properties.Id]
@@ -55 +56 @@ func Init() cstyle.Plugin {
- c.State[v.Children[0].Properties.Id] = p
+ (*state)[v.Children[0].Properties.Id] = p
@@ -61 +62 @@ func Init() cstyle.Plugin {
- p := c.State[v.Children[0].Properties.Id]
+ p := s[v.Children[0].Properties.Id]
@@ -67 +68 @@ func Init() cstyle.Plugin {
- c.State[v.Children[0].Properties.Id] = p
+ (*state)[v.Children[0].Properties.Id] = p
@@ -69 +70 @@ func Init() cstyle.Plugin {
- p := c.State[v.Properties.Id]
+ p := s[v.Properties.Id]
@@ -71,2 +72,2 @@ func Init() cstyle.Plugin {
- c.State[v.Properties.Id] = p
- p = c.State[v.Children[0].Properties.Id]
+ (*state)[v.Properties.Id] = p
+ p = s[v.Children[0].Properties.Id]
@@ -74 +75 @@ func Init() cstyle.Plugin {
- c.State[v.Children[0].Properties.Id] = p
+ (*state)[v.Children[0].Properties.Id] = p
@@ -94 +95 @@ func Init() cstyle.Plugin {
- child := c.State[v.Properties.Id]
+ child := s[v.Properties.Id]
@@ -98 +99 @@ func Init() cstyle.Plugin {
- c.State[v.Properties.Id] = child
+ (*state)[v.Properties.Id] = child
@@ -134 +135 @@ func Init() cstyle.Plugin {
- c.State[v.Properties.Id] = child
+ (*state)[v.Properties.Id] = child
@@ -136 +137 @@ func Init() cstyle.Plugin {
- updateChildren(v, c, scrollTop, scrollLeft)
+ updateChildren(v, state, scrollTop, scrollLeft)
@@ -139 +140 @@ func Init() cstyle.Plugin {
- c.State[n.Properties.Id] = self
+ (*state)[n.Properties.Id] = self
@@ -144,2 +145,2 @@ func Init() cstyle.Plugin {
-func updateChildren(n *element.Node, c *cstyle.CSS, offsetY, offsetX int) {
- self := c.State[n.Properties.Id]
+func updateChildren(n *element.Node, state *map[string]element.State, offsetY, offsetX int) {
+ self := (*state)[n.Properties.Id]
@@ -148 +149 @@ func updateChildren(n *element.Node, c *cstyle.CSS, offsetY, offsetX int) {
- c.State[n.Properties.Id] = self
+ (*state)[n.Properties.Id] = self
@@ -150 +151 @@ func updateChildren(n *element.Node, c *cstyle.CSS, offsetY, offsetX int) {
- updateChildren(v, c, offsetY, offsetX)
+ updateChildren(v, state, offsetY, offsetX)
package crop
import (
"grim/cstyle"
"grim/element"
"grim/utils"
)
func Init() cstyle.Plugin {
return cstyle.Plugin{
Selector: func(n *element.Node) bool {
if n.CStyle["overflow"] != "" || n.CStyle["overflow-x"] != "" || n.CStyle["overflow-y"] != "" {
return true
} else {
return false
}
},
Handler: func(n *element.Node, c *cstyle.CSS) {
// !TODO: Needs to find crop bounds for X
self := c.State[n.Properties.Id]
scrollTop, scrollLeft := findScroll(n)
var mod float32
if n.TagName == "html" {
for _, v := range n.Children {
if v.TagName == "body" {
cs := c.State[v.Properties.Id]
mod += cs.Margin.Bottom
}
}
}
containerHeight := self.Height - mod
contentHeight := float32(self.ScrollHeight)
containerWidth := self.Width
contentWidth := float32(self.ScrollWidth)
for _, v := range n.Children {
if v.TagName == "grim-track" && v.GetAttribute("direction") == "y" {
if containerHeight < contentHeight {
p := c.State[v.Children[0].Properties.Id]
p.Height = (containerHeight / contentHeight) * containerHeight
p.Y = self.Y + float32(scrollTop)
c.State[v.Children[0].Properties.Id] = p
} else {
p := c.State[v.Properties.Id]
p.Hidden = true
c.State[v.Properties.Id] = p
p = c.State[v.Children[0].Properties.Id]
p.Hidden = true
c.State[v.Children[0].Properties.Id] = p
}
}
if v.TagName == "grim-track" && v.GetAttribute("direction") == "x" {
if containerWidth < contentWidth {
containerHeight -= utils.ConvertToPixels(v.CStyle["height"], self.EM, self.Width)
p := c.State[v.Children[0].Properties.Id]
p.Width = (containerWidth / contentWidth) * containerWidth
p.X = self.X + float32(scrollLeft)
c.State[v.Children[0].Properties.Id] = p
} else {
p := c.State[v.Properties.Id]
p.Hidden = true
c.State[v.Properties.Id] = p
p = c.State[v.Children[0].Properties.Id]
p.Hidden = true
c.State[v.Children[0].Properties.Id] = p
}
}
}
scrollTop = int((float32(scrollTop) / ((containerHeight / contentHeight) * containerHeight)) * containerHeight)
scrollLeft = int((float32(scrollLeft) / ((containerWidth / contentWidth) * containerWidth)) * containerWidth)
if n.CStyle["overflow-y"] == "hidden" || n.CStyle["overflow-y"] == "clip" {
scrollTop = 0
}
if n.CStyle["overflow-x"] == "hidden" || n.CStyle["overflow-x"] == "clip" {
scrollLeft = 0
}
for _, v := range n.Children {
if v.CStyle["position"] == "fixed" || v.TagName == "grim-track" {
continue
}
child := c.State[v.Properties.Id]
if ((child.Y+child.Height)-float32(scrollTop) < (self.Y) || (child.Y-float32(scrollTop)) > self.Y+self.Height) ||
((child.X+child.Width)-float32(scrollLeft) < (self.X) || (child.X-float32(scrollLeft)) > self.X+self.Width) {
child.Hidden = true
c.State[v.Properties.Id] = child
} else {
child.Hidden = false
xCrop := 0
yCrop := 0
width := int(child.Width)
height := int(child.Height)
if child.Y-float32(scrollTop) < (self.Y) {
yCrop = int((self.Y) - (child.Y - float32(scrollTop)))
height = int(child.Height) - yCrop
} else if (child.Y-float32(scrollTop))+child.Height > self.Y+self.Height {
diff := ((child.Y - float32(scrollTop)) + child.Height) - (self.Y + self.Height)
height = int(child.Height) - int(diff)
}
if child.X-float32(scrollLeft) < (self.X) {
xCrop = int((self.X) - (child.X - float32(scrollLeft)))
w := child.Width
if self.Width < w-float32(xCrop) {
w = self.Width + float32(scrollLeft) - child.X
}
width = int(w) - xCrop
} else if (child.X-float32(scrollLeft))+child.Width > self.X+self.Width {
diff := ((child.X - float32(scrollLeft)) + child.Width) - (self.X + self.Width)
width = int(child.Width) - int(diff)
}
// !ISSUE: Elements disappear when out of view during the resize, because the element is cropped to much
child.Crop = element.Crop{
X: xCrop,
Y: yCrop,
Width: width,
Height: height,
}
c.State[v.Properties.Id] = child
updateChildren(v, c, scrollTop, scrollLeft)
}
}
c.State[n.Properties.Id] = self
},
}
}
func updateChildren(n *element.Node, c *cstyle.CSS, offsetY, offsetX int) {
self := c.State[n.Properties.Id]
self.X -= float32(offsetX)
self.Y -= float32(offsetY)
c.State[n.Properties.Id] = self
for _, v := range n.Children {
updateChildren(v, c, offsetY, offsetX)
}
}
func findScroll(n *element.Node) (int, int) {
if n.ScrollTop != 0 || n.ScrollLeft != 0 {
return n.ScrollTop, n.ScrollLeft
} else {
for _, v := range n.Children {
if v.CStyle["overflow"] == "" && v.CStyle["overflow-x"] == "" && v.CStyle["overflow-y"] == "" {
s, l := findScroll(v)
if s != 0 || l != 0 {
return s, l
}
}
}
return 0, 0
}
}