sites

public wiki contents of suckless.org
git clone git://git.suckless.org/sites
Log | Files | Refs

dwm-centered-tags-20240821-9480c051.diff (6624B)


      1 From 9480c0519f220c8fe82fc8e47897be365ce465f8 Mon Sep 17 00:00:00 2001
      2 From: elbachir-one <bachiralfa@gmail.com>
      3 Date: Wed, 21 Aug 2024 13:16:28 +0100
      4 Subject: [PATCH] No title and centering the tags
      5 
      6 ---
      7  dwm.c | 185 +++++++++++++++++++++++++++++++---------------------------
      8  1 file changed, 100 insertions(+), 85 deletions(-)
      9 
     10 diff --git a/dwm.c b/dwm.c
     11 index 67c6b2b..8f1344e 100644
     12 --- a/dwm.c
     13 +++ b/dwm.c
     14 @@ -418,43 +418,61 @@ attachstack(Client *c)
     15  void
     16  buttonpress(XEvent *e)
     17  {
     18 -	unsigned int i, x, click;
     19 -	Arg arg = {0};
     20 -	Client *c;
     21 -	Monitor *m;
     22 -	XButtonPressedEvent *ev = &e->xbutton;
     23 -
     24 -	click = ClkRootWin;
     25 -	/* focus monitor if necessary */
     26 -	if ((m = wintomon(ev->window)) && m != selmon) {
     27 -		unfocus(selmon->sel, 1);
     28 -		selmon = m;
     29 -		focus(NULL);
     30 -	}
     31 -	if (ev->window == selmon->barwin) {
     32 -		i = x = 0;
     33 -		do
     34 -			x += TEXTW(tags[i]);
     35 -		while (ev->x >= x && ++i < LENGTH(tags));
     36 -		if (i < LENGTH(tags)) {
     37 -			click = ClkTagBar;
     38 -			arg.ui = 1 << i;
     39 -		} else if (ev->x < x + TEXTW(selmon->ltsymbol))
     40 -			click = ClkLtSymbol;
     41 -		else if (ev->x > selmon->ww - (int)TEXTW(stext))
     42 -			click = ClkStatusText;
     43 -		else
     44 -			click = ClkWinTitle;
     45 -	} else if ((c = wintoclient(ev->window))) {
     46 -		focus(c);
     47 -		restack(selmon);
     48 -		XAllowEvents(dpy, ReplayPointer, CurrentTime);
     49 -		click = ClkClientWin;
     50 -	}
     51 -	for (i = 0; i < LENGTH(buttons); i++)
     52 -		if (click == buttons[i].click && buttons[i].func && buttons[i].button == ev->button
     53 -		&& CLEANMASK(buttons[i].mask) == CLEANMASK(ev->state))
     54 -			buttons[i].func(click == ClkTagBar && buttons[i].arg.i == 0 ? &arg : &buttons[i].arg);
     55 +    unsigned int i, x, click;
     56 +    Arg arg = {0};
     57 +    Client *c;
     58 +    Monitor *m;
     59 +    XButtonPressedEvent *ev = &e->xbutton;
     60 +
     61 +    click = ClkRootWin;
     62 +    /* Focus monitor if necessary */
     63 +    if ((m = wintomon(ev->window)) && m != selmon) {
     64 +        unfocus(selmon->sel, 1);
     65 +        selmon = m;
     66 +        focus(NULL);
     67 +    }
     68 +
     69 +    if (ev->window == selmon->barwin) {
     70 +        x = 0;
     71 +        /* Calculate the starting x position for tags */
     72 +        unsigned int tag_width = 0;
     73 +        for (i = 0; i < LENGTH(tags); i++) {
     74 +            tag_width += TEXTW(tags[i]);
     75 +        }
     76 +        int center_x = (m->ww - tag_width) / 2;
     77 +
     78 +        x = center_x;
     79 +        for (i = 0; i < LENGTH(tags); i++) {
     80 +            int tag_w = TEXTW(tags[i]);
     81 +            if (ev->x >= x && ev->x < x + tag_w) {
     82 +                click = ClkTagBar;
     83 +                arg.ui = 1 << i;
     84 +                break;
     85 +            }
     86 +            x += tag_w;
     87 +        }
     88 +
     89 +        if (click == ClkTagBar) {
     90 +            /* Handle tag click */
     91 +            // Call your function to switch to the clicked tag
     92 +        } else if (ev->x < x + TEXTW(selmon->ltsymbol)) {
     93 +            click = ClkLtSymbol;
     94 +        } else if (ev->x > selmon->ww - (int)TEXTW(stext)) {
     95 +            click = ClkStatusText;
     96 +        } else {
     97 +            click = ClkWinTitle;
     98 +        }
     99 +    } else if ((c = wintoclient(ev->window))) {
    100 +        focus(c);
    101 +        restack(selmon);
    102 +        XAllowEvents(dpy, ReplayPointer, CurrentTime);
    103 +        click = ClkClientWin;
    104 +    }
    105 +
    106 +    for (i = 0; i < LENGTH(buttons); i++)
    107 +        if (click == buttons[i].click && buttons[i].func && buttons[i].button == ev->button
    108 +        && CLEANMASK(buttons[i].mask) == CLEANMASK(ev->state))
    109 +            buttons[i].func(click == ClkTagBar && buttons[i].arg.i == 0 ? &arg : &buttons[i].arg);
    110  }
    111  
    112  void
    113 @@ -698,54 +716,51 @@ dirtomon(int dir)
    114  void
    115  drawbar(Monitor *m)
    116  {
    117 -	int x, w, tw = 0;
    118 -	int boxs = drw->fonts->h / 9;
    119 -	int boxw = drw->fonts->h / 6 + 2;
    120 -	unsigned int i, occ = 0, urg = 0;
    121 -	Client *c;
    122 -
    123 -	if (!m->showbar)
    124 -		return;
    125 -
    126 -	/* draw status first so it can be overdrawn by tags later */
    127 -	if (m == selmon) { /* status is only drawn on selected monitor */
    128 -		drw_setscheme(drw, scheme[SchemeNorm]);
    129 -		tw = TEXTW(stext) - lrpad + 2; /* 2px right padding */
    130 -		drw_text(drw, m->ww - tw, 0, tw, bh, 0, stext, 0);
    131 -	}
    132 -
    133 -	for (c = m->clients; c; c = c->next) {
    134 -		occ |= c->tags;
    135 -		if (c->isurgent)
    136 -			urg |= c->tags;
    137 -	}
    138 -	x = 0;
    139 -	for (i = 0; i < LENGTH(tags); i++) {
    140 -		w = TEXTW(tags[i]);
    141 -		drw_setscheme(drw, scheme[m->tagset[m->seltags] & 1 << i ? SchemeSel : SchemeNorm]);
    142 -		drw_text(drw, x, 0, w, bh, lrpad / 2, tags[i], urg & 1 << i);
    143 -		if (occ & 1 << i)
    144 -			drw_rect(drw, x + boxs, boxs, boxw, boxw,
    145 -				m == selmon && selmon->sel && selmon->sel->tags & 1 << i,
    146 -				urg & 1 << i);
    147 -		x += w;
    148 -	}
    149 -	w = TEXTW(m->ltsymbol);
    150 -	drw_setscheme(drw, scheme[SchemeNorm]);
    151 -	x = drw_text(drw, x, 0, w, bh, lrpad / 2, m->ltsymbol, 0);
    152 -
    153 -	if ((w = m->ww - tw - x) > bh) {
    154 -		if (m->sel) {
    155 -			drw_setscheme(drw, scheme[m == selmon ? SchemeSel : SchemeNorm]);
    156 -			drw_text(drw, x, 0, w, bh, lrpad / 2, m->sel->name, 0);
    157 -			if (m->sel->isfloating)
    158 -				drw_rect(drw, x + boxs, boxs, boxw, boxw, m->sel->isfixed, 0);
    159 -		} else {
    160 -			drw_setscheme(drw, scheme[SchemeNorm]);
    161 -			drw_rect(drw, x, 0, w, bh, 1, 1);
    162 -		}
    163 -	}
    164 -	drw_map(drw, m->barwin, 0, 0, m->ww, bh);
    165 +    int x, w, tw = 0;
    166 +    int boxs = drw->fonts->h / 9;
    167 +    int boxw = drw->fonts->h / 6 + 2;
    168 +    unsigned int i, occ = 0, urg = 0;
    169 +    Client *c;
    170 +
    171 +    if (!m->showbar)
    172 +        return;
    173 +
    174 +    /* Draw layout symbol on the left */
    175 +    w = TEXTW(m->ltsymbol);
    176 +    drw_setscheme(drw, scheme[SchemeNorm]);
    177 +    drw_text(drw, 0, 0, w, bh, lrpad / 2, m->ltsymbol, 0);
    178 +
    179 +    x = w; // Update x to the end of layout symbol
    180 +
    181 +    /* Calculate total width of tags and center them */
    182 +    int tag_width = 0;
    183 +    for (i = 0; i < LENGTH(tags); i++) {
    184 +        tag_width += TEXTW(tags[i]);
    185 +    }
    186 +    int center_x = (m->ww - tag_width) / 2;
    187 +
    188 +    /* Draw tags centered */
    189 +    x = center_x; // Start x at the centered position
    190 +    for (i = 0; i < LENGTH(tags); i++) {
    191 +        w = TEXTW(tags[i]);
    192 +        drw_setscheme(drw, scheme[m->tagset[m->seltags] & 1 << i ? SchemeSel : SchemeNorm]);
    193 +        drw_text(drw, x, 0, w, bh, lrpad / 2, tags[i], urg & 1 << i);
    194 +        if (occ & 1 << i)
    195 +            drw_rect(drw, x + boxs, boxs, boxw, boxw,
    196 +                m == selmon && selmon->sel && selmon->sel->tags & 1 << i,
    197 +                urg & 1 << i);
    198 +        x += w;
    199 +    }
    200 +
    201 +    /* Draw status text on the right */
    202 +    if (m == selmon) { /* Status is only drawn on selected monitor */
    203 +        drw_setscheme(drw, scheme[SchemeNorm]);
    204 +        tw = TEXTW(stext) - lrpad + 2; /* 2px right padding */
    205 +        drw_text(drw, m->ww - tw, 0, tw, bh, 0, stext, 0);
    206 +    }
    207 +
    208 +    /* Ensure layout symbol, tags, and status text do not overlap */
    209 +    drw_map(drw, m->barwin, 0, 0, m->ww, bh);
    210  }
    211  
    212  void
    213 -- 
    214 2.46.0
    215