-
Notifications
You must be signed in to change notification settings - Fork 114
/
browser.py
93 lines (78 loc) · 2.87 KB
/
browser.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
# flake8: noqa F403, F405
from cefpython3 import cefpython as cef
from PySide2.QtCore import *
from PySide2.QtGui import *
from PySide2.QtWidgets import *
from server.gui.utils import WINDOWS, LINUX
WindowUtils = cef.WindowUtils()
# OS differences
# noinspection PyUnresolvedReferences
CefWidgetParent = QWidget
class CefWidget(CefWidgetParent):
def __init__(self, parent=None):
super(CefWidget, self).__init__(parent)
self.parent = parent
self.browser = None
# TODO test without this on linux
self.hidden_window = None # Required for PyQt5 on Linux
self.show()
def focusInEvent(self, event):
# This event seems to never get called on Linux, as CEF is
# stealing all focus due to Issue #284.
if self.browser:
if WINDOWS:
WindowUtils.OnSetFocus(self.getHandle(), 0, 0, 0)
self.browser.SetFocus(True)
def focusOutEvent(self, event):
# This event seems to never get called on Linux, as CEF is
# stealing all focus due to Issue #284.
if self.browser:
self.browser.SetFocus(False)
def embedBrowser(self):
if LINUX:
self.hidden_window = QWindow()
window_info = cef.WindowInfo()
rect = [0, 0, self.width(), self.height()]
window_info.SetAsChild(self.getHandle(), rect)
# TODO better splash
self.browser = cef.CreateBrowserSync(window_info)
def getHandle(self):
if self.hidden_window:
# PyQt5 on Linux
return int(self.hidden_window.winId())
else:
return int(self.winId())
def moveEvent(self, _):
self.x = 0
self.y = 0
if self.browser:
if WINDOWS:
WindowUtils.OnSize(self.getHandle(), 0, 0, 0)
elif LINUX:
self.browser.SetBounds(self.x, self.y,
self.width(), self.height())
self.browser.NotifyMoveOrResizeStarted()
def resizeEvent(self, event):
size = event.size()
if self.browser:
if WINDOWS:
WindowUtils.OnSize(self.getHandle(), 0, 0, 0)
elif LINUX:
self.browser.SetBounds(self.x, self.y,
size.width(), size.height())
self.browser.NotifyMoveOrResizeStarted()
class CefApplication(QApplication):
def __init__(self, args):
super(CefApplication, self).__init__(args)
if not cef.GetAppSetting("external_message_pump"):
self.timer = self.createTimer()
def createTimer(self):
timer = QTimer()
timer.timeout.connect(self.onTimer)
timer.start(10)
return timer
def onTimer(self):
cef.MessageLoopWork()
def stopTimer(self):
# Stop the timer after Qt's message loop has ended
self.timer.stop()