From bb25194844599bb653a78633c9c09c0c0ff225ee Mon Sep 17 00:00:00 2001
From: Brian Ashworth <bosrsf04@gmail.com>
Date: Mon, 1 Oct 2018 12:56:27 -0400
Subject: Handle border options for gaps

Fixes `hide_edge_borders smart` when gaps are in use.
Implements `hide_edge_borders smart_no_gaps` and `smart_borders
on|no_gaps|off`.

Since `smart_borders on` is equivalent to `hide_edge_borders smart`
and `smart_borders no_gaps` is equivalent to `hide_edge_borders
smart_no_gaps`, I opted to just save the last value set for
`hide_edge_borders` and restore that on `smart_borders off`. This
simplifies the conditions for setting the border.
---
 sway/commands.c                   |  1 +
 sway/commands/hide_edge_borders.c | 16 ++++++----------
 sway/commands/smart_borders.c     | 25 +++++++++++++++++++++++++
 sway/config.c                     |  1 +
 sway/meson.build                  |  1 +
 sway/sway.5.scd                   |  8 +++++++-
 sway/tree/view.c                  | 31 +++++++++++++++++++++++--------
 7 files changed, 64 insertions(+), 19 deletions(-)
 create mode 100644 sway/commands/smart_borders.c

(limited to 'sway')

