make chat message components their own subclass of ListBoxRow

This commit is contained in:
ouwou 2020-08-26 01:46:43 -04:00
parent 82a21bd085
commit c6e2f266a1
6 changed files with 82 additions and 54 deletions

View File

@ -145,6 +145,7 @@
<ItemGroup>
<ClCompile Include="abaddon.cpp" />
<ClCompile Include="components\channels.cpp" />
<ClCompile Include="components\chatmessage.cpp" />
<ClCompile Include="components\chatwindow.cpp" />
<ClCompile Include="components\memberlist.cpp" />
<ClCompile Include="dialogs\token.cpp" />
@ -157,6 +158,7 @@
<ItemGroup>
<ClInclude Include="components\channels.hpp" />
<ClInclude Include="abaddon.hpp" />
<ClInclude Include="components\chatmessage.hpp" />
<ClInclude Include="components\chatwindow.hpp" />
<ClInclude Include="components\memberlist.hpp" />
<ClInclude Include="dialogs\token.hpp" />

View File

@ -45,6 +45,9 @@
<ClCompile Include="components\memberlist.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="components\chatmessage.cpp">
<Filter>Source Files</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<ClInclude Include="windows\mainwindow.hpp">
@ -77,5 +80,8 @@
<ClInclude Include="components\memberlist.hpp">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="components\chatmessage.hpp">
<Filter>Header Files</Filter>
</ClInclude>
</ItemGroup>
</Project>

View File

@ -0,0 +1,48 @@
#include "chatmessage.hpp"
ChatMessageTextItem::ChatMessageTextItem(const MessageData *data) {
auto *main_box = Gtk::manage(new Gtk::Box(Gtk::ORIENTATION_HORIZONTAL));
auto *sub_box = Gtk::manage(new Gtk::Box(Gtk::ORIENTATION_VERTICAL));
auto *meta_box = Gtk::manage(new Gtk::Box(Gtk::ORIENTATION_HORIZONTAL));
auto *author = Gtk::manage(new Gtk::Label);
auto *timestamp = Gtk::manage(new Gtk::Label);
auto *text = Gtk::manage(new Gtk::TextView);
text->set_can_focus(false);
text->set_editable(false);
text->set_wrap_mode(Gtk::WRAP_WORD_CHAR);
text->set_halign(Gtk::ALIGN_FILL);
text->set_hexpand(true);
text->get_buffer()->set_text(data->Content);
text->show();
author->set_markup("<span weight=\"bold\">" + Glib::Markup::escape_text(data->Author.Username) + "</span>");
author->set_single_line_mode(true);
author->set_line_wrap(false);
author->set_ellipsize(Pango::ELLIPSIZE_END);
author->set_xalign(0.f);
author->show();
timestamp->set_text(data->Timestamp);
timestamp->set_opacity(0.5);
timestamp->set_single_line_mode(true);
timestamp->set_margin_start(12);
timestamp->show();
main_box->set_hexpand(true);
main_box->set_vexpand(true);
main_box->show();
meta_box->show();
sub_box->show();
meta_box->add(*author);
meta_box->add(*timestamp);
sub_box->add(*meta_box);
sub_box->add(*text);
main_box->add(*sub_box);
add(*main_box);
set_margin_bottom(8);
show();
}

View File

@ -0,0 +1,13 @@
#pragma once
#include <gtkmm.h>
#include "../discord/discord.hpp"
class ChatMessageItem : public Gtk::ListBoxRow {
public:
Snowflake ID;
};
class ChatMessageTextItem : public ChatMessageItem {
public:
ChatMessageTextItem(const MessageData* data);
};

View File

@ -55,7 +55,7 @@ ChatWindow::ChatWindow() {
m_main->add(*m_entry_scroll);
}
void ChatWindow::SetAbaddon(Abaddon* ptr) {
void ChatWindow::SetAbaddon(Abaddon *ptr) {
m_abaddon = ptr;
}
@ -71,61 +71,19 @@ Snowflake ChatWindow::GetActiveChannel() const {
return m_active_channel;
}
Gtk::ListBoxRow *ChatWindow::CreateChatEntryComponentText(const MessageData *data) {
auto *row = Gtk::manage(new Gtk::ListBoxRow);
auto *main_box = Gtk::manage(new Gtk::Box(Gtk::ORIENTATION_HORIZONTAL));
auto *sub_box = Gtk::manage(new Gtk::Box(Gtk::ORIENTATION_VERTICAL));
auto *meta_box = Gtk::manage(new Gtk::Box(Gtk::ORIENTATION_HORIZONTAL));
auto *author = Gtk::manage(new Gtk::Label);
auto *timestamp = Gtk::manage(new Gtk::Label);
auto *text = Gtk::manage(new Gtk::TextView);
text->set_can_focus(false);
text->set_editable(false);
text->set_wrap_mode(Gtk::WRAP_WORD_CHAR);
text->set_halign(Gtk::ALIGN_FILL);
text->set_hexpand(true);
text->get_buffer()->set_text(data->Content);
text->show();
author->set_markup("<span weight=\"bold\">" + Glib::Markup::escape_text(data->Author.Username) + "</span>");
author->set_single_line_mode(true);
author->set_line_wrap(false);
author->set_ellipsize(Pango::ELLIPSIZE_END);
author->set_xalign(0.f);
author->show();
timestamp->set_text(data->Timestamp);
timestamp->set_opacity(0.5);
timestamp->set_single_line_mode(true);
timestamp->set_margin_start(12);
timestamp->show();
main_box->set_hexpand(true);
main_box->set_vexpand(true);
main_box->show();
meta_box->show();
sub_box->show();
meta_box->add(*author);
meta_box->add(*timestamp);
sub_box->add(*meta_box);
sub_box->add(*text);
main_box->add(*sub_box);
row->add(*main_box);
row->set_margin_bottom(8);
row->show();
return row;
ChatMessageItem *ChatWindow::CreateChatEntryComponentText(const MessageData *data) {
return Gtk::manage(new ChatMessageTextItem(data));
}
Gtk::ListBoxRow *ChatWindow::CreateChatEntryComponent(const MessageData *data) {
ChatMessageItem *ChatWindow::CreateChatEntryComponent(const MessageData *data) {
ChatMessageItem *item = nullptr;
if (data->Type == MessageType::DEFAULT && data->Content.size() > 0)
return CreateChatEntryComponentText(data);
item = CreateChatEntryComponentText(data);
return nullptr;
if (item != nullptr)
item->ID = data->ID;
return item;
}
bool ChatWindow::on_key_press_event(GdkEventKey *e) {

View File

@ -2,6 +2,7 @@
#include <gtkmm.h>
#include <queue>
#include <mutex>
#include "chatmessage.hpp"
#include "../discord/discord.hpp"
class Abaddon;
@ -20,8 +21,8 @@ protected:
void ScrollToBottom();
void SetMessagesInternal();
void AddNewMessageInternal();
Gtk::ListBoxRow *CreateChatEntryComponentText(const MessageData *data);
Gtk::ListBoxRow *CreateChatEntryComponent(const MessageData *data);
ChatMessageItem *CreateChatEntryComponentText(const MessageData *data);
ChatMessageItem *CreateChatEntryComponent(const MessageData *data);
bool on_key_press_event(GdkEventKey *e);