forked from tscizzlebg/pywebview
-
Notifications
You must be signed in to change notification settings - Fork 0
/
window.py
315 lines (256 loc) · 9.51 KB
/
window.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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
import inspect
import logging
import os
from functools import wraps
from webview.event import Event
from webview.http_server import start_server
from webview.util import base_uri, parse_file_type, escape_string, transform_url, make_unicode, WebViewException
from .js import css
logger = logging.getLogger('pywebview')
def _api_call(function, event_type):
"""
Decorator to call a pywebview API, checking for _webview_ready and raisings
appropriate Exceptions on failure.
"""
@wraps(function)
def wrapper(*args, **kwargs):
event = args[0].loaded if event_type == 'loaded' else args[0].shown
try:
if not event.wait(15):
raise WebViewException('Main window failed to start')
if args[0].gui is None:
raise WebViewException('GUI is not initialized')
return function(*args, **kwargs)
except NameError as e:
raise WebViewException('Create a web view window first, before invoking this function')
return wrapper
def _shown_call(function):
return _api_call(function, 'shown')
def _loaded_call(function):
return _api_call(function, 'loaded')
class Window:
def __init__(self, uid, title, url, html, width, height, x, y, resizable, fullscreen,
min_size, hidden, frameless, minimized, confirm_close, background_color,
js_api, text_select):
self.uid = uid
self.title = make_unicode(title)
self.url = None if html else transform_url(url)
self.html = html
self.initial_width = width
self.initial_height = height
self.initial_x = x
self.initial_y = y
self.resizable = resizable
self.fullscreen = fullscreen
self.min_size = min_size
self.confirm_close = confirm_close
self.background_color = background_color
self.text_select = text_select
self.frameless = frameless
self.hidden = hidden
self.minimized = minimized
self._js_api = js_api
self._functions = {}
self.closed = Event()
self.closing = Event()
self.loaded = Event()
self.shown = Event()
self.gui = None
self._httpd = None
self._is_http_server = False
def _initialize(self, gui, multiprocessing, http_server):
self.gui = gui
self.loaded._initialize(multiprocessing)
self.shown._initialize(multiprocessing)
self._is_http_server = http_server
if http_server and self.url and self.url.startswith('file://'):
self.url, self._httpd = start_server(self.url)
@property
def width(self):
self.shown.wait(15)
width, _ = self.gui.get_size(self.uid)
return width
@property
def height(self):
self.shown.wait(15)
_, height = self.gui.get_size(self.uid)
return height
@property
def x(self):
self.shown.wait(15)
x, _ = self.gui.get_position(self.uid)
return x
@property
def y(self):
self.shown.wait(15)
_, y = self.gui.get_position(self.uid)
return y
@_loaded_call
def get_elements(self, selector):
# check for GTK's WebKit2 version
if hasattr(self.gui, 'old_webkit') and self.gui.old_webkit:
raise NotImplementedError('get_elements requires WebKit2 2.2 or greater')
code = """
var elements = document.querySelectorAll('%s');
var serializedElements = [];
for (var i = 0; i < elements.length; i++) {
var node = pywebview.domJSON.toJSON(elements[i], {
metadata: false,
serialProperties: true
});
serializedElements.push(node);
}
serializedElements;
""" % selector
return self.evaluate_js(code)
@_shown_call
def load_url(self, url):
"""
Load a new URL into a previously created WebView window. This function must be invoked after WebView windows is
created with create_window(). Otherwise an exception is thrown.
:param url: url to load
:param uid: uid of the target instance
"""
if self._httpd:
self._httpd.shutdown()
self._httpd = None
url = transform_url(url)
if (self._is_http_server or self.gui.renderer == 'edgehtml') and url.startswith('file://'):
url, self._httpd = start_server(url)
self.gui.load_url(url, self.uid)
@_shown_call
def load_html(self, content, base_uri=base_uri()):
"""
Load a new content into a previously created WebView window. This function must be invoked after WebView windows is
created with create_window(). Otherwise an exception is thrown.
:param content: Content to load.
:param base_uri: Base URI for resolving links. Default is the directory of the application entry point.
:param uid: uid of the target instance
"""
if self._httpd:
self._httpd.shutdown()
content = make_unicode(content)
self.gui.load_html(content, base_uri, self.uid)
@_loaded_call
def load_css(self, stylesheet):
code = css.src % stylesheet.replace('\n', '').replace('\r', '').replace('"', "'")
self.gui.evaluate_js(code, self.uid)
@_shown_call
def set_title(self, title):
"""
Set a new title of the window
"""
self.gui.set_title(title, self.uid)
@_loaded_call
def get_current_url(self):
"""
Get the URL currently loaded in the target webview
"""
return self.gui.get_current_url(self.uid)
@_shown_call
def destroy(self):
"""
Destroy a web view window
"""
self.gui.destroy_window(self.uid)
@_shown_call
def show(self):
"""
Show a web view window.
"""
self.gui.show(self.uid)
@_shown_call
def hide(self):
"""
Hide a web view window.
"""
self.gui.hide(self.uid)
@_shown_call
def set_window_size(self, width, height):
"""
Resize window
:param width: desired width of target window
:param height: desired height of target window
"""
logger.warning('This function is deprecated and will be removed in future releases. Use resize() instead')
self.gui.resize(width, height, self.uid)
@_shown_call
def resize(self, width, height):
"""
Resize window
:param width: desired width of target window
:param height: desired height of target window
"""
self.gui.resize(width, height, self.uid)
@_shown_call
def minimize(self):
"""
Minimize window.
"""
self.gui.minimize(self.uid)
@_shown_call
def restore(self):
"""
Restore minimized window.
"""
self.gui.restore(self.uid)
@_shown_call
def toggle_fullscreen(self):
"""
Toggle fullscreen mode
"""
self.gui.toggle_fullscreen(self.uid)
@_shown_call
def move(self, x, y):
"""
Move Window
:param x: desired x coordinate of target window
:param y: desired y coordinate of target window
"""
self.gui.move(x, y, self.uid)
@_loaded_call
def evaluate_js(self, script):
"""
Evaluate given JavaScript code and return the result
:param script: The JavaScript code to be evaluated
:return: Return value of the evaluated code
"""
escaped_script = 'JSON.stringify(eval("{0}"))'.format(escape_string(script))
return self.gui.evaluate_js(escaped_script, self.uid)
@_shown_call
def create_file_dialog(self, dialog_type=10, directory='', allow_multiple=False, save_filename='', file_types=()):
"""
Create a file dialog
:param dialog_type: Dialog type: open file (OPEN_DIALOG), save file (SAVE_DIALOG), open folder (OPEN_FOLDER). Default
is open file.
:param directory: Initial directory
:param allow_multiple: Allow multiple selection. Default is false.
:param save_filename: Default filename for save file dialog.
:param file_types: Allowed file types in open file dialog. Should be a tuple of strings in the format:
filetypes = ('Description (*.extension[;*.extension[;...]])', ...)
:return: A tuple of selected files, None if cancelled.
"""
if type(file_types) != tuple and type(file_types) != list:
raise TypeError('file_types must be a tuple of strings')
for f in file_types:
parse_file_type(f)
if not os.path.exists(directory):
directory = ''
return self.gui.create_file_dialog(dialog_type, directory, allow_multiple, save_filename, file_types, self.uid)
def expose(self, *functions):
if not all(map(callable, functions)):
raise TypeError('Parameter must be a function')
func_list = []
for func in functions:
name = func.__name__
self._functions[name] = func
try:
params = list(inspect.getfullargspec(func).args) # Python 3
except AttributeError:
params = list(inspect.getargspec(func).args) # Python 2
func_list.append({
'func': name,
'params': params
})
if self.loaded.is_set():
self.evaluate_js('window.pywebview._createApi(%s)' % func_list)