shithub: mycel

Download patch

ref: f354673281df5b4e4aa397e7d914ea10f12223a7
parent: 2d8c5c0443f3d663243b167d97e569e27ab97ad4
author: Philip Silva <philip.silva@protonmail.com>
date: Mon Jan 25 16:45:28 EST 2021

improved background handling

--- a/browser/browser.go
+++ b/browser/browser.go
@@ -216,6 +216,10 @@
 		return nil
 	}
 
+	if box, ok := newBoxElement(ui, n); ok {
+		ui = box
+	}
+
 	return &Element{
 		UI: ui,
 		n: n,
@@ -222,12 +226,12 @@
 	}
 }
 
-func NewBoxElement(ui duit.UI, n *nodes.Node) *Element {
+func newBoxElement(ui duit.UI, n *nodes.Node) (box *duit.Box, ok bool) {
 	if ui == nil {
-		return nil
+		return nil, false
 	}
 	if n.IsDisplayNone() {
-		return nil
+		return nil, false
 	}
 
 	var i *draw.Image
@@ -234,22 +238,26 @@
 	w := n.Width()
 	h := n.Height()
 
-	if w == 0 && h == 0 {
-		return NewElement(ui, n)
-	}
+	/*if w == 0 && h == 0 {
+		return nil, false
+	}*/
 	if bg, err := n.BoxBackground(); err == nil {
 		i = bg
 	} else {
 		log.Printf("box background: %f", err)
 	}
-	box := &duit.Box{
+
+	if w == 0 && h == 0 && i == nil {
+		return nil, false
+	}
+
+	box = &duit.Box{
 		Kids:       duit.NewKids(ui),
 		Width:      w,
 		Height:     h,
 		Background: i,
 	}
-	el := NewElement(box, n)
-	return el
+	return box, true
 }
 
 func (el *Element) Draw(dui *duit.DUI, self *duit.Kid, img *draw.Image, orig image.Point, m draw.Mouse, force bool) {
@@ -963,7 +971,7 @@
 				innerContent = InnerNodesToBox(r+1, b, n)
 			}
 
-			return NewBoxElement(
+			return NewElement(
 				innerContent,
 				n,
 			)
@@ -984,9 +992,12 @@
 			}
 
 			text = strings.Join(nn, " ")
-			ui := &duit.Label{
-				Text: text,
-				Font: n.Font(),
+			ui := &ColoredLabel{
+				Label: &duit.Label{
+					Text: text,
+					Font: n.Font(),
+				},
+				n: n,
 			}
 
 			return NewElement(
--- a/browser/browser_test.go
+++ b/browser/browser_test.go
@@ -302,3 +302,7 @@
 		t.Errorf("ael: %+v %+v", ael, ael.n)
 	}
 }
+
+func TestAlwaysOneElement(t *testing.T) {
+}
+
--- a/style/experimental.go
+++ b/style/experimental.go
@@ -34,9 +34,11 @@
 		bgImg = cs.backgroundImage()
 
 		if bgImg == nil {
-			bgColor := cs.backgroundColor()
+			bgColor, ok := cs.backgroundColor()
+			if !ok {
+				return
+			}
 			log.Printf("bgColor=%+v", bgColor)
-			var ok bool
 			i, ok = colorCache[bgColor]
 			if !ok {
 				var err error
@@ -53,24 +55,24 @@
 	return
 }
 
-func (cs Map) backgroundColor() draw.Color {
-	_, ok := cs.Declarations["background-color"]
+func (cs Map) backgroundColor() (c draw.Color, ok bool) {
+	_, ok = cs.Declarations["background-color"]
 	if ok {
-		c, ok := cs.colorHex("background-color")
+		c, ok = cs.colorHex("background-color")
 		if !ok {
-			return draw.White
+			return
 		}
-		return c
+		return c, true
 	}
 	_, ok = cs.Declarations["background"]
 	if ok {
-		c, ok := cs.colorHex("background")
+		c, ok = cs.colorHex("background")
 		if !ok {
-			return draw.White
+			return
 		}
-		return c
+		return c, true
 	}
-	return draw.White
+	return
 }
 
 func backgroundImageUrl(decl css.Declaration) (url string, ok bool) {
--- a/style/experimental_test.go
+++ b/style/experimental_test.go
@@ -39,7 +39,7 @@
 				Value:    hex,
 			}
 
-			if b := m.backgroundColor(); b != d {
+			if b, ok := m.backgroundColor(); !ok || b != d {
 				t.Fatalf("%v", b)
 			}
 		}