diff options
author | David Eklov <david.eklov@gmail.com> | 2016-07-10 22:47:10 -0500 |
---|---|---|
committer | David Eklov <david.eklov@gmail.com> | 2016-07-14 17:18:01 -0500 |
commit | c805e42635f3935ccf0ce927143433b94c78a3e7 (patch) | |
tree | e5b5a1da0669ab99d8a9ee32d4146689d72de87d /swaybar | |
parent | Setup to receive mouse click events and log them (diff) | |
download | sway-c805e42635f3935ccf0ce927143433b94c78a3e7.tar.gz sway-c805e42635f3935ccf0ce927143433b94c78a3e7.tar.zst sway-c805e42635f3935ccf0ce927143433b94c78a3e7.zip |
Extract workspace size computation from render_workspace_button()
Also remove some unnecessary strtup()s and rename a few variables and functions.
Diffstat (limited to 'swaybar')
-rw-r--r-- | swaybar/render.c | 34 |
1 files changed, 19 insertions, 15 deletions
diff --git a/swaybar/render.c b/swaybar/render.c index 273bd4f0..cea36f52 100644 --- a/swaybar/render.c +++ b/swaybar/render.c | |||
@@ -172,7 +172,7 @@ static void render_block(struct window *window, struct config *config, struct st | |||
172 | 172 | ||
173 | } | 173 | } |
174 | 174 | ||
175 | static char *handle_workspace_number(bool strip_num, const char *ws_name) { | 175 | static const char *strip_workspace_name(bool strip_num, const char *ws_name) { |
176 | bool strip = false; | 176 | bool strip = false; |
177 | int i; | 177 | int i; |
178 | 178 | ||
@@ -190,18 +190,23 @@ static char *handle_workspace_number(bool strip_num, const char *ws_name) { | |||
190 | } | 190 | } |
191 | 191 | ||
192 | if (strip) { | 192 | if (strip) { |
193 | return strdup(ws_name + i); | 193 | return ws_name + i; |
194 | } | 194 | } |
195 | 195 | ||
196 | return strdup(ws_name); | 196 | return ws_name; |
197 | } | ||
198 | |||
199 | void workspace_button_size(struct window *window, const char *workspace_name, int *width, int *height) { | ||
200 | const char *stripped_name = strip_workspace_name(swaybar.config->strip_workspace_numbers, workspace_name); | ||
201 | |||
202 | get_text_size(window->cairo, window->font, width, height, false, "%s", stripped_name); | ||
203 | *width += 2 * ws_horizontal_padding; | ||
204 | *height += 2 * ws_vertical_padding; | ||
197 | } | 205 | } |
198 | 206 | ||
199 | static void render_workspace_button(struct window *window, struct config *config, struct workspace *ws, double *x) { | 207 | static void render_workspace_button(struct window *window, struct config *config, struct workspace *ws, double *x) { |
200 | // strip workspace numbers if required | 208 | const char *stripped_name = strip_workspace_name(config->strip_workspace_numbers, ws->name); |
201 | char *name = handle_workspace_number(config->strip_workspace_numbers, ws->name); | ||
202 | 209 | ||
203 | int width, height; | ||
204 | get_text_size(window->cairo, window->font, &width, &height, false, "%s", name); | ||
205 | struct box_colors box_colors; | 210 | struct box_colors box_colors; |
206 | if (ws->urgent) { | 211 | if (ws->urgent) { |
207 | box_colors = config->colors.urgent_workspace; | 212 | box_colors = config->colors.urgent_workspace; |
@@ -213,26 +218,25 @@ static void render_workspace_button(struct window *window, struct config *config | |||
213 | box_colors = config->colors.inactive_workspace; | 218 | box_colors = config->colors.inactive_workspace; |
214 | } | 219 | } |
215 | 220 | ||
221 | int width, height; | ||
222 | workspace_button_size(window, stripped_name, &width, &height); | ||
223 | |||
216 | // background | 224 | // background |
217 | cairo_set_source_u32(window->cairo, box_colors.background); | 225 | cairo_set_source_u32(window->cairo, box_colors.background); |
218 | cairo_rectangle(window->cairo, *x, 1.5, width + ws_horizontal_padding * 2 - 1, | 226 | cairo_rectangle(window->cairo, *x, 1.5, width - 1, height); |
219 | height + ws_vertical_padding * 2); | ||
220 | cairo_fill(window->cairo); | 227 | cairo_fill(window->cairo); |
221 | 228 | ||
222 | // border | 229 | // border |
223 | cairo_set_source_u32(window->cairo, box_colors.border); | 230 | cairo_set_source_u32(window->cairo, box_colors.border); |
224 | cairo_rectangle(window->cairo, *x, 1.5, width + ws_horizontal_padding * 2 - 1, | 231 | cairo_rectangle(window->cairo, *x, 1.5, width - 1, height); |
225 | height + ws_vertical_padding * 2); | ||
226 | cairo_stroke(window->cairo); | 232 | cairo_stroke(window->cairo); |
227 | 233 | ||
228 | // text | 234 | // text |
229 | cairo_set_source_u32(window->cairo, box_colors.text); | 235 | cairo_set_source_u32(window->cairo, box_colors.text); |
230 | cairo_move_to(window->cairo, (int)*x + ws_horizontal_padding, margin); | 236 | cairo_move_to(window->cairo, (int)*x + ws_horizontal_padding, margin); |
231 | pango_printf(window->cairo, window->font, false, "%s", name); | 237 | pango_printf(window->cairo, window->font, false, "%s", stripped_name); |
232 | |||
233 | *x += width + ws_horizontal_padding * 2 + ws_spacing; | ||
234 | 238 | ||
235 | free(name); | 239 | *x += width + ws_spacing; |
236 | } | 240 | } |
237 | 241 | ||
238 | static void render_binding_mode_indicator(struct window *window, struct config *config, double pos) { | 242 | static void render_binding_mode_indicator(struct window *window, struct config *config, double pos) { |