在Unity中获取完整的窗口尺寸(包括装饰)

在Unity中获取完整的窗口尺寸(包括装饰)

我已经用过答案如何准确找到窗户尺寸和位置(包括装饰)?在我的 bash 脚本中,并取得了很好的成功无声广播Xfce 上的应用程序,但现在其他人正在使用它,他们在 Ubuntu 14.04、Unity 中发现了一些问题。这是由输出引起的一个问题:

xwininfo -id $(xdotool getactivewindow)

显示:

Relative upper-left X:  0  
Relative upper-left Y:  0

我用它来计算边框的宽度和标题栏的高度。它们不应该是 0。

有谁知道如何在 Ubuntu 14.04 的 Unity 中获取窗口边框的宽度和标题栏的高度。

或者,如何直接获取完整的窗口尺寸?

我已经针对 xwininfo 包的 xorg 提交了一个错误https://bugs.freedesktop.org/show_bug.cgi?id=84348

我还找到了有关在 Ubuntu 14.04 下在 Unity 中创建自己的主题的信息:https://wiki.ubuntu.com/Unity/Theming

似乎答案就在主题页面的某个地方,但我还不明白,特别是因为我不使用 Ubuntu 或 Unity。

答案1

我还在 stackoverflow 上问了这个问题,并得到了一个很好的答案,我将其标记为正确答案并点赞:https://stackoverflow.com/a/26060527/1707904

这也适用于 compiz。您可以通过以下几种方式获取这些信息:

wnckprop --xid=$(xdotool getactivewindow)

否则,您可以将从 xwininfo 获得的绝对值与可使用以下方式访问的装饰的大小混合在一起:

xprop _NET_FRAME_EXTENTS -id $(xdotool getactivewindow)

供您参考,如果您想获取完整的帧大小,包括窗口周围的输入区域,您可以使用xwininfo -frame

经过实验,我更好地理解了Ubuntu中的“输入区域”指的是什么。由于边框默认大小为 0,因此窗口周围有一个 10 像素的区域,您可以在其中抓取窗口以调整大小。它可能有其他用途,但输入区域基本上就像一个不可见的 10px 边框。因此,xwininfo 中的 Absolute 提供内部窗口,而 xwininfo -frame 提供整个窗口,包括标题栏和周围额外的 10px(如果输入区域为 10px)。 xprop... 仅给出装饰的大小,不包括输入区域。因此,需要全部 3 个命令才能全面了解窗口的几何形状。

这是我最终使用的代码(它忽略了不可见的输入区域):

eval $(xwininfo -id "$aw" |
      sed -n -e "s/^ \+Absolute upper-left X: \+\([0-9]\+\).*/x=\1/p" \
             -e "s/^ \+Absolute upper-left Y: \+\([0-9]\+\).*/y=\1/p" \
             -e "s/^ \+Width: \+\([0-9]\+\).*/w=\1/p" \
             -e "s/^ \+Height: \+\([0-9]\+\).*/h=\1/p" )
if [ "$entire" = true ]
then
    extents=$(xprop _NET_FRAME_EXTENTS -id "$aw" | grep "NET_FRAME_EXTENTS" | cut -d '=' -f 2 | tr -d ' ')
    bl=$(echo $extents | cut -d ',' -f 1) # width of left border
    br=$(echo $extents | cut -d ',' -f 2) # width of right border
    t=$(echo $extents | cut -d ',' -f 3)  # height of title bar
    bb=$(echo $extents | cut -d ',' -f 4) # height of bottom border

    let x=$x-$bl
    let y=$y-$t
    let w=$w+$bl+$br
    let h=$h+$t+$bb
fi

答案2

下面是更详细的代码,也使用 _GTK_FRAME_EXTENTS

#!/bin/bash
# get window coordinates and width and height
# inspired by:
# https://unix.stackexchange.com/questions/14159/how-do-i-find-the-window-dimensions-and-position-accurately-including-decoration
window=$(xwininfo |  awk '/xwininfo: Window id:/{print $4}')
entire=true
set - $(xwininfo -id "$window" | awk '
   /Absolute upper-left X:/{ print $4 } \
   /Absolute upper-left Y:/{ print $4 } \
   /Width:/ { print $2 } \
   /Height:/{ print $2}')
x="$1"
y="$2"
w="$3"
h="$4"
if [ "$entire" = true ]; then
   set - $( xprop -id "$window" | awk ' BEGIN { set=0 } \
   /_..._FRAME_EXTENTS/{ print $1,$3,$4,$5,$6; set=1} \
   END { if (!set) print "none" }' | sed 's/(.*)//;s/,//g')
   prop="$1"
   if [ "$prop" != none ]; then
      bl="$2"
      br="$3"
      t="$4"
      bb="$5"
      case "$prop" in
     *NET*)
        (( x=x-bl    ))
        (( y=y-t     ))
        (( w=w+bl+br ))
        (( h=h+t+bb  ))
        ;;
     *GTK*)
        (( x=x+bl    ))
        (( y=y+t     ))
        (( w=w-bl-br ))
        (( h=h-t-bb  ))
        ;;
      esac
   fi
fi
echo "window: $window"
echo "property: $prop"
echo $x $y $w $h

相关内容