1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
|
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
#include "client/cairo.h"
#include "client/pango.h"
#include "client/window.h"
#include "swaybar/config.h"
#include "swaybar/status_line.h"
#include "swaybar/render.h"
#ifdef ENABLE_TRAY
#include "swaybar/tray/tray.h"
#include "swaybar/tray/sni.h"
#endif
#include "log.h"
/* internal spacing */
static int margin = 3;
static int ws_horizontal_padding = 5;
static double ws_vertical_padding = 1.5;
static int ws_spacing = 1;
/**
* Renders a sharp line of any width and height.
*
* The line is drawn from (x,y) to (x+width,y+height) where width/height is 0
* if the line has a width/height of one pixel, respectively.
*/
static void render_sharp_line(cairo_t *cairo, uint32_t color, double x, double y, double width, double height) {
cairo_set_source_u32(cairo, color);
if (width > 1 && height > 1) {
cairo_rectangle(cairo, x, y, width, height);
cairo_fill(cairo);
} else {
if (width == 1) {
x += 0.5;
height += y;
width = x;
}
if (height == 1) {
y += 0.5;
width += x;
height = y;
}
cairo_move_to(cairo, x, y);
cairo_set_line_width(cairo, 1.0);
cairo_line_to(cairo, width, height);
cairo_stroke(cairo);
}
}
static void render_block(struct window *window, struct config *config, struct status_block *block, double *x, bool edge, bool is_focused) {
int width, height, sep_width;
get_text_size(window->cairo, window->font, &width, &height,
window->scale, block->markup, "%s", block->full_text);
int textwidth = width;
double block_width = width;
if (width < block->min_width) {
width = block->min_width;
}
*x -= width;
if (block->border != 0 && block->border_left > 0) {
*x -= (block->border_left + margin);
block_width += block->border_left + margin;
}
if (block->border != 0 && block->border_right > 0) {
*x -= (block->border_right + margin);
block_width += block->border_right + margin;
}
// Add separator
if (!edge) {
if (config->sep_symbol) {
get_text_size(window->cairo, window->font, &sep_width, &height,
window->scale, false, "%s", config->sep_symbol);
if (sep_width > block->separator_block_width) {
block->separator_block_width = sep_width + margin * 2;
}
}
*x -= block->separator_block_width;
} else {
*x -= margin;
}
double pos = *x;
// render background
if (block->background != 0x0) {
cairo_set_source_u32(window->cairo, block->background);
cairo_rectangle(window->cairo, pos - 0.5, 1, block_width, (window->height * window->scale) - 2);
cairo_fill(window->cairo);
}
// render top border
if (block->border != 0 && block->border_top > 0) {
render_sharp_line(window->cairo, block->border,
pos - 0.5,
1,
block_width,
block->border_top);
}
// render bottom border
if (block->border != 0 && block->border_bottom > 0) {
render_sharp_line(window->cairo, block->border,
pos - 0.5,
(window->height * window->scale) - 1 - block->border_bottom,
block_width,
block->border_bottom);
}
// render left border
if (block->border != 0 && block->border_left > 0) {
render_sharp_line(window->cairo, block->border,
pos - 0.5,
1,
block->border_left,
(window->height * window->scale) - 2);
pos += block->border_left + margin;
}
// render text
double offset = 0;
if (strncmp(block->align, "left", 5) == 0) {
offset = pos;
} else if (strncmp(block->align, "right", 5) == 0) {
offset = pos + width - textwidth;
} else if (strncmp(block->align, "center", 6) == 0) {
offset = pos + (width - textwidth) / 2;
}
cairo_move_to(window->cairo, offset, margin);
cairo_set_source_u32(window->cairo, block->color);
pango_printf(window->cairo, window->font, window->scale,
block->markup, "%s", block->full_text);
pos += width;
// render right border
if (block->border != 0 && block->border_right > 0) {
pos += margin;
render_sharp_line(window->cairo, block->border,
pos - 0.5,
1,
block->border_right,
(window->height * window->scale) - 2);
pos += block->border_right;
}
// render separator
if (!edge && block->separator) {
if (is_focused) {
cairo_set_source_u32(window->cairo, config->colors.focused_separator);
} else {
cairo_set_source_u32(window->cairo, config->colors.separator);
}
if (config->sep_symbol) {
offset = pos + (block->separator_block_width - sep_width) / 2;
cairo_move_to(window->cairo, offset, margin);
pango_printf(window->cairo, window->font, window->scale,
false, "%s", config->sep_symbol);
} else {
cairo_set_line_width(window->cairo, 1);
cairo_move_to(window->cairo, pos + block->separator_block_width/2,
margin);
cairo_line_to(window->cairo, pos + block->separator_block_width/2,
(window->height * window->scale) - margin);
cairo_stroke(window->cairo);
}
}
}
static const char *strip_workspace_name(bool strip_num, const char *ws_name) {
bool strip = false;
int i;
if (strip_num) {
int len = strlen(ws_name);
for (i = 0; i < len; ++i) {
if (!('0' <= ws_name[i] && ws_name[i] <= '9')) {
if (':' == ws_name[i] && i < len-1 && i > 0) {
strip = true;
++i;
}
break;
}
}
}
if (strip) {
return ws_name + i;
}
return ws_name;
}
void workspace_button_size(struct window *window, const char *workspace_name, int *width, int *height) {
const char *stripped_name = strip_workspace_name(swaybar.config->strip_workspace_numbers, workspace_name);
get_text_size(window->cairo, window->font, width, height,
window->scale, true, "%s", stripped_name);
*width += 2 * ws_horizontal_padding;
*height += 2 * ws_vertical_padding;
}
static void render_workspace_button(struct window *window, struct config *config, struct workspace *ws, double *x) {
const char *stripped_name = strip_workspace_name(config->strip_workspace_numbers, ws->name);
struct box_colors box_colors;
if (ws->urgent) {
box_colors = config->colors.urgent_workspace;
} else if (ws->focused) {
box_colors = config->colors.focused_workspace;
} else if (ws->visible) {
box_colors = config->colors.active_workspace;
} else {
box_colors = config->colors.inactive_workspace;
}
int width, height;
workspace_button_size(window, stripped_name, &width, &height);
// background
cairo_set_source_u32(window->cairo, box_colors.background);
cairo_rectangle(window->cairo, *x, 1.5, width - 1, height);
cairo_fill(window->cairo);
// border
cairo_set_source_u32(window->cairo, box_colors.border);
cairo_rectangle(window->cairo, *x, 1.5, width - 1, height);
cairo_stroke(window->cairo);
// text
cairo_set_source_u32(window->cairo, box_colors.text);
cairo_move_to(window->cairo, (int)*x + ws_horizontal_padding, margin);
pango_printf(window->cairo, window->font, window->scale,
true, "%s", stripped_name);
*x += width + ws_spacing;
}
static void render_binding_mode_indicator(struct window *window, struct config *config, double pos) {
int width, height;
get_text_size(window->cairo, window->font, &width, &height,
window->scale, false, "%s", config->mode);
// background
cairo_set_source_u32(window->cairo, config->colors.binding_mode.background);
cairo_rectangle(window->cairo, pos, 1.5, width + ws_horizontal_padding * 2 - 1,
height + ws_vertical_padding * 2);
cairo_fill(window->cairo);
// border
cairo_set_source_u32(window->cairo, config->colors.binding_mode.border);
cairo_rectangle(window->cairo, pos, 1.5, width + ws_horizontal_padding * 2 - 1,
height + ws_vertical_padding * 2);
cairo_stroke(window->cairo);
// text
cairo_set_source_u32(window->cairo, config->colors.binding_mode.text);
cairo_move_to(window->cairo, (int)pos + ws_horizontal_padding, margin);
pango_printf(window->cairo, window->font, window->scale,
false, "%s", config->mode);
}
void render(struct output *output, struct config *config, struct status_line *line) {
int i;
struct window *window = output->window;
cairo_t *cairo = window->cairo;
bool is_focused = output->focused;
// Clear
cairo_save(cairo);
cairo_set_operator(cairo, CAIRO_OPERATOR_CLEAR);
cairo_paint(cairo);
cairo_restore(cairo);
cairo_set_operator(cairo, CAIRO_OPERATOR_SOURCE);
// Background
if (is_focused) {
cairo_set_source_u32(cairo, config->colors.focused_background);
} else {
cairo_set_source_u32(cairo, config->colors.background);
}
cairo_paint(cairo);
#ifdef ENABLE_TRAY
// Tray icons
uint32_t tray_padding = config->tray_padding;
unsigned int tray_width = window->width * window->scale;
const int item_size = (window->height * window->scale) - (2 * tray_padding);
if (item_size < 0) {
// Can't render items if the padding is too large
goto no_tray;
}
if (config->tray_output && strcmp(config->tray_output, output->name) != 0) {
goto no_tray;
}
for (int i = 0; i < tray->items->length; ++i) {
struct StatusNotifierItem *item =
tray->items->items[i];
if (!item->image) {
continue;
}
struct sni_icon_ref *render_item = NULL;
int j;
for (j = i; j < output->items->length; ++j) {
struct sni_icon_ref *ref =
output->items->items[j];
if (ref->ref == item) {
render_item = ref;
break;
} else {
sni_icon_ref_free(ref);
list_del(output->items, j);
}
}
if (!render_item) {
render_item = sni_icon_ref_create(item, item_size);
list_add(output->items, render_item);
} else if (item->dirty) {
// item needs re-render
sni_icon_ref_free(render_item);
output->items->items[j] = render_item =
sni_icon_ref_create(item, item_size);
}
tray_width -= tray_padding;
tray_width -= item_size;
cairo_operator_t op = cairo_get_operator(cairo);
cairo_set_operator(cairo, CAIRO_OPERATOR_OVER);
cairo_set_source_surface(cairo, render_item->icon, tray_width, tray_padding);
cairo_rectangle(cairo, tray_width, tray_padding, item_size, item_size);
cairo_fill(cairo);
cairo_set_operator(cairo, op);
item->dirty = false;
}
if (tray_width != window->width * window->scale) {
tray_width -= tray_padding;
}
no_tray:
#else
const int tray_width = window->width * window->scale;
#endif
// Command output
if (is_focused) {
cairo_set_source_u32(cairo, config->colors.focused_statusline);
} else {
cairo_set_source_u32(cairo, config->colors.statusline);
}
int width, height;
if (line->protocol == TEXT) {
get_text_size(window->cairo, window->font, &width, &height,
window->scale, config->pango_markup, "%s", line->text_line);
cairo_move_to(cairo, tray_width - margin - width, margin);
pango_printf(window->cairo, window->font, window->scale,
config->pango_markup, "%s", line->text_line);
} else if (line->protocol == I3BAR && line->block_line) {
double pos = tray_width - 0.5;
bool edge = true;
for (i = line->block_line->length - 1; i >= 0; --i) {
struct status_block *block = line->block_line->items[i];
if (block->full_text && block->full_text[0]) {
render_block(window, config, block, &pos, edge, is_focused);
edge = false;
}
}
}
cairo_set_line_width(cairo, 1.0);
double x = 0.5;
// Workspaces
if (config->workspace_buttons) {
for (i = 0; i < output->workspaces->length; ++i) {
struct workspace *ws = output->workspaces->items[i];
render_workspace_button(window, config, ws, &x);
}
}
// binding mode indicator
if (config->mode && config->binding_mode_indicator) {
render_binding_mode_indicator(window, config, x);
}
}
void set_window_height(struct window *window, int height) {
int text_width, text_height;
get_text_size(window->cairo, window->font,
&text_width, &text_height, window->scale, false,
"Test string for measuring purposes");
if (height > 0) {
margin = (height - text_height) / 2;
ws_vertical_padding = margin - 1.5;
}
window->height = (text_height + margin * 2) / window->scale;
}
|