-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathkamite-toggle-visibility.sh
executable file
·80 lines (69 loc) · 2.58 KB
/
kamite-toggle-visibility.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
#!/usr/bin/env bash
# Toggles the visibility* of a browser window with an active Kamite tab.
# Supports Xorg (requires `wmctrl` and `xdotool`) and Sway.
# When restoring visibility, changes the size and position of the window to
# those specified below. In Sway, it also moves the window to the workspace
# specified with the SWAY_WORKSPACE_NO variable.
# *In Sway, the hiding is achieved by moving the window to the scratchpad.
# In Xorg, it is achieved by putting the game window specified with the
# ACTIVE_WINDOW_PATTERN variable in front of the browser window.
# For ease of use, it is recommended to bind this command to a side mouse
# button or to a hot corner. Examples of hot corner programs are:
# - waycorner (Wayland; https://github.com/icxes/waycorner - hot edge fork),
# - cornora (Xorg; https://github.com/okitavera/cornora/).
# !!! SET DESIRED WINDOW DIMENSIONS
WINDOW_X=290
WINDOW_Y=0
WINDOW_WIDTH=1100
WINDOW_HEIGHT=940
BROWSER_TITLE_PATTERN="Kamite.*(Firefox|Chrome)"
toggle_for_sway() {
# !!! SET DESIRED WORKSPACE FOR THE BROWSER WINDOW
local WORKSPACE_NO=5
local PROCESS_PATTERN="kamite"
if ! pgrep -f "$PROCESS_PATTERN" &> /dev/null 2>&1; then
exit
fi
local browser_in_scratch
browser_in_scratch=$(
swaymsg -t get_tree \
| jq -r \
'recurse(.nodes[])
| select(.name == "__i3_scratch").floating_nodes[]
| select(.name|test("'"$BROWSER_TITLE_PATTERN"'"))' \
| wc -l
)
local selector
selector="[title=\"$BROWSER_TITLE_PATTERN\"]"
if [[ $browser_in_scratch -gt 0 ]] ; then
swaymsg "$selector resize set width ${WINDOW_WIDTH}px height ${WINDOW_HEIGHT}px"
swaymsg "$selector move position ${WINDOW_X} ${WINDOW_Y}"
swaymsg "$selector move workspace $WORKSPACE_NO"
swaymsg "$selector floating enable"
else
swaymsg "$selector move scratchpad"
fi
}
toggle_for_xorg() {
# This value works for games that are launched inside a Wine desktop, like so:
# wine explorer /desktop=example-name,1366x768 start /exec "/path/game.exe"
local ACTIVE_WINDOW_PATTERN="Wine desktop"
local active_window
active_window=$(xdotool getactivewindow getwindowname)
if [[ "$active_window" == *"$ACTIVE_WINDOW_PATTERN"* ]]; then
local wid
wid=$(wmctrl -l | grep -E "$BROWSER_TITLE_PATTERN" | tail -n1 | cut -d" " -f1)
wmctrl -i -a "$wid"
wmctrl -i -r "$wid" -e 0,${WINDOW_X},${WINDOW_Y},${WINDOW_WIDTH},${WINDOW_HEIGHT}
else
wmctrl -i -a "$(wmctrl -l | grep "$ACTIVE_WINDOW_PATTERN" | cut -d" " -f1)"
fi
}
main() {
if test -n "${WAYLAND_DISPLAY-}"; then
toggle_for_sway
else
toggle_for_xorg
fi
}
main