Created
June 29, 2015 11:25
-
-
Save opqopq/15c707dc4cffc2b6455f 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
"""Hoverable Behaviour (changing when the mouse is on the widget by O. Poyen. | |
License: LGPL | |
""" | |
__author__ = 'Olivier POYEN' | |
from kivy.properties import BooleanProperty, ObjectProperty | |
from kivy.core.window import Window | |
class HoverBehavior(object): | |
"""Hover behavior. | |
:Events: | |
`on_enter` | |
Fired when mouse enter the bbox of the widget. | |
`on_leave` | |
Fired when the mouse exit the widget | |
""" | |
hovered = BooleanProperty(False) | |
border_point= ObjectProperty(None) | |
'''Contains the last relevant point received by the Hoverable. This can | |
be used in `on_enter` or `on_leave` in order to know where was dispatched the event. | |
''' | |
def __init__(self, **kwargs): | |
self.register_event_type('on_enter') | |
self.register_event_type('on_leave') | |
Window.bind(mouse_pos=self.on_mouse_pos) | |
super(HoverBehavior, self).__init__(**kwargs) | |
def on_mouse_pos(self, *args): | |
if not self.get_root_window(): | |
return # do proceed if I'm not displayed <=> If have no parent | |
pos = args[1] | |
#Next line to_widget allow to compensate for relative layout | |
inside = self.collide_point(*self.to_widget(*pos)) | |
if self.hovered == inside: | |
#We have already done what was needed | |
return | |
self.border_point = pos | |
self.hovered = inside | |
if inside: | |
self.dispatch('on_enter') | |
else: | |
self.dispatch('on_leave') | |
def on_enter(self): | |
pass | |
def on_leave(self): | |
pass | |
from kivy.factory import Factory | |
Factory.register('HoverBehavior', HoverBehavior) | |
if __name__=='__main__': | |
from kivy.uix.floatlayout import FloatLayout | |
from kivy.lang import Builder | |
from kivy.uix.label import Label | |
from kivy.base import runTouchApp | |
class HoverLabel(Label, HoverBehavior): | |
def on_enter(self, *args): | |
print "You are in, through this point", self.border_point | |
def on_leave(self, *args): | |
print "You left through this point", self.border_point | |
Builder.load_string(''' | |
<HoverLabel>: | |
text: "inside" if self.hovered else "outside" | |
pos: 200,200 | |
size_hint: None, None | |
size: 100, 30 | |
canvas.before: | |
Color: | |
rgb: 1,0,0 | |
Rectangle: | |
size: self.size | |
pos: self.pos | |
''') | |
fl = FloatLayout() | |
fl.add_widget(HoverLabel()) | |
runTouchApp(fl) |
Hello
Thanks for your comment.
As I am hardly maintaining the code anymore, feel free to upload any changes!
I am curious though on how did you effectively track the faulty method.
Télécharger Outlook pour Android<https://aka.ms/ghei36>
…________________________________
From: TheEman1can <[email protected]>
Sent: Saturday, January 25, 2020 1:13:49 AM
To: opqopq <[email protected]>
Cc: opqopq <[email protected]>; Author <[email protected]>
Subject: Re: opqopq/hoverable.py
I know that this is very late, and not to hate or nag on your code at all, it is well written and works wonderfully, it is not exactly performance friendly. I incorporated it into my code to make a custom super button that has all kinds of features, and when I had too many of the button on screen, the whole kivy app started to lag. I tracked this down to the hover method. I determined that it's because of binding to the Window pos, that if you, say have a panel full of a hundred buttons, will simultaneously be calling on_mouse_pos a hundred times, which was causing a ton of lag, especially when I tried to move the mouse. So instead of binding to the Window on_pos, i created a custom inherited class of each widget type (Widget, Image, ScreenManager, Screen) with a method calling similar to on_touch_down, in that it will bubble down the list of widgets until one returns True. This greatly increased my performance when having many hover widgets on the screen at a time. Just a friendly suggestion for anybody that has similar problems.
—
You are receiving this because you authored the thread.
Reply to this email directly, view it on GitHub<https://gist.github.com/15c707dc4cffc2b6455f?email_source=notifications&email_token=AAJWAOTZJZSY3YDXJED32LDQ7N733A5CNFSM4KLNYRV2YY3PNVWWK3TUL52HS4DFVNDWS43UINXW23LFNZ2KUY3PNVWWK3TUL5UWJTQAGAOVS#gistcomment-3153241>, or unsubscribe<https://github.com/notifications/unsubscribe-auth/AAJWAORSTWN6STSF6UCKCRDQ7N733ANCNFSM4KLNYRVQ>.
Are you the author of this source code? GPL licenses are burdensome to use as open source. Do you have any plans to change the license type to a relaxed one such as MIT or BSD?
OK to change to bsd
Téléchargez Outlook pour Android<https://aka.ms/ghei36>
…________________________________
From: MKdays <[email protected]>
Sent: Monday, January 25, 2021 11:08:08 AM
To: opqopq <[email protected]>
Cc: opqopq <[email protected]>; Author <[email protected]>
Subject: Re: opqopq/hoverable.py
@MKdays commented on this gist.
________________________________
Are you the author of this source code? GPL licenses are burdensome to use as open source. Do you have any plans to change the license type to a relaxed one such as MIT or BSD?
—
You are receiving this because you authored the thread.
Reply to this email directly, view it on GitHub<https://gist.github.com/15c707dc4cffc2b6455f#gistcomment-3606340>, or unsubscribe<https://github.com/notifications/unsubscribe-auth/AAJWAOS4FSKU3CQZTZPAGHTS3U7IRANCNFSM4KLNYRVQ>.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Can you give an example?