Source code for pygame_menu.widgets.widget.menulink

"""
pygame-menu
https://github.com/ppizarror/pygame-menu

MENULINK
Similar to a Button that opens a Menu, MenuLink is a widget that contains a Menu
reference. This Menu can be opened with .open() method.
"""

from __future__ import annotations

__all__ = ["MenuLink", "MenuLinkManager"]

from abc import ABC
from typing import TYPE_CHECKING

import pygame_menu
from pygame_menu.widgets.core.widget import AbstractWidgetManager
from pygame_menu.widgets.widget.none import NoneWidget

if TYPE_CHECKING:
    from collections.abc import Callable






class MenuLinkManager(AbstractWidgetManager, ABC):
    """
    MenuLink manager.
    """

    def menu_link(
        self, menu: pygame_menu.Menu, link_id: str = ""
    ) -> pygame_menu.widgets.MenuLink:
        """
        Adds a link to another Menu. The behavior is similar to a button, but
        this widget is invisible, and cannot be selectable.

        Added menus can be opened using the ``.open()`` method. Opened menus change
        the state of the parent Menu (the current pointer).

        .. note::

            This is applied only to the base Menu (not the currently displayed,
            stored in ``_current`` pointer); for such behavior apply to
            :py:meth:`pygame_menu.menu.Menu.get_current` object.

        :param menu: Menu to be added as a link (the new submenu)
        :param link_id: ID of the menu link
        :return: Menu link widget
        :rtype: :py:class:`pygame_menu.widgets.MenuLink`
        """
        if isinstance(menu, (pygame_menu.Menu, type(self._menu))):
            # Check for recursive
            if menu == self._menu or menu.in_submenu(self._menu, recursive=True):
                raise ValueError(
                    f'Menu "{menu.get_title()}" is already on submenu structure,'
                    f" recursive menus lead to unexpected behaviours. For "
                    f"returning to previous menu use pygame_menu.events.BACK "
                    f"event defining an optional back_count number of menus to "
                    f"return from, default is 1"
                )

        else:
            raise ValueError("menu object is not a pygame_menu.Menu class")

        # noinspection PyProtectedMember
        widget = MenuLink(
            menu=menu, menu_opener_handler=self._menu._open, link_id=link_id
        )
        self.configure_defaults_widget(widget)
        self._append_widget(widget)
        self._add_submenu(menu, widget)

        return widget