shithub: mycel

Download patch

ref: 3e9277edf452d3657d7197919508bfe497a61aba
parent: dfab56b94698b9ebb5291931a9f67c6cebac8881
author: Philip Silva <philip.silva@protonmail.com>
date: Sat Feb 20 06:31:05 EST 2021

space margins

--- a/browser/box.go
+++ b/browser/box.go
@@ -49,7 +49,7 @@
 type Box struct {
 	Kids       []*duit.Kid      // Kids and UIs in this box.
 	Reverse    bool        // Lay out children from bottom to top. First kid will be at the bottom.
-	Margin     image.Point // In lowDPI pixels, will be adjusted for highDPI screens.
+	Margin     duit.Space // In lowDPI pixels, will be adjusted for highDPI screens.
 	Padding    duit.Space       // Padding inside box, so children don't touch the sides; in lowDPI pixels, also adjusted for highDPI screens.
 	Valign     duit.Valign      // How to align children on a line.
 	Width      int         // 0 means dynamic (as much as needed), -1 means full width, >0 means that exact amount of lowDPI pixels.
@@ -83,7 +83,7 @@
 		sizeAvail.Y = dui.Scale(ui.Height)
 	}
 	padding := dui.ScaleSpace(ui.Padding)
-	margin := scalePt(dui.Display, ui.Margin)
+	margin := dui.ScaleSpace(ui.Margin)
 	sizeAvail = sizeAvail.Sub(padding.Size())
 	nx := 0 // number on current line
 
@@ -113,7 +113,7 @@
 		var kr image.Rectangle
 		if nx == 0 || cur.X+childSize.X <= sizeAvail.X {
 			kr = rect(childSize).Add(cur).Add(padding.Topleft())
-			cur.X += childSize.X + margin.X
+			cur.X += childSize.X + margin.Topleft().X
 			lineY = maximum(lineY, childSize.Y)
 			nx += 1
 		} else {
@@ -120,11 +120,11 @@
 			if nx > 0 {
 				fixValign(ui.Kids[i-nx : i])
 				cur.X = 0
-				cur.Y += lineY + margin.Y
+				cur.Y += lineY + margin.Topleft().Y
 			}
 			kr = rect(childSize).Add(cur).Add(padding.Topleft())
 			nx = 1
-			cur.X = childSize.X + margin.X
+			cur.X = childSize.X + margin.Topleft().X
 			lineY = childSize.Y
 		}
 		k.R = kr
@@ -144,7 +144,7 @@
 		}
 	}
 
-	ui.size = image.Pt(xmax-margin.X, cur.Y).Add(padding.Size())
+	ui.size = image.Pt(xmax-margin.Dx(), cur.Y).Add(padding.Size())
 	if ui.Width < 0 {
 		ui.size.X = osize.X
 	}
@@ -151,11 +151,16 @@
 	if ui.Height < 0 && ui.size.Y < osize.Y {
 		ui.size.Y = osize.Y
 	}
-	self.R = rect(ui.size)
+	//self.R = rect(ui.size.Add(image.Point{X: margin.Right, Y: margin.Bottom}))
+	self.R = image.Rectangle{
+		image.ZP,
+		ui.size.Add(margin.Size()),
+	}
 }
 
 func (ui *Box) Draw(dui *duit.DUI, self *duit.Kid, img *draw.Image, orig image.Point, m draw.Mouse, force bool) {
-	duit.KidsDraw(dui, self, ui.Kids, ui.size, ui.Background, img, orig, m, force)
+	orig2 := orig.Add(ui.Margin.Topleft())
+	duit.KidsDraw(dui, self, ui.Kids, ui.size, ui.Background, img, orig2, m, force)
 }
 
 func (ui *Box) Mouse(dui *duit.DUI, self *duit.Kid, m draw.Mouse, origM draw.Mouse, orig image.Point) (r duit.Result) {
@@ -193,18 +198,4 @@
 func (ui *Box) Print(self *duit.Kid, indent int) {
 	duit.PrintUI("Box", self, indent)
 	duit.KidsPrint(ui.Kids, indent+1)
-}
-
-//////////////////////
-//                  //
-// helper functions //
-//                  //
-//////////////////////
-
-func scalePt(d *draw.Display, p image.Point) image.Point {
-	f := d.DPI / 100
-	if f <= 1 {
-		f = 1
-	}
-	return p.Mul(f)
 }
--- a/browser/browser.go
+++ b/browser/browser.go
@@ -107,7 +107,7 @@
 	}
 	log.Printf("formatted=%+v", formatted)
 	edit.Append([]byte(formatted))
