U
    Pe*                     @   s   d Z dZddlZddlmZ ddlmZmZmZm	Z	m
Z
mZ ddlmZ ddlmZmZmZ ddlmZ dd	lmZ dd
lmZ ddlmZ ddlmZ ddlmZ ddlmZ ddl m!Z! ddl"m#Z#m$Z$ ddl%m&Z& G dd deZ'dS )a67  
Application
===========

The :class:`App` class is the base for creating Kivy applications.
Think of it as your main entry point into the Kivy run loop. In most
cases, you subclass this class and make your own app. You create an
instance of your specific app class and then, when you are ready to
start the application's life cycle, you call your instance's
:meth:`App.run` method.


Creating an Application
-----------------------

Method using build() override
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

To initialize your app with a widget tree, override the :meth:`~App.build`
method in your app class and return the widget tree you constructed.

Here's an example of a very simple application that just shows a button:

.. include:: ../../examples/application/app_with_build.py
   :literal:

The file is also available in the examples folder at
:file:`kivy/examples/application/app_with_build.py`.

Here, no widget tree was constructed (or if you will, a tree with only
the root node).


Method using kv file
~~~~~~~~~~~~~~~~~~~~

You can also use the :doc:`api-kivy.lang` for creating applications. The
.kv can contain rules and root widget definitions at the same time. Here
is the same example as the Button one in a kv file.

Contents of 'test.kv':

.. include:: ../../examples/application/test.kv
   :literal:

Contents of 'main.py':

.. include:: ../../examples/application/app_with_kv.py
   :literal:

See :file:`kivy/examples/application/app_with_kv.py`.

The relationship between main.py and test.kv is explained in
:meth:`App.load_kv`.

.. _Application configuration:

Application configuration
-------------------------

Use the configuration file
~~~~~~~~~~~~~~~~~~~~~~~~~~

Your application might need its own configuration file. The
:class:`App` class handles 'ini' files automatically if you add
the section key-value pair to the :meth:`App.build_config` method using the
`config` parameter (an instance of :class:`~kivy.config.ConfigParser`)::

    class TestApp(App):
        def build_config(self, config):
            config.setdefaults('section1', {
                'key1': 'value1',
                'key2': '42'
            })

As soon as you add one section to the config, a file is created on the
disk (see :attr:`~App.get_application_config` for its location) and
named based your class name. "TestApp" will give a config file named
"test.ini" with the content::

    [section1]
    key1 = value1
    key2 = 42

The "test.ini" will be automatically loaded at runtime and you can access the
configuration in your :meth:`App.build` method::

    class TestApp(App):
        def build_config(self, config):
            config.setdefaults('section1', {
                'key1': 'value1',
                'key2': '42'
            })

        def build(self):
            config = self.config
            return Label(text='key1 is %s and key2 is %d' % (
                config.get('section1', 'key1'),
                config.getint('section1', 'key2')))

Create a settings panel
~~~~~~~~~~~~~~~~~~~~~~~

Your application can have a settings panel to let your user configure some of
your config tokens. Here is an example done in the KinectViewer example
(available in the examples directory):

    .. image:: images/app-settings.jpg
        :align: center

You can add your own panels of settings by extending
the :meth:`App.build_settings` method.
Check the :class:`~kivy.uix.settings.Settings` about how to create a panel,
because you need a JSON file / data first.

Let's take as an example the previous snippet of TestApp with custom
config. We could create a JSON like this::

    [
        { "type": "title",
          "title": "Test application" },

        { "type": "options",
          "title": "My first key",
          "desc": "Description of my first key",
          "section": "section1",
          "key": "key1",
          "options": ["value1", "value2", "another value"] },

        { "type": "numeric",
          "title": "My second key",
          "desc": "Description of my second key",
          "section": "section1",
          "key": "key2" }
    ]

Then, we can create a panel using this JSON to automatically create all the
options and link them to our :attr:`App.config` ConfigParser instance::

    class TestApp(App):
        # ...
        def build_settings(self, settings):
            jsondata = """... put the json data here ..."""
            settings.add_json_panel('Test application',
                self.config, data=jsondata)

