| 
									
										
										
										
											2018-07-25 21:57:19 -04:00
										 |  |  | #include <stdint.h>
 | 
					
						
							|  |  |  | #include "cairo.h"
 | 
					
						
							|  |  |  | #include "log.h"
 | 
					
						
							|  |  |  | #include "pango.h"
 | 
					
						
							|  |  |  | #include "pool-buffer.h"
 | 
					
						
							| 
									
										
										
										
											2018-07-28 23:15:12 -04:00
										 |  |  | #include "swaynag/swaynag.h"
 | 
					
						
							| 
									
										
										
										
											2018-07-28 09:34:25 -04:00
										 |  |  | #include "swaynag/types.h"
 | 
					
						
							| 
									
										
										
										
											2018-07-25 21:57:19 -04:00
										 |  |  | #include "wlr-layer-shell-unstable-v1-client-protocol.h"
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-07-28 23:15:12 -04:00
										 |  |  | static uint32_t render_message(cairo_t *cairo, struct swaynag *swaynag) { | 
					
						
							|  |  |  | 	uint32_t height = swaynag->height * swaynag->scale; | 
					
						
							| 
									
										
										
										
											2018-07-29 22:42:03 -04:00
										 |  |  | 	height -= swaynag->type->bar_border_thickness * swaynag->scale; | 
					
						
							| 
									
										
										
										
											2018-07-25 21:57:19 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	int text_width, text_height; | 
					
						
							| 
									
										
										
										
											2018-09-08 16:19:31 +10:00
										 |  |  | 	get_text_size(cairo, swaynag->type->font, &text_width, &text_height, NULL, | 
					
						
							| 
									
										
										
										
											2018-07-28 23:15:12 -04:00
										 |  |  | 			swaynag->scale, true, "%s", swaynag->message); | 
					
						
							| 
									
										
										
										
											2018-07-25 21:57:19 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-07-29 22:42:03 -04:00
										 |  |  | 	int padding = swaynag->type->message_padding * swaynag->scale; | 
					
						
							| 
									
										
										
										
											2018-07-25 21:57:19 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	uint32_t ideal_height = text_height + padding * 2; | 
					
						
							| 
									
										
										
										
											2018-07-28 23:15:12 -04:00
										 |  |  | 	uint32_t ideal_surface_height = ideal_height / swaynag->scale; | 
					
						
							|  |  |  | 	if (swaynag->height < ideal_surface_height) { | 
					
						
							| 
									
										
										
										
											2018-07-25 21:57:19 -04:00
										 |  |  | 		return ideal_surface_height; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-07-28 23:15:12 -04:00
										 |  |  | 	cairo_set_source_u32(cairo, swaynag->type->text); | 
					
						
							| 
									
										
										
										
											2018-07-27 01:30:35 -04:00
										 |  |  | 	cairo_move_to(cairo, padding, (int)(ideal_height - text_height) / 2); | 
					
						
							| 
									
										
										
										
											2018-07-30 01:02:50 -04:00
										 |  |  | 	pango_printf(cairo, swaynag->type->font, swaynag->scale, false, | 
					
						
							|  |  |  | 			"%s", swaynag->message); | 
					
						
							| 
									
										
										
										
											2018-07-25 21:57:19 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-07-30 01:02:50 -04:00
										 |  |  | 	return ideal_surface_height; | 
					
						
							| 
									
										
										
										
											2018-07-27 01:30:35 -04:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static void render_details_scroll_button(cairo_t *cairo, | 
					
						
							| 
									
										
										
										
											2018-07-28 23:15:12 -04:00
										 |  |  | 		struct swaynag *swaynag, struct swaynag_button *button) { | 
					
						
							| 
									
										
										
										
											2018-07-27 01:30:35 -04:00
										 |  |  | 	int text_width, text_height; | 
					
						
							| 
									
										
										
										
											2018-09-08 16:19:31 +10:00
										 |  |  | 	get_text_size(cairo, swaynag->type->font, &text_width, &text_height, NULL, | 
					
						
							| 
									
										
										
										
											2018-07-28 23:15:12 -04:00
										 |  |  | 			swaynag->scale, true, "%s", button->text); | 
					
						
							| 
									
										
										
										
											2018-07-27 01:30:35 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-07-29 22:42:03 -04:00
										 |  |  | 	int border = swaynag->type->button_border_thickness * swaynag->scale; | 
					
						
							|  |  |  | 	int padding = swaynag->type->button_padding * swaynag->scale; | 
					
						
							| 
									
										
										
										
											2018-07-27 01:30:35 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-07-28 23:15:12 -04:00
										 |  |  | 	cairo_set_source_u32(cairo, swaynag->type->border); | 
					
						
							| 
									
										
										
										
											2018-07-27 01:30:35 -04:00
										 |  |  | 	cairo_rectangle(cairo, button->x, button->y, | 
					
						
							|  |  |  | 			button->width, button->height); | 
					
						
							|  |  |  | 	cairo_fill(cairo); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-07-28 23:15:12 -04:00
										 |  |  | 	cairo_set_source_u32(cairo, swaynag->type->button_background); | 
					
						
							| 
									
										
										
										
											2018-07-27 01:30:35 -04:00
										 |  |  | 	cairo_rectangle(cairo, button->x + border, button->y + border, | 
					
						
							|  |  |  | 			button->width - (border * 2), button->height - (border * 2)); | 
					
						
							|  |  |  | 	cairo_fill(cairo); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-07-28 23:15:12 -04:00
										 |  |  | 	cairo_set_source_u32(cairo, swaynag->type->text); | 
					
						
							| 
									
										
										
										
											2018-07-27 01:30:35 -04:00
										 |  |  | 	cairo_move_to(cairo, button->x + border + padding, | 
					
						
							|  |  |  | 			button->y + border + (button->height - text_height) / 2); | 
					
						
							| 
									
										
										
										
											2018-07-29 22:42:03 -04:00
										 |  |  | 	pango_printf(cairo, swaynag->type->font, swaynag->scale, true, | 
					
						
							| 
									
										
										
										
											2018-07-28 23:15:12 -04:00
										 |  |  | 			"%s", button->text); | 
					
						
							| 
									
										
										
										
											2018-07-27 01:30:35 -04:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static int get_detailed_scroll_button_width(cairo_t *cairo, | 
					
						
							| 
									
										
										
										
											2018-07-28 23:15:12 -04:00
										 |  |  | 		struct swaynag *swaynag) { | 
					
						
							| 
									
										
										
										
											2018-07-27 01:30:35 -04:00
										 |  |  | 	int up_width, down_width, temp_height; | 
					
						
							| 
									
										
										
										
											2018-09-08 16:19:31 +10:00
										 |  |  | 	get_text_size(cairo, swaynag->type->font, &up_width, &temp_height, NULL, | 
					
						
							| 
									
										
										
										
											2018-07-30 01:02:50 -04:00
										 |  |  | 			swaynag->scale, true, | 
					
						
							|  |  |  | 			"%s", swaynag->details.button_up.text); | 
					
						
							| 
									
										
										
										
											2018-09-08 16:19:31 +10:00
										 |  |  | 	get_text_size(cairo, swaynag->type->font, &down_width, &temp_height, NULL, | 
					
						
							| 
									
										
										
										
											2018-07-30 01:02:50 -04:00
										 |  |  | 			swaynag->scale, true, | 
					
						
							|  |  |  | 			"%s", swaynag->details.button_down.text); | 
					
						
							| 
									
										
										
										
											2018-07-27 01:30:35 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	int text_width =  up_width > down_width ? up_width : down_width; | 
					
						
							| 
									
										
										
										
											2018-07-29 22:42:03 -04:00
										 |  |  | 	int border = swaynag->type->button_border_thickness * swaynag->scale; | 
					
						
							|  |  |  | 	int padding = swaynag->type->button_padding * swaynag->scale; | 
					
						
							| 
									
										
										
										
											2018-07-27 01:30:35 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	return text_width + border * 2 + padding * 2; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-07-28 23:15:12 -04:00
										 |  |  | static uint32_t render_detailed(cairo_t *cairo, struct swaynag *swaynag, | 
					
						
							| 
									
										
										
										
											2018-07-27 01:30:35 -04:00
										 |  |  | 		uint32_t y) { | 
					
						
							| 
									
										
										
										
											2018-07-28 23:15:12 -04:00
										 |  |  | 	uint32_t width = swaynag->width * swaynag->scale; | 
					
						
							|  |  |  | 	uint32_t height = swaynag->height * swaynag->scale; | 
					
						
							| 
									
										
										
										
											2018-07-29 22:42:03 -04:00
										 |  |  | 	height -= swaynag->type->bar_border_thickness * swaynag->scale; | 
					
						
							| 
									
										
										
										
											2018-07-27 01:30:35 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-07-29 22:42:03 -04:00
										 |  |  | 	int border = swaynag->type->details_border_thickness * swaynag->scale; | 
					
						
							|  |  |  | 	int padding = swaynag->type->message_padding * swaynag->scale; | 
					
						
							| 
									
										
										
										
											2018-07-27 01:30:35 -04:00
										 |  |  | 	int decor = padding + border; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-07-28 23:15:12 -04:00
										 |  |  | 	swaynag->details.x = decor; | 
					
						
							| 
									
										
										
										
											2018-07-30 01:02:50 -04:00
										 |  |  | 	swaynag->details.y = y * swaynag->scale + decor; | 
					
						
							| 
									
										
										
										
											2018-07-28 23:15:12 -04:00
										 |  |  | 	swaynag->details.width = width - decor * 2; | 
					
						
							| 
									
										
										
										
											2018-07-27 01:30:35 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-07-29 22:42:03 -04:00
										 |  |  | 	PangoLayout *layout = get_pango_layout(cairo, swaynag->type->font, | 
					
						
							| 
									
										
										
										
											2018-07-28 23:15:12 -04:00
										 |  |  | 			swaynag->details.message, swaynag->scale, false); | 
					
						
							| 
									
										
										
										
											2018-07-27 01:30:35 -04:00
										 |  |  | 	pango_layout_set_width(layout, | 
					
						
							| 
									
										
										
										
											2018-07-28 23:15:12 -04:00
										 |  |  | 			(swaynag->details.width - padding * 2) * PANGO_SCALE); | 
					
						
							| 
									
										
										
										
											2018-07-27 01:30:35 -04:00
										 |  |  | 	pango_layout_set_wrap(layout, PANGO_WRAP_WORD_CHAR); | 
					
						
							|  |  |  | 	pango_layout_set_single_paragraph_mode(layout, false); | 
					
						
							|  |  |  | 	pango_cairo_update_layout(cairo, layout); | 
					
						
							| 
									
										
										
										
											2018-07-28 23:15:12 -04:00
										 |  |  | 	swaynag->details.total_lines = pango_layout_get_line_count(layout); | 
					
						
							| 
									
										
										
										
											2018-07-27 01:30:35 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	PangoLayoutLine *line; | 
					
						
							| 
									
										
										
										
											2018-07-28 23:15:12 -04:00
										 |  |  | 	line = pango_layout_get_line_readonly(layout, swaynag->details.offset); | 
					
						
							| 
									
										
										
										
											2018-07-27 01:30:35 -04:00
										 |  |  | 	gint offset = line->start_index; | 
					
						
							|  |  |  | 	const char *text = pango_layout_get_text(layout); | 
					
						
							|  |  |  | 	pango_layout_set_text(layout, text + offset, strlen(text) - offset); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	int text_width, text_height; | 
					
						
							|  |  |  | 	pango_cairo_update_layout(cairo, layout); | 
					
						
							|  |  |  | 	pango_layout_get_pixel_size(layout, &text_width, &text_height); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-07-28 23:15:12 -04:00
										 |  |  | 	bool show_buttons = swaynag->details.offset > 0; | 
					
						
							|  |  |  | 	int button_width = get_detailed_scroll_button_width(cairo, swaynag); | 
					
						
							| 
									
										
										
										
											2018-07-27 01:30:35 -04:00
										 |  |  | 	if (show_buttons) { | 
					
						
							| 
									
										
										
										
											2018-07-28 23:15:12 -04:00
										 |  |  | 		swaynag->details.width -= button_width; | 
					
						
							| 
									
										
										
										
											2018-07-27 01:30:35 -04:00
										 |  |  | 		pango_layout_set_width(layout, | 
					
						
							| 
									
										
										
										
											2018-07-28 23:15:12 -04:00
										 |  |  | 				(swaynag->details.width - padding * 2) * PANGO_SCALE); | 
					
						
							| 
									
										
										
										
											2018-07-27 01:30:35 -04:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	uint32_t ideal_height; | 
					
						
							|  |  |  | 	do { | 
					
						
							| 
									
										
										
										
											2018-07-28 23:15:12 -04:00
										 |  |  | 		ideal_height = swaynag->details.y + text_height + decor + padding * 2; | 
					
						
							|  |  |  | 		if (ideal_height > SWAYNAG_MAX_HEIGHT) { | 
					
						
							|  |  |  | 			ideal_height = SWAYNAG_MAX_HEIGHT; | 
					
						
							| 
									
										
										
										
											2018-07-27 01:30:35 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | 			if (!show_buttons) { | 
					
						
							|  |  |  | 				show_buttons = true; | 
					
						
							| 
									
										
										
										
											2018-07-28 23:15:12 -04:00
										 |  |  | 				swaynag->details.width -= button_width; | 
					
						
							| 
									
										
										
										
											2018-07-27 01:30:35 -04:00
										 |  |  | 				pango_layout_set_width(layout, | 
					
						
							| 
									
										
										
										
											2018-07-28 23:15:12 -04:00
										 |  |  | 						(swaynag->details.width - padding * 2) * PANGO_SCALE); | 
					
						
							| 
									
										
										
										
											2018-07-27 01:30:35 -04:00
										 |  |  | 			} | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-07-28 23:15:12 -04:00
										 |  |  | 		swaynag->details.height = ideal_height - swaynag->details.y - decor; | 
					
						
							| 
									
										
										
										
											2018-07-27 01:30:35 -04:00
										 |  |  | 		pango_layout_set_height(layout, | 
					
						
							| 
									
										
										
										
											2018-07-28 23:15:12 -04:00
										 |  |  | 				(swaynag->details.height - padding * 2) * PANGO_SCALE); | 
					
						
							| 
									
										
										
										
											2018-07-27 01:30:35 -04:00
										 |  |  | 		pango_layout_set_ellipsize(layout, PANGO_ELLIPSIZE_END); | 
					
						
							|  |  |  | 		pango_cairo_update_layout(cairo, layout); | 
					
						
							|  |  |  | 		pango_layout_get_pixel_size(layout, &text_width, &text_height); | 
					
						
							| 
									
										
										
										
											2018-07-28 23:15:12 -04:00
										 |  |  | 	} while (text_height != (swaynag->details.height - padding * 2)); | 
					
						
							| 
									
										
										
										
											2018-07-27 01:30:35 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-07-28 23:15:12 -04:00
										 |  |  | 	swaynag->details.visible_lines = pango_layout_get_line_count(layout); | 
					
						
							| 
									
										
										
										
											2018-07-27 01:30:35 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	if (show_buttons) { | 
					
						
							| 
									
										
										
										
											2018-07-28 23:15:12 -04:00
										 |  |  | 		swaynag->details.button_up.x = | 
					
						
							|  |  |  | 			swaynag->details.x + swaynag->details.width; | 
					
						
							|  |  |  | 		swaynag->details.button_up.y = swaynag->details.y; | 
					
						
							|  |  |  | 		swaynag->details.button_up.width = button_width; | 
					
						
							|  |  |  | 		swaynag->details.button_up.height = swaynag->details.height / 2; | 
					
						
							|  |  |  | 		render_details_scroll_button(cairo, swaynag, | 
					
						
							|  |  |  | 				&swaynag->details.button_up); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		swaynag->details.button_down.x = | 
					
						
							|  |  |  | 			swaynag->details.x + swaynag->details.width; | 
					
						
							|  |  |  | 		swaynag->details.button_down.y = | 
					
						
							|  |  |  | 			swaynag->details.button_up.y + swaynag->details.button_up.height; | 
					
						
							|  |  |  | 		swaynag->details.button_down.width = button_width; | 
					
						
							|  |  |  | 		swaynag->details.button_down.height = swaynag->details.height / 2; | 
					
						
							|  |  |  | 		render_details_scroll_button(cairo, swaynag, | 
					
						
							|  |  |  | 				&swaynag->details.button_down); | 
					
						
							| 
									
										
										
										
											2018-07-27 01:30:35 -04:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-07-28 23:15:12 -04:00
										 |  |  | 	cairo_set_source_u32(cairo, swaynag->type->border); | 
					
						
							|  |  |  | 	cairo_rectangle(cairo, swaynag->details.x, swaynag->details.y, | 
					
						
							|  |  |  | 			swaynag->details.width, swaynag->details.height); | 
					
						
							| 
									
										
										
										
											2018-07-27 01:30:35 -04:00
										 |  |  | 	cairo_fill(cairo); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-07-28 23:15:12 -04:00
										 |  |  | 	cairo_move_to(cairo, swaynag->details.x + padding, | 
					
						
							|  |  |  | 			swaynag->details.y + padding); | 
					
						
							|  |  |  | 	cairo_set_source_u32(cairo, swaynag->type->text); | 
					
						
							| 
									
										
										
										
											2018-07-27 01:30:35 -04:00
										 |  |  | 	pango_cairo_show_layout(cairo, layout); | 
					
						
							|  |  |  | 	g_object_unref(layout); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-07-30 01:02:50 -04:00
										 |  |  | 	return ideal_height / swaynag->scale; | 
					
						
							| 
									
										
										
										
											2018-07-25 21:57:19 -04:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-07-28 23:15:12 -04:00
										 |  |  | static uint32_t render_button(cairo_t *cairo, struct swaynag *swaynag, | 
					
						
							| 
									
										
										
										
											2018-07-25 21:57:19 -04:00
										 |  |  | 		int button_index, int *x) { | 
					
						
							| 
									
										
										
										
											2018-07-28 23:15:12 -04:00
										 |  |  | 	uint32_t height = swaynag->height * swaynag->scale; | 
					
						
							| 
									
										
										
										
											2018-07-29 22:42:03 -04:00
										 |  |  | 	height -= swaynag->type->bar_border_thickness * swaynag->scale; | 
					
						
							| 
									
										
										
										
											2018-07-28 23:15:12 -04:00
										 |  |  | 	struct swaynag_button *button = swaynag->buttons->items[button_index]; | 
					
						
							| 
									
										
										
										
											2018-07-25 21:57:19 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	int text_width, text_height; | 
					
						
							| 
									
										
										
										
											2018-09-08 16:19:31 +10:00
										 |  |  | 	get_text_size(cairo, swaynag->type->font, &text_width, &text_height, NULL, | 
					
						
							| 
									
										
										
										
											2018-07-28 23:15:12 -04:00
										 |  |  | 			swaynag->scale, true, "%s", button->text); | 
					
						
							| 
									
										
										
										
											2018-07-25 21:57:19 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-07-29 22:42:03 -04:00
										 |  |  | 	int border = swaynag->type->button_border_thickness * swaynag->scale; | 
					
						
							|  |  |  | 	int padding = swaynag->type->button_padding * swaynag->scale; | 
					
						
							| 
									
										
										
										
											2018-07-25 21:57:19 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	uint32_t ideal_height = text_height + padding * 2 + border * 2; | 
					
						
							| 
									
										
										
										
											2018-07-28 23:15:12 -04:00
										 |  |  | 	uint32_t ideal_surface_height = ideal_height / swaynag->scale; | 
					
						
							|  |  |  | 	if (swaynag->height < ideal_surface_height) { | 
					
						
							| 
									
										
										
										
											2018-07-25 21:57:19 -04:00
										 |  |  | 		return ideal_surface_height; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	button->x = *x - border - text_width - padding * 2; | 
					
						
							| 
									
										
										
										
											2018-07-27 01:30:35 -04:00
										 |  |  | 	button->y = (int)(ideal_height - text_height) / 2 - padding; | 
					
						
							| 
									
										
										
										
											2018-07-25 21:57:19 -04:00
										 |  |  | 	button->width = text_width + padding * 2; | 
					
						
							|  |  |  | 	button->height = text_height + padding * 2; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-07-28 23:15:12 -04:00
										 |  |  | 	cairo_set_source_u32(cairo, swaynag->type->border); | 
					
						
							| 
									
										
										
										
											2018-07-25 21:57:19 -04:00
										 |  |  | 	cairo_rectangle(cairo, button->x - border, button->y - border, | 
					
						
							|  |  |  | 			button->width + border * 2, button->height + border * 2); | 
					
						
							|  |  |  | 	cairo_fill(cairo); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-07-28 23:15:12 -04:00
										 |  |  | 	cairo_set_source_u32(cairo, swaynag->type->button_background); | 
					
						
							| 
									
										
										
										
											2018-07-25 21:57:19 -04:00
										 |  |  | 	cairo_rectangle(cairo, button->x, button->y, | 
					
						
							|  |  |  | 			button->width, button->height); | 
					
						
							|  |  |  | 	cairo_fill(cairo); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-07-28 23:15:12 -04:00
										 |  |  | 	cairo_set_source_u32(cairo, swaynag->type->text); | 
					
						
							| 
									
										
										
										
											2018-07-25 21:57:19 -04:00
										 |  |  | 	cairo_move_to(cairo, button->x + padding, button->y + padding); | 
					
						
							| 
									
										
										
										
											2018-07-29 22:42:03 -04:00
										 |  |  | 	pango_printf(cairo, swaynag->type->font, swaynag->scale, true, | 
					
						
							| 
									
										
										
										
											2018-07-28 23:15:12 -04:00
										 |  |  | 			"%s", button->text); | 
					
						
							| 
									
										
										
										
											2018-07-25 21:57:19 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	*x = button->x - border; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-07-30 01:02:50 -04:00
										 |  |  | 	return ideal_surface_height; | 
					
						
							| 
									
										
										
										
											2018-07-25 21:57:19 -04:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-07-28 23:15:12 -04:00
										 |  |  | static uint32_t render_to_cairo(cairo_t *cairo, struct swaynag *swaynag) { | 
					
						
							| 
									
										
										
										
											2018-07-25 21:57:19 -04:00
										 |  |  | 	uint32_t max_height = 0; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	cairo_set_operator(cairo, CAIRO_OPERATOR_SOURCE); | 
					
						
							| 
									
										
										
										
											2018-07-28 23:15:12 -04:00
										 |  |  | 	cairo_set_source_u32(cairo, swaynag->type->background); | 
					
						
							| 
									
										
										
										
											2018-07-25 21:57:19 -04:00
										 |  |  | 	cairo_paint(cairo); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-07-28 23:15:12 -04:00
										 |  |  | 	uint32_t h = render_message(cairo, swaynag); | 
					
						
							| 
									
										
										
										
											2018-07-25 21:57:19 -04:00
										 |  |  | 	max_height = h > max_height ? h : max_height; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-07-29 22:42:03 -04:00
										 |  |  | 	int x = swaynag->width - swaynag->type->button_margin_right; | 
					
						
							|  |  |  | 	x *= swaynag->scale; | 
					
						
							| 
									
										
										
										
											2018-07-28 23:15:12 -04:00
										 |  |  | 	for (int i = 0; i < swaynag->buttons->length; i++) { | 
					
						
							|  |  |  | 		h = render_button(cairo, swaynag, i, &x); | 
					
						
							| 
									
										
										
										
											2018-07-25 21:57:19 -04:00
										 |  |  | 		max_height = h > max_height ? h : max_height; | 
					
						
							| 
									
										
										
										
											2018-07-29 22:42:03 -04:00
										 |  |  | 		x -= swaynag->type->button_gap * swaynag->scale; | 
					
						
							| 
									
										
										
										
											2018-07-25 21:57:19 -04:00
										 |  |  | 		if (i == 0) { | 
					
						
							| 
									
										
										
										
											2018-07-29 22:42:03 -04:00
										 |  |  | 			x -= swaynag->type->button_gap_close * swaynag->scale; | 
					
						
							| 
									
										
										
										
											2018-07-25 21:57:19 -04:00
										 |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-07-28 23:15:12 -04:00
										 |  |  | 	if (swaynag->details.visible) { | 
					
						
							|  |  |  | 		h = render_detailed(cairo, swaynag, max_height); | 
					
						
							| 
									
										
										
										
											2018-07-27 01:30:35 -04:00
										 |  |  | 		max_height = h > max_height ? h : max_height; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-07-29 22:42:03 -04:00
										 |  |  | 	int border = swaynag->type->bar_border_thickness * swaynag->scale; | 
					
						
							| 
									
										
										
										
											2018-07-28 23:15:12 -04:00
										 |  |  | 	if (max_height > swaynag->height) { | 
					
						
							| 
									
										
										
										
											2018-07-25 21:57:19 -04:00
										 |  |  | 		max_height += border; | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2018-07-28 23:15:12 -04:00
										 |  |  | 	cairo_set_source_u32(cairo, swaynag->type->border_bottom); | 
					
						
							| 
									
										
										
										
											2018-07-30 01:02:50 -04:00
										 |  |  | 	cairo_rectangle(cairo, 0, | 
					
						
							|  |  |  | 			swaynag->height * swaynag->scale - border, | 
					
						
							|  |  |  | 			swaynag->width * swaynag->scale, | 
					
						
							|  |  |  | 			border); | 
					
						
							| 
									
										
										
										
											2018-07-25 21:57:19 -04:00
										 |  |  | 	cairo_fill(cairo); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-07-27 01:30:35 -04:00
										 |  |  | 	return max_height; | 
					
						
							| 
									
										
										
										
											2018-07-25 21:57:19 -04:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-07-28 23:15:12 -04:00
										 |  |  | void render_frame(struct swaynag *swaynag) { | 
					
						
							|  |  |  | 	if (!swaynag->run_display) { | 
					
						
							| 
									
										
										
										
											2018-07-25 21:57:19 -04:00
										 |  |  | 		return; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	cairo_surface_t *recorder = cairo_recording_surface_create( | 
					
						
							|  |  |  | 			CAIRO_CONTENT_COLOR_ALPHA, NULL); | 
					
						
							|  |  |  | 	cairo_t *cairo = cairo_create(recorder); | 
					
						
							|  |  |  | 	cairo_save(cairo); | 
					
						
							|  |  |  | 	cairo_set_operator(cairo, CAIRO_OPERATOR_CLEAR); | 
					
						
							|  |  |  | 	cairo_paint(cairo); | 
					
						
							|  |  |  | 	cairo_restore(cairo); | 
					
						
							| 
									
										
										
										
											2018-07-28 23:15:12 -04:00
										 |  |  | 	uint32_t height = render_to_cairo(cairo, swaynag); | 
					
						
							|  |  |  | 	if (height != swaynag->height) { | 
					
						
							|  |  |  | 		zwlr_layer_surface_v1_set_size(swaynag->layer_surface, 0, height); | 
					
						
							|  |  |  | 		zwlr_layer_surface_v1_set_exclusive_zone(swaynag->layer_surface, | 
					
						
							| 
									
										
										
										
											2018-07-25 21:57:19 -04:00
										 |  |  | 				height); | 
					
						
							| 
									
										
										
										
											2018-07-28 23:15:12 -04:00
										 |  |  | 		wl_surface_commit(swaynag->surface); | 
					
						
							|  |  |  | 		wl_display_roundtrip(swaynag->display); | 
					
						
							| 
									
										
										
										
											2018-07-25 21:57:19 -04:00
										 |  |  | 	} else { | 
					
						
							| 
									
										
										
										
											2018-07-28 23:15:12 -04:00
										 |  |  | 		swaynag->current_buffer = get_next_buffer(swaynag->shm, | 
					
						
							|  |  |  | 				swaynag->buffers, | 
					
						
							|  |  |  | 				swaynag->width * swaynag->scale, | 
					
						
							|  |  |  | 				swaynag->height * swaynag->scale); | 
					
						
							|  |  |  | 		if (!swaynag->current_buffer) { | 
					
						
							| 
									
										
										
										
											2019-01-20 13:51:12 -05:00
										 |  |  | 			sway_log(SWAY_DEBUG, "Failed to get buffer. Skipping frame."); | 
					
						
							| 
									
										
										
										
											2018-07-27 11:19:42 -04:00
										 |  |  | 			goto cleanup; | 
					
						
							| 
									
										
										
										
											2018-07-27 01:30:35 -04:00
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											2018-07-25 21:57:19 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-07-28 23:15:12 -04:00
										 |  |  | 		cairo_t *shm = swaynag->current_buffer->cairo; | 
					
						
							| 
									
										
										
										
											2018-07-25 21:57:19 -04:00
										 |  |  | 		cairo_save(shm); | 
					
						
							|  |  |  | 		cairo_set_operator(shm, CAIRO_OPERATOR_CLEAR); | 
					
						
							|  |  |  | 		cairo_paint(shm); | 
					
						
							|  |  |  | 		cairo_restore(shm); | 
					
						
							|  |  |  | 		cairo_set_source_surface(shm, recorder, 0.0, 0.0); | 
					
						
							|  |  |  | 		cairo_paint(shm); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-07-28 23:15:12 -04:00
										 |  |  | 		wl_surface_set_buffer_scale(swaynag->surface, swaynag->scale); | 
					
						
							|  |  |  | 		wl_surface_attach(swaynag->surface, | 
					
						
							|  |  |  | 				swaynag->current_buffer->buffer, 0, 0); | 
					
						
							|  |  |  | 		wl_surface_damage(swaynag->surface, 0, 0, | 
					
						
							|  |  |  | 				swaynag->width, swaynag->height); | 
					
						
							|  |  |  | 		wl_surface_commit(swaynag->surface); | 
					
						
							|  |  |  | 		wl_display_roundtrip(swaynag->display); | 
					
						
							| 
									
										
										
										
											2018-07-25 21:57:19 -04:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2018-07-27 11:19:42 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | cleanup: | 
					
						
							| 
									
										
										
										
											2018-07-25 21:57:19 -04:00
										 |  |  | 	cairo_surface_destroy(recorder); | 
					
						
							|  |  |  | 	cairo_destroy(cairo); | 
					
						
							|  |  |  | } |