comparison cagou/core/utils.py @ 196:519b3a29743c

utils, plugin file sharing: new utils module, with a FilterBehavior: FilterBehavior do a smooth animation for filtering out children of a layout according to a text content.
author Goffi <goffi@goffi.org>
date Wed, 23 May 2018 21:25:08 +0200
parents
children 9faccd140119
comparison
equal deleted inserted replaced
195:cf30cb1d1c12 196:519b3a29743c
1 #!/usr//bin/env python2
2 # -*- coding: utf-8 -*-
3
4 # Cagou: desktop/mobile frontend for Salut à Toi XMPP client
5 # Copyright (C) 2016-2018 Jérôme Poisson (goffi@goffi.org)
6
7 # This program is free software: you can redistribute it and/or modify
8 # it under the terms of the GNU Affero General Public License as published by
9 # the Free Software Foundation, either version 3 of the License, or
10 # (at your option) any later version.
11
12 # This program is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU Affero General Public License for more details.
16
17 # You should have received a copy of the GNU Affero General Public License
18 # along with this program. If not, see <http://www.gnu.org/licenses/>.
19
20 """misc utils/behaviors"""
21
22
23 from kivy.animation import Animation
24
25
26 class FilterBehavior(object):
27 """class to handle items filtering with animation"""
28
29 def __init__(self, *args, **kwargs):
30 super(FilterBehavior, self).__init__(*args, **kwargs)
31 self._filter_last = u''
32 self._filter_anim = Animation(width = 0,
33 height = 0,
34 opacity = 0,
35 d = 0.5)
36
37 def do_filter(self, children, text, get_child_text, width_cb, height_cb, continue_tests=None):
38 """filter the children
39
40 filtered children will have a animation to set width, height and opacity to 0
41 @param children(kivy.uix.widget.Widget): widgets to filter
42 @param text(unicode): filter text (if this text is not present in a child,
43 the child is filtered out)
44 @param get_child_text(callable): must retrieve child text
45 child is used as sole argument
46 @param width_cb(callable, int, None): method to retrieve width when opened
47 child is used as sole argument, int can be used instead of callable
48 @param height_cb(callable, int, None): method to retrieve height when opened
49 child is used as sole argument, int can be used instead of callable
50 @param continue_tests(list[callable]): list of test to skip the item
51 all callables take child as sole argument.
52 if any of the callable return True, the child is skipped (i.e. not filtered)
53 """
54 text = text.strip().lower()
55 filtering = len(text)>len(self._filter_last)
56 self._filter_last = text
57 for child in self.layout.children:
58 if continue_tests is not None and any([t(child) for t in continue_tests]):
59 continue
60 if text in get_child_text(child).lower():
61 self._filter_anim.cancel(child)
62 for key, method in (('width', width_cb),
63 ('height', height_cb),
64 ('opacity', lambda c: 1)):
65 try:
66 setattr(child, key, method(child))
67 except TypeError:
68 # method is not a callable, must be an int
69 setattr(child, key, method)
70 elif (filtering
71 and child.opacity > 0
72 and not self._filter_anim.have_properties_to_animate(child)):
73 self._filter_anim.start(child)