That's all! Now you can press F1 (default keystroke) to toggle the
settings panel or press the "settings" key on your android device. You
can manually call :meth:`App.open_settings` and
:meth:`App.close_settings` if you want to handle this manually. Every
change in the panel is automatically saved in the config file.

You can also use :meth:`App.build_settings` to modify properties of
the settings panel. For instance, the default panel has a sidebar for
switching between json panels whose width defaults to 200dp. If you'd
prefer this to be narrower, you could add::

    settings.interface.menu.width = dp(100)

to your :meth:`build_settings` method.

You might want to know when a config value has been changed by the
user in order to adapt or reload your UI. You can then overload the
:meth:`on_config_change` method::

    class TestApp(App):
        # ...
        def on_config_change(self, config, section, key, value):
            if config is self.config:
                token = (section, key)
                if token == ('section1', 'key1'):
                    print('Our key1 has been changed to', value)
                elif token == ('section1', 'key2'):
                    print('Our key2 has been changed to', value)

The Kivy configuration panel is added by default to the settings
instance. If you don't want this panel, you can declare your Application as
follows::

    class TestApp(App):
        use_kivy_settings = False
        # ...

This only removes the Kivy panel but does not stop the settings instance
from appearing. If you want to prevent the settings instance from appearing
altogether, you can do this::

    class TestApp(App):
        def open_settings(self, *largs):
            pass

.. versionadded:: 1.0.7

Profiling with on_start and on_stop
-----------------------------------

