2018-07-26 01:57:19 +00:00
|
|
|
#include <stdint.h>
|
2021-05-07 20:57:51 +00:00
|
|
|
#include "cairo_util.h"
|
2018-07-26 01:57:19 +00:00
|
|
|
#include "log.h"
|
|
|
|
#include "pango.h"
|
|
|
|
#include "pool-buffer.h"
|
2018-07-29 03:15:12 +00:00
|
|
|
#include "swaynag/swaynag.h"
|
2018-07-28 13:34:25 +00:00
|
|
|
#include "swaynag/types.h"
|
2018-07-26 01:57:19 +00:00
|
|
|
#include "wlr-layer-shell-unstable-v1-client-protocol.h"
|
|
|
|
|
2018-07-29 03:15:12 +00:00
|
|
|
static uint32_t render_message(cairo_t *cairo, struct swaynag *swaynag) {
|
2018-07-26 01:57:19 +00:00
|
|
|
int text_width, text_height;
|
2022-07-01 10:23:04 +00:00
|
|
|
get_text_size(cairo, swaynag->type->font_description, &text_width, &text_height, NULL,
|
2021-09-09 23:06:17 +00:00
|
|
|
1, true, "%s", swaynag->message);
|
2018-07-26 01:57:19 +00:00
|
|
|
|
2021-09-09 23:06:17 +00:00
|
|
|
int padding = swaynag->type->message_padding;
|
2018-07-26 01:57:19 +00:00
|
|
|
|
|
|
|
uint32_t ideal_height = text_height + padding * 2;
|
2021-09-09 23:06:17 +00:00
|
|
|
uint32_t ideal_surface_height = ideal_height;
|
2018-07-29 03:15:12 +00:00
|
|
|
if (swaynag->height < ideal_surface_height) {
|
2018-07-26 01:57:19 +00:00
|
|
|
return ideal_surface_height;
|
|
|
|
}
|
|
|
|
|
2018-07-29 03:15:12 +00:00
|
|
|
cairo_set_source_u32(cairo, swaynag->type->text);
|
2018-07-27 05:30:35 +00:00
|
|
|
cairo_move_to(cairo, padding, (int)(ideal_height - text_height) / 2);
|
2022-07-01 10:23:04 +00:00
|
|
|
render_text(cairo, swaynag->type->font_description, 1, false,
|
2018-07-30 05:02:50 +00:00
|
|
|
"%s", swaynag->message);
|
2018-07-26 01:57:19 +00:00
|
|
|
|
2018-07-30 05:02:50 +00:00
|
|
|
return ideal_surface_height;
|
2018-07-27 05:30:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void render_details_scroll_button(cairo_t *cairo,
|
2018-07-29 03:15:12 +00:00
|
|
|
struct swaynag *swaynag, struct swaynag_button *button) {
|
2018-07-27 05:30:35 +00:00
|
|
|
int text_width, text_height;
|
2022-07-01 10:23:04 +00:00
|
|
|
get_text_size(cairo, swaynag->type->font_description, &text_width, &text_height, NULL,
|
2021-09-09 23:06:17 +00:00
|
|
|
1, true, "%s", button->text);
|
2018-07-27 05:30:35 +00:00
|
|
|
|
2021-09-09 23:06:17 +00:00
|
|
|
int border = swaynag->type->button_border_thickness;
|
|
|
|
int padding = swaynag->type->button_padding;
|
2018-07-27 05:30:35 +00:00
|
|
|
|
2020-09-09 02:43:35 +00:00
|
|
|
cairo_set_source_u32(cairo, swaynag->type->details_background);
|
2018-07-27 05:30:35 +00:00
|
|
|
cairo_rectangle(cairo, button->x, button->y,
|
|
|
|
button->width, button->height);
|
|
|
|
cairo_fill(cairo);
|
|
|
|
|
2018-07-29 03:15:12 +00:00
|
|
|
cairo_set_source_u32(cairo, swaynag->type->button_background);
|
2018-07-27 05:30:35 +00:00
|
|
|
cairo_rectangle(cairo, button->x + border, button->y + border,
|
|
|
|
button->width - (border * 2), button->height - (border * 2));
|
|
|
|
cairo_fill(cairo);
|
|
|
|
|
2020-09-02 13:03:48 +00:00
|
|
|
cairo_set_source_u32(cairo, swaynag->type->button_text);
|
2018-07-27 05:30:35 +00:00
|
|
|
cairo_move_to(cairo, button->x + border + padding,
|
|
|
|
button->y + border + (button->height - text_height) / 2);
|
2022-07-01 10:23:04 +00:00
|
|
|
render_text(cairo, swaynag->type->font_description, 1, true,
|
2018-07-29 03:15:12 +00:00
|
|
|
"%s", button->text);
|
2018-07-27 05:30:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static int get_detailed_scroll_button_width(cairo_t *cairo,
|
2018-07-29 03:15:12 +00:00
|
|
|
struct swaynag *swaynag) {
|
2018-07-27 05:30:35 +00:00
|
|
|
int up_width, down_width, temp_height;
|
2022-07-01 10:23:04 +00:00
|
|
|
get_text_size(cairo, swaynag->type->font_description, &up_width, &temp_height, NULL,
|
2021-09-09 23:06:17 +00:00
|
|
|
1, true,
|
2018-07-30 05:02:50 +00:00
|
|
|
"%s", swaynag->details.button_up.text);
|
2022-07-01 10:23:04 +00:00
|
|
|
get_text_size(cairo, swaynag->type->font_description, &down_width, &temp_height, NULL,
|
2021-09-09 23:06:17 +00:00
|
|
|
1, true,
|
2018-07-30 05:02:50 +00:00
|
|
|
"%s", swaynag->details.button_down.text);
|
2018-07-27 05:30:35 +00:00
|
|
|
|
|
|
|
int text_width = up_width > down_width ? up_width : down_width;
|
2021-09-09 23:06:17 +00:00
|
|
|
int border = swaynag->type->button_border_thickness;
|
|
|
|
int padding = swaynag->type->button_padding;
|
2018-07-27 05:30:35 +00:00
|
|
|
|
|
|
|
return text_width + border * 2 + padding * 2;
|
|
|
|
}
|
|
|
|
|
2018-07-29 03:15:12 +00:00
|
|
|
static uint32_t render_detailed(cairo_t *cairo, struct swaynag *swaynag,
|
2018-07-27 05:30:35 +00:00
|
|
|
uint32_t y) {
|
2021-09-09 23:06:17 +00:00
|
|
|
uint32_t width = swaynag->width;
|
2018-07-27 05:30:35 +00:00
|
|
|
|
2021-09-09 23:06:17 +00:00
|
|
|
int border = swaynag->type->details_border_thickness;
|
|
|
|
int padding = swaynag->type->message_padding;
|
2018-07-27 05:30:35 +00:00
|
|
|
int decor = padding + border;
|
|
|
|
|
2018-07-29 03:15:12 +00:00
|
|
|
swaynag->details.x = decor;
|
2021-09-09 23:06:17 +00:00
|
|
|
swaynag->details.y = y + decor;
|
2018-07-29 03:15:12 +00:00
|
|
|
swaynag->details.width = width - decor * 2;
|
2018-07-27 05:30:35 +00:00
|
|
|
|
2022-07-01 10:23:04 +00:00
|
|
|
PangoLayout *layout = get_pango_layout(cairo, swaynag->type->font_description,
|
2021-09-09 23:06:17 +00:00
|
|
|
swaynag->details.message, 1, false);
|
2018-07-27 05:30:35 +00:00
|
|
|
pango_layout_set_width(layout,
|
2018-07-29 03:15:12 +00:00
|
|
|
(swaynag->details.width - padding * 2) * PANGO_SCALE);
|
2018-07-27 05:30:35 +00: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-29 03:15:12 +00:00
|
|
|
swaynag->details.total_lines = pango_layout_get_line_count(layout);
|
2018-07-27 05:30:35 +00:00
|
|
|
|
|
|
|
PangoLayoutLine *line;
|
2018-07-29 03:15:12 +00:00
|
|
|
line = pango_layout_get_line_readonly(layout, swaynag->details.offset);
|
2018-07-27 05:30:35 +00: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-29 03:15:12 +00:00
|
|
|
bool show_buttons = swaynag->details.offset > 0;
|
|
|
|
int button_width = get_detailed_scroll_button_width(cairo, swaynag);
|
2018-07-27 05:30:35 +00:00
|
|
|
if (show_buttons) {
|
2018-07-29 03:15:12 +00:00
|
|
|
swaynag->details.width -= button_width;
|
2018-07-27 05:30:35 +00:00
|
|
|
pango_layout_set_width(layout,
|
2018-07-29 03:15:12 +00:00
|
|
|
(swaynag->details.width - padding * 2) * PANGO_SCALE);
|
2018-07-27 05:30:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t ideal_height;
|
|
|
|
do {
|
2018-07-29 03:15:12 +00: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 05:30:35 +00:00
|
|
|
|
|
|
|
if (!show_buttons) {
|
|
|
|
show_buttons = true;
|
2018-07-29 03:15:12 +00:00
|
|
|
swaynag->details.width -= button_width;
|
2018-07-27 05:30:35 +00:00
|
|
|
pango_layout_set_width(layout,
|
2018-07-29 03:15:12 +00:00
|
|
|
(swaynag->details.width - padding * 2) * PANGO_SCALE);
|
2018-07-27 05:30:35 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-29 03:15:12 +00:00
|
|
|
swaynag->details.height = ideal_height - swaynag->details.y - decor;
|
2018-07-27 05:30:35 +00:00
|
|
|
pango_layout_set_height(layout,
|
2018-07-29 03:15:12 +00:00
|
|
|
(swaynag->details.height - padding * 2) * PANGO_SCALE);
|
2018-07-27 05:30:35 +00: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-29 03:15:12 +00:00
|
|
|
} while (text_height != (swaynag->details.height - padding * 2));
|
2018-07-27 05:30:35 +00:00
|
|
|
|
2018-07-29 03:15:12 +00:00
|
|
|
swaynag->details.visible_lines = pango_layout_get_line_count(layout);
|
2018-07-27 05:30:35 +00:00
|
|
|
|
|
|
|
if (show_buttons) {
|
2018-07-29 03:15:12 +00: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 05:30:35 +00:00
|
|
|
}
|
|
|
|
|
2020-09-09 02:43:35 +00:00
|
|
|
cairo_set_source_u32(cairo, swaynag->type->details_background);
|
2018-07-29 03:15:12 +00:00
|
|
|
cairo_rectangle(cairo, swaynag->details.x, swaynag->details.y,
|
|
|
|
swaynag->details.width, swaynag->details.height);
|
2018-07-27 05:30:35 +00:00
|
|
|
cairo_fill(cairo);
|
|
|
|
|
2018-07-29 03:15:12 +00:00
|
|
|
cairo_move_to(cairo, swaynag->details.x + padding,
|
|
|
|
swaynag->details.y + padding);
|
|
|
|
cairo_set_source_u32(cairo, swaynag->type->text);
|
2018-07-27 05:30:35 +00:00
|
|
|
pango_cairo_show_layout(cairo, layout);
|
|
|
|
g_object_unref(layout);
|
|
|
|
|
2021-09-09 23:06:17 +00:00
|
|
|
return ideal_height;
|
2018-07-26 01:57:19 +00:00
|
|
|
}
|
|
|
|
|
2018-07-29 03:15:12 +00:00
|
|
|
static uint32_t render_button(cairo_t *cairo, struct swaynag *swaynag,
|
2018-07-26 01:57:19 +00:00
|
|
|
int button_index, int *x) {
|
2018-07-29 03:15:12 +00:00
|
|
|
struct swaynag_button *button = swaynag->buttons->items[button_index];
|
2018-07-26 01:57:19 +00:00
|
|
|
|
|
|
|
int text_width, text_height;
|
2022-07-01 10:23:04 +00:00
|
|
|
get_text_size(cairo, swaynag->type->font_description, &text_width, &text_height, NULL,
|
2021-09-09 23:06:17 +00:00
|
|
|
1, true, "%s", button->text);
|
2018-07-26 01:57:19 +00:00
|
|
|
|
2021-09-09 23:06:17 +00:00
|
|
|
int border = swaynag->type->button_border_thickness;
|
|
|
|
int padding = swaynag->type->button_padding;
|
2018-07-26 01:57:19 +00:00
|
|
|
|
|
|
|
uint32_t ideal_height = text_height + padding * 2 + border * 2;
|
2021-09-09 23:06:17 +00:00
|
|
|
uint32_t ideal_surface_height = ideal_height;
|
2018-07-29 03:15:12 +00:00
|
|
|
if (swaynag->height < ideal_surface_height) {
|
2018-07-26 01:57:19 +00:00
|
|
|
return ideal_surface_height;
|
|
|
|
}
|
|
|
|
|
2019-01-22 08:23:38 +00:00
|
|
|
button->x = *x - border - text_width - padding * 2 + 1;
|
|
|
|
button->y = (int)(ideal_height - text_height) / 2 - padding + 1;
|
2018-07-26 01:57:19 +00:00
|
|
|
button->width = text_width + padding * 2;
|
|
|
|
button->height = text_height + padding * 2;
|
|
|
|
|
2018-07-29 03:15:12 +00:00
|
|
|
cairo_set_source_u32(cairo, swaynag->type->border);
|
2018-07-26 01:57:19 +00:00
|
|
|
cairo_rectangle(cairo, button->x - border, button->y - border,
|
|
|
|
button->width + border * 2, button->height + border * 2);
|
|
|
|
cairo_fill(cairo);
|
|
|
|
|
2018-07-29 03:15:12 +00:00
|
|
|
cairo_set_source_u32(cairo, swaynag->type->button_background);
|
2018-07-26 01:57:19 +00:00
|
|
|
cairo_rectangle(cairo, button->x, button->y,
|
|
|
|
button->width, button->height);
|
|
|
|
cairo_fill(cairo);
|
|
|
|
|
2020-09-02 13:03:48 +00:00
|
|
|
cairo_set_source_u32(cairo, swaynag->type->button_text);
|
2018-07-26 01:57:19 +00:00
|
|
|
cairo_move_to(cairo, button->x + padding, button->y + padding);
|
2022-07-01 10:23:04 +00:00
|
|
|
render_text(cairo, swaynag->type->font_description, 1, true,
|
2018-07-29 03:15:12 +00:00
|
|
|
"%s", button->text);
|
2018-07-26 01:57:19 +00:00
|
|
|
|
|
|
|
*x = button->x - border;
|
|
|
|
|
2018-07-30 05:02:50 +00:00
|
|
|
return ideal_surface_height;
|
2018-07-26 01:57:19 +00:00
|
|
|
}
|
|
|
|
|
2018-07-29 03:15:12 +00:00
|
|
|
static uint32_t render_to_cairo(cairo_t *cairo, struct swaynag *swaynag) {
|
2018-07-26 01:57:19 +00:00
|
|
|
uint32_t max_height = 0;
|
|
|
|
|
|
|
|
cairo_set_operator(cairo, CAIRO_OPERATOR_SOURCE);
|
2018-07-29 03:15:12 +00:00
|
|
|
cairo_set_source_u32(cairo, swaynag->type->background);
|
2018-07-26 01:57:19 +00:00
|
|
|
cairo_paint(cairo);
|
|
|
|
|
2018-07-29 03:15:12 +00:00
|
|
|
uint32_t h = render_message(cairo, swaynag);
|
2018-07-26 01:57:19 +00:00
|
|
|
max_height = h > max_height ? h : max_height;
|
|
|
|
|
2018-07-30 02:42:03 +00:00
|
|
|
int x = swaynag->width - swaynag->type->button_margin_right;
|
2018-07-29 03:15:12 +00:00
|
|
|
for (int i = 0; i < swaynag->buttons->length; i++) {
|
|
|
|
h = render_button(cairo, swaynag, i, &x);
|
2018-07-26 01:57:19 +00:00
|
|
|
max_height = h > max_height ? h : max_height;
|
2021-09-09 23:06:17 +00:00
|
|
|
x -= swaynag->type->button_gap;
|
2018-07-26 01:57:19 +00:00
|
|
|
if (i == 0) {
|
2021-09-09 23:06:17 +00:00
|
|
|
x -= swaynag->type->button_gap_close;
|
2018-07-26 01:57:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-29 03:15:12 +00:00
|
|
|
if (swaynag->details.visible) {
|
|
|
|
h = render_detailed(cairo, swaynag, max_height);
|
2018-07-27 05:30:35 +00:00
|
|
|
max_height = h > max_height ? h : max_height;
|
|
|
|
}
|
|
|
|
|
2021-09-09 23:06:17 +00:00
|
|
|
int border = swaynag->type->bar_border_thickness;
|
2018-07-29 03:15:12 +00:00
|
|
|
if (max_height > swaynag->height) {
|
2018-07-26 01:57:19 +00:00
|
|
|
max_height += border;
|
|
|
|
}
|
2018-07-29 03:15:12 +00:00
|
|
|
cairo_set_source_u32(cairo, swaynag->type->border_bottom);
|
2018-07-30 05:02:50 +00:00
|
|
|
cairo_rectangle(cairo, 0,
|
2021-09-09 23:06:17 +00:00
|
|
|
swaynag->height - border,
|
|
|
|
swaynag->width,
|
2018-07-30 05:02:50 +00:00
|
|
|
border);
|
2018-07-26 01:57:19 +00:00
|
|
|
cairo_fill(cairo);
|
|
|
|
|
2018-07-27 05:30:35 +00:00
|
|
|
return max_height;
|
2018-07-26 01:57:19 +00:00
|
|
|
}
|
|
|
|
|
2018-07-29 03:15:12 +00:00
|
|
|
void render_frame(struct swaynag *swaynag) {
|
|
|
|
if (!swaynag->run_display) {
|
2018-07-26 01:57:19 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
cairo_surface_t *recorder = cairo_recording_surface_create(
|
|
|
|
CAIRO_CONTENT_COLOR_ALPHA, NULL);
|
|
|
|
cairo_t *cairo = cairo_create(recorder);
|
2021-09-09 23:06:17 +00:00
|
|
|
cairo_scale(cairo, swaynag->scale, swaynag->scale);
|
2018-07-26 01:57:19 +00:00
|
|
|
cairo_save(cairo);
|
|
|
|
cairo_set_operator(cairo, CAIRO_OPERATOR_CLEAR);
|
|
|
|
cairo_paint(cairo);
|
|
|
|
cairo_restore(cairo);
|
2018-07-29 03:15:12 +00: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-26 01:57:19 +00:00
|
|
|
height);
|
2018-07-29 03:15:12 +00:00
|
|
|
wl_surface_commit(swaynag->surface);
|
|
|
|
wl_display_roundtrip(swaynag->display);
|
2018-07-26 01:57:19 +00:00
|
|
|
} else {
|
2018-07-29 03:15:12 +00: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 18:51:12 +00:00
|
|
|
sway_log(SWAY_DEBUG, "Failed to get buffer. Skipping frame.");
|
2018-07-27 15:19:42 +00:00
|
|
|
goto cleanup;
|
2018-07-27 05:30:35 +00:00
|
|
|
}
|
2018-07-26 01:57:19 +00:00
|
|
|
|
2018-07-29 03:15:12 +00:00
|
|
|
cairo_t *shm = swaynag->current_buffer->cairo;
|
2018-07-26 01:57:19 +00: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-29 03:15:12 +00: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-26 01:57:19 +00:00
|
|
|
}
|
2018-07-27 15:19:42 +00:00
|
|
|
|
|
|
|
cleanup:
|
2018-07-26 01:57:19 +00:00
|
|
|
cairo_surface_destroy(recorder);
|
|
|
|
cairo_destroy(cairo);
|
|
|
|
}
|