shithub: orca

Download patch

ref: b0c2e60ec99cc8009e860f01cb965dfb3ac01594
parent: ea5ba1c288fd5aa77c5362699e0229b1e07e35ec
author: Sigrid Haflínudóttir <ftrvxmtrx@gmail.com>
date: Sun Feb 23 17:52:52 EST 2020

plan9: center-align the grid

--- a/plan9.c
+++ b/plan9.c
@@ -81,7 +81,7 @@
 static Rune cursor = '@';
 static vlong tick;
 static Point glyphsz;
-static Point cur, scroll, move = { .x = 1, .y = 1 };
+static Point cur, scroll, txtoff, move = { .x = 1, .y = 1 };
 static Rectangle sel;
 static Field field;
 static Mbuf_reusable mbuf, mscr;
@@ -703,13 +703,24 @@
 	char s[32];
 	Rune c, csel;
 
-	p = addpt(screen->r.min, Pt(Txtoff, Txtoff));
-	top = p;
-	bot.x = top.x;
+	/* bottom text is always in the same place */
+	bot.x = screen->r.min.x + Txtoff;
 	bot.y = screen->r.max.y - glyphsz.y*2 - Txtoff;
-	max.x = MIN(field.width, (Dx(screen->r) - 2*Txtoff) / glyphsz.x);
-	max.y = MIN(field.height, (bot.y - top.y - glyphsz.y) / glyphsz.y);
 
+	txtoff.x = Txtoff;
+	txtoff.y = Txtoff;
+	max.x = (Dx(screen->r) - 2*Txtoff) / glyphsz.x;
+	if (max.x > field.width) {
+		txtoff.x += (max.x - field.width) * glyphsz.x / 2;
+		max.x = field.width;
+	}
+	max.y = (bot.y - screen->r.min.y - Txtoff - glyphsz.y) / glyphsz.y;
+	if (max.y > field.height) {
+		txtoff.y += (max.y - field.height) * glyphsz.y / 2;
+		max.y = field.height;
+	}
+	top = addpt(screen->r.min, txtoff);
+
 	if (cur.x >= max.x+scroll.x-1)
 		scroll.x = cur.x-max.x+1;
 	else if (cur.x < scroll.x)
@@ -726,10 +737,10 @@
 
 	if (complete) {
 		r = screen->r;
-		r.max.y = r.min.y + Txtoff;
+		r.max.y = r.min.y + txtoff.y;
 		draw(screen, r, color[Dback], nil, ZP);
 		r = screen->r;
-		r.max.x = r.min.x + Txtoff;
+		r.max.x = r.min.x + txtoff.x;
 		draw(screen, r, color[Dback], nil, ZP);
 	}
 
@@ -739,6 +750,7 @@
 	bg = -1;
 	fg = -1;
 	r = screen->r;
+	p.y = top.y;
 	for (y = scroll.y; y < MIN(field.height, scroll.y+max.y); y++) {
 		p.x = top.x;
 		for (x = scroll.x, i = 0; x < MIN(field.width, scroll.x+max.x); x++) {
@@ -827,21 +839,21 @@
 	}
 
 	r = screen->r;
-	r.min.x += Txtoff + max.x*glyphsz.x;
+	r.min.x += txtoff.x + max.x*glyphsz.x;
 	draw(screen, r, color[Dback], nil, ZP);
 
 	r = screen->r;
-	r.min.y += Txtoff + max.y*glyphsz.y;
+	r.min.y += txtoff.y + max.y*glyphsz.y;
 	draw(screen, r, color[Dback], nil, ZP);
 
 	p = top;
 	p.y += glyphsz.y*(max.y-1)/2;
 	if (scroll.x > 0) {
-		p.x = top.x - Txtoff;
+		p.x = top.x - txtoff.x;
 		string(screen, p, color[Dfmed], ZP, font, "←");
 	}
 	if (max.x+scroll.x < field.width) {
-		p.x = top.x + max.x*glyphsz.x + Txtoff - glyphsz.x;
+		p.x = top.x + max.x*glyphsz.x + txtoff.x - glyphsz.x;
 		string(screen, p, color[Dfmed], ZP, font, "→");
 	}
 
@@ -1124,7 +1136,7 @@
 static Point
 ptmouse(Point p)
 {
-	p = subpt(subpt(p, screen->r.min), Pt(Txtoff, Txtoff));
+	p = subpt(subpt(p, screen->r.min), txtoff);
 	p.x /= glyphsz.x;
 	p.y /= glyphsz.y;
 	p.x += scroll.x;