diff options
author | Caduser2020 <51916507+Caduser2020@users.noreply.github.com> | 2021-09-09 18:06:17 -0500 |
---|---|---|
committer | Simon Ser <contact@emersion.fr> | 2021-09-12 09:05:12 +0200 |
commit | 57ae751655e106748644d1b356edd4aadd0510f4 (patch) | |
tree | 7e5db1f12e920c7f56b7108e2aa9059a8f7cddf4 /swaynag/render.c | |
parent | introduce wlr_drm_lease_v1 (diff) | |
download | sway-57ae751655e106748644d1b356edd4aadd0510f4.tar.gz sway-57ae751655e106748644d1b356edd4aadd0510f4.tar.zst sway-57ae751655e106748644d1b356edd4aadd0510f4.zip |
Simplify swaybar/swaynag scaling code
Use `cairo_scale` to set the scale factor, removing redundant
multiplications by `output->scale`.
Diffstat (limited to 'swaynag/render.c')
-rw-r--r-- | swaynag/render.c | 58 |
1 files changed, 29 insertions, 29 deletions
diff --git a/swaynag/render.c b/swaynag/render.c index 2a7f869a..c159294e 100644 --- a/swaynag/render.c +++ b/swaynag/render.c | |||
@@ -10,19 +10,19 @@ | |||
10 | static uint32_t render_message(cairo_t *cairo, struct swaynag *swaynag) { | 10 | static uint32_t render_message(cairo_t *cairo, struct swaynag *swaynag) { |
11 | int text_width, text_height; | 11 | int text_width, text_height; |
12 | get_text_size(cairo, swaynag->type->font, &text_width, &text_height, NULL, | 12 | get_text_size(cairo, swaynag->type->font, &text_width, &text_height, NULL, |
13 | swaynag->scale, true, "%s", swaynag->message); | 13 | 1, true, "%s", swaynag->message); |
14 | 14 | ||
15 | int padding = swaynag->type->message_padding * swaynag->scale; | 15 | int padding = swaynag->type->message_padding; |
16 | 16 | ||
17 | uint32_t ideal_height = text_height + padding * 2; | 17 | uint32_t ideal_height = text_height + padding * 2; |
18 | uint32_t ideal_surface_height = ideal_height / swaynag->scale; | 18 | uint32_t ideal_surface_height = ideal_height; |
19 | if (swaynag->height < ideal_surface_height) { | 19 | if (swaynag->height < ideal_surface_height) { |
20 | return ideal_surface_height; | 20 | return ideal_surface_height; |
21 | } | 21 | } |
22 | 22 | ||
23 | cairo_set_source_u32(cairo, swaynag->type->text); | 23 | cairo_set_source_u32(cairo, swaynag->type->text); |
24 | cairo_move_to(cairo, padding, (int)(ideal_height - text_height) / 2); | 24 | cairo_move_to(cairo, padding, (int)(ideal_height - text_height) / 2); |
25 | pango_printf(cairo, swaynag->type->font, swaynag->scale, false, | 25 | pango_printf(cairo, swaynag->type->font, 1, false, |
26 | "%s", swaynag->message); | 26 | "%s", swaynag->message); |
27 | 27 | ||
28 | return ideal_surface_height; | 28 | return ideal_surface_height; |
@@ -32,10 +32,10 @@ static void render_details_scroll_button(cairo_t *cairo, | |||
32 | struct swaynag *swaynag, struct swaynag_button *button) { | 32 | struct swaynag *swaynag, struct swaynag_button *button) { |
33 | int text_width, text_height; | 33 | int text_width, text_height; |
34 | get_text_size(cairo, swaynag->type->font, &text_width, &text_height, NULL, | 34 | get_text_size(cairo, swaynag->type->font, &text_width, &text_height, NULL, |
35 | swaynag->scale, true, "%s", button->text); | 35 | 1, true, "%s", button->text); |
36 | 36 | ||
37 | int border = swaynag->type->button_border_thickness * swaynag->scale; | 37 | int border = swaynag->type->button_border_thickness; |
38 | int padding = swaynag->type->button_padding * swaynag->scale; | 38 | int padding = swaynag->type->button_padding; |
39 | 39 | ||
40 | cairo_set_source_u32(cairo, swaynag->type->details_background); | 40 | cairo_set_source_u32(cairo, swaynag->type->details_background); |
41 | cairo_rectangle(cairo, button->x, button->y, | 41 | cairo_rectangle(cairo, button->x, button->y, |
@@ -50,7 +50,7 @@ static void render_details_scroll_button(cairo_t *cairo, | |||
50 | cairo_set_source_u32(cairo, swaynag->type->button_text); | 50 | cairo_set_source_u32(cairo, swaynag->type->button_text); |
51 | cairo_move_to(cairo, button->x + border + padding, | 51 | cairo_move_to(cairo, button->x + border + padding, |
52 | button->y + border + (button->height - text_height) / 2); | 52 | button->y + border + (button->height - text_height) / 2); |
53 | pango_printf(cairo, swaynag->type->font, swaynag->scale, true, | 53 | pango_printf(cairo, swaynag->type->font, 1, true, |
54 | "%s", button->text); | 54 | "%s", button->text); |
55 | } | 55 | } |
56 | 56 | ||
@@ -58,33 +58,33 @@ static int get_detailed_scroll_button_width(cairo_t *cairo, | |||
58 | struct swaynag *swaynag) { | 58 | struct swaynag *swaynag) { |
59 | int up_width, down_width, temp_height; | 59 | int up_width, down_width, temp_height; |
60 | get_text_size(cairo, swaynag->type->font, &up_width, &temp_height, NULL, | 60 | get_text_size(cairo, swaynag->type->font, &up_width, &temp_height, NULL, |
61 | swaynag->scale, true, | 61 | 1, true, |
62 | "%s", swaynag->details.button_up.text); | 62 | "%s", swaynag->details.button_up.text); |
63 | get_text_size(cairo, swaynag->type->font, &down_width, &temp_height, NULL, | 63 | get_text_size(cairo, swaynag->type->font, &down_width, &temp_height, NULL, |
64 | swaynag->scale, true, | 64 | 1, true, |
65 | "%s", swaynag->details.button_down.text); | 65 | "%s", swaynag->details.button_down.text); |
66 | 66 | ||
67 | int text_width = up_width > down_width ? up_width : down_width; | 67 | int text_width = up_width > down_width ? up_width : down_width; |
68 | int border = swaynag->type->button_border_thickness * swaynag->scale; | 68 | int border = swaynag->type->button_border_thickness; |
69 | int padding = swaynag->type->button_padding * swaynag->scale; | 69 | int padding = swaynag->type->button_padding; |
70 | 70 | ||
71 | return text_width + border * 2 + padding * 2; | 71 | return text_width + border * 2 + padding * 2; |
72 | } | 72 | } |
73 | 73 | ||
74 | static uint32_t render_detailed(cairo_t *cairo, struct swaynag *swaynag, | 74 | static uint32_t render_detailed(cairo_t *cairo, struct swaynag *swaynag, |
75 | uint32_t y) { | 75 | uint32_t y) { |
76 | uint32_t width = swaynag->width * swaynag->scale; | 76 | uint32_t width = swaynag->width; |
77 | 77 | ||
78 | int border = swaynag->type->details_border_thickness * swaynag->scale; | 78 | int border = swaynag->type->details_border_thickness; |
79 | int padding = swaynag->type->message_padding * swaynag->scale; | 79 | int padding = swaynag->type->message_padding; |
80 | int decor = padding + border; | 80 | int decor = padding + border; |
81 | 81 | ||
82 | swaynag->details.x = decor; | 82 | swaynag->details.x = decor; |
83 | swaynag->details.y = y * swaynag->scale + decor; | 83 | swaynag->details.y = y + decor; |
84 | swaynag->details.width = width - decor * 2; | 84 | swaynag->details.width = width - decor * 2; |
85 | 85 | ||
86 | PangoLayout *layout = get_pango_layout(cairo, swaynag->type->font, | 86 | PangoLayout *layout = get_pango_layout(cairo, swaynag->type->font, |
87 | swaynag->details.message, swaynag->scale, false); | 87 | swaynag->details.message, 1, false); |
88 | pango_layout_set_width(layout, | 88 | pango_layout_set_width(layout, |
89 | (swaynag->details.width - padding * 2) * PANGO_SCALE); | 89 | (swaynag->details.width - padding * 2) * PANGO_SCALE); |
90 | pango_layout_set_wrap(layout, PANGO_WRAP_WORD_CHAR); | 90 | pango_layout_set_wrap(layout, PANGO_WRAP_WORD_CHAR); |
@@ -164,7 +164,7 @@ static uint32_t render_detailed(cairo_t *cairo, struct swaynag *swaynag, | |||
164 | pango_cairo_show_layout(cairo, layout); | 164 | pango_cairo_show_layout(cairo, layout); |
165 | g_object_unref(layout); | 165 | g_object_unref(layout); |
166 | 166 | ||
167 | return ideal_height / swaynag->scale; | 167 | return ideal_height; |
168 | } | 168 | } |
169 | 169 | ||
170 | static uint32_t render_button(cairo_t *cairo, struct swaynag *swaynag, | 170 | static uint32_t render_button(cairo_t *cairo, struct swaynag *swaynag, |
@@ -173,13 +173,13 @@ static uint32_t render_button(cairo_t *cairo, struct swaynag *swaynag, | |||
173 | 173 | ||
174 | int text_width, text_height; | 174 | int text_width, text_height; |
175 | get_text_size(cairo, swaynag->type->font, &text_width, &text_height, NULL, | 175 | get_text_size(cairo, swaynag->type->font, &text_width, &text_height, NULL, |
176 | swaynag->scale, true, "%s", button->text); | 176 | 1, true, "%s", button->text); |
177 | 177 | ||
178 | int border = swaynag->type->button_border_thickness * swaynag->scale; | 178 | int border = swaynag->type->button_border_thickness; |
179 | int padding = swaynag->type->button_padding * swaynag->scale; | 179 | int padding = swaynag->type->button_padding; |
180 | 180 | ||
181 | uint32_t ideal_height = text_height + padding * 2 + border * 2; | 181 | uint32_t ideal_height = text_height + padding * 2 + border * 2; |
182 | uint32_t ideal_surface_height = ideal_height / swaynag->scale; | 182 | uint32_t ideal_surface_height = ideal_height; |
183 | if (swaynag->height < ideal_surface_height) { | 183 | if (swaynag->height < ideal_surface_height) { |
184 | return ideal_surface_height; | 184 | return ideal_surface_height; |
185 | } | 185 | } |
@@ -201,7 +201,7 @@ static uint32_t render_button(cairo_t *cairo, struct swaynag *swaynag, | |||
201 | 201 | ||
202 | cairo_set_source_u32(cairo, swaynag->type->button_text); | 202 | cairo_set_source_u32(cairo, swaynag->type->button_text); |
203 | cairo_move_to(cairo, button->x + padding, button->y + padding); | 203 | cairo_move_to(cairo, button->x + padding, button->y + padding); |
204 | pango_printf(cairo, swaynag->type->font, swaynag->scale, true, | 204 | pango_printf(cairo, swaynag->type->font, 1, true, |
205 | "%s", button->text); | 205 | "%s", button->text); |
206 | 206 | ||
207 | *x = button->x - border; | 207 | *x = button->x - border; |
@@ -220,13 +220,12 @@ static uint32_t render_to_cairo(cairo_t *cairo, struct swaynag *swaynag) { | |||
220 | max_height = h > max_height ? h : max_height; | 220 | max_height = h > max_height ? h : max_height; |
221 | 221 | ||
222 | int x = swaynag->width - swaynag->type->button_margin_right; | 222 | int x = swaynag->width - swaynag->type->button_margin_right; |
223 | x *= swaynag->scale; | ||
224 | for (int i = 0; i < swaynag->buttons->length; i++) { | 223 | for (int i = 0; i < swaynag->buttons->length; i++) { |
225 | h = render_button(cairo, swaynag, i, &x); | 224 | h = render_button(cairo, swaynag, i, &x); |
226 | max_height = h > max_height ? h : max_height; | 225 | max_height = h > max_height ? h : max_height; |
227 | x -= swaynag->type->button_gap * swaynag->scale; | 226 | x -= swaynag->type->button_gap; |
228 | if (i == 0) { | 227 | if (i == 0) { |
229 | x -= swaynag->type->button_gap_close * swaynag->scale; | 228 | x -= swaynag->type->button_gap_close; |
230 | } | 229 | } |
231 | } | 230 | } |
232 | 231 | ||
@@ -235,14 +234,14 @@ static uint32_t render_to_cairo(cairo_t *cairo, struct swaynag *swaynag) { | |||
235 | max_height = h > max_height ? h : max_height; | 234 | max_height = h > max_height ? h : max_height; |
236 | } | 235 | } |
237 | 236 | ||
238 | int border = swaynag->type->bar_border_thickness * swaynag->scale; | 237 | int border = swaynag->type->bar_border_thickness; |
239 | if (max_height > swaynag->height) { | 238 | if (max_height > swaynag->height) { |
240 | max_height += border; | 239 | max_height += border; |
241 | } | 240 | } |
242 | cairo_set_source_u32(cairo, swaynag->type->border_bottom); | 241 | cairo_set_source_u32(cairo, swaynag->type->border_bottom); |
243 | cairo_rectangle(cairo, 0, | 242 | cairo_rectangle(cairo, 0, |
244 | swaynag->height * swaynag->scale - border, | 243 | swaynag->height - border, |
245 | swaynag->width * swaynag->scale, | 244 | swaynag->width, |
246 | border); | 245 | border); |
247 | cairo_fill(cairo); | 246 | cairo_fill(cairo); |
248 | 247 | ||
@@ -257,6 +256,7 @@ void render_frame(struct swaynag *swaynag) { | |||
257 | cairo_surface_t *recorder = cairo_recording_surface_create( | 256 | cairo_surface_t *recorder = cairo_recording_surface_create( |
258 | CAIRO_CONTENT_COLOR_ALPHA, NULL); | 257 | CAIRO_CONTENT_COLOR_ALPHA, NULL); |
259 | cairo_t *cairo = cairo_create(recorder); | 258 | cairo_t *cairo = cairo_create(recorder); |
259 | cairo_scale(cairo, swaynag->scale, swaynag->scale); | ||
260 | cairo_save(cairo); | 260 | cairo_save(cairo); |
261 | cairo_set_operator(cairo, CAIRO_OPERATOR_CLEAR); | 261 | cairo_set_operator(cairo, CAIRO_OPERATOR_CLEAR); |
262 | cairo_paint(cairo); | 262 | cairo_paint(cairo); |