From b667298a0a1efead7949715a31ec86fe3b8b1cda Mon Sep 17 00:00:00 2001 From: Ryan Dwyer Date: Wed, 2 May 2018 23:07:52 +1000 Subject: Render titles --- sway/tree/arrange.c | 1 + sway/tree/container.c | 65 +++++++++++++++++++++++++++++++++++++++++++++++++++ sway/tree/view.c | 7 +++--- 3 files changed, 70 insertions(+), 3 deletions(-) (limited to 'sway/tree') diff --git a/sway/tree/arrange.c b/sway/tree/arrange.c index 83bb20fb..1299beb6 100644 --- a/sway/tree/arrange.c +++ b/sway/tree/arrange.c @@ -203,6 +203,7 @@ void arrange_children_of(struct sway_container *parent) { } else { arrange_children_of(child); } + container_update_title_textures(child); } container_damage_whole(parent); update_debug_tree(); diff --git a/sway/tree/container.c b/sway/tree/container.c index 6ac59547..b33985af 100644 --- a/sway/tree/container.c +++ b/sway/tree/container.c @@ -7,6 +7,8 @@ #include #include #include +#include "cairo.h" +#include "pango.h" #include "sway/config.h" #include "sway/input/input-manager.h" #include "sway/input/seat.h" @@ -120,6 +122,13 @@ static void _container_destroy(struct sway_container *cont) { if (cont->name) { free(cont->name); } + if (cont->title_focused) { + // If one is set then all of these are set + wlr_texture_destroy(cont->title_focused); + wlr_texture_destroy(cont->title_focused_inactive); + wlr_texture_destroy(cont->title_unfocused); + wlr_texture_destroy(cont->title_urgent); + } list_free(cont->children); cont->children = NULL; free(cont); @@ -546,3 +555,59 @@ void container_damage_whole(struct sway_container *con) { } output_damage_whole_container(output->sway_output, con); } + +static void update_title_texture(struct sway_container *con, + struct wlr_texture **texture, struct border_colors *class) { + if (!sway_assert(con->type == C_CONTAINER || con->type == C_VIEW, + "Unexpected type %s", container_type_to_str(con->type))) { + return; + } + if (!con->width) { + return; + } + struct sway_container *output = container_parent(con, C_OUTPUT); + if (!output) { + return; + } + if (*texture) { + wlr_texture_destroy(*texture); + } + if (!con->name) { + return; + } + + int width = con->width * output->sway_output->wlr_output->scale; + int height = config->font_height * output->sway_output->wlr_output->scale; + + cairo_surface_t *surface = cairo_image_surface_create( + CAIRO_FORMAT_ARGB32, width, height); + cairo_t *cairo = cairo_create(surface); + PangoContext *pango = pango_cairo_create_context(cairo); + cairo_set_source_u32(cairo, class->text); + cairo_move_to(cairo, 0, 0); + + pango_printf(cairo, config->font, output->sway_output->wlr_output->scale, + false, "%s", con->name); + + cairo_surface_flush(surface); + unsigned char *data = cairo_image_surface_get_data(surface); + int stride = cairo_format_stride_for_width(CAIRO_FORMAT_ARGB32, width); + struct wlr_renderer *renderer = wlr_backend_get_renderer( + output->sway_output->wlr_output->backend); + *texture = wlr_texture_from_pixels( + renderer, WL_SHM_FORMAT_ARGB8888, stride, width, height, data); + cairo_surface_destroy(surface); + g_object_unref(pango); + cairo_destroy(cairo); +} + +void container_update_title_textures(struct sway_container *container) { + update_title_texture(container, &container->title_focused, + &config->border_colors.focused); + update_title_texture(container, &container->title_focused_inactive, + &config->border_colors.focused_inactive); + update_title_texture(container, &container->title_unfocused, + &config->border_colors.unfocused); + update_title_texture(container, &container->title_urgent, + &config->border_colors.urgent); +} diff --git a/sway/tree/view.c b/sway/tree/view.c index 87ed62c2..84962306 100644 --- a/sway/tree/view.c +++ b/sway/tree/view.c @@ -103,11 +103,12 @@ void view_autoconfigure(struct sway_view *view) { height = view->swayc->height - view->border_thickness * 2; break; case B_NORMAL: - // TODO: Size the title bar by checking the font + // Height is: border + title height + border + view height + border x = view->swayc->x + view->border_thickness; - y = view->swayc->y + 20; + y = view->swayc->y + config->font_height + view->border_thickness * 2; width = view->swayc->width - view->border_thickness * 2; - height = view->swayc->height - view->border_thickness - 20; + height = view->swayc->height - config->font_height + - view->border_thickness * 3; break; } -- cgit v1.2.3