<commit_before>'''A round menu that appears on a long touch
'''
from __future__ import print_function
from kivy.uix.widget import Widget
from kivy.uix.label import Label
from kivy.uix.behaviors import ButtonBehavior
from kivy.lang import Builder
from kivy.clock import Clock
from kivy.animation import Animation
from kivy.properties import (
    NumericProperty, ListProperty, ObjectProperty, DictProperty)
from kivy.app import App

from functools import partial
from copy import copy

KV = '''
#:import pi math.pi
#:import cos math.cos
#:import sin math.sin
#:import V kivy.vector.Vector

<ModernMenu>:
    canvas.before:
        Color:
            rgba: root.cancel_color
        Ellipse:
            pos: self.center_x - self.radius, self.center_y - self.radius
            size: self.radius * 2, self.radius * 2
            angle_start: 0
            angle_end: self.circle_progress * 360 * self.creation_direction
        Color:
            rgba: self.color
        Line:
            circle:
                (
                self.center_x, self.center_y,
                self.radius, 0,
                self.circle_progress * 360 * self.creation_direction
                )
            width: self.line_width
    on_touch_down:
        V(args[1].pos).distance(self.center) < self.radius and (
        self.back() if self.choices_history else self.dismiss())

<ModernMenuLabel>:
    size: self.texture_size
    padding: 5, 5
    on_press: self.callback and self.callback(self)

    canvas.before:
        Color:
            rgba: .1, .4, .4, .9
        Rectangle:
            pos: self.pos
            size: self.size
        Line:
            points:
                (
                self.center_x, self.center_y,
                self.parent.center_x + cos(
                self.opacity * self.index * 2 * pi / self.siblings
                ) * self.parent.radius,
                self.parent.center_y + sin(
                self.opacity * self.index * 2 * pi / self.siblings
                ) * self.parent.radius
                ) if self.parent else []
            width: self.parent.line_width if self.parent else 1

    center:
        (
        self.parent.center_x +
        cos(self.opacity * self.index * 2 * pi / self.siblings) * self.radius,
        self.parent.center_y +
        sin(self.opacity * self.index * 2 * pi / self.siblings) * self.radius
        ) if (self.size and self.parent and self.parent.children) else (0, 0)
'''


def dist(p1, p2):
    (x1, y1) = p1
    (x2, y2) = p2
    return ((x1 - x2) ** 2 + (y1 - y2) ** 2) ** 5


class ModernMenuLabel(ButtonBehavior, Label):
    index = NumericProperty(0)
    radius = NumericProperty(100)
    siblings = NumericProperty(1)
    callback = ObjectProperty(None)

    def on_parent(self, *args):
        if self.parent:
            self.parent.bind(children=self.update_siblings)

    def update_siblings(self, *args):
        if self.parent:
            self.siblings = max(0, len(self.parent.children))
        else:
            self.siblings = 1


class ModernMenu(Widget):
    radius = NumericProperty(50)
    circle_width = NumericProperty(5)
    line_width = NumericProperty(2)
    color = ListProperty([.3, .3, .3, 1])
    circle_progress = NumericProperty(0)
    creation_direction = NumericProperty(1)
    creation_timeout = NumericProperty(1)
    choices = ListProperty([])
    item_cls = ObjectProperty(ModernMenuLabel)
    item_args = DictProperty({'opacity': 0})
    animation = ObjectProperty(Animation(opacity=1, d=.5))
    choices_history = ListProperty([])
    cancel_color = ListProperty([1, 0, 0, .4])

    def start_display(self, touch):
        touch.grab(self)
        a = Animation(circle_progress=1, d=self.creation_timeout)
        a.bind(on_complete=self.open_menu)
        touch.ud['animation'] = a
        a.start(self)

    def open_menu(self, *args):
        self.clear_widgets()
        for i in self.choices:
            kwargs = copy(self.item_args)<commit_msg>adapt angles on clicks close to borders<commit_after>'''A round menu that appears on a long touch
'''
from __future__ import print_function
from kivy.uix.widget import Widget
from kivy.uix.label import Label
from kivy.uix.behaviors import ButtonBehavior
from kivy.lang import Builder
from kivy.clock import Clock
from kivy.animation import Animation
from kivy.properties import (
    NumericProperty, ListProperty, ObjectProperty, DictProperty)
from kivy.app import App

from functools import partial
from copy import copy
from math import pi

