aboutsummaryrefslogtreecommitdiffstats
path: root/swaybar/main.c
diff options
context:
space:
mode:
authorLibravatar crondog <crondog@gmail.com>2015-12-24 13:55:17 +1100
committerLibravatar crondog <crondog@gmail.com>2015-12-24 13:55:17 +1100
commitedd93b5b55c66f3914f95738b9b23b2ac1ef7c75 (patch)
treef096c311426627bae9e1751948064de5dbecfcd2 /swaybar/main.c
parentMerge pull request #400 from gpyh/forgot_include (diff)
downloadsway-edd93b5b55c66f3914f95738b9b23b2ac1ef7c75.tar.gz
sway-edd93b5b55c66f3914f95738b9b23b2ac1ef7c75.tar.zst
sway-edd93b5b55c66f3914f95738b9b23b2ac1ef7c75.zip
swaybar: min_width and align
Diffstat (limited to 'swaybar/main.c')
-rw-r--r--swaybar/main.c52
1 files changed, 32 insertions, 20 deletions
diff --git a/swaybar/main.c b/swaybar/main.c
index 74930075..37218c32 100644
--- a/swaybar/main.c
+++ b/swaybar/main.c
@@ -414,9 +414,29 @@ void render() {
414 struct status_block *block = status_line->items[i]; 414 struct status_block *block = status_line->items[i];
415 if (block->full_text && block->full_text[0]) { 415 if (block->full_text && block->full_text[0]) {
416 get_text_size(window, &width, &height, "%s", block->full_text); 416 get_text_size(window, &width, &height, "%s", block->full_text);
417
418 int textwidth = width;
419
420 if (width < block->min_width) {
421 width = block->min_width;
422 }
423
417 moved += width + block->separator_block_width; 424 moved += width + block->separator_block_width;
418 blockpos = window->width - margin - moved; 425 blockpos = window->width - margin - moved;
419 cairo_move_to(window->cairo, blockpos, margin); 426
427 int offset = 0;
428
429 if (strncmp(block->align, "left", 5) == 0) {
430 offset = blockpos;
431 }
432 else if (strncmp(block->align, "right", 5) == 0) {
433 offset = blockpos + width - textwidth;
434 }
435 else if (strncmp(block->align, "center", 6) == 0) {
436 offset = blockpos + (width - textwidth) / 2;
437 }
438
439 cairo_move_to(window->cairo, offset, margin);
420 cairo_set_source_u32(window->cairo, block->color); 440 cairo_set_source_u32(window->cairo, block->color);
421 pango_printf(window, "%s", block->full_text); 441 pango_printf(window, "%s", block->full_text);
422 if (corner) { 442 if (corner) {
@@ -425,9 +445,9 @@ void render() {
425 cairo_set_source_u32(window->cairo, colors.separator); 445 cairo_set_source_u32(window->cairo, colors.separator);
426 cairo_set_line_width(window->cairo, 1); 446 cairo_set_line_width(window->cairo, 1);
427 cairo_move_to(window->cairo, blockpos + width 447 cairo_move_to(window->cairo, blockpos + width
428 + block->separator_block_width/2, margin); 448 + block->separator_block_width/2, margin);
429 cairo_line_to(window->cairo, blockpos + width 449 cairo_line_to(window->cairo, blockpos + width
430 + block->separator_block_width/2, window->height - margin); 450 + block->separator_block_width/2, window->height - margin);
431 cairo_stroke(window->cairo); 451 cairo_stroke(window->cairo);
432 } 452 }
433 } 453 }
@@ -492,22 +512,6 @@ void free_status_block(void *item) {
492} 512}
493 513
494void parse_json(const char *text) { 514void parse_json(const char *text) {
495/* the array of objects looks like this:
496 * [ {
497 * "full_text": "E: 10.0.0.1 (1000 Mbit/s)",
498 * "short_text": "10.0.0.1",
499 * "color": "#00ff00",
500 * "min_width": 300,
501 * "align": "right",
502 * "urgent": false,
503 * "name": "ethernet",
504 * "instance": "eth0",
505 * "separator": true,
506 * "separator_block_width": 9
507 * },
508 * { ... }, ...
509 * ]
510 */
511 json_object *results = json_tokener_parse(text); 515 json_object *results = json_tokener_parse(text);
512 if (!results) { 516 if (!results) {
513 sway_log(L_DEBUG, "xxx Failed to parse json"); 517 sway_log(L_DEBUG, "xxx Failed to parse json");
@@ -565,7 +569,15 @@ void parse_json(const char *text) {
565 } 569 }
566 570
567 if (min_width) { 571 if (min_width) {
568 new->min_width = json_object_get_int(min_width); 572 json_type type = json_object_get_type(min_width);
573 if (type == json_type_int) {
574 new->min_width = json_object_get_int(min_width);
575 }
576 else if (type == json_type_string) {
577 int width, height;
578 get_text_size(window, &width, &height, "%s", json_object_get_string(min_width));
579 new->min_width = width;
580 }
569 } 581 }
570 582
571 if (align) { 583 if (align) {