charmy-gui 0.0.1__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.
- charmy_gui-0.0.1/LICENSE +51 -0
- charmy_gui-0.0.1/NOTICE +80 -0
- charmy_gui-0.0.1/PKG-INFO +28 -0
- charmy_gui-0.0.1/README.md +9 -0
- charmy_gui-0.0.1/charmy/__init__.py +27 -0
- charmy_gui-0.0.1/charmy/_event.py +418 -0
- charmy_gui-0.0.1/charmy/const.py +39 -0
- charmy_gui-0.0.1/charmy/event.py +414 -0
- charmy_gui-0.0.1/charmy/framework.py +340 -0
- charmy_gui-0.0.1/charmy/object.py +231 -0
- charmy_gui-0.0.1/charmy/pos.py +20 -0
- charmy_gui-0.0.1/charmy/rect.py +88 -0
- charmy_gui-0.0.1/charmy/resources/imgs/icon.ico +0 -0
- charmy_gui-0.0.1/charmy/resources/themes/dark.json +604 -0
- charmy_gui-0.0.1/charmy/resources/themes/light.json +790 -0
- charmy_gui-0.0.1/charmy/resources/themes/sv_1light.json +711 -0
- charmy_gui-0.0.1/charmy/resources/themes/sv_2dark.json +59 -0
- charmy_gui-0.0.1/charmy/size.py +29 -0
- charmy_gui-0.0.1/charmy/styles/__init__.py +1 -0
- charmy_gui-0.0.1/charmy/styles/_theme.py +677 -0
- charmy_gui-0.0.1/charmy/styles/color.py +117 -0
- charmy_gui-0.0.1/charmy/styles/theme.py +217 -0
- charmy_gui-0.0.1/charmy/this.py +54 -0
- charmy_gui-0.0.1/charmy/widgets/__init__.py +7 -0
- charmy_gui-0.0.1/charmy/widgets/canvas.py +94 -0
- charmy_gui-0.0.1/charmy/widgets/cmm.py +198 -0
- charmy_gui-0.0.1/charmy/widgets/container.py +89 -0
- charmy_gui-0.0.1/charmy/widgets/widget.py +19 -0
- charmy_gui-0.0.1/charmy/widgets/window.py +21 -0
- charmy_gui-0.0.1/charmy/widgets/windowbase.py +400 -0
- charmy_gui-0.0.1/pyproject.toml +29 -0
charmy_gui-0.0.1/LICENSE
ADDED
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
Apache License
|
|
2
|
+
Version 2.0, January 2004
|
|
3
|
+
http://www.apache.org/licenses/
|
|
4
|
+
|
|
5
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
6
|
+
|
|
7
|
+
1. Definitions.
|
|
8
|
+
|
|
9
|
+
"License" shall mean the terms and conditions for use, reproduction, and distribution as defined by Sections 1 through 9 of this document.
|
|
10
|
+
|
|
11
|
+
"Licensor" shall mean the copyright owner or entity authorized by the copyright owner that is granting the License.
|
|
12
|
+
|
|
13
|
+
"Legal Entity" shall mean the union of the acting entity and all other entities that control, are controlled by, or are under common control with that entity. For the purposes of this definition, "control" means (i) the power, direct or indirect, to cause the direction or management of such entity, whether by contract or otherwise, or (ii) ownership of fifty percent (50%) or more of the outstanding shares, or (iii) beneficial ownership of such entity.
|
|
14
|
+
|
|
15
|
+
"You" (or "Your") shall mean an individual or Legal Entity exercising permissions granted by this License.
|
|
16
|
+
|
|
17
|
+
"Source" form shall mean the preferred form for making modifications, including but not limited to software source code, documentation source, and configuration files.
|
|
18
|
+
|
|
19
|
+
"Object" form shall mean any form resulting from mechanical transformation or translation of a Source form, including but not limited to compiled object code, generated documentation, and conversions to other media types.
|
|
20
|
+
|
|
21
|
+
"Work" shall mean the work of authorship, whether in Source or Object form, made available under the License, as indicated by a copyright notice that is included in or attached to the work (an example is provided in the Appendix below).
|
|
22
|
+
|
|
23
|
+
"Derivative Works" shall mean any work, whether in Source or Object form, that is based on (or derived from) the Work and for which the editorial revisions, annotations, elaborations, or other modifications represent, as a whole, an original work of authorship. For the purposes of this License, Derivative Works shall not include works that remain separable from, or merely link (or bind by name) to the interfaces of, the Work and Derivative Works thereof.
|
|
24
|
+
|
|
25
|
+
"Contribution" shall mean any work of authorship, including the original version of the Work and any modifications or additions to that Work or Derivative Works thereof, that is intentionally submitted to Licensor for inclusion in the Work by the copyright owner or by an individual or Legal Entity authorized to submit on behalf of the copyright owner. For the purposes of this definition, "submitted" means any form of electronic, verbal, or written communication sent to the Licensor or its representatives, including but not limited to communication on electronic mailing lists, source code control systems, and issue tracking systems that are managed by, or on behalf of, the Licensor for the purpose of discussing and improving the Work, but excluding communication that is conspicuously marked or otherwise designated in writing by the copyright owner as "Not a Contribution."
|
|
26
|
+
|
|
27
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity on behalf of whom a Contribution has been received by Licensor and subsequently incorporated within the Work.
|
|
28
|
+
|
|
29
|
+
2. Grant of Copyright License. Subject to the terms and conditions of this License, each Contributor hereby grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable copyright license to reproduce, prepare Derivative Works of, publicly display, publicly perform, sublicense, and distribute the Work and such Derivative Works in Source or Object form.
|
|
30
|
+
|
|
31
|
+
3. Grant of Patent License. Subject to the terms and conditions of this License, each Contributor hereby grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable (except as stated in this section) patent license to make, have made, use, offer to sell, sell, import, and otherwise transfer the Work, where such license applies only to those patent claims licensable by such Contributor that are necessarily infringed by their Contribution(s) alone or by combination of their Contribution(s) with the Work to which such Contribution(s) was submitted. If You institute patent litigation against any entity (including a cross-claim or counterclaim in a lawsuit) alleging that the Work or a Contribution incorporated within the Work constitutes direct or contributory patent infringement, then any patent licenses granted to You under this License for that Work shall terminate as of the date such litigation is filed.
|
|
32
|
+
|
|
33
|
+
4. Redistribution. You may reproduce and distribute copies of the Work or Derivative Works thereof in any medium, with or without modifications, and in Source or Object form, provided that You meet the following conditions:
|
|
34
|
+
|
|
35
|
+
You must give any other recipients of the Work or Derivative Works a copy of this License; and
|
|
36
|
+
You must cause any modified files to carry prominent notices stating that You changed the files; and
|
|
37
|
+
You must retain, in the Source form of any Derivative Works that You distribute, all copyright, patent, trademark, and attribution notices from the Source form of the Work, excluding those notices that do not pertain to any part of the Derivative Works; and
|
|
38
|
+
If the Work includes a "NOTICE" text file as part of its distribution, then any Derivative Works that You distribute must include a readable copy of the attribution notices contained within such NOTICE file, excluding those notices that do not pertain to any part of the Derivative Works, in at least one of the following places: within a NOTICE text file distributed as part of the Derivative Works; within the Source form or documentation, if provided along with the Derivative Works; or, within a display generated by the Derivative Works, if and wherever such third-party notices normally appear. The contents of the NOTICE file are for informational purposes only and do not modify the License. You may add Your own attribution notices within Derivative Works that You distribute, alongside or as an addendum to the NOTICE text from the Work, provided that such additional attribution notices cannot be construed as modifying the License.
|
|
39
|
+
You may add Your own copyright statement to Your modifications and may provide additional or different license terms and conditions for use, reproduction, or distribution of Your modifications, or for any such Derivative Works as a whole, provided Your use, reproduction, and distribution of the Work otherwise complies with the conditions stated in this License.
|
|
40
|
+
|
|
41
|
+
5. Submission of Contributions. Unless You explicitly state otherwise, any Contribution intentionally submitted for inclusion in the Work by You to the Licensor shall be under the terms and conditions of this License, without any additional terms or conditions. Notwithstanding the above, nothing herein shall supersede or modify the terms of any separate license agreement you may have executed with Licensor regarding such Contributions.
|
|
42
|
+
|
|
43
|
+
6. Trademarks. This License does not grant permission to use the trade names, trademarks, service marks, or product names of the Licensor, except as required for reasonable and customary use in describing the origin of the Work and reproducing the content of the NOTICE file.
|
|
44
|
+
|
|
45
|
+
7. Disclaimer of Warranty. Unless required by applicable law or agreed to in writing, Licensor provides the Work (and each Contributor provides its Contributions) on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied, including, without limitation, any warranties or conditions of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A PARTICULAR PURPOSE. You are solely responsible for determining the appropriateness of using or redistributing the Work and assume any risks associated with Your exercise of permissions under this License.
|
|
46
|
+
|
|
47
|
+
8. Limitation of Liability. In no event and under no legal theory, whether in tort (including negligence), contract, or otherwise, unless required by applicable law (such as deliberate and grossly negligent acts) or agreed to in writing, shall any Contributor be liable to You for damages, including any direct, indirect, special, incidental, or consequential damages of any character arising as a result of this License or out of the use or inability to use the Work (including but not limited to damages for loss of goodwill, work stoppage, computer failure or malfunction, or any and all other commercial damages or losses), even if such Contributor has been advised of the possibility of such damages.
|
|
48
|
+
|
|
49
|
+
9. Accepting Warranty or Additional Liability. While redistributing the Work or Derivative Works thereof, You may choose to offer, and charge a fee for, acceptance of support, warranty, indemnity, or other liability obligations and/or rights consistent with this License. However, in accepting such obligations, You may act only on Your own behalf and on Your sole responsibility, not on behalf of any other Contributor, and only if You agree to indemnify, defend, and hold each Contributor harmless for any liability incurred by, or claims asserted against, such Contributor by reason of your accepting any such warranty or additional liability.
|
|
50
|
+
|
|
51
|
+
END OF TERMS AND CONDITIONS
|
charmy_gui-0.0.1/NOTICE
ADDED
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
NOTICE
|
|
2
|
+
========
|
|
3
|
+
|
|
4
|
+
Copyright (C) 2026 XiangQinxi <XiangQinxi@outlook.com>
|
|
5
|
+
|
|
6
|
+
This software is licensed under the Apache License, Version 2.0 (the "License");
|
|
7
|
+
you may not use this file except in compliance with the License.
|
|
8
|
+
You may obtain a copy of the License at
|
|
9
|
+
|
|
10
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
11
|
+
|
|
12
|
+
Unless required by applicable law or agreed to in writing, software
|
|
13
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
14
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
15
|
+
See the License for the specific language governing permissions and
|
|
16
|
+
limitations under the License.
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
THIRD-PARTY DEPENDENCIES
|
|
20
|
+
========================
|
|
21
|
+
|
|
22
|
+
This software includes the following third-party dependencies:
|
|
23
|
+
|
|
24
|
+
1. json5
|
|
25
|
+
- License: Apache-2.0
|
|
26
|
+
- URL: https://github.com/json5/json5
|
|
27
|
+
|
|
28
|
+
2. skia-python
|
|
29
|
+
- License: BSD-3-Clause
|
|
30
|
+
- URL: https://github.com/kyamagu/skia-python
|
|
31
|
+
|
|
32
|
+
3. glfw
|
|
33
|
+
- License: zlib/libpng
|
|
34
|
+
- URL: https://github.com/glfw/glfw
|
|
35
|
+
|
|
36
|
+
4. PyOpenGL
|
|
37
|
+
- License: BSD-3-Clause
|
|
38
|
+
- URL: http://pyopengl.sourceforge.net/
|
|
39
|
+
|
|
40
|
+
5. Poetry Core
|
|
41
|
+
- License: MIT
|
|
42
|
+
- URL: https://github.com/python-poetry/poetry-core
|
|
43
|
+
|
|
44
|
+
Optional Dependencies (for documentation):
|
|
45
|
+
6. myst-parser
|
|
46
|
+
- License: MIT
|
|
47
|
+
- URL: https://github.com/executablebooks/MyST-Parser
|
|
48
|
+
|
|
49
|
+
7. sphinx-design
|
|
50
|
+
- License: MIT
|
|
51
|
+
- URL: https://github.com/executablebooks/sphinx-design
|
|
52
|
+
|
|
53
|
+
8. furo
|
|
54
|
+
- License: MIT
|
|
55
|
+
- URL: https://github.com/pradyunsg/furo
|
|
56
|
+
|
|
57
|
+
9. sphinx-copybutton
|
|
58
|
+
- License: MIT
|
|
59
|
+
- URL: https://github.com/executablebooks/sphinx-copybutton
|
|
60
|
+
|
|
61
|
+
10. sphinx
|
|
62
|
+
- License: BSD-2-Clause
|
|
63
|
+
- URL: https://github.com/sphinx-doc/sphinx
|
|
64
|
+
|
|
65
|
+
|
|
66
|
+
ATTRIBUTIONS
|
|
67
|
+
============
|
|
68
|
+
|
|
69
|
+
This product includes software developed by:
|
|
70
|
+
- The JSON5 Project (https://github.com/json5/json5)
|
|
71
|
+
- The Skia Graphics Library (https://skia.org/)
|
|
72
|
+
- The GLFW Project (https://www.glfw.org/)
|
|
73
|
+
- The PyOpenGL Project (http://pyopengl.sourceforge.net/)
|
|
74
|
+
- The Python Poetry Project (https://python-poetry.org/)
|
|
75
|
+
- The Sphinx Documentation Project (https://www.sphinx-doc.org/)
|
|
76
|
+
|
|
77
|
+
For commercial licensing inquiries, please contact:
|
|
78
|
+
XiangQinxi <XiangQinxi@outlook.com>
|
|
79
|
+
|
|
80
|
+
Generated: 2026-02-16
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: charmy-gui
|
|
3
|
+
Version: 0.0.1
|
|
4
|
+
Summary:
|
|
5
|
+
License: Apache 2.0
|
|
6
|
+
License-File: LICENSE
|
|
7
|
+
License-File: NOTICE
|
|
8
|
+
Author: XiangQinxi
|
|
9
|
+
Author-email: XiangQinxi@outlook.com
|
|
10
|
+
Requires-Python: >=3.11
|
|
11
|
+
Classifier: License :: Other/Proprietary License
|
|
12
|
+
Classifier: Programming Language :: Python :: 3
|
|
13
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
14
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
15
|
+
Classifier: Programming Language :: Python :: 3.13
|
|
16
|
+
Classifier: Programming Language :: Python :: 3.14
|
|
17
|
+
Requires-Dist: json5
|
|
18
|
+
Description-Content-Type: text/markdown
|
|
19
|
+
|
|
20
|
+
# Charmy GUI
|
|
21
|
+
|
|
22
|
+
`Suzaku`的重制版。
|
|
23
|
+
|
|
24
|
+
## 依赖
|
|
25
|
+
### 组合一(默认):
|
|
26
|
+
1. skia
|
|
27
|
+
2. glfw
|
|
28
|
+
3. pyopengl
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
# Copyright (C) 2026 XiangQinxi
|
|
2
|
+
#
|
|
3
|
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
4
|
+
# you may not use this file except in compliance with the License.
|
|
5
|
+
# You may obtain a copy of the License at
|
|
6
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
|
7
|
+
# Unless required by applicable law or agreed to in writing, software
|
|
8
|
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
9
|
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
10
|
+
# See the License for the specific language governing permissions and
|
|
11
|
+
# limitations under the License.
|
|
12
|
+
|
|
13
|
+
# Author: XiangQinxi
|
|
14
|
+
# Contributors: XiangQinxi, rgzz666, littlewhitecloud
|
|
15
|
+
|
|
16
|
+
"""A modern GUI library."""
|
|
17
|
+
|
|
18
|
+
from .const import *
|
|
19
|
+
|
|
20
|
+
# from .drawing import *
|
|
21
|
+
from .event import Event, EventHandling, EventTask, WorkingThread
|
|
22
|
+
from .object import CharmyObject
|
|
23
|
+
from .pos import Pos
|
|
24
|
+
from .rect import Rect
|
|
25
|
+
from .size import Size
|
|
26
|
+
from .styles import *
|
|
27
|
+
from .widgets import *
|
|
@@ -0,0 +1,418 @@
|
|
|
1
|
+
from __future__ import annotations as _
|
|
2
|
+
|
|
3
|
+
import collections.abc
|
|
4
|
+
import re
|
|
5
|
+
import threading
|
|
6
|
+
import time
|
|
7
|
+
import typing
|
|
8
|
+
import warnings
|
|
9
|
+
|
|
10
|
+
# [TODO] Implementation of repeat events
|
|
11
|
+
# [TODO] Fix a type error in CEventHandling.bind()
|
|
12
|
+
# [TODO] Support unbind for another widget's event
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
class CBoundTask:
|
|
16
|
+
"""A class to represent bound Task when a event is triggered."""
|
|
17
|
+
|
|
18
|
+
def __init__(
|
|
19
|
+
self,
|
|
20
|
+
id_: str,
|
|
21
|
+
target: typing.Callable | typing.Iterable,
|
|
22
|
+
multithread: bool = False,
|
|
23
|
+
_keep_at_clear: bool = False,
|
|
24
|
+
):
|
|
25
|
+
"""Each object is to represent a Task bound to the event.
|
|
26
|
+
|
|
27
|
+
Example
|
|
28
|
+
-------
|
|
29
|
+
This is mostly for internal use of suzaku.
|
|
30
|
+
.. code-block:: python
|
|
31
|
+
class CEventHandling():
|
|
32
|
+
def bind(self, ...):
|
|
33
|
+
...
|
|
34
|
+
Task = CBoundTask(event_id, target, multithread, _keep_at_clear)
|
|
35
|
+
...
|
|
36
|
+
This shows where this class is used for storing Task properties in most cases.
|
|
37
|
+
|
|
38
|
+
:param id_: The Task id of this Task
|
|
39
|
+
:param target: A callable thing, what to do when this Task is executed
|
|
40
|
+
:param multithread: If this Task should be executed in another thread (False by default)
|
|
41
|
+
:param _keep_at_clear: If the Task should be kept when cleaning the event's binding
|
|
42
|
+
"""
|
|
43
|
+
self.id: str = id_
|
|
44
|
+
self.target: typing.Callable | typing.Iterable = target
|
|
45
|
+
self.multithread: bool = multithread
|
|
46
|
+
self.keep_at_clear: bool = _keep_at_clear
|
|
47
|
+
|
|
48
|
+
|
|
49
|
+
class CDelayTask(CBoundTask):
|
|
50
|
+
"""A class to represent delay Tasks"""
|
|
51
|
+
|
|
52
|
+
def __init__(
|
|
53
|
+
self,
|
|
54
|
+
id_: str,
|
|
55
|
+
target: typing.Callable | typing.Iterable,
|
|
56
|
+
delay_: str,
|
|
57
|
+
*args,
|
|
58
|
+
**kwargs,
|
|
59
|
+
):
|
|
60
|
+
"""Inherited from CBoundTask, used to store Tasks bound to `delay` events.
|
|
61
|
+
|
|
62
|
+
:param delay: Time to delay, in seconds, indicating how log to wait before the Task is
|
|
63
|
+
executed.
|
|
64
|
+
:param (Other): See `CBoundTask.__init__()`
|
|
65
|
+
"""
|
|
66
|
+
CBoundTask.__init__(self, id_, target, *args, **kwargs) # For other things,same as
|
|
67
|
+
# CBoundTask
|
|
68
|
+
if delay_.endswith("s"):
|
|
69
|
+
if delay_.endswith("ms"):
|
|
70
|
+
delay_ = float(delay_[:-2]) / 1000
|
|
71
|
+
else:
|
|
72
|
+
delay_ = float(delay_[:-1])
|
|
73
|
+
else:
|
|
74
|
+
delay_ = float(delay_)
|
|
75
|
+
self.target_time = float(time.time()) + delay_ # To store when to execute the Task
|
|
76
|
+
|
|
77
|
+
|
|
78
|
+
class CRepeatTask(CBoundTask):
|
|
79
|
+
"""A class to represent repeat Tasks"""
|
|
80
|
+
|
|
81
|
+
def __init__(self, id_: str, target: typing.Callable, interval, *args, **kwargs):
|
|
82
|
+
"""Inherited from CBoundTask, used to store Tasks bound to `repeat` events.
|
|
83
|
+
|
|
84
|
+
:param delay: Time to delay, in seconds, indicating how log to wait before the Task is
|
|
85
|
+
executed.
|
|
86
|
+
"""
|
|
87
|
+
CBoundTask.__init__(self, id_, target, *args, **kwargs) # For other things,same as
|
|
88
|
+
# CBoundTask
|
|
89
|
+
self.target_time = float(time.time()) + interval # To store when to execute the Task for
|
|
90
|
+
# the next time, will be accumulated after
|
|
91
|
+
# execution of the Task
|
|
92
|
+
self.interval = interval # Interval of the Task
|
|
93
|
+
|
|
94
|
+
|
|
95
|
+
class CEventHandling:
|
|
96
|
+
"""A class containing event handling abilities.
|
|
97
|
+
|
|
98
|
+
This class should be inherited by other classes with such abilities.
|
|
99
|
+
|
|
100
|
+
Events should be represented in the form of `event_type` or `event_type[args]`. e.g. `delay` or
|
|
101
|
+
`delay[500]`
|
|
102
|
+
"""
|
|
103
|
+
|
|
104
|
+
# fmt: off
|
|
105
|
+
EVENT_TYPES: list[str] = [
|
|
106
|
+
"resize", "move",
|
|
107
|
+
"configure", "update", "redraw",
|
|
108
|
+
"mouse_move", "mouse_enter", "mouse_leave", "mouse_press", "mouse_release", "click", "double_click",
|
|
109
|
+
"focus_gain", "focus_loss",
|
|
110
|
+
"key_press", "key_release", "key_repeat", "char",
|
|
111
|
+
"delay", "repeat", # This row shows special event type(s)
|
|
112
|
+
]
|
|
113
|
+
# fmt: on
|
|
114
|
+
multithread_Tasks: list[tuple[CBoundTask, CEvent]] = []
|
|
115
|
+
WORKING_THREAD: threading.Thread
|
|
116
|
+
instance_count = 0
|
|
117
|
+
|
|
118
|
+
@classmethod
|
|
119
|
+
def _working_thread_loop(cls):
|
|
120
|
+
while True:
|
|
121
|
+
try:
|
|
122
|
+
CEventHandling._execute_Task(
|
|
123
|
+
cls.multithread_Tasks[0][0], cls.multithread_Tasks[0][1]
|
|
124
|
+
)
|
|
125
|
+
# For line above: [0][0] is Task object, [0][1] is CEvent object
|
|
126
|
+
except Exception as e:
|
|
127
|
+
warnings.warn(
|
|
128
|
+
RuntimeWarning(
|
|
129
|
+
"Error in multithread suzaku-event-bound Task "
|
|
130
|
+
f"with ID {cls.multithread_Tasks[0][0].id}, "
|
|
131
|
+
f'detailed error info: "{str(e)}". '
|
|
132
|
+
"The working thread will head to the next Task "
|
|
133
|
+
"(Cipping current causing the error)."
|
|
134
|
+
)
|
|
135
|
+
)
|
|
136
|
+
cls.multithread_Tasks.pop(0) # Executed Tasks should be removed, anyway
|
|
137
|
+
|
|
138
|
+
if not cls.multithread_Tasks:
|
|
139
|
+
time.sleep(0.01) # Avoid CPU draining while no Tasks avail
|
|
140
|
+
continue
|
|
141
|
+
|
|
142
|
+
@staticmethod
|
|
143
|
+
def _execute_Task(Task: CBoundTask, event_obj: CEvent) -> None:
|
|
144
|
+
"""To execute the binded Task directly, regardless its props, mainly for internal use."""
|
|
145
|
+
match Task.target:
|
|
146
|
+
case _ if callable(Task.target):
|
|
147
|
+
Task.target(event_obj)
|
|
148
|
+
case _ if isinstance(Task.target, collections.abc.Iterable):
|
|
149
|
+
for Task_step in Task.target:
|
|
150
|
+
Task_step(event_obj)
|
|
151
|
+
case _:
|
|
152
|
+
raise ValueError(
|
|
153
|
+
"Error type for suzaku Task target! Excepted callable or "
|
|
154
|
+
f"iterable but received {type(Task.target)}"
|
|
155
|
+
)
|
|
156
|
+
|
|
157
|
+
def __init__(self):
|
|
158
|
+
"""A class containing event handling abilities.
|
|
159
|
+
|
|
160
|
+
Example
|
|
161
|
+
-------
|
|
162
|
+
This is mostly for internal use of Charmy.
|
|
163
|
+
.. code-block:: python
|
|
164
|
+
class CWidget(CEventHandling, ...):
|
|
165
|
+
def __init__(self):
|
|
166
|
+
super().__init__(self)
|
|
167
|
+
...
|
|
168
|
+
This shows subclassing CEventHandling to let CWidget gain the ability of handling events.
|
|
169
|
+
"""
|
|
170
|
+
self.latest_event: CEvent = CEvent(widget=None, event_type="NO_EVENT")
|
|
171
|
+
self.Tasks: dict[str, list[CBoundTask]] = {}
|
|
172
|
+
# Make a initial ID here as it will be needed anyway even if the object does not have an ID.
|
|
173
|
+
self.id = f"{self.__class__.__name__}{self.__class__.instance_count}"
|
|
174
|
+
## Initialize Tasks list
|
|
175
|
+
for event_type in self.__class__.EVENT_TYPES:
|
|
176
|
+
self.Tasks[event_type] = []
|
|
177
|
+
## Accumulate instance count
|
|
178
|
+
self.__class__.instance_count += 1
|
|
179
|
+
# Event binds
|
|
180
|
+
self.bind("update", self._check_delay_events, _keep_at_clear=True) # Delay checking loop
|
|
181
|
+
|
|
182
|
+
def parse_event_type_str(self, event_type_str) -> dict:
|
|
183
|
+
"""This function parses event type string.
|
|
184
|
+
|
|
185
|
+
:param event_type_str: The event type string to be parsed
|
|
186
|
+
:returns: json, parsed event type
|
|
187
|
+
"""
|
|
188
|
+
if not re.match(".*\\[.*\\]", event_type_str): # NOQA
|
|
189
|
+
return {"type": event_type_str, "params": []}
|
|
190
|
+
event_type = re.findall("^(.*?)\\[", event_type_str)[0]
|
|
191
|
+
params_raw = re.findall("\\[(.*?)\\]$", event_type_str)[0] # NOQA
|
|
192
|
+
params = params_raw.split(",")
|
|
193
|
+
if len(params) == 1:
|
|
194
|
+
if params[0].strip() == "":
|
|
195
|
+
params = []
|
|
196
|
+
return {"type": event_type, "params": params}
|
|
197
|
+
|
|
198
|
+
def execute_Task(self, Task: CBoundTask, event_obj: CEvent | None = None):
|
|
199
|
+
"""To execute a Task
|
|
200
|
+
|
|
201
|
+
Example
|
|
202
|
+
-------
|
|
203
|
+
.. code-block:: python
|
|
204
|
+
my_Task = CWidget.bind("delay[5]", lambda: print("Hello Suzaku"))
|
|
205
|
+
CWidget.execute_Task(my_Task)
|
|
206
|
+
"""
|
|
207
|
+
if event_obj is None:
|
|
208
|
+
event_obj = CEvent()
|
|
209
|
+
assert event_obj is not None
|
|
210
|
+
if event_obj.widget is None:
|
|
211
|
+
event_obj.widget = self
|
|
212
|
+
if not Task.multithread:
|
|
213
|
+
# If not multiTask, execute directly
|
|
214
|
+
CEventHandling._execute_Task(Task, event_obj)
|
|
215
|
+
# If is a delay event, it should be removed right after execution
|
|
216
|
+
if isinstance(Task, CDelayTask):
|
|
217
|
+
self.unbind(Task)
|
|
218
|
+
else:
|
|
219
|
+
# Otherwise add to multithread Tasks list and let the working thread to deal with it
|
|
220
|
+
# If is a delay Task, should add some code to let it unbind itself, here is a way,
|
|
221
|
+
# which is absolutely not perfect, though works, to implement this mechanism, by
|
|
222
|
+
# overriding its target with a modified version
|
|
223
|
+
def self_destruct_template(Task, event_obj):
|
|
224
|
+
CEventHandling._execute_Task(Task, event_obj)
|
|
225
|
+
self.unbind(Task)
|
|
226
|
+
|
|
227
|
+
if isinstance(Task, CDelayTask):
|
|
228
|
+
Task.target = lambda event_obj: self_destruct_template(Task, event_obj)
|
|
229
|
+
CEventHandling.multithread_Tasks.append((Task, event_obj))
|
|
230
|
+
|
|
231
|
+
def trigger(self, event_type: str, event_obj: CEvent | None = None) -> None:
|
|
232
|
+
"""To trigger a type of event
|
|
233
|
+
|
|
234
|
+
Example
|
|
235
|
+
-------
|
|
236
|
+
.. code-block:: python
|
|
237
|
+
class CWidget(CEventHandling, ...):
|
|
238
|
+
...
|
|
239
|
+
|
|
240
|
+
my_widget = CWidget()
|
|
241
|
+
my_widget.trigger("mouse_press")
|
|
242
|
+
This shows triggering a `mouse_press` event in a `CWidget`, which inherited `CEventHandling` so has the
|
|
243
|
+
ability to handle events.
|
|
244
|
+
|
|
245
|
+
:param event_type: The type of event to trigger
|
|
246
|
+
"""
|
|
247
|
+
# Parse event type string
|
|
248
|
+
parsed_event_type = self.parse_event_type_str(event_type)
|
|
249
|
+
# Create a default CEvent object if not specified
|
|
250
|
+
if event_obj is None:
|
|
251
|
+
event_obj = CEvent(widget=self, event_type=tuple(parsed_event_type.keys())[0])
|
|
252
|
+
# Add the event to event lists (the widget itself and the global list)
|
|
253
|
+
self.latest_event = event_obj
|
|
254
|
+
CEvent.latest = event_obj
|
|
255
|
+
# Find targets
|
|
256
|
+
targets = [parsed_event_type["type"]]
|
|
257
|
+
if not parsed_event_type["params"]:
|
|
258
|
+
targets.append(parsed_event_type["type"] + "[*]")
|
|
259
|
+
else:
|
|
260
|
+
targets.append(event_type)
|
|
261
|
+
# if parsed_event_type["params"][0] in ["", "*"]: # If match all
|
|
262
|
+
# targets.append(parsed_event_type["type"])
|
|
263
|
+
# targets.append(parsed_event_type["type"] + "[*]")
|
|
264
|
+
for target in targets:
|
|
265
|
+
if target in self.Tasks:
|
|
266
|
+
for Task in self.Tasks[target]:
|
|
267
|
+
# To execute all Tasks bound under this event
|
|
268
|
+
self.execute_Task(Task, event_obj)
|
|
269
|
+
|
|
270
|
+
def bind(
|
|
271
|
+
self,
|
|
272
|
+
event_type: str,
|
|
273
|
+
target: typing.Callable | typing.Iterable,
|
|
274
|
+
multithread: bool = False,
|
|
275
|
+
_keep_at_clear: bool = False,
|
|
276
|
+
) -> CBoundTask | bool:
|
|
277
|
+
"""To bind a Task to the object when a specific type of event is triggered.
|
|
278
|
+
|
|
279
|
+
Example
|
|
280
|
+
-------
|
|
281
|
+
.. code-block
|
|
282
|
+
my_button = CButton(...).pack()
|
|
283
|
+
press_down_event = my_button.bind("mouse_press", lambda _: print("Hello world!"))
|
|
284
|
+
This shows binding a hello world to the button when it's press.
|
|
285
|
+
|
|
286
|
+
:param event_type: The type of event to be bound to
|
|
287
|
+
:param target: A (list of) callable thing, what to do when this Task is executed
|
|
288
|
+
:param multithread: If this Task should be executed in another thread (False by default)
|
|
289
|
+
:param _keep_at_clear: If the Task should be kept when cleaning the event's binding
|
|
290
|
+
:return: CBoundTask that is bound to the Task if success, otherwise False
|
|
291
|
+
"""
|
|
292
|
+
parsed_event_type = self.parse_event_type_str(event_type)
|
|
293
|
+
if parsed_event_type["type"] not in self.__class__.EVENT_TYPES:
|
|
294
|
+
# warnings.warn(f"Event type {event_type} is not present in {self.__class__.__name__}, "
|
|
295
|
+
# "so the Task cannot be bound as expected.")
|
|
296
|
+
# return False
|
|
297
|
+
self.EVENT_TYPES.append(event_type)
|
|
298
|
+
if event_type not in self.Tasks:
|
|
299
|
+
self.Tasks[event_type] = []
|
|
300
|
+
Task_id = f"{self.id}.{event_type}.{len(self.Tasks[event_type])}"
|
|
301
|
+
# e.g. CButton114.focus_gain.514 / CEventHandling114.focus_gain.514
|
|
302
|
+
match parsed_event_type["type"]:
|
|
303
|
+
case "delay":
|
|
304
|
+
Task = CDelayTask(
|
|
305
|
+
Task_id,
|
|
306
|
+
target, # I will fix this type error later (ignore is ur type check is off)
|
|
307
|
+
parsed_event_type["params"][0],
|
|
308
|
+
multithread,
|
|
309
|
+
_keep_at_clear,
|
|
310
|
+
)
|
|
311
|
+
case "repeat":
|
|
312
|
+
raise NotImplementedError("repeat events is not implemented yet!")
|
|
313
|
+
case _: # All normal event types
|
|
314
|
+
Task = CBoundTask(Task_id, target, multithread, _keep_at_clear)
|
|
315
|
+
self.Tasks[event_type].append(Task)
|
|
316
|
+
return Task
|
|
317
|
+
|
|
318
|
+
def clear_bind(self, event_type: str) -> bool:
|
|
319
|
+
"""To clear clear Tasks binded to a spcific event or widget
|
|
320
|
+
|
|
321
|
+
Example
|
|
322
|
+
-------
|
|
323
|
+
.. code-block:: python
|
|
324
|
+
my_widget = CWidget(...)
|
|
325
|
+
my_widget.clear_bind("click")
|
|
326
|
+
This shows clearing Tasks bound to `click` event on `my_widget`.
|
|
327
|
+
|
|
328
|
+
.. code-block:: python
|
|
329
|
+
my_widget = CWidget(...)
|
|
330
|
+
my_widget.clear_bind("*")
|
|
331
|
+
This shows clearing all events bound to any event on `my_widget`.
|
|
332
|
+
|
|
333
|
+
:param event_type: Type of event to clear binds, `*` for all
|
|
334
|
+
:return: Boolean, whether success or not
|
|
335
|
+
"""
|
|
336
|
+
if event_type == "*": # Clear all Tasks of this object
|
|
337
|
+
return not False in [self.clear_bind(this_type) for this_type in self.Tasks]
|
|
338
|
+
else: # In other cases, this must be an specific event type
|
|
339
|
+
if event_type in self.Tasks: # If type given existed and include some Tasks
|
|
340
|
+
for Task in self.Tasks[event_type]:
|
|
341
|
+
if not Task.keep_at_clear: # Cip any keep_at_clear Tasks
|
|
342
|
+
self.unbind(Task)
|
|
343
|
+
return True
|
|
344
|
+
else:
|
|
345
|
+
return False
|
|
346
|
+
|
|
347
|
+
def _check_delay_events(self, _=None) -> None:
|
|
348
|
+
"""To check and execute delay events.
|
|
349
|
+
|
|
350
|
+
Example
|
|
351
|
+
-------
|
|
352
|
+
Mostly used by CWidget.update(), which is internal use.
|
|
353
|
+
|
|
354
|
+
:param _: To accept an event object if is given, will be ignored
|
|
355
|
+
"""
|
|
356
|
+
# print("Checking delayed events...")
|
|
357
|
+
for binded_event_type in tuple(self.Tasks):
|
|
358
|
+
if self.parse_event_type_str(binded_event_type)["type"] == "delay":
|
|
359
|
+
for Task in self.Tasks[binded_event_type]:
|
|
360
|
+
if isinstance(Task, CDelayTask):
|
|
361
|
+
if float(time.time()) >= Task.target_time:
|
|
362
|
+
# print(f"Current time is later than target time of {Task.id}, "
|
|
363
|
+
# "execute the Task.")
|
|
364
|
+
self.execute_Task(Task)
|
|
365
|
+
|
|
366
|
+
|
|
367
|
+
# Initialize working thread
|
|
368
|
+
CEventHandling.WORKING_THREAD = threading.Thread(target=CEventHandling._working_thread_loop)
|
|
369
|
+
|
|
370
|
+
|
|
371
|
+
# @daTasklass
|
|
372
|
+
class CEvent:
|
|
373
|
+
"""Used to represent an event."""
|
|
374
|
+
|
|
375
|
+
latest: CEvent
|
|
376
|
+
|
|
377
|
+
def __init__(
|
|
378
|
+
self,
|
|
379
|
+
widget: CEventHandling | None = None,
|
|
380
|
+
event_type: str = "[Unspecified]",
|
|
381
|
+
**kwargs,
|
|
382
|
+
):
|
|
383
|
+
"""This class is used to represent events.
|
|
384
|
+
|
|
385
|
+
Some properties owned by all types of events are stored as attributes, such as widget and type.
|
|
386
|
+
Others are stored as items, which can be accessed or manipulated just like dict, e.g.
|
|
387
|
+
`CEvent["x"]` for get and `CEvent["y"] = 16` for set.
|
|
388
|
+
|
|
389
|
+
Example
|
|
390
|
+
-------
|
|
391
|
+
Included in description.
|
|
392
|
+
|
|
393
|
+
:param widget: The widget of the event, None by default
|
|
394
|
+
:param event_type: Type of the event, in string, `"[Unspecified]"` by default
|
|
395
|
+
:param **kwargs: Other properties of the event, will be added as items
|
|
396
|
+
"""
|
|
397
|
+
self.event_type: str = event_type # Type of event
|
|
398
|
+
self.widget: typing.Optional[typing.Any] = widget # Relating widget
|
|
399
|
+
self.window_base: typing.Optional[typing.Any] = None # WindowBase of the current window
|
|
400
|
+
self.window: typing.Optional[typing.Any] = None # Current window
|
|
401
|
+
self.event_data: dict = {}
|
|
402
|
+
# Not all properties above will be used
|
|
403
|
+
# Update stuff from args into attributes
|
|
404
|
+
for prop in kwargs.keys():
|
|
405
|
+
if prop not in ["widget", "event_type"]:
|
|
406
|
+
self[prop] = kwargs[prop]
|
|
407
|
+
|
|
408
|
+
def __setitem__(self, key: str, value: typing.Any):
|
|
409
|
+
self.event_data[key] = value
|
|
410
|
+
|
|
411
|
+
def __getitem__(self, key: str) -> typing.Any:
|
|
412
|
+
if key in self.event_data:
|
|
413
|
+
return self.event_data[key]
|
|
414
|
+
else:
|
|
415
|
+
return None # If no such item avail, returns None
|
|
416
|
+
|
|
417
|
+
|
|
418
|
+
CEvent.latest = CEvent(widget=None, event_type="NO_EVENT")
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
"""Charmy constants."""
|
|
2
|
+
|
|
3
|
+
from enum import Enum
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
class ID(Enum):
|
|
7
|
+
"""ID is an enum to store object ID.
|
|
8
|
+
|
|
9
|
+
AUTO: Auto generate ID.
|
|
10
|
+
NONE: No ID.
|
|
11
|
+
"""
|
|
12
|
+
|
|
13
|
+
AUTO = 0
|
|
14
|
+
NONE = 1
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
class Backends(Enum):
|
|
18
|
+
"""UIFrame is an enum to store UI frame.
|
|
19
|
+
|
|
20
|
+
GLFW, SDL, OPENGL, SKIA are consts representing those backends
|
|
21
|
+
"""
|
|
22
|
+
|
|
23
|
+
GLFW = glfw = "GLFW"
|
|
24
|
+
SDL = sdl = "SDL"
|
|
25
|
+
OPENGL = opengl = "OPENGL"
|
|
26
|
+
SKIA = skia = "SKIA"
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+
class DrawingMode(Enum):
|
|
30
|
+
"""DrawingMode is an enum to store drawing mode.
|
|
31
|
+
IMMEDIATE(immediate): IMMEDIATE is an enum to store drawing mode.
|
|
32
|
+
RETAINED(retained): RETAINED is an enum to store drawing mode.
|
|
33
|
+
"""
|
|
34
|
+
|
|
35
|
+
IMMEDIATE = immediate = "IMMEDIATE"
|
|
36
|
+
RETAINED = retained = "READIED"
|
|
37
|
+
|
|
38
|
+
|
|
39
|
+
MANAGER_ID = "main_manager"
|