It is often useful to profile python code in order to discover locations to
optimise. The standard library profilers
(http://docs.python.org/2/library/profile.html) provides multiple options for
profiling code. For profiling the entire program, the natural
approaches of using profile as a module or profile's run method does not work
with Kivy. It is however, possible to use :meth:`App.on_start` and
:meth:`App.on_stop` methods::

    import cProfile

    class MyApp(App):
        def on_start(self):
            self.profile = cProfile.Profile()
            self.profile.enable()

        def on_stop(self):
            self.profile.disable()
            self.profile.dump_stats('myapp.profile')

This will create a file called `myapp.profile` when you exit your app.

Customising layout
------------------

You can choose different settings widget layouts by setting
:attr:`App.settings_cls`. By default, this is a
:class:`~kivy.uix.settings.Settings` class which provides the pictured
sidebar layout, but you could set it to any of the other layouts
provided in :mod:`kivy.uix.settings` or create your own. See the
module documentation for :mod:`kivy.uix.settings` for more
information.

You can customise how the settings panel is displayed by
overriding :meth:`App.display_settings` which is called before
displaying the settings panel on the screen. By default, it
simply draws the panel on top of the window, but you could modify it
to (for instance) show the settings in a
:class:`~kivy.uix.popup.Popup` or add it to your app's
:class:`~kivy.uix.screenmanager.ScreenManager` if you are using
one. If you do so, you should also modify :meth:`App.close_settings`
to exit the panel appropriately. For instance, to have the settings
panel appear in a popup you can do::

    def display_settings(self, settings):
        try:
            p = self.settings_popup
        except AttributeError:
            self.settings_popup = Popup(content=settings,
                                        title='Settings',
                                        size_hint=(0.8, 0.8))
            p = self.settings_popup
        if p.content is not settings:
            p.content = settings
        p.open()

    def close_settings(self, *args):
        try:
            p = self.settings_popup
            p.dismiss()
        except AttributeError:
            pass # Settings popup doesn't exist

Finally, if you want to replace the current settings panel widget, you
can remove the internal references to it using
:meth:`App.destroy_settings`. If you have modified
:meth:`App.display_settings`, you should be careful to detect if the
settings panel has been replaced.

Pause mode
----------

.. versionadded:: 1.1.0

On tablets and phones, the user can switch at any moment to another
application. By default, your application will close and the
:meth:`App.on_stop` event will be fired.

If you support Pause mode, when switching to another application, your
application will wait indefinitely until the user
switches back to your application. There is an issue with OpenGL on Android
devices: it is not guaranteed that the OpenGL ES Context will be restored when
your app resumes. The mechanism for restoring all the OpenGL data is not yet
implemented in Kivy.

The currently implemented Pause mechanism is:

    #. Kivy checks every frame if Pause mode is activated by the Operating
       System due to the user switching to another application, a phone
       shutdown or any other reason.
    #. :meth:`App.on_pause` is called:
    #. If False is returned or :meth:`App.on_pause` has no return statement,
       then :meth:`App.on_stop` is called.
    #. If True is returned or :meth:`App.on_pause` is not defined, the
       application will sleep until the OS resumes our App.
    #. When the app is resumed, :meth:`App.on_resume` is called.
    #. If our app memory has been reclaimed by the OS, then nothing will be
       called.

Here is a simple example of how on_pause() should be used::

   class TestApp(App):

      def on_pause(self):
         # Here you can save data if needed
         return True

      def on_resume(self):
         # Here you can check if any data needs replacing (usually nothing)
         pass

.. warning::

    Both `on_pause` and `on_stop` must save important data because after
    `on_pause` is called, `on_resume` may not be called at all.

Asynchronous app
----------------

In addition to running an app normally,
Kivy can be run within an async event loop such as provided by the standard
library asyncio package or the trio package (highly recommended).

Background
~~~~~~~~~~

Normally, when a Kivy app is run, it blocks the thread that runs it until the
app exits. Internally, at each clock iteration it executes all the app
callbacks, handles graphics and input, and idles by sleeping for any remaining
time.

To be able to run asynchronously, the Kivy app may not sleep, but instead must
release control of the running context to the asynchronous event loop running
the Kivy app. We do this when idling by calling the appropriate functions of
the async package being used instead of sleeping.

Async configuration
~~~~~~~~~~~~~~~~~~~

To run a Kivy app asynchronously, either the :func:`async_runTouchApp` or
:meth:`App.async_run` coroutine must be scheduled to run in the event loop of
the async library being used.

The environmental variable ``KIVY_EVENTLOOP`` or the ``async_lib`` parameter in
:func:`async_runTouchApp` and :meth:`App.async_run` set the async
library that Kivy uses internally when the app is run with
:func:`async_runTouchApp` and :meth:`App.async_run`. It can be set to one of
`"asyncio"` when the standard library `asyncio` is used, or `"trio"` if the
trio library is used. If the environment variable is not set and ``async_lib``
is not provided, the stdlib ``asyncio`` is used.

:meth:`~kivy.clock.ClockBaseBehavior.init_async_lib` can also be directly
called to set the async library to use, but it may only be called before the
app has begun running with :func:`async_runTouchApp` or :meth:`App.async_run`.

To run the app asynchronously, one schedules :func:`async_runTouchApp`
or :meth:`App.async_run` to run within the given library's async event loop as
in the examples shown below. Kivy is then treated as just another coroutine
that the given library runs in its event loop. Internally, Kivy will use the
specified async library's API, so ``KIVY_EVENTLOOP`` or ``async_lib`` must
match the async library that is running Kivy.


For a fuller basic and more advanced examples, see the demo apps in
``examples/async``.

Asyncio example
~~~~~~~~~~~~~--

.. code-block:: python

    import asyncio

    from kivy.app import async_runTouchApp
    from kivy.uix.label import Label


    loop = asyncio.get_event_loop()
    loop.run_until_complete(
        async_runTouchApp(Label(text='Hello, World!'), async_lib='asyncio'))
    loop.close()

Trio example
~~~~~~~~~~--

.. code-block:: python

    import trio

    from kivy.app import async_runTouchApp
    from kivy.uix.label import Label

    from functools import partial

    # use functools.partial() to pass keyword arguments:
    async_runTouchApp_func = partial(async_runTouchApp, async_lib='trio')

    trio.run(async_runTouchApp_func, Label(text='Hello, World!'))

Interacting with Kivy app from other coroutines
-----------------------------------------------

It is fully safe to interact with any kivy object from other coroutines
running within the same async event loop. This is because they are all running
from the same thread and the other coroutines are only executed when Kivy
is idling.

Similarly, the kivy callbacks may safely interact with objects from other
coroutines running in the same event loop. Normal single threaded rules apply
to both case.

.. versionadded:: 2.0.0

)ApprunTouchAppasync_runTouchAppstopTouchApp    N)getfile)dirnamejoinexistssep
expanduserisfile)ConfigParser)r   r   r   )string_types)Factory)Logger)EventDispatcher)Builder)resource_find)platform)Widget)ObjectPropertyStringProperty)USE_SDL2c                       s  e Zd ZdZedZedZdZedZ	edZ
edZdZdZdZ fddZdd	 Zd
d Zdd ZdOddZdd Zdd ZdPddZedd Zdd Zedd Zdd Zedd  Zed!d" Zd#d$ Zd%d& ZdQd'd(Z d)d* Z!d+d, Z"d-d. Z#d/d0 Z$d1d2 Z%d3d4 Z&d5d6 Z'e(d7d8 Z)d9d: Z*d;d< Z+d=d> Z,d?d@ Z-dAdB Z.dCdD Z/dEdF Z0dGdH Z1dIdJ Z2dKdL Z3dMdN Z4  Z5S )Rr   a   Application class, see module documentation for more information.

    :Events:
        `on_start`:
            Fired when the application is being started (before the
            :func:`~kivy.base.runTouchApp` call.
        `on_stop`:
            Fired when the application stops.
        `on_pause`:
            Fired when the application is paused by the OS.
        `on_resume`:
            Fired when the application is resumed from pause by the OS. Beware:
            you have no guarantee that this event will be fired after the
            `on_pause` event has been called.

    .. versionchanged:: 1.7.0
        Parameter `kv_file` added.

    .. versionchanged:: 1.8.0
        Parameters `kv_file` and `kv_directory` are now properties of App.
    NT)on_starton_stopon_pause	on_resumeon_config_change c                    sL   | t _d | _d | _d | _d | _tt | jf | d| _|| _	d | _
