first commit

This commit is contained in:
Yura 2024-09-15 15:12:16 +03:00
commit 417e54da96
5696 changed files with 900003 additions and 0 deletions

View file

@ -0,0 +1,11 @@
__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

View file

@ -0,0 +1,12 @@
"""
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

@ -0,0 +1,81 @@
"""
AndroidToast
============
.. rubric:: Native implementation of toast for Android devices.
.. code-block:: python
# 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.
from kivy.lang import Builder
from kivy.uix.screenmanager import ScreenManager
from kivymd.toast import toast
from kivymd.app import MDApp
KV = '''
MDScreen:
MDFlatButton:
text: "My Toast"
pos_hint:{"center_x": .5, "center_y": .5}
on_press: app.show_toast()
'''
class Test(MDApp):
def build(self):
return Builder.load_string(KV)
def show_toast(self):
toast("Hello World", True, 80, 200, 0)
Test().run()
"""
__all__ = ("toast",)
from kivy import platform
if platform != "android":
raise TypeError(
f"{platform.capitalize()} platform does not support Android Toast"
)
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")
@run_on_ui_thread
def toast(text, length_long=False, gravity=0, y=0, x=0):
"""
Displays a toast.
: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
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;
:param y: refers to the vertical position of the toast;
:param x: refers to the horizontal position of the toast;
Important: if only the text value is specified and the value of
the `gravity`, `y`, `x` parameters is not specified, their values will
be 0 which means that the toast will be shown in the center.
"""
duration = Toast.LENGTH_SHORT if length_long else Toast.LENGTH_LONG
t = Toast.makeText(activity, String(text), duration)
t.setGravity(gravity, x, y)
t.show()

View file

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

View file

@ -0,0 +1,154 @@
"""
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)