diff options
author | dec05eba <dec05eba@protonmail.com> | 2019-01-27 16:23:26 +0100 |
---|---|---|
committer | dec05eba <dec05eba@protonmail.com> | 2020-08-19 00:14:44 +0200 |
commit | 61c3ec1e6e8f26f6d1efc271794e791ecde8f1e2 (patch) | |
tree | aaa7d7c88f20b5972cf85514ea5c5990ea1a6a9c /src | |
parent | a089da2e943ee4e0237a370b9372f232970c22b3 (diff) |
Use rich text for messages
Diffstat (limited to 'src')
-rw-r--r-- | src/ChatMessage.cpp | 63 | ||||
-rw-r--r-- | src/ChatWindow.cpp | 16 |
2 files changed, 69 insertions, 10 deletions
diff --git a/src/ChatMessage.cpp b/src/ChatMessage.cpp index e1fe927..e440409 100644 --- a/src/ChatMessage.cpp +++ b/src/ChatMessage.cpp @@ -1,10 +1,46 @@ #include "../include/ChatMessage.hpp" +#include <dchat/IncomingMessage.hpp> +#include <assert.h> namespace dchat { + static void appendRichText(Gtk::TextView *textView, Glib::RefPtr<Gtk::TextBuffer> buffer, Gtk::TextIter iter, const Glib::ustring &text) + { + parseIncomingMessage(text.data(), text.bytes(), [textView, &text, &iter, &buffer](IncomingMessagePart messagePart) + { + switch(messagePart.type) + { + case IncomingMessagePart::Type::TEXT: + { + iter = buffer->insert(iter, text.data() + messagePart.textRange.start, text.data() + messagePart.textRange.end); + break; + } + case IncomingMessagePart::Type::EMOJI: + { + auto anchor = Gtk::TextChildAnchor::create(); + iter = buffer->insert_child_anchor(iter, anchor); + auto image = Gtk::manage(new DynamicImage()); + image->url = text.substr(messagePart.textRange.start, messagePart.textRange.length()); + image->set_size_request(35, 35); + textView->add_child_at_anchor(*image, anchor); + break; + } + default: + assert(false); + break; + } + }); + } + + void applyRichText(Gtk::TextView *textView, const Glib::ustring &text) + { + auto buffer = textView->get_buffer(); + buffer->set_text(""); + appendRichText(textView, buffer, buffer->begin(), text); + } + ChatMessage::ChatMessage(const Glib::ustring &_username, const Glib::ustring &_text, uint32_t _timestampSeconds) : username(_username), - text(_text), timestampSeconds(_timestampSeconds) { avatar.set_halign(Gtk::ALIGN_START); @@ -15,11 +51,12 @@ namespace dchat username.set_alignment(Gtk::ALIGN_START, Gtk::ALIGN_START); username.get_style_context()->add_class("message-message-username"); - text.set_selectable(true); - text.set_alignment(Gtk::ALIGN_START, Gtk::ALIGN_START); - text.set_line_wrap(true); - text.set_line_wrap_mode(Pango::WRAP_WORD_CHAR); - text.set_vexpand(true); + text.set_wrap_mode(Gtk::WRAP_WORD_CHAR); + //text.set_halign(Gtk::ALIGN_START); + //text.set_valign(Gtk::ALIGN_START); + text.set_hexpand(true); + text.set_editable(false); + applyRichText(&text, _text); text.get_style_context()->add_class("message-message-text"); attach(avatar, 0, 0, 1, 2); @@ -31,4 +68,18 @@ namespace dchat set_row_spacing(0); set_vexpand(false); } + + void ChatMessage::appendText(const Glib::ustring &_text) + { + auto buffer = text.get_buffer(); + // Optimized for single ascii characters, such as "\n" + if(_text.bytes() == 1) + { + buffer->insert(buffer->end(), _text); + } + else + { + appendRichText(&text, buffer, buffer->end(), _text); + } + } }
\ No newline at end of file diff --git a/src/ChatWindow.cpp b/src/ChatWindow.cpp index 9fc810b..d33c824 100644 --- a/src/ChatWindow.cpp +++ b/src/ChatWindow.cpp @@ -303,8 +303,11 @@ namespace dchat { if(chatInput.get_editable()) { - currentRoom->publishMessage(chatInput.get_buffer()->get_text()); + Glib::ustring str = chatInput.get_buffer()->get_text(); chatInput.get_buffer()->set_text(""); + while(gtk_events_pending()) + gtk_main_iteration_do(FALSE); + currentRoom->publishMessage(str); } return true; } @@ -441,14 +444,19 @@ namespace dchat if(msgTimeDiff <= MERGE_MESSAGE_TIMESTAMP_DIFF_SEC) { auto message = messageById[request.prevMessage->id]; - message->text.set_text(message->text.get_text() + "\n" + request.message->text); + message->appendText("\n"); + message->appendText(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(); + currentRoomData->messageAreaLayout->signal_draw(); scrollToBottom(); } + else + { + currentRoomData->messageAreaLayout->signal_draw(); + } return; } } @@ -471,7 +479,7 @@ namespace dchat // 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(); + roomData->messageAreaLayout->signal_draw(); scrollToBottom(); } } |