Mercurial > libervia-desktop-kivy
annotate cagou/core/common_widgets.py @ 325:5868a5575e01
chat: cleaning + some improvments:
- code cleaning, removed some dead code
- some improvments on the way size is calculated, removed unnecessary sizing methods which
were linked to properties
- image have now a max size, this avoid gigantic image in the whole screen
- in SimpleXHTMLWidget, Label are now splitted when xhtml is set
- use a DelayedBoxLayout for messages, as they are really slow to be resized
- use of RecycleView has been investigated, but it is not currently usable as dynamic
contents are not propertly handled (see https://github.com/kivy/kivy/issues/6580 and
https://github.com/kivy/kivy/issues/6582). Furthermore, some tests with RecycleView on
Android don't give the expected speed boost, so BoxLayout still seems like the way to go
for the moment. To be re-investigated at a later point if necessary.
author | Goffi <goffi@goffi.org> |
---|---|
date | Fri, 06 Dec 2019 13:25:31 +0100 |
parents | 4374cb741eb5 |
children | 51520ce98154 |
rev | line source |
---|---|
237
059c5b39032d
plugin file sharing: moved common discovery widgets to new core.common_widgets module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
1 #!/usr/bin/env python2 |
059c5b39032d
plugin file sharing: moved common discovery widgets to new core.common_widgets module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
2 # -*- coding: utf-8 -*- |
059c5b39032d
plugin file sharing: moved common discovery widgets to new core.common_widgets module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
3 |
059c5b39032d
plugin file sharing: moved common discovery widgets to new core.common_widgets module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
4 # Cagou: desktop/mobile frontend for Salut à Toi XMPP client |
282 | 5 # Copyright (C) 2016-2019 Jérôme Poisson (goffi@goffi.org) |
237
059c5b39032d
plugin file sharing: moved common discovery widgets to new core.common_widgets module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
6 |
059c5b39032d
plugin file sharing: moved common discovery widgets to new core.common_widgets module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
7 # This program is free software: you can redistribute it and/or modify |
059c5b39032d
plugin file sharing: moved common discovery widgets to new core.common_widgets module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
8 # it under the terms of the GNU Affero General Public License as published by |
059c5b39032d
plugin file sharing: moved common discovery widgets to new core.common_widgets module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
9 # the Free Software Foundation, either version 3 of the License, or |
059c5b39032d
plugin file sharing: moved common discovery widgets to new core.common_widgets module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
10 # (at your option) any later version. |
059c5b39032d
plugin file sharing: moved common discovery widgets to new core.common_widgets module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
11 |
059c5b39032d
plugin file sharing: moved common discovery widgets to new core.common_widgets module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
12 # This program is distributed in the hope that it will be useful, |
059c5b39032d
plugin file sharing: moved common discovery widgets to new core.common_widgets module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of |
059c5b39032d
plugin file sharing: moved common discovery widgets to new core.common_widgets module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
059c5b39032d
plugin file sharing: moved common discovery widgets to new core.common_widgets module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
15 # GNU Affero General Public License for more details. |
059c5b39032d
plugin file sharing: moved common discovery widgets to new core.common_widgets module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
16 |
059c5b39032d
plugin file sharing: moved common discovery widgets to new core.common_widgets module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
17 # You should have received a copy of the GNU Affero General Public License |
059c5b39032d
plugin file sharing: moved common discovery widgets to new core.common_widgets module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
18 # along with this program. If not, see <http://www.gnu.org/licenses/>. |
059c5b39032d
plugin file sharing: moved common discovery widgets to new core.common_widgets module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
19 |
059c5b39032d
plugin file sharing: moved common discovery widgets to new core.common_widgets module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
20 """common advanced widgets, which can be reused everywhere.""" |
059c5b39032d
plugin file sharing: moved common discovery widgets to new core.common_widgets module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
21 |
059c5b39032d
plugin file sharing: moved common discovery widgets to new core.common_widgets module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
22 from sat.core.i18n import _ |
059c5b39032d
plugin file sharing: moved common discovery widgets to new core.common_widgets module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
23 from kivy.uix.label import Label |
059c5b39032d
plugin file sharing: moved common discovery widgets to new core.common_widgets module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
24 from kivy.uix.boxlayout import BoxLayout |
059c5b39032d
plugin file sharing: moved common discovery widgets to new core.common_widgets module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
25 from cagou.core.menu import TouchMenuItemBehaviour |
059c5b39032d
plugin file sharing: moved common discovery widgets to new core.common_widgets module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
26 from kivy import properties |
059c5b39032d
plugin file sharing: moved common discovery widgets to new core.common_widgets module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
27 from kivy.metrics import dp |
324
4374cb741eb5
core (common widgets): added a DelayedBoxLayout class:
Goffi <goffi@goffi.org>
parents:
312
diff
changeset
|
28 from kivy.clock import Clock |
237
059c5b39032d
plugin file sharing: moved common discovery widgets to new core.common_widgets module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
29 from cagou import G |
059c5b39032d
plugin file sharing: moved common discovery widgets to new core.common_widgets module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
30 from sat.core import log as logging |
059c5b39032d
plugin file sharing: moved common discovery widgets to new core.common_widgets module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
31 |
059c5b39032d
plugin file sharing: moved common discovery widgets to new core.common_widgets module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
32 log = logging.getLogger(__name__) |
059c5b39032d
plugin file sharing: moved common discovery widgets to new core.common_widgets module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
33 |
059c5b39032d
plugin file sharing: moved common discovery widgets to new core.common_widgets module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
34 |
324
4374cb741eb5
core (common widgets): added a DelayedBoxLayout class:
Goffi <goffi@goffi.org>
parents:
312
diff
changeset
|
35 class DelayedBoxLayout(BoxLayout): |
4374cb741eb5
core (common widgets): added a DelayedBoxLayout class:
Goffi <goffi@goffi.org>
parents:
312
diff
changeset
|
36 """A BoxLayout with delayed layout, to avoid slowing down during resize""" |
4374cb741eb5
core (common widgets): added a DelayedBoxLayout class:
Goffi <goffi@goffi.org>
parents:
312
diff
changeset
|
37 # XXX: thanks to Alexander Taylor for his blog post at |
4374cb741eb5
core (common widgets): added a DelayedBoxLayout class:
Goffi <goffi@goffi.org>
parents:
312
diff
changeset
|
38 # https://blog.kivy.org/2019/07/a-delayed-resize-layout-in-kivy/ |
4374cb741eb5
core (common widgets): added a DelayedBoxLayout class:
Goffi <goffi@goffi.org>
parents:
312
diff
changeset
|
39 |
4374cb741eb5
core (common widgets): added a DelayedBoxLayout class:
Goffi <goffi@goffi.org>
parents:
312
diff
changeset
|
40 do_layout_event = properties.ObjectProperty(None, allownone=True) |
4374cb741eb5
core (common widgets): added a DelayedBoxLayout class:
Goffi <goffi@goffi.org>
parents:
312
diff
changeset
|
41 layout_delay_s = properties.NumericProperty(0.2) |
4374cb741eb5
core (common widgets): added a DelayedBoxLayout class:
Goffi <goffi@goffi.org>
parents:
312
diff
changeset
|
42 |
4374cb741eb5
core (common widgets): added a DelayedBoxLayout class:
Goffi <goffi@goffi.org>
parents:
312
diff
changeset
|
43 def do_layout(self, *args, **kwargs): |
4374cb741eb5
core (common widgets): added a DelayedBoxLayout class:
Goffi <goffi@goffi.org>
parents:
312
diff
changeset
|
44 if self.do_layout_event is not None: |
4374cb741eb5
core (common widgets): added a DelayedBoxLayout class:
Goffi <goffi@goffi.org>
parents:
312
diff
changeset
|
45 self.do_layout_event.cancel() |
4374cb741eb5
core (common widgets): added a DelayedBoxLayout class:
Goffi <goffi@goffi.org>
parents:
312
diff
changeset
|
46 real_do_layout = super().do_layout |
4374cb741eb5
core (common widgets): added a DelayedBoxLayout class:
Goffi <goffi@goffi.org>
parents:
312
diff
changeset
|
47 self.do_layout_event = Clock.schedule_once( |
4374cb741eb5
core (common widgets): added a DelayedBoxLayout class:
Goffi <goffi@goffi.org>
parents:
312
diff
changeset
|
48 lambda dt: real_do_layout(*args, **kwargs), |
4374cb741eb5
core (common widgets): added a DelayedBoxLayout class:
Goffi <goffi@goffi.org>
parents:
312
diff
changeset
|
49 self.layout_delay_s) |
4374cb741eb5
core (common widgets): added a DelayedBoxLayout class:
Goffi <goffi@goffi.org>
parents:
312
diff
changeset
|
50 |
4374cb741eb5
core (common widgets): added a DelayedBoxLayout class:
Goffi <goffi@goffi.org>
parents:
312
diff
changeset
|
51 |
237
059c5b39032d
plugin file sharing: moved common discovery widgets to new core.common_widgets module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
52 class Identities(object): |
059c5b39032d
plugin file sharing: moved common discovery widgets to new core.common_widgets module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
53 |
059c5b39032d
plugin file sharing: moved common discovery widgets to new core.common_widgets module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
54 def __init__(self, entity_ids): |
059c5b39032d
plugin file sharing: moved common discovery widgets to new core.common_widgets module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
55 identities = {} |
059c5b39032d
plugin file sharing: moved common discovery widgets to new core.common_widgets module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
56 for cat, type_, name in entity_ids: |
059c5b39032d
plugin file sharing: moved common discovery widgets to new core.common_widgets module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
57 identities.setdefault(cat, {}).setdefault(type_, []).append(name) |
059c5b39032d
plugin file sharing: moved common discovery widgets to new core.common_widgets module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
58 client = identities.get('client', {}) |
059c5b39032d
plugin file sharing: moved common discovery widgets to new core.common_widgets module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
59 if 'pc' in client: |
059c5b39032d
plugin file sharing: moved common discovery widgets to new core.common_widgets module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
60 self.type = 'desktop' |
059c5b39032d
plugin file sharing: moved common discovery widgets to new core.common_widgets module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
61 elif 'phone' in client: |
059c5b39032d
plugin file sharing: moved common discovery widgets to new core.common_widgets module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
62 self.type = 'phone' |
059c5b39032d
plugin file sharing: moved common discovery widgets to new core.common_widgets module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
63 elif 'web' in client: |
059c5b39032d
plugin file sharing: moved common discovery widgets to new core.common_widgets module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
64 self.type = 'web' |
059c5b39032d
plugin file sharing: moved common discovery widgets to new core.common_widgets module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
65 elif 'console' in client: |
059c5b39032d
plugin file sharing: moved common discovery widgets to new core.common_widgets module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
66 self.type = 'console' |
059c5b39032d
plugin file sharing: moved common discovery widgets to new core.common_widgets module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
67 else: |
059c5b39032d
plugin file sharing: moved common discovery widgets to new core.common_widgets module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
68 self.type = 'desktop' |
059c5b39032d
plugin file sharing: moved common discovery widgets to new core.common_widgets module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
69 |
059c5b39032d
plugin file sharing: moved common discovery widgets to new core.common_widgets module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
70 self.identities = identities |
059c5b39032d
plugin file sharing: moved common discovery widgets to new core.common_widgets module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
71 |
059c5b39032d
plugin file sharing: moved common discovery widgets to new core.common_widgets module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
72 @property |
059c5b39032d
plugin file sharing: moved common discovery widgets to new core.common_widgets module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
73 def name(self): |
312 | 74 return list(self.identities.values())[0].values()[0][0] |
237
059c5b39032d
plugin file sharing: moved common discovery widgets to new core.common_widgets module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
75 |
059c5b39032d
plugin file sharing: moved common discovery widgets to new core.common_widgets module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
76 |
059c5b39032d
plugin file sharing: moved common discovery widgets to new core.common_widgets module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
77 class ItemWidget(TouchMenuItemBehaviour, BoxLayout): |
059c5b39032d
plugin file sharing: moved common discovery widgets to new core.common_widgets module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
78 name = properties.StringProperty() |
059c5b39032d
plugin file sharing: moved common discovery widgets to new core.common_widgets module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
79 base_width = properties.NumericProperty(dp(100)) |
059c5b39032d
plugin file sharing: moved common discovery widgets to new core.common_widgets module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
80 |
059c5b39032d
plugin file sharing: moved common discovery widgets to new core.common_widgets module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
81 |
059c5b39032d
plugin file sharing: moved common discovery widgets to new core.common_widgets module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
82 class DeviceWidget(ItemWidget): |
059c5b39032d
plugin file sharing: moved common discovery widgets to new core.common_widgets module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
83 |
059c5b39032d
plugin file sharing: moved common discovery widgets to new core.common_widgets module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
84 def __init__(self, main_wid, entity_jid, identities, **kw): |
059c5b39032d
plugin file sharing: moved common discovery widgets to new core.common_widgets module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
85 self.entity_jid = entity_jid |
059c5b39032d
plugin file sharing: moved common discovery widgets to new core.common_widgets module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
86 self.identities = identities |
312 | 87 own_jid = next(iter(G.host.profiles.values())).whoami |
237
059c5b39032d
plugin file sharing: moved common discovery widgets to new core.common_widgets module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
88 self.own_device = entity_jid.bare == own_jid |
059c5b39032d
plugin file sharing: moved common discovery widgets to new core.common_widgets module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
89 if self.own_device: |
059c5b39032d
plugin file sharing: moved common discovery widgets to new core.common_widgets module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
90 name = self.identities.name |
059c5b39032d
plugin file sharing: moved common discovery widgets to new core.common_widgets module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
91 elif self.entity_jid.node: |
059c5b39032d
plugin file sharing: moved common discovery widgets to new core.common_widgets module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
92 name = self.entity_jid.node |
059c5b39032d
plugin file sharing: moved common discovery widgets to new core.common_widgets module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
93 elif self.entity_jid == own_jid.domain: |
312 | 94 name = _("your server") |
237
059c5b39032d
plugin file sharing: moved common discovery widgets to new core.common_widgets module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
95 else: |
059c5b39032d
plugin file sharing: moved common discovery widgets to new core.common_widgets module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
96 name = entity_jid |
059c5b39032d
plugin file sharing: moved common discovery widgets to new core.common_widgets module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
97 |
059c5b39032d
plugin file sharing: moved common discovery widgets to new core.common_widgets module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
98 super(DeviceWidget, self).__init__(name=name, main_wid=main_wid, **kw) |
059c5b39032d
plugin file sharing: moved common discovery widgets to new core.common_widgets module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
99 |
059c5b39032d
plugin file sharing: moved common discovery widgets to new core.common_widgets module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
100 @property |
059c5b39032d
plugin file sharing: moved common discovery widgets to new core.common_widgets module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
101 def profile(self): |
059c5b39032d
plugin file sharing: moved common discovery widgets to new core.common_widgets module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
102 return self.main_wid.profile |
059c5b39032d
plugin file sharing: moved common discovery widgets to new core.common_widgets module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
103 |
059c5b39032d
plugin file sharing: moved common discovery widgets to new core.common_widgets module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
104 def getSymbol(self): |
059c5b39032d
plugin file sharing: moved common discovery widgets to new core.common_widgets module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
105 if self.identities.type == 'desktop': |
059c5b39032d
plugin file sharing: moved common discovery widgets to new core.common_widgets module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
106 return 'desktop' |
059c5b39032d
plugin file sharing: moved common discovery widgets to new core.common_widgets module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
107 elif self.identities.type == 'phone': |
059c5b39032d
plugin file sharing: moved common discovery widgets to new core.common_widgets module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
108 return 'mobile' |
059c5b39032d
plugin file sharing: moved common discovery widgets to new core.common_widgets module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
109 elif self.identities.type == 'web': |
059c5b39032d
plugin file sharing: moved common discovery widgets to new core.common_widgets module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
110 return 'globe' |
059c5b39032d
plugin file sharing: moved common discovery widgets to new core.common_widgets module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
111 elif self.identities.type == 'console': |
059c5b39032d
plugin file sharing: moved common discovery widgets to new core.common_widgets module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
112 return 'terminal' |
059c5b39032d
plugin file sharing: moved common discovery widgets to new core.common_widgets module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
113 else: |
059c5b39032d
plugin file sharing: moved common discovery widgets to new core.common_widgets module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
114 return 'desktop' |
059c5b39032d
plugin file sharing: moved common discovery widgets to new core.common_widgets module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
115 |
059c5b39032d
plugin file sharing: moved common discovery widgets to new core.common_widgets module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
116 def do_item_action(self, touch): |
059c5b39032d
plugin file sharing: moved common discovery widgets to new core.common_widgets module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
117 pass |
059c5b39032d
plugin file sharing: moved common discovery widgets to new core.common_widgets module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
118 |
059c5b39032d
plugin file sharing: moved common discovery widgets to new core.common_widgets module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
119 |
059c5b39032d
plugin file sharing: moved common discovery widgets to new core.common_widgets module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
120 class CategorySeparator(Label): |
059c5b39032d
plugin file sharing: moved common discovery widgets to new core.common_widgets module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
121 pass |