Skip to content

Commit

Permalink
Merge pull request #2047 from natesymer/master
Browse files Browse the repository at this point in the history
Implement Gaps
  • Loading branch information
ddevault authored Jun 9, 2018
2 parents 21d98d5 + 6a910b9 commit d9fc381
Show file tree
Hide file tree
Showing 9 changed files with 320 additions and 9 deletions.
6 changes: 6 additions & 0 deletions include/sway/tree/arrange.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
7 changes: 7 additions & 0 deletions include/sway/tree/container.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
3 changes: 3 additions & 0 deletions sway/commands.c
Original file line number Diff line number Diff line change
Expand Up @@ -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 },
Expand All @@ -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 },
};
Expand Down
183 changes: 183 additions & 0 deletions sway/commands/gaps.c
Original file line number Diff line number Diff line change
@@ -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);
}
28 changes: 28 additions & 0 deletions sway/commands/smart_gaps.c
Original file line number Diff line number Diff line change
@@ -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);
}
2 changes: 2 additions & 0 deletions sway/meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand All @@ -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',
Expand Down
Loading

0 comments on commit d9fc381

Please sign in to comment.