在产生并使用python-wnck移动之后,窗口位置稍微偏离

时间:2012-05-02 13:48:17

标签: python terminal pygtk wnck

我制作了这个Python脚本:

from gi.repository import Gtk, Gdk, GdkX11, Wnck
from subprocess import PIPE, Popen


class WindowError(Exception):
    pass


def undecorate(aWindow):
    gdkdis = GdkX11.X11Display.get_default()
    gdkwin = GdkX11.X11Window.foreign_new_for_display(gdkdis, aWindow.get_xid())
    gdkwin.set_decorations(Gdk.WMDecoration.BORDER)

def dropdown(aTitle):
    Gtk.main_iteration()

    screen = Wnck.Screen.get_default()
    screen.force_update()

    for window in screen.get_windows():
        if window.get_name() == aTitle:
            timestamp = Gtk.get_current_event_time()

            undecorate(window)

            window.set_skip_pager(True)
            window.set_skip_tasklist(True)
            window.stick()
            window.pin()

            window.maximize_horizontally()
            window.set_geometry(Wnck.WindowGravity.STATIC, 
                                Wnck.WindowMoveResizeMask.Y, 
                                0, 0, -1, -1)

            window.activate(timestamp)
            window.unminimize(timestamp)

            break
    else:
        raise WindowError('Window with title "{}" not found'.format(aTitle))

def getActive():
    Gtk.main_iteration()

    screen = Wnck.Screen.get_default()
    screen.force_update()

    return screen.get_active_window()


def main():
    active = getActive()

    if active.get_name() == 'Dropdown Terminal':
        if active.is_minimized():
            dropdown('Dropdown Terminal')
        else:
            active.minimize()
            return
    else:
        try:
            dropdown('Dropdown Terminal')
        except WindowError:
            Popen(['roxterm', '--profile=Dropdown'], stdout=PIPE, stderr=PIPE)
            dropdown('Dropdown Terminal')


if __name__ == "__main__":
    main()

它的作用是让roxterm像Guake一样行事。我遇到的唯一问题是当我打开一个新的roxterm实例并将窗口移动到(0,0)之后,窗口的最终y位置是几个可见像素向下。我使用wnck库中的set_geometry函数来执行此操作。关于如何修复它的任何想法?感谢。

1 个答案:

答案 0 :(得分:1)

哎呀,忘了提到我解决了这个问题。我改变了代码中的一些东西,但我认为使用GdkWindow重新定位而不是Wnck修复了定位问题。此版本保持下拉终端打开,直到再次按下热键。顺便说一句,我通过在Gnome的键盘首选项中添加自定义热键将热键映射到此脚本。

from gi.repository import Gtk, Gdk, GdkX11, Wnck
from subprocess import PIPE, Popen


class WindowError(Exception):
    pass


def getScreen():
    Gtk.main_iteration()

    screen = Wnck.Screen.get_default()
    screen.force_update()

    return screen

def getGDKWindow(aWindow):
    gdkdisplay = GdkX11.X11Display.get_default()
    gdkwindow  = GdkX11.X11Window.foreign_new_for_display(gdkdisplay, aWindow.get_xid())

    return gdkwindow

def getWindow(aTitle):
    screen = getScreen()
    active = screen.get_active_window()

    if active.get_name() == aTitle:
        return active

    for window in screen.get_windows():
        if window.get_name() == aTitle:
            return window

    return None

def adjust(aWindow):
    gdkwindow = getGDKWindow(aWindow)

    gdkwindow.set_decorations(Gdk.WMDecoration.BORDER)
    gdkwindow.move(0,0)

    aWindow.set_skip_pager(True)
    aWindow.set_skip_tasklist(True)

    aWindow.maximize_horizontally()

    aWindow.stick()
    aWindow.make_above()


def onWindowOpen(aScreen, aWindow, aData):
    if aWindow.get_name() == aData:
        adjust(aWindow)

        Gtk.main_quit()


def main():
    timestamp = Gtk.get_current_event_time()
    window      = getWindow('Dropdown Terminal')

    if window:
        if window.is_minimized():
            window.activate(timestamp)
            window.unminimize(timestamp)
        else:
            window.minimize()
    else:
        Popen(['roxterm', '--separate', '--profile=Dropdown', '--directory=.'])

        getScreen().connect('window-opened', onWindowOpen, 'Dropdown Terminal')

        Gtk.main()


if __name__ == "__main__":
    main()