sway/swaynag/render.c

309 lines
11 KiB
C
Raw Normal View History

2018-07-26 01:57:19 +00:00
#include <stdint.h>
#include "cairo.h"
#include "log.h"
#include "pango.h"
#include "pool-buffer.h"
#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"
static uint32_t render_message(cairo_t *cairo, struct swaynag *swaynag) {
uint32_t height = swaynag->height * swaynag->scale;
2018-07-30 02:42:03 +00:00
height -= swaynag->type->bar_border_thickness * swaynag->scale;
2018-07-26 01:57:19 +00:00
int text_width, text_height;
get_text_size(cairo, swaynag->type->font, &text_width, &text_height, NULL,
swaynag->scale, true, "%s", swaynag->message);
2018-07-26 01:57:19 +00:00
2018-07-30 02:42:03 +00:00
int padding = swaynag->type->message_padding * swaynag->scale;
2018-07-26 01:57:19 +00:00
uint32_t ideal_height = text_height + padding * 2;
uint32_t ideal_surface_height = ideal_height / swaynag->scale;
if (swaynag->height < ideal_surface_height) {
2018-07-26 01:57:19 +00:00
return ideal_surface_height;
}
cairo_set_source_u32(cairo, swaynag->type->text);
cairo_move_to(cairo, padding, (int)(ideal_height - text_height) / 2);
2018-07-30 05:02:50 +00:00
pango_printf(cairo, swaynag->type->font, swaynag->scale, false,
"%s", swaynag->message);
2018-07-26 01:57:19 +00:00
2018-07-30 05:02:50 +00:00
return ideal_surface_height;
}
static void render_details_scroll_button(cairo_t *cairo,
struct swaynag *swaynag, struct swaynag_button *button) {
int text_width, text_height;
get_text_size(cairo, swaynag->type->font, &text_width, &text_height, NULL,
swaynag->scale, true, "%s", button->text);
2018-07-30 02:42:03 +00:00
int border = swaynag->type->button_border_thickness * swaynag->scale;
int padding = swaynag->type->button_padding * swaynag->scale;
cairo_set_source_u32(cairo, swaynag->type->border);
cairo_rectangle(cairo, button->x, button->y,
button->width, button->height);
cairo_fill(cairo);
cairo_set_source_u32(cairo, swaynag->type->button_background);
cairo_rectangle(cairo, button->x + border, button->y + border,
button->width - (border * 2), button->height - (border * 2));
cairo_fill(cairo);
cairo_set_source_u32(cairo, swaynag->type->text);
cairo_move_to(cairo, button->x + border + padding,
button->y + border + (button->height - text_height) / 2);
2018-07-30 02:42:03 +00:00
pango_printf(cairo, swaynag->type->font, swaynag->scale, true,
"%s", button->text);
}
static int get_detailed_scroll_button_width(cairo_t *cairo,
struct swaynag *swaynag) {
int up_width, down_width, temp_height;
get_text_size(cairo, swaynag->type->font, &up_width, &temp_height, NULL,
2018-07-30 05:02:50 +00:00
swaynag->scale, true,
"%s", swaynag->details.button_up.text);
get_text_size(cairo, swaynag->type->font, &down_width, &temp_height, NULL,
2018-07-30 05:02:50 +00:00
swaynag->scale, true,
"%s", swaynag->details.button_down.text);
int text_width = up_width > down_width ? up_width : down_width;
2018-07-30 02:42:03 +00:00
int border = swaynag->type->button_border_thickness * swaynag->scale;
int padding = swaynag->type->button_padding * swaynag->scale;
return text_width + border * 2 + padding * 2;
}
static uint32_t render_detailed(cairo_t *cairo, struct swaynag *swaynag,
uint32_t y) {
uint32_t width = swaynag->width * swaynag->scale;
uint32_t height = swaynag->height * swaynag->scale;
2018-07-30 02:42:03 +00:00
height -= swaynag->type->bar_border_thickness * swaynag->scale;
2018-07-30 02:42:03 +00:00
int border = swaynag->type->details_border_thickness * swaynag->scale;
int padding = swaynag->type->message_padding * swaynag->scale;
int decor = padding + border;
swaynag->details.x = decor;
2018-07-30 05:02:50 +00:00
swaynag->details.y = y * swaynag->scale + decor;
swaynag->details.width = width - decor * 2;
2018-07-30 02:42:03 +00:00
PangoLayout *layout = get_pango_layout(cairo, swaynag->type->font,
swaynag->details.message, swaynag->scale, false);
pango_layout_set_width(layout,
(swaynag->details.width - padding * 2) * PANGO_SCALE);
pango_layout_set_wrap(layout, PANGO_WRAP_WORD_CHAR);
pango_layout_set_single_paragraph_mode(layout, false);
pango_cairo_update_layout(cairo, layout);
swaynag->details.total_lines = pango_layout_get_line_count(layout);
PangoLayoutLine *line;
line = pango_layout_get_line_readonly(layout, swaynag->details.offset);
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);
bool show_buttons = swaynag->details.offset > 0;
int button_width = get_detailed_scroll_button_width(cairo, swaynag);
if (show_buttons) {
swaynag->details.width -= button_width;
pango_layout_set_width(layout,
(swaynag->details.width - padding * 2) * PANGO_SCALE);
}
uint32_t ideal_height;
do {
ideal_height = swaynag->details.y + text_height + decor + padding * 2;
if (ideal_height > SWAYNAG_MAX_HEIGHT) {
ideal_height = SWAYNAG_MAX_HEIGHT;
if (!show_buttons) {
show_buttons = true;
swaynag->details.width -= button_width;
pango_layout_set_width(layout,
(swaynag->details.width - padding * 2) * PANGO_SCALE);
}
}
swaynag->details.height = ideal_height - swaynag->details.y - decor;
pango_layout_set_height(layout,
(swaynag->details.height - padding * 2) * PANGO_SCALE);
pango_layout_set_ellipsize(layout, PANGO_ELLIPSIZE_END);
pango_cairo_update_layout(cairo, layout);
pango_layout_get_pixel_size(layout, &text_width, &text_height);
} while (text_height != (swaynag->details.height - padding * 2));
swaynag->details.visible_lines = pango_layout_get_line_count(layout);
if (show_buttons) {
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);
}
cairo_set_source_u32(cairo, swaynag->type->border);
cairo_rectangle(cairo, swaynag->details.x, swaynag->details.y,
swaynag->details.width, swaynag->details.height);
cairo_fill(cairo);
cairo_move_to(cairo, swaynag->details.x + padding,
swaynag->details.y + padding);
cairo_set_source_u32(cairo, swaynag->type->text);
pango_cairo_show_layout(cairo, layout);
g_object_unref(layout);
2018-07-30 05:02:50 +00:00
return ideal_height / swaynag->scale;
2018-07-26 01:57:19 +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) {
uint32_t height = swaynag->height * swaynag->scale;
2018-07-30 02:42:03 +00:00
height -= swaynag->type->bar_border_thickness * swaynag->scale;
struct swaynag_button *button = swaynag->buttons->items[button_index];
2018-07-26 01:57:19 +00:00
int text_width, text_height;
get_text_size(cairo, swaynag->type->font, &text_width, &text_height, NULL,
swaynag->scale, true, "%s", button->text);
2018-07-26 01:57:19 +00:00
2018-07-30 02:42:03 +00:00
int border = swaynag->type->button_border_thickness * swaynag->scale;
int padding = swaynag->type->button_padding * swaynag->scale;
2018-07-26 01:57:19 +00:00
uint32_t ideal_height = text_height + padding * 2 + border * 2;
uint32_t ideal_surface_height = ideal_height / swaynag->scale;
if (swaynag->height < ideal_surface_height) {
2018-07-26 01:57:19 +00:00
return ideal_surface_height;
}
button->x = *x - border - text_width - padding * 2;
button->y = (int)(ideal_height - text_height) / 2 - padding;
2018-07-26 01:57:19 +00:00
button->width = text_width + padding * 2;
button->height = text_height + padding * 2;
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);
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);
cairo_set_source_u32(cairo, swaynag->type->text);
2018-07-26 01:57:19 +00:00
cairo_move_to(cairo, button->x + padding, button->y + padding);
2018-07-30 02:42:03 +00:00
pango_printf(cairo, swaynag->type->font, swaynag->scale, true,
"%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
}
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);
cairo_set_source_u32(cairo, swaynag->type->background);
2018-07-26 01:57:19 +00:00
cairo_paint(cairo);
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;
x *= swaynag->scale;
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;
2018-07-30 02:42:03 +00:00
x -= swaynag->type->button_gap * swaynag->scale;
2018-07-26 01:57:19 +00:00
if (i == 0) {
2018-07-30 02:42:03 +00:00
x -= swaynag->type->button_gap_close * swaynag->scale;
2018-07-26 01:57:19 +00:00
}
}
if (swaynag->details.visible) {
h = render_detailed(cairo, swaynag, max_height);
max_height = h > max_height ? h : max_height;
}
2018-07-30 02:42:03 +00:00
int border = swaynag->type->bar_border_thickness * swaynag->scale;
if (max_height > swaynag->height) {
2018-07-26 01:57:19 +00:00
max_height += border;
}
cairo_set_source_u32(cairo, swaynag->type->border_bottom);
2018-07-30 05:02:50 +00:00
cairo_rectangle(cairo, 0,
swaynag->height * swaynag->scale - border,
swaynag->width * swaynag->scale,
border);
2018-07-26 01:57:19 +00:00
cairo_fill(cairo);
return max_height;
2018-07-26 01:57:19 +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);
cairo_save(cairo);
cairo_set_operator(cairo, CAIRO_OPERATOR_CLEAR);
cairo_paint(cairo);
cairo_restore(cairo);
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);
wl_surface_commit(swaynag->surface);
wl_display_roundtrip(swaynag->display);
2018-07-26 01:57:19 +00:00
} else {
swaynag->current_buffer = get_next_buffer(swaynag->shm,
swaynag->buffers,
swaynag->width * swaynag->scale,
swaynag->height * swaynag->scale);
if (!swaynag->current_buffer) {
wlr_log(WLR_DEBUG, "Failed to get buffer. Skipping frame.");
2018-07-27 15:19:42 +00:00
goto cleanup;
}
2018-07-26 01:57:19 +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);
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);
}