shortcut/windowmanagement.sh

109 lines
2.9 KiB
Bash
Raw Normal View History

#!/bin/bash
# 2022 R. Branten
# ----
2025-05-26 13:17:09 +02:00
# This script manages window positioning and sizing on X11 systems
# It allows positioning windows in different preset layouts using xdotool and wmctrl
# Requires xdpyinfo, xdotool and wmctrl to be installed
# ----
2025-05-26 13:17:09 +02:00
# check if xdpyinfo is installed
if ! command -v xdpyinfo &> /dev/null
then
notify-send WindowManagement: "xdpyinfo not installed"
exit 1
fi
# check if xdotool is installed
if ! command -v xdotool &> /dev/null
then
notify-send WindowManagement: "xdotool not installed"
2025-05-26 13:17:09 +02:00
exit 1
fi
# check if wmctrl is installed
if ! command -v wmctrl &> /dev/null
then
notify-send WindowManagement: "wmctrl not installed"
exit 1
fi
2025-05-26 13:17:09 +02:00
# Function to get usable screen height (screen height minus panel height)
get_usable_height() {
local screen_height=$(xdpyinfo | awk '/dimensions/{print $2}' | grep -oE [[:digit:]]\+$)
local panel_height=$(xdotool getwindowgeometry $(xdotool search --onlyvisible --class 'panel') | awk '/Geometry/{print $2}' | grep -oE [[:digit:]]\+$)
2025-09-05 13:44:57 +02:00
local margin=26 # For window decoration subtract margin
echo $((screen_height - panel_height - margin))
2025-05-26 13:17:09 +02:00
}
# extract and set argument
case $1 in
2025-05-26 13:17:09 +02:00
center)
# center window, unused as native window size is used
WIDTH=100%
2025-05-26 13:17:09 +02:00
XPOS=50%
MESSAGE="center"
;;
2025-05-26 13:17:09 +02:00
center50)
# set window to 50% of screen and place in center of screen
WIDTH=50%
XPOS=25%
2025-05-26 13:17:09 +02:00
MESSAGE="50% center"
;;
2025-05-26 13:17:09 +02:00
left25)
# put a window to 25% of screen and place in left side of screen
WIDTH=25%
XPOS=0%
2025-05-26 13:17:09 +02:00
MESSAGE="25% left"
;;
2025-05-26 13:17:09 +02:00
right25)
# put a window to 25% of screen and place in right side of screen
WIDTH=25%
XPOS=75%
2025-05-26 13:17:09 +02:00
MESSAGE="25% right"
;;
2025-05-26 13:17:09 +02:00
left50)
# put a window to 50% of screen and place in left side of screen
WIDTH=50%
XPOS=0%
2025-05-26 13:17:09 +02:00
MESSAGE="50% left"
;;
2025-05-26 13:17:09 +02:00
right50)
# put a window to 50% of screen and place in right side of screen
WIDTH=50%
XPOS=50%
2025-05-26 13:17:09 +02:00
MESSAGE="50% right"
;;
2025-05-26 13:17:09 +02:00
centerleft25)
# 25% window size left side of center screen
WIDTH=25%
XPOS=25%
2025-05-26 13:17:09 +02:00
MESSAGE="25% left from center"
;;
2025-05-26 13:17:09 +02:00
centerright25)
# 25% window size right side of center screen
WIDTH=25%
XPOS=50%
2025-05-26 13:17:09 +02:00
MESSAGE="25% right from center"
;;
*)
2025-05-26 13:17:09 +02:00
echo "usage: $0 [center|center50|left25|right25|left50|right50|centerleft25|centerright25]"
exit
;;
esac
2025-05-26 13:17:09 +02:00
# get current window
currentWindow=$(xdotool getactivewindow)
# undo maximized
2025-09-05 13:44:57 +02:00
wmctrl -ir "$currentWindow" -b remove,maximized_vert,maximized_horz
2025-05-26 13:17:09 +02:00
2025-09-05 13:44:57 +02:00
# resize
xdotool getactivewindow windowsize $WIDTH "$(get_usable_height)"
2025-05-26 13:17:09 +02:00
# move window to location steer clear of top
xdotool getactivewindow windowmove $XPOS 0.1
# display alert window
2025-05-26 13:17:09 +02:00
notify-send WindowManagement: "${MESSAGE}"