aboutsummaryrefslogtreecommitdiff
path: root/.scripts/scratchpad
diff options
context:
space:
mode:
authorsrdusr <trevorgray@srdusr.com>2023-10-22 22:50:02 +0200
committersrdusr <trevorgray@srdusr.com>2023-10-22 22:50:02 +0200
commita4f18086ba7ce108ebb041b9b0c52bf522413000 (patch)
tree8d63a98b329f4af486f38db47d0b6049c6e441d8 /.scripts/scratchpad
parentbf45ef759815ad7ac3f6423114a96f4d56d1a2a2 (diff)
parent68a2612c8b737f6770a4d23848fe6409ddce7888 (diff)
downloaddotfiles-a4f18086ba7ce108ebb041b9b0c52bf522413000.tar.gz
dotfiles-a4f18086ba7ce108ebb041b9b0c52bf522413000.zip
Add '.scripts/' from commit 'a4ba0376ba73b1bb295d8bb459a4bf2b063ddca9'
git-subtree-dir: .scripts git-subtree-mainline: 03c0cf66468d5e04c0e3559f9f89b5eec28feb9e git-subtree-split: a4ba0376ba73b1bb295d8bb459a4bf2b063ddca9
Diffstat (limited to '.scripts/scratchpad')
-rwxr-xr-x.scripts/scratchpad64
1 files changed, 64 insertions, 0 deletions
diff --git a/.scripts/scratchpad b/.scripts/scratchpad
new file mode 100755
index 0000000..8a1aea0
--- /dev/null
+++ b/.scripts/scratchpad
@@ -0,0 +1,64 @@
+#!/bin/bash
+
+# Created By: srdusr
+# Created On: Tue 07 Mar 2023 15:06:47 PM CAT
+# Project: Agnostic scratchpad/dropdown terminal that works on most window managers
+
+# Dependencies: wmctrl, xprop, xdo, xdotool
+# NOTE: Ensure script is included in system's path and can therefore be invoked with the command 'scratchpad'.
+# Furthermore make sure the terminal is using x11 as a backend in wayland to allow this to work.
+# Example: wezterm.lua: enable_wayland = false,
+# kitty.conf: linux_display_server x11
+
+# Set the environment variables to x11 to allow working in Wayland
+export GDK_BACKEND=x11
+export QT_QPA_PLATFORM=xcb
+export WAYLAND_DISPLAY=""
+export WINIT_UNIX_BACKEND=x11
+
+# Supported terminals and dropdown class
+supported_terminals=("wezterm" "kitty" "alacritty")
+
+# Check if any supported terminal with scratchpad class is running
+for term in "${supported_terminals[@]}"; do
+ if pgrep -f "$term.*--class scratchpad" >/dev/null; then
+ my_term="$term"
+ break
+ fi
+done
+
+# If no supported terminal is running, start the first available one
+if [ "$my_term" = "" ]; then
+ for term in "${supported_terminals[@]}"; do
+ if command -v "$term" >/dev/null 2>&1; then
+ my_term="$term"
+ break
+ fi
+ done
+ if [ "$my_term" = "" ]; then
+ echo "No supported terminal found." && exit 1
+ fi
+
+ # Start terminal with scratchpad class
+ case "$my_term" in
+ "wezterm") wezterm start --class scratchpad -e tmux new-session -A -s tmux -e bash & ;;
+ "kitty") kitty --class scratchpad tmux new-session -A -s tmux -e bash & ;;
+ "alacritty") alacritty --class scratchpad -e tmux new-session -A -s tmux -e bash & ;;
+ esac
+fi
+
+# Get the window ID of the scratchpad terminal
+id="$(xdo id -N scratchpad)"
+
+# Toggle scratchpad terminal visibility
+if [ "$id" != "" ]; then
+ if xwininfo -id "$id" | grep "Map State: IsViewable" >/dev/null; then
+ # Scratchpad is visible, hide it
+ dimensions="$(xwininfo -id "$id" | awk '/Width:|Height:/ { printf("%s=%s;", tolower($1), $2) }')"
+ xdo hide "$id" 2>/dev/null
+ else
+ # Scratchpad is hidden, show it and restore dimensions
+ xdo show "$id"
+ xdotool windowsize "$id" "$(echo "$dimensions" | tr ';' ' ')" 2>/dev/null
+ fi
+fi