Created
March 25, 2019 22:07
-
-
Save tshirtman/3fa263ef8a67c7c3f2aee2c99c4ce6ed 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
from kivy.app import App | |
from kivy.lang import Builder | |
from kivy.clock import Clock | |
from kivy.properties import ListProperty | |
from kivy.animation import Animation | |
KV = ''' | |
#:import RGBA kivy.utils.rgba | |
<ImageButton@ButtonBehavior+Image>: | |
size_hint: None, None | |
size: self.texture_size | |
canvas.before: | |
PushMatrix | |
Scale: | |
origin: self.center | |
x: .75 if self.state == 'down' else 1 | |
y: .75 if self.state == 'down' else 1 | |
canvas.after: | |
PopMatrix | |
BoxLayout: | |
orientation: 'vertical' | |
RecycleView: | |
id: rv | |
data: app.messages | |
viewclass: 'Message' | |
do_scroll_x: False | |
RecycleBoxLayout: | |
id: box | |
orientation: 'vertical' | |
size_hint_y: None | |
size: self.minimum_size | |
default_size_hint: 1, None | |
key_size: '_size' | |
FloatLayout: | |
size_hint_y: None | |
height: 0 | |
Button: | |
size_hint_y: None | |
height: self.texture_size[1] | |
opacity: 0 if not self.height else 1 | |
text: 'go to last message' if rv.height < box.height and rv.scroll_y > 0 else '' | |
pos_hint: {'pos': (0, 0)} | |
on_release: app.scroll_bottom() | |
BoxLayout: | |
size_hint: 1, None | |
size: self.minimum_size | |
TextInput: | |
id: ti | |
size_hint: 1, None | |
height: min(max(self.line_height, self.minimum_height), 150) | |
ImageButton: | |
source: 'data/logo/kivy-icon-48.png' | |
on_release: | |
app.send_message(ti.text) | |
ti.text = '' | |
<Message@FloatLayout>: | |
message_id: -1 | |
bg_color: '#223344' | |
side: 'left' | |
text: '' | |
size_hint_y: None | |
_size: 0, 0 | |
Label: | |
text: root.text | |
padding: 10, 10 | |
size_hint: None, 1 | |
size: root._size | |
on_texture_size: | |
message = dict(app.messages[root.message_id]) | |
message['_size'] = self.texture_size | |
app.messages[root.message_id] = message | |
pos_hint: | |
( | |
{'x': 0, 'center_y': .5} | |
if root.side == 'left' else | |
{'right': 1, 'center_y': .5} | |
) | |
canvas.before: | |
Color: | |
rgba: RGBA(root.bg_color) | |
RoundedRectangle: | |
size: self.size | |
pos: self.pos | |
radius: 5, 5, 5, 5 | |
''' | |
class MessengerApp(App): | |
messages = ListProperty() | |
def build(self): | |
return Builder.load_string(KV) | |
def add_message(self, text, side, color): | |
self.messages.append({ | |
'message_id': len(self.messages), | |
'text': text, | |
'side': side, | |
'bg_color': color | |
}) | |
def send_message(self, text): | |
self.add_message(text, 'right', '#223344') | |
Clock.schedule_once(lambda *args: self.answer(text), 1) | |
self.scroll_bottom() | |
def answer(self, text, *args): | |
self.add_message('do you really think so?', 'left', '#332211') | |
def scroll_bottom(self): | |
Animation.cancel_all(self.root.ids.rv, 'scroll_y') | |
Animation(scroll_y=0, t='out_quad', d=.5).start(self.root.ids.rv) | |
if __name__ == '__main__': | |
MessengerApp().run() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment