只有在需要时才能自动添加工作区?
假设我使用4个工作区,我偶然需要更多,是否有自动化过程,或者,如果不可能,一个简单的方法来顺便添加更多的工作区(而不是安装Ubuntu tweak
等等)。
自动设置工作区数量; 根据您的需要添加和删除列和行
如果您输入工作空间矩阵的最后一列或一行,则会自动添加工作空间的()背景脚本版本。
这是它的工作原理:
-
如果您到达最后一列或最后一行,则会添加其他视口:
-
如果您的工作空间未使用5-10秒且其上没有窗口,则将再次删除其他工作空间。 但是,您将始终在下方添加一行,并在当前视口右侧添加一列:
剧本:
#!/usr/bin/env python3 import subprocess import time import math # --- set default workspaces below (horizontally, vertically) hsize = 2 vsize = 2 # --- set the maximum number of workspaces below max_ws = 10 def set_workspaces(size, axis): subprocess.Popen([ "dconf", "write", "/org/compiz/profiles/unity/plugins/core/"+axis, str(size) ]) def get_res(): resdata = subprocess.check_output(["xrandr"]).decode("utf-8").split() curr = resdata.index("current") return (int(resdata[curr+1]), int(resdata[curr+3].replace(",", ""))) def wspace(): try: sp = subprocess.check_output(["wmctrl", "-d"]).decode("utf-8").split() return ([int(n) for n in sp[3].split("x")], [int(n) for n in sp[5].split(",")]) except subprocess.CalledProcessError: pass def clean_up(curr_col, curr_row): try: w_list = [l.split() for l in subprocess.check_output(["wmctrl", "-lG"]).decode("utf-8").splitlines()] xpos = max([math.ceil((int(w[2])+span[1][0])/res[0]) for w in w_list]) min_x = max(xpos, curr_col+1, hsize) if xpos >= hsize: set_workspaces(min_x, "hsize") else: set_workspaces(min_x, "hsize") ypos = max([math.ceil((int(w[3])+span[1][1])/res[1]) for w in w_list]) min_y = max(ypos, curr_row+1, vsize) if ypos >= vsize: set_workspaces(min_y, "vsize") else: set_workspaces(min_y, "vsize") except subprocess.CalledProcessError: pass res = get_res() t = 0 while True: span = wspace() if span != None: cols = int(span[0][0]/res[0]); rows = int(span[0][1]/res[1]) currcol = int((span[1][0]+res[0])/res[0]) if all([currcol == cols, cols*rows < max_ws]): set_workspaces(cols+1, "hsize") currrow = int((span[1][1]+res[1])/res[1]) if all([currrow == rows, cols*rows < max_ws]): set_workspaces(rows+1, "vsize") if t == 10: clean_up(currcol, currrow) t = 0 else: t = t+1 time.sleep(1)
如何使用
- 将下面的脚本复制到一个空文件中,将其另存为
add_space.py
-
在脚本的head部分,如果您喜欢其他设置(最大工作空间数,默认矩阵,例如2x2),请编辑行:
# --- set default workspaces below (horizontally, vertically) hsize = 2 vsize = 2 # --- set the maximum number of workspaces below max_ws = 10
-
测试 - 通过命令运行它:
python3 /path/to/add_space.py
-
如果一切正常,请将其添加到启动应用程序:Dash> Startup Applications>添加命令:
/bin/bash -c "sleep 15 && python3 /path/to/add_space.py`
注意
与往常一样,脚本非常“低效”,并且不会给处理器增加任何明显的负载。
说明
下面的故事有点复杂,主要是对概念和程序的解释,而不是编码。 只有你感兴趣才能阅读。
如何计算所需的工作空间(示例列)
wmctrl -d
的输出如下:
0 * DG: 3360x2100 VP: 1680,1050 WA: 65,24 1615x1026 N/A
在输出中, VP: 1680,1050
为我们提供了有关跨越工作空间(所有视口的矩阵)的信息。 此信息仅在我们也具有屏幕分辨率时才有用,因为例如1680
可以是两个宽度(不太可能,但仍然是)或一次屏幕。
幸运的是,我们可以从命令xrandr
解析出屏幕的分辨率。
然后,如果我们知道屏幕的x大小是1680
并且我们当前在VP: 1680,1050
,我们知道我们在工作区矩阵的第二列 。 由于我们也知道总矩阵的大小( DG: 3360x2100
,也来自wmctrl -d
的输出),我们知道当前矩阵包括两列(3360/1680),我们处于“最后一列”。
然后,该脚本将发送一条指令,通过命令将列添加到矩阵:
dconf write /org/compiz/profiles/unity/plugins/core/hsize
这是原则。
如何计算要删除的工作空间(示例列)
每10秒一次,脚本运行命令列出所有当前打开的窗口,命令为:
wmctrl -lG
这也为我们提供了窗口位置的信息,如下所示:
0x04604837 0 3425 24 1615 1026 jacob-System-Product-Name Niet-opgeslagen document 2 - gedit
在输出中, 3425
是窗口的x位置。 然而,该图相对于当前工作空间 (左侧)。 要知道工作空间矩阵中窗口的绝对位置(x方向),我们必须添加当前视口信息的第一个数字(例如VP: 1680,1050
,来自wmctrl -d
的输出)。
但是,为简单起见,我们假设我们在视口1,1
(topleft视口)上,因此窗口的相对位置等于其绝对位置。
由于屏幕的分辨率为1680
,我们知道窗口在3425/1680
列3425/1680
,向上舍入,因为3360 and 5040
之间的所有内容都在矩阵的同一列上(分辨率的3到4倍)。 为了正确计算,我们使用math.ceil()
( python
)
由于脚本还会使规则在右侧/下方始终具有额外的工作空间,因此我们需要将列数设置为最高值:
- 当前工作空间列+ 1
- 最后一列有一个窗口
- 默认列数,在脚本头部设置
所以脚本做:)
行以完全相同的过程进行管理。
从技术上讲,没有用于调整工作空间大小的快捷方式,但您可以使用下面的简单脚本并将其绑定到快捷方式。
- 以下脚本,将其保存在
.local/share/applications
文件夹中,或者您喜欢的位置。 - 确保使用
chmod 755 /path/to/script
使脚本可执行 - 将其绑定到系统设置 – >键盘 – >快捷方式 – >自定义快捷方式中的快捷方式
例如,我有这个设置:
该脚本绑定到Shift Ctrl Alt I. 但Ctrl Alt 我也可以工作。 我给出了脚本的完整路径,即
/home/xieerqi/resize-workspaces.sh
这是它应该是这样的:
脚本
#!/bin/bash # Author : Serg Kolo # Date: Sept 19, 2015 # Purpose: simple script to resize # unity workspaces # Written for: http://askubuntu.com/q/676046/295286 HEIGHT=$(gsettings get org.compiz.core:/org/compiz/profiles/unity/plugins/core/ hsize) WIDTH=$(gsettings get org.compiz.core:/org/compiz/profiles/unity/plugins/core/ vsize) NEWSIZE=$(zenity --entry --text="Current desktop set-up $HEIGHT x $WIDTH. Enter new setup in HEIGHTxWIDTH format" --width=250 | tr 'x' ' ' ) ARRAY=( $NEWSIZE ) [ -z ${ARRAY[1]} ] && exit gsettings set org.compiz.core:/org/compiz/profiles/unity/plugins/core/ hsize ${ARRAY[0]} gsettings set org.compiz.core:/org/compiz/profiles/unity/plugins/core/ vsize ${ARRAY[1]}
使用非常简单,设置非常简单