Skip to content

Commit

Permalink
* Refactor pinned message list.
Browse files Browse the repository at this point in the history
* Pin list: Fix repaint bug where avatars, attachments and custom emoji weren't updated when loaded.
  • Loading branch information
iProgramMC committed May 31, 2024
1 parent b60ef4e commit a5e0afb
Show file tree
Hide file tree
Showing 8 changed files with 230 additions and 191 deletions.
6 changes: 3 additions & 3 deletions src/windows/Frontend_Win32.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
#include "ImageLoader.hpp"
#include "ProfilePopout.hpp"
#include "QRCodeDialog.hpp"
#include "PinnedMessageViewer.hpp"
#include "PinList.hpp"
#include "../discord/UpdateChecker.hpp"
#include "../discord/LocalSettings.hpp"

Expand Down Expand Up @@ -130,8 +130,8 @@ void Frontend_Win32::OnRequestDone(NetRequest* pRequest)

void Frontend_Win32::OnLoadedPins(Snowflake channel, const std::string& data)
{
if (PmvIsActive())
PmvOnLoadedPins(channel, data);
if (PinList::IsActive())
PinList::OnLoadedPins(channel, data);
}

void Frontend_Win32::OnUpdateAvailable(const std::string& url, const std::string& version)
Expand Down
4 changes: 2 additions & 2 deletions src/windows/GuildHeader.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#include "GuildHeader.hpp"
#include "PinnedMessageViewer.hpp"
#include "PinList.hpp"

#define GUILD_HEADER_COLOR COLOR_ACTIVECAPTION
#define GUILD_HEADER_COLOR_2 COLOR_GRADIENTACTIVECAPTION
Expand Down Expand Up @@ -515,7 +515,7 @@ LRESULT CALLBACK GuildHeader::WndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARA
pThis->m_buttons[lParam].m_rect.bottom
};
ClientToScreen(hWnd, &pt);
PmvCreate(pChan->m_snowflake, pGuild->m_snowflake, pt.x, pt.y, true);
PinList::Show(pChan->m_snowflake, pGuild->m_snowflake, pt.x, pt.y, true);
break;
}
case IDTB_MEMBERS:
Expand Down
6 changes: 4 additions & 2 deletions src/windows/Main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
#include "ImageLoader.hpp"
#include "ProfilePopout.hpp"
#include "ImageViewer.hpp"
#include "PinnedMessageViewer.hpp"
#include "PinList.hpp"
#include "MemberList.hpp"
#include "LogonDialog.hpp"
#include "QRCodeDialog.hpp"
Expand Down Expand Up @@ -609,6 +609,7 @@ LRESULT CALLBACK WindowProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
Snowflake sf = *(Snowflake*) lParam;
g_pMessageList->OnUpdateEmoji(sf);
PinList::OnUpdateEmoji(sf);
break;
}
case WM_UPDATEUSER:
Expand All @@ -617,6 +618,7 @@ LRESULT CALLBACK WindowProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
g_pMessageList->OnUpdateAvatar(sf);
g_pMemberList->OnUpdateAvatar(sf);
g_pChannelView->OnUpdateAvatar(sf);
PinList::OnUpdateAvatar(sf);

if (ProfilePopout::GetUser() == sf)
ProfilePopout::Update();
Expand Down Expand Up @@ -649,7 +651,7 @@ LRESULT CALLBACK WindowProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
ImagePlace pl = *(ImagePlace*)lParam;
g_pMessageList->OnUpdateEmbed(pl.key);

PinList::OnUpdateEmbed(pl.key);
break;
}
case WM_REPAINTPROFILE:
Expand Down
179 changes: 179 additions & 0 deletions src/windows/PinList.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,179 @@
#include <nlohmann/json.h>
#include "PinList.hpp"
#include "MessageList.hpp"

#define DM_PINNED_MESSAGES_CLASS TEXT("DMPinnedMessageListClass");

Snowflake PinList::m_channel, PinList::m_guild;
MessageList* PinList::m_pMessageList;
PinnedMap PinList::m_map;
POINT PinList::m_appearXY;
bool PinList::m_bRightJustify;
bool PinList::m_bActive;

bool PinList::IsActive()
{
return m_bActive;
}

void PinList::AddMessage(Snowflake channelID, const Message& msg)
{
m_map[channelID].push_back(msg);

if (m_channel == channelID && m_pMessageList)
m_pMessageList->AddMessage(msg);
}