d | _d S )NF)r   _running_app_app_directory	_app_name_app_settings_app_windowsuper__init__builtoptionsconfigroot)selfkwargs	__class__ ,/tmp/pip-unpacked-wheel-xzebddm3/kivy/app.pyr%   7  s    zApp.__init__c                 C   s   | j st S dS )a(  Initializes the application; it will be called only once.
        If this method returns a widget (tree), it will be used as the root
        widget and added to the window.

        :return:
            None or a root :class:`~kivy.uix.widget.Widget` instance
            if no self.root exists.N)r)   r   r*   r.   r.   r/   buildL  s    	z	App.buildc                 C   s   dS )a%  .. versionadded:: 1.0.7

        This method is called before the application is initialized to
        construct your :class:`~kivy.config.ConfigParser` object. This
        is where you can put any default section / key / value for your
        config. If anything is set, the configuration will be
        automatically saved in the file returned by
        :meth:`get_application_config`.

        :Parameters:
            `config`: :class:`~kivy.config.ConfigParser`
                Use this to add default section / key / value items

        Nr.   )r*   r(   r.   r.   r/   build_configX  s    zApp.build_configc                 C   s   dS )a  .. versionadded:: 1.0.7

        This method is called when the user (or you) want to show the
        application settings. It is called once when the settings panel
        is first opened, after which the panel is cached. It may be
        called again if the cached settings panel is removed by
        :meth:`destroy_settings`.

        You can use this method to add settings panels and to
        customise the settings widget e.g. by changing the sidebar
        width. See the module documentation for full details.

        :Parameters:
            `settings`: :class:`~kivy.uix.settings.Settings`
                Settings instance for adding panels

        Nr.   )r*   settingsr.   r.   r/   build_settingsh  s    zApp.build_settingsc                 C   s   |rt |}nztt| j}|dkr*d}W n tk
rD   d}Y nX | jpN|}| jj }|drt	t
|d| s|dd }t
|d| }td| t |}|dkst|std|  d	S t|}|r|| _d
S )a  This method is invoked the first time the app is being run if no
        widget tree has been constructed before for this app.
        This method then looks for a matching kv file in the same directory as
        the file that contains the application class.

        For example, say you have a file named main.py that contains::

            class ShowcaseApp(App):
                pass

        This method will search for a file named `showcase.kv` in
        the directory that contains main.py. The name of the kv file has to be
        the lowercase name of the class, without the 'App' postfix at the end
        if it exists.

        You can define rules and a root widget in your kv file::

            <ClassName>: # this is a rule
                ...

            ClassName: # this is a root widget
                ...

        There must be only one root widget. See the :doc:`api-kivy.lang`
        documentation for more information on how to create kv files. If your
        kv file contains a root widget, it will be used as self.root, the root
        widget for the application.

        .. note::

            This function is called from :meth:`run`, therefore, any widget
            whose styling is defined in this kv file and is created before
            :meth:`run` is called (e.g. in `__init__`), won't have its styling
            applied. Note that :meth:`build` is called after :attr:`load_kv`
            has been called.
        r   .appz%s.kvNzApp: Loading kv <{0}>zApp: kv <%s> not foundFT)r   r   r   r-   	TypeErrorkv_directory__name__lowerendswithr   r   r   debugformatr	   r   Z	load_filer)   )r*   filenameZdefault_kv_directoryr9   clsnameZ	rfilenamer)   r.   r.   r/   load_kv{  s0    &




zApp.load_kvc                 C   s2   | j dk	r| j S | jj}|dr.|dd }|S )z,Return the name of the application.
        Nr   r7   )titler-   r:   r<   r*   r@   r.   r.   r/   get_application_name  s    

zApp.get_application_namec                 C   s   t | jsdS t | jS dS )z,Return the icon of the application.
        r   N)r   iconr0   r.   r.   r/   get_application_icon  s    
zApp.get_application_icon%(appdir)s/%(appname)s.inic                 C   sT   t dkrt| jd| jS t dkr*d}nt dkr>|dt}t|| j| jd S )aV  
        Return the filename of your application configuration. Depending
        on the platform, the application file will be stored in
        different locations:

            - on iOS: <appdir>/Documents/.<appname>.ini
            - on Android: <user_data_dir>/.<appname>.ini
            - otherwise: <appdir>/<appname>.ini

        When you are distributing your application on Desktops, please
        note that if the application is meant to be installed
        system-wide, the user might not have write-access to the
        application directory. If you want to store user settings, you
        should overload this method and change the default behavior to
        save the configuration file in the user directory. ::

            class TestApp(App):
                def get_application_config(self):
                    return super(TestApp, self).get_application_config(
                        '~/.%(appname)s.ini')

        Some notes:

        - The tilda '~' will be expanded to the user directory.
        - %(appdir)s will be replaced with the application :attr:`directory`
        - %(appname)s will be replaced with the application :attr:`name`

        .. versionadded:: 1.0.7

        .. versionchanged:: 1.4.0
            Customized the defaultpath for iOS and Android platforms. Added a
            defaultpath parameter for desktop OS's (not applicable to iOS
            and Android.)

        .. versionchanged:: 1.11.0
            Changed the Android version to make use of the
            :attr:`~App.user_data_dir` and added a missing dot to the iOS
            config file name.
        androidz.{0}.iniiosz~/Documents/.%(appname)s.iniwin/)appnameZappdir)	r   r   user_data_dirr>   namereplacer
   r   	directory)r*   Zdefaultpathr.   r.   r/   get_application_config  s    ) zApp.get_application_configc                 C   s   | j S )z_.. versionadded:: 1.9.0

        Returns the root window instance used by :meth:`run`.
        )r#   r0   r.   r.   r/   root_window  s    zApp.root_windowc                 C   s&  zt d}W n tk
