working condition

This commit is contained in:
Yura 2024-09-15 20:57:02 +03:00
parent 417e54da96
commit 511e0b0379
517 changed files with 29187 additions and 32696 deletions

View file

@ -1 +1,8 @@
from .tooltip import MDTooltip, MDTooltipViewClass # NOQA F401
from .tooltip import (
MDTooltip,
MDTooltipPlain,
MDTooltipRich,
MDTooltipRichSubhead,
MDTooltipRichActionButton,
MDTooltipRichSupportingText,
) # NOQA F401

View file

@ -1,38 +1,78 @@
<MDTooltipViewClass>
size_hint: None, None
width: self.minimum_width
height: self.minimum_height + root.padding[1]
<MDTooltipPlain>
adaptive_size: True
opacity: 0
font_style: "Body"
role: "small"
padding: "8dp", "4dp", "8dp", "4dp"
radius: [dp(4), ]
scale_value_x: 0
scale_value_y: 0
text_color:
self.theme_cls.inverseOnSurfaceColor \
if self.theme_text_color == "Primary" else \
self.text_color
canvas.before:
PushMatrix
Color:
rgba:
root.theme_cls.opposite_bg_dark if not root.tooltip_bg_color \
else root.tooltip_bg_color
self.theme_cls.inverseSurfaceColor \
if self.theme_bg_color == "Primary" else \
self.md_bg_color
RoundedRectangle:
pos: self.pos
size: self.size
radius: root.tooltip_radius
Scale:
origin: self.center
x: root._scale_x
y: root._scale_y
canvas.after:
PopMatrix
pos: self.pos
radius: self.radius
MDLabel:
id: label_tooltip
text: root.tooltip_text
size_hint: None, None
-text_size: None, None
size: self.texture_size
bold: True
theme_text_color: "Custom"
font_style: root.tooltip_font_style
markup: True
pos_hint: {"center_y": .5}
text_color:
([0, 0, 0, 1] if not root.tooltip_text_color else root.tooltip_text_color) \
if root.theme_cls.theme_style == "Dark" else \
([1, 1, 1, 1] if not root.tooltip_text_color else root.tooltip_text_color)
<MDTooltipRich>
orientation: "vertical"
scale_value_x: 0
scale_value_y: 0
opacity: 0
radius: [dp(12), ]
size_hint: None, None
size: self.minimum_size
padding: "16dp", "12dp", "16dp", "8dp"
spacing: "4dp"
elevation_level: 2
elevation: self.elevation_levels[self.elevation_level]
md_bg_color:
self.theme_cls.surfaceContainerColor \
if self.theme_bg_color == "Primary" else \
self.md_bg_color
shadow_softness:
2 \
if self.theme_shadow_softness == "Primary" else \
self.shadow_softness
shadow_offset:
(0, -1) \
if self.theme_shadow_offset == "Primary" else \
self.shadow_offset
shadow_radius: [value - 2 for value in self.radius]
<MDTooltipRichSubhead>
bold: True
adaptive_size: True
font_style: "Title"
role: "small"
text_color:
self.theme_cls.onSurfaceVariantColor \
if self.theme_text_color == "Primary" else \
self.text_color
<MDTooltipRichActionButton>
style: "text"
_text_left_pad: 0
_text_right_pad: 0
ripple_effect: False
<MDTooltipRichSupportingText>
adaptive_size: True
font_style: "Body"
role: "medium"
text_color:
self.theme_cls.onSurfaceVariantColor \
if self.theme_text_color == "Primary" else \
self.text_color

View file

