summary refs log tree commit diff
diff options
context:
space:
mode:
-rw-r--r--.config/sway/binds.conf4
-rwxr-xr-xbin/centercon40
2 files changed, 44 insertions, 0 deletions
diff --git a/.config/sway/binds.conf b/.config/sway/binds.conf
index 6a9120f..7ad9f2d 100644
--- a/.config/sway/binds.conf
+++ b/.config/sway/binds.conf
@@ -132,6 +132,10 @@ mode "layout" {
     bindsym t sticky toggle; mode "default"; exec notify-send "container sticky'd"
     bindsym Shift+t floating enable; sticky enable; resize set 853; move position 1695 px -300 px; mode "default"
 
+    # make a centered container
+    bindsym c exec centercon; mode "default"
+    bindsym Shift+c exec centercon 0.5; mode "default"
+
     # return to default mode
     bindsym Return mode "default"
     bindsym Escape mode "default"
diff --git a/bin/centercon b/bin/centercon
new file mode 100755
index 0000000..e9cd189
--- /dev/null
+++ b/bin/centercon
@@ -0,0 +1,40 @@
+#!/bin/sh
+
+if [ "$1" = "-h" ]; then
+	printf "usage: %s [RESIZE_PERC]\n" "$(basename "$0")"
+	exit 2
+fi
+
+con=$(swq current con)
+if [ "$(echo "$con" | jq -r '.type')" = "con" ]; then
+	printf "not a floating container\n"
+	exit 1
+fi
+
+ws=$(swq current workspace)
+ws_width=$(echo "$ws" | jq -r '.rect.width')
+ws_height=$(echo "$ws" | jq -r '.rect.height')
+ws_x=$(echo "$ws" | jq -r '.rect.x')
+ws_y=$(echo "$ws" | jq -r '.rect.y')
+
+center_x=$(awk "BEGIN{print int($ws_width - $ws_x)/2}")
+center_y=$(awk "BEGIN{print int($ws_height - $ws_y)/2}")
+
+if [ -n "$1" ]; then
+	perc="$1"
+	con_width=$(awk "BEGIN{print int($ws_width * $perc)}")
+	con_height=$(awk "BEGIN{print int($ws_height * $perc)}")
+	swaymsg resize set "$con_width" "$con_height"
+else
+	con_width=$(echo "$con" | jq -r '.window_rect.width')
+	con_height=$(echo "$con" | jq -r '.window_rect.height')
+fi
+
+echo "center: $center_x x $center_y"
+echo "con: $con_width x $con_height"
+
+tar_x=$(awk "BEGIN{print int($center_x - ($con_width / 2))}")
+tar_y=$(awk "BEGIN{print int($center_y - ($con_height / 2))}")
+
+echo "moveto: $tar_x x $tar_y"
+swaymsg move position "$tar_x" "$tar_y"