如何使用键盘快捷键使窗口居中?

max*_*ner 6 shortcut-keys compiz tiling 16.04

我已经进入 compizconfig 的网格部分并自定义了所有平铺命令。

当我测试这些命令时,我没有看到它们中的任何一个有效地将屏幕居中。

我进入窗口放置部分并将新窗口配置为居中打开。但是,如果我将窗口移动到右侧然后想要将其居中,我不知道应该如何使用键盘命令来做到这一点。“放置中心”会将其最大化,“恢复”会将其移动到最近的位置/大小。

总结一下

假设我有一个窗口覆盖了屏幕的右半部分。我想保持尺寸/尺寸相同,但只需将其居中即可。

Ser*_*nyy 3

介绍:

以下脚本将用户活动窗口的中心与屏幕中心对齐。它旨在绑定到“设置”->“键盘”->“快捷方式”菜单中的键盘快捷方式。


用法:

  1. 将脚本另存为~/bin/center_active_window.py. 确保它具有可执行权限chmod +x ~/bin/center_active_window.py
  2. 打开系统设置 -> 键盘 -> 快捷方式 -> 自定义。点击+
  3. 系统将弹出一个窗口,询问您名称和命令。名称可以是任何内容,命令必须是新脚本的完整路径,即/home/your_user_name/bin/center_active_window.py. 点击Apply
  4. 单击Disabled文本并在出现提示后为其分配自定义键绑定。我使用的是Ctrl+ Super+ C,但你可以使用你喜欢的任何东西。

源代码

也可作为GitHub 上的要点获取

#!/usr/bin/env python3

# Author: Serg Kolo
# Date: Oct 3rd, 2016
# Description: Script for aligning the center of 
#     user's active window with the center of the monitor
# Tested on: Ubuntu 16.04
# Written for: http://askubuntu.com/q/832720/295286

from __future__ import print_function
from gi.repository import Gdk
import subprocess

def get_offset(*args):
    command = ['xprop','-notype','_NET_FRAME_EXTENTS',
               '-id',str(args[0])
    ]

    out = subprocess.check_output(command)
    return int(out.decode().strip().split(',')[-2])

def main():

    screen = Gdk.Screen.get_default()
    window = screen.get_active_window()
    window.unmaximize()
    window_width = window.get_width()
    window_y = window.get_origin()[-1]
    print(window_y)
    window_monitor = screen.get_monitor_at_window(window)
    monitor_center = screen.get_monitor_geometry(window_monitor).width/2

    # if centers of window and screen are aligned
    # the top left corner will be at screen_center - window_width/2    
    new_position = monitor_center - window_width /2

    # For some reason there is vertical offset necessary
    # Apparently this comes form _NET_FRAME_EXTENTS value
    offset = get_offset(int(window.get_xid()))

    window.move(new_position,window_y-offset)
    print(window.get_origin()) 

if __name__ == '__main__':
    main()
Run Code Online (Sandbox Code Playgroud)