我想最大化一个应用程序(可以认为是左键单击图标),它位于顶栏部分的右侧,在关机按钮前只有几个图标。我试过使用 xdotool,但没有成功。用这个不能点击顶栏区域,只能点击距离顶部 27 像素以下的区域(顶栏垂直方向为 0-27px)。
现在我想写一个 shell 扩展。你觉得怎么样?
答案1
您想最大化窗口,对吗?
最小化、最大化和全屏显示wmctrl -h
:
-r <WIN> -b <STARG> Change the state of the window. Using this option it's
possible for example to make the window maximized,
minimized or fullscreen. The format of the <STARG>
argument and list of possible states is given below.
这将提示您选择一个要最大化的窗口。然后您可以单击托盘中的图标来切换效果。右键单击以设置按键绑定来切换它,或者退出。
#!/bin/bash
tmp_error() {
yad --skip-taskbar --on-top --window-icon="gtk-error" --title="Write Error" --image="gtk-error" \
--button="Quit":1 \
--text="Unable to write to /tmp\nCheck permissions for $USER."
exit 1
}
export -f tmp_error
error_control() {
# Workaround: Clicking OSK's enter-key will cause crosshair failure.
# wmctrl, xdotool & xprop—all fail with 'onboard' OSK.
yad --skip-taskbar --on-top --window-icon="dialog-question" --title="Retry?" --image="dialog-question" \
--button="Try Again":0 \
--button="Quit":1 \
--text="Failed to focus on the window.\nDo you want to retry?" \
response=$?
if [ $response -eq 0 ]; then
select_window
else
exit 1
fi
}
export -f error_control
about_dialog() {
yad --window-icon="help-faq" --title="About" --on-top --borders=10 --center --skip-taskbar --image="preferences-system-windows" --buttons-layout="center" --text-align=center --text="Maximize Toggle\n" --button=Close!application-exit!:0
}
export -f about_dialog
set_keybinding() {
if [ ! -f ~/.maximize_tray_xkbrc ]; then
current_binding='[none]'
else
current_binding=$(sed '2q;d' ~/.maximize_tray_xkbrc)
fi
remove_button='rm ~/.maximize_tray_xkbrc; echo "\"bash -c 'toggle_gnome'\"" > ~/.maximize_tray_xkbrc && xdotool windowkill window="cat /tmp/xwin_id_xkb" && bash -c set_keybinding'
show_current=$(echo "$current_binding")
yad --window-icon="input-keyboard" --borders="15" --on-top --skip-taskbar --title="Key Binding" \
--text-align="center" --buttons-layout="center" --text="Current Binding\n$show_current\n" \
--separator=" " --item-separator=" " --print-xid="/tmp/xwin_id_xkb" --form \
--field="Hold 1:":CB "Ctrl Alt Shift" \
--field="Hold 2:":CB "Shift Ctrl Alt" \
--field="Action:":entry "T" \
--field=Remove-Binding:BTN "bash -c '$remove_button'" \
--button=Confirm:0 \
--button=Cancel:1 > /tmp/entries
if [ $? -ne 0 ]; then
exit 1
fi
entries=$(cut -d'|' -f1 < /tmp/entries)
f1=$(echo "$entries" | awk '{print $1}')
f2=$(echo "$entries" | awk '{print $2}')
f3=$(echo "$entries" | awk '{print $3}')
key_combo=$(echo "$f1+$f2+$f3" | tr ' ' '+')
if [ ! -f ~/.maximize_tray_xkbrc ]; then
echo "\"bash -c 'toggle_gnome'\"" > ~/.maximize_tray_xkbrc
fi
echo "$key_combo" > ~/.maximize_tray_xkbrc
# Restart xbindkeys
killall xbindkeys
xbindkeys --file ~/.maximize_tray_xkbrc
}
export -f set_keybinding
#!/bin/bash
toggle_effect() {
toggle_xwin_id=$(</tmp/xwin_id)
if xprop -id "$toggle_xwin_id" | grep -q "_NET_WM_STATE(ATOM) .* _NET_WM_STATE_MAXIMIZED_HORZ, _NET_WM_STATE_MAXIMIZED_VERT"; then
wmctrl -i -r "$toggle_xwin_id" -b remove,maximized_vert,maximized_horz
else
wmctrl -i -r "$toggle_xwin_id" -b add,maximized_vert,maximized_horz
fi
}
export -f toggle_effect
select_window() {
xdotool selectwindow > /tmp/xwin_id
if [ $? -ne 0 ]; then
error_control
fi
if [ ! -f /tmp/xwin_id ]; then
tmp_error
fi
toggle_effect
}
export -f select_window
select_window
# Tray Icon & Menu
yad --notification --skip-taskbar --image="starred" \
--command="bash -c toggle_effect" \
--menu=" About !bash -c 'about_dialog' \
| Keybind !bash -c 'set_keybinding' \
| Exit !bash -c 'wmctrl -i -r $(sed '1q;d' /tmp/xwin_id) -b remove,maximized_vert,maximized_horz && killall yad'" \
--text="Toggle Window State"
exit 0
另存为:toggle_maximize.sh
更改模式可执行文件:chmod +x toggle_maximize.sh
用法:./toggle_maximize.sh