Merge pull request #1854 from RyanDwyer/refactor-arrange-windows

Refactor arrange_windows()
This commit is contained in:
emersion 2018-04-29 13:44:37 +01:00 committed by GitHub
commit 78e3bc1329
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
17 changed files with 328 additions and 283 deletions

View file

@ -0,0 +1,20 @@
#ifndef _SWAY_ARRANGE_H
#define _SWAY_ARRANGE_H
struct sway_container;
void arrange_windows(struct sway_container *container);
// Determine the root container's geometry, then iterate to everything below
void arrange_root(void);
// Determine the output's geometry, then iterate to everything below
void arrange_output(struct sway_container *output);
// Determine the workspace's geometry, then iterate to everything below
void arrange_workspace(struct sway_container *workspace);
// Arrange layout for all the children of the given workspace/container
void arrange_children_of(struct sway_container *parent);
#endif

View file

@ -75,6 +75,7 @@ struct sway_container {
double x, y; double x, y;
// does not include borders or gaps. // does not include borders or gaps.
double width, height; double width, height;
double saved_width, saved_height;
list_t *children; list_t *children;

View file

@ -60,9 +60,6 @@ enum sway_container_layout container_get_default_layout(
void container_sort_workspaces(struct sway_container *output); void container_sort_workspaces(struct sway_container *output);
void arrange_windows(struct sway_container *container,
double width, double height);
struct sway_container *container_get_in_direction(struct sway_container struct sway_container *container_get_in_direction(struct sway_container
*container, struct sway_seat *seat, enum movement_direction dir); *container, struct sway_seat *seat, enum movement_direction dir);

View file

@ -162,6 +162,8 @@ void view_configure(struct sway_view *view, double ox, double oy, int width,
void view_set_activated(struct sway_view *view, bool activated); void view_set_activated(struct sway_view *view, bool activated);
void view_set_fullscreen_raw(struct sway_view *view, bool fullscreen);
void view_set_fullscreen(struct sway_view *view, bool fullscreen); void view_set_fullscreen(struct sway_view *view, bool fullscreen);
void view_close(struct sway_view *view); void view_close(struct sway_view *view);

View file

@ -1,8 +1,8 @@
#include <string.h> #include <string.h>
#include <strings.h> #include <strings.h>
#include "sway/commands.h" #include "sway/commands.h"
#include "sway/tree/arrange.h"
#include "sway/tree/container.h" #include "sway/tree/container.h"
#include "sway/tree/layout.h"
#include "log.h" #include "log.h"
struct cmd_results *cmd_layout(int argc, char **argv) { struct cmd_results *cmd_layout(int argc, char **argv) {
@ -48,7 +48,7 @@ struct cmd_results *cmd_layout(int argc, char **argv) {
} }
} }
arrange_windows(parent, parent->width, parent->height); arrange_children_of(parent);
return cmd_results_new(CMD_SUCCESS, NULL, NULL); return cmd_results_new(CMD_SUCCESS, NULL, NULL);
} }

View file

@ -1,6 +1,6 @@
#include "sway/commands.h" #include "sway/commands.h"
#include "sway/config.h" #include "sway/config.h"
#include "sway/tree/layout.h" #include "sway/tree/arrange.h"
struct cmd_results *cmd_reload(int argc, char **argv) { struct cmd_results *cmd_reload(int argc, char **argv) {
struct cmd_results *error = NULL; struct cmd_results *error = NULL;
@ -12,6 +12,6 @@ struct cmd_results *cmd_reload(int argc, char **argv) {
} }
load_swaybars(); load_swaybars();
arrange_windows(&root_container, -1, -1); arrange_root();
return cmd_results_new(CMD_SUCCESS, NULL, NULL); return cmd_results_new(CMD_SUCCESS, NULL, NULL);
} }

View file

