i3 - improved tiling WM


i3bar: Realign tray clients on map/unmap notify

Patch status: merged

Patch by Tony Crisci

Long description:

UnmapNotify events are interpreted by i3bar as an action taken by an
application to hide its tray window. Likewise, MapNotify events are
interpreted as an action taken by by an application to show its tray
window.

The actual cause of these events may be the application itself, or the
result of some action taken by i3bar itself at the request of the
application in the course of the XEmbed protocol.

We respond by adjusting the size of the tray window and realigning any
tray clients that remain. This will make room for the mapping window or
close the gap left by the unmapping window when the bar is redrawn.

fixes #1110

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

b/i3bar/src/xcb.c

27
@@ -433,8 +433,9 @@ void handle_button(xcb_button_press_event_t *event) {
28
 }
29
 
30
 /*
31
- * Configures the x coordinate of all trayclients. To be called after adding a
32
- * new tray client or removing an old one.
33
+ * Adjusts the size of the tray window and alignment of the tray clients by
34
+ * configuring their respective x coordinates. To be called when mapping or
35
+ * unmapping a tray client window.
36
  *
37
  */
38
 static void configure_trayclients(void) {
39
@@ -610,7 +611,6 @@ static void handle_client_message(xcb_client_message_event_t* event) {
40
             }
41
             trayclient *tc = smalloc(sizeof(trayclient));
42
             tc->win = client;
43
-            tc->mapped = map_it;
44
             tc->xe_version = xe_version;
45
             TAILQ_INSERT_TAIL(output->trayclients, tc, tailq);
46
 
47
@@ -656,8 +656,36 @@ static void handle_destroy_notify(xcb_destroy_notify_event_t* event) {
48
 }
49
 
50
 /*
51
- * Handles UnmapNotify events. These events happen when a tray window unmaps
52
- * itself. We then update our data structure
53
+ * Handles MapNotify events. These events happen when a tray client shows its
54
+ * window. We respond by realigning the tray clients.
55
+ *
56
+ */
57
+static void handle_map_notify(xcb_map_notify_event_t* event) {
58
+    DLOG("MapNotify for window = %08x, event = %08x\n", event->window, event->event);
59
+
60
+    i3_output *walk;
61
+    SLIST_FOREACH(walk, outputs, slist) {
62
+        if (!walk->active)
63
+            continue;
64
+        DLOG("checking output %s\n", walk->name);
65
+        trayclient *trayclient;
66
+        TAILQ_FOREACH(trayclient, walk->trayclients, tailq) {
67
+            if (trayclient->win != event->window)
68
+                continue;
69
+
70
+            DLOG("Tray client mapped (window ID %08x). Adjusting tray.\n", event->window);
71
+            trayclient->mapped = true;
72
+
73
+            /* Trigger an update, we now have more space for the statusline */
74
+            configure_trayclients();
75
+            draw_bars(false);
76
+            return;
77
+        }
78
+    }
79
+}
80
+/*
81
+ * Handles UnmapNotify events. These events happen when a tray client hides its
82
+ * window. We respond by realigning the tray clients.
83
  *
84
  */
85
 static void handle_unmap_notify(xcb_unmap_notify_event_t* event) {
86
@@ -673,8 +701,8 @@ static void handle_unmap_notify(xcb_unmap_notify_event_t* event) {
87
             if (trayclient->win != event->window)
88
                 continue;
89
 
90
-            DLOG("Removing tray client with window ID %08x\n", event->window);
91
-            TAILQ_REMOVE(walk->trayclients, trayclient, tailq);
92
+            DLOG("Tray client unmapped (window ID %08x). Adjusting tray.\n", event->window);
93
+            trayclient->mapped = false;
94
 
95
             /* Trigger an update, we now have more space for the statusline */
96
             configure_trayclients();
97
@@ -741,15 +769,9 @@ static void handle_property_notify(xcb_property_notify_event_t *event) {
98
         if (trayclient->mapped && !map_it) {
99
             /* need to unmap the window */
100
             xcb_unmap_window(xcb_connection, trayclient->win);
101
-            trayclient->mapped = map_it;
102
-            configure_trayclients();
103
-            draw_bars(false);
104
         } else if (!trayclient->mapped && map_it) {
105
             /* need to map the window */
106
             xcb_map_window(xcb_connection, trayclient->win);
107
-            trayclient->mapped = map_it;
108
-            configure_trayclients();
109
-            draw_bars(false);
110
         }
111
         free(xembedr);
112
     }
113
@@ -836,9 +858,12 @@ void xcb_chk_cb(struct ev_loop *loop, ev_check *watcher, int revents) {
114
                 handle_destroy_notify((xcb_destroy_notify_event_t*) event);
115
                 break;
116
             case XCB_UNMAP_NOTIFY:
117
-                /* UnmapNotifies are received when a tray window unmaps itself */
118
+                /* UnmapNotify is received when a tray client hides its window. */
119
                 handle_unmap_notify((xcb_unmap_notify_event_t*) event);
120
                 break;
121
+            case XCB_MAP_NOTIFY:
122
+                handle_map_notify((xcb_map_notify_event_t*) event);
123
+                break;
124
             case XCB_PROPERTY_NOTIFY:
125
                 /* PropertyNotify */
126
                 handle_property_notify((xcb_property_notify_event_t*) event);