r&   d}Y nX |dkr:t dd}|| _| | t| dkr^dS |  }|dkrr|S t	d
| t|rz|| W nf   td d|_zt d}W n tk
r   d}Y nX |dkrt dd}|| _| | Y nX nt	d
| ||_|  |S )	a  (internal) This function is used for returning a ConfigParser with
        the application configuration. It's doing 3 things:

            #. Creating an instance of a ConfigParser
            #. Loading the default configuration by calling
               :meth:`build_config`, then
            #. If it exists, it loads the application configuration file,
               otherwise it creates one.

        :return:
            :class:`~kivy.config.ConfigParser` instance
        r6   N)rN   r   z App: Loading configuration <{0}>z$App: Corrupted config file, ignored.r   z&App: First configuration, create <{0}>)r   Zget_configparserKeyErrorr(   r2   lensectionsrQ   r   r=   r>   r	   readerrorrN   r?   write)r*   r(   r?   r.   r.   r/   load_config
  sF    







zApp.load_configc                 C   sP   | j dkrJz$tt| j| _ | j dkr,d| _ W n tk
rH   d| _ Y nX | j S )z[.. versionadded:: 1.0.7

        Return the directory where the application lives.
        Nr   r5   )r    r   r   r-   r8   r0   r.   r.   r/   rP   ?  s    


zApp.directoryc                 C   s   d}t dkrttd| j}nt dkrbddlm}m} |d}|d|j}|d	| }|	 }n\t d
krt
jt
jd | j}n<t dkrd| j}t|}nt
jdd}tt|| j}t|st
| |S )Nr   rI   z~/DocumentsrH   r   )	autoclasscastzorg.kivy.android.PythonActivityzandroid.content.Contextzjava.io.FilerJ   APPDATAmacosxz ~/Library/Application Support/{}XDG_CONFIG_HOMEz	~/.config)r   r   r   rN   ZjniusrZ   r[   	mActivityZgetFilesDirZgetAbsolutePathospathenvironr>   getr	   mkdir)r*   data_dirrZ   r[   ZPythonActivitycontextZfile_pr.   r.   r/   _get_user_data_dirO  s&    


zApp._get_user_data_dirc                 C   s   | j dkr|  | _ | j S )a  
        .. versionadded:: 1.7.0

        Returns the path to the directory in the users file system which the
        application can use to store additional data.

        Different platforms have different conventions with regards to where
        the user can store data such as preferences, saved games and settings.
        This function implements these conventions. The <app_name> directory
        is created when the property is called, unless it already exists.

        On iOS, `~/Documents/<app_name>` is returned (which is inside the
        app's sandbox).

        On Windows, `%APPDATA%/<app_name>` is returned.

        On OS X, `~/Library/Application Support/<app_name>` is returned.

        On Linux, `$XDG_CONFIG_HOME/<app_name>` is returned.

        On Android, `Context.GetFilesDir
        <https://developer.android.com/reference/android/content/Context.html#getFilesDir()>`_ is returned.

        .. versionchanged:: 1.11.0

            On Android, this function previously returned
            `/sdcard/<app_name>`. This folder became read-only by default
            in Android API 26 and the user_data_dir has therefore been moved
            to a writeable location.

        r   )_user_data_dirrg   r0   r.   r.   r/   rM   f  s    "

zApp.user_data_dirc                 C   s8   | j dkr2| jj}|dr(|dd }| | _ | j S )ze.. versionadded:: 1.0.7

        Return the name of the application based on the class name.
        Nr   r7   )r!   r-   r:   r<   r;   rC   r.   r.   r/   rN     s    


zApp.namec                 C   s   | j s.|   | j| jd |  }|r.|| _| jrjt| jtsRt	d t
dddlm} || j ddlm} |j}|r|| _||   |  }|r|| | | nt	d d S | d d S )	N)r?   z(App.root must be an _instance_ of WidgetzInvalid instance in App.rootr   )Window)	EventLoopz?Application: No window is created. Terminating application run.r   )r&   rY   rA   kv_filer1   r)   
isinstancer   r   critical	ExceptionZkivy.core.windowri   
add_widget	kivy.baserj   windowr#   	set_titlerD   rF   set_icon_install_settings_keysdispatch)r*   r)   ri   rj   rq   rE   r.   r.   r/   _run_prepare  s0    


