This repository has been archived by the owner on Jan 20, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathshuffler_tools.py
141 lines (113 loc) · 3.87 KB
/
shuffler_tools.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
#!/usr/bin/env python3
import os
import subprocess
import gi
gi.require_version("Wnck", "3.0")
from gi.repository import Wnck
from gi.repository import Gdk
"""
WindowShuffler
Author: Jacob Vlijm
Copyright © 2017-2018 Ubuntu Budgie Developers
Website=https://ubuntubudgie.org
This program is free software: you can redistribute it and/or modify it under
the terms of the GNU General Public License as published by the Free Software
Foundation, either version 3 of the License, or any later version. This
program is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
A PARTICULAR PURPOSE. See the GNU General Public License for more details. You
should have received a copy of the GNU General Public License along with this
program. If not, see <https://www.gnu.org/licenses/>.
"""
# paths
userdata = os.path.join(
os.environ["HOME"], ".config/budgie-extras/windowshuffler",
)
try:
os.makedirs(userdata)
except FileExistsError:
pass
matr_file = os.path.join(userdata, "matrix")
app_path = os.path.dirname(os.path.abspath(__file__))
shortcuts = os.path.join(app_path, "shortcuts")
firstrun = os.path.join(userdata, "firstrun")
recorded_layout = os.path.join(userdata, "recorded")
def get(cmd):
try:
return subprocess.check_output(cmd).decode("utf-8".strip())
except subprocess.CalledProcessError:
pass
def check_windowtype(window):
try:
return "WNCK_WINDOW_NORMAL" in str(
window.get_window_type()
)
except AttributeError:
pass
def calc_playfield(win_geodata):
wins = win_geodata["windows"]
offset = win_geodata["offset"]
wa = win_geodata["wa"]
return [
[offset[0] + wa[0], offset[1] + wa[1]],
[wa[2], wa[3]],
]
def get_yshift(window):
"""
windows with property NET_FRAME_EXTENTS are not positioned correctly.
we can fix that by looking up the top- extent value, add it to the
targeted y- position.
"""
wid = window.get_xid()
xprop_data = get(["xprop", "-id", str(wid)])
try:
check = [
l.split("=")[1].strip().split(", ")
for l in xprop_data.splitlines()
if "_NET_FRAME_EXTENTS(CARDINAL)" in l
][0]
y_shift = - int(check[2])
except IndexError:
y_shift = 0
return y_shift
def get_window(win_title):
# see if window exists
return get(["xdotool", "search", win_title])
def save_grid(x, y):
open(matr_file, "wt").write(str(x) + " " + str(y))
def get_initialgrid():
try:
return [
int(n) for n in open(matr_file).read().strip().split()
]
except FileNotFoundError:
return [2, 2]
def windowtarget(span, cols, rows, playfield, yoffset=0, overrule=None):
# calculates the targeted position and size of a window
colwidth = int(playfield[1][0] / cols)
rowheight = int(playfield[1][1] / rows)
window_width = (span[1][0] + 1 - span[0][0]) * colwidth
window_height = (span[1][1] + 1 - span[0][1]) * rowheight
originx = (span[0][0] * colwidth) + playfield[0][0]
originy = (span[0][1] * rowheight) + playfield[0][1] + yoffset
# get scale factor
display = Gdk.Display.get_default()
seat = display.get_default_seat()
device = seat.get_pointer()
(screen, x, y) = device.get_position()
monitor = display.get_monitor_at_point(x, y)
scale_factor = monitor.get_scale_factor()
return [
originx * scale_factor,
originy * scale_factor,
window_width * scale_factor,
window_height * scale_factor
]
def shuffle(win, x, y, w, h):
win.unmaximize()
g = Wnck.WindowGravity.NORTHWEST
flags = Wnck.WindowMoveResizeMask.X | \
Wnck.WindowMoveResizeMask.Y | \
Wnck.WindowMoveResizeMask.WIDTH | \
Wnck.WindowMoveResizeMask.HEIGHT
win.set_geometry(g, flags, x, y, w, h)