diff options
author | Drew DeVault <sir@cmpwn.com> | 2018-03-29 00:49:59 -0400 |
---|---|---|
committer | Drew DeVault <sir@cmpwn.com> | 2018-03-29 22:11:08 -0400 |
commit | 3a458cd7b55bdfad1b04a01064f4fe8fa86ed0de (patch) | |
tree | bb84a32f194d83eb7341d007f6a80d8eab398976 /swaybar | |
parent | Round up workspaces on each output (diff) | |
download | sway-3a458cd7b55bdfad1b04a01064f4fe8fa86ed0de.tar.gz sway-3a458cd7b55bdfad1b04a01064f4fe8fa86ed0de.tar.zst sway-3a458cd7b55bdfad1b04a01064f4fe8fa86ed0de.zip |
Implement workspace button rendering
Diffstat (limited to 'swaybar')
-rw-r--r-- | swaybar/render.c | 92 |
1 files changed, 88 insertions, 4 deletions
diff --git a/swaybar/render.c b/swaybar/render.c index 2eaa0195..226e4ce3 100644 --- a/swaybar/render.c +++ b/swaybar/render.c | |||
@@ -10,8 +10,79 @@ | |||
10 | #include "swaybar/render.h" | 10 | #include "swaybar/render.h" |
11 | #include "wlr-layer-shell-unstable-v1-client-protocol.h" | 11 | #include "wlr-layer-shell-unstable-v1-client-protocol.h" |
12 | 12 | ||
13 | static uint32_t render_to_cairo(cairo_t *cairo, struct swaybar *bar, | 13 | static const char *strip_workspace_number(const char *ws_name) { |
14 | struct swaybar_output *output) { | 14 | size_t len = strlen(ws_name); |
15 | for (size_t i = 0; i < len; ++i) { | ||
16 | if (ws_name[i] < '0' || ws_name[i] > '9') { | ||
17 | if (':' == ws_name[i] && i < len - 1 && i > 0) { | ||
18 | return ws_name + i + 1; | ||
19 | } | ||
20 | return ws_name; | ||
21 | } | ||
22 | } | ||
23 | return ws_name; | ||
24 | } | ||
25 | |||
26 | static uint32_t render_workspace_button(cairo_t *cairo, | ||
27 | struct swaybar_config *config, struct swaybar_workspace *ws, | ||
28 | double *x, uint32_t height) { | ||
29 | static const int ws_horizontal_padding = 5; | ||
30 | static const double ws_vertical_padding = 1.5; | ||
31 | static const int ws_spacing = 1; | ||
32 | |||
33 | const char *name = ws->name; | ||
34 | if (config->strip_workspace_numbers) { | ||
35 | name = strip_workspace_number(ws->name); | ||
36 | } | ||
37 | |||
38 | struct box_colors box_colors; | ||
39 | if (ws->urgent) { | ||
40 | box_colors = config->colors.urgent_workspace; | ||
41 | } else if (ws->focused) { | ||
42 | box_colors = config->colors.focused_workspace; | ||
43 | } else if (ws->visible) { | ||
44 | box_colors = config->colors.active_workspace; | ||
45 | } else { | ||
46 | box_colors = config->colors.inactive_workspace; | ||
47 | } | ||
48 | |||
49 | int text_width, text_height; | ||
50 | get_text_size(cairo, config->font, &text_width, &text_height, | ||
51 | 1, true, "%s", name); | ||
52 | uint32_t ideal_height = ws_vertical_padding * 2 + text_height; | ||
53 | if (height < ideal_height) { | ||
54 | height = ideal_height; | ||
55 | } | ||
56 | uint32_t width = ws_horizontal_padding * 2 + text_width; | ||
57 | |||
58 | cairo_set_source_u32(cairo, box_colors.background); | ||
59 | cairo_rectangle(cairo, *x, 0, width - 1, height); | ||
60 | cairo_fill(cairo); | ||
61 | |||
62 | cairo_set_source_u32(cairo, box_colors.border); | ||
63 | cairo_rectangle(cairo, *x, 0, width - 1, height); | ||
64 | cairo_stroke(cairo); | ||
65 | |||
66 | double text_y = height / 2.0 - text_height / 2.0; | ||
67 | cairo_set_source_u32(cairo, box_colors.text); | ||
68 | cairo_move_to(cairo, (int)*x + ws_horizontal_padding, (int)floor(text_y)); | ||
69 | pango_printf(cairo, config->font, 1, true, "%s", name); | ||
70 | |||
71 | *x += width + ws_spacing; | ||
72 | return ideal_height; | ||
73 | } | ||
74 | |||
75 | static void update_heights(uint32_t height, uint32_t *min, uint32_t *max) { | ||
76 | if (*min < height) { | ||
77 | *min = height; | ||
78 | } | ||
79 | if (height > *max) { | ||
80 | *max = height; | ||
81 | } | ||
82 | } | ||
83 | |||
84 | static uint32_t render_to_cairo(cairo_t *cairo, | ||
85 | struct swaybar *bar, struct swaybar_output *output) { | ||
15 | struct swaybar_config *config = bar->config; | 86 | struct swaybar_config *config = bar->config; |
16 | 87 | ||
17 | cairo_save(cairo); | 88 | cairo_save(cairo); |
@@ -27,8 +98,20 @@ static uint32_t render_to_cairo(cairo_t *cairo, struct swaybar *bar, | |||
27 | } | 98 | } |
28 | cairo_paint(cairo); | 99 | cairo_paint(cairo); |
29 | 100 | ||
30 | // TODO: use actual height | 101 | uint32_t min_height = output->height, max_height = output->height; |
31 | return 20; | 102 | |
103 | double x = 0; | ||
104 | if (config->workspace_buttons) { | ||
105 | struct swaybar_workspace *ws; | ||
106 | wl_list_for_each(ws, &output->workspaces, link) { | ||
107 | uint32_t h = render_workspace_button( | ||
108 | cairo, config, ws, &x, output->height); | ||
109 | update_heights(h, &min_height, &max_height); | ||
110 | } | ||
111 | } | ||
112 | |||
113 | // TODO: Shrink via min_height if sane | ||
114 | return max_height; | ||
32 | } | 115 | } |
33 | 116 | ||
34 | void render_frame(struct swaybar *bar, | 117 | void render_frame(struct swaybar *bar, |
@@ -41,6 +124,7 @@ void render_frame(struct swaybar *bar, | |||
41 | // Reconfigure surface | 124 | // Reconfigure surface |
42 | zwlr_layer_surface_v1_set_size( | 125 | zwlr_layer_surface_v1_set_size( |
43 | output->layer_surface, 0, height); | 126 | output->layer_surface, 0, height); |
127 | zwlr_layer_surface_v1_set_exclusive_zone(output->layer_surface, height); | ||
44 | // TODO: this could infinite loop if the compositor assigns us a | 128 | // TODO: this could infinite loop if the compositor assigns us a |
45 | // different height than what we asked for | 129 | // different height than what we asked for |
46 | wl_surface_commit(output->surface); | 130 | wl_surface_commit(output->surface); |