void PinList::Initialize(HWND hWnd)
{
Channel* pChan = GetDiscordInstance()->GetChannelGlobally(m_channel);
if (!pChan) {
EndDialog(hWnd, 0);
return;
}

char buff[4096];
snprintf(buff, _countof(buff), TmGetString(IDS_PINNED_MESSAGES_IN).c_str(), pChan->m_name.c_str());
TCHAR* tchr = ConvertCppStringToTString(buff);
SetWindowText(hWnd, tchr);
free(tchr);

// Move the window
RECT rect{};
GetWindowRect(hWnd, &rect);
int width = rect.right - rect.left;
int height = rect.bottom - rect.top;
int xPos = m_appearXY.x;
int yPos = m_appearXY.y;
if (m_bRightJustify) {
xPos -= width;
}
MoveWindow(hWnd, xPos, yPos, width, height, false);

HWND child = GetDlgItem(hWnd, IDC_MESSAGE_LIST);

GetWindowRect(child, &rect);
ScreenToClientRect(hWnd, &rect);
DestroyWindow(child);

SAFE_DELETE(m_pMessageList);

if (m_map[m_channel].empty()) {
Message msg;
msg.m_type = MessageType::LOADING_PINNED_MESSAGES;
msg.m_author = TmGetString(IDS_PLEASE_WAIT);
msg.m_snowflake = 1;
AddMessage(m_channel, msg);

GetDiscordInstance()->RequestPinnedMessages(m_channel);
}

m_pMessageList = MessageList::Create(hWnd, &rect);
m_pMessageList->SetManagedByOwner(true);
m_pMessageList->SetTopDown(true);
m_pMessageList->SetGuild(m_guild);
m_pMessageList->SetChannel(m_channel);

for (auto& msg : m_map[m_channel])
m_pMessageList->AddMessage(msg);

m_bActive = true;
}

void PinList::OnLoadedPins(Snowflake channelID, const std::string& data)
{
nlohmann::json j = nlohmann::json::parse(data);

if (!j.is_array()) {
assert(!"uh oh");
return;
}

m_map[m_channel].clear();
if (m_channel == channelID)
m_pMessageList->ClearMessages();

if (j.empty()) {
Message msg;
msg.m_author = " ";
msg.m_type = MessageType::NO_PINNED_MESSAGES;
msg.m_anchor = 1;
msg.m_snowflake = 1;
AddMessage(channelID, msg);
}
else
{
for (auto& msgo : j)
{
Message msg;
msg.Load(msgo, m_guild);
msg.m_anchor = 1;
AddMessage(channelID, msg);
}
}

// HACK
m_pMessageList->Repaint();
}

void PinList::OnUpdateEmbed(const std::string& key)
{
if (m_pMessageList)
m_pMessageList->OnUpdateEmbed(key);
}

void PinList::OnUpdateAvatar(Snowflake key)
{
if (m_pMessageList)
m_pMessageList->OnUpdateAvatar(key);
}

void PinList::OnUpdateEmoji(Snowflake key)
{
if (m_pMessageList)
m_pMessageList->OnUpdateEmoji(key);
}

void PinList::OnClickMessage(Snowflake sf)
{
GetDiscordInstance()->JumpToMessage(m_guild, m_channel, sf);
}

BOOL CALLBACK PinList::DlgProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
switch (uMsg)
{
case WM_INITDIALOG:
Initialize(hWnd);
return TRUE;

case WM_COMMAND:
if (wParam == IDCANCEL) {
EndDialog(hWnd, 0);
return TRUE;
}
break;

case WM_CLICKEDMESSAGE:
EndDialog(hWnd, 0);
OnClickMessage(*(Snowflake*) lParam);
return TRUE;

case WM_DESTROY:
SAFE_DELETE(m_pMessageList);
m_bActive = false;
break;
}

return FALSE;
}

void PinList::Show(Snowflake channelID, Snowflake guildID, int x, int y, bool rightJustify)
{
m_channel = channelID;
m_guild = guildID;
m_appearXY = { x, y };
m_bRightJustify = rightJustify;

DialogBox(g_hInstance, MAKEINTRESOURCE(IDD_DIALOG_PINNEDMESSAGES), g_Hwnd, &DlgProc);
}
40 changes: 40 additions & 0 deletions src/windows/PinList.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
#pragma once
#define WIN32_LEAN_AND_MEAN
#include <string>
#include <windows.h>
#include "../discord/Snowflake.hpp"
#include "../discord/Message.hpp"

// GET Request: discordapi/channels/$CHANNEL_ID/pins
// RESPONSE: Array of message objects

class MessageList;

typedef std::map <Snowflake, std::vector<Message> > PinnedMap;

class PinList
{
public:
static void Initialize(HWND hWnd);
static void OnLoadedPins(Snowflake channelID, const std::string& data);
static void OnUpdateEmbed(const std::string& key);
static void OnUpdateAvatar(Snowflake key);
static void OnUpdateEmoji(Snowflake key);
static bool IsActive();
static void Show(Snowflake channelID, Snowflake guildID, int x, int y, bool rightJustify = false);

protected:
friend class MessageList;
static void OnClickMessage(Snowflake sf);

private:
static void AddMessage(Snowflake channelID, const Message& msg);
static BOOL CALLBACK DlgProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam);

private:
static Snowflake m_channel, m_guild;
static MessageList* m_pMessageList;
static PinnedMap m_map;
static POINT m_appearXY;
static bool m_bActive, m_bRightJustify;
};
Loading

0 comments on commit a5e0afb

Please sign in to comment.