shithub: mycel

Download patch

ref: a5d269cda172e9612b99a5efca2e8d414dee3771
parent: f354673281df5b4e4aa397e7d914ea10f12223a7
author: Philip Silva <philip.silva@protonmail.com>
date: Wed Jan 27 15:47:40 EST 2021

func tlbr

--- a/browser/browser.go
+++ b/browser/browser.go
@@ -64,7 +64,6 @@
 }
 
 func (ui *ColoredLabel) Draw(dui *duit.DUI, self *duit.Kid, img *draw.Image, orig image.Point, m draw.Mouse, force bool) {
-	// TODO: hacky function, might lead to crashes and memory leaks
 	c := ui.n.Map.Color()
 	i, ok := colorCache[c]
 	if !ok {
@@ -234,20 +233,27 @@
 		return nil, false
 	}
 
+	var err error
 	var i *draw.Image
+	var m, p duit.Space
+	zs := duit.Space{}
 	w := n.Width()
 	h := n.Height()
 
-	/*if w == 0 && h == 0 {
-		return nil, false
-	}*/
 	if bg, err := n.BoxBackground(); err == nil {
 		i = bg
 	} else {
 		log.Printf("box background: %f", err)
 	}
+	
+	if p, err = n.Tlbr("padding"); err != nil {
+		log.Errorf("padding: %v", err)
+	}
+	if m, err = n.Tlbr("margin"); err != nil {
+		log.Errorf("margin: %v", err)
+	}
 
-	if w == 0 && h == 0 && i == nil {
+	if w == 0 && h == 0 && i == nil && m == zs && p == zs {
 		return nil, false
 	}
 
@@ -256,7 +262,10 @@
 		Width:      w,
 		Height:     h,
 		Background: i,
+		Margin: m.Topleft(),
+		Padding: p,
 	}
+
 	return box, true
 }
 
@@ -264,6 +273,9 @@
 	if el == nil {
 		return
 	}
+	/*if self.Draw == duit.DirtyKid {
+		force = true
+	}*/
 	box, ok := el.UI.(*duit.Box)
 	if ok && box.Width > 0 && box.Height > 0 {
 		uiSize := image.Point{X: box.Width, Y: box.Height}
--- a/style/stylesheets.go
+++ b/style/stylesheets.go
@@ -491,10 +491,55 @@
 	return true // TODO: be more specific
 }
 
+// tlbr parses 4-tuple of top-right-bottom-left like in margin,
+// margin-top, ...-right, ...-bottom, ...-left.
+func (cs *Map) Tlbr(key string) (s duit.Space, err error) {
+	if all, ok := cs.Declarations[key]; ok {
+		parts := strings.Split(all.Value, " ")
+		nums := make([]int, len(parts))
+		for i, p := range parts {
+			if f, _, err := length(p); err == nil {
+				nums[i] = int(f)
+			} else {
+				return s, fmt.Errorf("length: %w", err)
+			}
+		}
+		s.Top = nums[0]
+		s.Right = s.Top
+		s.Bottom = s.Top
+		s.Left = s.Top
+		if len(nums) >= 2 {
+			s.Right = nums[1]
+			s.Left = s.Right
+		}
+		if len(nums) >= 3 {
+			s.Bottom = nums[2]
+		}
+		if len(nums) == 4 {
+			s.Left = nums[3]
+		}
+	}
+	
+	if t, err := cs.CssPx(key+"-top"); err == nil {
+		s.Top = t
+	}
+	if r, err := cs.CssPx(key+"-right"); err == nil {
+		s.Right = r
+	}
+	if b, err := cs.CssPx(key+"-bottom"); err == nil {
+		s.Bottom = b
+	}
+	if l, err := cs.CssPx(key+"-left"); err == nil {
+		s.Left = l
+	}
+
+	return
+}
+
 func length(l string) (f float64, unit string, err error) {
 	var s string
 
-	if l == "auto" || l == "inherit" {
+	if l == "auto" || l == "inherit" || l == "0" {
 		return 0, "px", nil
 	}
 
@@ -562,4 +607,17 @@
 		return ""
 	}
 	return d.Value
+}
+
+func (cs Map) CssPx(propName string) (l int, err error) {
+	d, ok := cs.Declarations[propName]
+	if !ok {
+		return 0, fmt.Errorf("property doesn't exist")
+	}
+	f, _, err := length(d.Value)
+	if err != nil {
+		return 0, err
+	}
+	l = int(f)
+	return
 }
--- a/style/stylesheets_test.go
+++ b/style/stylesheets_test.go
@@ -3,6 +3,7 @@
 import (
 	"github.com/chris-ramon/douceur/css"
 	"golang.org/x/net/html"
+	"github.com/mjl-/duit"
 	"github.com/psilva261/opossum/logger"
 	"strings"
 	"testing"
@@ -231,6 +232,31 @@
 		}
 		if f != px {
 			t.Fatalf("expected %v but got %v", px, f)
+		}
+	}
+}
+
+func TestTlbr(tt *testing.T) {
+	cases := map[string]duit.Space{
+		"1px 2px 3px 4px": duit.Space{1, 2, 3, 4},
+		"1px 2px 3px": duit.Space{1, 2, 3, 2},
+		"1px 2px": duit.Space{1, 2, 1, 2},
+		"1px": duit.Space{1, 1, 1, 1},
+	}
+	for v, exp := range cases {
+		m := Map{
+			Declarations: make(map[string]css.Declaration),
+		}
+		m.Declarations["margin"] = css.Declaration{
+			Property: "margin",
+			Value:    v,
+		}
+		s, err := m.Tlbr("margin")
+		if err != nil {
+			tt.Errorf("%v", s)
+		}
+		if s != exp {
+			tt.Errorf("%v: %v", s, exp)
 		}
 	}
 }