axon-gui 5.3.4__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- axon_gui-5.3.4/PKG-INFO +52 -0
- axon_gui-5.3.4/README.md +41 -0
- axon_gui-5.3.4/axon/Axon.py +149 -0
- axon_gui-5.3.4/axon/AxonEngineView.py +44 -0
- axon_gui-5.3.4/axon/AxonEnv.py +31 -0
- axon_gui-5.3.4/axon/AxonEvent.py +92 -0
- axon_gui-5.3.4/axon/AxonGUI.py +153 -0
- axon_gui-5.3.4/axon/AxonJSCallHandler.py +277 -0
- axon_gui-5.3.4/axon/AxonMagicGUI.py +52 -0
- axon_gui-5.3.4/axon/AxonPYCall.py +135 -0
- axon_gui-5.3.4/axon/AxonStore.py +196 -0
- axon_gui-5.3.4/axon/__init__.py +7 -0
- axon_gui-5.3.4/axon/cli.py +272 -0
- axon_gui-5.3.4/axon/types.py +0 -0
- axon_gui-5.3.4/axon_gui.egg-info/PKG-INFO +52 -0
- axon_gui-5.3.4/axon_gui.egg-info/SOURCES.txt +22 -0
- axon_gui-5.3.4/axon_gui.egg-info/dependency_links.txt +1 -0
- axon_gui-5.3.4/axon_gui.egg-info/entry_points.txt +2 -0
- axon_gui-5.3.4/axon_gui.egg-info/requires.txt +1 -0
- axon_gui-5.3.4/axon_gui.egg-info/top_level.txt +3 -0
- axon_gui-5.3.4/pyproject.toml +22 -0
- axon_gui-5.3.4/setup.cfg +4 -0
- axon_gui-5.3.4/tests/controllers.py +93 -0
- axon_gui-5.3.4/tests/run_web_tests.py +349 -0
axon_gui-5.3.4/PKG-INFO
ADDED
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: axon-gui
|
|
3
|
+
Version: 5.3.4
|
|
4
|
+
Summary: PySide6 + Web hybrid desktop framework: expose Python objects to the page and call JS back / 基于 PySide6 的 Qt-Web 混合开发框架
|
|
5
|
+
Classifier: Programming Language :: Python :: 3
|
|
6
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
7
|
+
Classifier: Operating System :: OS Independent
|
|
8
|
+
Requires-Python: >=3.12
|
|
9
|
+
Description-Content-Type: text/markdown
|
|
10
|
+
Requires-Dist: PySide6
|
|
11
|
+
|
|
12
|
+
# axon-gui (Python side)
|
|
13
|
+
|
|
14
|
+
The Python runtime of [Axon](https://github.com/AxonGUI/Axon): `AxonGUI` opens a
|
|
15
|
+
`QWebEngineView` with a QWebChannel attached, `AxonController` exposes Python objects to the
|
|
16
|
+
frontend, and `Axon` calls objects the frontend exposed.
|
|
17
|
+
|
|
18
|
+
```python
|
|
19
|
+
import controller # your @AxonController definitions
|
|
20
|
+
from axon import AxonGUI, AxonMagicGUI
|
|
21
|
+
from PySide6.QtWidgets import QApplication
|
|
22
|
+
|
|
23
|
+
app = QApplication([])
|
|
24
|
+
AxonMagicGUI() # only needed when you want to run code on the Qt main thread
|
|
25
|
+
view = AxonGUI("main", "http://127.0.0.1:5173/") # web_name must match the frontend
|
|
26
|
+
view.show()
|
|
27
|
+
app.exec()
|
|
28
|
+
```
|
|
29
|
+
|
|
30
|
+
Docs and live examples: <https://axongui.github.io/Axon/en/> · 中文文档:<https://axongui.github.io/Axon/>
|
|
31
|
+
|
|
32
|
+
---
|
|
33
|
+
|
|
34
|
+
## 中文
|
|
35
|
+
|
|
36
|
+
Axon 的 Python 运行时:`AxonGUI` 开一个挂了 QWebChannel 的 `QWebEngineView`,
|
|
37
|
+
`AxonController` 把 Python 对象暴露给前端,`Axon` 反过来调前端暴露的对象。
|
|
38
|
+
|
|
39
|
+
```python
|
|
40
|
+
import controller # 你的 @AxonController 定义
|
|
41
|
+
from axon import AxonGUI, AxonMagicGUI
|
|
42
|
+
from PySide6.QtWidgets import QApplication
|
|
43
|
+
|
|
44
|
+
app = QApplication([])
|
|
45
|
+
AxonMagicGUI() # 需要 Qt 主线程执行能力时
|
|
46
|
+
view = AxonGUI("main", "http://127.0.0.1:5173/") # web_name 要和前端一致
|
|
47
|
+
view.show()
|
|
48
|
+
app.exec()
|
|
49
|
+
```
|
|
50
|
+
|
|
51
|
+
完整文档与可交互示例见文档站 <https://axongui.github.io/Axon/>(本仓库 `docs/` 下也能
|
|
52
|
+
`python docs/main.py` 在框架自己的窗口里打开)。
|
axon_gui-5.3.4/README.md
ADDED
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
# axon-gui (Python side)
|
|
2
|
+
|
|
3
|
+
The Python runtime of [Axon](https://github.com/AxonGUI/Axon): `AxonGUI` opens a
|
|
4
|
+
`QWebEngineView` with a QWebChannel attached, `AxonController` exposes Python objects to the
|
|
5
|
+
frontend, and `Axon` calls objects the frontend exposed.
|
|
6
|
+
|
|
7
|
+
```python
|
|
8
|
+
import controller # your @AxonController definitions
|
|
9
|
+
from axon import AxonGUI, AxonMagicGUI
|
|
10
|
+
from PySide6.QtWidgets import QApplication
|
|
11
|
+
|
|
12
|
+
app = QApplication([])
|
|
13
|
+
AxonMagicGUI() # only needed when you want to run code on the Qt main thread
|
|
14
|
+
view = AxonGUI("main", "http://127.0.0.1:5173/") # web_name must match the frontend
|
|
15
|
+
view.show()
|
|
16
|
+
app.exec()
|
|
17
|
+
```
|
|
18
|
+
|
|
19
|
+
Docs and live examples: <https://axongui.github.io/Axon/en/> · 中文文档:<https://axongui.github.io/Axon/>
|
|
20
|
+
|
|
21
|
+
---
|
|
22
|
+
|
|
23
|
+
## 中文
|
|
24
|
+
|
|
25
|
+
Axon 的 Python 运行时:`AxonGUI` 开一个挂了 QWebChannel 的 `QWebEngineView`,
|
|
26
|
+
`AxonController` 把 Python 对象暴露给前端,`Axon` 反过来调前端暴露的对象。
|
|
27
|
+
|
|
28
|
+
```python
|
|
29
|
+
import controller # 你的 @AxonController 定义
|
|
30
|
+
from axon import AxonGUI, AxonMagicGUI
|
|
31
|
+
from PySide6.QtWidgets import QApplication
|
|
32
|
+
|
|
33
|
+
app = QApplication([])
|
|
34
|
+
AxonMagicGUI() # 需要 Qt 主线程执行能力时
|
|
35
|
+
view = AxonGUI("main", "http://127.0.0.1:5173/") # web_name 要和前端一致
|
|
36
|
+
view.show()
|
|
37
|
+
app.exec()
|
|
38
|
+
```
|
|
39
|
+
|
|
40
|
+
完整文档与可交互示例见文档站 <https://axongui.github.io/Axon/>(本仓库 `docs/` 下也能
|
|
41
|
+
`python docs/main.py` 在框架自己的窗口里打开)。
|
|
@@ -0,0 +1,149 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
import asyncio
|
|
4
|
+
from concurrent.futures import Future
|
|
5
|
+
from typing import Any
|
|
6
|
+
|
|
7
|
+
from .AxonPYCall import broadcast as _py_broadcast
|
|
8
|
+
from .AxonPYCall import call as _py_call
|
|
9
|
+
from .AxonPYCall import getVar as _py_getVar
|
|
10
|
+
from .AxonPYCall import setVar as _py_setVar
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
class _JSProperty:
|
|
14
|
+
"""JS 侧对象的一个成员: 既能当方法调, 也能当属性读。
|
|
15
|
+
|
|
16
|
+
- ``Axon("w").Ctl.func(1, 2)`` → 调用 JS 方法, 返回 ``Future``
|
|
17
|
+
- ``await Axon("w").Ctl.prop`` → 读取 JS 属性
|
|
18
|
+
- ``Axon("w").Ctl.prop.get()`` → 同步读取 (会阻塞)
|
|
19
|
+
"""
|
|
20
|
+
|
|
21
|
+
def __init__(self, web_name: str, controller_id: str, prop: str) -> None:
|
|
22
|
+
self.web_name = web_name
|
|
23
|
+
self.controller_id = controller_id
|
|
24
|
+
self.prop = prop
|
|
25
|
+
self._get_future: Future | None = None
|
|
26
|
+
|
|
27
|
+
def _ensure_get(self) -> Future:
|
|
28
|
+
if self._get_future is None:
|
|
29
|
+
self._get_future = _py_getVar(self.web_name, self.controller_id, self.prop)
|
|
30
|
+
return self._get_future
|
|
31
|
+
|
|
32
|
+
def call(self, *args: Any, kwargs: dict | None = None) -> Future:
|
|
33
|
+
"""调用 JS 方法并返回 ``Future``; 需要限时请 ``.result(timeout=...)``。"""
|
|
34
|
+
return _py_call(
|
|
35
|
+
self.web_name,
|
|
36
|
+
self.controller_id,
|
|
37
|
+
self.prop,
|
|
38
|
+
args,
|
|
39
|
+
kwargs=kwargs,
|
|
40
|
+
)
|
|
41
|
+
|
|
42
|
+
def __call__(self, *args: Any, **kwargs: Any) -> Future:
|
|
43
|
+
return _py_call(
|
|
44
|
+
self.web_name,
|
|
45
|
+
self.controller_id,
|
|
46
|
+
self.prop,
|
|
47
|
+
args,
|
|
48
|
+
kwargs=kwargs or None,
|
|
49
|
+
)
|
|
50
|
+
|
|
51
|
+
def get(self, timeout: float | None = None) -> Any:
|
|
52
|
+
"""同步读取属性值 (阻塞当前线程)。"""
|
|
53
|
+
return self._ensure_get().result(timeout)
|
|
54
|
+
|
|
55
|
+
def __await__(self):
|
|
56
|
+
# 关键: 必须返回迭代器。旧实现 ``return future.result()`` 返回的是一个值,
|
|
57
|
+
# 于是 ``await`` 直接抛 ``TypeError: __await__() returned non-iterator``。
|
|
58
|
+
return asyncio.wrap_future(self._ensure_get()).__await__()
|
|
59
|
+
|
|
60
|
+
def __getattr__(self, name: str):
|
|
61
|
+
# 把 result()/add_done_callback() 等 Future 方法透传出去
|
|
62
|
+
if name.startswith("__"):
|
|
63
|
+
raise AttributeError(name)
|
|
64
|
+
return getattr(self._ensure_get(), name)
|
|
65
|
+
|
|
66
|
+
def __repr__(self) -> str:
|
|
67
|
+
return f"<JSProperty {self.controller_id}.{self.prop} @ {self.web_name}>"
|
|
68
|
+
|
|
69
|
+
|
|
70
|
+
|
|
71
|
+
|
|
72
|
+
class JSController:
|
|
73
|
+
"""``Axon("web").Controller`` 对应的代理对象。"""
|
|
74
|
+
|
|
75
|
+
def __init__(self, web_name: str, controller_id: str) -> None:
|
|
76
|
+
# 必须用 object.__setattr__: 本类重写了 __setattr__ (写属性 = 写 JS),
|
|
77
|
+
# 直接 ``self.x = ...`` 会触发 __setattr__ 里对未初始化字段的访问,
|
|
78
|
+
# 经 __getattr__ 又生成新的 JSController, 无限递归。
|
|
79
|
+
object.__setattr__(self, "web_name", web_name)
|
|
80
|
+
object.__setattr__(self, "controller_id", controller_id)
|
|
81
|
+
object.__setattr__(self, "_last_future", None)
|
|
82
|
+
|
|
83
|
+
def __getattr__(self, prop: str):
|
|
84
|
+
if prop.startswith("__"):
|
|
85
|
+
raise AttributeError(prop)
|
|
86
|
+
return _JSProperty(self.web_name, self.controller_id, prop)
|
|
87
|
+
|
|
88
|
+
def __setattr__(self, prop: str, value: Any):
|
|
89
|
+
# 写 JS 属性是「发出即忘」: 不能在这里阻塞等待 JS 回复,
|
|
90
|
+
# 因为回复要回到 Qt 主线程, 主线程阻塞 = 死锁。
|
|
91
|
+
future = _py_setVar(self.web_name, self.controller_id, prop, value)
|
|
92
|
+
object.__setattr__(self, "_last_future", future)
|
|
93
|
+
|
|
94
|
+
def __repr__(self) -> str:
|
|
95
|
+
return f"<JSController {self.controller_id} @ {self.web_name}>"
|
|
96
|
+
|
|
97
|
+
|
|
98
|
+
class Axon:
|
|
99
|
+
"""Python 侧访问 JS 的入口: ``Axon("web_name").Controller.func(...)``。"""
|
|
100
|
+
|
|
101
|
+
broadcast: "AxonBroadcast"
|
|
102
|
+
|
|
103
|
+
def __init__(self, web_name: str) -> None:
|
|
104
|
+
object.__setattr__(self, "_web_name", web_name)
|
|
105
|
+
|
|
106
|
+
def __getattr__(self, controller_id: str):
|
|
107
|
+
if controller_id.startswith("__"):
|
|
108
|
+
raise AttributeError(controller_id)
|
|
109
|
+
return JSController(self._web_name, controller_id)
|
|
110
|
+
|
|
111
|
+
def __repr__(self) -> str:
|
|
112
|
+
return f"<Axon web={self._web_name!r}>"
|
|
113
|
+
|
|
114
|
+
|
|
115
|
+
class BroadcastJSProperty(_JSProperty):
|
|
116
|
+
def __call__(self, *args: Any, **kwargs: Any): # type: ignore[override]
|
|
117
|
+
return _py_broadcast(
|
|
118
|
+
self.controller_id, self.prop, args, kwargs=kwargs or None
|
|
119
|
+
)
|
|
120
|
+
|
|
121
|
+
def call(self, *args: Any, kwargs: dict | None = None): # type: ignore[override]
|
|
122
|
+
return _py_broadcast(self.controller_id, self.prop, args, kwargs=kwargs)
|
|
123
|
+
|
|
124
|
+
|
|
125
|
+
class BroadcastJSController(JSController):
|
|
126
|
+
def __getattr__(self, prop: str): # type: ignore[override]
|
|
127
|
+
if prop.startswith("__"):
|
|
128
|
+
raise AttributeError(prop)
|
|
129
|
+
return BroadcastJSProperty(
|
|
130
|
+
self.web_name,
|
|
131
|
+
self.controller_id,
|
|
132
|
+
prop,
|
|
133
|
+
)
|
|
134
|
+
|
|
135
|
+
|
|
136
|
+
class AxonBroadcast(Axon):
|
|
137
|
+
def __init__(self):
|
|
138
|
+
super().__init__("__axon__broadcast__")
|
|
139
|
+
|
|
140
|
+
def __getattr__(self, controller_id: str):
|
|
141
|
+
if controller_id.startswith("__"):
|
|
142
|
+
raise AttributeError(controller_id)
|
|
143
|
+
return BroadcastJSController(
|
|
144
|
+
self._web_name,
|
|
145
|
+
controller_id,
|
|
146
|
+
)
|
|
147
|
+
|
|
148
|
+
|
|
149
|
+
Axon.broadcast = AxonBroadcast()
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
from PySide6.QtCore import QObject, QUrl
|
|
3
|
+
from PySide6.QtWebChannel import QWebChannel
|
|
4
|
+
from PySide6.QtWebEngineCore import QWebEngineSettings
|
|
5
|
+
from PySide6.QtWebEngineWidgets import QWebEngineView
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
class AxonWebEngineView(QWebEngineView):
|
|
9
|
+
def __init__(self, url: str):
|
|
10
|
+
super().__init__()
|
|
11
|
+
# webChannel 必须先于 load(): 页面里的 qwebchannel.js 一执行就会找
|
|
12
|
+
# qt.webChannelTransport, 先 load 再 setWebChannel 属于竞态。
|
|
13
|
+
self.webChannel = QWebChannel()
|
|
14
|
+
self.page().setWebChannel(self.webChannel)
|
|
15
|
+
self.channelDict: dict[str, QObject] = {}
|
|
16
|
+
self.get_channel = self.channelDict.get
|
|
17
|
+
# settings https://doc.qt.io/qtforpython-6/PySide6/QtWebEngineCore/QWebEngineSettings.html#qwebenginesettings
|
|
18
|
+
self.page().settings().setAttribute(
|
|
19
|
+
QWebEngineSettings.WebAttribute.JavascriptCanAccessClipboard, True
|
|
20
|
+
)
|
|
21
|
+
self.page().settings().setAttribute(
|
|
22
|
+
QWebEngineSettings.WebAttribute.LocalContentCanAccessRemoteUrls, True
|
|
23
|
+
)
|
|
24
|
+
self.page().settings().setAttribute(
|
|
25
|
+
QWebEngineSettings.WebAttribute.LocalContentCanAccessFileUrls, True
|
|
26
|
+
)
|
|
27
|
+
self.page().settings().setAttribute(
|
|
28
|
+
QWebEngineSettings.WebAttribute.FullScreenSupportEnabled, True
|
|
29
|
+
)
|
|
30
|
+
self.page().settings().setAttribute(
|
|
31
|
+
QWebEngineSettings.WebAttribute.AllowRunningInsecureContent, True
|
|
32
|
+
)
|
|
33
|
+
self.page().settings().setAttribute(
|
|
34
|
+
QWebEngineSettings.WebAttribute.JavascriptCanPaste, True
|
|
35
|
+
)
|
|
36
|
+
self.page().settings().setAttribute(
|
|
37
|
+
QWebEngineSettings.WebAttribute.ScrollAnimatorEnabled, True
|
|
38
|
+
)
|
|
39
|
+
# 通道与 settings 都就绪后再真正开始加载
|
|
40
|
+
self.load(QUrl(url))
|
|
41
|
+
|
|
42
|
+
def registerChannel(self, id: str, object: QObject):
|
|
43
|
+
self.webChannel.registerObject(id, object)
|
|
44
|
+
self.channelDict[id] = object
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
from typing import TYPE_CHECKING
|
|
4
|
+
|
|
5
|
+
from PySide6.QtCore import Property, QObject, Slot
|
|
6
|
+
|
|
7
|
+
if TYPE_CHECKING:
|
|
8
|
+
from .AxonGUI import AxonGUI
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
class AxonEnv(QObject):
|
|
12
|
+
"""暴露给 JS 的环境对象(通道里叫 AxonEnv)。"""
|
|
13
|
+
|
|
14
|
+
def __init__(self, web: AxonGUI):
|
|
15
|
+
super().__init__()
|
|
16
|
+
self._web = web
|
|
17
|
+
self._web_name = web.web_name
|
|
18
|
+
|
|
19
|
+
@Property(str, constant=True)
|
|
20
|
+
def web_name(self):
|
|
21
|
+
return self._web_name
|
|
22
|
+
|
|
23
|
+
@Slot()
|
|
24
|
+
def ready(self):
|
|
25
|
+
"""JS 侧 AxonInit() 完成后回调,标记「前端已经能收事件了」。
|
|
26
|
+
|
|
27
|
+
页面加载完成(loadFinished)早于前端通道就绪是常态: 前端此时还没
|
|
28
|
+
连上 onCallSignal, 直接派发 loadFinished 会丢。所以由前端主动报到,
|
|
29
|
+
后端再把攒着的事件派发出去。
|
|
30
|
+
"""
|
|
31
|
+
self._web.mark_js_channel_ready()
|
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
import threading
|
|
2
|
+
import traceback
|
|
3
|
+
from collections import defaultdict
|
|
4
|
+
from typing import Any, Callable, Literal, Optional, TypedDict
|
|
5
|
+
|
|
6
|
+
from .AxonJSCallHandler import AxonController
|
|
7
|
+
from .AxonPYCall import AxonCall
|
|
8
|
+
|
|
9
|
+
EventType = Literal["test"] | str
|
|
10
|
+
EventListener = Callable[..., Any]
|
|
11
|
+
RawEventListener = Callable[..., Any]
|
|
12
|
+
EventListeners = defaultdict[EventType, set[EventListener]]
|
|
13
|
+
OnceEventListenerMap = defaultdict[EventType, dict[RawEventListener, EventListener]]
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
class AddEventListenerOptions(TypedDict):
|
|
17
|
+
once: bool
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
class _AxonEvent:
|
|
21
|
+
def __init__(self):
|
|
22
|
+
self.__listeners: EventListeners = defaultdict(set)
|
|
23
|
+
self.__once_event_listener_map: OnceEventListenerMap = defaultdict(dict)
|
|
24
|
+
self.__lock = threading.RLock()
|
|
25
|
+
self.on = self.add_listener
|
|
26
|
+
self.off = self.remove_listener
|
|
27
|
+
|
|
28
|
+
def add_listener(
|
|
29
|
+
self,
|
|
30
|
+
type: EventType,
|
|
31
|
+
listener: EventListener,
|
|
32
|
+
options: Optional[AddEventListenerOptions] = None,
|
|
33
|
+
):
|
|
34
|
+
with self.__lock:
|
|
35
|
+
if listener in self.__listeners[type]:
|
|
36
|
+
return
|
|
37
|
+
if listener in self.__once_event_listener_map[type]:
|
|
38
|
+
return
|
|
39
|
+
if options and options.get("once"):
|
|
40
|
+
raw_listener = listener
|
|
41
|
+
|
|
42
|
+
def wrapper(*args):
|
|
43
|
+
self.remove_listener(type, raw_listener)
|
|
44
|
+
return raw_listener(*args)
|
|
45
|
+
|
|
46
|
+
self.__once_event_listener_map[type][raw_listener] = wrapper
|
|
47
|
+
listener = wrapper
|
|
48
|
+
self.__listeners[type].add(listener)
|
|
49
|
+
|
|
50
|
+
def remove_listener(self, type: EventType, listener: EventListener):
|
|
51
|
+
with self.__lock:
|
|
52
|
+
wrapper = self.__once_event_listener_map[type].pop(listener, None)
|
|
53
|
+
if wrapper:
|
|
54
|
+
self.__listeners[type].discard(wrapper)
|
|
55
|
+
return
|
|
56
|
+
self.__listeners[type].discard(listener)
|
|
57
|
+
|
|
58
|
+
def remove_all_listeners(self, type: EventType):
|
|
59
|
+
with self.__lock:
|
|
60
|
+
self.__listeners[type].clear()
|
|
61
|
+
self.__once_event_listener_map[type].clear()
|
|
62
|
+
|
|
63
|
+
def dispatch_event(self, type: EventType, *args):
|
|
64
|
+
with self.__lock:
|
|
65
|
+
for listener in self.__listeners[type].copy():
|
|
66
|
+
try:
|
|
67
|
+
listener(*args)
|
|
68
|
+
except Exception as e:
|
|
69
|
+
# 捕获异常避免影响其他监听器
|
|
70
|
+
print(f"事件监听器执行异常 - 事件类型: {type}")
|
|
71
|
+
print(f"异常信息: {str(e)}")
|
|
72
|
+
print("堆栈跟踪:")
|
|
73
|
+
traceback.print_exc()
|
|
74
|
+
|
|
75
|
+
@staticmethod
|
|
76
|
+
def dispatch_js_event(web_name: str, type: str, args: list | tuple = []):
|
|
77
|
+
|
|
78
|
+
return AxonCall.call(web_name, "AxonEvent", "dispatchEvent", [type, *args])
|
|
79
|
+
|
|
80
|
+
@staticmethod
|
|
81
|
+
def dispatch_js_event_batch(
|
|
82
|
+
type: str, args: list | tuple = [], web_names: Literal["*"] | list[str] = "*"
|
|
83
|
+
):
|
|
84
|
+
from .AxonGUI import AxonGUI
|
|
85
|
+
|
|
86
|
+
_web_names = AxonGUI.get_web_names() if web_names == "*" else web_names
|
|
87
|
+
return [_AxonEvent.dispatch_js_event(wn, type, args) for wn in _web_names]
|
|
88
|
+
|
|
89
|
+
|
|
90
|
+
AxonEvent = _AxonEvent()
|
|
91
|
+
|
|
92
|
+
AxonController.register(AxonEvent, id="AxonEvent")
|
|
@@ -0,0 +1,153 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
from typing import Iterable, Optional, TypedDict
|
|
4
|
+
|
|
5
|
+
from PySide6 import QtGui
|
|
6
|
+
from PySide6.QtCore import QTimer
|
|
7
|
+
|
|
8
|
+
from .AxonEngineView import AxonWebEngineView
|
|
9
|
+
from .AxonEvent import AxonEvent
|
|
10
|
+
|
|
11
|
+
# need import to register controller
|
|
12
|
+
from .AxonStore import AxonStoreBase, AxonStoreSyncManager # noqa: F401
|
|
13
|
+
|
|
14
|
+
WebName = str
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
class AxonGUIOptions(TypedDict, total=False):
|
|
18
|
+
# 是否禁用 Qt 原生右鍵菜單 (設為 True 以支持前端自定義菜單)
|
|
19
|
+
disable_native_context_menu: bool
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
# noinspection DuplicatedCode,PyShadowingBuiltins
|
|
23
|
+
class AxonGUI(AxonWebEngineView):
|
|
24
|
+
instance_dict: dict[WebName, AxonGUI] = {}
|
|
25
|
+
|
|
26
|
+
get_web = instance_dict.get
|
|
27
|
+
|
|
28
|
+
@classmethod
|
|
29
|
+
def get_web_names(cls) -> Iterable[WebName]:
|
|
30
|
+
return cls.instance_dict.keys()
|
|
31
|
+
|
|
32
|
+
def __init__(
|
|
33
|
+
self, web_name: WebName, url: str, options: Optional[AxonGUIOptions] = None
|
|
34
|
+
):
|
|
35
|
+
super().__init__(url)
|
|
36
|
+
self.web_name = web_name
|
|
37
|
+
self.options = options or {}
|
|
38
|
+
# 实例字典
|
|
39
|
+
self.instance_dict[web_name] = self
|
|
40
|
+
# 内置channel注册
|
|
41
|
+
from .AxonEnv import AxonEnv
|
|
42
|
+
from .AxonJSCallHandler import AxonController, AxonJSCallHandler
|
|
43
|
+
from .AxonPYCall import AxonPYCall
|
|
44
|
+
|
|
45
|
+
self.registerChannel("AxonEnv", AxonEnv(self))
|
|
46
|
+
self.registerChannel("AxonJSCallHandler", AxonJSCallHandler())
|
|
47
|
+
self.registerChannel("AxonPYCall", AxonPYCall())
|
|
48
|
+
# 自动注册controller
|
|
49
|
+
AxonController.auto_register(self)
|
|
50
|
+
# loadFinished 只有一个: 页面加载完成 + 前端通道就绪, 两件事都齐了才算数
|
|
51
|
+
self._page_loaded = False
|
|
52
|
+
self._js_ready = False
|
|
53
|
+
self._load_finished_dispatched = False
|
|
54
|
+
self.loadStarted.connect(self._loadStarted)
|
|
55
|
+
self.loadFinished.connect(self._loadFinished)
|
|
56
|
+
|
|
57
|
+
def _loadStarted(self):
|
|
58
|
+
# 重新加载/跳转: 前端通道需要重新握手, 事件重新攒
|
|
59
|
+
self._page_loaded = False
|
|
60
|
+
self._js_ready = False
|
|
61
|
+
self._load_finished_dispatched = False
|
|
62
|
+
|
|
63
|
+
def _loadFinished(self):
|
|
64
|
+
self._page_loaded = True
|
|
65
|
+
self._dispatch_load_finished()
|
|
66
|
+
|
|
67
|
+
def mark_js_channel_ready(self):
|
|
68
|
+
self._js_ready = True
|
|
69
|
+
self._dispatch_load_finished()
|
|
70
|
+
|
|
71
|
+
def _dispatch_load_finished(self):
|
|
72
|
+
if (
|
|
73
|
+
self._page_loaded
|
|
74
|
+
and self._js_ready
|
|
75
|
+
and not self._load_finished_dispatched
|
|
76
|
+
):
|
|
77
|
+
self._load_finished_dispatched = True
|
|
78
|
+
# 延后一轮: 前端 await AxonInit() 之后还要注册监听器
|
|
79
|
+
QTimer.singleShot(
|
|
80
|
+
0,
|
|
81
|
+
lambda: AxonEvent.dispatch_js_event(self.web_name, "loadFinished"),
|
|
82
|
+
)
|
|
83
|
+
|
|
84
|
+
def contextMenuEvent(self, arg__1: QtGui.QContextMenuEvent, /) -> None:
|
|
85
|
+
AxonEvent.dispatch_js_event(
|
|
86
|
+
self.web_name,
|
|
87
|
+
"contextMenu",
|
|
88
|
+
[
|
|
89
|
+
{
|
|
90
|
+
"reason": arg__1.reason().value,
|
|
91
|
+
"globalPos": {
|
|
92
|
+
"x": arg__1.globalPos().x(),
|
|
93
|
+
"y": arg__1.globalPos().y(),
|
|
94
|
+
},
|
|
95
|
+
"pos": {
|
|
96
|
+
"x": arg__1.pos().x(),
|
|
97
|
+
"y": arg__1.pos().y(),
|
|
98
|
+
},
|
|
99
|
+
}
|
|
100
|
+
],
|
|
101
|
+
)
|
|
102
|
+
if not self.options.get("disable_native_context_menu", False):
|
|
103
|
+
super().contextMenuEvent(arg__1)
|
|
104
|
+
|
|
105
|
+
def dragEnterEvent(self, e: QtGui.QDragEnterEvent, /) -> None:
|
|
106
|
+
AxonEvent.dispatch_js_event(self.web_name, "dragEnter", [QDropEventToDict(e)])
|
|
107
|
+
super().dragEnterEvent(e)
|
|
108
|
+
|
|
109
|
+
def dragMoveEvent(self, e: QtGui.QDragMoveEvent, /) -> None:
|
|
110
|
+
AxonEvent.dispatch_js_event(self.web_name, "dragMove", [QDropEventToDict(e)])
|
|
111
|
+
super().dragMoveEvent(e)
|
|
112
|
+
|
|
113
|
+
def dropEvent(self, e: QtGui.QDropEvent, /) -> None:
|
|
114
|
+
AxonEvent.dispatch_js_event(self.web_name, "drop", [QDropEventToDict(e)])
|
|
115
|
+
super().dropEvent(e)
|
|
116
|
+
|
|
117
|
+
def dragLeaveEvent(self, e: QtGui.QDragLeaveEvent, /) -> None:
|
|
118
|
+
AxonEvent.dispatch_js_event(self.web_name, "dragLeave")
|
|
119
|
+
super().dragLeaveEvent(e)
|
|
120
|
+
|
|
121
|
+
def hideEvent(self, arg__1: QtGui.QHideEvent, /) -> None:
|
|
122
|
+
AxonEvent.dispatch_js_event(self.web_name, "hide")
|
|
123
|
+
super().hideEvent(arg__1)
|
|
124
|
+
|
|
125
|
+
def showEvent(self, arg__1: QtGui.QShowEvent, /) -> None:
|
|
126
|
+
AxonEvent.dispatch_js_event(self.web_name, "show")
|
|
127
|
+
super().showEvent(arg__1)
|
|
128
|
+
|
|
129
|
+
|
|
130
|
+
def QDropEventToDict(e: QtGui.QDropEvent):
|
|
131
|
+
return {
|
|
132
|
+
"mimeData": {
|
|
133
|
+
"urls": [u.toLocalFile() for u in e.mimeData().urls()],
|
|
134
|
+
"text": e.mimeData().text(),
|
|
135
|
+
"html": e.mimeData().html(),
|
|
136
|
+
"formats": e.mimeData().formats(),
|
|
137
|
+
},
|
|
138
|
+
"pos": {
|
|
139
|
+
"x": e.pos().x(),
|
|
140
|
+
"y": e.pos().y(),
|
|
141
|
+
},
|
|
142
|
+
"posF": {
|
|
143
|
+
"x": e.posF().x(),
|
|
144
|
+
"y": e.posF().y(),
|
|
145
|
+
},
|
|
146
|
+
"modifiers": e.keyboardModifiers().value,
|
|
147
|
+
"keyboardModifiers": e.keyboardModifiers().value,
|
|
148
|
+
"buttons": e.mouseButtons().value,
|
|
149
|
+
"mouseButtons": e.mouseButtons().value,
|
|
150
|
+
"dropAction": e.dropAction().value,
|
|
151
|
+
"possibleActions": e.possibleActions().value,
|
|
152
|
+
"proposedAction": e.proposedAction().value,
|
|
153
|
+
}
|