KV = '''
#:import pi math.pi
#:import cos math.cos
#:import sin math.sin
#:import V kivy.vector.Vector

<ModernMenu>:
    canvas.before:
        Color:
            rgba: root.cancel_color
        Ellipse:
            pos: self.center_x - self.radius, self.center_y - self.radius
            size: self.radius * 2, self.radius * 2
            angle_start: 0
            angle_end: self.circle_progress * 360 * self.creation_direction
        Color:
            rgba: self.color
        Line:
            circle:
                (
                self.center_x, self.center_y,
                self.radius, 0,
                self.circle_progress * 360 * self.creation_direction
                )
            width: self.line_width
    on_touch_down:
        V(args[1].pos).distance(self.center) < self.radius and (
        self.back() if self.choices_history else self.dismiss())

<ModernMenuLabel>:
    size: self.texture_size
    padding: 5, 5
    on_press: self.callback and self.callback(self)

    canvas.before:
        Color:
            rgba: .1, .4, .4, .9
        Rectangle:
            pos: self.pos
            size: self.size
        Line:
            points:
                (
                self.center_x, self.center_y,
                self.parent.center_x + cos(
                self.start_angle +
                self.opacity * self.index_adj * self.angle / self.siblings_adj
                ) * self.parent.radius,
                self.parent.center_y + sin(
                self.start_angle +
                self.opacity * self.index_adj * self.angle / self.siblings_adj
                ) * self.parent.radius
                ) if self.parent else []
            width: self.parent.line_width if self.parent else 1

    center:
        (
        self.parent.center_x +
        cos(self.start_angle +
        self.opacity * self.index_adj * self.angle / self.siblings_adj
        ) * self.radius,
        self.parent.center_y +
        sin(self.start_angle +
        self.opacity * self.index_adj * self.angle / self.siblings_adj
        ) * self.radius
        ) if (self.size and self.parent and self.parent.children) else (0, 0)
'''


def dist(p1, p2):
    (x1, y1) = p1
    (x2, y2) = p2
    return ((x1 - x2) ** 2 + (y1 - y2) ** 2) ** 5


class ModernMenuLabel(ButtonBehavior, Label):
    index = NumericProperty(0)
    radius = NumericProperty(100)
    siblings = NumericProperty(1)
    callback = ObjectProperty(None)

    def calculate_angle(self, *args):
        if self.parent is None:
            return
        factor = 2.0
        self.start_angle = 0
        left = top = False
        if self.parent.center_x < self.radius:
            factor /= 2
            self.start_angle = 1.5 * pi
            left = True
        if self.parent.center_y < self.radius:
            factor /= 2
            self.start_angle = 0
        if self.parent.parent:
            if self.parent.center_y + self.radius > self.parent.parent.height:
                factor /= 2
                self.start_angle = 1.5 * pi if left else pi
                top = True
            if self.parent.center_x + self.radius > self.parent.parent.width:
                factor /= 2
                self.start_angle = pi if top else pi / 2
        self.angle = factor * pi
        # index adjustment: if 1, the items will spread the whole angle,
        #                   which is what we want if angle is < 2 * pi
        #                   if angle is 2 * pi, first item would be at the
        #                   same location as the last one, so we set 0
        idx_adj = 0 if factor == 2 else 1
        self.index_adj = self.index - idx_adj
        self.siblings_adj = max(1, self.siblings - idx_adj)

    def on_parent(self, *args):
        if self.parent:
            self.parent.bind(children=self.update_siblings,
                             center=self.calculate_angle)
            if self.parent.parent:
                self.parent.parent.bind(size=self.calculate_angle)
            self.calculate_angle()

    def update_siblings(self, *args):
        if self.parent:
            self.siblings = max(1, len(self.parent.children))
        else:
            self.siblings = 1
        self.calculate_angle()


class ModernMenu(Widget):
    radius = NumericProperty(50)
    circle_width = NumericProperty(5)
    line_width = NumericProperty(2)
    color = ListProperty([.3, .3, .3, 1])
    circle_progress = NumericProperty(0)
    creation_direction = NumericProperty(1)
    creation_timeout = NumericProperty(1)
    choices = ListProperty([])
    item_cls = ObjectProperty(ModernMenuLabel)
    item_args = DictProperty({'opacity': 0})
    animation = ObjectProperty(Animation(opacity=1, d=.5))
    choices_history = ListProperty([])
    cancel_color = ListProperty([1, 0, 0, .4])

    def start_display(self, touch):
        touch.grab(self)
        a = Animation(circle_progress=1, d=self.creation_timeout)
        a.bind(on_complete=self.open_menu)
        touch.ud['animation'] = a
        a.start(self)

    def open_menu(self, *args):
        self.clear_widgets()
        for i in self.choices:
            kwargs = copy(self.item_args)
{'fraction_non_alphanumeric': 0.09918295583824593, 'fraction_numerical': 0.01520322680732237, 'mean_word_length': 1.9790511398644486, 'pattern_counts': {'":': 0, '<': 12, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '557877', 'n_tokens_mistral': 3066, 'n_tokens_neox': 2904, 'n_words': 718}