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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
|
#include "../include/ChatWindow.hpp"
#include "../include/ChatMessage.hpp"
#include "../include/InputDialog.hpp"
#include "../include/Window.hpp"
#include <gtkmm/alignment.h>
#include <gtkmm/viewport.h>
#include <gtkmm/scrollbar.h>
#include <gtkmm/eventbox.h>
#include <assert.h>
namespace dchat
{
// Merge all messages that are written by the same user without interrupt within a timeframe
const int MERGE_MESSAGE_TIMESTAMP_DIFF_SEC = 60;
ChatWindow::ChatWindow(Window *_window) :
addRoomButton("images/add_button_small.png", " Add room"),
roomCount(0),
currentRoomData(nullptr),
window(_window)
{
assert(window);
leftPanelUsersStack.set_homogeneous(false);
messageAreaStack.set_homogeneous(false);
setupTopBar();
Gtk::Paned *sidePanels = Gtk::manage(new Gtk::Paned());
sidePanels->set_name("side-panels");
sidePanels->set_border_width(0);
attach(*sidePanels, 0, 1, 1, 2);
setupLeftPanel(sidePanels);
Gtk::Grid *rightPanel = Gtk::manage(new Gtk::Grid());
rightPanel->set_vexpand(true);
rightPanel->set_hexpand(true);
sidePanels->add2(*rightPanel);
setupMessageArea(rightPanel);
setupChatInput(rightPanel);
set_vexpand(true);
set_hexpand(true);
}
ChatWindow::~ChatWindow()
{
for(auto &it : roomDataById)
{
delete it.second->leftPanelUsersLayout;
delete it.second->messageAreaLayout;
delete it.second->button;
delete it.second;
}
}
void ChatWindow::setupTopBar()
{
topbar.set_name("top-bar");
topbar.set_hexpand(true);
attach(topbar, 0, 0, 2, 1);
Gtk::Grid *topbarLeft = Gtk::manage(new Gtk::Grid());
topbarLeft->set_name("top-bar-left");
topbarLeft->set_size_request(180);
topbarLeft->set_valign(Gtk::ALIGN_CENTER);
topbarLeft->set_halign(Gtk::ALIGN_CENTER);
topbar.attach(*topbarLeft, 0, 0, 1, 1);
topbarSearchBar.set_name("top-bar-search");
topbarSearchBar.set_placeholder_text("Search...");
topbarSearchBar.set_size_request(180);
topbarLeft->attach(topbarSearchBar, 0, 0, 1, 1);
Gtk::Grid *topbarRight = Gtk::manage(new Gtk::Grid());
topbarRight->set_name("top-bar-right");
topbarRight->set_hexpand(true);
topbarRight->set_valign(Gtk::ALIGN_CENTER);
topbar.attach_next_to(*topbarRight, *topbarLeft, Gtk::POS_RIGHT, 1, 1);
currentChannelTitle.set_name("current-room-title");
currentChannelTitle.set_hexpand(true);
currentChannelTitle.set_alignment(Gtk::ALIGN_START, Gtk::ALIGN_CENTER);
topbarRight->attach(currentChannelTitle, 0, 0, 1, 1);
ImageButton *channelSettings = Gtk::manage(new ImageButton("images/settings-icon.png", nullptr));
topbarRight->attach_next_to(*channelSettings, currentChannelTitle, Gtk::POS_RIGHT, 1, 1);
}
void ChatWindow::setupLeftPanel(Gtk::Paned *sidePanels)
{
Gtk::Grid *leftPanelLayout = Gtk::manage(new Gtk::Grid());
leftPanelLayout->set_vexpand(true);
leftPanelLayout->set_size_request(200);
leftPanelLayout->set_name("left-panel");
sidePanels->add1(*leftPanelLayout);
Gtk::Paned *leftPanel = Gtk::manage(new Gtk::Paned(Gtk::ORIENTATION_VERTICAL));
leftPanel->set_vexpand(true);
leftPanelLayout->attach(*leftPanel, 0, 0, 1, 2);
leftPanelChannels.set_vexpand(true);
leftPanel->add1(leftPanelChannels);
Gtk::Label *channelsTitle = Gtk::manage(new Gtk::Label());
channelsTitle->set_name("channels-title");
channelsTitle->set_text("Channels");
channelsTitle->set_halign(Gtk::ALIGN_START);
leftPanelChannels.attach(*channelsTitle, 0, 0, 1, 1);
////
leftPanelUsersStack.set_vexpand(true);
leftPanel->add2(leftPanelUsersStack);
addRoomButton.set_halign(Gtk::ALIGN_START);
leftPanelLayout->attach_next_to(addRoomButton, *leftPanel, Gtk::POS_BOTTOM, 1, 1);
addRoomButton.signal_clicked().connect([this]()
{
InputDialog createRoomDialog("Create a new room", "Room name");
switch(createRoomDialog.run())
{
case Gtk::RESPONSE_ACCEPT:
{
// TODO: Show error inline in the create room dialog
Glib::ustring roomName = createRoomDialog.getInput();
if(roomName.size() == 0 || roomName.size() > 32)
window->windowNotification->show("Room name has to be between 1 and 32 characters");
else
window->rooms->createRoom(roomName);
break;
}
default:
break;
}
});
}
void ChatWindow::setupMessageArea(Gtk::Grid *rightPanel)
{
//messageArea.set_valign(Gtk::ALIGN_START);
messageArea.set_vexpand(true);
messageArea.set_policy(Gtk::PolicyType::POLICY_NEVER, Gtk::PolicyType::POLICY_AUTOMATIC);
rightPanel->attach(messageArea, 0, 0, 1, 2);
messageAreaStack.set_name("chat-area-layout");
messageArea.add(messageAreaStack);
}
void ChatWindow::setupChatInput(Gtk::Grid *rightPanel)
{
Gtk::Grid *chatArea = Gtk::manage(new Gtk::Grid());
rightPanel->attach_next_to(*chatArea, messageArea, Gtk::POS_BOTTOM, 1, 1);
Gtk::ScrolledWindow *chatScrollWindow = Gtk::manage(new Gtk::ScrolledWindow());
chatScrollWindow->set_hexpand(true);
chatScrollWindow->set_policy(Gtk::PolicyType::POLICY_NEVER, Gtk::PolicyType::POLICY_NEVER);
chatScrollWindow->set_name("chat-scroll-view");
chatArea->attach(*chatScrollWindow, 0, 0, 1, 1);
chatInput.set_hexpand(true);
chatInput.set_editable(false);
chatInput.set_name("chat-input");
chatInput.set_wrap_mode(Gtk::WrapMode::WRAP_WORD_CHAR);
chatScrollWindow->add(chatInput);
double fontSize = 18.5;//PANGO_PIXELS(chatInput.get_style_context()->get_font().get_size());
chatPrevNumLines = 1;
chatInput.signal_key_press_event().connect([this](GdkEventKey *event)
{
if((event->keyval == GDK_KEY_Return || event->keyval == GDK_KEY_KP_Enter) && !(event->state & Gdk::SHIFT_MASK))
{
if(chatInput.get_editable())
{
currentRoom->publishMessage(chatInput.get_buffer()->get_text());
chatInput.get_buffer()->set_text("");
}
return true;
}
return false;
}, false);
chatInput.get_buffer()->signal_changed().connect([this, chatScrollWindow, fontSize]
{
int numLines = chatInput.get_buffer()->get_line_count();
numLines = std::min(numLines, 10);
if(numLines != chatPrevNumLines)
{
chatPrevNumLines = numLines;
chatScrollWindow->set_min_content_height(fontSize * numLines);
auto adj = chatScrollWindow->get_vadjustment();
if(chatInput.get_buffer()->get_line_count() <= 10)
{
chatScrollWindow->set_policy(Gtk::PolicyType::POLICY_NEVER, Gtk::PolicyType::POLICY_NEVER);
adj->set_value(0);
}
else
{
chatScrollWindow->set_policy(Gtk::PolicyType::POLICY_NEVER, Gtk::PolicyType::POLICY_ALWAYS);
}
}
});
chatScrollWindow->get_vadjustment()->signal_value_changed().connect([this, chatScrollWindow]()
{
auto adj = chatScrollWindow->get_vadjustment();
if(chatInput.get_buffer()->get_line_count() <= 11)
{
chatScrollWindow->set_policy(Gtk::PolicyType::POLICY_NEVER, Gtk::PolicyType::POLICY_NEVER);
adj->set_value(0);
}
else
{
chatScrollWindow->set_policy(Gtk::PolicyType::POLICY_NEVER, Gtk::PolicyType::POLICY_ALWAYS);
}
});
}
void ChatWindow::addRoom(std::shared_ptr<Room> room)
{
std::string roomIdStr = room->id->toString();
Gtk::Grid *leftPanelUsersLayout = new Gtk::Grid();
leftPanelUsersLayout->set_vexpand(true);
leftPanelUsersLayout->show();
leftPanelUsersStack.add(*leftPanelUsersLayout, roomIdStr);
Gtk::Label *usersTitle = Gtk::manage(new Gtk::Label());
usersTitle->get_style_context()->add_class("users-title");
usersTitle->set_text("Users");
usersTitle->set_halign(Gtk::ALIGN_START);
usersTitle->show();
leftPanelUsersLayout->attach(*usersTitle, 0, 0, 1, 1);
Gtk::Grid *messageAreaLayout = new Gtk::Grid();
messageAreaLayout->show();
messageAreaStack.add(*messageAreaLayout, roomIdStr);
fprintf(stderr, "Added channel %s\n", room->id->toString().c_str());
Gtk::ToggleButton *roomButton = new Gtk::ToggleButton("Room name");
roomButton->set_active(true);
roomButton->get_style_context()->add_class("room-button");
roomButton->set_hexpand(true);
roomButton->set_halign(Gtk::ALIGN_START);
roomButton->get_child()->set_halign(Gtk::ALIGN_START);
roomButton->show();
roomButton->signal_clicked().connect([this, room]
{
setCurrentRoom(room);
});
leftPanelChannels.attach(*roomButton, 0, 1 + roomCount, 1, 1);
++roomCount;
currentRoomData = new RoomData { leftPanelUsersLayout, messageAreaLayout, roomButton };
roomDataById[*room->id] = currentRoomData;
currentRoom = room;
chatInput.set_editable(true);
leftPanelUsersStack.set_visible_child(roomIdStr);
messageAreaStack.set_visible_child(roomIdStr);
}
void ChatWindow::setCurrentRoom(std::shared_ptr<Room> room)
{
std::string roomIdStr = room->id->toString();
leftPanelUsersStack.set_visible_child(roomIdStr);
messageAreaStack.set_visible_child(roomIdStr);
currentChannelTitle.set_text(room->name);
currentRoom = room;
currentRoomData = roomDataById[*room->id];
chatInput.set_editable(true);
// TODO: Instead of scrolling to bottom, remember scroll position (even after restarting application).
// We want to show oldest unread message first
scrollToBottom();
}
void ChatWindow::addMessage(const RoomAddMessageRequest &request)
{
auto roomMessages = request.room->messages;
RoomMessage *lastMessage = nullptr;
if(!roomMessages.empty())
lastMessage = &roomMessages.back();
if(lastMessage && lastMessage->creator->publicKey == request.message.creator->publicKey)
{
int64_t msgTimeDiff = (int64_t)request.message.timestampSeconds - (int64_t)lastMessage->timestampSeconds;
if(msgTimeDiff <= MERGE_MESSAGE_TIMESTAMP_DIFF_SEC)
{
auto message = messageById[lastMessage->id];
message->text.set_text(message->text.get_text() + "\n" + request.message.text);
// Since messages that are sent withing a timeframe are combined, several message ids can refer to the same message
messageById[request.message.id] = message;
if(!request.loadedFromCache && *request.room->id == *currentRoom->id)
{
currentRoomData->messageAreaLayout->queue_draw();
scrollToBottom();
}
return;
}
}
std::string userNickname = request.message.creator->nickname;
if(userNickname.empty())
userNickname = "Anonymous";
ChatMessage *message = Gtk::manage(new ChatMessage(userNickname, request.message.text, request.message.timestampSeconds));
message->set_valign(Gtk::Align::ALIGN_START);
message->set_hexpand(true);
message->show_all();
messageById[request.message.id] = message;
RoomData *roomData = roomDataById[*request.room->id];
roomData->messageAreaLayout->attach(*message, 0, roomMessages.size(), 1, 1);
// TODO: When we get a message in the current room we scroll to the bottom, but this should only be done if we are not manually scrolling to view old messages
if(!request.loadedFromCache && *request.room->id == *currentRoom->id)
{
roomData->messageAreaLayout->queue_draw();
scrollToBottom();
}
}
void ChatWindow::addUser(std::shared_ptr<Room> room, std::shared_ptr<User> user)
{
Gtk::Label *username = Gtk::manage(new Gtk::Label("Anonymous"));
username->set_halign(Gtk::ALIGN_START);
username->show();
username->get_style_context()->add_class("username-list-username");
user->userdata = username;
RoomData *roomData = roomDataById[*room->id];
roomData->leftPanelUsersLayout->attach(*username, 0, room->userByPublicKey.size(), 1, 1);
fprintf(stderr, "Added user %s\n", user->publicKey.toString().c_str());
}
void ChatWindow::setUserNickname(const UserChangeNicknameRequest &request)
{
Gtk::Label *userNicknameLabel = (Gtk::Label*)request.user->userdata;
userNicknameLabel->set_text(request.newNickname);
fprintf(stderr, "Set nickname for user %s to %s\n", request.user->publicKey.toString().c_str(), request.newNickname.c_str());
}
void ChatWindow::changeRoomName(const RoomChangeNameRequest &request)
{
Gtk::Button *button = roomDataById[*request.room->id]->button;
static_cast<Gtk::Label*>(button->get_child())->set_text(request.newName);
if(*request.room->id == *currentRoom->id)
currentChannelTitle.set_text(request.newName);
fprintf(stderr, "Changed room %s name to %s\n", request.room->id->toString().c_str(), request.newName.c_str());
}
void ChatWindow::scrollToBottom()
{
while(gtk_events_pending())
gtk_main_iteration_do(FALSE);
auto adj = messageArea.get_vadjustment();
adj->set_value(adj->get_upper());
}
}
|