-	cv.UI = &duit.Box{
+	cv.UI = &Box{
 		Kids:   duit.NewKids(edit),
 		Height: n.Font().Height * (len(lines)+2),
 	}
@@ -230,7 +230,7 @@
 	}
 }
 
-func newBoxElement(ui duit.UI, n *nodes.Node) (box *duit.Box, ok bool) {
+func newBoxElement(ui duit.UI, n *nodes.Node) (box *Box, ok bool) {
 	if ui == nil {
 		return nil, false
 	}
@@ -266,13 +266,13 @@
 		return nil, false
 	}
 
-	box = &duit.Box{
+	box = &Box{
 		Kids:       duit.NewKids(ui),
 		Width:      w,
 		Height:     h,
 		MaxWidth: mw,
 		Background: i,
-		Margin: m.Topleft(),
+		Margin: m,
 		Padding: p,
 	}
 
@@ -290,7 +290,7 @@
 	// }
 	//
 	// Make boxes use full size for image backgrounds
-	box, ok := el.UI.(*duit.Box)
+	box, ok := el.UI.(*Box)
 	if ok && box.Width > 0 && box.Height > 0 {
 		uiSize := image.Point{X: box.Width, Y: box.Height}
 		duit.KidsDraw(dui, self, box.Kids, uiSize, box.Background, img, orig, m, force)
@@ -304,7 +304,7 @@
 		return
 	}
 	// Make boxes use full size for image backgrounds
-	box, ok := el.UI.(*duit.Box)
+	box, ok := el.UI.(*Box)
 	if ok && box.Width > 0 && box.Height > 0 {
 		//dui.debugLayout(self)
 		//if ui.Image == nil {
@@ -456,7 +456,7 @@
 
 	el := NewElement(edit, n)
 	el.Changed = func(e *Element) {
-		ed := e.UI.(*duit.Box).Kids[0].UI.(*duit.Edit)
+		ed := e.UI.(*Box).Kids[0].UI.(*duit.Edit)
 
 		tt, err := ed.Text()
 		if err != nil {
@@ -712,9 +712,9 @@
 			}
 		}
 
-		return &duit.Box{
+		return &Box{
 			Padding: duit.SpaceXY(6, 4),
-			Margin:  image.Pt(6, 4),
+			Margin:  duit.SpaceXY(6, 4),
 			Kids:    duit.NewKids(finalUis...),
 		}
 	} else {
@@ -1096,7 +1096,7 @@
 		panic("null")
 	case *Scroll:
 		traverseTree(r+1, v.Kid.UI, f)
-	case *duit.Box:
+	case *Box:
 		for _, kid := range v.Kids {
 			traverseTree(r+1, kid.UI, f)
 		}
@@ -1149,8 +1149,8 @@
 	case *duit.Scroll:
 		fmt.Printf("duit.Scroll\n")
 		printTree(r+1, v.Kid.UI)
-	case *duit.Box:
-		fmt.Printf("duit.Box\n")
+	case *Box:
+		fmt.Printf("Box\n")
 		for _, kid := range v.Kids {
 			printTree(r+1, kid.UI)
 		}
--- a/browser/browser_test.go
+++ b/browser/browser_test.go
@@ -90,7 +90,7 @@
 			}
 		}
 		if d == "inline" {
-			b := v.UI.(*duit.Box)
+			b := v.UI.(*Box)
 			if len(b.Kids) != 3 {
 				t.Fatalf("%+v", b)
 			}
@@ -222,7 +222,7 @@
 			break
 		}
 	}
-	el := e.UI.(*duit.Box)
+	el := e.UI.(*Box)
 	return el.Kids, true
 }