shithub: zuke

Download patch

ref: 2a933163ce1662c38390bdcd8848370460d17362
parent: a62cbf2bafdc78059301ce61f62b152fdb022b80
author: Sigrid Haflínudóttir <ftrvxmtrx@gmail.com>
date: Wed May 27 09:33:02 EDT 2020

redrawing: simplify and fix for cover images of different size

--- a/zuke.c
+++ b/zuke.c
@@ -16,11 +16,6 @@
 	Cforwardfast,
 	Cbackwardfast,
 
-	Rcover = 1<<0,
-	Rprogress = 1<<1,
-	Rlist = 1<<2,
-	Rall = Rcover|Rprogress|Rlist,
-
 	Everror = 1,
 	Evready,
 
@@ -117,7 +112,7 @@
 }
 
 static void
-redraw(int flags)
+redraw(int full)
 {
 	Image *col;
 	Point p, sp;
@@ -129,120 +124,123 @@
 		return;
 
 	lockdisplay(display);
-	if(flags & Rlist){
-		draw(screen, screen->r, colors[Dback].im, nil, ZP);
+	if(!full){
+		r = screen->r;
+		r.min.x = r.max.x - 128 - 8;
+		r.min.y = r.max.y - font->height - 128 - 8 - 4;
+		replclipr(screen, 0, r);
+	}
 
-		scrollsz = Dy(screen->r) / f->height - 1;
-		adjustcolumns();
-		left = screen->r.min.x;
-		if(scrollsz < plnum){ /* add a scrollbar */
-			p.x = sp.x = screen->r.min.x + Scrollwidth;
-			p.y = screen->r.min.y;
-			sp.y = screen->r.max.y;
-			line(screen, p, sp, Endsquare, Endsquare, 0, colors[Dfmed].im, ZP);
+	draw(screen, screen->r, colors[Dback].im, nil, ZP);
 
-			r = screen->r;
-			r.max.x = r.min.x + Scrollwidth - 1;
-			r.min.x += 1;
-			if(scroll < 1)
-				scrollcenter = 0;
-			else
-				scrollcenter = (Dy(screen->r)-Scrollheight*5/4)*scroll / (plnum - scrollsz);
-			r.min.y += scrollcenter + Scrollheight/4;
-			r.max.y = r.min.y + Scrollheight;
-			draw(screen, r, colors[Dfmed].im, nil, ZP);
-
-			left += Scrollwidth + 4;
-		}
-
-		p.x = sp.x = left + colwidth[0] + 4;
-		p.y = 0;
+	scrollsz = Dy(screen->r) / f->height - 1;
+	adjustcolumns();
+	left = screen->r.min.x;
+	if(scrollsz < plnum){ /* add a scrollbar */
+		p.x = sp.x = screen->r.min.x + Scrollwidth;
+		p.y = screen->r.min.y;
 		sp.y = screen->r.max.y;
 		line(screen, p, sp, Endsquare, Endsquare, 0, colors[Dfmed].im, ZP);
 
-		p.x = sp.x = left + colwidth[0] + 8 + colwidth[1] + 4;
-		p.y = 0;
-		sp.y = screen->r.max.y;
-		line(screen, p, sp, Endsquare, Endsquare, 0, colors[Dfmed].im, ZP);
+		r = screen->r;
+		r.max.x = r.min.x + Scrollwidth - 1;
+		r.min.x += 1;
+		if(scroll < 1)
+			scrollcenter = 0;
+		else
+			scrollcenter = (Dy(screen->r)-Scrollheight*5/4)*scroll / (plnum - scrollsz);
+		r.min.y += scrollcenter + Scrollheight/4;
+		r.max.y = r.min.y + Scrollheight;
+		draw(screen, r, colors[Dfmed].im, nil, ZP);
 
-		sp.x = sp.y = 0;
-		p.x = left + 2;
-		p.y = screen->r.min.y + 2;
+		left += Scrollwidth + 4;
+	}
 
-		for(i = scroll; i < plnum; i++, p.y += f->height){
-			if(i < 0)
-				continue;
-			if(p.y > screen->r.max.y)
-				break;
+	p.x = sp.x = left + colwidth[0] + 4;
+	p.y = 0;
+	sp.y = screen->r.max.y;
+	line(screen, p, sp, Endsquare, Endsquare, 0, colors[Dfmed].im, ZP);
 
-			if(pcur == i){
-				sel.min.x = left;
-				sel.min.y = p.y;
-				sel.max.x = screen->r.max.x;
-				sel.max.y = p.y + f->height;
-				draw(screen, sel, colors[Dbinv].im, nil, ZP);
-				col = colors[Dfinv].im;
-			}else{
-				col = colors[pcurplaying == i ? Dfhigh : Dfmed].im;
-			}
+	p.x = sp.x = left + colwidth[0] + 8 + colwidth[1] + 4;
+	p.y = 0;
+	sp.y = screen->r.max.y;
+	line(screen, p, sp, Endsquare, Endsquare, 0, colors[Dfmed].im, ZP);
 
-			sel = screen->r;
-			r = screen->r;
+	sp.x = sp.y = 0;
+	p.x = left + 2;
+	p.y = screen->r.min.y + 2;
 
-			p.x = left + 2;
-			sel.max.x = p.x + colwidth[0];
-			replclipr(screen, 0, sel);
-			string(screen, p, col, sp, f, pl[i].artist[0]);
-			p.x += colwidth[0] + 8;
-			sel.min.x = p.x;
-			sel.max.x = p.x + colwidth[1];
-			replclipr(screen, 0, sel);
-			string(screen, p, col, sp, f, pl[i].album);
-			p.x += colwidth[1] + 8;
-			sel.min.x = p.x;
-			sel.max.x = p.x + colwidth[2];
-			replclipr(screen, 0, sel);
-			string(screen, p, col, sp, f, pl[i].title);
+	for(i = scroll; i < plnum; i++, p.y += f->height){
+		if(i < 0)
+			continue;
+		if(p.y > screen->r.max.y)
+			break;
 
-			replclipr(screen, 0, r);
+		if(pcur == i){
+			sel.min.x = left;
+			sel.min.y = p.y;
+			sel.max.x = screen->r.max.x;
+			sel.max.y = p.y + f->height;
+			draw(screen, sel, colors[Dbinv].im, nil, ZP);
+			col = colors[Dfinv].im;
+		}else{
+			col = colors[pcurplaying == i ? Dfhigh : Dfmed].im;
+		}
 
-			if(pcurplaying == i){
-				Point rightp, leftp;
-				leftp.y = rightp.y = p.y - 1;
-				leftp.x = left;
-				rightp.x = screen->r.max.x;
-				line(screen, leftp, rightp, 0, 0, 0, colors[Dfmed].im, sp);
-				leftp.y = rightp.y = p.y + f->height;
-				line(screen, leftp, rightp, 0, 0, 0, colors[Dfmed].im, sp);
-			}
+		sel = screen->r;
+		r = screen->r;
+
+		p.x = left + 2;
+		sel.max.x = p.x + colwidth[0];
+		replclipr(screen, 0, sel);
+		string(screen, p, col, sp, f, pl[i].artist[0]);
+		p.x += colwidth[0] + 8;
+		sel.min.x = p.x;
+		sel.max.x = p.x + colwidth[1];
+		replclipr(screen, 0, sel);
+		string(screen, p, col, sp, f, pl[i].album);
+		p.x += colwidth[1] + 8;
+		sel.min.x = p.x;
+		sel.max.x = p.x + colwidth[2];
+		replclipr(screen, 0, sel);
+		string(screen, p, col, sp, f, pl[i].title);
+
+		replclipr(screen, 0, r);
+
+		if(pcurplaying == i){
+			Point rightp, leftp;
+			leftp.y = rightp.y = p.y - 1;
+			leftp.x = left;
+			rightp.x = screen->r.max.x;
+			line(screen, leftp, rightp, 0, 0, 0, colors[Dfmed].im, sp);
+			leftp.y = rightp.y = p.y + f->height;
+			line(screen, leftp, rightp, 0, 0, 0, colors[Dfmed].im, sp);
 		}
 	}
 
-	if(cover != nil && (flags & Rcover) != 0){
+	if(pcurplaying >= 0)
+		snprint(tmp, sizeof(tmp), "%P/%P %d%%", (int)(byteswritten/Bps), pl[pcurplaying].duration/1000, volume);
+	else
+		snprint(tmp, sizeof(tmp), "%d%%", volume);
+	r = screen->r;
+	r.min.x = r.max.x - stringwidth(f, tmp) - 4;
+	r.min.y = r.max.y - f->height - 4;
+	draw(screen, r, colors[Dblow].im, nil, ZP);
+	string(screen, addpt(r.min, Pt(2, 2)), colors[Dfhigh].im, sp, f, tmp);
+
+	if(cover != nil){
+		r.max.x = r.min.x;
+		r.min.x = screen->r.max.x - cover->r.max.x - 8;
+		draw(screen, r, colors[Dblow].im, nil, ZP);
 		r = screen->r;
 		r.min.x = r.max.x - cover->r.max.x - 8;
 		r.min.y = r.max.y - cover->r.max.y - 8 - f->height - 4;
+		r.max.y = r.min.y + cover->r.max.y + 8;
 		draw(screen, r, colors[Dblow].im, nil, ZP);
-		r.min.x += 4;
-		r.min.y += 4;
-		r.max.x -= 4;
-		r.max.y -= 4;
-		draw(screen, r, cover, nil, ZP);
+		draw(screen, insetrect(r, 4), cover, nil, ZP);
 	}
 
-	if(flags & Rprogress){
-		if(pcurplaying >= 0)
-			snprint(tmp, sizeof(tmp), "%P/%P %d%%", (int)(byteswritten/Bps), pl[pcurplaying].duration/1000, volume);
-		else
-			snprint(tmp, sizeof(tmp), "%d%%", volume);
-		r = screen->r;
-		r.min.x = r.max.x - stringwidth(f, tmp) - 4;
-		r.min.y = r.max.y - f->height - 4;
-		draw(screen, r, colors[Dblow].im, nil, ZP);
-		r.min.x += 2;
-		r.min.y += 2;
-		string(screen, r.min, colors[Dfhigh].im, sp, f, tmp);
-	}
+	replclipr(screen, 0, screen->r);
 
 	flushimage(display, 1);
 	unlockdisplay(display);
@@ -251,7 +249,7 @@
 void
 themechanged(void)
 {
-	redraw(Rall);
+	redraw(1);
 }
 
 static void
@@ -460,7 +458,7 @@
 
 	pcurplaying = player->pcur;
 	if(c != Cbackward && c != Cbackwardfast)
-		redraw(Rall);
+		redraw(1);
 
 	while(1){
 		n = Relbufsz;
@@ -474,7 +472,7 @@
 		if(player->img != nil && nbrecv(player->img, &thiscover) != 0){
 			freeimage(cover);
 			cover = thiscover;
-			redraw(Rcover);
+			redraw(0);
 			player->img = nil;
 		}
 		c = nbrecvul(player->ctl);
@@ -515,7 +513,7 @@
 		}
 		byteswritten += n;
 		if(bytesfrom == byteswritten || (byteswritten/Bps > (byteswritten-n)/Bps))
-			redraw(Rprogress);
+			redraw(0);
 	}
 
 	if(n == 0){ /* seeking backwards or end of the song */
@@ -707,7 +705,7 @@
 		for(s--; s != plraw; s--){
 			if(memcmp(s, "\0# ", 3) == 0 && isdigit(s[3])){
 				pcur = atoi(s+3);
-				redraw(Rall);
+				redraw(1);
 				return;
 			}
 		}
@@ -815,7 +813,7 @@
 	}
 
 	themeinit();
-	redraw(Rall);
+	redraw(1);
 	oldbuttons = 0;
 	scrolling = 0;
 
@@ -849,7 +847,7 @@
 					scroll = plnum-scrollsz-1;
 				if(scroll < 0)
 					scroll = 0;
-				redraw(Rall);
+				redraw(1);
 			}else if(m.buttons == 1 || m.buttons == 2){
 				pcur = scroll + (m.xy.y - screen->r.min.y)/f->height;
 				if(m.buttons == 2){
@@ -862,7 +860,7 @@
 		case 1: /* resize */
 			if(getwindow(display, Refnone) < 0)
 				sysfatal("getwindow: %r");
-			redraw(Rall);
+			redraw(1);
 			break;
 		case 2:
 			switch(key){
@@ -931,11 +929,11 @@
 				break;
 			case '-':
 				chvolume(-1);
-				redraw(Rprogress);
+				redraw(0);
 				break;
 			case '+':
 				chvolume(+1);
-				redraw(Rprogress);
+				redraw(0);
 				break;
 			case 's':
 				stop(playercurr);
@@ -943,7 +941,7 @@
 				pcurplaying = -1;
 				freeimage(cover);
 				cover = nil;
-				redraw(Rall);
+				redraw(1);
 				break;
 			case 'p':
 				toggle(playercurr);
@@ -971,7 +969,7 @@
 				scroll = 0;
 
 			if(pcur != oldpcur)
-				redraw(Rall);
+				redraw(1);
 		}
 	}