Merge pull request #566 from mikkeloscar/tabbed-stacking-layout

Tabbed and stacked layout
master
Drew DeVault 9 years ago
commit dba1195b44

@ -3,6 +3,11 @@
#include <wlc/wlc.h> #include <wlc/wlc.h>
#include "container.h" #include "container.h"
struct border {
unsigned char *buffer;
struct wlc_geometry geometry;
};
void render_view_borders(wlc_handle view); void render_view_borders(wlc_handle view);
void update_view_border(swayc_t *view); void update_view_border(swayc_t *view);
void map_update_view_border(swayc_t *view, void *data); void map_update_view_border(swayc_t *view, void *data);

@ -2,9 +2,12 @@
#define _SWAY_CONTAINER_H #define _SWAY_CONTAINER_H
#include <sys/types.h> #include <sys/types.h>
#include <wlc/wlc.h> #include <wlc/wlc.h>
#include "list.h"
typedef struct sway_container swayc_t; typedef struct sway_container swayc_t;
#include "layout.h" extern swayc_t root_container;
/** /**
* Different kinds of containers. * Different kinds of containers.
@ -56,6 +59,7 @@ struct sway_container {
enum swayc_types type; enum swayc_types type;
enum swayc_layouts layout; enum swayc_layouts layout;
enum swayc_layouts prev_layout;
/** /**
* Width and height of this container, without borders or gaps. * Width and height of this container, without borders or gaps.
@ -74,6 +78,12 @@ struct sway_container {
*/ */
double x, y; double x, y;
/**
* Cached geometry used to store view/container geometry when switching
* between tabbed/stacked and horizontal/vertical layouts.
*/
struct wlc_geometry cached_geometry;
/** /**
* False if this view is invisible. It could be in the scratchpad or on a * False if this view is invisible. It could be in the scratchpad or on a
* workspace that is not shown. * workspace that is not shown.
@ -119,7 +129,7 @@ struct sway_container {
* If this container is a view, this may be set to the window's decoration * If this container is a view, this may be set to the window's decoration
* buffer (or NULL). * buffer (or NULL).
*/ */
unsigned char *border; struct border *border;
enum swayc_border_types border_type; enum swayc_border_types border_type;
struct wlc_geometry border_geometry; struct wlc_geometry border_geometry;
struct wlc_geometry title_bar_geometry; struct wlc_geometry title_bar_geometry;
@ -240,6 +250,13 @@ bool swayc_is_parent_of(swayc_t *parent, swayc_t *child);
* Returns true if the child is a desecendant of the parent. * Returns true if the child is a desecendant of the parent.
*/ */
bool swayc_is_child_of(swayc_t *child, swayc_t *parent); bool swayc_is_child_of(swayc_t *child, swayc_t *parent);
/**
* Returns the top most tabbed or stacked parent container. Returns NULL if
* view is not in a tabbed/stacked layout.
*/
swayc_t *swayc_tabbed_stacked_parent(swayc_t *view);
/** /**
* Returns the gap (padding) of the container. * Returns the gap (padding) of the container.
* *

@ -7,8 +7,6 @@
#include "container.h" #include "container.h"
#include "focus.h" #include "focus.h"
extern swayc_t root_container;
extern list_t *scratchpad; extern list_t *scratchpad;
extern int min_sane_w; extern int min_sane_w;
@ -55,6 +53,10 @@ void move_container_to(swayc_t* container, swayc_t* destination);
void move_workspace_to(swayc_t* workspace, swayc_t* destination); void move_workspace_to(swayc_t* workspace, swayc_t* destination);
// Layout // Layout
/**
* Update child container geometries when switching between layouts.
*/
void update_layout_geometry(swayc_t *parent, enum swayc_layouts prev_layout);
void update_geometry(swayc_t *view); void update_geometry(swayc_t *view);
void arrange_windows(swayc_t *container, double width, double height); void arrange_windows(swayc_t *container, double width, double height);
@ -67,4 +69,9 @@ void recursive_resize(swayc_t *container, double amount, enum wlc_resize_edge ed
void layout_log(const swayc_t *c, int depth); void layout_log(const swayc_t *c, int depth);
void swayc_log(log_importance_t verbosity, swayc_t *cont, const char* format, ...) __attribute__((format(printf,3,4))); void swayc_log(log_importance_t verbosity, swayc_t *cont, const char* format, ...) __attribute__((format(printf,3,4)));
/**
* Get default layout.
*/
enum swayc_layouts default_layout(swayc_t *output);
#endif #endif

@ -20,28 +20,31 @@ void cairo_set_source_u32(cairo_t *cairo, uint32_t color) {
(color >> (3*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; cairo_t *cr;
view->border_geometry = geo; int stride = cairo_format_stride_for_width(CAIRO_FORMAT_ARGB32, g.size.w);
int stride = cairo_format_stride_for_width(CAIRO_FORMAT_ARGB32, geo.size.w); view->border->buffer = calloc(stride * g.size.h, sizeof(unsigned char));
view->border = calloc(stride * geo.size.h, sizeof(unsigned char)); view->border->geometry = g;
if (!view->border) { if (!view->border->buffer) {
sway_log(L_DEBUG, "Unable to allocate buffer"); sway_log(L_DEBUG, "Unable to allocate buffer");
return NULL; return NULL;
} }
*surface = cairo_image_surface_create_for_data(view->border, *surface = cairo_image_surface_create_for_data(view->border->buffer,
CAIRO_FORMAT_ARGB32, geo.size.w, geo.size.h, stride); CAIRO_FORMAT_ARGB32, g.size.w, g.size.h, stride);
if (cairo_surface_status(*surface) != CAIRO_STATUS_SUCCESS) { if (cairo_surface_status(*surface) != CAIRO_STATUS_SUCCESS) {
free(view->border); free(view->border);
view->border = NULL; view->border->buffer = NULL;
sway_log(L_DEBUG, "Unable to allocate surface"); sway_log(L_DEBUG, "Unable to allocate surface");
return NULL; return NULL;
} }
cr = cairo_create(*surface); cr = cairo_create(*surface);
if (cairo_status(cr) != CAIRO_STATUS_SUCCESS) { if (cairo_status(cr) != CAIRO_STATUS_SUCCESS) {
cairo_surface_destroy(*surface); cairo_surface_destroy(*surface);
free(view->border); free(view->border->buffer);
view->border = NULL; view->border->buffer = NULL;
sway_log(L_DEBUG, "Unable to create cairo context"); sway_log(L_DEBUG, "Unable to create cairo context");
return NULL; return NULL;
} }
@ -91,16 +94,20 @@ int get_font_text_height(const char *font) {
return height; 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 *b = &view->border_geometry;
struct wlc_geometry *v = &view->actual_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 // left border
int left_border = v->origin.x - b->origin.x; int left_border = v->origin.x - b->origin.x;
if (left_border > 0) { if (left_border > 0) {
render_sharp_line(cr, render_sharp_line(cr,
colors->child_border, colors->child_border,
0, 0, x, y,
left_border, left_border,
b->size.h); 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) { if (right_border > 0) {
render_sharp_line(cr, render_sharp_line(cr,
colors->child_border, colors->child_border,
b->size.w - right_border, x + b->size.w - right_border,
0, y,
right_border, right_border,
b->size.h); b->size.h);
} }
// top border // top border
int top_border = v->origin.y - b->origin.y; int top_border = v->origin.y - b->origin.y;
if (top_border > 0) { if (top && top_border > 0) {
render_sharp_line(cr, render_sharp_line(cr,
colors->child_border, colors->child_border,
0, 0, x, y,
b->size.w, b->size.w,
top_border); top_border);
} }
@ -131,45 +138,63 @@ static void render_borders(swayc_t *view, cairo_t *cr, struct border_colors *col
if (bottom_border > 0) { if (bottom_border > 0) {
render_sharp_line(cr, render_sharp_line(cr,
colors->child_border, colors->child_border,
0, x,
b->size.h - bottom_border, y + b->size.h - bottom_border,
b->size.w, b->size.w,
bottom_border); 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 *tb = &view->title_bar_geometry;
struct wlc_geometry *b = &view->border_geometry; int x = MIN(tb->origin.x, tb->origin.x - b->origin.x);
int title_y = MIN(view->actual_geometry.origin.y - (int)tb->size.h, 0); int y = MIN(tb->origin.y, tb->origin.y - b->origin.y);
// borders
render_borders(view, cr, colors);
// title bar background // title bar background
cairo_set_source_u32(cr, colors->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); cairo_fill(cr);
// header top line // 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 // text
if (view->name) { if (view->name) {
int width, height; int width, height;
get_text_size(cr, config->font, &width, &height, false, "%s", view->name); get_text_size(cr, config->font, &width, &height, false, "%s", view->name);
int x = MIN(view->actual_geometry.origin.x, view->border_thickness); cairo_move_to(cr, x + 2, y + 2);
int y = MIN(view->actual_geometry.origin.y - height - 2, 2);
cairo_move_to(cr, x, y);
cairo_set_source_u32(cr, colors->text); cairo_set_source_u32(cr, colors->text);
pango_printf(cr, config->font, false, "%s", view->name); pango_printf(cr, config->font, false, "%s", view->name);
} }
// 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 // header bottom line
render_sharp_line(cr, colors->border, render_sharp_line(cr, colors->border,
view->actual_geometry.origin.x - b->origin.x, x + view->actual_geometry.origin.x - tb->origin.x,
title_y + tb->size.h - 1, y + tb->size.h - 1,
view->actual_geometry.size.w, 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) { 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) { void update_view_border(swayc_t *view) {
if (!view->visible) {
return;
}
cairo_t *cr = NULL; cairo_t *cr = NULL;
cairo_surface_t *surface = NULL; cairo_surface_t *surface = NULL;
if (view->border) { if (view->border && view->border->buffer) {
free(view->border); free(view->border->buffer);
view->border = NULL; view->border->buffer = NULL;
} }
// get focused and focused_inactive views
swayc_t *focused = get_focused_view(&root_container); swayc_t *focused = get_focused_view(&root_container);
swayc_t *container = swayc_parent_by_type(view, C_CONTAINER); swayc_t *container = swayc_parent_by_type(view, C_CONTAINER);
swayc_t *focused_inactive = NULL; swayc_t *focused_inactive = NULL;
@ -199,6 +313,38 @@ void update_view_border(swayc_t *view) {
} }
} }
// 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);
}
// 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);
}
}
update_tabbed_stacked_titlebars(p, cr, &g, focused, focused_inactive);
} else {
switch (view->border_type) { switch (view->border_type) {
case B_NONE: case B_NONE:
break; break;
@ -209,11 +355,11 @@ void update_view_border(swayc_t *view) {
} }
if (focused == view) { if (focused == view) {
render_borders(view, cr, &config->border_colors.focused); render_borders(view, cr, &config->border_colors.focused, true);
} else if (focused_inactive == view) { } else if (focused_inactive == view) {
render_borders(view, cr, &config->border_colors.focused_inactive); render_borders(view, cr, &config->border_colors.focused_inactive, true);
} else { } else {
render_borders(view, cr, &config->border_colors.unfocused); render_borders(view, cr, &config->border_colors.unfocused, true);
} }
break; break;
@ -224,19 +370,28 @@ void update_view_border(swayc_t *view) {
} }
if (focused == view) { if (focused == view) {
render_with_title_bar(view, cr, &config->border_colors.focused); 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) { } else if (focused_inactive == view) {
render_with_title_bar(view, cr, &config->border_colors.focused_inactive); render_borders(view, cr, &config->border_colors.focused_inactive, false);
render_title_bar(view, cr, &view->border_geometry,
&config->border_colors.focused_inactive);
} else { } else {
render_with_title_bar(view, cr, &config->border_colors.unfocused); render_borders(view, cr, &config->border_colors.unfocused, false);
render_title_bar(view, cr, &view->border_geometry,
&config->border_colors.unfocused);
} }
break; break;
} }
}
if (surface) { if (surface) {
cairo_surface_flush(surface); cairo_surface_flush(surface);
cairo_surface_destroy(surface); cairo_surface_destroy(surface);
} }
if (cr) { if (cr) {
cairo_destroy(cr); cairo_destroy(cr);
} }
@ -262,7 +417,7 @@ void render_view_borders(wlc_handle view) {
} }
} }
if (c->border) { if (c->border && c->border->buffer) {
wlc_pixels_write(WLC_RGBA8888, &c->border_geometry, c->border); wlc_pixels_write(WLC_RGBA8888, &c->border->geometry, c->border->buffer);
} }
} }

