Source code for ui.menu

from base_list_ui import BaseListBackgroundableUIElement, to_be_foreground, TextView, EightPtView, SixteenPtView
from helpers import setup_logger

logger = setup_logger(__name__, "warning")








class MenuRenderingMixin(object):
    """A mixin to add Menu-specific rendering to views.
    If you're making your own view for BaseListUIElements and want it to
    work with menu UI elements, you will probably want to use this mixin,
    like this:

    .. code-block:: python

        class MeEightPtView(MenuRenderingMixin, EightPtView):
            pass

    """

    def draw_triangle(self, c, index):
        contents_entry = self.el.contents[self.first_displayed_entry + index/self.el.entry_height]
        if len(contents_entry) > 2 and callable(contents_entry[2]):
            tw, th = self.charwidth / 2, self.charheight / 2
            right_offset = 1
            top_offset = (self.charheight - th) / 2
            coords = (
                (str(-1*(right_offset+tw)), index * self.charheight + top_offset),
                (str(-1*(right_offset+tw)), index * self.charheight + top_offset + th),
                (str(-1*(right_offset)), index * self.charheight + th),
            )
            c.polygon(coords, fill=c.default_color)


class MeEightPtView(MenuRenderingMixin, EightPtView):

    def draw_menu_text(self, c, menu_text, left_offset):
        for i, line in enumerate(menu_text):
            y = (i * self.charheight - 1) if i != 0 else 0
            c.text(line, (left_offset, y))
            self.draw_triangle(c, i)


class MeTextView(MenuRenderingMixin, TextView):
    # Arrow rendering not yet done for text-based displays =(
    pass


class MeSixteenPtView(MenuRenderingMixin, SixteenPtView):

    def draw_menu_text(self, c, menu_text, left_offset):
        font = c.load_font(self.font, self.charheight)
        for i, line in enumerate(menu_text):
            y = (i * self.charheight - 1) if i != 0 else 0
            c.text(line, (left_offset, y), font=font)
            self.draw_triangle(c, i)