shithub: mycel

Download patch

ref: 2f32575415b57d62b82664764855cf51896aa2f0
parent: de7f6220b338f2e7b18b753d038737de134abae5
author: Philip Silva <philip.silva@protonmail.com>
date: Sun Nov 21 17:33:05 EST 2021

Expose geometry

--- a/browser/browser.go
+++ b/browser/browser.go
@@ -310,6 +310,7 @@
 type Element struct {
 	duit.UI
 	n       *nodes.Node
+	rect    image.Rectangle
 	IsLink  bool
 	Click   func() duit.Event
 	Changed func(*Element)
@@ -333,10 +334,12 @@
 		}
 	}
 
-	return &Element{
+	el := &Element{
 		UI: ui,
 		n:  n,
 	}
+	n.Rectangular = el
+	return el
 }
 
 func newBoxElement(n *nodes.Node, force bool, uis ...duit.UI) (box *duitx.Box, ok bool) {
@@ -417,6 +420,13 @@
 	return box, true
 }
 
+func (el *Element) Rect() image.Rectangle {
+	if el == nil {
+		log.Errorf("Rect: nil element")
+	}
+	return el.rect
+}
+
 func (el *Element) Draw(dui *duit.DUI, self *duit.Kid, img *draw.Image, orig image.Point, m draw.Mouse, force bool) {
 	if el == nil {
 		return
@@ -466,6 +476,8 @@
 		el.UI.Layout(dui, self, sizeAvail, force)
 	}
 
+	el.rect  = self.R
+
 	return
 }
 
@@ -854,10 +866,12 @@
 	if ui == nil {
 		return nil
 	}
-	return &Element{
+	el := &Element{
 		n:  n,
 		UI: ui,
 	}
+	n.Rectangular = el
+	return el
 }
 
 func horizontalSeq(parent *nodes.Node, wrap bool, es []*Element) duit.UI {
--- a/browser/fs/experimental.go
+++ b/browser/fs/experimental.go
@@ -15,7 +15,9 @@
 //
 // /0
 // /0/attrs
+// /0/geom
 // /0/html
+// /0/style
 // /0/tag
 // /0/0
 //   ...
@@ -63,6 +65,7 @@
 	if n.nt.Type() == html.ElementNode {
 		cs["tag"] = n.tag()
 		cs["attrs"] = Attrs{attrs: &n.nt.DomSubtree.Attr}
+		cs["geom"] = n.geom()
 		cs["html"] = n.html()
 		cs["style"] = Style{cs: &n.nt.Map}
 	}
@@ -75,6 +78,20 @@
 		oFS.NewStat("tag", un, gn, 0666),
 		func() []byte {
 			return []byte(n.nt.Data())
+		},
+	)
+}
+
+func (n Node) geom() fs.FSNode {
+	return fs.NewDynamicFile(
+		oFS.NewStat("geom", un, gn, 0666),
+		func() (bs []byte) {
+			var dt style.DomTree
+			if dt = n.nt.Map.DomTree; dt == nil {
+				return
+			}
+			r := dt.Rect()
+			return []byte(fmt.Sprintf("%v,%v,%v,%v", r.Min.X, r.Min.Y, r.Max.X, r.Max.Y))
 		},
 	)
 }
--- a/nodes/nodes.go
+++ b/nodes/nodes.go
@@ -7,6 +7,7 @@
 	"github.com/psilva261/opossum/logger"
 	"github.com/psilva261/opossum/style"
 	"golang.org/x/net/html"
+	"image"
 	"strings"
 )
 
@@ -15,10 +16,15 @@
 	Text       string
 	Wrappable  bool
 	style.Map
+	Rectangular
 	Children []*Node
 	parent   *Node `json:"-"`
 }
 
+type Rectangular interface {
+	Rect() image.Rectangle
+}
+
 // NewNodeTree propagates the cascading styles to the leaves
 //
 // First applies the parent style and at the end the local style attribute's style is attached.
@@ -102,6 +108,14 @@
 
 func (n *Node) Style() style.Map {
 	return n.Map
+}
+
+func (n *Node) Rect() image.Rectangle {
+	if n.Rectangular == nil {
+		log.Errorf("rectangular nil")
+		return image.Rectangle{}
+	}
+	return n.Rectangular.Rect()
 }
 
 // Ancestor of tag
--- a/style/stylesheets.go
+++ b/style/stylesheets.go
@@ -11,6 +11,7 @@
 	"github.com/psilva261/opossum/logger"
 	"golang.org/x/image/colornames"
 	"golang.org/x/net/html"
+	"image"
 	"math"
 	"os/exec"
 	"regexp"
@@ -226,6 +227,7 @@
 }
 
 type DomTree interface {
+	Rect() image.Rectangle
 	Parent() (p DomTree, ok bool)
 	Style() Map
 }