Skip to content

Commit

Permalink
Prepare arrange code for type safe arguments
Browse files Browse the repository at this point in the history
This commit changes the arrange code in a way that will support type
safe arguments.

The arrange_output et al functions are now public, however I opted not
to use them directly yet. I've kept the generic arrange_windows there
for convenience until type safety is fully implemented. This means this
patch has much less risk of breaking things as it would otherwise.

To be type safe, arrange_children_of cannot exist in its previous form
because the thing passed to it could be either a workspace or a
container. So it's now renamed to arrange_children and accepts a list_t,
as well as the parent layout and parent's box.

There was some code which checked the grandparent's layout to see if it
was tabbed or stacked and adjusted the Y offset of the grandchild
accordingly. Accessing the grandparent layout isn't easy when using type
safe arguments, and it seemed odd to even need to do this. I determined
that this was needed because a child of a tabbed container would have a
swayc Y matching the top of the tab bar. I've changed this so a child of
a tabbed container will have a swayc Y matching the bottom of the tab
bar, which means we don't need to access the grandparent layout.  Some
tweaks to the rendering and autoconfigure code have been made to
implement this, and the container_at code appears to work without
needing any changes.

arrange_children_of (now arrange_children) would check if the parent had
gaps and would copy them to the child, effectively making the
workspace's gaps recurse into all children. We can't do this any more
without passing has_gaps, gaps_inner and gaps_outer as arguments to
arrange_children, so I've changed the add_gaps function to retrieve it
from the workspace directly.

apply_tabbed_or_stacked_layout has been split into two functions, as it
had different logic depending on the layout.

Lastly, arrange.h had an unnecessary include of transaction.h. I've
removed it, which means I've had to add it to several other files.
  • Loading branch information
RyanDwyer committed Aug 26, 2018
1 parent 1a30c50 commit 0d5dc48
Show file tree
Hide file tree
Showing 8 changed files with 120 additions and 136 deletions.
12 changes: 8 additions & 4 deletions include/sway/tree/arrange.h
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
#ifndef _SWAY_ARRANGE_H
#define _SWAY_ARRANGE_H
#include "sway/desktop/transaction.h"

struct sway_container;

/**
* Arrange layout for all the children of the given container.
*/
void arrange_container(struct sway_container *container);

void arrange_workspace(struct sway_container *workspace);

void arrange_output(struct sway_container *output);

void arrange_root(void);

void arrange_windows(struct sway_container *container);

#endif
1 change: 1 addition & 0 deletions sway/desktop/output.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
#include "log.h"
#include "config.h"
#include "sway/config.h"
#include "sway/desktop/transaction.h"
#include "sway/input/input-manager.h"
#include "sway/input/seat.h"
#include "sway/layers.h"
Expand Down
8 changes: 4 additions & 4 deletions sway/desktop/render.c
Original file line number Diff line number Diff line change
Expand Up @@ -660,7 +660,7 @@ static void render_container_tabbed(struct sway_output *output,
pstate->swayc_width - width_gap_adjustment - tab_width * i;
}

