Mercurial > libervia-desktop-kivy
annotate src/cagou/plugins/plugin_wid_chat.py @ 45:b0595a33465d
chat: design improvments:
- all message are now on the same side, no more left/right which can be difficult to read on big screen
- use of GridLayout for MessageWidget and MessagesWidget, as the minimum_height attribute simplify size handling. This attribute will be present in BoxLayout too in Kivy 1.9.2, so we may switch back in the future
- info messages are displayed in yellow with a black background
- nick and date are now displayed in the same label
- avatar, nick and date are on the same line just above the message bubble
- padding/spacing adjustments
author | Goffi <goffi@goffi.org> |
---|---|
date | Sun, 04 Sep 2016 19:38:21 +0200 |
parents | 7819e9efa250 |
children | d6a63942d5ad |
rev | line source |
---|---|
22 | 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 sat.core.i18n import _ | |
24 from cagou.core.constants import Const as C | |
45 | 25 from kivy.uix.gridlayout import GridLayout |
22 | 26 from kivy.uix.scrollview import ScrollView |
27 from kivy.uix.textinput import TextInput | |
45 | 28 from kivy.metrics import dp |
22 | 29 from kivy import properties |
30 from sat_frontends.quick_frontend import quick_widgets | |
31 from sat_frontends.quick_frontend import quick_chat | |
32 from sat_frontends.tools import jid | |
33 from cagou.core import cagou_widget | |
44
7819e9efa250
chat: avatar and nick are now displayed, need further aesthetic improvments
Goffi <goffi@goffi.org>
parents:
42
diff
changeset
|
34 from cagou.core.image import Image |
22 | 35 from cagou import G |
36 | |
37 | |
38 PLUGIN_INFO = { | |
39 "name": _(u"chat"), | |
40 "main": "Chat", | |
41 "description": _(u"instant messaging with one person or a group"), | |
25
d09bd16dbbe2
code (cagou widget), selector: icons handling + use of new muchoslava icon set
Goffi <goffi@goffi.org>
parents:
24
diff
changeset
|
42 "icon_small": u"{media}/icons/muchoslava/png/chat_rouge_32.png", |
d09bd16dbbe2
code (cagou widget), selector: icons handling + use of new muchoslava icon set
Goffi <goffi@goffi.org>
parents:
24
diff
changeset
|
43 "icon_medium": u"{media}/icons/muchoslava/png/chat_rouge_44.png" |
22 | 44 } |
45 | |
46 | |
44
7819e9efa250
chat: avatar and nick are now displayed, need further aesthetic improvments
Goffi <goffi@goffi.org>
parents:
42
diff
changeset
|
47 class MessAvatar(Image): |
7819e9efa250
chat: avatar and nick are now displayed, need further aesthetic improvments
Goffi <goffi@goffi.org>
parents:
42
diff
changeset
|
48 pass |
7819e9efa250
chat: avatar and nick are now displayed, need further aesthetic improvments
Goffi <goffi@goffi.org>
parents:
42
diff
changeset
|
49 |
7819e9efa250
chat: avatar and nick are now displayed, need further aesthetic improvments
Goffi <goffi@goffi.org>
parents:
42
diff
changeset
|
50 |
45 | 51 class MessageWidget(GridLayout): |
22 | 52 mess_data = properties.ObjectProperty() |
44
7819e9efa250
chat: avatar and nick are now displayed, need further aesthetic improvments
Goffi <goffi@goffi.org>
parents:
42
diff
changeset
|
53 mess_label = properties.ObjectProperty() |
45 | 54 mess_padding = (dp(5), dp(5)) |
44
7819e9efa250
chat: avatar and nick are now displayed, need further aesthetic improvments
Goffi <goffi@goffi.org>
parents:
42
diff
changeset
|
55 |
7819e9efa250
chat: avatar and nick are now displayed, need further aesthetic improvments
Goffi <goffi@goffi.org>
parents:
42
diff
changeset
|
56 @property |
7819e9efa250
chat: avatar and nick are now displayed, need further aesthetic improvments
Goffi <goffi@goffi.org>
parents:
42
diff
changeset
|
57 def chat(self): |
7819e9efa250
chat: avatar and nick are now displayed, need further aesthetic improvments
Goffi <goffi@goffi.org>
parents:
42
diff
changeset
|
58 """return parent Chat instance""" |
7819e9efa250
chat: avatar and nick are now displayed, need further aesthetic improvments
Goffi <goffi@goffi.org>
parents:
42
diff
changeset
|
59 return self.mess_data.parent |
22 | 60 |
61 @property | |
62 def message(self): | |
63 """Return currently displayed message""" | |
64 return self.mess_data.main_message | |
65 | |
45 | 66 def widthAdjust(self): |
22 | 67 """this widget grows up with its children""" |
45 | 68 parent = self.mess_label.parent |
69 padding_x = self.mess_padding[0] | |
24
bc15b55a4114
chat: better bubble and time resizing
Goffi <goffi@goffi.org>
parents:
22
diff
changeset
|
70 text_width, text_height = self.mess_label.texture_size |
45 | 71 if text_width > parent.width: |
72 self.mess_label.text_size = (parent.width - padding_x, None) | |
24
bc15b55a4114
chat: better bubble and time resizing
Goffi <goffi@goffi.org>
parents:
22
diff
changeset
|
73 self.text_max = text_width |
45 | 74 elif self.mess_label.text_size[0] is not None and text_width < parent.width - padding_x: |
75 if text_width < self.text_max: | |
24
bc15b55a4114
chat: better bubble and time resizing
Goffi <goffi@goffi.org>
parents:
22
diff
changeset
|
76 self.mess_label.text_size = (None, None) |
bc15b55a4114
chat: better bubble and time resizing
Goffi <goffi@goffi.org>
parents:
22
diff
changeset
|
77 else: |
45 | 78 self.mess_label.text_size = (parent.width - padding_x, None) |
22 | 79 |
80 | |
81 class MessageInputWidget(TextInput): | |
82 | |
83 def _key_down(self, key, repeat=False): | |
84 displayed_str, internal_str, internal_action, scale = key | |
85 if internal_action == 'enter': | |
86 self.dispatch('on_text_validate') | |
87 else: | |
88 super(MessageInputWidget, self)._key_down(key, repeat) | |
89 | |
90 | |
45 | 91 class MessagesWidget(GridLayout): |
92 pass | |
22 | 93 |
94 | |
95 class Chat(quick_chat.QuickChat, cagou_widget.CagouWidget): | |
96 | |
97 def __init__(self, host, target, type_=C.CHAT_ONE2ONE, occupants=None, subject=None, profiles=None): | |
98 quick_chat.QuickChat.__init__(self, host, target, type_, occupants, subject, profiles=profiles) | |
99 cagou_widget.CagouWidget.__init__(self) | |
100 self.header_input.hint_text = u"You are talking with {}".format(target) | |
37
6cf08d0ee460
chat: forbid scrolling on X axis + don't delete widget until explicitly requested (with force attribute)
Goffi <goffi@goffi.org>
parents:
35
diff
changeset
|
101 scroll_view = ScrollView(size_hint=(1,0.8), scroll_y=0, do_scroll_x=False) |
22 | 102 self.messages_widget = MessagesWidget() |
103 scroll_view.add_widget(self.messages_widget) | |
104 self.add_widget(scroll_view) | |
105 message_input = MessageInputWidget() | |
106 message_input.bind(on_text_validate=self.onSend) | |
107 self.add_widget(message_input) | |
108 self.postInit() | |
109 | |
110 @classmethod | |
111 def factory(cls, plugin_info, target, profiles): | |
112 profiles = list(profiles) | |
113 if len(profiles) > 1: | |
114 raise NotImplementedError(u"Multi-profiles is not available yet for chat") | |
115 if target is None: | |
116 target = G.host.profiles[profiles[0]].whoami | |
117 return G.host.widgets.getOrCreateWidget(cls, target, on_new_widget=None, on_existing_widget=C.WIDGET_RECREATE, profiles=profiles) | |
118 | |
119 def messageDataConverter(self, idx, mess_id): | |
120 return {"mess_data": self.messages[mess_id]} | |
121 | |
122 def _onHistoryPrinted(self): | |
123 """Refresh or scroll down the focus after the history is printed""" | |
124 # self.adapter.data = self.messages | |
125 for mess_data in self.messages.itervalues(): | |
126 self.appendMessage(mess_data) | |
127 super(Chat, self)._onHistoryPrinted() | |
128 | |
129 def createMessage(self, message): | |
130 self.appendMessage(message) | |
131 | |
132 def appendMessage(self, mess_data): | |
133 self.messages_widget.add_widget(MessageWidget(mess_data=mess_data)) | |
134 | |
135 def onSend(self, input_widget): | |
136 G.host.messageSend( | |
137 self.target, | |
138 {'': input_widget.text}, # TODO: handle language | |
139 mess_type = C.MESS_TYPE_GROUPCHAT if self.type == C.CHAT_GROUP else C.MESS_TYPE_CHAT, # TODO: put this in QuickChat | |
140 profile_key=self.profile | |
141 ) | |
142 input_widget.text = '' | |
143 | |
42
286865bc013a
chat: joining MUC using header input is now working:
Goffi <goffi@goffi.org>
parents:
41
diff
changeset
|
144 def _mucJoinCb(self, bare): |
286865bc013a
chat: joining MUC using header input is now working:
Goffi <goffi@goffi.org>
parents:
41
diff
changeset
|
145 jid_ = jid.JID(bare) |
286865bc013a
chat: joining MUC using header input is now working:
Goffi <goffi@goffi.org>
parents:
41
diff
changeset
|
146 self.changeWidget(jid_) |
286865bc013a
chat: joining MUC using header input is now working:
Goffi <goffi@goffi.org>
parents:
41
diff
changeset
|
147 |
286865bc013a
chat: joining MUC using header input is now working:
Goffi <goffi@goffi.org>
parents:
41
diff
changeset
|
148 def _mucJoinEb(self, failure): |
286865bc013a
chat: joining MUC using header input is now working:
Goffi <goffi@goffi.org>
parents:
41
diff
changeset
|
149 log.warning(u"Can't join room: {}".format(failure)) |
286865bc013a
chat: joining MUC using header input is now working:
Goffi <goffi@goffi.org>
parents:
41
diff
changeset
|
150 |
286865bc013a
chat: joining MUC using header input is now working:
Goffi <goffi@goffi.org>
parents:
41
diff
changeset
|
151 def changeWidget(self, jid_): |
286865bc013a
chat: joining MUC using header input is now working:
Goffi <goffi@goffi.org>
parents:
41
diff
changeset
|
152 """change current widget for a new one with given jid |
286865bc013a
chat: joining MUC using header input is now working:
Goffi <goffi@goffi.org>
parents:
41
diff
changeset
|
153 |
286865bc013a
chat: joining MUC using header input is now working:
Goffi <goffi@goffi.org>
parents:
41
diff
changeset
|
154 @param jid_(jid.JID): jid of the widget to create |
286865bc013a
chat: joining MUC using header input is now working:
Goffi <goffi@goffi.org>
parents:
41
diff
changeset
|
155 """ |
286865bc013a
chat: joining MUC using header input is now working:
Goffi <goffi@goffi.org>
parents:
41
diff
changeset
|
156 plugin_info = G.host.getPluginInfo(main=Chat) |
286865bc013a
chat: joining MUC using header input is now working:
Goffi <goffi@goffi.org>
parents:
41
diff
changeset
|
157 factory = plugin_info['factory'] |
286865bc013a
chat: joining MUC using header input is now working:
Goffi <goffi@goffi.org>
parents:
41
diff
changeset
|
158 G.host.switchWidget(self, factory(plugin_info, jid_, profiles=[self.profile])) |
286865bc013a
chat: joining MUC using header input is now working:
Goffi <goffi@goffi.org>
parents:
41
diff
changeset
|
159 self.header_input.text = '' |
286865bc013a
chat: joining MUC using header input is now working:
Goffi <goffi@goffi.org>
parents:
41
diff
changeset
|
160 |
22 | 161 def onHeaderInput(self): |
162 text = self.header_input.text.strip() | |
163 try: | |
164 if text.count(u'@') != 1 or text.count(u' '): | |
165 raise ValueError | |
166 jid_ = jid.JID(text) | |
167 except ValueError: | |
168 log.info(u"entered text is not a jid") | |
169 return | |
170 | |
171 def discoCb(disco): | |
172 # TODO: check if plugin XEP-0045 is activated | |
173 if "conference" in [i[0] for i in disco[1]]: | |
42
286865bc013a
chat: joining MUC using header input is now working:
Goffi <goffi@goffi.org>
parents:
41
diff
changeset
|
174 G.host.bridge.mucJoin(unicode(jid_), "", "", self.profile, callback=self._mucJoinCb, errback=self._mucJoinEb) |
22 | 175 else: |
42
286865bc013a
chat: joining MUC using header input is now working:
Goffi <goffi@goffi.org>
parents:
41
diff
changeset
|
176 self.changeWidget(jid_) |
22 | 177 |
178 def discoEb(failure): | |
179 log.warning(u"Disco failure, ignore this text: {}".format(failure)) | |
180 | |
181 G.host.bridge.discoInfos(jid_.domain, self.profile, callback=discoCb, errback=discoEb) | |
182 | |
37
6cf08d0ee460
chat: forbid scrolling on X axis + don't delete widget until explicitly requested (with force attribute)
Goffi <goffi@goffi.org>
parents:
35
diff
changeset
|
183 def onDelete(self, force=False): |
6cf08d0ee460
chat: forbid scrolling on X axis + don't delete widget until explicitly requested (with force attribute)
Goffi <goffi@goffi.org>
parents:
35
diff
changeset
|
184 if force==True: |
6cf08d0ee460
chat: forbid scrolling on X axis + don't delete widget until explicitly requested (with force attribute)
Goffi <goffi@goffi.org>
parents:
35
diff
changeset
|
185 return True |
6cf08d0ee460
chat: forbid scrolling on X axis + don't delete widget until explicitly requested (with force attribute)
Goffi <goffi@goffi.org>
parents:
35
diff
changeset
|
186 if len(list(G.host.widgets.getWidgets(self.__class__, self.target, profiles=self.profiles))) > 1: |
6cf08d0ee460
chat: forbid scrolling on X axis + don't delete widget until explicitly requested (with force attribute)
Goffi <goffi@goffi.org>
parents:
35
diff
changeset
|
187 # we don't keep duplicate widgets |
6cf08d0ee460
chat: forbid scrolling on X axis + don't delete widget until explicitly requested (with force attribute)
Goffi <goffi@goffi.org>
parents:
35
diff
changeset
|
188 return True |
6cf08d0ee460
chat: forbid scrolling on X axis + don't delete widget until explicitly requested (with force attribute)
Goffi <goffi@goffi.org>
parents:
35
diff
changeset
|
189 return False |
22 | 190 |
191 | |
192 PLUGIN_INFO["factory"] = Chat.factory | |
193 quick_widgets.register(quick_chat.QuickChat, Chat) |