@ -6,7 +6,7 @@
#include <strings.h> #include <strings.h>
#include <wlr/util/log.h> #include <wlr/util/log.h>
#include "sway/commands.h" #include "sway/commands.h"
#include "sway/tree/layout.h" #include "sway/tree/arrange.h"
#include "log.h" #include "log.h"
static const int MIN_SANE_W = 100, MIN_SANE_H = 60; static const int MIN_SANE_W = 100, MIN_SANE_H = 60;
@ -182,7 +182,7 @@ static void resize_tiled(int amount, enum resize_axis axis) {
} }
} }
arrange_windows(parent->parent, -1, -1); arrange_children_of(parent->parent);
} }
static void resize(int amount, enum resize_axis axis, enum resize_unit unit) { static void resize(int amount, enum resize_axis axis, enum resize_unit unit) {

View file

@ -1,8 +1,8 @@
#include <string.h> #include <string.h>
#include <strings.h> #include <strings.h>
#include "sway/commands.h" #include "sway/commands.h"
#include "sway/tree/arrange.h"
#include "sway/tree/container.h" #include "sway/tree/container.h"
#include "sway/tree/layout.h"
#include "sway/tree/view.h" #include "sway/tree/view.h"
#include "sway/input/input-manager.h" #include "sway/input/input-manager.h"
#include "sway/input/seat.h" #include "sway/input/seat.h"
@ -12,7 +12,7 @@ static struct cmd_results *do_split(int layout) {
struct sway_container *con = config->handler_context.current_container; struct sway_container *con = config->handler_context.current_container;
struct sway_container *parent = container_split(con, layout); struct sway_container *parent = container_split(con, layout);
container_create_notify(parent); container_create_notify(parent);
arrange_windows(parent, -1, -1); arrange_children_of(parent);
return cmd_results_new(CMD_SUCCESS, NULL, NULL); return cmd_results_new(CMD_SUCCESS, NULL, NULL);
} }

View file

@ -12,6 +12,7 @@
#include "sway/layers.h" #include "sway/layers.h"
#include "sway/output.h" #include "sway/output.h"
#include "sway/server.h" #include "sway/server.h"
#include "sway/tree/arrange.h"
#include "sway/tree/layout.h" #include "sway/tree/layout.h"
#include "log.h" #include "log.h"
@ -175,7 +176,7 @@ void arrange_layers(struct sway_output *output) {
sizeof(struct wlr_box)) != 0) { sizeof(struct wlr_box)) != 0) {
wlr_log(L_DEBUG, "Usable area changed, rearranging output"); wlr_log(L_DEBUG, "Usable area changed, rearranging output");
memcpy(&output->usable_area, &usable_area, sizeof(struct wlr_box)); memcpy(&output->usable_area, &usable_area, sizeof(struct wlr_box));
arrange_windows(output->swayc, -1, -1); arrange_output(output->swayc);
} }
// Arrange non-exlusive surfaces from top->bottom // Arrange non-exlusive surfaces from top->bottom

View file

@ -19,6 +19,7 @@
#include "sway/layers.h" #include "sway/layers.h"
#include "sway/output.h" #include "sway/output.h"
#include "sway/server.h" #include "sway/server.h"
#include "sway/tree/arrange.h"
#include "sway/tree/container.h" #include "sway/tree/container.h"
#include "sway/tree/layout.h" #include "sway/tree/layout.h"
#include "sway/tree/view.h" #include "sway/tree/view.h"
@ -534,19 +535,19 @@ static void handle_destroy(struct wl_listener *listener, void *data) {
static void handle_mode(struct wl_listener *listener, void *data) { static void handle_mode(struct wl_listener *listener, void *data) {
struct sway_output *output = wl_container_of(listener, output, mode); struct sway_output *output = wl_container_of(listener, output, mode);
arrange_layers(output); arrange_layers(output);
arrange_windows(output->swayc, -1, -1); arrange_output(output->swayc);
} }
static void handle_transform(struct wl_listener *listener, void *data) { static void handle_transform(struct wl_listener *listener, void *data) {
struct sway_output *output = wl_container_of(listener, output, transform); struct sway_output *output = wl_container_of(listener, output, transform);
arrange_layers(output); arrange_layers(output);
arrange_windows(output->swayc, -1, -1); arrange_output(output->swayc);
} }
static void handle_scale(struct wl_listener *listener, void *data) { static void handle_scale(struct wl_listener *listener, void *data) {
struct sway_output *output = wl_container_of(listener, output, scale); struct sway_output *output = wl_container_of(listener, output, scale);
arrange_layers(output); arrange_layers(output);
arrange_windows(output->swayc, -1, -1); arrange_output(output->swayc);
} }
void handle_new_output(struct wl_listener *listener, void *data) { void handle_new_output(struct wl_listener *listener, void *data) {
@ -598,5 +599,5 @@ void handle_new_output(struct wl_listener *listener, void *data) {
output->damage_destroy.notify = damage_handle_destroy; output->damage_destroy.notify = damage_handle_destroy;
arrange_layers(output); arrange_layers(output);
arrange_windows(&root_container, -1, -1); arrange_root();
} }

View file

@ -16,6 +16,7 @@
#include "sway/ipc-server.h" #include "sway/ipc-server.h"
#include "sway/layers.h" #include "sway/layers.h"
#include "sway/output.h" #include "sway/output.h"
#include "sway/tree/arrange.h"
#include "sway/tree/container.h" #include "sway/tree/container.h"
#include "sway/tree/view.h" #include "sway/tree/view.h"
#include "sway/tree/workspace.h" #include "sway/tree/workspace.h"

View file

@ -105,6 +105,7 @@ sway_sources = files(
'commands/input/xkb_rules.c', 'commands/input/xkb_rules.c',
'commands/input/xkb_variant.c', 'commands/input/xkb_variant.c',
'tree/arrange.c',
'tree/container.c', 'tree/container.c',
'tree/layout.c', 'tree/layout.c',
'tree/view.c', 'tree/view.c',

239
sway/tree/arrange.c Normal file
View file

@ -0,0 +1,239 @@
#define _POSIX_C_SOURCE 200809L
#include <ctype.h>
#include <stdbool.h>
#include <stdlib.h>
#include <string.h>
#include <wlr/types/wlr_output.h>
#include <wlr/types/wlr_output_layout.h>
#include "sway/debug.h"
#include "sway/tree/arrange.h"
#include "sway/tree/container.h"
#include "sway/tree/layout.h"
#include "sway/output.h"
#include "sway/tree/workspace.h"
#include "sway/tree/view.h"
#include "list.h"
#include "log.h"
struct sway_container root_container;
void arrange_windows(struct sway_container *container) {
switch (container->type) {
case C_ROOT:
arrange_root();
break;
case C_OUTPUT:
arrange_output(container);
break;
case C_WORKSPACE:
arrange_workspace(container);
break;
case C_CONTAINER:
arrange_children_of(container);
break;
case C_VIEW:
// ignore
break;
case C_TYPES:
sway_assert(
false, "Called arrange_windows() with container type C_TYPES");
break;
}
}
void arrange_root() {
if (config->reloading) {
return;
}
struct wlr_output_layout *output_layout =
root_container.sway_root->output_layout;
const struct wlr_box *layout_box =
wlr_output_layout_get_box(output_layout, NULL);
root_container.x = layout_box->x;
root_container.y = layout_box->y;
root_container.width = layout_box->width;
root_container.height = layout_box->height;
for (int i = 0; i < root_container.children->length; ++i) {
struct sway_container *output = root_container.children->items[i];
arrange_output(output);
}
}
void arrange_output(struct sway_container *output) {
if (config->reloading) {
return;
}
if (!sway_assert(output->type == C_OUTPUT,
"called arrange_output() on non-output container")) {
return;
}
const struct wlr_box *output_box = wlr_output_layout_get_box(
root_container.sway_root->output_layout,
output->sway_output->wlr_output);
output->x = output_box->x;
output->y = output_box->y;
output->width = output_box->width;
output->height = output_box->height;
wlr_log(L_DEBUG, "Arranging output '%s' at %f,%f",
output->name, output->x, output->y);
for (int i = 0; i < output->children->length; ++i) {
struct sway_container *workspace = output->children->items[i];
arrange_workspace(workspace);
}
container_damage_whole(output);
}
void arrange_workspace(struct sway_container *workspace) {
if (config->reloading) {
return;
}
if (!sway_assert(workspace->type == C_WORKSPACE,
"called arrange_workspace() on non-workspace container")) {
return;
}
struct sway_container *output = workspace->parent;
struct wlr_box *area = &output->sway_output->usable_area;
wlr_log(L_DEBUG, "Usable area for ws: %dx%d@%d,%d",
area->width, area->height, area->x, area->y);
workspace->width = area->width;
workspace->height = area->height;
workspace->x = area->x;
workspace->y = area->y;
wlr_log(L_DEBUG, "Arranging workspace '%s' at %f, %f",
workspace->name, workspace->x, workspace->y);
arrange_children_of(workspace);
container_damage_whole(workspace);
}
static void apply_horiz_layout(struct sway_container *parent) {
size_t num_children = parent->children->length;
if (!num_children) {
return;
}
// 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];
if (child->width <= 0) {
if (num_children > 1) {
child->width = parent->width / (num_children - 1);
} else {
child->width = parent->width;
}
}
total_width += child->width;
}
double scale = parent->width / total_width;
// Resize windows
wlr_log(L_DEBUG, "Arranging %p horizontally", parent);
double child_x = parent->x;
struct sway_container *child;
for (size_t i = 0; i < num_children; ++i) {
child = parent->children->items[i];
wlr_log(L_DEBUG,
"Calculating arrangement for %p:%d (will scale %f by %f)",
child, child->type, child->width, scale);
child->x = child_x;
child->y = parent->y;
child->width = floor(child->width * scale);
child->height = parent->height;
child_x += child->width;
}
// Make last child use remaining width of parent
child->width = parent->x + parent->width - child->x;
}
static void apply_vert_layout(struct sway_container *parent) {
size_t num_children = parent->children->length;
if (!num_children) {
return;
}
// 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];
if (child->height <= 0) {
if (num_children > 1) {
child->height = parent->height / (num_children - 1);
} else {
child->height = parent->height;
}
}
total_height += child->height;
}
double scale = parent->height / total_height;
// Resize
wlr_log(L_DEBUG, "Arranging %p vertically", parent);
double child_y = parent->y;
struct sway_container *child;
for (size_t i = 0; i < num_children; ++i) {
child = parent->children->items[i];
wlr_log(L_DEBUG,
"Calculating arrangement for %p:%d (will scale %f by %f)",
child, child->type, child->height, scale);
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
child->height = parent->y + parent->height - child->y;
}
void arrange_children_of(struct sway_container *parent) {
if (config->reloading) {
return;
}
if (!sway_assert(parent->type == C_WORKSPACE || parent->type == C_CONTAINER,
"container is a %s", container_type_to_str(parent->type))) {
return;
}
struct sway_container *workspace = parent;
if (workspace->type != C_WORKSPACE) {
workspace = container_parent(workspace, C_WORKSPACE);
}
if (workspace->sway_workspace->fullscreen) {
// Just arrange the fullscreen view and jump out
struct sway_container *view =
workspace->sway_workspace->fullscreen->swayc;
view_configure(view->sway_view, 0, 0,
workspace->parent->width, workspace->parent->height);
view->width = workspace->parent->width;
view->height = workspace->parent->height;
return;
}
wlr_log(L_DEBUG, "Arranging layout for %p %s %fx%f+%f,%f", parent,
parent->name, parent->width, parent->height, parent->x, parent->y);
// Calculate x, y, width and height of children
switch (parent->layout) {
case L_HORIZ:
apply_horiz_layout(parent);
break;
case L_VERT:
apply_vert_layout(parent);
break;
default:
wlr_log(L_DEBUG, "TODO: arrange layout type %d", parent->layout);
apply_horiz_layout(parent);
break;
}
// Apply x, y, width and height to children and recurse if needed
for (int i = 0; i < parent->children->length; ++i) {
struct sway_container *child = parent->children->items[i];
if (child->type == C_VIEW) {
view_configure(child->sway_view, child->x, child->y,
child->width, child->height);
} else {
arrange_children_of(child);
}
}
container_damage_whole(parent);
update_debug_tree();
}

View file

@ -13,6 +13,7 @@
#include "sway/ipc-server.h" #include "sway/ipc-server.h"
#include "sway/output.h" #include "sway/output.h"
#include "sway/server.h" #include "sway/server.h"
#include "sway/tree/arrange.h"
#include "sway/tree/layout.h" #include "sway/tree/layout.h"
#include "sway/tree/view.h" #include "sway/tree/view.h"
#include "sway/tree/workspace.h" #include "sway/tree/workspace.h"
@ -143,8 +144,7 @@ static struct sway_container *container_output_destroy(
container_add_child(root_container.children->items[p], child); container_add_child(root_container.children->items[p], child);
} }
container_sort_workspaces(root_container.children->items[p]); container_sort_workspaces(root_container.children->items[p]);
arrange_windows(root_container.children->items[p], arrange_output(root_container.children->items[p]);
-1, -1);
} }
} }

