1#!/usr/local/bin/bash
2
3# a i3-like scratchpad for arbitrary applications.
4#
5# this lets a new monitor called "scratchpad" appear in from the top into the
6# current monitor. There the "scratchpad" will be shown (it will be created if
7# it doesn't exist yet). If the monitor already exists it is scrolled out of
8# the screen and removed again.
9#
10# Warning: this uses much resources because herbstclient is forked for each
11# animation step.
12#
13# If a tag name is supplied, this is used instead of the scratchpad
14
15tag="${1:-scratchpad}"
16hc() { "${herbstclient_command[@]:-herbstclient}" "$@" ;}
17
18mrect=( $(hc monitor_rect "" ) )
19
20width=${mrect[2]}
21height=${mrect[3]}
22
23rect=(
24    $((width/2))
25    $((height/2))
26    $((${mrect[0]}+(width/4)))
27    $((${mrect[1]}+(height/4)))
28)
29
30hc add "$tag"
31
32monitor=scratchpad
33
34exists=false
35if ! hc add_monitor $(printf "%dx%d%+d%+d" "${rect[@]}") \
36                    "$tag" $monitor 2> /dev/null ; then
37    exists=true
38else
39    # remember which monitor was focused previously
40    hc chain \
41        , new_attr string monitors.by-name."$monitor".my_prev_focus \
42        , substitute M monitors.focus.index \
43            set_attr monitors.by-name."$monitor".my_prev_focus M
44fi
45
46show() {
47    hc lock
48    hc raise_monitor "$monitor"
49    hc focus_monitor "$monitor"
50    hc unlock
51    hc lock_tag "$monitor"
52}
53
54hide() {
55    # if q3terminal still is focused, then focus the previously focused monitor
56    # (that mon which was focused when starting q3terminal)
57    hc substitute M monitors.by-name."$monitor".my_prev_focus \
58        and + compare monitors.focus.name = "$monitor" \
59            + focus_monitor M
60    hc remove_monitor "$monitor"
61}
62
63[ $exists = true ] && hide || show
64
65