zApp._run_preparec                 C   s   |    t  |   dS )z-Launches the app in standalone mode.
        N)rv   r   _stopr0   r.   r.   r/   run  s    zApp.runc                    s$   |    t|dI dH  |   dS )zIdentical to :meth:`run`, but is a coroutine and can be
        scheduled in a running async event loop.

        See :mod:`kivy.app` for example usage.

        .. versionadded:: 2.0.0
        )	async_libN)rv   r   rw   )r*   ry   r.   r.   r/   	async_run  s    zApp.async_runc                 G   s*   t dkrddlm} |  n|   dS )zStop the application.

        If you use this method, the whole application will stop by issuing
        a call to :func:`~kivy.base.stopTouchApp`.
        Except on Android, set Android state to stop, Kivy state then follows.
        rH   r   r_   N)r   rH   r_   ZfinishAndRemoveTaskrw   r*   largsr_   r.   r.   r/   stop  s    
zApp.stopc                 G   s:   |  d t  | jr0| jjD ]}| j| qd t_d S )Nr   )ru   r   r#   childrenremove_widgetr   r   )r*   r}   childr.   r.   r/   rw     s    
z	App._stopc                 G   s.   t dkr ddlm} |d n
td dS )zPause the application.

        On Android set OS state to pause, Kivy app state follows.
        No functionality on other OS.
        .. versionadded:: 2.2.0
        rH   r   r{   Tz(App.pause() is not available on this OS.N)r   rH   r_   ZmoveTaskToBackr   infor|   r.   r.   r/   pause  s    z	App.pausec                 C   s   dS )zEvent handler for the `on_start` event which is fired after
        initialization (after build() has been called) but before the
        application has started running.
        Nr.   r0   r.   r.   r/   r     s    zApp.on_startc                 C   s   dS )zEvent handler for the `on_stop` event which is fired when the
        application has finished running (i.e. the window is about to be
        closed).
        Nr.   r0   r.   r.   r/   r     s    zApp.on_stopc                 C   s   dS )a/  Event handler called when Pause mode is requested. You should
        return True if your app can go into Pause mode, otherwise
        return False and your application will be stopped.

        You cannot control when the application is going to go into this mode.
        It's determined by the Operating System and mostly used for mobile
        devices (android/ios) and for resizing.

        The default return value is True.

        .. versionadded:: 1.1.0
        .. versionchanged:: 1.10.0
            The default return value is now True.
        Tr.   r0   r.   r.   r/   r     s    zApp.on_pausec                 C   s   dS )aI  Event handler called when your application is resuming from
        the Pause mode.

        .. versionadded:: 1.1.0

        .. warning::

            When resuming, the OpenGL Context might have been damaged / freed.
            This is where you can reconstruct some of your OpenGL state
            e.g. FBO content.
        Nr.   r0   r.   r.   r/   r     s    zApp.on_resumec                   C   s   t jS )z\Return the currently running application instance.

        .. versionadded:: 1.1.0
        )r   r   r.   r.   r.   r/   get_running_app  s    zApp.get_running_appc                 C   s   dS )zEvent handler fired when a configuration token has been changed by
        the settings page.

        .. versionchanged:: 1.10.1
           Added corresponding ``on_config_change`` event.
        Nr.   )r*   r(   sectionkeyvaluer.   r.   r/   r   $  s    zApp.on_config_changec                 G   s,   | j dkr|  | _ | | j }|r(dS dS )a'  Open the application settings panel. It will be created the very
        first time, or recreated if the previously cached panel has been
        removed by :meth:`destroy_settings`. The settings panel will be
        displayed with the
        :meth:`display_settings` method, which by default adds the
        settings panel to the Window attached to your application. You
        should override that method if you want to display the
        settings panel differently.

        :return:
            True if the settings has been opened.

        NTF)r"   create_settingsdisplay_settings)r*   r}   Z	displayedr.   r.   r/   open_settings-  s    

zApp.open_settingsc                 C   s.   | j }|std||jkr*|| dS dS )a  .. versionadded:: 1.8.0

        Display the settings panel. By default, the panel is drawn directly
        on top of the window. You can define other behaviour by overriding
        this method, such as adding it to a ScreenManager or Popup.

        You should return True if the display is successful, otherwise False.

        :Parameters:
            `settings`: :class:`~kivy.uix.settings.Settings`
                You can modify this object in order to modify the settings
                display.

        zDNo windows are set on the application, you cannot open settings yet.TF)r#   rn   r   ro   )r*   r3   rJ   r.   r.   r/   r   B  s    

