shithub: zuke

Download patch

ref: 56d78c5f36adc9dd6dd6f5fa9063f795888e5de6
parent: 17b3ea38d2c957d182b9f4cba318243c098419b2
author: Sigrid Solveig Haflínudóttir <ftrvxmtrx@gmail.com>
date: Tue Feb 23 09:34:44 EST 2016

scrolling 2.0

--- a/zuke.c
+++ b/zuke.c
@@ -21,6 +21,8 @@
 
 	Bps = 44100*2*2, /* 44100KHz, stereo, u16 for a sample */
 	Seekbytes = Bps*10, /* 10 seconds */
+
+	Scrollwidth = 12,
 };
 
 static Meta *pl;
@@ -74,7 +76,7 @@
 	Image *col;
 	Point p, sp;
 	Rectangle sel, r;
-	int i, colwidth;
+	int i, colwidth, left, scrollcenter;
 	char tmp[32];
 
 	if(entering)
@@ -87,28 +89,51 @@
 
 	scrollsz = Dy(screen->r) / f->height - 1;
 	colwidth = (Dx(screen->r) - 8) / 3; /* 3 = artist, album, title */
+	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, colb, ZP);
 
-	p.x = sp.x = screen->r.min.x + colwidth + 4;
+		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)-8)*scroll / plnum;
+		r.min.y += scrollcenter + 4;
+		r.max.y = r.min.y + 16;
+		draw(screen, r, colb, nil, ZP);
+
+		left += Scrollwidth + 4;
+	}
+
+	p.x = sp.x = left + colwidth + 4;
 	p.y = 0;
 	sp.y = screen->r.max.y;
-	line(screen, p, sp, Endsquare, Endsquare, 0, colb, ZP);
+	line(screen, p, sp, Endsquare, Endsquare, 0, cola, ZP);
 
-	p.x = sp.x = screen->r.min.x + colwidth + 8 + colwidth + 4;
+	p.x = sp.x = left + colwidth + 8 + colwidth + 4;
 	p.y = 0;
 	sp.y = screen->r.max.y;
 	line(screen, p, sp, Endsquare, Endsquare, 0, colb, ZP);
 
 	sp.x = sp.y = 0;
-	p.x = screen->r.min.x + 2;
+	p.x = left + 2;
 	p.y = screen->r.min.y + 2;
 
-	for(i = scroll; i < plnum; i++){
+	for(i = scroll; i < plnum; i++, p.y += f->height){
+		if(i < 0)
+			continue;
 		if(p.y > screen->r.max.y)
 			break;
 
 		if(pcur == i){
-			sel = screen->r;
+			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, colb, nil, ZP);
 			col = cola;
@@ -119,7 +144,7 @@
 		sel = screen->r;
 		r = screen->r;
 
-		p.x = screen->r.min.x + 2;
+		p.x = left + 2;
 		sel.max.x = p.x + colwidth;
 		replclipr(screen, 0, sel);
 		string(screen, p, col, sp, f, pl[i].artist[0]);
@@ -137,16 +162,14 @@
 		replclipr(screen, 0, r);
 
 		if(pcurplaying == i){
-			Point right, left;
-			left.y = right.y = p.y - 1;
-			left.x = screen->r.min.x;
-			right.x = screen->r.max.x;
-			line(screen, left, right, 0, 0, 0, colb, sp);
-			left.y = right.y = p.y + f->height;
-			line(screen, left, right, 0, 0, 0, colb, sp);
+			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, colb, sp);
+			leftp.y = rightp.y = p.y + f->height;
+			line(screen, leftp, rightp, 0, 0, 0, colb, sp);
 		}
-
-		p.y += f->height;
 	}
 
 	if(cover != nil){
@@ -536,7 +559,8 @@
 void
 threadmain(int argc, char **argv)
 {
-	int inv, fd, oldpcur;
+	int inv, fd, scrolling, oldscroll, oldpcur, oldscrolling, usingscrollbar;
+	Point lastclick;
 	Player *player;
 	Rune key;
 	Mouse m;
@@ -584,6 +608,7 @@
 	colb = inv ? display->white : display->black;
 	srand(time(0));
 	pcurplaying = -1;
+	scrolling = oldscroll = usingscrollbar = 0;
 	player = nil;
 	fmtinstall('P', positionfmt);
 	threadsetname("zuke");
@@ -601,7 +626,29 @@
 
 		switch(alt(a)){
 		case 0:
-			if(m.buttons > 0){
+			oldscrolling = scrolling;
+			scrolling = m.buttons & 2;
+			if(!oldscrolling && (m.buttons & 2) != 0){
+				usingscrollbar = m.xy.x < screen->r.min.x+Scrollwidth;
+				lastclick = m.xy;
+				oldscroll = scroll;
+			}
+			if(m.buttons == 0)
+				break;
+
+			if(scrolling){
+				if(scrollsz >= plnum)
+					break;
+				if(usingscrollbar)
+					scroll = (m.xy.y - screen->r.min.y)*plnum / (Dy(screen->r)-8);
+				else
+					scroll = oldscroll + (lastclick.y - m.xy.y) / f->height;
+				if(scroll < 0)
+					scroll = 0;
+				else if(scroll > plnum-scrollsz-1)
+					scroll = plnum-scrollsz-1;
+				redraw(screen, 0);
+			}else{
 				pcur = scroll + (m.xy.y - screen->r.min.y)/f->height;
 				if(m.buttons == 4){
 					stop(player);