View file

@ -7,6 +7,7 @@
#include <wlr/types/wlr_output.h> #include <wlr/types/wlr_output.h>
#include <wlr/types/wlr_output_layout.h> #include <wlr/types/wlr_output_layout.h>
#include "sway/debug.h" #include "sway/debug.h"
#include "sway/tree/arrange.h"
#include "sway/tree/container.h" #include "sway/tree/container.h"
#include "sway/tree/layout.h" #include "sway/tree/layout.h"
#include "sway/output.h" #include "sway/output.h"
@ -21,16 +22,7 @@ struct sway_container root_container;
static void output_layout_handle_change(struct wl_listener *listener, static void output_layout_handle_change(struct wl_listener *listener,
void *data) { void *data) {
struct wlr_output_layout *output_layout = arrange_root();
root_container.sway_root->output_layout;
const struct wlr_box *layout_box =
wlr_output_layout_get_box(output_layout, NULL);
root_container.x = layout_box->x;
root_container.y = layout_box->y;
root_container.width = layout_box->width;
root_container.height = layout_box->height;
arrange_windows(&root_container, layout_box->width, layout_box->height);
} }
void layout_init(void) { void layout_init(void) {
@ -91,13 +83,15 @@ static void container_handle_fullscreen_reparent(struct sway_container *viewcon,
// Mark the new workspace as fullscreen // Mark the new workspace as fullscreen
if (new_workspace->sway_workspace->fullscreen) { if (new_workspace->sway_workspace->fullscreen) {
view_set_fullscreen(new_workspace->sway_workspace->fullscreen, false); view_set_fullscreen_raw(
new_workspace->sway_workspace->fullscreen, false);
} }
new_workspace->sway_workspace->fullscreen = view; new_workspace->sway_workspace->fullscreen = view;
// Resize view to new output dimensions // Resize view to new output dimensions
struct sway_output *output = new_workspace->parent->sway_output; struct sway_container *output = new_workspace->parent;
view_configure(view, 0, 0, view_configure(view, 0, 0, output->width, output->height);
output->wlr_output->width, output->wlr_output->height); view->swayc->width = output->width;
view->swayc->height = output->height;
} }
void container_insert_child(struct sway_container *parent, void container_insert_child(struct sway_container *parent,
@ -166,6 +160,7 @@ void container_move_to(struct sway_container *container,
} }
struct sway_container *old_parent = container_remove_child(container); struct sway_container *old_parent = container_remove_child(container);
container->width = container->height = 0; container->width = container->height = 0;
container->saved_width = container->saved_height = 0;
struct sway_container *new_parent; struct sway_container *new_parent;
if (destination->type == C_VIEW) { if (destination->type == C_VIEW) {
new_parent = container_add_sibling(destination, container); new_parent = container_add_sibling(destination, container);
@ -188,9 +183,9 @@ void container_move_to(struct sway_container *container,
seat_set_focus(seat, new_parent); seat_set_focus(seat, new_parent);
} }
if (old_parent) { if (old_parent) {
arrange_windows(old_parent, -1, -1); arrange_children_of(old_parent);
} }
arrange_windows(new_parent, -1, -1); arrange_children_of(new_parent);
// If view was moved to a fullscreen workspace, refocus the fullscreen view // If view was moved to a fullscreen workspace, refocus the fullscreen view
struct sway_container *new_workspace = container; struct sway_container *new_workspace = container;
if (new_workspace->type != C_WORKSPACE) { if (new_workspace->type != C_WORKSPACE) {
@ -299,7 +294,7 @@ static void workspace_rejigger(struct sway_container *ws,
container_reap_empty_recursive(original_parent); container_reap_empty_recursive(original_parent);
wl_signal_emit(&child->events.reparent, original_parent); wl_signal_emit(&child->events.reparent, original_parent);
container_create_notify(new_parent); container_create_notify(new_parent);
arrange_windows(ws, -1, -1); arrange_workspace(ws);
} }
void container_move(struct sway_container *container, void container_move(struct sway_container *container,
@ -378,7 +373,7 @@ void container_move(struct sway_container *container,
move_dir == MOVE_LEFT || move_dir == MOVE_RIGHT ? move_dir == MOVE_LEFT || move_dir == MOVE_RIGHT ?
L_HORIZ : L_VERT; L_HORIZ : L_VERT;
container_insert_child(current, container, offs < 0 ? 0 : 1); container_insert_child(current, container, offs < 0 ? 0 : 1);
arrange_windows(current, -1, -1); arrange_workspace(current);
} }
return; return;
} else { } else {
@ -402,8 +397,8 @@ void container_move(struct sway_container *container,
container_insert_child(current->parent, container, container_insert_child(current->parent, container,
index + (offs < 0 ? 0 : 1)); index + (offs < 0 ? 0 : 1));
container->width = container->height = 0; container->width = container->height = 0;
arrange_windows(current->parent, -1, -1); arrange_children_of(current->parent);
arrange_windows(old_parent, -1, -1); arrange_children_of(old_parent);
return; return;
} }
} else { } else {
@ -433,14 +428,14 @@ void container_move(struct sway_container *container,
wlr_log(L_DEBUG, "Swapping siblings"); wlr_log(L_DEBUG, "Swapping siblings");
sibling->parent->children->items[index + offs] = container; sibling->parent->children->items[index + offs] = container;
sibling->parent->children->items[index] = sibling; sibling->parent->children->items[index] = sibling;
arrange_windows(sibling->parent, -1, -1); arrange_children_of(sibling->parent);
} else { } else {
wlr_log(L_DEBUG, "Promoting to sibling of cousin"); wlr_log(L_DEBUG, "Promoting to sibling of cousin");
container_insert_child(sibling->parent, container, container_insert_child(sibling->parent, container,
index_child(sibling) + (offs > 0 ? 0 : 1)); index_child(sibling) + (offs > 0 ? 0 : 1));
container->width = container->height = 0; container->width = container->height = 0;
arrange_windows(sibling->parent, -1, -1); arrange_children_of(sibling->parent);
arrange_windows(old_parent, -1, -1); arrange_children_of(old_parent);
} }
sibling = NULL; sibling = NULL;
break; break;
@ -454,8 +449,8 @@ void container_move(struct sway_container *container,
"(move dir: %d)", limit, move_dir); "(move dir: %d)", limit, move_dir);
container_insert_child(sibling, container, limit); container_insert_child(sibling, container, limit);
container->width = container->height = 0; container->width = container->height = 0;
arrange_windows(sibling, -1, -1); arrange_children_of(sibling);
arrange_windows(old_parent, -1, -1); arrange_children_of(old_parent);
sibling = NULL; sibling = NULL;
} else { } else {
wlr_log(L_DEBUG, "Reparenting container (perpendicular)"); wlr_log(L_DEBUG, "Reparenting container (perpendicular)");
@ -478,8 +473,8 @@ void container_move(struct sway_container *container,
container_add_child(sibling, container); container_add_child(sibling, container);
} }
container->width = container->height = 0; container->width = container->height = 0;
arrange_windows(sibling, -1, -1); arrange_children_of(sibling);
arrange_windows(old_parent, -1, -1); arrange_children_of(old_parent);
sibling = NULL; sibling = NULL;
} }
break; break;
@ -553,234 +548,6 @@ void container_sort_workspaces(struct sway_container *output) {
list_stable_sort(output->children, sort_workspace_cmp_qsort); list_stable_sort(output->children, sort_workspace_cmp_qsort);
} }
static void apply_horiz_layout(struct sway_container *container, const double x,
const double y, const double width,
const double height, const int start,
const int end);
static void apply_vert_layout(struct sway_container *container, const double x,
const double y, const double width,
const double height, const int start,
const int end);
void arrange_windows(struct sway_container *container,
double width, double height) {
if (config->reloading) {
return;
}
int i;
if (width == -1 || height == -1) {
width = container->width;
height = container->height;
}
// pixels are indivisible. if we don't round the pixels, then the view
// calculations will be off (e.g. 50.5 + 50.5 = 101, but in reality it's
// 50 + 50 = 100). doing it here cascades properly to all width/height/x/y.
width = floor(width);
height = floor(height);
wlr_log(L_DEBUG, "Arranging layout for %p %s %fx%f+%f,%f", container,
container->name, container->width, container->height, container->x,
container->y);
double x = 0, y = 0;
switch (container->type) {
case C_ROOT:
for (i = 0; i < container->children->length; ++i) {
struct sway_container *output = container->children->items[i];
const struct wlr_box *output_box = wlr_output_layout_get_box(
container->sway_root->output_layout,
output->sway_output->wlr_output);
output->x = output_box->x;
output->y = output_box->y;
output->width = output_box->width;
output->height = output_box->height;
wlr_log(L_DEBUG, "Arranging output '%s' at %f,%f",
output->name, output->x, output->y);
arrange_windows(output, output_box->width, output_box->height);
}
return;
case C_OUTPUT:
// arrange all workspaces:
for (i = 0; i < container->children->length; ++i) {
struct sway_container *child = container->children->items[i];
arrange_windows(child, -1, -1);
}
return;
case C_WORKSPACE:
{
struct sway_container *output =
container_parent(container, C_OUTPUT);
struct wlr_box *area = &output->sway_output->usable_area;
wlr_log(L_DEBUG, "Usable area for ws: %dx%d@%d,%d",
area->width, area->height, area->x, area->y);
container->width = width = area->width;
container->height = height = area->height;
container->x = x = area->x;
container->y = y = area->y;
wlr_log(L_DEBUG, "Arranging workspace '%s' at %f, %f",
container->name, container->x, container->y);
if (container->sway_workspace->fullscreen) {
view_configure(container->sway_workspace->fullscreen, 0, 0,
output->width, output->height);
return;
}
}
// children are properly handled below
break;
case C_VIEW:
{
container->width = width;
container->height = height;
view_configure(container->sway_view, container->x, container->y,
container->width, container->height);
wlr_log(L_DEBUG, "Set view to %.f x %.f @ %.f, %.f",
container->width, container->height,
container->x, container->y);
}
return;
default:
container->width = width;
container->height = height;
x = container->x;
y = container->y;
break;
}
switch (container->layout) {
case L_HORIZ:
apply_horiz_layout(container, x, y, width, height, 0,
container->children->length);
break;
case L_VERT:
apply_vert_layout(container, x, y, width, height, 0,
container->children->length);
break;
default:
wlr_log(L_DEBUG, "TODO: arrange layout type %d", container->layout);
apply_horiz_layout(container, x, y, width, height, 0,
container->children->length);
break;
}
container_damage_whole(container);
// TODO: Make this less shitty
update_debug_tree();
}
static void apply_horiz_layout(struct sway_container *container,
const double x, const double y,
const double width, const double height,
const int start, const int end) {
double scale = 0;
// Calculate total width
for (int i = start; i < end; ++i) {
double *old_width =
&((struct sway_container *)container->children->items[i])->width;
if (*old_width <= 0) {
if (end - start > 1) {
*old_width = width / (end - start - 1);
} else {
*old_width = width;
}
}
scale += *old_width;
}
scale = width / scale;
// Resize windows
double child_x = x;
if (scale > 0.1) {
wlr_log(L_DEBUG, "Arranging %p horizontally", container);
for (int i = start; i < end; ++i) {
struct sway_container *child = container->children->items[i];
wlr_log(L_DEBUG,
"Calculating arrangement for %p:%d (will scale %f by %f)",
child, child->type, width, scale);
if (child->type == C_VIEW) {
view_configure(child->sway_view, child_x, y, child->width,
child->height);
} else {
child->x = child_x;
child->y = y;
}
if (i == end - 1) {
double remaining_width = x + width - child_x;
arrange_windows(child, remaining_width, height);
} else {
arrange_windows(child, child->width * scale, height);
}
child_x += child->width;
}
// update focused view border last because it may
// depend on the title bar geometry of its siblings.
/* TODO WLR
if (focused && container->children->length > 1) {
update_container_border(focused);
}
*/
}
}
void apply_vert_layout(struct sway_container *container,
const double x, const double y,
const double width, const double height, const int start,
const int end) {
int i;
double scale = 0;
// Calculate total height
for (i = start; i < end; ++i) {
double *old_height =
&((struct sway_container *)container->children->items[i])->height;
if (*old_height <= 0) {
if (end - start > 1) {
*old_height = height / (end - start - 1);
} else {
*old_height = height;
}
}
scale += *old_height;
}
scale = height / scale;
// Resize
double child_y = y;
if (scale > 0.1) {
wlr_log(L_DEBUG, "Arranging %p vertically", container);
for (i = start; i < end; ++i) {
struct sway_container *child = container->children->items[i];
wlr_log(L_DEBUG,
"Calculating arrangement for %p:%d (will scale %f by %f)",
child, child->type, height, scale);
if (child->type == C_VIEW) {
view_configure(child->sway_view, x, child_y, child->width,
child->height);
} else {
child->x = x;
child->y = child_y;
}
if (i == end - 1) {
double remaining_height = y + height - child_y;
arrange_windows(child, width, remaining_height);
} else {
arrange_windows(child, width, child->height * scale);
}
child_y += child->height;
}
// update focused view border last because it may
// depend on the title bar geometry of its siblings.
/* TODO WLR
if (focused && container->children->length > 1) {
update_container_border(focused);
}
*/
}
}
/** /**
* Get swayc in the direction of newly entered output. * Get swayc in the direction of newly entered output.
*/ */
@ -1026,7 +793,7 @@ struct sway_container *container_split(struct sway_container *child,
// Special case: this just behaves like splitt // Special case: this just behaves like splitt
child->prev_layout = child->layout; child->prev_layout = child->layout;
child->layout = layout; child->layout = layout;
arrange_windows(child, -1, -1); arrange_children_of(child);
return child; return child;
} }

