-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
224 lines (165 loc) · 7.26 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
import sys
import kivy
from kivy.app import App
from kivy.uix.label import Label
from kivy.uix.gridlayout import GridLayout
from kivy.uix.textinput import TextInput
from kivy.uix.button import Button
from kivy.uix.screenmanager import ScreenManager, Screen
from kivy.clock import Clock
from kivy.core.window import Window
from kivy.uix.scrollview import ScrollView
import socket_client
import sys
import os
# print(kivy.__version__)
kivy.require("2.0.0")
class ScrollableLabel(ScrollView):
def __init__(self, **kwargs):
super().__init__(**kwargs)
self.layout = GridLayout(cols = 1, size_hint_y = None)
self.add_widget(self.layout)
self.chat_history = Label(size_hint_y = None, markup = True) # markup to give different colors to different users
self.scroll_point = Label() # just to stop scrolling to this point
self.layout.add_widget(self.chat_history)
self.layout.add_widget(self.scroll_point)
def update_chat_history(self, message):
self.chat_history.text += '\n' + message
self.layout.height = self.chat_history.texture_size[1] + 15
self.chat_history.height = self.chat_history.texture_size[1]
self.chat_history.text_size = (self.chat_history.width*0.98, None)
self.scroll_to(self.scroll_point)
#self.scroll_y = 0
def update_chat_history_layout(self, _ = None):
self.layout.height = self.chat_history.texture_size[1] + 15
self.chat_history.height = self.chat_history.texture_size[1]
self.chat_history.text_size = (self.chat_history.width * 0.98, None)
class LoginPage(GridLayout):
def __init__(self, **kwargs):
super().__init__(
**kwargs) # kwargs : keyward arguments :Kwargs is an unpacking operator that we use with dictionaries
if os.path.isfile("prev_details.txt"): # just to save the previous input
with open("prev_details.txt", "r") as f:
d = f.read().split(",")
prev_ip = d[0]
prev_port = d[1]
prev_username = d[2]
else:
prev_ip = ""
prev_port = ""
prev_username = ""
self.cols = 2
self.add_widget(Label(text="IP: "))
self.ip = TextInput(text=prev_ip, multiline=False)
self.add_widget(self.ip)
self.add_widget(Label(text="Port: "))
self.port = TextInput(text=prev_port, multiline=False)
self.add_widget(self.port)
self.add_widget(Label(text="Username: "))
self.username = TextInput(text=prev_username, multiline=False)
self.add_widget(self.username)
self.enter = Button(text="Enter")
self.enter.bind(on_press=self.enter_button)
self.add_widget(Label())
self.add_widget(self.enter)
def enter_button(self, instance):
port = self.port.text
ip = self.ip.text
username = self.username.text
with open("prev_details.txt", "w") as f: # to create the txt file to store the input
f.write(f"{ip},{port},{username}")
info = f"Attempting to join {ip}:{port} as {username}"
chat_bot.info_page.update_info(info)
chat_bot.screen_manager.current = "Info"
Clock.schedule_once(self.connect, 1)
def connect(self, _): # _ : how many seconds has gone since the schedule
ip = self.ip.text
port = int(self.port.text)
username = self.username.text
if not socket_client.connect(ip, port, username, show_error):
return
chat_bot.create_chat_page()
chat_bot.screen_manager.current = "Chat"
class InfoPage(GridLayout):
def __init__(self, **kwargs):
super().__init__(**kwargs)
self.cols = 1
self.message = Label(halign = "center", valign = "middle", font_size = 30) # to print out whats going on
self.message.bind(width = self.update_text_width)
self.add_widget(self.message)
def update_info(self, message):
self.message.text = message
def update_text_width(self, *_): # *_ : anything else i.e we don't care :p
self.message.text_size = (self.message.width*0.9, None)
class ChatPage(GridLayout):
def __init__(self, **kwargs):
super().__init__(**kwargs)
self.cols = 1
self.rows = 2
self.history = ScrollableLabel(height = Window.size[1]*0.9, size_hint_y = None)
self.add_widget(self.history)
self.new_message = TextInput(width= Window.size[0]*0.8, size_hint_x = None, multiline = False)
self.send = Button(text = "Send")
self.send.bind(on_press= self.send_message)
bottom_line = GridLayout(cols=2)
bottom_line.add_widget(self.new_message)
bottom_line.add_widget(self.send)
self.add_widget(bottom_line)
Window.bind(on_key_down = self.on_key_down)
Clock.schedule_once(self.focus_text_input, 1)
socket_client.start_listening(self.incoming_message, show_error)
self.bind(size = self.adjust_fields)
def adjust_fields(self, *_):
# Chat history height - 90%, but at least 50px for bottom new message/send button part
if Window.size[1] * 0.1 < 50:
new_height = Window.size[1] - 50
else:
new_height = Window.size[1] * 0.9
self.history.height = new_height
# New message input width - 80%, but at least 160px for send button
if Window.size[0] * 0.2 < 160:
new_width = Window.size[0] - 160
else:
new_width = Window.size[0] * 0.8
self.new_message.width = new_width
# Update chat history layout
#self.history.update_chat_history_layout()
Clock.schedule_once(self.history.update_chat_history_layout, 0.01)
def on_key_down(self, instance, keyboard, keycode, text, modifiers):
if keycode == 40:
self.send_message(None)
def send_message(self, _):
message = self.new_message.text
self.new_message.text = ""
if message:
self.history.update_chat_history(f'[color=dd2020] {chat_bot.connect_page.username.text}[/color] > {message}')
socket_client.send(message)
Clock.schedule_once(self.focus_text_input, 0.1)
def focus_text_input(self, _):
self.new_message.focus = True
def incoming_message(self, username, message):
self.history.update_chat_history(f'[color=20dd20]{username}[/color] > {message}')
class MainScreen(App):
def build(self):
self.screen_manager = ScreenManager()
self.connect_page = LoginPage()
screen = Screen(name = "Connect")
screen.add_widget(self.connect_page)
self.screen_manager.add_widget(screen)
self.info_page = InfoPage()
screen = Screen(name = "Info")
screen.add_widget(self.info_page)
self.screen_manager.add_widget(screen)
return self.screen_manager
def create_chat_page(self):
self.chat_page = ChatPage()
screen = Screen(name = "Chat")
screen.add_widget(self.chat_page)
self.screen_manager.add_widget(screen)
def show_error(message):
chat_bot.info_page.update_info(message)
chat_bot.screen_manager.current = "Info" # just to make sure we stay here
Clock.schedule_once(sys.exit, 10)
if __name__ == "__main__":
chat_bot = MainScreen()
chat_bot.run()