diff options
Diffstat (limited to 'sway/layout.c')
-rw-r--r-- | sway/layout.c | 271 |
1 files changed, 207 insertions, 64 deletions
diff --git a/sway/layout.c b/sway/layout.c index e2ea46a7..a48f15c4 100644 --- a/sway/layout.c +++ b/sway/layout.c @@ -1,11 +1,13 @@ #include <stdlib.h> #include <stdbool.h> #include <wlc/wlc.h> -#include "list.h" -#include "log.h" #include "layout.h" +#include "log.h" +#include "list.h" +#include "config.h" #include "container.h" #include "workspace.h" +#include "focus.h" swayc_t root_container; @@ -31,6 +33,21 @@ void add_child(swayc_t *parent, swayc_t *child) { child->width, child->height, parent, parent->type, parent->width, parent->height); list_add(parent->children, child); child->parent = parent; + // set focus for this container + if (parent->children->length == 1) { + set_focused_container_for(parent, child); + } +} + +void add_floating(swayc_t *ws, swayc_t *child) { + sway_log(L_DEBUG, "Adding %p (%d, %dx%d) to %p (%d, %dx%d)", child, child->type, + child->width, child->height, ws, ws->type, ws->width, ws->height); + list_add(ws->floating, child); + child->parent = ws; + child->is_floating = true; + if (!ws->focused) { + set_focused_container_for(ws, child); + } } swayc_t *add_sibling(swayc_t *sibling, swayc_t *child) { @@ -54,7 +71,7 @@ swayc_t *replace_child(swayc_t *child, swayc_t *new_child) { new_child->parent = child->parent; if (child->parent->focused == child) { - child->parent->focused = new_child; + set_focused_container_for(child->parent, new_child); } child->parent = NULL; return parent; @@ -71,6 +88,7 @@ swayc_t *remove_child(swayc_t *child) { break; } } + i = 0; } else { for (i = 0; i < parent->children->length; ++i) { if (parent->children->items[i] == child) { @@ -79,9 +97,10 @@ swayc_t *remove_child(swayc_t *child) { } } } + // Set focused to new container if (parent->focused == child) { if (parent->children->length > 0) { - parent->focused = parent->children->items[i?i-1:0]; + set_focused_container_for(parent, parent->children->items[i?i-1:0]); } else { parent->focused = NULL; } @@ -89,6 +108,50 @@ swayc_t *remove_child(swayc_t *child) { return parent; } +void move_container(swayc_t *container,swayc_t* root,int direction){ + sway_log(L_DEBUG, "Moved window"); + swayc_t *temp; + int i; + uint clength = root->children->length; + //Rearrange + for (i = 0; i < clength; ++i) { + swayc_t *child = root->children->items[i]; + if(child->handle == container->handle){ + if(clength == 1){ + //Only one container, meh. + break; + } + //TODO: Implement horizontal movement. + //TODO: Implement move to a different workspace. + if(direction == MOVE_LEFT && i > 0){ + temp = root->children->items[i-1]; + root->children->items[i] = temp; + root->children->items[i-1] = container; + arrange_windows(&root_container,-1,-1); + } + else if(direction == MOVE_RIGHT && i < clength-1){ + temp = root->children->items[i+1]; + root->children->items[i] = temp; + root->children->items[i+1] = container; + arrange_windows(&root_container,-1,-1); + + } + else if(direction == MOVE_UP){ + sway_log(L_INFO, "Moving up not implemented"); + } + else if(direction == MOVE_DOWN){ + sway_log(L_INFO, "Moving down not implemented"); + } + + break; + } + else if(child->children != NULL){ + move_container(container,child,direction); + } + } + +} + void arrange_windows(swayc_t *container, int width, int height) { int i; @@ -121,11 +184,11 @@ void arrange_windows(swayc_t *container, int width, int height) { // y -= container->y; for (i = 0; i < container->children->length; ++i) { swayc_t *child = container->children->items[i]; - sway_log(L_DEBUG, "Arranging workspace #%d", i); - child->x = x; - child->y = y; - child->width = width; - child->height = height; + child->x = x + container->gaps; + child->y = y + container->gaps; + child->width = width - container->gaps * 2; + child->height = height - container->gaps * 2; + sway_log(L_DEBUG, "Arranging workspace #%d at %d, %d", i, child->x, child->y); arrange_windows(child, -1, -1); } return; @@ -133,27 +196,24 @@ void arrange_windows(swayc_t *container, int width, int height) { { struct wlc_geometry geometry = { .origin = { - .x = container->x, - .y = container->y + .x = container->x + container->gaps / 2, + .y = container->y + container->gaps / 2 }, .size = { - .w = width, - .h = height + .w = width - container->gaps, + .h = height - container->gaps } }; if (wlc_view_get_state(container->handle) & WLC_BIT_FULLSCREEN) { - swayc_t *parent = container; - while (parent->type != C_OUTPUT) { - parent = parent->parent; - } + swayc_t *parent = swayc_parent_by_type(container, C_OUTPUT); geometry.origin.x = 0; geometry.origin.y = 0; geometry.size.w = parent->width; geometry.size.h = parent->height; - wlc_view_set_geometry(container->handle, &geometry); + wlc_view_set_geometry(container->handle, 0, &geometry); wlc_view_bring_to_front(container->handle); } else { - wlc_view_set_geometry(container->handle, &geometry); + wlc_view_set_geometry(container->handle, 0, &geometry); container->width = width; container->height = height; } @@ -167,40 +227,62 @@ void arrange_windows(swayc_t *container, int width, int height) { break; } - double total_weight = 0; - for (i = 0; i < container->children->length; ++i) { - swayc_t *child = container->children->items[i]; - total_weight += child->weight; - } - + x = y = 0; + double scale = 0; switch (container->layout) { case L_HORIZ: default: - sway_log(L_DEBUG, "Arranging %p horizontally", container); + // Calculate total width for (i = 0; i < container->children->length; ++i) { - swayc_t *child = container->children->items[i]; - double percent = child->weight / total_weight; - sway_log(L_DEBUG, "Calculating arrangement for %p:%d (will receive %.2f of %d)", child, child->type, percent, width); - child->x = x + container->x; - child->y = y + container->y; - int w = width * percent; - int h = height; - arrange_windows(child, w, h); - x += w; + int *old_width = &((swayc_t *)container->children->items[i])->width; + if (*old_width <= 0) { + if (container->children->length > 1) { + *old_width = width / (container->children->length - 1); + } else { + *old_width = width; + } + } + scale += *old_width; + } + // Resize windows + if (scale > 0.1) { + scale = width / scale; + sway_log(L_DEBUG, "Arranging %p horizontally", container); + for (i = 0; i < container->children->length; ++i) { + swayc_t *child = container->children->items[i]; + sway_log(L_DEBUG, "Calculating arrangement for %p:%d (will scale %d by %f)", child, child->type, width, scale); + child->x = x + container->x; + child->y = y + container->y; + arrange_windows(child, child->width * scale, height); + x += child->width; + } } break; case L_VERT: - sway_log(L_DEBUG, "Arranging %p vertically", container); + // Calculate total height for (i = 0; i < container->children->length; ++i) { - swayc_t *child = container->children->items[i]; - double percent = child->weight / total_weight; - sway_log(L_DEBUG, "Calculating arrangement for %p:%d (will receive %.2f of %d)", child, child->type, percent, width); - child->x = x + container->x; - child->y = y + container->y; - int w = width; - int h = height * percent; - arrange_windows(child, w, h); - y += h; + int *old_height = &((swayc_t *)container->children->items[i])->height; + if (*old_height <= 0) { + if (container->children->length > 1) { + *old_height = height / (container->children->length - 1); + } else { + *old_height = height; + } + } + scale += *old_height; + } + // Resize + if (scale > 0.1) { + scale = height / scale; + sway_log(L_DEBUG, "Arranging %p vertically", container); + for (i = 0; i < container->children->length; ++i) { + swayc_t *child = container->children->items[i]; + sway_log(L_DEBUG, "Calculating arrangement for %p:%d (will scale %d by %f)", child, child->type, height, scale); + child->x = x + container->x; + child->y = y + container->y; + arrange_windows(child, width, child->height * scale); + y += child->height; + } } break; } @@ -209,26 +291,37 @@ void arrange_windows(swayc_t *container, int width, int height) { if (container->type == C_WORKSPACE) { for (i = 0; i < container->floating->length; ++i) { swayc_t *view = container->floating->items[i]; - // Set the geometry - struct wlc_geometry geometry = { - .origin = { - .x = view->x, - .y = view->y - }, - .size = { - .w = view->width, - .h = view->height + if (view->type == C_VIEW) { + // Set the geometry + struct wlc_geometry geometry = { + .origin = { + .x = view->x, + .y = view->y + }, + .size = { + .w = view->width, + .h = view->height + } + }; + if (wlc_view_get_state(view->handle) & WLC_BIT_FULLSCREEN) { + swayc_t *parent = swayc_parent_by_type(view, C_OUTPUT); + geometry.origin.x = 0; + geometry.origin.y = 0; + geometry.size.w = parent->width; + geometry.size.h = parent->height; + wlc_view_set_geometry(view->handle, 0, &geometry); + wlc_view_bring_to_front(view->handle); + } else { + wlc_view_set_geometry(view->handle, 0, &geometry); + // Bring the views to the front in order of the list, the list + // will be kept up to date so that more recently focused views + // have higher indexes + // This is conditional on there not being a fullscreen view in the workspace + if (!container->focused + || !(wlc_view_get_state(container->focused->handle) & WLC_BIT_FULLSCREEN)) { + wlc_view_bring_to_front(view->handle); + } } - }; - wlc_view_set_geometry(view->handle, &geometry); - - // Bring the views to the front in order of the list, the list - // will be kept up to date so that more recently focused views - // have higher indexes - // This is conditional on there not being a fullscreen view in the workspace - if (!container->focused - || !(wlc_view_get_state(container->focused->handle) & WLC_BIT_FULLSCREEN)) { - wlc_view_bring_to_front(view->handle); } } } @@ -265,3 +358,53 @@ swayc_t *get_swayc_for_handle(wlc_handle handle, swayc_t *parent) { return NULL; } +swayc_t *get_swayc_in_direction(swayc_t *container, enum movement_direction dir) { + swayc_t *parent = container->parent; + + if (dir == MOVE_PARENT) { + if (parent->type == C_OUTPUT) { + return NULL; + } else { + return parent; + } + } + while (true) { + // Test if we can even make a difference here + bool can_move = false; + int diff = 0; + if (dir == MOVE_LEFT || dir == MOVE_RIGHT) { + if (parent->layout == L_HORIZ || parent->type == C_ROOT) { + can_move = true; + diff = dir == MOVE_LEFT ? -1 : 1; + } + } else { + if (parent->layout == L_VERT) { + can_move = true; + diff = dir == MOVE_UP ? -1 : 1; + } + } + if (can_move) { + int i; + for (i = 0; i < parent->children->length; ++i) { + swayc_t *child = parent->children->items[i]; + if (child == container) { + break; + } + } + int desired = i + diff; + if (desired < 0 || desired >= parent->children->length) { + can_move = false; + } else { + return parent->children->items[desired]; + } + } + if (!can_move) { + container = parent; + parent = parent->parent; + if (!parent) { + // Nothing we can do + return NULL; + } + } + } +} |