/
WindowManager.py
201 lines (135 loc) · 4.24 KB
/
WindowManager.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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
#!/usr/bin/python3
import json
import os
class Dimensions(object):
def __init__(self, file: str) -> None:
"""
Handles dimension.json file storage
Args:
file (str): path to .json
"""
self.file = self._check_file(file)
def _check_file(self, file):
if not os.path.exists(file):
with open(file, "w") as f:
json.dump({}, f)
return file
def add_dimension(self, app_id: str, dim: dict, prev_action: str = '') -> None:
"""
Add dimension app setting
Args:
app_id (str): Bundle ID of the app
dim (dict): Dimension Dictonary
"""
jsn = dict()
if os.path.exists(self.file):
with open(self.file, "r") as f:
jsn: dict = json.load(f)
jsn.pop(app_id, False)
dim.update({'prev_action': prev_action})
jsn[app_id] = dim
self._save_json_file(jsn)
def get_dimension(self, app_id: str, delete: bool = False) -> dict:
"""
get dimension for an app id
Args:
app_id (str): app bundle id
Returns:
dict: _description_
"""
jsn = self._read_json_file()
dimension = jsn.get(app_id, {})
if delete:
self.delete_dimension(app_id)
return dimension
def delete_dimension(self, app_id: str) -> None:
"""
Delete a dimension for an app id
Args:
app_id (str): _description_
"""
jsn = self._read_json_file()
jsn.pop(app_id, False)
self._save_json_file(jsn)
def _read_json_file(self) -> dict:
"""
Read json file
Returns:
dict: json of the file
"""
jsn = dict()
if os.path.exists(self.file):
with open(self.file, "r") as f:
jsn: dict = json.load(f)
return jsn
def _save_json_file(self, jsn: dict) -> None:
"""
Save the json to a file
Args:
jsn (dict): json with dimensions
"""
with open(self.file, "w") as f:
f.seek(0)
json.dump(jsn, f)
f.truncate()
class Window(object):
def __init__(self, dim: str()) -> None:
self.dimension = json.loads(dim)
def x_pos(self) -> int:
"""
Get x position of the window from a str represenation of a dimension
Returns:
int: x postition
"""
return int(self.dimension.get('x', None))
def y_pos(self) -> int:
"""
Get y position of the window from a str represenation of a dimension
Returns:
int: y postition
"""
return int(self.dimension.get('y', None))
def width(self) -> int:
"""
Get width of the window from a str represenation of a dimension
Returns:
int: window width
"""
return int(self.dimension.get('width', None))
def height(self) -> int:
"""
Get height of the window from a str represenation of a dimension
Returns:
int: window height
"""
return int(self.dimension.get('height', None))
def get_dimensions(self) -> dict:
"""
Get dimension from dimension provided as string
Returns:
int: dimension
"""
return self.dimension
class Screen(object):
def __init__(self) -> None:
self.screen_res = self._sys_profiler()
def screen_width(self) -> int:
"""
Get screen width
Returns:
int: screen width
"""
return self.screen_res[0]
def screen_height(self) -> int:
"""
Get Screen height
Returns:
int: screen height
"""
return self.screen_res[1]
def _sys_profiler(self) -> tuple:
sysinfo: dict = json.loads(os.popen("system_profiler SPDisplaysDataType -json").read())
screen_dimensions = sysinfo.get('SPDisplaysDataType')[0].get('spdisplays_ndrvs')[0].get('_spdisplays_resolution')
res, freq = screen_dimensions.split(" @ ")
screen_width, screen_height = res.split(" x ")
return (int(screen_width), int(screen_height))