toggle-term.sh 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. #!/bin/bash
  2. # NOTE: this script is intended to be the target of keybindings
  3. # to alternatively show/hide a running xWindow
  4. # 'hide' corresponds to desktop switching keybinding (always hide)
  5. # otherwise assume "toggle" keybinding (toggle visibility)
  6. [[ "$1" == 'hide' ]] && readonly SHOULD_HIDE=1 || readonly SHOULD_HIDE=0
  7. readonly DEBUG=false
  8. readonly DEBUGVB=false
  9. readonly TOGGLE_WINDOW_NAME='toggle-term'
  10. readonly TOGGLE_WINDOW_PID_FILE=/tmp/toggle_window_pid
  11. DBG_LOG() { ${DEBUG} && echo -n "[TOGGLE_TERM]: $*" ; } ;
  12. DBGVB_LOG() { ${DEBUGVB} && echo -n "[TOGGLE_TERM]: $*" ; } ;
  13. cached_window_pid=$(cat ${TOGGLE_WINDOW_PID_FILE} 2> /dev/null )
  14. toggle_window_pid=$(xdotool search --name "^${TOGGLE_WINDOW_NAME}$")
  15. active_window_pid=$(xdotool getactivewindow )
  16. DBGVB_LOG "cached_window_pid=${cached_window_pid} "
  17. DBGVB_LOG "toggle_window_pid=${toggle_window_pid} "
  18. DBGVB_LOG "active_window_pid=${active_window_pid}\n"
  19. if [[ -z "${toggle_window_pid}" ]] && ((! ${SHOULD_HIDE}))
  20. then DBG_LOG "window does not exist - launching\n"
  21. lxterminal --title="${TOGGLE_WINDOW_NAME}"
  22. which alltray 2> /dev/null && sleep 5 && /code/scripts/bash/alltray-auto
  23. elif [[ "${toggle_window_pid}" != "$cached_window_pid" ]] && ((! ${SHOULD_HIDE}))
  24. then DBG_LOG "window is not visible - mapping\n"
  25. xdotool windowmap ${toggle_window_pid}
  26. xdotool windowactivate ${toggle_window_pid} 2> /dev/null
  27. elif [[ "${toggle_window_pid}" == "${active_window_pid}" ]] || (( ${SHOULD_HIDE} ))
  28. then DBG_LOG "window is visible - unmapping\n"
  29. xdotool windowunmap ${toggle_window_pid}
  30. toggle_window_pid=''
  31. else DBG_LOG "window is visible but blurred - focussing\n"
  32. xdotool windowactivate ${toggle_window_pid} 2> /dev/null
  33. fi
  34. echo ${toggle_window_pid} > ${TOGGLE_WINDOW_PID_FILE}