Skip to content

Instantly share code, notes, and snippets.

@me2beats
Created June 18, 2019 23:48
Show Gist options
  • Save me2beats/086cf788794b7c1fe87163663c118f7c to your computer and use it in GitHub Desktop.
Save me2beats/086cf788794b7c1fe87163663c118f7c to your computer and use it in GitHub Desktop.
kivy test UnderlineSelection Behavior (for KivyStudio)
'''folder: root/bhv'''
from kivy.lang import Builder
from kivy.uix.widget import Widget
from kivy.properties import ListProperty
KV = '''
<CanvasBeforeRect>
canvas_color: [0.5,0.5,0.5,1.]
canvas.before:
Color:
rgba: self.canvas_color
Rectangle:
pos:self.canvas_pos
size: self.canvas_size
'''
class CanvasBeforeRect(Widget):
canvas_pos = ListProperty([0,0])
canvas_size = ListProperty([100,100])
def __init__(self, **kwargs):
super(CanvasBeforeRect, self).__init__(**kwargs)
self.canvas_pos = self.pos
self.canvas_size = self.size
Builder.load_string(KV)
'''folder: root/properties'''
from kivy.properties import AliasProperty
from kivy.utils import rgba
class ColorProperty(AliasProperty):
'''gets kivy rgba or hex string (#2B2B32); returns kivy rgba'''
_data = []
def __init__(self, **kwargs):
kwargs['getter'] = self._getter
kwargs['setter'] = self._setter
kwargs['cache'] =True
super().__init__(**kwargs)
def _getter(self, obj):
return self._data
def _setter(self, obj, value):
if type(value) is str:
value = rgba(value)
self._data = value
return True # Value changed, dispatch event
'''folder: root'''
from kivy.app import App
from kivy.lang import Builder
from kivy.uix.label import Label
from kivy.uix.widget import Widget
from bhv.underline_selection_bhv import UnderlineSelectionBhv
KV = """
BoxLayout
MyLabel
MyLabel
MyLabel
MyLabel
<MyLabel>
text: '123'
on_touch_down: if self.collide_point(*args[1].pos):\
self.selected = True
"""
class MyLabel(
UnderlineSelectionBhv,
Label
):
pass
class MyApp(App):
def build(self):
self.root = Builder.load_string(KV)
MyApp().run()
'''folder: root/bhv'''
'''instead I can use ColorProperty from kivy.properties'''
from properties.color_property import ColorProperty
class RgbaColorBhv(object):
canvas_color = ColorProperty(value = [0.5,0.5,0.5,1.])
'''folder: root/bhv'''
from kivy.lang import Builder
from kivy.uix.widget import Widget
from kivy.properties import BooleanProperty
from bhv.canvas_before_rect import CanvasBeforeRect
from bhv.rgba_color_bhv import RgbaColorBhv
KV = '''
<UnderlineSelectionBhv>
selected_color: '#00BA82'
unselected_color: '#404049'
canvas_size: self.width, 2
canvas_pos: self.pos
canvas_color: self.selected_color if self.selected else self.unselected_color
'''
class UnderlineSelectionBhv(
RgbaColorBhv,
CanvasBeforeRect,
Widget
):
selected = BooleanProperty(False)
Builder.load_string(KV)
@me2beats
Copy link
Author

me2beats commented Jun 18, 2019

Example (gif):
https://imgur.com/3RBrRin

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment