i3 - improved tiling WM


i3-nagbar: Set button inner-width to the width of the label

Patch status: merged

Patch by Tony Crisci

Long description:

Use predict_text_width to find the width of the label and then account
for right padding when calculating the width of the button.

To apply this patch, use:
curl http://cr.i3wm.org/patch/257/raw.patch | git am

b/i3-nagbar/main.c

15
@@ -198,8 +198,12 @@ static int handle_expose(xcb_connection_t *conn, xcb_expose_event_t *event) {
16
             4 + 4, 4 + 4, rect.width - 4 - 4);
17
 
18
     /* render close button */
19
+    const char *close_button_label = "X";
20
     int line_width = 4;
21
-    int w = 20;
22
+    /* set width to the width of the label */
23
+    int w = predict_text_width(i3string_from_utf8(close_button_label));
24
+    /* account for left/right padding, which seems to be set to 8px (total) below */
25
+    w += 8;
26
     int y = rect.width;
27
     uint32_t values[3];
28
     values[0] = color_button_background;
29
@@ -221,7 +225,8 @@ static int handle_expose(xcb_connection_t *conn, xcb_expose_event_t *event) {
30
 
31
     values[0] = 1;
32
     set_font_colors(pixmap_gc, color_text, color_button_background);
33
-    draw_text_ascii("X", pixmap, pixmap_gc, y - w - line_width + w / 2 - 4,
34
+    /* the x term here seems to set left/right padding */
35
+    draw_text_ascii(close_button_label, pixmap, pixmap_gc, y - w - line_width + w / 2 - 4,
36
             4 + 4 - 1, rect.width - y + w + line_width - w / 2 + 4);
37
     y -= w;
38
 
39
@@ -230,8 +235,10 @@ static int handle_expose(xcb_connection_t *conn, xcb_expose_event_t *event) {
40
     /* render custom buttons */
41
     line_width = 1;
42
     for (int c = 0; c < buttoncnt; c++) {
43
-        /* TODO: make w = text extents of the label */
44
-        w = 100;
45
+        /* set w to the width of the label */
46
+        w = predict_text_width(buttons[c].label);
47
+        /* account for left/right padding, which seems to be set to 12px (total) below */
48
+        w += 12;
49
         y -= 30;
50
         xcb_change_gc(conn, pixmap_gc, XCB_GC_FOREGROUND, (uint32_t[]){ color_button_background });
51
         close = (xcb_rectangle_t){ y - w - (2 * line_width), 2, w + (2 * line_width), rect.height - 6 };
52
@@ -252,6 +259,7 @@ static int handle_expose(xcb_connection_t *conn, xcb_expose_event_t *event) {
53
         values[0] = color_text;
54
         values[1] = color_button_background;
55
         set_font_colors(pixmap_gc, color_text, color_button_background);
56
+        /* the x term seems to set left/right padding */
57
         draw_text(buttons[c].label, pixmap, pixmap_gc,
58
                 y - w - line_width + 6, 4 + 3, rect.width - y + w + line_width - 6);
59