summaryrefslogtreecommitdiffstats
path: root/swaybar
diff options
context:
space:
mode:
authorLibravatar Drew DeVault <sir@cmpwn.com>2016-07-14 18:57:37 -0400
committerLibravatar GitHub <noreply@github.com>2016-07-14 18:57:37 -0400
commit6abbe04e7590f3bfba13351eb87ada70ac3d506e (patch)
treef7c64d6e9d082bd080960c42e21e54edf01839de /swaybar
parentMerge pull request #726 from Hummer12007/hwc (diff)
parentSend command to sway to change workspace when workspace button is clicked (diff)
downloadsway-6abbe04e7590f3bfba13351eb87ada70ac3d506e.tar.gz
sway-6abbe04e7590f3bfba13351eb87ada70ac3d506e.tar.zst
sway-6abbe04e7590f3bfba13351eb87ada70ac3d506e.zip
Merge pull request #743 from deklov/panel-as-shell-03
Set panels/backgrounds' geometries correctly and don't render them ex…
Diffstat (limited to 'swaybar')
-rw-r--r--swaybar/bar.c34
-rw-r--r--swaybar/ipc.c9
-rw-r--r--swaybar/render.c34
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) {
58 return output; 58 return output;
59} 59}
60 60
61static void mouse_button_notify(struct window *window, int x, int y, uint32_t button) {
62 sway_log(L_DEBUG, "Mouse button %d clicked at %d %d\n", button, x, y);
63
64 struct output *clicked_output = NULL;
65 for (int i = 0; i < swaybar.outputs->length; i++) {
66 struct output *output = swaybar.outputs->items[i];
67 if (window == output->window) {
68 clicked_output = output;
69 break;
70 }
71 }
72
73 if (!sway_assert(clicked_output != NULL, "Got pointer event for non-existing output")) {
74 return;
75 }
76
77 double button_x = 0.5;
78 for (int i = 0; i < clicked_output->workspaces->length; i++) {
79 struct workspace *workspace = clicked_output->workspaces->items[i];
80 int button_width, button_height;
81
82 workspace_button_size(window, workspace->name, &button_width, &button_height);
83
84 button_x += button_width;
85 if (x <= button_x) {
86 ipc_send_workspace_command(workspace->name);
87 break;
88 }
89 }
90}
91
61void bar_setup(struct bar *bar, const char *socket_path, const char *bar_id) { 92void bar_setup(struct bar *bar, const char *socket_path, const char *bar_id) {
62 /* initialize bar with default values */ 93 /* initialize bar with default values */
63 bar_init(bar); 94 bar_init(bar);
@@ -92,6 +123,9 @@ void bar_setup(struct bar *bar, const char *socket_path, const char *bar_id) {
92 /* set font */ 123 /* set font */
93 bar_output->window->font = bar->config->font; 124 bar_output->window->font = bar->config->font;
94 125
126 /* set font */
127 bar_output->window->pointer_input.notify = mouse_button_notify;
128
95 /* set window height */ 129 /* set window height */
96 set_window_height(bar_output->window, bar->config->height); 130 set_window_height(bar_output->window, bar->config->height);
97 } 131 }
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 @@
7#include "bar/config.h" 7#include "bar/config.h"
8#include "bar/ipc.h" 8#include "bar/ipc.h"
9 9
10void ipc_send_workspace_command(const char *workspace_name) {
11 uint32_t size = strlen("workspace ") + strlen(workspace_name) + 1;
12
13 char command[size];
14 sprintf(command, "workspace %s", workspace_name);
15
16 ipc_single_command(swaybar.ipc_socketfd, IPC_COMMAND, command, &size);
17}
18
10static void ipc_parse_config(struct config *config, const char *payload) { 19static void ipc_parse_config(struct config *config, const char *payload) {
11 json_object *bar_config = json_tokener_parse(payload); 20 json_object *bar_config = json_tokener_parse(payload);
12 json_object *tray_output, *mode, *hidden_bar, *position, *status_command; 21 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
172 172
173} 173}
174 174
175static char *handle_workspace_number(bool strip_num, const char *ws_name) { 175static 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
199void 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
199static void render_workspace_button(struct window *window, struct config *config, struct workspace *ws, double *x) { 207static 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
238static void render_binding_mode_indicator(struct window *window, struct config *config, double pos) { 242static void render_binding_mode_indicator(struct window *window, struct config *config, double pos) {