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,11 +1,3 @@
__all__ = ("toast",)
from kivy.utils import platform
if platform == "android":
try:
from .androidtoast import toast
except ModuleNotFoundError:
from .kivytoast import toast
else:
from .kivytoast import toast
from .androidtoast import toast

View file

@ -8,26 +8,27 @@ AndroidToast
# Will be automatically used native implementation of the toast
# if your application is running on an Android device.
# Otherwise, will be used toast implementation
# from the kivymd/toast/kivytoast package.
# On desktop use `MDSnackbar < https://kivymd.readthedocs.io/en/latest/components/snackbar/>`_
from kivy.lang import Builder
from kivy.uix.screenmanager import ScreenManager
from kivymd.toast import toast
from kivymd.app import MDApp
KV = '''
MDScreen:
md_bg_color: self.theme_cls.backgroundColor
MDFlatButton:
text: "My Toast"
MDButton:
pos_hint:{"center_x": .5, "center_y": .5}
on_press: app.show_toast()
MDButtonText:
text: "Make toast"
'''
class Test(MDApp):
class Example(MDApp):
def build(self):
return Builder.load_string(KV)
@ -35,7 +36,7 @@ AndroidToast
toast("Hello World", True, 80, 200, 0)
Test().run()
Example().run()
"""
__all__ = ("toast",)
@ -47,10 +48,10 @@ if platform != "android":
f"{platform.capitalize()} platform does not support Android Toast"
)
from android import mActivity
from android.runnable import run_on_ui_thread
from jnius import autoclass
activity = autoclass("org.kivy.android.PythonActivity").mActivity
Toast = autoclass("android.widget.Toast")
String = autoclass("java.lang.String")
@ -63,7 +64,7 @@ def toast(text, length_long=False, gravity=0, y=0, x=0):
:param length_long: the amount of time (in seconds) that the toast is
visible on the screen;
:param text: text to be displayed in the toast;
:param short_duration: duration of the toast, if `True` the toast
:param length_long: duration of the toast, if `True` the toast
will last 2.3s but if it is `False` the toast will last 3.9s;
:param gravity: refers to the toast position, if it is 80 the toast will
be shown below, if it is 40 the toast will be displayed above;
@ -76,6 +77,6 @@ def toast(text, length_long=False, gravity=0, y=0, x=0):
"""
duration = Toast.LENGTH_SHORT if length_long else Toast.LENGTH_LONG
t = Toast.makeText(activity, String(text), duration)
t = Toast.makeText(mActivity, String(text), duration)
t.setGravity(gravity, x, y)
t.show()

View file

@ -1,12 +0,0 @@
"""
Toast for Android device
========================
.. image:: https://github.com/HeaTTheatR/KivyMD-data/raw/master/gallery/kivymddoc/toast.png
:align: center
"""
__all__ = ("toast",)
from .androidtoast import toast

View file

@ -1,3 +0,0 @@
__all__ = ("toast",)
from .kivytoast import toast

View file

@ -1,154 +0,0 @@
"""
KivyToast
=========
.. rubric:: Implementation of toasts for desktop.
.. code-block:: python
from kivy.lang import Builder
from kivymd.app import MDApp
from kivymd.toast import toast
KV = '''
MDScreen:
MDTopAppBar:
title: 'Test Toast'
pos_hint: {'top': 1}
left_action_items: [['menu', lambda x: x]]
MDRaisedButton:
text: 'TEST KIVY TOAST'
pos_hint: {'center_x': .5, 'center_y': .5}
on_release: app.show_toast()
'''
class Test(MDApp):
def show_toast(self):
'''Displays a toast on the screen.'''
toast('Test Kivy Toast')
def build(self):
return Builder.load_string(KV)
Test().run()
"""
from typing import List
from kivy.animation import Animation
from kivy.clock import Clock
from kivy.core.window import Window
from kivy.lang import Builder
from kivy.metrics import dp
from kivy.properties import ListProperty, NumericProperty
from kivy.uix.label import Label
from kivymd.uix.dialog import BaseDialog
Builder.load_string(
"""
<Toast>:
size_hint: (None, None)
pos_hint: {"center_x": 0.5, "center_y": 0.1}
opacity: 0
auto_dismiss: True
overlay_color: [0, 0, 0, 0]
canvas:
Color:
rgba: root._md_bg_color
RoundedRectangle:
pos: self.pos
size: self.size
radius: root.radius
"""
)
class Toast(BaseDialog):
duration = NumericProperty(2.5)
"""
The amount of time (in seconds) that the toast is visible on the screen.
:attr:`duration` is an :class:`~kivy.properties.NumericProperty`
and defaults to `2.5`.
"""
_md_bg_color = ListProperty()
def __init__(self, **kwargs):
super().__init__(**kwargs)
self.label_toast = Label(size_hint=(None, None), markup=True, opacity=0)
self.label_toast.bind(texture_size=self.label_check_texture_size)
self.add_widget(self.label_toast)
def label_check_texture_size(
self, instance_label: Label, texture_size: List[int]
) -> None:
"""
Resizes the text if the text texture is larger than the screen size.
Sets the size of the toast according to the texture size of the toast
text.
"""
texture_width, texture_height = texture_size
if texture_width > Window.width:
instance_label.text_size = (Window.width - dp(10), None)
instance_label.texture_update()
texture_width, texture_height = instance_label.texture_size
self.size = (texture_width + 25, texture_height + 25)
def toast(self, text_toast: str) -> None:
"""Displays a toast."""
self.label_toast.text = text_toast
self.open()
def on_open(self) -> None:
"""Default open event handler."""
self.fade_in()
Clock.schedule_once(self.fade_out, self.duration)
def fade_in(self) -> None:
"""Animation of opening toast on the screen."""
anim = Animation(opacity=1, duration=0.4)
anim.start(self.label_toast)
anim.start(self)
def fade_out(self, *args) -> None:
"""Animation of hiding toast on the screen."""
anim = Animation(opacity=0, duration=0.4)
anim.bind(on_complete=lambda *x: self.dismiss())
anim.start(self.label_toast)
anim.start(self)
def on_touch_down(self, touch):
if not self.collide_point(*touch.pos):
if self.auto_dismiss:
self.fade_out()
return False
super().on_touch_down(touch)
return True
def toast(
text: str = "", background: list = None, duration: float = 2.5
) -> None:
"""
Displays a toast.
:param text: text to be displayed in the toast;
:param duration: the amount of time (in seconds) that the toast is visible on the screen
:param background: toast background color in ``rgba`` format;
"""
if background is None:
background = [0.2, 0.2, 0.2, 1]
Toast(duration=duration, _md_bg_color=background).toast(text)