From fed11d1c7b388e999414dd8cec4b8982ba5ce950 Mon Sep 17 00:00:00 2001 From: Ian Fan Date: Fri, 12 Oct 2018 20:23:01 +0100 Subject: [PATCH] swaybar: move mode & mode_pango_markup to bar struct This distinguishes the binding mode from the distinct config mode, as well as removing mode_pango_markup from the config struct where it should not be present. --- include/swaybar/bar.h | 2 ++ include/swaybar/config.h | 1 - swaybar/bar.c | 1 + swaybar/ipc.c | 11 +++-------- swaybar/render.c | 14 +++++++++----- 5 files changed, 15 insertions(+), 14 deletions(-) diff --git a/include/swaybar/bar.h b/include/swaybar/bar.h index 72e0ca75..593f4f6d 100644 --- a/include/swaybar/bar.h +++ b/include/swaybar/bar.h @@ -47,6 +47,8 @@ struct swaybar_hotspot { struct swaybar { char *id; + char *mode; + bool mode_pango_markup; struct wl_display *display; struct wl_compositor *compositor; diff --git a/include/swaybar/config.h b/include/swaybar/config.h index 99948463..68ee2087 100644 --- a/include/swaybar/config.h +++ b/include/swaybar/config.h @@ -31,7 +31,6 @@ struct swaybar_config { char *font; char *sep_symbol; char *mode; - bool mode_pango_markup; bool strip_workspace_numbers; bool binding_mode_indicator; bool wrap_scroll; diff --git a/swaybar/bar.c b/swaybar/bar.c index ecf1bf82..77294136 100644 --- a/swaybar/bar.c +++ b/swaybar/bar.c @@ -631,4 +631,5 @@ void bar_teardown(struct swaybar *bar) { status_line_free(bar->status); } free(bar->id); + free(bar->mode); } diff --git a/swaybar/ipc.c b/swaybar/ipc.c index 26b7eca6..8568f957 100644 --- a/swaybar/ipc.c +++ b/swaybar/ipc.c @@ -386,12 +386,8 @@ bool handle_ipc_readable(struct swaybar *bar) { json_object *json_change, *json_pango_markup; if (json_object_object_get_ex(result, "change", &json_change)) { const char *change = json_object_get_string(json_change); - free(bar->config->mode); - if (strcmp(change, "default") == 0) { - bar->config->mode = NULL; - } else { - bar->config->mode = strdup(change); - } + free(bar->mode); + bar->mode = strcmp(change, "default") != 0 ? strdup(change) : NULL; } else { wlr_log(WLR_ERROR, "failed to parse response"); json_object_put(result); @@ -400,8 +396,7 @@ bool handle_ipc_readable(struct swaybar *bar) { } if (json_object_object_get_ex(result, "pango_markup", &json_pango_markup)) { - bar->config->mode_pango_markup = json_object_get_boolean( - json_pango_markup); + bar->mode_pango_markup = json_object_get_boolean(json_pango_markup); } json_object_put(result); break; diff --git a/swaybar/render.c b/swaybar/render.c index 2a06a79b..d226ba32 100644 --- a/swaybar/render.c +++ b/swaybar/render.c @@ -296,11 +296,15 @@ static uint32_t render_status_line(cairo_t *cairo, static uint32_t render_binding_mode_indicator(cairo_t *cairo, struct swaybar_output *output, double x) { + const char *mode = output->bar->mode; + if (!mode) { + return 0; + } + struct swaybar_config *config = output->bar->config; - const char *mode = config->mode; int text_width, text_height; get_text_size(cairo, config->font, &text_width, &text_height, NULL, - output->scale, config->mode_pango_markup, + output->scale, output->bar->mode_pango_markup, "%s", mode); int ws_vertical_padding = WS_VERTICAL_PADDING * output->scale; @@ -333,8 +337,8 @@ static uint32_t render_binding_mode_indicator(cairo_t *cairo, double text_y = height / 2.0 - text_height / 2.0; cairo_set_source_u32(cairo, config->colors.binding_mode.text); cairo_move_to(cairo, x + width / 2 - text_width / 2, (int)floor(text_y)); - pango_printf(cairo, config->font, output->scale, config->mode_pango_markup, - "%s", mode); + pango_printf(cairo, config->font, output->scale, + output->bar->mode_pango_markup, "%s", mode); return output->height; } @@ -465,7 +469,7 @@ static uint32_t render_to_cairo(cairo_t *cairo, struct swaybar_output *output) { max_height = h > max_height ? h : max_height; } } - if (config->binding_mode_indicator && config->mode) { + if (config->binding_mode_indicator) { uint32_t h = render_binding_mode_indicator(cairo, output, x); max_height = h > max_height ? h : max_height; }