@ -1,6 +1,7 @@
#include <xkbcommon/xkbcommon.h> #include <xkbcommon/xkbcommon.h>
#include <xkbcommon/xkbcommon-names.h> #include <xkbcommon/xkbcommon-names.h>
#include <wlc/wlc.h> #include <wlc/wlc.h>
#include <wlc/wlc-render.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <errno.h> #include <errno.h>
@ -1759,7 +1760,32 @@ static struct cmd_results *cmd_layout(int argc, char **argv) {
parent = parent->parent; parent = parent->parent;
} }
if (strcasecmp(argv[0], "splith") == 0) { enum swayc_layouts old_layout = parent->layout;
if (strcasecmp(argv[0], "default") == 0) {
parent->layout = parent->prev_layout;
if (parent->layout == L_NONE) {
swayc_t *output = swayc_parent_by_type(parent, C_OUTPUT);
parent->layout = default_layout(output);
}
} else {
if (parent->layout != L_TABBED && parent->layout != L_STACKED) {
parent->prev_layout = parent->layout;
}
if (strcasecmp(argv[0], "tabbed") == 0) {
if (parent->type != C_CONTAINER) {
parent = new_container(parent, L_TABBED);
}
parent->layout = L_TABBED;
} else if (strcasecmp(argv[0], "stacking") == 0) {
if (parent->type != C_CONTAINER) {
parent = new_container(parent, L_STACKED);
}
parent->layout = L_STACKED;
} else if (strcasecmp(argv[0], "splith") == 0) {
parent->layout = L_HORIZ; parent->layout = L_HORIZ;
} else if (strcasecmp(argv[0], "splitv") == 0) { } else if (strcasecmp(argv[0], "splitv") == 0) {
parent->layout = L_VERT; parent->layout = L_VERT;
@ -1770,6 +1796,10 @@ static struct cmd_results *cmd_layout(int argc, char **argv) {
parent->layout = L_VERT; parent->layout = L_VERT;
} }
} }
}
update_layout_geometry(parent, old_layout);
arrange_windows(parent, parent->width, parent->height); arrange_windows(parent, parent->width, parent->height);
return cmd_results_new(CMD_SUCCESS, NULL, NULL); return cmd_results_new(CMD_SUCCESS, NULL, NULL);
@ -2007,10 +2037,22 @@ static struct cmd_results *_do_split(int argc, char **argv, int layout) {
/* regular case where new split container is build around focused container /* regular case where new split container is build around focused container
* or in case of workspace, container inherits its children */ * or in case of workspace, container inherits its children */
sway_log(L_DEBUG, "Adding new container around current focused container"); sway_log(L_DEBUG, "Adding new container around current focused container");
sway_log(L_INFO, "FOCUSED SIZE: %.f %.f", focused->width, focused->height);
swayc_t *parent = new_container(focused, layout); swayc_t *parent = new_container(focused, layout);
set_focused_container(focused); set_focused_container(focused);
arrange_windows(parent, -1, -1); arrange_windows(parent, -1, -1);
} }
// update container title if tabbed/stacked
if (swayc_tabbed_stacked_parent(focused)) {
update_view_border(focused);
swayc_t *output = swayc_parent_by_type(focused, C_OUTPUT);
// schedule render to make changes take effect right away,
// otherwise we would have to wait for the view to render,
// which is unpredictable.
wlc_output_schedule_render(output->handle);
}
return cmd_results_new(CMD_SUCCESS, NULL, NULL); return cmd_results_new(CMD_SUCCESS, NULL, NULL);
} }

