Implement gaps (PR #2047)

This commit is contained in:
Nate Symer 2018-06-09 09:34:56 -04:00
parent 21d98d5bfb
commit 6a910b9ba5
9 changed files with 320 additions and 9 deletions

View file

@ -3,6 +3,12 @@
struct sway_container;
// Remove gaps around container
void remove_gaps(struct sway_container *c);
// Add gaps around container
void add_gaps(struct sway_container *c);
// Determine the root container's geometry, then iterate to everything below
void arrange_root(void);

View file

@ -86,6 +86,13 @@ struct sway_container {
double saved_x, saved_y;
double saved_width, saved_height;
// The gaps currently applied to the container.
double current_gaps;
bool has_gaps;
double gaps_inner;
double gaps_outer;
list_t *children;
struct sway_container *parent;

View file

@ -105,6 +105,8 @@ static struct cmd_handler handlers[] = {
{ "font", cmd_font },
{ "for_window", cmd_for_window },
{ "force_focus_wrapping", cmd_force_focus_wrapping },
{ "fullscreen", cmd_fullscreen },
{ "gaps", cmd_gaps },
{ "hide_edge_borders", cmd_hide_edge_borders },
{ "include", cmd_include },
{ "input", cmd_input },
@ -114,6 +116,7 @@ static struct cmd_handler handlers[] = {
{ "seat", cmd_seat },
{ "set", cmd_set },
{ "show_marks", cmd_show_marks },
{ "smart_gaps", cmd_smart_gaps },
{ "workspace", cmd_workspace },
{ "workspace_auto_back_and_forth", cmd_ws_auto_back_and_forth },
};

183
sway/commands/gaps.c Normal file
View file

@ -0,0 +1,183 @@
#include <string.h>
#include "sway/commands.h"
#include "sway/config.h"
#include "sway/tree/arrange.h"
#include "log.h"
#include "stringop.h"
#include <math.h>
enum gaps_op {
GAPS_OP_SET,
GAPS_OP_ADD,
GAPS_OP_SUBTRACT
};
enum gaps_scope {
GAPS_SCOPE_ALL,
GAPS_SCOPE_WORKSPACE,
GAPS_SCOPE_CURRENT
};
struct cmd_results *cmd_gaps(int argc, char **argv) {
struct cmd_results *error = checkarg(argc, "gaps", EXPECTED_AT_LEAST, 1);
if (error) {
return error;
}
if (strcmp(argv[0], "edge_gaps") == 0) {
if ((error = checkarg(argc, "gaps", EXPECTED_AT_LEAST, 2))) {
return error;
}
if (strcmp(argv[1], "on") == 0) {
config->edge_gaps = true;
arrange_root();
} else if (strcmp(argv[1], "off") == 0) {
config->edge_gaps = false;
arrange_root();
} else if (strcmp(argv[1], "toggle") == 0) {
if (!config->active) {
return cmd_results_new(CMD_INVALID, "gaps",
"Cannot toggle gaps while not running.");
}
config->edge_gaps = !config->edge_gaps;
arrange_root();
} else {
return cmd_results_new(CMD_INVALID, "gaps",
"gaps edge_gaps on|off|toggle");
}
} else {
int amount_idx = 0; // the current index in argv
enum gaps_op op = GAPS_OP_SET;
enum gaps_scope scope = GAPS_SCOPE_ALL;
bool inner = true;
if (strcmp(argv[0], "inner") == 0) {
amount_idx++;
inner = true;
} else if (strcmp(argv[0], "outer") == 0) {
amount_idx++;
inner = false;
}
// If one of the long variants of the gaps command is used
// (which starts with inner|outer) check the number of args
if (amount_idx > 0) { // if we've seen inner|outer
if (argc > 2) { // check the longest variant
error = checkarg(argc, "gaps", EXPECTED_EQUAL_TO, 4);
if (error) {
return error;
}
} else { // check the next longest format
error = checkarg(argc, "gaps", EXPECTED_EQUAL_TO, 2);
if (error) {
return error;
}
}
} else {
error = checkarg(argc, "gaps", EXPECTED_EQUAL_TO, 1);
if (error) {
return error;
}
}
if (argc == 4) {
// Long format: all|workspace|current.
if (strcmp(argv[amount_idx], "all") == 0) {
amount_idx++;
scope = GAPS_SCOPE_ALL;
} else if (strcmp(argv[amount_idx], "workspace") == 0) {
amount_idx++;
scope = GAPS_SCOPE_WORKSPACE;
} else if (strcmp(argv[amount_idx], "current") == 0) {
amount_idx++;
scope = GAPS_SCOPE_CURRENT;
}
// Long format: set|plus|minus
if (strcmp(argv[amount_idx], "set") == 0) {
amount_idx++;
op = GAPS_OP_SET;
} else if (strcmp(argv[amount_idx], "plus") == 0) {
amount_idx++;
op = GAPS_OP_ADD;
} else if (strcmp(argv[amount_idx], "minus") == 0) {
amount_idx++;
op = GAPS_OP_SUBTRACT;
}
}
char *end;
double val = strtod(argv[amount_idx], &end);
if (strlen(end) && val == 0.0) { // invalid <amount>
// guess which variant of the command was attempted
if (argc == 1) {
return cmd_results_new(CMD_INVALID, "gaps", "gaps <amount>");
}
if (argc == 2) {
return cmd_results_new(CMD_INVALID, "gaps",
"gaps inner|outer <amount>");
}
return cmd_results_new(CMD_INVALID, "gaps",
"gaps inner|outer all|workspace|current set|plus|minus <amount>");
}
if (amount_idx == 0) { // gaps <amount>
config->gaps_inner = val;
config->gaps_outer = val;
arrange_root();
return cmd_results_new(CMD_SUCCESS, NULL, NULL);
}
// Other variants. The middle-length variant (gaps inner|outer <amount>)
// just defaults the scope to "all" and defaults the op to "set".
double total;
switch (op) {
case GAPS_OP_SUBTRACT: {
total = (inner ? config->gaps_inner : config->gaps_outer) - val;
if (total < 0) {
total = 0;
}
break;
}
case GAPS_OP_ADD: {
total = (inner ? config->gaps_inner : config->gaps_outer) + val;
break;
}
case GAPS_OP_SET: {
total = val;
break;
}
}
if (scope == GAPS_SCOPE_ALL) {
if (inner) {
config->gaps_inner = total;
} else {
config->gaps_outer = total;
}
arrange_root();
} else {
struct sway_container *c =
config->handler_context.current_container;
if (scope == GAPS_SCOPE_WORKSPACE && c->type != C_WORKSPACE) {
c = container_parent(c, C_WORKSPACE);
}
c->has_gaps = true;
if (inner) {
c->gaps_inner = total;
} else {
c->gaps_outer = total;
}
if (c->parent) {
arrange_children_of(c->parent);
} else {
arrange_root();
}
}
}
return cmd_results_new(CMD_SUCCESS, NULL, NULL);
}

View file

@ -0,0 +1,28 @@
#include <string.h>
#include "sway/commands.h"
#include "sway/config.h"
#include "sway/tree/arrange.h"
#include "sway/tree/view.h"
#include "sway/tree/container.h"
#include "log.h"
#include "stringop.h"
struct cmd_results *cmd_smart_gaps(int argc, char **argv) {
struct cmd_results *error = checkarg(argc, "smart_gaps", EXPECTED_AT_LEAST, 1);
if (error) {
return error;
}
if (strcmp(argv[0], "on") == 0) {
config->smart_gaps = true;
arrange_root();
} else if (strcmp(argv[0], "off") == 0) {
config->smart_gaps = false;
arrange_root();
} else {
return cmd_results_new(CMD_INVALID, "smart_gaps",
"Expected 'smart_gaps <on|off>' ");
}
return cmd_results_new(CMD_SUCCESS, NULL, NULL);
}

View file

@ -44,6 +44,7 @@ sway_sources = files(
'commands/for_window.c',
'commands/force_focus_wrapping.c',
'commands/fullscreen.c',
'commands/gaps.c',
'commands/hide_edge_borders.c',
'commands/kill.c',
'commands/mark.c',
@ -64,6 +65,7 @@ sway_sources = files(
'commands/seat/fallback.c',
'commands/set.c',
'commands/show_marks.c',
'commands/smart_gaps.c',
'commands/split.c',
'commands/sticky.c',
'commands/swaybg_command.c',

View file

@ -43,6 +43,7 @@ void arrange_output(struct sway_container *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);
@ -52,6 +53,7 @@ void arrange_output(struct sway_container *output) {
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);
@ -67,18 +69,62 @@ void arrange_workspace(struct sway_container *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);
remove_gaps(workspace);
workspace->width = area->width;
workspace->height = area->height;
workspace->x = output->x + area->x;
workspace->y = output->y + area->y;
add_gaps(workspace);
wlr_log(L_DEBUG, "Arranging workspace '%s' at %f, %f",
workspace->name, workspace->x, workspace->y);
arrange_children_of(workspace);
container_damage_whole(workspace);
}
void remove_gaps(struct sway_container *c) {
if (c->current_gaps == 0) {
wlr_log(L_DEBUG, "Removing gaps: not gapped: %p", c);
return;
}
c->width += c->current_gaps * 2;
c->height += c->current_gaps * 2;
c->x -= c->current_gaps;
c->y -= c->current_gaps;
c->current_gaps = 0;
wlr_log(L_DEBUG, "Removing gaps %p", c);
}
void add_gaps(struct sway_container *c) {
if (c->current_gaps > 0 || c->type == C_CONTAINER) {
wlr_log(L_DEBUG, "Not adding gaps: %p", c);
return;
}
if (c->type == C_WORKSPACE &&
!(config->edge_gaps || (config->smart_gaps && c->children->length > 1))) {
return;
}
double gaps = c->has_gaps ? c->gaps_inner : config->gaps_inner;
c->x += gaps;
c->y += gaps;
c->width -= 2 * gaps;
c->height -= 2 * gaps;
c->current_gaps = gaps;
wlr_log(L_DEBUG, "Adding gaps: %p", c);
}
static void apply_horiz_layout(struct sway_container *parent) {
@ -99,6 +145,7 @@ static void apply_horiz_layout(struct sway_container *parent) {
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);
@ -106,6 +153,7 @@ static void apply_horiz_layout(struct sway_container *parent) {
child->width = parent->width;
}
}
remove_gaps(child);
total_width += child->width;
}
double scale = parent->width / total_width;
@ -121,12 +169,19 @@ static void apply_horiz_layout(struct sway_container *parent) {
child, child->type, child->width, scale);
child->x = child_x;
child->y = parent->y + parent_offset;
child->width = floor(child->width * scale);
child->height = parent_height;
if (i == num_children - 1) {
// Make last child use remaining width of parent
child->width = parent->x + parent->width - child->x;
} else {
child->width = floor(child->width * scale);
}
child_x += child->width;
add_gaps(child);
}
// 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) {
@ -141,12 +196,13 @@ static void apply_vert_layout(struct sway_container *parent) {
parent_offset =
container_titlebar_height() * parent->parent->children->length;
}
size_t parent_height = parent->height - parent_offset;
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];
if (child->height <= 0) {
if (num_children > 1) {
child->height = parent_height / (num_children - 1);
@ -154,6 +210,7 @@ static void apply_vert_layout(struct sway_container *parent) {
child->height = parent_height;
}
}
remove_gaps(child);
total_height += child->height;
}
double scale = parent_height / total_height;
@ -170,11 +227,18 @@ static void apply_vert_layout(struct sway_container *parent) {
child->x = parent->x;
child->y = child_y;
child->width = parent->width;
child->height = floor(child->height * scale);
if (i == num_children - 1) {
// Make last child use remaining height of parent
child->height = parent->y + parent_offset + parent_height - child->y;
} else {
child->height = floor(child->height * scale);
}
child_y += child->height;
add_gaps(child);
}
// Make last child use remaining height of parent
child->height = parent->y + parent_offset + parent_height - child->y;
}
static void apply_tabbed_or_stacked_layout(struct sway_container *parent) {
@ -191,10 +255,12 @@ static void apply_tabbed_or_stacked_layout(struct sway_container *parent) {
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];
remove_gaps(child);
child->x = parent->x;
child->y = parent->y + parent_offset;
child->width = parent->width;
child->height = parent_height;
add_gaps(child);
}
}
@ -211,6 +277,7 @@ void arrange_children_of(struct sway_container *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
view_autoconfigure(workspace->sway_workspace->fullscreen);
@ -241,6 +308,11 @@ void arrange_children_of(struct sway_container *parent) {
// 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 (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 {

View file

@ -124,6 +124,11 @@ struct sway_container *container_create(enum sway_container_type type) {
wl_signal_add(&c->events.reparent, &c->reparent);
c->reparent.notify = handle_reparent;
c->has_gaps = false;
c->gaps_inner = 0;
c->gaps_outer = 0;
c->current_gaps = 0;
return c;
}

View file

@ -871,6 +871,8 @@ struct sway_container *container_split(struct sway_container *child,
wlr_log(L_DEBUG, "creating container %p around %p", cont, child);
remove_gaps(child);
cont->prev_layout = L_NONE;
cont->width = child->width;
cont->height = child->height;
@ -879,6 +881,9 @@ struct sway_container *container_split(struct sway_container *child,
struct sway_seat *seat = input_manager_get_default_seat(input_manager);
bool set_focus = (seat_get_focus(seat) == child);
add_gaps(cont);
if (child->type == C_WORKSPACE) {
struct sway_container *workspace = child;
while (workspace->children->length) {
@ -906,7 +911,7 @@ struct sway_container *container_split(struct sway_container *child,
}
container_notify_subtree_changed(cont);
arrange_children_of(cont);
return cont;
}