前言
之前因为一直在用Ubuntu 18或者Ubuntu 20然后发现装了GNOME插件后,电脑在使用过程中,会时不时的卡死(鼠标没问题,键盘输入会有10-20秒的延迟)频率基本是一小时一次,因为这种卡顿会很容易打断思路,所以我重装了系统,从最小化安装开始,什么软件都经过我一手安装了(没有安装GNOME插件)发现没有卡顿的现象了(初步排查是GNOME插件的问题)。
所以就有了这篇用脚本实现窗口平铺的文章。如何你们用插件没有这个BUG那么可以直接下载GNOME- WinTile插件就可以实现了。
正文
创建脚本文件/usr/local/bin/ubuntu_move_windows
,内容如下:
#!/bin/bash
ScriptVersion="1.0"
unset key
unset file
unset directory
wid=$(xwininfo -root | grep Width | awk '{print $2}')
hei=$(xwininfo -root | grep Height | awk '{print $2}')
if [ "$(xrandr 2>/dev/null | grep ' connected' | wc -l )" == "1" ];then
half_wid=$(expr $wid / 2)
half_hei=$(expr $hei / 2)
else
half_wid=$(expr $wid / 4)
half_hei=$(expr $hei / 2)
fi
windwos_name=":ACTIVE:"
main(){
local offset=0
if [ "$second_screen" == "2" ];then
offset=$wid
fi
if [ "$key" == "left" ];then
#置于左半屏幕
wmctrl -r $windwos_name -e 2,$offset,0,$half_wid,$hei
elif [ "$key" == "right" ];then
#置于右半屏幕
wmctrl -r $windwos_name -e 2,$(expr $half_wid + $offset),0,$half_wid,$hei
elif [ "$key" == "u" ];then
#置于左上屏幕
wmctrl -r $windwos_name -e 2,$offset,0,$half_wid,$half_hei
elif [ "$key" == "i" ];then
#置于右上屏幕
wmctrl -r $windwos_name -e 2,$(expr $half_wid + $offset),0,$half_wid,$half_hei
elif [ "$key" == "j" ];then
#置于左下屏幕
wmctrl -r $windwos_name -e 2,$offset,$half_hei,$half_wid,$half_hei
elif [ "$key" == "k" ];then
#置于右下屏幕
wmctrl -r $windwos_name -e 2,$(expr $half_wid + $offset),$half_hei,$half_wid,$half_hei
elif [ "$key" == "top" ];then
#置于上半屏幕
wmctrl -r $windwos_name -e 2,0,0,$wid,$half_hei
elif [ "$key" == "down" ];then
#置于下半屏幕
wmctrl -r $windwos_name -e 2,0,$half_hei,$wid,$half_hei
elif [ "$key" == "center" ];then
#居中屏幕
get_current_windows_size
fi
}
get_current_windows_size(){
windows_wid=$(xwininfo -id $(xdotool getactivewindow) | awk '/Width/ {print $2}')
windows_hei=$(xwininfo -id $(xdotool getactivewindow) | awk '/Height/ {print $2}')
wmctrl -r $windwos_name -e 2,$(expr $(expr $wid - $windows_wid ) / 2),$(expr $(expr $hei - $windows_hei ) / 2),$windows_wid,$windows_hei
}
usage (){
echo "Usage : $(basename "$0") [options] [--] argument-1 argument-2
Options:
-h, --help Display this message
-d, --debug Debug model run
-v, --version Display script version
-p, Screen to be displayed, E.g: -p [1|2] Suitable for multiple monitors
-l, Move windows to Left
-r, Move windows to Right
-u, Move windows to Left-Up
-i, Move windows to Right-Up
-j, Move windows to Left-Down
-k, Move windows to Right-Down"
}
while getopts ":hp:vlruijkctdw:D:-:" opt; do
case "${opt}" in
h) usage && exit 0;;
p) second_screen="${OPTARG}";;
v) echo "$0 -- Version $ScriptVersion"; exit ;;
l) key="left";;
r) key="right";;
u) key="u";;
i) key="i";;
j) key="j";;
k) key="k";;
t) key="top";;
d) key="down";;
c) key="center";;
w) windwos_name="${OPTARG}";;
D) directory=${OPTARG};;
-) case "${OPTARG}" in
help) usage && exit 0;;
version) echo "$0 -- Version $ScriptVersion"; exit ;;
file) file=${!OPTIND}; OPTIND=$((OPTIND+1));;
directory) directory=${!OPTIND}; OPTIND=$((OPTIND+1));;
*) echo "Invalid option: --${OPTARG}" >&2 && exit 1;;
esac;;
:) echo "Option -${OPTARG} requires an argument." >&2 && exit 1;;
*) echo "Invalid option: -${OPTARG}" >&2 && exit 1;;
esac
done
shift $((OPTIND-1))
main "$@"
保存写入后,给这个文件添加执行权限:sudo chmod +x /usr/local/bin/ubuntu_move_windows
。 然后来到设置中添加键盘快捷键-自定义快捷键,如下:
这个快捷键是按照Mac OS里的Magnet
软件来设置的