diff options
Diffstat (limited to 'swaybar')
-rw-r--r-- | swaybar/bar.c | 34 | ||||
-rw-r--r-- | swaybar/ipc.c | 9 | ||||
-rw-r--r-- | swaybar/render.c | 34 |
3 files changed, 62 insertions, 15 deletions
diff --git a/swaybar/bar.c b/swaybar/bar.c index 6d858f92..4f8063ac 100644 --- a/swaybar/bar.c +++ b/swaybar/bar.c @@ -58,6 +58,37 @@ struct output *new_output(const char *name) { return output; } +static void mouse_button_notify(struct window *window, int x, int y, uint32_t button) { + sway_log(L_DEBUG, "Mouse button %d clicked at %d %d\n", button, x, y); + + struct output *clicked_output = NULL; + for (int i = 0; i < swaybar.outputs->length; i++) { + struct output *output = swaybar.outputs->items[i]; + if (window == output->window) { + clicked_output = output; + break; + } + } + + if (!sway_assert(clicked_output != NULL, "Got pointer event for non-existing output")) { + return; + } + + double button_x = 0.5; + for (int i = 0; i < clicked_output->workspaces->length; i++) { + struct workspace *workspace = clicked_output->workspaces->items[i]; + int button_width, button_height; + + workspace_button_size(window, workspace->name, &button_width, &button_height); + + button_x += button_width; + if (x <= button_x) { + ipc_send_workspace_command(workspace->name); + break; + } + } +} + void bar_setup(struct bar *bar, const char *socket_path, const char *bar_id) { /* initialize bar with default values */ bar_init(bar); @@ -92,6 +123,9 @@ void bar_setup(struct bar *bar, const char *socket_path, const char *bar_id) { /* set font */ bar_output->window->font = bar->config->font; + /* set font */ + bar_output->window->pointer_input.notify = mouse_button_notify; + /* set window height */ set_window_height(bar_output->window, bar->config->height); } diff --git a/swaybar/ipc.c b/swaybar/ipc.c index dacee4c2..15f40508 100644 --- a/swaybar/ipc.c +++ b/swaybar/ipc.c @@ -7,6 +7,15 @@ #include "bar/config.h" #include "bar/ipc.h" +void ipc_send_workspace_command(const char *workspace_name) { + uint32_t size = strlen("workspace ") + strlen(workspace_name) + 1; + + char command[size]; + sprintf(command, "workspace %s", workspace_name); + + ipc_single_command(swaybar.ipc_socketfd, IPC_COMMAND, command, &size); +} + static void ipc_parse_config(struct config *config, const char *payload) { json_object *bar_config = json_tokener_parse(payload); json_object *tray_output, *mode, *hidden_bar, *position, *status_command; 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 } -static char *handle_workspace_number(bool strip_num, const char *ws_name) { +static const char *strip_workspace_name(bool strip_num, const char *ws_name) { bool strip = false; int i; @@ -190,18 +190,23 @@ static char *handle_workspace_number(bool strip_num, const char *ws_name) { } if (strip) { - return strdup(ws_name + i); + return ws_name + i; } - return strdup(ws_name); + 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, false, "%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) { - // strip workspace numbers if required - char *name = handle_workspace_number(config->strip_workspace_numbers, ws->name); + const char *stripped_name = strip_workspace_name(config->strip_workspace_numbers, ws->name); - int width, height; - get_text_size(window->cairo, window->font, &width, &height, false, "%s", name); struct box_colors box_colors; if (ws->urgent) { box_colors = config->colors.urgent_workspace; @@ -213,26 +218,25 @@ static void render_workspace_button(struct window *window, struct config *config 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 + ws_horizontal_padding * 2 - 1, - height + ws_vertical_padding * 2); + 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 + ws_horizontal_padding * 2 - 1, - height + ws_vertical_padding * 2); + 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, false, "%s", name); - - *x += width + ws_horizontal_padding * 2 + ws_spacing; + pango_printf(window->cairo, window->font, false, "%s", stripped_name); - free(name); + *x += width + ws_spacing; } static void render_binding_mode_indicator(struct window *window, struct config *config, double pos) { |