@ -4,70 +4,179 @@ Components/Tooltip
.. seealso::
`Material Design spec, Tooltips <https://material.io/components/tooltips>`_
`Material Design spec, Tooltips <https://m3.material.io/components/tooltips/specs>`_
.. rubric:: Tooltips display informative text when users hover over, focus on,
or tap an element.
.. rubric:: Tooltips display brief labels or messages.
.. image:: https://github.com/HeaTTheatR/KivyMD-data/raw/master/gallery/kivymddoc/tooltip.png
.. image:: https://github.com/HeaTTheatR/KivyMD-data/raw/master/gallery/kivymddoc/tooltip-m3-preview.png
:align: center
To use the :class:`~MDTooltip` class, you must create a new class inherited
from the :class:`~MDTooltip` class:
- Use tooltips to add additional context to a button or other UI element
- Two types: plain and rich
- Use plain tooltips to describe elements or actions of icon buttons
- Use rich tooltips to provide more details, like describing the value of a feature
- Rich tooltips can include an optional title, link, and buttons
In Kv-language:
**KivyMD provides two types of tooltip:**
.. code-block:: kv
1. Plain tooltip
2. Rich tooltip
<TooltipMDIconButton@MDIconButton+MDTooltip>
.. image:: https://github.com/HeaTTheatR/KivyMD-data/raw/master/gallery/kivymddoc/tooltip-m3-type.png
:align: center
In Python code:
.. code-block:: python
class TooltipMDIconButton(MDIconButton, MDTooltip):
pass
.. Warning:: :class:`~MDTooltip` only works correctly with button and label classes.
Usage of tooltip plain
----------------------
.. code-block:: python
from kivy.lang import Builder
from kivy.properties import StringProperty
from kivymd.uix.button import MDButton
from kivymd.uix.tooltip import MDTooltip
from kivymd.app import MDApp
KV = '''
<TooltipMDIconButton@MDIconButton+MDTooltip>
<YourTooltipClass>
MDTooltipPlain:
text:
"Grant value is calculated using the closing stock price \\\\n" \\
"from the day before the grant date. Amounts do not \\\\n" \\
"reflect tax witholdings."
<TooltipMDIconButton>
MDButtonText:
text: root.text
MDScreen:
md_bg_color: self.theme_cls.backgroundColor
TooltipMDIconButton:
icon: "language-python"
tooltip_text: self.icon
text: "Tooltip button"
pos_hint: {"center_x": .5, "center_y": .5}
'''
class Test(MDApp):
class YourTooltipClass(MDTooltip):
'''Implements your tooltip base class.'''
class TooltipMDIconButton(YourTooltipClass, MDButton):
'''Implements a button with tooltip behavior.'''
text = StringProperty()
class Example(MDApp):
def build(self):
self.theme_cls.primary_palette = "Olive"
return Builder.load_string(KV)
Test().run()
Example().run()
.. image:: https://github.com/HeaTTheatR/KivyMD-data/raw/master/gallery/kivymddoc/tooltip.gif
.. image:: https://github.com/HeaTTheatR/KivyMD-data/raw/master/gallery/kivymddoc/tooltip-m3-plain-usage.gif
:align: center
.. Note:: The behavior of tooltips on desktop and mobile devices is different.
For more detailed information,
`click here <https://github.com/kivymd/KivyMD/wiki/Components-Tooltips>`_.
The anatomy of a plain tooltip
------------------------------
.. image:: https://github.com/HeaTTheatR/KivyMD-data/raw/master/gallery/kivymddoc/tooltip-m3-plain-anatomy.png
:align: center
Usage of tooltip rich
---------------------
.. code-block:: python
from kivy.lang import Builder
from kivy.properties import StringProperty
from kivymd.uix.button import MDButton
from kivymd.uix.tooltip import MDTooltip
from kivymd.app import MDApp
KV = '''
<YourTooltipClass>
MDTooltipRich:
id: tooltip
auto_dismiss: False
MDTooltipRichSubhead:
text: "Add others"
MDTooltipRichSupportingText:
text:
"Grant value is calculated using the closing stock price \\\\n" \\
"from the day before the grant date. Amounts do not \\\\n" \\
"reflect tax witholdings."
MDTooltipRichActionButton:
on_press: tooltip.dismiss()
MDButtonText:
text: "Learn more"
<TooltipMDIconButton>
MDButtonText:
text: root.text
MDScreen:
md_bg_color: self.theme_cls.backgroundColor
TooltipMDIconButton:
text: "Tooltip button"
pos_hint: {"center_x": .5, "center_y": .5}
'''
class YourTooltipClass(MDTooltip):
'''Implements your tooltip base class.'''
class TooltipMDIconButton(YourTooltipClass, MDButton):
'''Implements a button with tooltip behavior.'''
text = StringProperty()
class Example(MDApp):
def build(self):
self.theme_cls.primary_palette = "Olive"
return Builder.load_string(KV)
Example().run()
.. image:: https://github.com/HeaTTheatR/KivyMD-data/raw/master/gallery/kivymddoc/tooltip-m3-rich-usage.gif
:align: center
The anatomy of a plain tooltip
------------------------------
.. image:: https://github.com/HeaTTheatR/KivyMD-data/raw/master/gallery/kivymddoc/tooltip-m3-rich-anatomy.png
:align: center
"""
__all__ = ("MDTooltip", "MDTooltipViewClass")
__all__ = (
"MDTooltip",
"MDTooltipPlain",
"MDTooltipRich",
"MDTooltipRichActionButton",
"MDTooltipRichSubhead",
"MDTooltipRichSupportingText",
)
import os
from typing import Union
from kivy.animation import Animation
from kivy.clock import Clock
@ -76,19 +185,24 @@ from kivy.lang import Builder
from kivy.metrics import dp
from kivy.properties import (
BoundedNumericProperty,
ColorProperty,
ListProperty,
NumericProperty,
OptionProperty,
StringProperty,
BooleanProperty,
)
from kivy.uix.boxlayout import BoxLayout
from kivymd.uix.behaviors.state_layer_behavior import StateLayerBehavior
from kivymd.uix.button import MDButton
from kivymd.uix.label import MDLabel
from kivymd import uix_path
from kivymd.font_definitions import theme_font_styles
from kivymd.material_resources import DEVICE_TYPE
from kivymd.theming import ThemableBehavior
from kivymd.uix.behaviors import HoverBehavior, TouchBehavior
from kivymd.uix.behaviors import (
TouchBehavior,
ScaleBehavior,
DeclarativeBehavior,
BackgroundColorBehavior,
CommonElevationBehavior,
)
with open(
os.path.join(uix_path, "tooltip", "tooltip.kv"), encoding="utf-8"
@ -96,74 +210,34 @@ with open(
Builder.load_string(kv_file.read())
class MDTooltip(ThemableBehavior, HoverBehavior, TouchBehavior):
class MDTooltip(TouchBehavior):
"""
Tooltip class.
For more information, see in the
:class:`~kivymd.theming.ThemableBehavior and
:class:`~kivymd.uix.behaviors.HoverBehavior` and
:class:`~kivymd.uix.behaviors.TouchBehavior`
classes documentation.
"""
:class:`~kivymd.uix.behaviors.touch_behavior.TouchBehavior`
class documentation.
tooltip_bg_color = ColorProperty(None)
"""
Tooltip background color in (r, g, b, a) or string format
:attr:`tooltip_bg_color` is an :class:`~kivy.properties.ColorProperty`
and defaults to `None`.
"""
tooltip_text_color = ColorProperty(None)
"""
Tooltip text color in (r, g, b, a) or string format
:attr:`tooltip_text_color` is an :class:`~kivy.properties.ColorProperty`
and defaults to `None`.
"""
tooltip_text = StringProperty()
"""
Tooltip text.
:attr:`tooltip_text` is an :class:`~kivy.properties.StringProperty`
and defaults to `''`.
"""
tooltip_font_style = OptionProperty("Caption", options=theme_font_styles)
"""
Tooltip font style. Available options are: `'H1'`, `'H2'`, `'H3'`, `'H4'`,
`'H5'`, `'H6'`, `'Subtitle1'`, `'Subtitle2'`, `'Body1'`, `'Body2'`,
`'Button'`, `'Caption'`, `'Overline'`, `'Icon'`.
:attr:`tooltip_font_style` is an :class:`~kivy.properties.OptionProperty`
and defaults to `'Caption'`.
"""
tooltip_radius = ListProperty(
[
dp(7),
]
)
"""
Corner radius values.
:attr:`radius` is an :class:`~kivy.properties.ListProperty`
and defaults to `[dp(7),]`.
:Events:
`on_open`:
Fired when the tooltip opens.
`on_dismiss`:
Fired when the tooltip is closed.
"""
tooltip_display_delay = BoundedNumericProperty(0, min=0, max=4)
"""
Tooltip dsiplay delay.
Tooltip display delay.
.. note:: This property only works on desktop.
:attr:`tooltip_display_delay` is an :class:`~kivy.properties.BoundedNumericProperty`
and defaults to `0`, min of `0` & max of `4`. This property only works on desktop.
and defaults to `0`, min of `0` & max of `4`.
"""
shift_y = NumericProperty()
"""
Y-offset of tooltip text.
Y-offset of tooltip to the top.
:attr:`shift_y` is an :class:`~kivy.properties.NumericProperty`
and defaults to `0`.
@ -171,7 +245,7 @@ class MDTooltip(ThemableBehavior, HoverBehavior, TouchBehavior):
shift_right = NumericProperty()
"""
Shifting the tooltip text to the right.
Shifting the tooltip to the right.
.. versionadded:: 1.0.0
@ -181,7 +255,7 @@ class MDTooltip(ThemableBehavior, HoverBehavior, TouchBehavior):
shift_left = NumericProperty()
"""
Shifting the tooltip text to the left.
Shifting the tooltip to the left.
.. versionadded:: 1.0.0
@ -193,7 +267,7 @@ class MDTooltip(ThemableBehavior, HoverBehavior, TouchBehavior):
def __init__(self, **kwargs):
super().__init__(**kwargs)
self.register_event_type("on_show")
self.register_event_type("on_open")
self.register_event_type("on_dismiss")
def delete_clock(self, widget, touch, *args):
@ -204,12 +278,26 @@ class MDTooltip(ThemableBehavior, HoverBehavior, TouchBehavior):
pass
self.on_leave()
def adjust_tooltip_position(self, x: float, y: float) -> tuple:
def adjust_tooltip_position(self) -> tuple:
"""
Returns the coordinates of the tooltip that fit into the borders of the
screen.
Returns the coordinates of the tooltip that fit into the borders
of the screen.
"""
pos = self.to_window(self.center_x, self.center_y)
if not self.shift_right and not self.shift_left:
x = pos[0] - (self._tooltip.width / 2)
else:
if self.shift_right:
x = pos[0] - (self._tooltip.width / 2) + self.shift_right
if self.shift_left:
x = pos[0] - (self._tooltip.width / 2) - self.shift_left
if not self.shift_y:
y = pos[1] - (self._tooltip.height + self.height)
else:
y = pos[1] - self._tooltip.height / 2 - self.height + self.shift_y
# If the position of the tooltip is outside the right border
# of the screen.
if x + self._tooltip.width > Window.width:
@ -228,27 +316,14 @@ class MDTooltip(ThemableBehavior, HoverBehavior, TouchBehavior):
y = Window.height - (self._tooltip.height + dp(10))
return x, y
def display_tooltip(self, interval: Union[int, float]) -> None:
def display_tooltip(self, *args) -> None:
"""Adds a tooltip widget to the screen and animates its display."""
if not self._tooltip or self._tooltip.parent:
return
Window.add_widget(self._tooltip)
pos = self.to_window(self.center_x, self.center_y)
if not self.shift_right and not self.shift_left:
x = pos[0] - (self._tooltip.width / 2)
else:
if self.shift_right:
x = pos[0] - (self._tooltip.width / 2) + self.shift_right
if self.shift_left:
x = pos[0] - (self._tooltip.width / 2) - self.shift_left
if not self.shift_y:
y = pos[1] - self._tooltip.height / 2 - self.height / 2 - dp(20)
else:
y = pos[1] - self._tooltip.height / 2 - self.height + self.shift_y
x, y = self.adjust_tooltip_position(x, y)
x, y = self.adjust_tooltip_position()
self._tooltip.pos = (x, y)
if DEVICE_TYPE == "desktop":
@ -258,27 +333,31 @@ class MDTooltip(ThemableBehavior, HoverBehavior, TouchBehavior):
else:
Clock.schedule_once(self.animation_tooltip_show, 0)
def animation_tooltip_show(self, interval: Union[int, float]) -> None:
def animation_tooltip_show(self, *args) -> None:
"""Animation of opening tooltip on the screen."""
if self._tooltip:
self._tooltip.shadow_color = self._tooltip.theme_cls.shadowColor
(
Animation(_scale_x=1, _scale_y=1, d=0.1)
Animation(scale_value_x=1, scale_value_y=1, d=0.2)
+ Animation(opacity=1, d=0.2)
).start(self._tooltip)
self.dispatch("on_show")
self.dispatch("on_open")
def animation_tooltip_dismiss(self, interval: Union[int, float]) -> None:
def animation_tooltip_dismiss(self, *args) -> None:
"""
.. versionadded:: 1.0.0
Animation of closing tooltip on the screen.
.. versionadded:: 1.0.0
"""
if self._tooltip:
anim = Animation(_scale_x=0, _scale_y=0, d=0.1) + Animation(
opacity=0, d=0.2
self._tooltip.shadow_color = (
self._tooltip.theme_cls.transparentColor
)
anim = Animation(
scale_value_x=0, scale_value_y=0, d=0.2
) + Animation(opacity=0, d=0.2)
anim.bind(on_complete=self._on_dismiss_anim_complete)
anim.start(self._tooltip)
@ -287,101 +366,170 @@ class MDTooltip(ThemableBehavior, HoverBehavior, TouchBehavior):
Window.remove_widget(self._tooltip)
def add_widget(self, widget, *args, **kwargs):
"""Add a new widget as a child of this widget."""
if isinstance(widget, (MDTooltipPlain, MDTooltipRich)):
self._tooltip = widget
widget._tooltip = self
else:
return super().add_widget(widget)
def on_long_touch(self, touch, *args) -> None:
if DEVICE_TYPE != "desktop":
self.on_enter()
Clock.schedule_once(self.display_tooltip, -1)
Clock.schedule_once(
self.animation_tooltip_show, self.tooltip_display_delay
)
def on_enter(self, *args) -> None:
"""
See
:attr:`~kivymd.uix.behaviors.hover_behavior.HoverBehavior.on_enter`
method in :class:`~kivymd.uix.behaviors.hover_behavior.HoverBehavior`
class.
"""
"""Fired when mouse enter the bbox of the widget."""
if self.tooltip_text:
super().on_enter()
if DEVICE_TYPE == "desktop":
if self._tooltip:
self.remove_tooltip()
self._tooltip = MDTooltipViewClass(
tooltip_bg_color=self.tooltip_bg_color,
tooltip_text_color=self.tooltip_text_color,
tooltip_text=self.tooltip_text,
tooltip_font_style=self.tooltip_font_style,
tooltip_radius=self.tooltip_radius,
)
Clock.schedule_once(self.display_tooltip, -1)
Clock.schedule_once(self.display_tooltip, 0.2)
def on_leave(self) -> None:
"""
See
:attr:`~kivymd.uix.behaviors.hover_behavior.HoverBehavior.on_leave`
method in :class:`~kivymd.uix.behaviors.hover_behavior.HoverBehavior`
class.
"""
def on_leave(self, *args) -> None:
"""Fired when the mouse goes outside the widget border."""
if self._tooltip:
super().on_leave()
if self._tooltip and (
not isinstance(self._tooltip, MDTooltipRich)
or self._tooltip.auto_dismiss
):
Clock.schedule_once(self.animation_tooltip_dismiss)
def on_show(self) -> None:
"""Default display event handler."""
def on_open(self) -> None:
"""
Default display event handler.
.. versionchanged:: 2.0.0 Rename from `on_show` to `on_open`.
"""
def on_dismiss(self) -> None:
"""
.. versionadded:: 1.0.0
Default dismiss event handler.
.. versionadded:: 1.0.0
"""
def _on_dismiss_anim_complete(self, *args):
self.dispatch("on_dismiss")
self.remove_tooltip()
self._tooltip = None
# self._tooltip = None
def _on_release(self, *args):
...
class MDTooltipViewClass(ThemableBehavior, BoxLayout):
class MDTooltipPlain(MDLabel, ScaleBehavior):
"""
Tooltip view class.
Tooltip plain class.
.. versionadded:: 2.0.0
For more information, see in the
:class:`~kivymd.theming.ThemableBehavior` and
:class:`~kivy.uix.boxlayout.BoxLayout`
:class:`~kivymd.uix.label.label.MDLabel` and
:class:`~kivymd.uix.behaviors.scale_behavior.ScaleBehavior`
classes documentation.
"""
tooltip_bg_color = ColorProperty(None)
class MDTooltipRichSupportingText(MDLabel):
"""
See :attr:`~MDTooltip.tooltip_bg_color`.
Implements supporting text for the :class:`~MDTooltipRich` class.
.. versionadded:: 2.0.0
For more information, see in the
:class:`~kivymd.uix.label.label.MDLabel` class documentation.
"""
tooltip_text_color = ColorProperty(None)
class MDTooltipRichSubhead(MDLabel):
"""
See :attr:`~MDTooltip.tooltip_text_color`.
Implements subhead text for the :class:`~MDTooltipRich` class.
.. versionadded:: 2.0.0
For more information, see in the
:class:`~kivymd.uix.label.label.MDLabel` class documentation.
"""
tooltip_text = StringProperty()
class MDTooltipRichActionButton(MDButton):
"""
See :attr:`~MDTooltip.tooltip_text`.
Implements action button for the :class:`~MDTooltipRich` class.
.. versionadded:: 2.0.0
For more information, see in the
:class:`~kivymd.uix.button.button.MDButton` class documentation.
"""
tooltip_font_style = OptionProperty("Caption", options=theme_font_styles)
# Override methods.
# Their functionality is not needed in this class.
def _set_state_layer_color(self) -> None:
...
def on_enter(self) -> None:
...
def on_leave(self) -> None:
...
class MDTooltipRich(
DeclarativeBehavior,
ThemableBehavior,
BackgroundColorBehavior,
CommonElevationBehavior,
ScaleBehavior,
StateLayerBehavior,
BoxLayout,
):
"""
See :attr:`~MDTooltip.tooltip_font_style`.
Tooltip rich class.
.. versionadded:: 2.0.0
For more information, see in the
:class:`~kivymd.uix.behaviors.declarative_behavior.DeclarativeBehavior` and
:class:`~kivymd.theming.ThemableBehavior` and
:class:`~kivymd.uix.behaviors.backgroundcolor_behavior.BackgroundColorBehavior` and
:class:`~kivymd.uix.behaviors.elevation.CommonElevationBehavior` and
:class:`~kivymd.uix.behaviors.scale_behavior.ScaleBehavior` and
:class:`~kivymd.uix.behaviors.state_layer_behavior.StateLayerBehavior` and
:class:`~kivy.uix.boxlayout.BoxLayout` and
classes documentation.
"""
tooltip_radius = ListProperty()
auto_dismiss = BooleanProperty(True)
"""
See :attr:`~MDTooltip.tooltip_radius`.
This property determines if the view is automatically dismissed when
the cursor goes outside of the tooltip body.
:attr:`auto_dismiss` is a :class:`~kivy.properties.BooleanProperty` and
defaults to True.
"""
_scale_x = NumericProperty(0)
_scale_y = NumericProperty(0)
_tooltip = None
def __init__(self, **kwargs):
super().__init__(**kwargs)
self.padding = [
dp(8) if DEVICE_TYPE == "desktop" else dp(16),
dp(4),
dp(8) if DEVICE_TYPE == "desktop" else dp(16),
dp(4),
]
def on_leave(self) -> None:
"""Fired when the mouse goes outside the widget border."""
super().on_leave()
if self._tooltip:
Clock.schedule_once(self._tooltip.animation_tooltip_dismiss)
def dismiss(self) -> None:
"""Hides the tooltip."""
self.on_leave()