zApp.display_settingsc                 G   s<   | j }| j}|dks|dkr dS ||jkr8|| dS dS )zxClose the previously opened settings panel.

        :return:
            True if the settings has been closed.
        NTF)r#   r"   r   r   )r*   r}   rJ   r3   r.   r.   r/   close_settingsZ  s    

zApp.close_settingsc                 C   sn   | j dkrddlm} || _ nt| j tr8t| j | _ |   }| | | jrX|	  |j
| j| jd |S )a  Create the settings panel. This method will normally
        be called only one time per
        application life-time and the result is cached internally,
        but it may be called again if the cached panel is removed
        by :meth:`destroy_settings`.

        By default, it will build a settings panel according to
        :attr:`settings_cls`, call :meth:`build_settings`, add a Kivy panel if
        :attr:`use_kivy_settings` is True, and bind to
        on_close/on_config_change.

        If you want to plug your own way of doing settings, without the Kivy
        panel or close/config change events, this is the method you want to
        overload.

        .. versionadded:: 1.8.0
        Nr   )SettingsWithSpinner)Zon_closer   )settings_clsZkivy.uix.settingsr   rl   r   r   rc   r4   use_kivy_settingsZadd_kivy_panelbindr   _on_config_change)r*   r   sr.   r.   r/   r   i  s    

zApp.create_settingsc                 C   s   | j dk	rd| _ dS )a  .. versionadded:: 1.8.0

        Dereferences the current settings panel if one
        exists. This means that when :meth:`App.open_settings` is next
        run, a new panel will be created and displayed. It doesn't
        affect any of the contents of the panel, but lets you (for
        instance) refresh the settings panel layout if you have
        changed the settings widget in response to a screen size
        change.

        If you have modified :meth:`~App.open_settings` or
        :meth:`~App.display_settings`, you should be careful to
        correctly detect if the previous settings widget has been
        destroyed.

        N)r"   r0   r.   r.   r/   destroy_settings  s    
zApp.destroy_settingsc                 G   s   | j d|dd    d S )Nr      )r   )ru   )r*   r}   r.   r.   r/   r     s    zApp._on_config_changec                 C   s   |j | jd d S )N)Zon_keyboard)r   _on_keyboard_settings)r*   rq   r.   r.   r/   rt     s    zApp._install_settings_keysc                 G   sV   |d }d}t dkr&ts&dd l}|j}||krB|  s>|   dS |dkrR|  S d S )Nr   i  rH   T   )r   r   pygameZK_MENUr   r   )r*   rq   r}   r   Zsetting_keyr   r.   r.   r/   r     s    zApp._on_keyboard_settingsc                 C   s   | j r| j | d S N)r#   rr   )r*   instancerB   r.   r.   r/   on_title  s    zApp.on_titlec                 C   s   | j r| j |   d S r   )r#   rs   rF   )r*   r   rE   r.   r.   r/   on_icon  s    zApp.on_icon)N)rG   )N)6r:   
__module____qualname____doc__r   rB   rE   r   r   r   r9   rk   r   Z
__events__rh   r%   r1   r2   r4   rA   rD   rF   rQ   propertyrR   rY   rP   rg   rM   rN   rv   rx   rz   r~   rw   r   r   r   r   r   staticmethodr   r   r   r   r   r   r   r   rt   r   r   r   __classcell__r.   r.   r,   r/   r     sf   


C

2
5

%



	r   )(r   __all__r`   inspectr   os.pathr   r   r	   r
   r   r   Zkivy.configr   rp   r   r   r   Zkivy.compatr   Zkivy.factoryr   Zkivy.loggerr   Z
kivy.eventr   Z	kivy.langr   Zkivy.resourcesr   Z
kivy.utilsr   Zkivy.uix.widgetr   Zkivy.propertiesr   r   Zkivy.setupconfigr   r   r.   r.   r.   r/   <module>   s(       