This commit is contained in:
Nathaniel Symer 2018-05-26 18:09:29 +00:00 committed by GitHub
commit 702e454df5
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
7 changed files with 194 additions and 7 deletions

View file

@ -84,6 +84,10 @@ struct sway_container {
double width, height;
double saved_width, saved_height;
bool has_gaps;
double gaps_inner;
double gaps_outer;
list_t *children;
struct sway_container *parent;

View file

@ -109,6 +109,7 @@ static struct cmd_handler handlers[] = {
{ "font", cmd_font },
{ "for_window", cmd_for_window },
{ "fullscreen", cmd_fullscreen },
{ "gaps", cmd_gaps },
{ "hide_edge_borders", cmd_hide_edge_borders },
{ "include", cmd_include },
{ "input", cmd_input },
@ -117,6 +118,7 @@ static struct cmd_handler handlers[] = {
{ "output", cmd_output },
{ "seat", cmd_seat },
{ "show_marks", cmd_show_marks },
{ "smart_gaps", cmd_smart_gaps },
{ "workspace", cmd_workspace },
{ "workspace_auto_back_and_forth", cmd_ws_auto_back_and_forth },
};

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

@ -0,0 +1,133 @@
#include <string.h>
#include "sway/commands.h"
#include "sway/config.h"
#include "sway/tree/arrange.h"
#include "log.h"
#include "stringop.h"
int read_double(char *str, double *out) {
char *end;
double v = strtod(str, &end);
if (v == -HUGE_VAL || v == HUGE_VAL || strlen(end) > 0) {
*out = 0.0;
return 1;
}
*out = v;
return 0;
}
struct cmd_results *cmd_gaps(int argc, char **argv) {
struct cmd_results *error = NULL;
if ((error = checkarg(argc, "gaps", EXPECTED_AT_LEAST, 1))) {
return error;
}
if (strcmp(argv[0], "edge_gaps") == 0) {
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) {
config->edge_gaps = !(config->edge_gaps);
arrange_root();
} else {
return cmd_results_new(CMD_INVALID, "gaps", "Cannot toggle gaps while not running.");
}
} else {
return cmd_results_new(CMD_INVALID, "gaps", "");
}
} else {
int amount_idx = 0;
char op = '=';
char scope = 'a';
bool inner = true;
if (strcmp(argv[amount_idx], "inner") == 0) {
amount_idx++;
inner = true;
} else if (strcmp(argv[amount_idx], "outer") == 0) {
amount_idx++;
inner = false;
}
if (amount_idx > 0) {
if (strcmp(argv[0], "all") == 0) {
amount_idx++;
scope = 'a';
} else if (strcmp(argv[0], "workspace") == 0) {
amount_idx++;
scope = 'w';
} else if (strcmp(argv[0], "current") == 0) {
amount_idx++;
scope = 'c';
}
if (strcmp(argv[amount_idx], "set") == 0) {
amount_idx++;
op = '=';
} else if (strcmp(argv[amount_idx], "plus") == 0) {
amount_idx++;
op = '+';
} else if (strcmp(argv[amount_idx], "minus") == 0) {
amount_idx++;
op = '-';
}
}
double val;
if (read_double(argv[amount_idx], &val) == 0) {
if (amount_idx == 0) {
config->gaps_inner = val;
config->gaps_outer = val;
arrange_root();
} else {
double total = val;
if (op == '-') {
total = (inner ? config->gaps_inner : config->gaps_outer) - val;
if (total < 0) {
total = 0;
}
} else if (op == '+') {
total = (inner ? config->gaps_inner : config->gaps_inner) + val;
}
if (scope == 'a') {
if (inner) {
config->gaps_inner = total;
} else {
config->gaps_outer = total;
}
arrange_root();
} else if (scope == 'w') {
struct sway_container *workspace =
config->handler_context.current_container;
if (workspace->type != C_WORKSPACE) {
workspace = container_parent(workspace, C_WORKSPACE);
}
workspace->has_gaps = true;
if (inner) {
workspace->gaps_inner = total;
} else {
workspace->gaps_outer = total;
}
arrange_workspace(workspace);
} else if (scope == 'c') {
struct sway_container *container =
config->handler_context.current_container;
container->has_gaps = true;
if (inner) {
container->gaps_inner = total;
} else {
container->gaps_outer = total;
}
arrange_workspace(container_parent(container, C_WORKSPACE));
}
}
}
}
return cmd_results_new(CMD_SUCCESS, NULL, NULL);
}

