Mercurial > libervia-desktop-kivy
annotate src/cagou/core/widgets_handler.py @ 31:4f9e701d76b4
core: expand now accepts extra arguments, which will be used in format
author | Goffi <goffi@goffi.org> |
---|---|
date | Sun, 21 Aug 2016 17:47:23 +0200 |
parents | ba14b596b90e |
children | 02acbb297a61 |
rev | line source |
---|---|
13 | 1 #!/usr/bin/python |
2 # -*- coding: utf-8 -*- | |
3 | |
4 # Cagou: desktop/mobile frontend for Salut à Toi XMPP client | |
5 # Copyright (C) 2016 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 | |
21 from sat.core import log as logging | |
22 log = logging.getLogger(__name__) | |
23 from kivy.uix.boxlayout import BoxLayout | |
24 from kivy.uix.button import Button | |
25 from kivy import properties | |
16
ba14b596b90e
host can now be get as a global value:
Goffi <goffi@goffi.org>
parents:
15
diff
changeset
|
26 from cagou import G |
13 | 27 |
28 | |
29 NEW_WIDGET_DIST = 10 | |
30 REMOVE_WIDGET_DIST = NEW_WIDGET_DIST | |
31 | |
32 | |
33 class WHSplitter(Button): | |
34 horizontal=properties.BooleanProperty(True) | |
35 thickness=properties.NumericProperty(15) | |
36 split_move = None # we handle one split at a time, so we use a class attribute | |
37 | |
38 def __init__(self, handler, **kwargs): | |
39 super(WHSplitter, self).__init__(**kwargs) | |
40 self.handler = handler | |
41 | |
42 def getPos(self, touch): | |
43 if self.horizontal: | |
44 relative_y = self.handler.to_local(*touch.pos, relative=True)[1] | |
45 return self.handler.height - relative_y | |
46 else: | |
47 return touch.x | |
48 | |
49 def on_touch_move(self, touch): | |
50 if self.split_move is None and self.collide_point(*touch.opos): | |
51 WHSplitter.split_move = self | |
52 | |
53 if self.split_move is self: | |
54 pos = self.getPos(touch) | |
55 if pos > NEW_WIDGET_DIST: | |
56 # we are above minimal distance, we resize the widget | |
57 self.handler.setWidgetSize(self.horizontal, pos) | |
58 | |
59 def on_touch_up(self, touch): | |
60 if self.split_move is self: | |
61 pos = self.getPos(touch) | |
62 if pos <= REMOVE_WIDGET_DIST: | |
63 # if we go under minimal distance, the widget is not wanted anymore | |
64 self.handler.removeWidget(self.horizontal) | |
65 WHSplitter.split_move=None | |
66 return super(WHSplitter, self).on_touch_up(touch) | |
67 | |
68 | |
69 class WidgetsHandler(BoxLayout): | |
70 | |
16
ba14b596b90e
host can now be get as a global value:
Goffi <goffi@goffi.org>
parents:
15
diff
changeset
|
71 def __init__(self, wid=None, **kw): |
14 | 72 if wid is None: |
73 wid=self.default_widget | |
13 | 74 self.vert_wid = self.hor_wid = None |
75 BoxLayout.__init__(self, orientation="vertical", **kw) | |
76 self.blh = BoxLayout(orientation="horizontal") | |
77 self.blv = BoxLayout(orientation="vertical") | |
78 self.blv.add_widget(WHSplitter(self)) | |
79 self.blv.add_widget(wid) | |
80 self.blh.add_widget(WHSplitter(self, horizontal=False)) | |
81 self.blh.add_widget(self.blv) | |
82 self.add_widget(self.blh) | |
83 | |
14 | 84 @property |
85 def default_widget(self): | |
16
ba14b596b90e
host can now be get as a global value:
Goffi <goffi@goffi.org>
parents:
15
diff
changeset
|
86 return G.host.default_wid['factory'](G.host.default_wid, None, None) |
14 | 87 |
13 | 88 def removeWidget(self, vertical): |
89 if vertical and self.vert_wid is not None: | |
90 self.remove_widget(self.vert_wid) | |
91 self.vert_wid = None | |
92 elif self.hor_wid is not None: | |
93 self.blh.remove_widget(self.hor_wid) | |
94 self.hor_wid = None | |
95 | |
96 def setWidgetSize(self, vertical, size): | |
97 if vertical: | |
98 if self.vert_wid is None: | |
16
ba14b596b90e
host can now be get as a global value:
Goffi <goffi@goffi.org>
parents:
15
diff
changeset
|
99 self.vert_wid = WidgetsHandler(self.default_widget, size_hint=(1, None)) |
13 | 100 self.add_widget(self.vert_wid, len(self.children)) |
101 self.vert_wid.height=size | |
102 else: | |
103 if self.hor_wid is None: | |
16
ba14b596b90e
host can now be get as a global value:
Goffi <goffi@goffi.org>
parents:
15
diff
changeset
|
104 self.hor_wid = WidgetsHandler(self.default_widget, size_hint=(None, 1)) |
13 | 105 self.blh.add_widget(self.hor_wid, len(self.blh.children)) |
106 self.hor_wid.width=size |