diff --git a/sway/commands.c b/sway/commands.c
index 03cfebd7..72db8ab9 100644
--- a/sway/commands.c
+++ b/sway/commands.c
@@ -110,6 +110,7 @@ static struct cmd_handler handlers[] = {
 	{ "seat", cmd_seat },
 	{ "set", cmd_set },
 	{ "show_marks", cmd_show_marks },
+	{ "smart_borders", cmd_smart_borders },
 	{ "smart_gaps", cmd_smart_gaps },
 	{ "tiling_drag", cmd_tiling_drag },
 	{ "workspace", cmd_workspace },
diff --git a/sway/commands/hide_edge_borders.c b/sway/commands/hide_edge_borders.c
index 0a5c7f28..ea261fb1 100644
--- a/sway/commands/hide_edge_borders.c
+++ b/sway/commands/hide_edge_borders.c
@@ -1,15 +1,8 @@
 #include "sway/commands.h"
 #include "sway/config.h"
-#include "sway/tree/container.h"
-#include "sway/tree/root.h"
+#include "sway/tree/arrange.h"
 #include "sway/tree/view.h"
 
-static void _configure_view(struct sway_container *con, void *data) {
-	if (con->view) {
-		view_autoconfigure(con->view);
-	}
-}
-
 struct cmd_results *cmd_hide_edge_borders(int argc, char **argv) {
 	struct cmd_results *error = NULL;
 	if ((error = checkarg(argc, "hide_edge_borders", EXPECTED_EQUAL_TO, 1))) {
@@ -26,13 +19,16 @@ struct cmd_results *cmd_hide_edge_borders(int argc, char **argv) {
 		config->hide_edge_borders = E_BOTH;
 	} else if (strcmp(argv[0], "smart") == 0) {
 		config->hide_edge_borders = E_SMART;
+	} else if (strcmp(argv[0], "smart_no_gaps") == 0) {
+		config->hide_edge_borders = E_SMART_NO_GAPS;
 	} else {
 		return cmd_results_new(CMD_INVALID, "hide_edge_borders",
 				"Expected 'hide_edge_borders "
-				"<none|vertical|horizontal|both|smart>'");
+				"<none|vertical|horizontal|both|smart|smart_no_gaps>'");
 	}
+	config->saved_edge_borders = config->hide_edge_borders;
 
-	root_for_each_container(_configure_view, NULL);
+	arrange_root();
 
 	return cmd_results_new(CMD_SUCCESS, NULL, NULL);
 }
diff --git a/sway/commands/smart_borders.c b/sway/commands/smart_borders.c
new file mode 100644
index 00000000..fcb4040e
--- /dev/null
+++ b/sway/commands/smart_borders.c
@@ -0,0 +1,25 @@
+#include "sway/commands.h"
+#include "sway/config.h"
+#include "sway/tree/arrange.h"
+#include "sway/tree/view.h"
+#include "util.h"
+
+struct cmd_results *cmd_smart_borders(int argc, char **argv) {
+	struct cmd_results *error = NULL;
+	if ((error = checkarg(argc, "smart_borders", EXPECTED_EQUAL_TO, 1))) {
+		return error;
+	}
+
+	enum edge_border_types saved = config->hide_edge_borders;
+	if (strcmp(argv[0], "no_gaps") == 0) {
+		config->hide_edge_borders = E_SMART_NO_GAPS;
+	} else {
+		config->hide_edge_borders = parse_boolean(argv[0], true) ?
+			E_SMART : config->saved_edge_borders;
+	}
+	config->saved_edge_borders = saved;
+
+	arrange_root();
+
+	return cmd_results_new(CMD_SUCCESS, NULL, NULL);
+}
diff --git a/sway/config.c b/sway/config.c
index 1e08559d..048b57de 100644
--- a/sway/config.c
+++ b/sway/config.c
@@ -249,6 +249,7 @@ static void config_defaults(struct sway_config *config) {
 	config->border_thickness = 2;
 	config->floating_border_thickness = 2;
 	config->hide_edge_borders = E_NONE;
+	config->saved_edge_borders = E_NONE;
 
 	// border colors
 	set_color(config->border_colors.focused.border, 0x4C7899);
diff --git a/sway/meson.build b/sway/meson.build
index 6eb9a9d7..ec6546a2 100644
--- a/sway/meson.build
+++ b/sway/meson.build
@@ -78,6 +78,7 @@ sway_sources = files(
 	'commands/seat/fallback.c',
 	'commands/set.c',
 	'commands/show_marks.c',
+	'commands/smart_borders.c',
 	'commands/smart_gaps.c',
 	'commands/split.c',
 	'commands/sticky.c',
diff --git a/sway/sway.5.scd b/sway/sway.5.scd
index aa5b38ab..3202d517 100644
--- a/sway/sway.5.scd
+++ b/sway/sway.5.scd
@@ -431,7 +431,7 @@ The default colors are:
 	Changes the _inner_ or _outer_ gaps for either _all_ workspaces or the
 	_current_ workspace.
 
-*hide\_edge\_borders* none|vertical|horizontal|both|smart
+*hide\_edge\_borders* none|vertical|horizontal|both|smart|smart\_no\_gaps
 	Hides window borders adjacent to the screen edges. Default is _none_.
 
 *input* <input\_device> <input-subcommands...>
@@ -456,6 +456,12 @@ The default colors are:
 *kill*
 	Kills (closes) the currently focused container and all of its children.
 
+*smart\_borders* on|no\_gaps|off
+	If smart\_borders are _on_, borders will only be enabled if the workspace
+	only has one visible child (identical to _hide\_edge\_borders_ smart). If
+	smart\_borders is set to _no\_gaps_, borders will only be enabled if the
+	workspace only has one visible child and gaps greater than zero.
+
 *smart\_gaps* on|off
 	If smart\_gaps are _on_ gaps will only be enabled if a workspace has more
 	than one child.
diff --git a/sway/tree/view.c b/sway/tree/view.c
index 3b271159..ca5e6ab0 100644
--- a/sway/tree/view.c
+++ b/sway/tree/view.c
@@ -179,6 +179,17 @@ bool view_is_only_visible(struct sway_view *view) {
 	return only_view;
 }
 
+static bool gaps_to_edge(struct sway_view *view) {
+	struct sway_container *con = view->container;
+	while (con) {
+		if (con->current_gaps > 0) {
+			return true;
+		}
+		con = con->parent;
+	}
+	return view->container->workspace->current_gaps > 0;
+}
+
 void view_autoconfigure(struct sway_view *view) {
 	if (!view->container->workspace) {
 		// Hidden in the scratchpad
@@ -196,23 +207,27 @@ void view_autoconfigure(struct sway_view *view) {
 
 	struct sway_workspace *ws = view->container->workspace;
 	struct sway_container *con = view->container;
-	bool other_views = config->hide_edge_borders == E_SMART ?
-		!view_is_only_visible(view) : false;
+
+	bool smart = config->hide_edge_borders == E_SMART ||
+		config->hide_edge_borders == E_SMART_NO_GAPS;
+	bool other_views = smart && !view_is_only_visible(view);
+	bool no_gaps = config->hide_edge_borders != E_SMART_NO_GAPS
+		|| !gaps_to_edge(view);
 
 	view->border_top = view->border_bottom = true;
 	view->border_left = view->border_right = true;
 	if (config->hide_edge_borders == E_BOTH
 			|| config->hide_edge_borders == E_VERTICAL
-			|| (config->hide_edge_borders == E_SMART && !other_views)) {
-		view->border_left = con->x != ws->x;
-		int right_x = con->x + con->width;
+			|| (smart && !other_views && no_gaps)) {
+		view->border_left = con->x - con->current_gaps != ws->x;
+		int right_x = con->x + con->width + con->current_gaps;
 		view->border_right = right_x != ws->x + ws->width;
 	}
 	if (config->hide_edge_borders == E_BOTH
 			|| config->hide_edge_borders == E_HORIZONTAL
-			|| (config->hide_edge_borders == E_SMART && !other_views)) {
-		view->border_top = con->y != ws->y;
-		int bottom_y = con->y + con->height;
+			|| (smart && !other_views && no_gaps)) {
+		view->border_top = con->y - con->current_gaps != ws->y;
+		int bottom_y = con->y + con->height + con->current_gaps;
 		view->border_bottom = bottom_y != ws->y + ws->height;
 	}
 
-- 
cgit v1.2.3