diff --git a/utils/cheatsheet_viewer.sh b/utils/cheatsheet_viewer.sh index e9cd58547aedc214e19f718c25c4e5e17349af13..c8365d1f3d6c5382db14fd08b4c41a488905fb36 100755 --- a/utils/cheatsheet_viewer.sh +++ b/utils/cheatsheet_viewer.sh @@ -110,6 +110,12 @@ toggle [PROFILE] $PROGRAM accepts the following options: + -o + Display cheatsheets in "overlay" mode, keeping the current + window in focus, and making the cheatsheet window + always-on-top. Useful when playing games and similar. The + cheatsheet window can still be focused using keyboard and + mouse-clicks. -q Quiet mode. -d @@ -245,16 +251,21 @@ function command_info() { # $2 (profile) # Profile name. # +# $3 (overlay_mode) +# Specify if cheatsheets should be shown in overlay mode. +# # Returns: # 0 on success, 1 otherwise. # function command_display() { local config_dir="$1" local profile="$2" + local overlay_mode="$3" local profile_dir="$config_dir/profiles/$profile" local cheatsheets_file="$profile_dir/cheatsheets" + local caller_window_id local cheatsheets=() if [[ ! -d $profile_dir ]]; then @@ -271,15 +282,31 @@ function command_display() { return 0 fi + if (( overlay_mode )); then + caller_window_id=$(xprop -root | grep '^_NET_ACTIVE_WINDOW(WINDOW)' | sed -e 's/.*window id # //') + fi + + local pqiv_options=( "--scale-mode-screen-fraction=1.0" "--zoom-level=1.0" "--transparent-background" "--hide-info-box" - "--window-title=cheatsheet-viewer" + "--window-title=cheatsheet-viewer-$profile" ) pqiv "${pqiv_options[@]}" "${cheatsheets[@]}" & + + if (( overlay_mode )); then + local counter=0 + while (( counter < 10 )) && ! wmctrl -l | grep -q "cheatsheet-viewer-$profile"; do + sleep 0.05 + (( counter+=1 )) + done + + wmctrl -F -r "cheatsheet-viewer-$profile" -b add,above + wmctrl -i -a "$caller_window_id" + fi } @@ -294,17 +321,22 @@ function command_display() { # $2 (profile) # Profile name. # +# $3 (overlay_mode) +# Specify if cheatsheets should be shown in overlay mode. +# # Returns: # 0 on success, 1 otherwise. # function command_toggle() { local config_dir="$1" local profile="$2" + local overlay_mode="$3" + local profile_dir="$config_dir/profiles/$profile" local cheatsheets_file="$profile_dir/cheatsheets" local last_cheatsheet_file="$profile_dir/last" - local current_process_id current_profile last_cheatsheet + local current_process_id current_profile last_cheatsheet caller_window_id local cheatsheets=() @@ -340,6 +372,10 @@ function command_toggle() { # If requested profile is not the same as current one, we want to # show cheatsheets from requested profile. if [[ $current_profile != "$profile" ]]; then + if (( overlay_mode )); then + caller_window_id=$(xprop -root | grep '^_NET_ACTIVE_WINDOW(WINDOW)' | sed -e 's/.*window id # //') + fi + local pqiv_options=( "--scale-mode-screen-fraction=1.0" "--zoom-level=1.0" @@ -360,6 +396,17 @@ function command_toggle() { echo "$CURRENT_FILE_NAME" > "$profile_dir/last" done < <(pqiv "${pqiv_options[@]}" "${cheatsheets[@]}" | grep --line-buffered '^CURRENT_FILE_NAME=') ) & + + if (( overlay_mode )); then + local counter=0 + while (( counter < 10 )) && ! wmctrl -l | grep -q "cheatsheet-viewer-$profile"; do + sleep 0.05 + (( counter+=1 )) + done + + wmctrl -F -r "cheatsheet-viewer-$profile" -b add,above + wmctrl -i -a "$caller_window_id" + fi fi } @@ -424,6 +471,9 @@ QUIET=0 # Default paths, directories etc. CONFIG_DIR="$HOME/.config/cheatsheet_viewer" +# Default option values. +OVERLAY_MODE=0 + # If no arguments were given, just show usage help. if [[ -z ${1-} ]]; then short_help @@ -431,8 +481,9 @@ if [[ -z ${1-} ]]; then fi # Parse the arguments -while getopts "qdvh" opt; do +while getopts "oqdvh" opt; do case "$opt" in + o) OVERLAY_MODE=1;; q) QUIET=1;; d) DEBUG=1;; v) version @@ -476,14 +527,14 @@ elif [[ $COMMAND == display ]]; then PROFILE="${1-default}" shift - command_display "$CONFIG_DIR" "$PROFILE" + command_display "$CONFIG_DIR" "$PROFILE" "$OVERLAY_MODE" elif [[ $COMMAND == toggle ]]; then PROFILE="${1-default}" shift - command_toggle "$CONFIG_DIR" "$PROFILE" + command_toggle "$CONFIG_DIR" "$PROFILE" "$OVERLAY_MODE" else