将Zenity对话框始终放在最前面


8

有没有办法检测Zenity对话框失去焦点?

我想将对话框保持在前台,除非用户按ESC

我正在尝试将其添加到此脚本中

#!/bin/bash

# requires these packages from ubuntu repository:
# wmctrl, zenity, x11-utils 
# and the script mouse-speed

# This procect on git:        https://github.com/rubo77/mouse-speed


######## configuration ##########
# seconds between micro breaks
microbreak_time=$(( 10 * 60 ))
# micro break duration in seconds
microbreak_duration=15
# seconds between long breaks
longbreak_time=$(( 120 * 60 ))

# message to display 
message="Try focussing a far object outside the window with the eye to relax!"
longbreak_message="Change your seating or continue work in a standing/sitting position"
#postpone label
postpone="Postpone"

window_title="typebreak"

# global zoom of your window manager:
ZOOM=2
# height in px of the top system-bar:
TOPMARGIN=57
# sum in px of all horizontal borders:
HORIZONTALMARGIN=40
# get width of screen and height of screen
SCREEN_WIDTH=$(xwininfo -root | awk '$1=="Width:" {print $2}')
SCREEN_HEIGHT=$(xwininfo -root | awk '$1=="Height:" {print $2}')
# width and height
W=$(( $SCREEN_WIDTH / $ZOOM - 2 * $HORIZONTALMARGIN ))
H=$(( $SCREEN_HEIGHT / $ZOOM - 2 * $TOPMARGIN ))

function slow_down(){
    #zenity --warning --text "slow down mouse";
    mouse-speed -d 30
}

while true; do
    # short loop every few minutes to look around
    sleep $microbreak_time
    (
    echo "99"
    sleep $(( $microbreak_duration - 2 ))
    echo "# Mouse speed reset to 100%"
    sleep 2
    echo "100"
    ) | if ( sleep 1 && wmctrl -F -a "$window_title" -b add,maximized_vert,maximized_horz && sleep 3 &&  wmctrl -F -a "$window_title" -b add,above ) & ( zenity --progress --text "$message" --percentage=0 --auto-close  --height=$H --width=$W --pulsate --title="$window_title" --cancel-label="$postpone" ); then
        #zenity --info --text "Maus normal speed!"
        mouse-speed -r
    else 
        slow_down
    fi
done &
while true; do
    # second long loop to change seat position
    sleep $longbreak_time
    zenity --warning --text "$longbreak_message" --title="$window_title - long break"
done

Answers:


12
#!/bin/bash
# This will wait one second and then steal focus and make the Zenity dialog box always-on-top (aka. 'above').

(sleep 1 && wmctrl -F -a "I am on top" -b add,above) &
(zenity --info --title="I am on top" --text="How to help Zenity to get focus and be always on top")

资源:


我想知道是否add,fullscreen还会起作用
rubo77'9

我还在第二个wmctrl调用中添加了-b add,maximized_vert,maximized_horz`。现在,如果最小化,调整大小或移动了zenity窗口,
rubo77

在启动时,事情可能会变得很慢,所以我建议这样做:(while ! wmctrl -F -a "$$ the title" -b add,above;do sleep 1;done) &并在标题上加上脚本pid,以使其更加独特。
Aquarius Power

0

如果将其作为cron作业运行,则可能会遇到问题。Cron的环境无法识别您的X显示器,dbus或桌面,也不会显示zenity框或将其放在顶部。=添加DISPLAY:0之前 wmctrl和zenity解决问题:

(sleep 1 && DISPLAY=:0 wmctrl -F -a "I am on top" -b add,above) & (DISPLAY=:0 zenity --info --title="I am on top" --text="How to help Zenity to get focus and be always on top")


0

此解决方案与@Jan的解决方案基本相同,但是使用PID而不是窗口标题来标识窗口,并且还与Bourne Shell兼容(->不需要bash)。

#!/bin/sh
# This will wait 200ms, steal focus and set the Zenity dialog box
# to "always-on-top".

modify_win_by_pid() {
    pid=$1

    sleep 0.2
    win_id=`wmctrl -l -p | grep ${pid} | awk '{print $1}'`
    wmctrl -i - ${win_id} -b add,above
}

zenity --info --title="I am on top" --text="I received focus and will stay on top" &
modify_win_by_pid $!
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.