kivydk 0.0.3.dev2__py3-none-any.whl
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.
- kivydk/__init__.py +48 -0
- kivydk/_version.py +31 -0
- kivydk/core/__init__.py +19 -0
- kivydk/core/mapping/__init__.py +19 -0
- kivydk/core/mapping/cursors.py +121 -0
- kivydk/core/mapping/icons.py +7328 -0
- kivydk/core/resources/__init__.py +14 -0
- kivydk/core/resources/fonts.py +99 -0
- kivydk/data/fonts/JetBrainsMono/JetBrainsMono-Bold.otf +0 -0
- kivydk/data/fonts/JetBrainsMono/JetBrainsMono-Bold.ttf +0 -0
- kivydk/data/fonts/JetBrainsMono/JetBrainsMono-BoldItalic.otf +0 -0
- kivydk/data/fonts/JetBrainsMono/JetBrainsMono-BoldItalic.ttf +0 -0
- kivydk/data/fonts/JetBrainsMono/JetBrainsMono-Italic.otf +0 -0
- kivydk/data/fonts/JetBrainsMono/JetBrainsMono-Italic.ttf +0 -0
- kivydk/data/fonts/JetBrainsMono/JetBrainsMono-Regular.otf +0 -0
- kivydk/data/fonts/JetBrainsMono/JetBrainsMono-Regular.ttf +0 -0
- kivydk/data/fonts/JetBrainsMono/LICENSE.txt +93 -0
- kivydk/data/fonts/MaterialIcons/LICENSE.txt +202 -0
- kivydk/data/fonts/MaterialIcons/MaterialIcons-Filled.ttf +0 -0
- kivydk/data/fonts/MaterialIcons/MaterialIcons-Outlined.otf +0 -0
- kivydk/data/fonts/MaterialIcons/MaterialIcons-Round.otf +0 -0
- kivydk/data/fonts/MaterialIcons/MaterialIcons-Sharp.otf +0 -0
- kivydk/data/fonts/MaterialIcons/MaterialIcons-TwoTone.otf +0 -0
- kivydk/data/fonts/materialdesignicons-webfont.ttf +0 -0
- kivydk/logger.py +76 -0
- kivydk/tools/monkey/event.py +37 -0
- kivydk/tools/monkey/patch.py +12 -0
- kivydk/uix/__init__.py +24 -0
- kivydk/uix/behavior/__init__.py +40 -0
- kivydk/uix/behavior/click.py +87 -0
- kivydk/uix/behavior/hover.py +89 -0
- kivydk/uix/manager/__init__.py +17 -0
- kivydk/uix/manager/hover.py +79 -0
- kivydk/uix/widgets/__init__.py +29 -0
- kivydk/uix/widgets/line_number.py +346 -0
- kivydk-0.0.3.dev2.dist-info/METADATA +70 -0
- kivydk-0.0.3.dev2.dist-info/RECORD +39 -0
- kivydk-0.0.3.dev2.dist-info/WHEEL +5 -0
- kivydk-0.0.3.dev2.dist-info/top_level.txt +1 -0
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
#//|>-----------------------------------------------------------------------------------------------------------------<|
|
|
2
|
+
#//| Copyright (c) 09 Jan 2026. All rights are reserved by ASI
|
|
3
|
+
#//|>-----------------------------------------------------------------------------------------------------------------<|
|
|
4
|
+
"""
|
|
5
|
+
This behavior encapsulates mouse click detection for widgets. It listens for press and release events,
|
|
6
|
+
tracks the click state and triggers callbacks when a valid click occurs. This behavior removes the need for manual
|
|
7
|
+
input handling and provides a consistent, reusable pattern for widgets that respond to user clicks.
|
|
8
|
+
|
|
9
|
+
.. tip::
|
|
10
|
+
Click detection is based on touch events, so finger presses on touch‑enabled monitors are treated as clicks.
|
|
11
|
+
|
|
12
|
+
§ section : example ¶
|
|
13
|
+
|
|
14
|
+
Sample demonstrating how ``ClickBehavior`` can be used to detect and handle click events.
|
|
15
|
+
|
|
16
|
+
.. image:: § example image : uix, behavior, TestClick.gif ¶
|
|
17
|
+
:align: center
|
|
18
|
+
|
|
19
|
+
.. literalinclude:: § example code : uix, behavior, click.py ¶
|
|
20
|
+
:language: python
|
|
21
|
+
:linenos:
|
|
22
|
+
"""
|
|
23
|
+
__all__ = ("ClickBehavior",)
|
|
24
|
+
|
|
25
|
+
#// IMPORT
|
|
26
|
+
from kivydk.__ref.window import Window
|
|
27
|
+
|
|
28
|
+
from kivy.clock import Clock
|
|
29
|
+
from kivy.properties import NumericProperty
|
|
30
|
+
from kivy.uix.behaviors.button import ButtonBehavior
|
|
31
|
+
|
|
32
|
+
|
|
33
|
+
#// LOGIC
|
|
34
|
+
class ClickBehavior(ButtonBehavior):
|
|
35
|
+
"""
|
|
36
|
+
A mixin that adds lightweight click and double click detection to any widget.
|
|
37
|
+
|
|
38
|
+
This extends :class:`~kivy.uix.behaviors.button.ButtonBehavior`, so the standard ``on_press`` and ``on_release``
|
|
39
|
+
events remain available.
|
|
40
|
+
|
|
41
|
+
|
|
42
|
+
:Events:
|
|
43
|
+
`on_click`
|
|
44
|
+
Fired when the widget is clicked once.
|
|
45
|
+
`on_double_click`
|
|
46
|
+
Fired when the widget is double-clicked within :attr:`click_interval`.
|
|
47
|
+
"""
|
|
48
|
+
|
|
49
|
+
click_interval: NumericProperty = NumericProperty(0.25)
|
|
50
|
+
"""Maximum time (seconds) in which a second click is considered part of a double‑click.
|
|
51
|
+
|
|
52
|
+
The sweet spot is between ``0.2`` and ``0.3``.
|
|
53
|
+
|
|
54
|
+
:attr:`click_interval` is a :class:`~kivy.properties.NumericProperty` and defaults to `0.25`."""
|
|
55
|
+
|
|
56
|
+
__events__ = ["on_click", "on_double_click"]
|
|
57
|
+
|
|
58
|
+
def __init__(self, **kwargs) -> None:
|
|
59
|
+
super().__init__(**kwargs)
|
|
60
|
+
|
|
61
|
+
# Local variables
|
|
62
|
+
self.__count_click: int = 0
|
|
63
|
+
self.__clock: Clock = Clock.create_trigger(self._dispatch_double_click, self.click_interval)
|
|
64
|
+
|
|
65
|
+
self.fbind("on_release", self._dispatch_click)
|
|
66
|
+
|
|
67
|
+
def on_click(self) -> None:
|
|
68
|
+
"""Event called when the widget is pressed and released."""
|
|
69
|
+
pass
|
|
70
|
+
|
|
71
|
+
def on_double_click(self) -> None:
|
|
72
|
+
"""Event called when the widget is pressed and released two times."""
|
|
73
|
+
pass
|
|
74
|
+
|
|
75
|
+
def _dispatch_click(self, *args) -> None:
|
|
76
|
+
if self.collide_point(*Window.mouse_pos):
|
|
77
|
+
self.__count_click += 1
|
|
78
|
+
self.__clock()
|
|
79
|
+
|
|
80
|
+
if self.__count_click < 2:
|
|
81
|
+
self.dispatch("on_click")
|
|
82
|
+
|
|
83
|
+
def _dispatch_double_click(self, *args) -> None:
|
|
84
|
+
if self.__count_click > 1:
|
|
85
|
+
self.dispatch("on_double_click")
|
|
86
|
+
|
|
87
|
+
self.__count_click = 0
|
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
#//|>-----------------------------------------------------------------------------------------------------------------<|
|
|
2
|
+
#//| Copyright (c) 07 Jan 2026. All rights are reserved by ASI
|
|
3
|
+
#//|>-----------------------------------------------------------------------------------------------------------------<|
|
|
4
|
+
"""
|
|
5
|
+
This behavior adds simple hover awareness to widgets, allowing them to react when the mouse cursor moves over
|
|
6
|
+
or away from them. It provides an easy way to create interactive, desktop‑friendly UI elements that respond visually
|
|
7
|
+
or functionally to hover interactions.
|
|
8
|
+
|
|
9
|
+
§ section : example ¶
|
|
10
|
+
|
|
11
|
+
Sample illustrating how ``HoverBehavior`` can be used to create widgets that react to mouse hover.
|
|
12
|
+
|
|
13
|
+
.. image:: § example image : uix, behavior, TestHover.gif ¶
|
|
14
|
+
:align: center
|
|
15
|
+
|
|
16
|
+
.. literalinclude:: § example code : uix, behavior, hover.py ¶
|
|
17
|
+
:language: python
|
|
18
|
+
:linenos:
|
|
19
|
+
"""
|
|
20
|
+
__all__ = ("HoverBehavior",)
|
|
21
|
+
|
|
22
|
+
#// IMPORT
|
|
23
|
+
from kivy.properties import AliasProperty, BooleanProperty
|
|
24
|
+
|
|
25
|
+
from kivydk.uix.manager.hover import HoverManager
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
#// LOGIC
|
|
29
|
+
class HoverBehavior(object):
|
|
30
|
+
"""
|
|
31
|
+
A mixin that adds lightweight hover detection to any widget.
|
|
32
|
+
|
|
33
|
+
:Events:
|
|
34
|
+
`on_hover`
|
|
35
|
+
Fired when the cursor enters and leaves the widget's bounds.
|
|
36
|
+
`on_hover_start`
|
|
37
|
+
Fired when the cursor enters the widget's bounds.
|
|
38
|
+
`on_hover_end`
|
|
39
|
+
Fired when the cursor leaves the widget's bounds.
|
|
40
|
+
"""
|
|
41
|
+
|
|
42
|
+
_hover_state: BooleanProperty = BooleanProperty(False)
|
|
43
|
+
|
|
44
|
+
def _get_hovered(self) -> bool:
|
|
45
|
+
return self._hover_state
|
|
46
|
+
|
|
47
|
+
hovered: AliasProperty = AliasProperty(_get_hovered, None, bind=("_hover_state",), cache=True)
|
|
48
|
+
"""Whether the mouse cursor is currently hovering over the widget.
|
|
49
|
+
|
|
50
|
+
:attr:`hovered` is a read-only :class:`~kivy.properties.AliasProperty`
|
|
51
|
+
that returns a :class:`bool` and defaults to `False`."""
|
|
52
|
+
|
|
53
|
+
__events__ = ["on_hover", "on_hover_start", "on_hover_end"]
|
|
54
|
+
|
|
55
|
+
def __init__(self, **kwargs) -> None:
|
|
56
|
+
super().__init__(**kwargs)
|
|
57
|
+
|
|
58
|
+
self.fbind("parent", self._auto_register)
|
|
59
|
+
self.fbind("_hover_state", self._dispatch_hover)
|
|
60
|
+
|
|
61
|
+
def on_hover(self, state: bool) -> None:
|
|
62
|
+
"""
|
|
63
|
+
Event called when the cursor enters and leaves the widget's bounds.
|
|
64
|
+
|
|
65
|
+
This event is dispatched every time the hover state changes, allowing widgets to react
|
|
66
|
+
to both hover‑start and hover‑end transitions.
|
|
67
|
+
|
|
68
|
+
:type state: bool
|
|
69
|
+
:param state: ``True`` when the cursor is currently hovering over the widget and ``False`` otherwise.
|
|
70
|
+
"""
|
|
71
|
+
pass
|
|
72
|
+
|
|
73
|
+
def on_hover_start(self) -> None:
|
|
74
|
+
"""Event called when the cursor enters the widget's bounds."""
|
|
75
|
+
pass
|
|
76
|
+
|
|
77
|
+
def on_hover_end(self) -> None:
|
|
78
|
+
"""Event called when the cursor leaves the widget's bounds."""
|
|
79
|
+
pass
|
|
80
|
+
|
|
81
|
+
def _auto_register(self, instance, parent) -> None:
|
|
82
|
+
if parent is not None:
|
|
83
|
+
HoverManager.register(self)
|
|
84
|
+
else:
|
|
85
|
+
HoverManager.unregister(self)
|
|
86
|
+
|
|
87
|
+
def _dispatch_hover(self, *args) -> None:
|
|
88
|
+
self.dispatch("on_hover", self._hover_state)
|
|
89
|
+
self.dispatch("on_hover_start" if self._hover_state else "on_hover_end")
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
#//|>-----------------------------------------------------------------------------------------------------------------<|
|
|
2
|
+
#//| Copyright (c) 12 Jan 2026. All rights are reserved by ASI
|
|
3
|
+
#//|>-----------------------------------------------------------------------------------------------------------------<|
|
|
4
|
+
"""
|
|
5
|
+
This module contains internal management systems used by KivyDK to coordinate and orchestrate widget‑level behavior.
|
|
6
|
+
These managers operate behind the scenes to provide higher‑level functionality such as hover tracking and other
|
|
7
|
+
UI interaction services that may be added in the future. They form part of the UIX infrastructure, ensuring that
|
|
8
|
+
complex interactions work consistently across the framework without requiring manual setup from the user.
|
|
9
|
+
|
|
10
|
+
.. attention::
|
|
11
|
+
These modules are not intended for direct use. All managers are created and controlled internally by KivyDK
|
|
12
|
+
and developers typically never need to interact with them directly.
|
|
13
|
+
"""
|
|
14
|
+
__all__ = ("HoverManager",)
|
|
15
|
+
|
|
16
|
+
#// IMPORT
|
|
17
|
+
from kivydk.uix.manager.hover import HoverManager
|
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
#//|>-----------------------------------------------------------------------------------------------------------------<|
|
|
2
|
+
#//| Copyright (c) 09 Jan 2026. All rights are reserved by ASI
|
|
3
|
+
#//|>-----------------------------------------------------------------------------------------------------------------<|
|
|
4
|
+
"""
|
|
5
|
+
This manager coordinates hover detection by keeping a registry of widgets that support hover interactions
|
|
6
|
+
and performing optimized checks to determine which one is currently under the cursor. This centralized approach
|
|
7
|
+
eliminates redundant per‑widget computations and ensures that only the relevant widgets are evaluated until a hover
|
|
8
|
+
target is identified.
|
|
9
|
+
"""
|
|
10
|
+
__all__ = ("HoverManager", "HoverManagerBase")
|
|
11
|
+
|
|
12
|
+
#// IMPORT
|
|
13
|
+
from kivydk.__ref.window import Window
|
|
14
|
+
|
|
15
|
+
from kivy.event import EventDispatcher
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
#// LOGIC
|
|
19
|
+
class HoverBehavior:
|
|
20
|
+
"""Lightweight forward declaration used to break a circular import.
|
|
21
|
+
|
|
22
|
+
Stub used for type checking. Attributes reflect their runtime value types."""
|
|
23
|
+
_hover_state: bool # actually a BooleanProperty
|
|
24
|
+
def collide_point(self, x:int|float, y:int|float) -> bool: ...
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
class HoverManagerBase(EventDispatcher):
|
|
28
|
+
"""
|
|
29
|
+
Centralized manager responsible for efficient hover detection across all widgets that inherit from
|
|
30
|
+
:class:`~uix.behavior.hover.HoverBehavior`.
|
|
31
|
+
|
|
32
|
+
.. todo::
|
|
33
|
+
- Sort system need to be implemented. Usually the last registered widgets have high chance to be hovered ones.
|
|
34
|
+
- Some system to group widgets in chunks and to check them first.
|
|
35
|
+
- Tree system will be faster if the UIX become more complex.
|
|
36
|
+
"""
|
|
37
|
+
def __init__(self, **kwargs) -> None:
|
|
38
|
+
super().__init__(**kwargs)
|
|
39
|
+
|
|
40
|
+
# Local variables
|
|
41
|
+
self.__widgets: list[HoverBehavior] = []
|
|
42
|
+
self.__last_hovered: HoverBehavior|None = None
|
|
43
|
+
|
|
44
|
+
if Window is not None:
|
|
45
|
+
Window.fbind("mouse_pos", self._do_mouse_pos)
|
|
46
|
+
Window.fbind("on_cursor_leave", self._hover_end_last)
|
|
47
|
+
|
|
48
|
+
def register(self, widget:HoverBehavior) -> None:
|
|
49
|
+
"""Register a specific widget for hovering detection system."""
|
|
50
|
+
self.__widgets.append(widget)
|
|
51
|
+
|
|
52
|
+
def unregister(self, widget:HoverBehavior) -> None:
|
|
53
|
+
"""Unregister a specific widget from hovering detection system."""
|
|
54
|
+
if widget in self.__widgets:
|
|
55
|
+
self.__widgets.remove(widget)
|
|
56
|
+
|
|
57
|
+
def _hover_end_last(self, *args) -> None:
|
|
58
|
+
if self.__last_hovered:
|
|
59
|
+
self.__last_hovered._hover_state = False
|
|
60
|
+
self.__last_hovered = None
|
|
61
|
+
|
|
62
|
+
def _do_mouse_pos(self, instance:Window, mouse_pos:tuple[float, float]) -> None:
|
|
63
|
+
tmp_hover: HoverBehavior|None = None
|
|
64
|
+
|
|
65
|
+
for widget in self.__widgets:
|
|
66
|
+
if widget.collide_point(*mouse_pos):
|
|
67
|
+
tmp_hover = widget
|
|
68
|
+
break
|
|
69
|
+
|
|
70
|
+
if tmp_hover is not self.__last_hovered:
|
|
71
|
+
self._hover_end_last()
|
|
72
|
+
|
|
73
|
+
if tmp_hover is not None:
|
|
74
|
+
tmp_hover._hover_state = True
|
|
75
|
+
self.__last_hovered = tmp_hover
|
|
76
|
+
|
|
77
|
+
|
|
78
|
+
#: Global manager responsible for efficient hover detection.
|
|
79
|
+
HoverManager:HoverManagerBase = HoverManagerBase()
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
#//|>-----------------------------------------------------------------------------------------------------------------<|
|
|
2
|
+
#//| Copyright (c) 12 Feb 2026. All rights are reserved by ASI
|
|
3
|
+
#//|>-----------------------------------------------------------------------------------------------------------------<|
|
|
4
|
+
"""
|
|
5
|
+
This module contains the reusable UI components provided by KivyDK. Each widget is self‑contained and focuses on
|
|
6
|
+
a single responsibility providing, offering small, interactive elements used throughout the framework.
|
|
7
|
+
|
|
8
|
+
All public widgets are implemented entirely in Python (no KV language) to provide a clean, consistent and fully
|
|
9
|
+
code‑driven development experience. This approach keeps the API intuitive for developers who prefer Python‑only
|
|
10
|
+
workflows and avoids the structural complexity often introduced by KV‑based widget bundles.
|
|
11
|
+
Developers who wish to style these widgets using KV language are still free to do so.
|
|
12
|
+
|
|
13
|
+
.. important::
|
|
14
|
+
All public widgets are also re‑exported through :mod:`kivydk.uix` to offer a flexible import style for developers:
|
|
15
|
+
|
|
16
|
+
.. code-block:: python
|
|
17
|
+
:class: no-copybutton
|
|
18
|
+
|
|
19
|
+
from kivydk.uix import LineNumber # style: kivy
|
|
20
|
+
from kivydk.uix.widgets import LineNumber # style: general
|
|
21
|
+
from kivydk.uix.widgets.line_number import LineNumber # style: explicit
|
|
22
|
+
|
|
23
|
+
This structure keeps the internal organization clean while allowing users to choose the import pattern
|
|
24
|
+
that best fits their workflow.
|
|
25
|
+
"""
|
|
26
|
+
__all__ = ("LineNumber",)
|
|
27
|
+
|
|
28
|
+
#// IMPORT
|
|
29
|
+
from .line_number import LineNumber
|
|
@@ -0,0 +1,346 @@
|
|
|
1
|
+
#//|>-----------------------------------------------------------------------------------------------------------------<|
|
|
2
|
+
#//| Copyright (c) 12 Feb 2026. All rights are reserved by ASI
|
|
3
|
+
#//|>-----------------------------------------------------------------------------------------------------------------<|
|
|
4
|
+
"""
|
|
5
|
+
.. TODO::
|
|
6
|
+
• Description of this module need to be implemented.
|
|
7
|
+
• Fixing the rendering glitch when trying to customize the appearance.
|
|
8
|
+
"""
|
|
9
|
+
__all__ = ("LineNumber",)
|
|
10
|
+
|
|
11
|
+
#// IMPORT
|
|
12
|
+
from os import cpu_count
|
|
13
|
+
from concurrent.futures import ThreadPoolExecutor as Threads
|
|
14
|
+
|
|
15
|
+
from kivy.core.text import DEFAULT_FONT, Label
|
|
16
|
+
from kivy.graphics import Color, Rectangle, BorderImage
|
|
17
|
+
from kivy.graphics.texture import Texture
|
|
18
|
+
from kivy.uix.widget import Widget
|
|
19
|
+
from kivy.uix.scrollview import ScrollView
|
|
20
|
+
from kivy.uix.textinput import TextInput
|
|
21
|
+
|
|
22
|
+
from kivy.properties import StringProperty, NumericProperty, ColorProperty, OptionProperty
|
|
23
|
+
from kivy.properties import ListProperty, VariableListProperty
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
#// LOGIC
|
|
27
|
+
class LineNumber(Widget):
|
|
28
|
+
align: OptionProperty = OptionProperty("right", options=["left", "center", "right"])
|
|
29
|
+
'''Horizontal alignment of the text.
|
|
30
|
+
|
|
31
|
+
+-------------+-------+--------+-------+
|
|
32
|
+
+=============+=======+========+=======+
|
|
33
|
+
| **Default** | right |
|
|
34
|
+
+-------------+-------+--------+-------+
|
|
35
|
+
| **Options** | left | center | right |
|
|
36
|
+
+-------------+-------+--------+-------+
|
|
37
|
+
'''
|
|
38
|
+
|
|
39
|
+
background_color: ColorProperty = ColorProperty()
|
|
40
|
+
'''Current tint color of the background.
|
|
41
|
+
|
|
42
|
+
+-------------+--------------+-------+
|
|
43
|
+
+=============+==============+=======+
|
|
44
|
+
| **Default** | [1, 1, 1, 1] | white |
|
|
45
|
+
+-------------+--------------+-------+
|
|
46
|
+
'''
|
|
47
|
+
|
|
48
|
+
background_texture = StringProperty('atlas://data/images/defaulttheme/textinput')
|
|
49
|
+
'''Background image of the line numbers.
|
|
50
|
+
|
|
51
|
+
:attr:`background_texture` is a :class:`~kivy.properties.StringProperty` and defaults to
|
|
52
|
+
'atlas://data/images/defaulttheme/textinput'.
|
|
53
|
+
'''
|
|
54
|
+
|
|
55
|
+
border = ListProperty([4, 4, 4, 4])
|
|
56
|
+
'''Border used for :class:`~kivy.graphics.vertex_instructions.BorderImage` graphics instruction.
|
|
57
|
+
Used with :attr:`background_texture`. Can be used for a custom background.
|
|
58
|
+
|
|
59
|
+
It must be a list of four values: (bottom, right, top, left). Read the BorderImage instruction for more
|
|
60
|
+
information about how to use it.
|
|
61
|
+
|
|
62
|
+
:attr:`border` is a :class:`~kivy.properties.ListProperty` and defaults to [4, 4, 4, 4].
|
|
63
|
+
'''
|
|
64
|
+
|
|
65
|
+
font_context = StringProperty(None, allownone=True)
|
|
66
|
+
'''Font context. `None` means the font is used in isolation, so you are guaranteed to be drawing with the TTF file
|
|
67
|
+
resolved by :attr:`font_name`. Specifying a value here will load the font file into a named context, enabling
|
|
68
|
+
fallback between all fonts in the same context. If a font context is set, you are not guaranteed that rendering
|
|
69
|
+
will actually use the specified TTF file for all glyphs (Pango will pick the one it thinks is best).
|
|
70
|
+
|
|
71
|
+
If Kivy is linked against a system-wide installation of FontConfig, you can load the system fonts by specifying a
|
|
72
|
+
font context starting with the special string `system://`. This will load the system fontconfig configuration and
|
|
73
|
+
add your application-specific fonts on top of it (this imposes a significant risk of family name collision,
|
|
74
|
+
Pango may not use your custom font file, but pick one from the system)
|
|
75
|
+
|
|
76
|
+
.. note::
|
|
77
|
+
This feature requires the Pango text provider.
|
|
78
|
+
|
|
79
|
+
:attr:`font_context` is a :class:`~kivy.properties.StringProperty` and defaults to None.
|
|
80
|
+
'''
|
|
81
|
+
|
|
82
|
+
font_family = StringProperty(None, allownone=True)
|
|
83
|
+
'''Font family, this is only applicable when using :attr:`font_context` option. The specified font family will be
|
|
84
|
+
requested, but note that it may not be available, or there could be multiple fonts registered with the same family.
|
|
85
|
+
The value can be a family name (string) available in the font context (for example a system font in a `system://`
|
|
86
|
+
context, or a custom font file added using :class:`kivy.core.text.FontContextManager`).
|
|
87
|
+
If set to `None`, font selection is controlled by the :attr:`font_name` setting.
|
|
88
|
+
|
|
89
|
+
.. note::
|
|
90
|
+
If using :attr:`font_name` to reference a custom font file, you should leave this as `None`.
|
|
91
|
+
The family name is managed automatically in this case.
|
|
92
|
+
|
|
93
|
+
.. note::
|
|
94
|
+
This feature requires the Pango text provider.
|
|
95
|
+
|
|
96
|
+
:attr:`font_family` is a :class:`~kivy.properties.StringProperty` and defaults to None.
|
|
97
|
+
'''
|
|
98
|
+
|
|
99
|
+
font_name = StringProperty(DEFAULT_FONT)
|
|
100
|
+
'''Filename of the font to use. The path can be absolute or relative.
|
|
101
|
+
Relative paths are resolved by the :func:`~kivy.resources.resource_find` function.
|
|
102
|
+
|
|
103
|
+
:attr:`font_name` is a :class:`~kivy.properties.StringProperty` and defaults to 'Roboto'.
|
|
104
|
+
This value is taken from :class:`~kivy.config.Config`.
|
|
105
|
+
'''
|
|
106
|
+
|
|
107
|
+
font_size = NumericProperty('15sp')
|
|
108
|
+
'''Font size of the text in pixels.
|
|
109
|
+
|
|
110
|
+
:attr:`font_size` is a :class:`~kivy.properties.NumericProperty` and defaults to 15 :attr:`~kivy.metrics.sp`.
|
|
111
|
+
'''
|
|
112
|
+
|
|
113
|
+
foreground_color = ColorProperty([0, 0, 0, 1])
|
|
114
|
+
'''Current color of the foreground, in (r, g, b, a) format.
|
|
115
|
+
|
|
116
|
+
:attr:`foreground_color` is a :class:`~kivy.properties.ColorProperty` and defaults to [0, 0, 0, 1] (black).
|
|
117
|
+
'''
|
|
118
|
+
|
|
119
|
+
padding = VariableListProperty([4], length=2)
|
|
120
|
+
'''Horizontal padding of the text: [padding_left, padding_right].
|
|
121
|
+
|
|
122
|
+
padding also accepts a one argument form [padding_horizontal].
|
|
123
|
+
|
|
124
|
+
:attr:`padding` is a :class:`~kivy.properties.VariableListProperty` and defaults to [4, 4].
|
|
125
|
+
'''
|
|
126
|
+
|
|
127
|
+
width_min = NumericProperty('18sp')
|
|
128
|
+
'''Minimum desired width of the text in pixels.
|
|
129
|
+
|
|
130
|
+
:attr:`width_min` is a :class:`~kivy.properties.NumericProperty` and defaults to 18 :attr:`~kivy.metrics.sp`.
|
|
131
|
+
'''
|
|
132
|
+
|
|
133
|
+
def __init__(self, text_input, **kwargs) -> None:
|
|
134
|
+
super().__init__(**kwargs)
|
|
135
|
+
self.size_hint_x = None
|
|
136
|
+
|
|
137
|
+
# Private variables
|
|
138
|
+
self.__text_input: TextInput = text_input
|
|
139
|
+
self.__desired_number_width: int = 0
|
|
140
|
+
|
|
141
|
+
self.__bind()
|
|
142
|
+
self._update_font()
|
|
143
|
+
|
|
144
|
+
def __bind(self) -> None:
|
|
145
|
+
self.bind(align=self._update_line_numbers,
|
|
146
|
+
background_color=self._update_line_numbers,
|
|
147
|
+
background_texture=self._update_line_numbers,
|
|
148
|
+
border=self._update_line_numbers,
|
|
149
|
+
font_context=self._update_font,
|
|
150
|
+
font_family=self._update_font,
|
|
151
|
+
font_name=self._update_font,
|
|
152
|
+
font_size=self._update_font,
|
|
153
|
+
foreground_color=self._update_line_numbers,
|
|
154
|
+
padding=self._update_line_numbers)
|
|
155
|
+
|
|
156
|
+
self.__text_input.bind(parent=self._sync_scroll,
|
|
157
|
+
size=self._update_line_numbers,
|
|
158
|
+
text=self._update_line_numbers)
|
|
159
|
+
|
|
160
|
+
def _sync_scroll(self, instance: TextInput, parent) -> None:
|
|
161
|
+
"""Used to bind the scroll event for updating the line numbers."""
|
|
162
|
+
# Kivy’s unbind method is designed to quietly fail if the event handler doesn't exist
|
|
163
|
+
instance.unbind(scroll_y=self._update_line_numbers)
|
|
164
|
+
try:
|
|
165
|
+
instance.parent.unbind(scroll_y=self._update_line_numbers)
|
|
166
|
+
except KeyError:
|
|
167
|
+
pass
|
|
168
|
+
|
|
169
|
+
# Bind :attr:`scroll_y` base on the parent
|
|
170
|
+
if isinstance(parent, ScrollView):
|
|
171
|
+
instance.parent.bind(scroll_y=self._update_line_numbers)
|
|
172
|
+
else:
|
|
173
|
+
instance.bind(scroll_y=self._update_line_numbers)
|
|
174
|
+
|
|
175
|
+
def _update_font(self, *_) -> None:
|
|
176
|
+
"""Used to calculate the maximum width need it to draw one number."""
|
|
177
|
+
sizes: list[int] = []
|
|
178
|
+
|
|
179
|
+
for number in range(10):
|
|
180
|
+
label = Label(text=str(number), font_size=self.font_size, font_name=self.font_name,
|
|
181
|
+
font_family=self.font_family, font_context=self.font_context)
|
|
182
|
+
label.refresh()
|
|
183
|
+
sizes.append(label.texture.size[0])
|
|
184
|
+
|
|
185
|
+
self.__desired_number_width = max(*sizes)
|
|
186
|
+
self._update_line_numbers()
|
|
187
|
+
|
|
188
|
+
def __draw_line_number(self, number: int, y: float, y_min_render: float, y_max_render: float) -> None:
|
|
189
|
+
"""Used to draw one line number."""
|
|
190
|
+
# Make texture
|
|
191
|
+
label: Label = Label(text=str(number), color=self.foreground_color, font_size=self.font_size,
|
|
192
|
+
font_name=self.font_name, font_family=self.font_family, font_context=self.font_context)
|
|
193
|
+
label.refresh()
|
|
194
|
+
|
|
195
|
+
# Get necessary values for drawing
|
|
196
|
+
texture: Texture = label.texture
|
|
197
|
+
size: list[int] = [*texture.size]
|
|
198
|
+
uv: list[float] = [
|
|
199
|
+
0.0, 1.0, # bottom-left
|
|
200
|
+
1.0, 1.0, # bottom-right
|
|
201
|
+
1.0, 0.0, # top-right
|
|
202
|
+
0.0, 0.0 # top-left
|
|
203
|
+
]
|
|
204
|
+
|
|
205
|
+
# Update the values for partial visible ones
|
|
206
|
+
if y > y_max_render:
|
|
207
|
+
h: int = int(size[1] - (y - y_max_render))
|
|
208
|
+
uv[5] = uv[7] = 1.0 - h / size[1]
|
|
209
|
+
size[1] = h
|
|
210
|
+
|
|
211
|
+
if y < y_min_render:
|
|
212
|
+
h: int = int(y + size[1] - y_min_render)
|
|
213
|
+
uv[1] = uv[3] = h / size[1]
|
|
214
|
+
size[1] = h
|
|
215
|
+
y = y_min_render
|
|
216
|
+
|
|
217
|
+
# Calculate the horizontal position
|
|
218
|
+
if self.align == "left":
|
|
219
|
+
x = self.x + self.padding[0]
|
|
220
|
+
elif self.align == "right":
|
|
221
|
+
x = self.width - size[0] - self.padding[1]
|
|
222
|
+
else:
|
|
223
|
+
x = (self.width - size[0]) // 2
|
|
224
|
+
|
|
225
|
+
# Draw the line number
|
|
226
|
+
Rectangle(texture=texture, pos=(x, y), size=size, tex_coords=uv)
|
|
227
|
+
|
|
228
|
+
def __count_wrapped_lines(self, arg: list[int, int]) -> int:
|
|
229
|
+
"""Used to count the amount of wrapped lines with multithreading."""
|
|
230
|
+
wrapped: int = 0
|
|
231
|
+
|
|
232
|
+
try:
|
|
233
|
+
for index in range(arg[0], sum(arg)):
|
|
234
|
+
if self.__text_input._lines_flags[index] != 1:
|
|
235
|
+
wrapped += 1
|
|
236
|
+
|
|
237
|
+
except IndexError:
|
|
238
|
+
pass
|
|
239
|
+
|
|
240
|
+
return wrapped
|
|
241
|
+
|
|
242
|
+
def _update_line_numbers(self, *_) -> None:
|
|
243
|
+
"""Update the visible line numbers"""
|
|
244
|
+
self.canvas.clear()
|
|
245
|
+
|
|
246
|
+
# Draw the line number
|
|
247
|
+
with self.canvas:
|
|
248
|
+
Color(*self.background_color)
|
|
249
|
+
|
|
250
|
+
# Get necessary values
|
|
251
|
+
total_lines: int = len(self.__text_input._lines_flags)
|
|
252
|
+
padding_top: float = self.__text_input.padding[1]
|
|
253
|
+
padding_bottom: float = self.__text_input.padding[3]
|
|
254
|
+
line_height: int = self.__text_input.line_height
|
|
255
|
+
|
|
256
|
+
# Calculate the constraints
|
|
257
|
+
y: float = self.top - padding_top - line_height
|
|
258
|
+
y_min: float = self.y - line_height + 1
|
|
259
|
+
# y_max:float = self.top - 1
|
|
260
|
+
y_min_render: float = self.y
|
|
261
|
+
y_max_render: float = self.top - line_height
|
|
262
|
+
|
|
263
|
+
# Update the constraints base on the parent instance
|
|
264
|
+
if not isinstance(self.__text_input.parent, ScrollView):
|
|
265
|
+
y_min += padding_bottom
|
|
266
|
+
# y_max -= padding_top
|
|
267
|
+
y_min_render += padding_bottom
|
|
268
|
+
y_max_render -= padding_top
|
|
269
|
+
|
|
270
|
+
# Auto update necessary parameters
|
|
271
|
+
desired_width: int = self.padding[0] + self.padding[1] + self.__desired_number_width * len(str(total_lines))
|
|
272
|
+
self.width = max(self.width_min, desired_width)
|
|
273
|
+
|
|
274
|
+
# Calculate the scroll position, first visible lines and vertical position of the first line
|
|
275
|
+
if isinstance(self.__text_input.parent, ScrollView):
|
|
276
|
+
hidden_area: float = self.__text_input.height - self.height
|
|
277
|
+
scroll: float = (1.0 - self.__text_input.parent.scroll_y) * hidden_area
|
|
278
|
+
first_visible_line: int = max(0, int((scroll - padding_top) / line_height))
|
|
279
|
+
|
|
280
|
+
try:
|
|
281
|
+
y = self.__text_input._lines_rects[first_visible_line].pos[1] - hidden_area + scroll + self.y
|
|
282
|
+
except IndexError:
|
|
283
|
+
pass
|
|
284
|
+
|
|
285
|
+
else:
|
|
286
|
+
scroll: float = self.__text_input.scroll_y
|
|
287
|
+
first_visible_line: int = max(0, int(scroll / line_height))
|
|
288
|
+
y += scroll % line_height
|
|
289
|
+
|
|
290
|
+
# If the text is wrapped, calculate the first logical line number
|
|
291
|
+
if self.__text_input.do_wrap:
|
|
292
|
+
line_number: int = first_visible_line
|
|
293
|
+
cpus: int = max(2, cpu_count() - 7)
|
|
294
|
+
|
|
295
|
+
# Use multithreading if is worth it. Otherwise, will be much slower than main thread
|
|
296
|
+
if line_number > 100:
|
|
297
|
+
chunk_size: int = line_number // cpus
|
|
298
|
+
chunk_offset: int = line_number - chunk_size * cpus
|
|
299
|
+
|
|
300
|
+
with Threads() as threads:
|
|
301
|
+
chunks = [[cpu * chunk_size, chunk_size] for cpu in range(cpus)]
|
|
302
|
+
# Offset the first chunk by one, because the first line is not counted as a new line
|
|
303
|
+
chunks[0][0] += 1
|
|
304
|
+
chunks[0][1] -= 1
|
|
305
|
+
|
|
306
|
+
# If we have an unequal chunk size, use it also
|
|
307
|
+
if chunk_offset > 0:
|
|
308
|
+
chunks.append([cpus * chunk_size, chunk_offset])
|
|
309
|
+
|
|
310
|
+
# Use the multithreading for counting wrapped lines and update the line number
|
|
311
|
+
line_number -= sum(threads.map(self.__count_wrapped_lines, chunks))
|
|
312
|
+
|
|
313
|
+
# Otherwise, use the main thread for counting the real line numbers
|
|
314
|
+
else:
|
|
315
|
+
try:
|
|
316
|
+
for hidden_line in range(1, first_visible_line):
|
|
317
|
+
if self.__text_input._lines_flags[hidden_line] != 1:
|
|
318
|
+
line_number -= 1
|
|
319
|
+
|
|
320
|
+
except IndexError:
|
|
321
|
+
pass
|
|
322
|
+
|
|
323
|
+
# Draw background
|
|
324
|
+
BorderImage(source=self.background_texture, pos=self.pos, size=self.size, border=self.border)
|
|
325
|
+
|
|
326
|
+
# Check for all visible lines
|
|
327
|
+
for line in range(first_visible_line, total_lines):
|
|
328
|
+
try:
|
|
329
|
+
# If the text is wrapped, draw the logical line number only for non-wrapped lines
|
|
330
|
+
if self.__text_input.do_wrap:
|
|
331
|
+
if self.__text_input._lines_flags[line] == 1 or line == 0:
|
|
332
|
+
line_number += 1
|
|
333
|
+
self.__draw_line_number(line_number, y, y_min_render, y_max_render)
|
|
334
|
+
|
|
335
|
+
# Otherwise draw the line number for every line
|
|
336
|
+
else:
|
|
337
|
+
self.__draw_line_number(line + 1, y, y_min_render, y_max_render)
|
|
338
|
+
|
|
339
|
+
except IndexError:
|
|
340
|
+
pass
|
|
341
|
+
|
|
342
|
+
# Move to the next line's y position
|
|
343
|
+
y -= line_height
|
|
344
|
+
|
|
345
|
+
# Break if we've rendered all the visible lines
|
|
346
|
+
if y < y_min: break
|