aboutsummaryrefslogtreecommitdiff
path: root/sway/border.c
diff options
context:
space:
mode:
authorDrew DeVault <sir@cmpwn.com>2016-04-25 11:34:27 -0400
committerDrew DeVault <sir@cmpwn.com>2016-04-25 11:34:27 -0400
commitdba1195b4452dd7497d780b5d8c0b43f361f5aab (patch)
tree0b329d72536a75e5d960b25b4414b9d1fe4a5018 /sway/border.c
parent7efa9ab34ae1dabcc7c87d22bfba0b1312c8c662 (diff)
parent05b4965a99417b74df13e9138b14347e7dc47685 (diff)
Merge pull request #566 from mikkeloscar/tabbed-stacking-layout
Tabbed and stacked layout
Diffstat (limited to 'sway/border.c')
-rw-r--r--sway/border.c297
1 files changed, 226 insertions, 71 deletions
diff --git a/sway/border.c b/sway/border.c
index a6ed4238..cec443f4 100644
--- a/sway/border.c
+++ b/sway/border.c
@@ -11,37 +11,40 @@
#include <arpa/inet.h>
void cairo_set_source_u32(cairo_t *cairo, uint32_t color) {
- color = htonl(color);
+ color = htonl(color);
- cairo_set_source_rgba(cairo,
- (color >> (2*8) & 0xFF) / 255.0,
- (color >> (1*8) & 0xFF) / 255.0,
- (color >> (0*8) & 0xFF) / 255.0,
- (color >> (3*8) & 0xFF) / 255.0);
+ cairo_set_source_rgba(cairo,
+ (color >> (2*8) & 0xFF) / 255.0,
+ (color >> (1*8) & 0xFF) / 255.0,
+ (color >> (0*8) & 0xFF) / 255.0,
+ (color >> (3*8) & 0xFF) / 255.0);
}
-static cairo_t *create_border_buffer(swayc_t *view, struct wlc_geometry geo, cairo_surface_t **surface) {
+static cairo_t *create_border_buffer(swayc_t *view, struct wlc_geometry g, cairo_surface_t **surface) {
+ if (view->border == NULL) {
+ view->border = malloc(sizeof(struct border));
+ }
cairo_t *cr;
- view->border_geometry = geo;
- int stride = cairo_format_stride_for_width(CAIRO_FORMAT_ARGB32, geo.size.w);
- view->border = calloc(stride * geo.size.h, sizeof(unsigned char));
- if (!view->border) {
+ int stride = cairo_format_stride_for_width(CAIRO_FORMAT_ARGB32, g.size.w);
+ view->border->buffer = calloc(stride * g.size.h, sizeof(unsigned char));
+ view->border->geometry = g;
+ if (!view->border->buffer) {
sway_log(L_DEBUG, "Unable to allocate buffer");
return NULL;
}
- *surface = cairo_image_surface_create_for_data(view->border,
- CAIRO_FORMAT_ARGB32, geo.size.w, geo.size.h, stride);
+ *surface = cairo_image_surface_create_for_data(view->border->buffer,
+ CAIRO_FORMAT_ARGB32, g.size.w, g.size.h, stride);
if (cairo_surface_status(*surface) != CAIRO_STATUS_SUCCESS) {
free(view->border);
- view->border = NULL;
+ view->border->buffer = NULL;
sway_log(L_DEBUG, "Unable to allocate surface");
return NULL;
}
cr = cairo_create(*surface);
if (cairo_status(cr) != CAIRO_STATUS_SUCCESS) {
cairo_surface_destroy(*surface);
- free(view->border);
- view->border = NULL;
+ free(view->border->buffer);
+ view->border->buffer = NULL;
sway_log(L_DEBUG, "Unable to create cairo context");
return NULL;
}
@@ -91,16 +94,20 @@ int get_font_text_height(const char *font) {
return height;
}
-static void render_borders(swayc_t *view, cairo_t *cr, struct border_colors *colors) {
+static void render_borders(swayc_t *view, cairo_t *cr, struct border_colors *colors, bool top) {
+ struct wlc_geometry *g = &view->border->geometry;
struct wlc_geometry *b = &view->border_geometry;
struct wlc_geometry *v = &view->actual_geometry;
+ int x = b->origin.x - g->origin.x;
+ int y = b->origin.y - g->origin.y;
+
// left border
int left_border = v->origin.x - b->origin.x;
if (left_border > 0) {
render_sharp_line(cr,
colors->child_border,
- 0, 0,
+ x, y,
left_border,
b->size.h);
}
@@ -110,18 +117,18 @@ static void render_borders(swayc_t *view, cairo_t *cr, struct border_colors *col
if (right_border > 0) {
render_sharp_line(cr,
colors->child_border,
- b->size.w - right_border,
- 0,
+ x + b->size.w - right_border,
+ y,
right_border,
b->size.h);
}
// top border
int top_border = v->origin.y - b->origin.y;
- if (top_border > 0) {
+ if (top && top_border > 0) {
render_sharp_line(cr,
colors->child_border,
- 0, 0,
+ x, y,
b->size.w,
top_border);
}
@@ -131,45 +138,63 @@ static void render_borders(swayc_t *view, cairo_t *cr, struct border_colors *col
if (bottom_border > 0) {
render_sharp_line(cr,
colors->child_border,
- 0,
- b->size.h - bottom_border,
+ x,
+ y + b->size.h - bottom_border,
b->size.w,
bottom_border);
}
}
-static void render_with_title_bar(swayc_t *view, cairo_t *cr, struct border_colors *colors) {
+static void render_title_bar(swayc_t *view, cairo_t *cr, struct wlc_geometry *b, struct border_colors *colors) {
struct wlc_geometry *tb = &view->title_bar_geometry;
- struct wlc_geometry *b = &view->border_geometry;
- int title_y = MIN(view->actual_geometry.origin.y - (int)tb->size.h, 0);
-
- // borders
- render_borders(view, cr, colors);
+ int x = MIN(tb->origin.x, tb->origin.x - b->origin.x);
+ int y = MIN(tb->origin.y, tb->origin.y - b->origin.y);
// title bar background
cairo_set_source_u32(cr, colors->background);
- cairo_rectangle(cr, 0, title_y, tb->size.w, tb->size.h);
+ cairo_rectangle(cr, x, y, tb->size.w, tb->size.h);
cairo_fill(cr);
// header top line
- render_sharp_line(cr, colors->border, 0, title_y, tb->size.w, 1);
+ render_sharp_line(cr, colors->border, x, y, tb->size.w, 1);
// text
if (view->name) {
int width, height;
get_text_size(cr, config->font, &width, &height, false, "%s", view->name);
- int x = MIN(view->actual_geometry.origin.x, view->border_thickness);
- int y = MIN(view->actual_geometry.origin.y - height - 2, 2);
- cairo_move_to(cr, x, y);
+ cairo_move_to(cr, x + 2, y + 2);
cairo_set_source_u32(cr, colors->text);
pango_printf(cr, config->font, false, "%s", view->name);
}
- // header bottom line
- render_sharp_line(cr, colors->border,
- view->actual_geometry.origin.x - b->origin.x,
- title_y + tb->size.h - 1,
- view->actual_geometry.size.w, 1);
+ // titlebars has a border all around for tabbed layouts
+ if (view->parent->layout == L_TABBED) {
+ // header bottom line
+ render_sharp_line(cr, colors->border, x, y + tb->size.h - 1,
+ tb->size.w, 1);
+
+ // left border
+ render_sharp_line(cr, colors->border, x, y, 1, tb->size.h);
+
+ // right border
+ render_sharp_line(cr, colors->border, x + tb->size.w - 1, y,
+ 1, tb->size.h);
+
+ return;
+ }
+
+ if ((uint32_t)(view->actual_geometry.origin.y - tb->origin.y) == tb->size.h) {
+ // header bottom line
+ render_sharp_line(cr, colors->border,
+ x + view->actual_geometry.origin.x - tb->origin.x,
+ y + tb->size.h - 1,
+ view->actual_geometry.size.w, 1);
+ } else {
+ // header bottom line
+ render_sharp_line(cr, colors->border, x,
+ y + tb->size.h - 1,
+ tb->size.w, 1);
+ }
}
void map_update_view_border(swayc_t *view, void *data) {
@@ -178,15 +203,104 @@ void map_update_view_border(swayc_t *view, void *data) {
}
}
+/**
+ * Generate nested container title for tabbed/stacked layouts
+ */
+static char *generate_container_title(swayc_t *container) {
+ char layout = 'H';
+ char *name, *prev_name = NULL;
+ switch (container->layout) {
+ case L_TABBED:
+ layout = 'T';
+ break;
+ case L_STACKED:
+ layout = 'S';
+ break;
+ case L_VERT:
+ layout = 'V';
+ break;
+ default:
+ layout = 'H';
+ }
+ int len = 9;
+ name = malloc(len * sizeof(char));
+ snprintf(name, len, "sway: %c[", layout);
+
+ int i;
+ for (i = 0; i < container->children->length; ++i) {
+ prev_name = name;
+ swayc_t* child = container->children->items[i];
+ const char *title = child->name;
+ if (child->type == C_CONTAINER) {
+ title = generate_container_title(child);
+ }
+
+ len = strlen(name) + strlen(title) + 1;
+ if (i < container->children->length-1) {
+ len++;
+ }
+
+ name = malloc(len * sizeof(char));
+ if (i < container->children->length-1) {
+ snprintf(name, len, "%s%s ", prev_name, title);
+ } else {
+ snprintf(name, len, "%s%s", prev_name, title);
+ }
+ free(prev_name);
+ }
+
+ prev_name = name;
+ len = strlen(name) + 2;
+ name = malloc(len * sizeof(char));
+ snprintf(name, len, "%s]", prev_name);
+ free(prev_name);
+ free(container->name);
+ container->name = name;
+ return container->name + 6; // don't include "sway: "
+}
+
+void update_tabbed_stacked_titlebars(swayc_t *c, cairo_t *cr, struct wlc_geometry *g, swayc_t *focused, swayc_t *focused_inactive) {
+ if (c->type == C_CONTAINER) {
+ if (c->parent->focused == c) {
+ render_title_bar(c, cr, g, &config->border_colors.focused_inactive);
+ } else {
+ render_title_bar(c, cr, g, &config->border_colors.unfocused);
+ }
+
+ if (!c->visible) {
+ return;
+ }
+
+ int i;
+ for (i = 0; i < c->children->length; ++i) {
+ swayc_t *child = c->children->items[i];
+ update_tabbed_stacked_titlebars(child, cr, g, focused, focused_inactive);
+ }
+ } else {
+ if (focused == c) {
+ render_title_bar(c, cr, g, &config->border_colors.focused);
+ } else if (focused_inactive == c) {
+ render_title_bar(c, cr, g, &config->border_colors.focused_inactive);
+ } else {
+ render_title_bar(c, cr, g, &config->border_colors.unfocused);
+ }
+ }
+}
+
void update_view_border(swayc_t *view) {
+ if (!view->visible) {
+ return;
+ }
+
cairo_t *cr = NULL;
cairo_surface_t *surface = NULL;
- if (view->border) {
- free(view->border);
- view->border = NULL;
+ if (view->border && view->border->buffer) {
+ free(view->border->buffer);
+ view->border->buffer = NULL;
}
+ // get focused and focused_inactive views
swayc_t *focused = get_focused_view(&root_container);
swayc_t *container = swayc_parent_by_type(view, C_CONTAINER);
swayc_t *focused_inactive = NULL;
@@ -199,44 +313,85 @@ void update_view_border(swayc_t *view) {
}
}
- switch (view->border_type) {
- case B_NONE:
- break;
- case B_PIXEL:
- cr = create_border_buffer(view, view->border_geometry, &surface);
- if (!cr) {
- break;
+ // for tabbed/stacked layouts the focused view has to draw all the
+ // titlebars of the hidden views.
+ swayc_t *p = swayc_tabbed_stacked_parent(view);
+ if (p && view->parent->focused == view) {
+ struct wlc_geometry g = {
+ .origin = {
+ .x = p->x,
+ .y = p->y
+ },
+ .size = {
+ .w = p->width,
+ .h = p->height
+ }
+ };
+ cr = create_border_buffer(view, g, &surface);
+ if (view == focused) {
+ render_borders(view, cr, &config->border_colors.focused, false);
+ } else {
+ render_borders(view, cr, &config->border_colors.focused_inactive, false);
}
- if (focused == view) {
- render_borders(view, cr, &config->border_colors.focused);
- } else if (focused_inactive == view) {
- render_borders(view, cr, &config->border_colors.focused_inactive);
- } else {
- render_borders(view, cr, &config->border_colors.unfocused);
+ // generate container titles
+ int i;
+ for (i = 0; i < p->children->length; ++i) {
+ swayc_t *child = p->children->items[i];
+ if (child->type == C_CONTAINER) {
+ generate_container_title(child);
+ }
}
- break;
- case B_NORMAL:
- cr = create_border_buffer(view, view->border_geometry, &surface);
- if (!cr) {
+ update_tabbed_stacked_titlebars(p, cr, &g, focused, focused_inactive);
+ } else {
+ switch (view->border_type) {
+ case B_NONE:
break;
- }
+ case B_PIXEL:
+ cr = create_border_buffer(view, view->border_geometry, &surface);
+ if (!cr) {
+ break;
+ }
- if (focused == view) {
- render_with_title_bar(view, cr, &config->border_colors.focused);
- } else if (focused_inactive == view) {
- render_with_title_bar(view, cr, &config->border_colors.focused_inactive);
- } else {
- render_with_title_bar(view, cr, &config->border_colors.unfocused);
- }
+ if (focused == view) {
+ render_borders(view, cr, &config->border_colors.focused, true);
+ } else if (focused_inactive == view) {
+ render_borders(view, cr, &config->border_colors.focused_inactive, true);
+ } else {
+ render_borders(view, cr, &config->border_colors.unfocused, true);
+ }
- break;
+ break;
+ case B_NORMAL:
+ cr = create_border_buffer(view, view->border_geometry, &surface);
+ if (!cr) {
+ break;
+ }
+
+ if (focused == view) {
+ render_borders(view, cr, &config->border_colors.focused, false);
+ render_title_bar(view, cr, &view->border_geometry,
+ &config->border_colors.focused);
+ } else if (focused_inactive == view) {
+ render_borders(view, cr, &config->border_colors.focused_inactive, false);
+ render_title_bar(view, cr, &view->border_geometry,
+ &config->border_colors.focused_inactive);
+ } else {
+ render_borders(view, cr, &config->border_colors.unfocused, false);
+ render_title_bar(view, cr, &view->border_geometry,
+ &config->border_colors.unfocused);
+ }
+
+ break;
+ }
}
+
if (surface) {
cairo_surface_flush(surface);
cairo_surface_destroy(surface);
}
+
if (cr) {
cairo_destroy(cr);
}
@@ -262,7 +417,7 @@ void render_view_borders(wlc_handle view) {
}
}
- if (c->border) {
- wlc_pixels_write(WLC_RGBA8888, &c->border_geometry, c->border);
+ if (c->border && c->border->buffer) {
+ wlc_pixels_write(WLC_RGBA8888, &c->border->geometry, c->border->buffer);
}
}