Created
March 10, 2020 18:58
-
-
Save tshirtman/126e1481d3d588f14c617a7fcbc38b5d to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from kivy.app import App | |
from kivy.lang import Builder | |
from kivy.factory import Factory as F | |
from kivy.core.window import Window as W | |
from kivy import properties as P | |
KV = ''' | |
#:import C kivy.utils.rgba | |
<HoverableButtonLabel>: | |
font_size: 20 | |
on_press: print(f"woohoo, i'm clicked! {self.text}") | |
canvas.before: | |
Color: | |
rgba: C('222222BB') if self.hovered else C('00000000') | |
Rectangle: | |
pos: self.pos | |
size: self.size | |
Color: | |
rgba: C("FFFFFF") | |
FloatLayout: | |
RecycleView: | |
size_hint: .9, .9 | |
pos_hint: {'center': (.5, .5)} | |
data: app.data | |
viewclass: 'HoverableButtonLabel' | |
RecycleBoxLayout: | |
orientation: 'vertical' | |
default_size_hint: 1, None | |
default_size: 0, 50 | |
size_hint_y: None | |
height: self.minimum_height | |
''' | |
class HoverableButtonLabel(F.ButtonBehavior, F.Label): | |
mouse_pos = P.ListProperty() | |
hovered = P.BooleanProperty(False) | |
def __init__(self, **kwargs): | |
super().__init__(**kwargs) | |
W.bind(mouse_pos=self.save_and_check_hover) | |
self.bind(size=self.check_hover, pos=self.check_hover) | |
def save_and_check_hover(self, window, mouse_pos, *args): | |
self.mouse_pos = mouse_pos | |
self.check_hover() | |
def check_hover(self, *args): | |
if self.mouse_pos: | |
self.hovered = self.collide_point(*self.to_widget(*self.mouse_pos)) | |
class Application(App): | |
data = P.ListProperty([]) | |
def build(self): | |
self.data = [ | |
{'text': f'text {i}'} | |
for i in range(1000) | |
] | |
return Builder.load_string(KV) | |
if __name__ == "__main__": | |
Application().run() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment