Adding tagpreview patch (#271)

This commit is contained in:
Stein Gunnar Bakkeby
2022-07-05 14:55:42 +02:00
committed by GitHub
parent 20692bea01
commit 5f7df0b0dc
13 changed files with 296 additions and 32 deletions

39
patch/bar.c Normal file
View File

@@ -0,0 +1,39 @@
void
barhover(XEvent *e, Bar *bar)
{
const BarRule *br;
Monitor *m = bar->mon;
XMotionEvent *ev = &e->xmotion;
BarArg barg = { 0, 0, 0, 0 };
int r;
for (r = 0; r < LENGTH(barrules); r++) {
br = &barrules[r];
if (br->bar != bar->idx || (br->monitor == 'A' && m != selmon) || br->hoverfunc == NULL)
continue;
if (br->monitor != 'A' && br->monitor != -1 && br->monitor != bar->mon->num)
continue;
if (bar->x[r] > ev->x || ev->x > bar->x[r] + bar->w[r])
continue;
barg.x = ev->x - bar->x[r];
barg.y = ev->y - bar->borderpx;
barg.w = bar->w[r];
barg.h = bar->bh - 2 * bar->borderpx;
br->hoverfunc(bar, &barg, ev);
break;
}
}
Bar *
wintobar(Window win)
{
Monitor *m;
Bar *bar;
for (m = mons; m; m = m->next)
for (bar = m->bar; bar; bar = bar->next)
if (bar->win == win)
return bar;
return NULL;
}

2
patch/bar.h Normal file
View File

@@ -0,0 +1,2 @@
static void barhover(XEvent *e, Bar *bar);
static Bar *wintobar(Window win);

95
patch/bar_tagpreview.c Normal file
View File

@@ -0,0 +1,95 @@
#include <Imlib2.h>
void
showtagpreview(int tag, int x, int y)
{
if (selmon->tagmap[tag]) {
XSetWindowBackgroundPixmap(dpy, selmon->tagwin, selmon->tagmap[tag]);
XCopyArea(dpy, selmon->tagmap[tag], selmon->tagwin, drw->gc, 0, 0, selmon->mw / scalepreview, selmon->mh / scalepreview, 0, 0);
XMoveWindow(dpy, selmon->tagwin, x, y);
XSync(dpy, False);
XMapWindow(dpy, selmon->tagwin);
} else
XUnmapWindow(dpy, selmon->tagwin);
}
void
hidetagpreview(Monitor *m)
{
m->previewshow = 0;
XUnmapWindow(dpy, m->tagwin);
}
void
tagpreviewswitchtag(void)
{
int i;
unsigned int occ = 0;
Client *c;
Imlib_Image image;
for (c = selmon->clients; c; c = c->next)
occ |= c->tags;
for (i = 0; i < NUMTAGS; i++) {
if (selmon->tagset[selmon->seltags] & 1 << i) {
if (selmon->tagmap[i] != 0) {
XFreePixmap(dpy, selmon->tagmap[i]);
selmon->tagmap[i] = 0;
}
if (occ & 1 << i) {
image = imlib_create_image(sw, sh);
imlib_context_set_image(image);
imlib_context_set_display(dpy);
#if BAR_ALPHA_PATCH
imlib_image_set_has_alpha(1);
imlib_context_set_blend(0);
imlib_context_set_visual(visual);
#else
imlib_context_set_visual(DefaultVisual(dpy, screen));
#endif // BAR_ALPHA_PATCH
imlib_context_set_drawable(root);
imlib_copy_drawable_to_image(0, selmon->mx, selmon->my, selmon->mw ,selmon->mh, 0, 0, 1);
#if BAR_ALPHA_PATCH
selmon->tagmap[i] = XCreatePixmap(dpy, selmon->tagwin, selmon->mw / scalepreview, selmon->mh / scalepreview, depth);
#else
selmon->tagmap[i] = XCreatePixmap(dpy, selmon->tagwin, selmon->mw / scalepreview, selmon->mh / scalepreview, DefaultDepth(dpy, screen));
#endif // BAR_ALPHA_PATCH
imlib_context_set_drawable(selmon->tagmap[i]);
imlib_render_image_part_on_drawable_at_size(0, 0, selmon->mw, selmon->mh, 0, 0, selmon->mw / scalepreview, selmon->mh / scalepreview);
imlib_free_image();
}
}
}
}
void
updatepreview(void)
{
Monitor *m;
XSetWindowAttributes wa = {
.override_redirect = True,
#if BAR_ALPHA_PATCH
.background_pixel = 0,
.border_pixel = 0,
.colormap = cmap,
#else
.background_pixmap = ParentRelative,
#endif // BAR_ALPHA_PATCH
.event_mask = ButtonPressMask|ExposureMask
};
for (m = mons; m; m = m->next) {
m->tagwin = XCreateWindow(dpy, root, m->wx, m->bar->by + bh, m->mw / 4, m->mh / 4, 0,
#if BAR_ALPHA_PATCH
depth, CopyFromParent, visual,
CWOverrideRedirect|CWBackPixel|CWBorderPixel|CWColormap|CWEventMask, &wa
#else
DefaultDepth(dpy, screen), CopyFromParent, DefaultVisual(dpy, screen),
CWOverrideRedirect|CWBackPixmap|CWEventMask, &wa
#endif // BAR_ALPHA_PATCH
);
XDefineCursor(dpy, m->tagwin, cursor[CurNormal]->cursor);
XMapRaised(dpy, m->tagwin);
XUnmapWindow(dpy, m->tagwin);
}
}

4
patch/bar_tagpreview.h Normal file
View File

@@ -0,0 +1,4 @@
static void showtagpreview(int tag, int x, int y);
static void hidetagpreview(Monitor *m);
static void tagpreviewswitchtag(void);
static void updatepreview(void);

View File

@@ -91,3 +91,56 @@ click_tags(Bar *bar, Arg *arg, BarArg *a)
return ClkTagBar;
}
int
hover_tags(Bar *bar, BarArg *a, XMotionEvent *ev)
{
#if BAR_TAGPREVIEW_PATCH
int i = 0, x = lrpad / 2;
int px, py;
Monitor *m = bar->mon;
#if VANITYGAPS_PATCH
int ov = gappov;
int oh = gappoh;
#else
int ov = 0;
int oh = 0;
#endif // VANITYGAPS_PATCH
#if BAR_HIDEVACANTTAGS_PATCH
Client *c;
unsigned int occ = 0;
for (c = bar->mon->clients; c; c = c->next)
occ |= c->tags == 255 ? 0 : c->tags;
#endif // BAR_HIDEVACANTTAGS_PATCH
do {
#if BAR_HIDEVACANTTAGS_PATCH
if (!(occ & 1 << i || bar->mon->tagset[bar->mon->seltags] & 1 << i))
continue;
#endif // BAR_HIDEVACANTTAGS_PATCH
x += TEXTW(tagicon(bar->mon, i));
} while (a->x >= x && ++i < NUMTAGS);
if (i < NUMTAGS) {
if ((i + 1) != selmon->previewshow && !(selmon->tagset[selmon->seltags] & 1 << i)) {
if (bar->by > m->my + m->mh / 2) // bottom bar
py = bar->by - m->mh / scalepreview - oh;
else // top bar
py = bar->by + bar->bh + oh;
px = bar->bx + ev->x - m->mw / scalepreview / 2;
if (px + m->mw / scalepreview > m->mx + m->mw)
px = m->wx + m->ww - m->mw / scalepreview - ov;
else if (px < bar->bx)
px = m->wx + ov;
selmon->previewshow = i + 1;
showtagpreview(i, px, py);
} else if (selmon->tagset[selmon->seltags] & 1 << i) {
hidetagpreview(selmon);
}
} else if (selmon->previewshow != 0) {
hidetagpreview(selmon);
}
#endif // BAR_TAGPREVIEW_PATCH
return 1;
}

View File

@@ -1,4 +1,4 @@
static int width_tags(Bar *bar, BarArg *a);
static int draw_tags(Bar *bar, BarArg *a);
static int click_tags(Bar *bar, Arg *arg, BarArg *a);
static int hover_tags(Bar *bar, BarArg *a, XMotionEvent *ev);

View File

@@ -1,6 +1,7 @@
/* Bar functionality */
#include "bar_indicators.c"
#include "bar_tagicons.c"
#include "bar.c"
#if BAR_ALPHA_PATCH
#include "bar_alpha.c"
@@ -50,6 +51,9 @@
#if BAR_TABGROUPS_PATCH
#include "bar_tabgroups.c"
#endif
#if BAR_TAGPREVIEW_PATCH
#include "bar_tagpreview.c"
#endif
#if BAR_TAGS_PATCH
#include "bar_tags.c"
#endif

View File

@@ -1,6 +1,7 @@
/* Bar functionality */
#include "bar_indicators.h"
#include "bar_tagicons.h"
#include "bar.h"
#if BAR_ALPHA_PATCH
#include "bar_alpha.h"
@@ -59,6 +60,9 @@
#if BAR_TAGLABELS_PATCH
#include "bar_taglabels.h"
#endif
#if BAR_TAGPREVIEW_PATCH
#include "bar_tagpreview.h"
#endif
#if BAR_TAGGRID_PATCH
#include "bar_taggrid.h"
#endif