View file

@ -7,6 +7,7 @@
#include "sway/ipc-server.h" #include "sway/ipc-server.h"
#include "sway/output.h" #include "sway/output.h"
#include "sway/input/seat.h" #include "sway/input/seat.h"
#include "sway/tree/arrange.h"
#include "sway/tree/container.h" #include "sway/tree/container.h"
#include "sway/tree/layout.h" #include "sway/tree/layout.h"
#include "sway/tree/view.h" #include "sway/tree/view.h"
@ -78,14 +79,14 @@ void view_set_activated(struct sway_view *view, bool activated) {
} }
} }
void view_set_fullscreen(struct sway_view *view, bool fullscreen) { // Set fullscreen, but without IPC events or arranging windows.
void view_set_fullscreen_raw(struct sway_view *view, bool fullscreen) {
if (view->is_fullscreen == fullscreen) { if (view->is_fullscreen == fullscreen) {
return; return;
} }
struct sway_container *workspace = container_parent(view->swayc, C_WORKSPACE); struct sway_container *workspace =
struct sway_container *container = container_parent(workspace, C_OUTPUT); container_parent(view->swayc, C_WORKSPACE);
struct sway_output *output = container->sway_output;
if (view->impl->set_fullscreen) { if (view->impl->set_fullscreen) {
view->impl->set_fullscreen(view, fullscreen); view->impl->set_fullscreen(view, fullscreen);
@ -98,6 +99,8 @@ void view_set_fullscreen(struct sway_view *view, bool fullscreen) {
view_set_fullscreen(workspace->sway_workspace->fullscreen, false); view_set_fullscreen(workspace->sway_workspace->fullscreen, false);
} }
workspace->sway_workspace->fullscreen = view; workspace->sway_workspace->fullscreen = view;
view->swayc->saved_width = view->swayc->width;
view->swayc->saved_height = view->swayc->height;
struct sway_seat *seat; struct sway_seat *seat;
struct sway_container *focus, *focus_ws; struct sway_container *focus, *focus_ws;
@ -114,11 +117,22 @@ void view_set_fullscreen(struct sway_view *view, bool fullscreen) {
} }
} else { } else {
workspace->sway_workspace->fullscreen = NULL; workspace->sway_workspace->fullscreen = NULL;
view->swayc->width = view->swayc->saved_width;
view->swayc->height = view->swayc->saved_height;
}
}
void view_set_fullscreen(struct sway_view *view, bool fullscreen) {
if (view->is_fullscreen == fullscreen) {
return;
} }
arrange_windows(workspace, -1, -1); view_set_fullscreen_raw(view, fullscreen);
output_damage_whole(output);
struct sway_container *workspace =
container_parent(view->swayc, C_WORKSPACE);
arrange_workspace(workspace);
output_damage_whole(workspace->parent->sway_output);
ipc_event_window(view->swayc, "fullscreen_mode"); ipc_event_window(view->swayc, "fullscreen_mode");
} }
@ -257,7 +271,7 @@ void view_map(struct sway_view *view, struct wlr_surface *wlr_surface) {
wl_signal_add(&view->swayc->events.reparent, &view->container_reparent); wl_signal_add(&view->swayc->events.reparent, &view->container_reparent);
view->container_reparent.notify = view_handle_container_reparent; view->container_reparent.notify = view_handle_container_reparent;
arrange_windows(cont->parent, -1, -1); arrange_children_of(cont->parent);
input_manager_set_focus(input_manager, cont); input_manager_set_focus(input_manager, cont);
view_damage(view, true); view_damage(view, true);
@ -288,7 +302,7 @@ void view_unmap(struct sway_view *view) {
view->swayc = NULL; view->swayc = NULL;
view->surface = NULL; view->surface = NULL;
arrange_windows(parent, -1, -1); arrange_children_of(parent);
} }
void view_update_position(struct sway_view *view, double ox, double oy) { void view_update_position(struct sway_view *view, double ox, double oy) {

View file

@ -9,6 +9,7 @@
#include "sway/input/input-manager.h" #include "sway/input/input-manager.h"
#include "sway/input/seat.h" #include "sway/input/seat.h"
#include "sway/ipc-server.h" #include "sway/ipc-server.h"
#include "sway/tree/arrange.h"
#include "sway/tree/container.h" #include "sway/tree/container.h"
#include "sway/tree/workspace.h" #include "sway/tree/workspace.h"
#include "log.h" #include "log.h"
@ -392,7 +393,7 @@ bool workspace_switch(struct sway_container *workspace) {
} }
seat_set_focus(seat, next); seat_set_focus(seat, next);
struct sway_container *output = container_parent(workspace, C_OUTPUT); struct sway_container *output = container_parent(workspace, C_OUTPUT);
arrange_windows(output, -1, -1); arrange_output(output);
return true; return true;
} }