View file

@ -0,0 +1,27 @@
#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 = NULL;
if ((error = checkarg(argc, "smart_gaps", EXPECTED_AT_LEAST, 1))) {
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

@ -41,6 +41,7 @@ sway_sources = files(
'commands/font.c',
'commands/for_window.c',
'commands/fullscreen.c',
'commands/gaps.c',
'commands/hide_edge_borders.c',
'commands/kill.c',
'commands/mark.c',
@ -61,6 +62,7 @@ sway_sources = files(
'commands/seat/fallback.c',
'commands/set.c',
'commands/show_marks.c',
'commands/smart_gaps.c',
'commands/split.c',
'commands/swaybg_command.c',
'commands/title_format.c',

View file

@ -71,10 +71,18 @@ void arrange_workspace(struct sway_container *workspace) {
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;
if (config->edge_gaps || (config->smart_gaps && workspace->children->length > 1)) {
double gaps = workspace->has_gaps ? workspace->gaps_outer : config->gaps_outer;
workspace->width = area->width - (2 * gaps);
workspace->height = area->height - (2 * gaps);
workspace->x = area->x + gaps;
workspace->y = area->y + gaps;
} else {
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);
@ -116,6 +124,7 @@ static void apply_horiz_layout(struct sway_container *parent) {
struct sway_container *child;
for (size_t i = 0; i < num_children; ++i) {
child = parent->children->items[i];
double gaps = child->has_gaps ? child->gaps_inner : config->gaps_inner;
wlr_log(L_DEBUG,
"Calculating arrangement for %p:%d (will scale %f by %f)",
child, child->type, child->width, scale);
@ -123,7 +132,7 @@ static void apply_horiz_layout(struct sway_container *parent) {
child->y = parent->y + parent_offset;
child->width = floor(child->width * scale);
child->height = parent_height;
child_x += child->width;
child_x += child->width + gaps;
}
// Make last child use remaining width of parent
child->width = parent->x + parent->width - child->x;
@ -141,7 +150,7 @@ 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;
@ -164,6 +173,7 @@ static void apply_vert_layout(struct sway_container *parent) {
struct sway_container *child;
for (size_t i = 0; i < num_children; ++i) {
child = parent->children->items[i];
double gaps = child->has_gaps ? child->gaps_inner : config->gaps_inner;
wlr_log(L_DEBUG,
"Calculating arrangement for %p:%d (will scale %f by %f)",
child, child->type, child->height, scale);
@ -171,7 +181,7 @@ static void apply_vert_layout(struct sway_container *parent) {
child->y = child_y;
child->width = parent->width;
child->height = floor(child->height * scale);
child_y += child->height;
child_y += child->height + gaps;
}
// Make last child use remaining height of parent
child->height = parent->y + parent_offset + parent_height - child->y;
@ -244,6 +254,11 @@ void arrange_children_of(struct sway_container *parent) {
if (child->type == C_VIEW) {
view_autoconfigure(child->sway_view);
} else {
if (parent->has_gaps && !child->has_gaps) {
child->has_gaps = true;
child->gaps_inner = parent->gaps_inner;
child->gaps_outer = parent->gaps_outer;
}
arrange_children_of(child);
}
}

View file

@ -134,6 +134,10 @@ 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;
return c;
}