render_titlebar(output, damage, child, x, cstate->swayc_y, tab_width,
render_titlebar(output, damage, child, x, pstate->swayc_y, tab_width,
colors, title_texture, marks_texture);

if (child == current) {
Expand Down Expand Up @@ -721,9 +721,9 @@ static void render_container_stacked(struct sway_output *output,
marks_texture = view ? view->marks_unfocused : NULL;
}

int y = cstate->swayc_y + titlebar_height * i;
render_titlebar(output, damage, child, cstate->swayc_x, y,
cstate->swayc_width, colors, title_texture, marks_texture);
int y = pstate->swayc_y + titlebar_height * i;
render_titlebar(output, damage, child, pstate->swayc_x, y,
pstate->swayc_width, colors, title_texture, marks_texture);

if (child == current) {
current_colors = colors;
Expand Down
1 change: 1 addition & 0 deletions sway/desktop/xdg_shell.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include "log.h"
#include "sway/decoration.h"
#include "sway/desktop.h"
#include "sway/desktop/transaction.h"
#include "sway/input/input-manager.h"
#include "sway/input/seat.h"
#include "sway/server.h"
Expand Down
1 change: 1 addition & 0 deletions sway/desktop/xdg_shell_v6.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include "log.h"
#include "sway/decoration.h"
#include "sway/desktop.h"
#include "sway/desktop/transaction.h"
#include "sway/input/input-manager.h"
#include "sway/input/seat.h"
#include "sway/server.h"
Expand Down
197 changes: 90 additions & 107 deletions sway/tree/arrange.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,27 +14,18 @@
#include "list.h"
#include "log.h"

static void apply_horiz_layout(struct sway_container *parent) {
size_t num_children = parent->children->length;
if (!num_children) {
static void apply_horiz_layout(list_t *children, struct wlr_box *parent) {
if (!children->length) {
return;
}
size_t parent_offset = 0;
if (parent->parent->layout == L_TABBED) {
parent_offset = container_titlebar_height();
} else if (parent->parent->layout == L_STACKED) {
parent_offset = container_titlebar_height() *
parent->parent->children->length;
}
size_t parent_height = parent->height - parent_offset;

// Calculate total width of children
double total_width = 0;
for (size_t i = 0; i < num_children; ++i) {
struct sway_container *child = parent->children->items[i];
for (int i = 0; i < children->length; ++i) {
struct sway_container *child = children->items[i];
if (child->width <= 0) {
if (num_children > 1) {
child->width = parent->width / (num_children - 1);
if (children->length > 1) {
child->width = parent->width / (children->length - 1);
} else {
child->width = parent->width;
}
Expand All @@ -47,92 +38,70 @@ static void apply_horiz_layout(struct sway_container *parent) {
// Resize windows
wlr_log(WLR_DEBUG, "Arranging %p horizontally", parent);
double child_x = parent->x;
for (size_t i = 0; i < num_children; ++i) {
struct sway_container *child = parent->children->items[i];
wlr_log(WLR_DEBUG,
"Calculating arrangement for %p:%d (will scale %f by %f)",
child, child->type, child->width, scale);
for (int i = 0; i < children->length; ++i) {
struct sway_container *child = children->items[i];
child->x = child_x;
child->y = parent->y + parent_offset;
child->y = parent->y;
child->width = floor(child->width * scale);
child->height = parent_height;
child->height = parent->height;
child_x += child->width;

// Make last child use remaining width of parent
if (i == num_children - 1) {
if (i == children->length - 1) {
child->width = parent->x + parent->width - child->x;
}
container_add_gaps(child);
}
}

static void apply_vert_layout(struct sway_container *parent) {
size_t num_children = parent->children->length;
if (!num_children) {
static void apply_vert_layout(list_t *children, struct wlr_box *parent) {
if (!children->length) {
return;
}
size_t parent_offset = 0;
if (parent->parent->layout == L_TABBED) {
parent_offset = container_titlebar_height();
} else if (parent->parent->layout == L_STACKED) {
parent_offset =
container_titlebar_height() * parent->parent->children->length;
}
size_t parent_height = parent->height + parent_offset;

// Calculate total height of children
double total_height = 0;
for (size_t i = 0; i < num_children; ++i) {
struct sway_container *child = parent->children->items[i];
for (int i = 0; i < children->length; ++i) {
struct sway_container *child = children->items[i];
if (child->height <= 0) {
if (num_children > 1) {
child->height = parent_height / (num_children - 1);
if (children->length > 1) {
child->height = parent->height / (children->length - 1);
} else {
child->height = parent_height;
child->height = parent->height;
}
}
container_remove_gaps(child);
total_height += child->height;
}
double scale = parent_height / total_height;
double scale = parent->height / total_height;

// Resize
wlr_log(WLR_DEBUG, "Arranging %p vertically", parent);
double child_y = parent->y + parent_offset;
for (size_t i = 0; i < num_children; ++i) {
struct sway_container *child = parent->children->items[i];
wlr_log(WLR_DEBUG,
"Calculating arrangement for %p:%d (will scale %f by %f)",
child, child->type, child->height, scale);
double child_y = parent->y;
for (int i = 0; i < children->length; ++i) {
struct sway_container *child = children->items[i];
child->x = parent->x;
child->y = child_y;
child->width = parent->width;
child->height = floor(child->height * scale);
child_y += child->height;

// Make last child use remaining height of parent
if (i == num_children - 1) {
child->height =
parent->y + parent_offset + parent_height - child->y;
if (i == children->length - 1) {
child->height = parent->y + parent->height - child->y;
}
container_add_gaps(child);
}
}

static void apply_tabbed_or_stacked_layout(struct sway_container *parent) {
if (!parent->children->length) {
static void apply_tabbed_layout(list_t *children, struct wlr_box *parent) {
if (!children->length) {
return;
}
size_t parent_offset = 0;
if (parent->parent->layout == L_TABBED) {
parent_offset = container_titlebar_height();
} else if (parent->parent->layout == L_STACKED) {
parent_offset =
container_titlebar_height() * parent->parent->children->length;
}
size_t parent_offset = container_titlebar_height();
size_t parent_height = parent->height - parent_offset;
for (int i = 0; i < parent->children->length; ++i) {
struct sway_container *child = parent->children->items[i];
for (int i = 0; i < children->length; ++i) {
struct sway_container *child = children->items[i];
container_remove_gaps(child);
child->x = parent->x;
child->y = parent->y + parent_offset;
Expand All @@ -142,65 +111,83 @@ static void apply_tabbed_or_stacked_layout(struct sway_container *parent) {
}
}

static void arrange_children_of(struct sway_container *parent);
static void apply_stacked_layout(list_t *children, struct wlr_box *parent) {
if (!children->length) {
return;
}
size_t parent_offset = container_titlebar_height() * children->length;
size_t parent_height = parent->height - parent_offset;
for (int i = 0; i < children->length; ++i) {
struct sway_container *child = children->items[i];
container_remove_gaps(child);
child->x = parent->x;
child->y = parent->y + parent_offset;
child->width = parent->width;
child->height = parent_height;
container_add_gaps(child);
}
}

static void arrange_floating(list_t *floating) {
for (int i = 0; i < floating->length; ++i) {
struct sway_container *floater = floating->items[i];
if (floater->type == C_VIEW) {
view_autoconfigure(floater->sway_view);
} else {
arrange_children_of(floater);
}
container_set_dirty(floater);
arrange_container(floater);
}
}

static void arrange_children_of(struct sway_container *parent) {
if (config->reloading) {
return;
}
wlr_log(WLR_DEBUG, "Arranging layout for %p %s %fx%f+%f,%f", parent,
parent->name, parent->width, parent->height, parent->x, parent->y);

static void arrange_children(list_t *children,
enum sway_container_layout layout, struct wlr_box *parent) {
// Calculate x, y, width and height of children
switch (parent->layout) {
switch (layout) {
case L_HORIZ:
apply_horiz_layout(parent);
apply_horiz_layout(children, parent);
break;
case L_VERT:
apply_vert_layout(parent);
apply_vert_layout(children, parent);
break;
case L_TABBED:
apply_tabbed_layout(children, parent);
break;
case L_STACKED:
apply_tabbed_or_stacked_layout(parent);
apply_stacked_layout(children, parent);
break;
case L_NONE:
apply_horiz_layout(parent);
apply_horiz_layout(children, parent);
break;
}

// Recurse into child containers
for (int i = 0; i < parent->children->length; ++i) {
struct sway_container *child = parent->children->items[i];
if (parent->has_gaps && !child->has_gaps) {
child->has_gaps = true;
child->gaps_inner = parent->gaps_inner;
child->gaps_outer = parent->gaps_outer;
}
if (child->type == C_VIEW) {
view_autoconfigure(child->sway_view);
} else {
arrange_children_of(child);
}
container_set_dirty(child);
for (int i = 0; i < children->length; ++i) {
struct sway_container *child = children->items[i];
arrange_container(child);
}
}

void arrange_container(struct sway_container *container) {
if (config->reloading) {
return;
}
if (container->type == C_VIEW) {
view_autoconfigure(container->sway_view);
container_set_dirty(container);
return;
}
if (!sway_assert(container->type == C_CONTAINER, "Expected a container")) {
return;
}
struct wlr_box box;
container_get_box(container, &box);
arrange_children(container->children, container->layout, &box);
container_set_dirty(container);
}

static void arrange_workspace(struct sway_container *workspace) {
void arrange_workspace(struct sway_container *workspace) {
if (config->reloading) {
return;
}
if (!sway_assert(workspace->type == C_WORKSPACE, "Expected a workspace")) {
return;
}
struct sway_container *output = workspace->parent;
struct wlr_box *area = &output->sway_output->usable_area;
wlr_log(WLR_DEBUG, "Usable area for ws: %dx%d@%d,%d",
Expand Down Expand Up @@ -240,22 +227,22 @@ static void arrange_workspace(struct sway_container *workspace) {
fs->y = workspace->parent->y;
fs->width = workspace->parent->width;
fs->height = workspace->parent->height;
if (fs->type == C_VIEW) {
view_autoconfigure(fs->sway_view);
} else {
arrange_children_of(fs);
}
container_set_dirty(fs);
arrange_container(fs);
} else {
struct wlr_box box;
container_get_box(workspace, &box);
arrange_children(workspace->children, workspace->layout, &box);
arrange_floating(workspace->sway_workspace->floating);
arrange_children_of(workspace);
}
}

static void arrange_output(struct sway_container *output) {
void arrange_output(struct sway_container *output) {
if (config->reloading) {
return;
}
if (!sway_assert(output->type == C_OUTPUT, "Expected an output")) {
return;
}
const struct wlr_box *output_box = wlr_output_layout_get_box(
root_container.sway_root->output_layout,
output->sway_output->wlr_output);
Expand All @@ -272,7 +259,7 @@ static void arrange_output(struct sway_container *output) {
}
}

static void arrange_root() {
void arrange_root(void) {
if (config->reloading) {
return;
}
Expand Down Expand Up @@ -303,12 +290,8 @@ void arrange_windows(struct sway_container *container) {
arrange_workspace(container);
break;
case C_CONTAINER:
arrange_children_of(container);
container_set_dirty(container);
break;
case C_VIEW:
view_autoconfigure(container->sway_view);
container_set_dirty(container);
arrange_container(container);
break;
case C_TYPES:
break;
Expand Down
1 change: 1 addition & 0 deletions sway/tree/root.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include <stdlib.h>
#include <string.h>
#include <wlr/types/wlr_output_layout.h>
#include "sway/desktop/transaction.h"
#include "sway/input/seat.h"
#include "sway/output.h"
#include "sway/tree/arrange.h"
Expand Down
Loading

0 comments on commit 0d5dc48

Please sign in to comment.