@ -8,6 +8,7 @@
#include "container.h" #include "container.h"
#include "workspace.h" #include "workspace.h"
#include "focus.h" #include "focus.h"
#include "border.h"
#include "layout.h" #include "layout.h"
#include "input_state.h" #include "input_state.h"
#include "log.h" #include "log.h"
@ -64,7 +65,12 @@ static void free_swayc(swayc_t *cont) {
if (cont->bg_pid != 0) { if (cont->bg_pid != 0) {
terminate_swaybg(cont->bg_pid); terminate_swaybg(cont->bg_pid);
} }
if (cont->border) {
if (cont->border->buffer) {
free(cont->border->buffer);
}
free(cont->border); free(cont->border);
}
free(cont); free(cont);
} }
@ -163,16 +169,9 @@ swayc_t *new_workspace(swayc_t *output, const char *name) {
sway_log(L_DEBUG, "Added workspace %s for output %u", name, (unsigned int)output->handle); sway_log(L_DEBUG, "Added workspace %s for output %u", name, (unsigned int)output->handle);
swayc_t *workspace = new_swayc(C_WORKSPACE); swayc_t *workspace = new_swayc(C_WORKSPACE);
// TODO: default_layout workspace->prev_layout = L_NONE;
if (config->default_layout != L_NONE) { workspace->layout = default_layout(output);
workspace->layout = config->default_layout;
} else if (config->default_orientation != L_NONE) {
workspace->layout = config->default_orientation;
} else if (output->width >= output->height) {
workspace->layout = L_HORIZ;
} else {
workspace->layout = L_VERT;
}
workspace->x = output->x; workspace->x = output->x;
workspace->y = output->y; workspace->y = output->y;
workspace->width = output->width; workspace->width = output->width;
@ -211,12 +210,15 @@ swayc_t *new_container(swayc_t *child, enum swayc_layouts layout) {
sway_log(L_DEBUG, "creating container %p around %p", cont, child); sway_log(L_DEBUG, "creating container %p around %p", cont, child);
cont->prev_layout = L_NONE;
cont->layout = layout; cont->layout = layout;
cont->width = child->width; cont->width = child->width;
cont->height = child->height; cont->height = child->height;
cont->x = child->x; cont->x = child->x;
cont->y = child->y; cont->y = child->y;
cont->visible = child->visible; cont->visible = child->visible;
cont->cached_geometry = child->cached_geometry;
cont->gaps = child->gaps;
/* Container inherits all of workspaces children, layout and whatnot */ /* Container inherits all of workspaces children, layout and whatnot */
if (child->type == C_WORKSPACE) { if (child->type == C_WORKSPACE) {
@ -237,7 +239,8 @@ swayc_t *new_container(swayc_t *child, enum swayc_layouts layout) {
add_child(workspace, cont); add_child(workspace, cont);
// give them proper layouts // give them proper layouts
cont->layout = workspace->layout; cont->layout = workspace->layout;
workspace->layout = layout; cont->prev_layout = workspace->prev_layout;
/* TODO: might break shit in move_container!!! workspace->layout = layout; */
set_focused_container_for(workspace, get_focused_view(workspace)); set_focused_container_for(workspace, get_focused_view(workspace));
} else { // Or is built around container } else { // Or is built around container
swayc_t *parent = replace_child(child, cont); swayc_t *parent = replace_child(child, cont);
@ -678,7 +681,7 @@ bool swayc_is_child_of(swayc_t *child, swayc_t *parent) {
} }
int swayc_gap(swayc_t *container) { int swayc_gap(swayc_t *container) {
if (container->type == C_VIEW) { if (container->type == C_VIEW || container->type == C_CONTAINER) {
return container->gaps >= 0 ? container->gaps : config->gaps_inner; return container->gaps >= 0 ? container->gaps : config->gaps_inner;
} else if (container->type == C_WORKSPACE) { } else if (container->type == C_WORKSPACE) {
int base = container->gaps >= 0 ? container->gaps : config->gaps_outer; int base = container->gaps >= 0 ? container->gaps : config->gaps_outer;
@ -722,18 +725,14 @@ void update_visibility_output(swayc_t *container, wlc_handle output) {
swayc_t *parent = container->parent; swayc_t *parent = container->parent;
container->visible = parent->visible; container->visible = parent->visible;
// special cases where visibility depends on focus // special cases where visibility depends on focus
if (parent->type == C_OUTPUT if (parent->type == C_OUTPUT || parent->layout == L_TABBED ||
|| parent->layout == L_TABBED parent->layout == L_STACKED) {
|| parent->layout == L_STACKED) { container->visible = parent->focused == container && parent->visible;
container->visible = parent->focused == container;
} }
// Set visibility and output for view // Set visibility and output for view
if (container->type == C_VIEW) { if (container->type == C_VIEW) {
wlc_view_set_output(container->handle, output); wlc_view_set_output(container->handle, output);
wlc_view_set_mask(container->handle, container->visible ? VISIBLE : 0); wlc_view_set_mask(container->handle, container->visible ? VISIBLE : 0);
if (!container->visible) {
wlc_view_send_to_back(container->handle);
}
} }
// Update visibility for children // Update visibility for children
else { else {
@ -814,3 +813,18 @@ static void close_view(swayc_t *container, void *data) {
void close_views(swayc_t *container) { void close_views(swayc_t *container) {
container_map(container, close_view, NULL); container_map(container, close_view, NULL);
} }
swayc_t *swayc_tabbed_stacked_parent(swayc_t *view) {
swayc_t *parent = NULL;
if (!ASSERT_NONNULL(view)) {
return NULL;
}
do {
view = view->parent;
if (view->layout == L_TABBED || view->layout == L_STACKED) {
parent = view;
}
} while (view && view->type != C_WORKSPACE);
return parent;
}

@ -38,6 +38,7 @@ static void container_log(const swayc_t *c, int depth) {
c->layout == L_HORIZ ? "Horiz": c->layout == L_HORIZ ? "Horiz":
c->layout == L_VERT ? "Vert": c->layout == L_VERT ? "Vert":
c->layout == L_STACKED ? "Stack": c->layout == L_STACKED ? "Stack":
c->layout == L_TABBED ? "Tab":
c->layout == L_FLOATING ? "Float": c->layout == L_FLOATING ? "Float":
"Unknown"); "Unknown");
fprintf(stderr, "w:%4.f|h:%4.f|", c->width, c->height); fprintf(stderr, "w:%4.f|h:%4.f|", c->width, c->height);

@ -141,9 +141,18 @@ bool set_focused_container(swayc_t *c) {
// set focus if view_focus is unlocked // set focus if view_focus is unlocked
if (!locked_view_focus) { if (!locked_view_focus) {
wlc_view_focus(p->handle); wlc_view_focus(p->handle);
if (p->parent->layout != L_TABBED
&& p->parent->layout != L_STACKED) {
update_view_border(p); update_view_border(p);
} }
} }
}
// rearrange if parent container is tabbed/stacked
swayc_t *parent = swayc_tabbed_stacked_parent(p);
if (parent != NULL) {
arrange_windows(parent, -1, -1);
}
} else if (p->type == C_WORKSPACE) { } else if (p->type == C_WORKSPACE) {
// remove previous focus if view_focus is unlocked // remove previous focus if view_focus is unlocked
if (!locked_view_focus) { if (!locked_view_focus) {

@ -3,7 +3,6 @@
#include <math.h> #include <math.h>
#include <wlc/wlc.h> #include <wlc/wlc.h>
#include "extensions.h" #include "extensions.h"
#include "layout.h"
#include "log.h" #include "log.h"
#include "list.h" #include "list.h"
#include "config.h" #include "config.h"
@ -13,6 +12,7 @@
#include "output.h" #include "output.h"
#include "ipc-server.h" #include "ipc-server.h"
#include "border.h" #include "border.h"
#include "layout.h"
swayc_t root_container; swayc_t root_container;
list_t *scratchpad; list_t *scratchpad;
@ -244,7 +244,9 @@ void move_container(swayc_t *container, enum movement_direction dir) {
while (true) { while (true) {
sway_log(L_DEBUG, "container:%p, parent:%p, child %p,", sway_log(L_DEBUG, "container:%p, parent:%p, child %p,",
container,parent,child); container,parent,child);
if (parent->layout == layout) { if (parent->layout == layout
|| (parent->layout == L_TABBED && layout == L_HORIZ)
|| (parent->layout == L_STACKED && layout == L_VERT)) {
int diff; int diff;
// If it has ascended (parent has moved up), no container is removed // If it has ascended (parent has moved up), no container is removed
// so insert it at index, or index+1. // so insert it at index, or index+1.
@ -264,9 +266,11 @@ void move_container(swayc_t *container, enum movement_direction dir) {
// Move container into sibling container // Move container into sibling container
if (child->type == C_CONTAINER) { if (child->type == C_CONTAINER) {
parent = child; parent = child;
// Insert it in first/last if matching layout,otherwise // Insert it in first/last if matching layout, otherwise
// inesrt it next to focused container // inesrt it next to focused container
if (parent->layout == layout) { if (parent->layout == layout
|| (parent->layout == L_TABBED && layout == L_HORIZ)
|| (parent->layout == L_STACKED && layout == L_VERT)) {
desired = (diff < 0) * parent->children->length; desired = (diff < 0) * parent->children->length;
} else { } else {
desired = index_child(child->focused); desired = index_child(child->focused);
@ -299,8 +303,6 @@ void move_container(swayc_t *container, enum movement_direction dir) {
child = parent; child = parent;
parent = child->parent; parent = child->parent;
} }
// Dirty hack to fix a certain case
arrange_windows(parent, -1, -1);
arrange_windows(parent->parent, -1, -1); arrange_windows(parent->parent, -1, -1);
set_focused_container_for(parent->parent, container); set_focused_container_for(parent->parent, container);
} }
@ -380,16 +382,14 @@ static void adjust_border_geometry(swayc_t *c, struct wlc_geometry *g,
g->size.w += left + right; g->size.w += left + right;
if (g->origin.x - left < 0) { if (g->origin.x - left < 0) {
g->size.w += g->origin.x - left; g->size.w += g->origin.x - left;
} } else if (g->origin.x + g->size.w - right > res->w) {
else if (g->origin.x + g->size.w - right > res->w) {
g->size.w = res->w - g->origin.x + right; g->size.w = res->w - g->origin.x + right;
} }
g->size.h += top + bottom; g->size.h += top + bottom;
if (g->origin.y - top < 0) { if (g->origin.y - top < 0) {
g->size.h += g->origin.y - top; g->size.h += g->origin.y - top;
} } else if (g->origin.y + g->size.h - top > res->h) {
else if (g->origin.y + g->size.h - top > res->h) {
g->size.h = res->h - g->origin.y + top; g->size.h = res->h - g->origin.y + top;
} }
@ -421,11 +421,11 @@ static void update_border_geometry_floating(swayc_t *c, struct wlc_geometry *geo
struct wlc_geometry title_bar = { struct wlc_geometry title_bar = {
.origin = { .origin = {
.x = g.origin.x, .x = c->actual_geometry.origin.x - c->border_thickness,
.y = g.origin.y .y = c->actual_geometry.origin.y - title_bar_height
}, },
.size = { .size = {
.w = g.size.w, .w = c->actual_geometry.size.w + (2 * c->border_thickness),
.h = title_bar_height .h = title_bar_height
} }
}; };
@ -440,10 +440,42 @@ static void update_border_geometry_floating(swayc_t *c, struct wlc_geometry *geo
update_view_border(c); update_view_border(c);
} }
void update_geometry(swayc_t *container) { void update_layout_geometry(swayc_t *parent, enum swayc_layouts prev_layout) {
if (container->type != C_VIEW) { switch (parent->layout) {
return; case L_TABBED:
case L_STACKED:
if (prev_layout != L_TABBED && prev_layout != L_STACKED) {
// cache current geometry for all non-float children
int i;
for (i = 0; i < parent->children->length; ++i) {
swayc_t *child = parent->children->items[i];
child->cached_geometry.origin.x = child->x;
child->cached_geometry.origin.y = child->y;
child->cached_geometry.size.w = child->width;
child->cached_geometry.size.h = child->height;
}
}
break;
default:
if (prev_layout == L_TABBED || prev_layout == L_STACKED) {
// recover cached geometry for all non-float children
int i;
for (i = 0; i < parent->children->length; ++i) {
swayc_t *child = parent->children->items[i];
// only recoverer cached geometry if non-zero
if (!wlc_geometry_equals(&child->cached_geometry, &wlc_geometry_zero)) {
child->x = child->cached_geometry.origin.x;
child->y = child->cached_geometry.origin.y;
child->width = child->cached_geometry.size.w;
child->height = child->cached_geometry.size.h;
}
}
} }
break;
}
}
static int update_gap_geometry(swayc_t *container, struct wlc_geometry *g) {
swayc_t *ws = swayc_parent_by_type(container, C_WORKSPACE); swayc_t *ws = swayc_parent_by_type(container, C_WORKSPACE);
swayc_t *op = ws->parent; swayc_t *op = ws->parent;
int gap = container->is_floating ? 0 : swayc_gap(container); int gap = container->is_floating ? 0 : swayc_gap(container);
@ -453,16 +485,63 @@ void update_geometry(swayc_t *container) {
gap -= 1; gap -= 1;
} }
g->origin.x = container->x + gap/2 < op->width ? container->x + gap/2 : op->width-1;
g->origin.y = container->y + gap/2 < op->height ? container->y + gap/2 : op->height-1;
g->size.w = container->width > gap ? container->width - gap : 1;
g->size.h = container->height > gap ? container->height - gap : 1;
if ((!config->edge_gaps && gap > 0) || (config->smart_gaps && ws->children->length == 1)) {
// Remove gap against the workspace edges. Because a pixel is not
// divisable, depending on gap size and the number of siblings our view
// might be at the workspace edge without being exactly so (thus test
// with gap, and align correctly).
if (container->x - gap <= ws->x) {
g->origin.x = ws->x;
g->size.w = container->width - gap/2;
}
if (container->y - gap <= ws->y) {
g->origin.y = ws->y;
g->size.h = container->height - gap/2;
}
if (container->x + container->width + gap >= ws->x + ws->width) {
g->size.w = ws->x + ws->width - g->origin.x;
}
if (container->y + container->height + gap >= ws->y + ws->height) {
g->size.h = ws->y + ws->height - g->origin.y;
}
}
return gap;
}
void update_geometry(swayc_t *container) {
if (container->type != C_VIEW && container->type != C_CONTAINER) {
return;
}
swayc_t *ws = swayc_parent_by_type(container, C_WORKSPACE);
swayc_t *op = ws->parent;
swayc_t *parent = container->parent;
struct wlc_geometry geometry = { struct wlc_geometry geometry = {
.origin = { .origin = {
.x = container->x + gap/2 < op->width ? container->x + gap/2 : op->width-1, .x = container->x < op->width ? container->x : op->width-1,
.y = container->y + gap/2 < op->height ? container->y + gap/2 : op->height-1 .y = container->y < op->height ? container->y : op->height-1
}, },
.size = { .size = {
.w = container->width > gap ? container->width - gap : 1, .w = container->width,
.h = container->height > gap ? container->height - gap : 1, .h = container->height,
} }
}; };
int gap = 0;
// apply inner gaps to non-tabbed/stacked containers
swayc_t *p = swayc_tabbed_stacked_parent(container);
if (p == NULL) {
gap = update_gap_geometry(container, &geometry);
}
if (swayc_is_fullscreen(container)) { if (swayc_is_fullscreen(container)) {
swayc_t *output = swayc_parent_by_type(container, C_OUTPUT); swayc_t *output = swayc_parent_by_type(container, C_OUTPUT);
const struct wlc_size *size = wlc_output_get_resolution(output->handle); const struct wlc_size *size = wlc_output_get_resolution(output->handle);
@ -473,28 +552,7 @@ void update_geometry(swayc_t *container) {
if (op->focused == ws) { if (op->focused == ws) {
wlc_view_bring_to_front(container->handle); wlc_view_bring_to_front(container->handle);
} }
} else if ((!config->edge_gaps && gap > 0) || (config->smart_gaps && ws->children->length == 1)) {
// Remove gap against the workspace edges. Because a pixel is not
// divisable, depending on gap size and the number of siblings our view
// might be at the workspace edge without being exactly so (thus test
// with gap, and align correctly).
if (container->x - gap <= ws->x) {
geometry.origin.x = ws->x;
geometry.size.w = container->width - gap/2;
}
if (container->y - gap <= ws->y) {
geometry.origin.y = ws->y;
geometry.size.h = container->height - gap/2;
}
if (container->x + container->width + gap >= ws->x + ws->width) {
geometry.size.w = ws->x + ws->width - geometry.origin.x;
}
if (container->y + container->height + gap >= ws->y + ws->height) {
geometry.size.h = ws->y + ws->height - geometry.origin.y;
}
}
if (swayc_is_fullscreen(container)) {
container->border_geometry = wlc_geometry_zero; container->border_geometry = wlc_geometry_zero;
container->title_bar_geometry = wlc_geometry_zero; container->title_bar_geometry = wlc_geometry_zero;
} else if (container->is_floating) { // allocate border for floating window } else if (container->is_floating) { // allocate border for floating window
@ -533,6 +591,65 @@ void update_geometry(swayc_t *container) {
} }
} }
int title_bar_height = config->font_height + 4; //borders + padding
if (parent->layout == L_TABBED) {
int i, x = 0, w, l, r;
l = parent->children->length;
w = geometry.size.w / l;
r = geometry.size.w % l;
for (i = 0; i < parent->children->length; ++i) {
swayc_t *view = parent->children->items[i];
if (view == container) {
x = w * i;
if (i == l - 1) {
w += r;
}
break;
}
}
struct wlc_geometry title_bar = {
.origin = {
.x = container->border_geometry.origin.x + x,
.y = container->border_geometry.origin.y
},
.size = {
.w = w,
.h = title_bar_height
}
};
geometry.origin.x += border_left;
geometry.origin.y += title_bar.size.h;
geometry.size.w -= (border_left + border_right);
geometry.size.h -= (border_bottom + title_bar.size.h);
container->title_bar_geometry = title_bar;
} else if (parent->layout == L_STACKED) {
int i, y;
for (i = 0; i < parent->children->length; ++i) {
swayc_t *view = parent->children->items[i];
if (view == container) {
y = title_bar_height * i;
}
}
struct wlc_geometry title_bar = {
.origin = {
.x = container->border_geometry.origin.x,
.y = container->border_geometry.origin.y + y
},
.size = {
.w = container->border_geometry.size.w,
.h = title_bar_height
}
};
title_bar_height = title_bar_height * parent->children->length;
geometry.origin.x += border_left;
geometry.origin.y += title_bar_height;
geometry.size.w -= (border_left + border_right);
geometry.size.h -= (border_bottom + title_bar_height);
container->title_bar_geometry = title_bar;
} else {
switch (container->border_type) { switch (container->border_type) {
case B_NONE: case B_NONE:
break; break;
@ -551,7 +668,7 @@ void update_geometry(swayc_t *container) {
}, },
.size = { .size = {
.w = container->border_geometry.size.w, .w = container->border_geometry.size.w,
.h = config->font_height + 4 // borders + padding .h = title_bar_height
} }
}; };
geometry.origin.x += border_left; geometry.origin.x += border_left;
@ -562,13 +679,18 @@ void update_geometry(swayc_t *container) {
break; break;
} }
} }
}
container->actual_geometry = geometry; container->actual_geometry = geometry;
if (container->type == C_VIEW) {
update_view_border(container); update_view_border(container);
} }
}
if (container->type == C_VIEW) {
wlc_view_set_geometry(container->handle, 0, &geometry); wlc_view_set_geometry(container->handle, 0, &geometry);
}
} }
static void arrange_windows_r(swayc_t *container, double width, double height) { static void arrange_windows_r(swayc_t *container, double width, double height) {
@ -664,6 +786,33 @@ static void arrange_windows_r(swayc_t *container, double width, double height) {
container->height = height; container->height = height;
x = container->x; x = container->x;
y = container->y; y = container->y;
// add gaps to top level tapped/stacked container
if (container->parent->type == C_WORKSPACE &&
(container->layout == L_TABBED || container->layout == L_STACKED)) {
update_geometry(container);
width = container->border_geometry.size.w;
height = container->border_geometry.size.h;
x = container->border_geometry.origin.x;
y = container->border_geometry.origin.y;
}
// update container size if it's a child in a tabbed/stacked layout
if (swayc_tabbed_stacked_parent(container) != NULL) {
// Use parent actual_geometry as a base for calculating
// container geometry
container->width = container->parent->actual_geometry.size.w;
container->height = container->parent->actual_geometry.size.h;
container->x = container->parent->actual_geometry.origin.x;
container->y = container->parent->actual_geometry.origin.y;
update_geometry(container);
width = container->width = container->actual_geometry.size.w;
height = container->height = container->actual_geometry.size.h;
x = container->x = container->actual_geometry.origin.x;
y = container->y = container->actual_geometry.origin.y;
}
break; break;
} }
@ -683,15 +832,22 @@ static void arrange_windows_r(swayc_t *container, double width, double height) {
} }
scale += *old_width; scale += *old_width;
} }
// Resize windows // Resize windows
if (scale > 0.1) { if (scale > 0.1) {
scale = width / scale; scale = width / scale;
sway_log(L_DEBUG, "Arranging %p horizontally", container); sway_log(L_DEBUG, "Arranging %p horizontally", container);
swayc_t *focused = NULL;
for (i = 0; i < container->children->length; ++i) { for (i = 0; i < container->children->length; ++i) {
swayc_t *child = container->children->items[i]; swayc_t *child = container->children->items[i];
sway_log(L_DEBUG, "Calculating arrangement for %p:%d (will scale %f by %f)", child, child->type, width, scale); sway_log(L_DEBUG, "Calculating arrangement for %p:%d (will scale %f by %f)", child, child->type, width, scale);
child->x = x; child->x = x;
child->y = y; child->y = y;
if (child == container->focused) {
focused = child;
}
if (i == container->children->length - 1) { if (i == container->children->length - 1) {
double remaining_width = container->x + width - x; double remaining_width = container->x + width - x;
arrange_windows_r(child, remaining_width, height); arrange_windows_r(child, remaining_width, height);
@ -700,6 +856,12 @@ static void arrange_windows_r(swayc_t *container, double width, double height) {
} }
x += child->width; x += child->width;
} }
// update focused view border last because it may
// depend on the title bar geometry of its siblings.
if (focused && container->children->length > 1) {
update_view_border(focused);
}
} }
break; break;
case L_VERT: case L_VERT:
@ -719,11 +881,17 @@ static void arrange_windows_r(swayc_t *container, double width, double height) {
if (scale > 0.1) { if (scale > 0.1) {
scale = height / scale; scale = height / scale;
sway_log(L_DEBUG, "Arranging %p vertically", container); sway_log(L_DEBUG, "Arranging %p vertically", container);
swayc_t *focused = NULL;
for (i = 0; i < container->children->length; ++i) { for (i = 0; i < container->children->length; ++i) {
swayc_t *child = container->children->items[i]; swayc_t *child = container->children->items[i];
sway_log(L_DEBUG, "Calculating arrangement for %p:%d (will scale %f by %f)", child, child->type, height, scale); sway_log(L_DEBUG, "Calculating arrangement for %p:%d (will scale %f by %f)", child, child->type, height, scale);
child->x = x; child->x = x;
child->y = y; child->y = y;
if (child == container->focused) {
focused = child;
}
if (i == container->children->length - 1) { if (i == container->children->length - 1) {
double remaining_height = container->y + height - y; double remaining_height = container->y + height - y;
arrange_windows_r(child, width, remaining_height); arrange_windows_r(child, width, remaining_height);
@ -732,9 +900,35 @@ static void arrange_windows_r(swayc_t *container, double width, double height) {
} }
y += child->height; y += child->height;
} }
// update focused view border last because it may
// depend on the title bar geometry of its siblings.
if (focused && container->children->length > 1) {
update_view_border(focused);
}
}
break;
case L_TABBED:
case L_STACKED:
{
swayc_t *focused = NULL;
for (i = 0; i < container->children->length; ++i) {
swayc_t *child = container->children->items[i];
child->x = x;
child->y = y;
if (child == container->focused) {
focused = child;
} else {
arrange_windows_r(child, width, height);
}
}
if (focused) {
arrange_windows_r(focused, width, height);
} }
break; break;
} }
}
// Arrage floating layouts for workspaces last // Arrage floating layouts for workspaces last
if (container->type == C_WORKSPACE) { if (container->type == C_WORKSPACE) {
@ -742,6 +936,8 @@ static void arrange_windows_r(swayc_t *container, double width, double height) {
swayc_t *view = container->floating->items[i]; swayc_t *view = container->floating->items[i];
if (view->type == C_VIEW) { if (view->type == C_VIEW) {
update_geometry(view); update_geometry(view);
sway_log(L_DEBUG, "Set floating view to %.f x %.f @ %.f, %.f", view->width,
view->height, view->x, view->y);
if (swayc_is_fullscreen(view)) { if (swayc_is_fullscreen(view)) {
wlc_view_bring_to_front(view->handle); wlc_view_bring_to_front(view->handle);
} else if (!container->focused } else if (!container->focused
@ -840,12 +1036,12 @@ swayc_t *get_swayc_in_direction_under(swayc_t *container, enum movement_directio
return get_swayc_in_output_direction(output, dir); return get_swayc_in_output_direction(output, dir);
} else { } else {
if (dir == MOVE_LEFT || dir == MOVE_RIGHT) { if (dir == MOVE_LEFT || dir == MOVE_RIGHT) {
if (parent->layout == L_HORIZ) { if (parent->layout == L_HORIZ || parent->layout == L_TABBED) {
can_move = true; can_move = true;
diff = dir == MOVE_LEFT ? -1 : 1; diff = dir == MOVE_LEFT ? -1 : 1;
} }
} else { } else {
if (parent->layout == L_VERT) { if (parent->layout == L_VERT || parent->layout == L_STACKED) {
can_move = true; can_move = true;
diff = dir == MOVE_UP ? -1 : 1; diff = dir == MOVE_UP ? -1 : 1;
} }
@ -900,3 +1096,15 @@ void recursive_resize(swayc_t *container, double amount, enum wlc_resize_edge ed
} }
} }
} }
enum swayc_layouts default_layout(swayc_t *output) {
if (config->default_layout != L_NONE) {
return config->default_layout;
} else if (config->default_orientation != L_NONE) {
return config->default_orientation;
} else if (output->width >= output->height) {
return L_HORIZ;
} else {
return L_VERT;
}
}

Loading…
Cancel
Save