Skip to content

Commit

Permalink
* Add welcome header.
Browse files Browse the repository at this point in the history
Closes #86.

* More fixes to the message list.
  • Loading branch information
iProgramMC committed May 29, 2024
1 parent b634cc3 commit 5a291fa
Show file tree
Hide file tree
Showing 17 changed files with 163 additions and 13 deletions.
Binary file added res/icon_header_1.ico
Binary file not shown.
Binary file added res/icon_header_2.ico
Binary file not shown.
Binary file added res/icon_header_3.ico
Binary file not shown.
Binary file added res/icon_header_4.ico
Binary file not shown.
Binary file added res/icon_header_5.ico
Binary file not shown.
10 changes: 7 additions & 3 deletions src/discord/DiscordInstance.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -659,6 +659,10 @@ void DiscordInstance::HandleRequest(void* pRequestPtr)
}
case MESSAGES:
{
Channel* pChan = GetDiscordInstance()->GetChannel(pRequest->key);
if (!pChan)
break;

ScrollDir::eScrollDir sd = ScrollDir::BEFORE;
switch (pRequest->additional_data[0]) {
case /*b*/'e': sd = ScrollDir::BEFORE; break;
Expand All @@ -667,7 +671,7 @@ void DiscordInstance::HandleRequest(void* pRequestPtr)
}
DbgPrintF("Processing request %d (%c)", sd, pRequest->additional_data[0]);
uint64_t ts = GetTimeUs();
GetMessageCache()->ProcessRequest(pRequest->key, sd, (Snowflake)GetIntFromString(pRequest->additional_data.substr(1)), j);
GetMessageCache()->ProcessRequest(pRequest->key, sd, (Snowflake)GetIntFromString(pRequest->additional_data.substr(1)), j, pChan->m_name);
uint64_t te = GetTimeUs();
DbgPrintF("Total process took %lld us", te - ts);

Expand Down Expand Up @@ -1147,8 +1151,8 @@ void DiscordInstance::JumpToMessage(Snowflake guild, Snowflake channel, Snowflak
if (m_CurrentChannel != channel) {
OnSelectChannel(channel);
}

GetFrontend()->JumpToMessage(message);
if (message)
GetFrontend()->JumpToMessage(message);
}

void DiscordInstance::LaunchURL(const std::string& url)
Expand Down
23 changes: 18 additions & 5 deletions src/discord/MessageCache.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
#include "Frontend.hpp"
#include "DiscordInstance.hpp"

constexpr int MESSAGES_PER_REQUEST = 50;

using nlohmann::json;
static MessageCache g_MCSingleton;

Expand All @@ -19,10 +21,10 @@ void MessageCache::GetLoadedMessages(Snowflake channel, Snowflake guild, std::li
out.push_back(msg.second);
}

void MessageCache::ProcessRequest(Snowflake channel, ScrollDir::eScrollDir sd, Snowflake anchor, nlohmann::json& j)
void MessageCache::ProcessRequest(Snowflake channel, ScrollDir::eScrollDir sd, Snowflake anchor, nlohmann::json& j, const std::string& channelName)
{
MessageChunkList& lst = m_mapMessages[channel];
lst.ProcessRequest(sd, anchor, j);
lst.ProcessRequest(sd, anchor, j, channelName);
}

void MessageCache::AddMessage(Snowflake channel, const Message& msg)
Expand Down Expand Up @@ -74,7 +76,7 @@ MessageChunkList::MessageChunkList()
m_messages[msg.m_snowflake] = msg;
}

void MessageChunkList::ProcessRequest(ScrollDir::eScrollDir sd, Snowflake gap, json& j)
void MessageChunkList::ProcessRequest(ScrollDir::eScrollDir sd, Snowflake gap, json& j, const std::string& channelName)
{
Snowflake lowestMsg = (Snowflake) -1LL, highestMsg = 0;

Expand All @@ -89,6 +91,7 @@ void MessageChunkList::ProcessRequest(ScrollDir::eScrollDir sd, Snowflake gap, j
}

// for each message
int receivedMessages = 0;
for (json& data : j)
{
Message msg;
Expand All @@ -101,22 +104,32 @@ void MessageChunkList::ProcessRequest(ScrollDir::eScrollDir sd, Snowflake gap, j
}

m_messages[msg.m_snowflake] = msg;
receivedMessages++;

if (lowestMsg > msg.m_snowflake)
lowestMsg = msg.m_snowflake;
if (highestMsg < msg.m_snowflake)
highestMsg = msg.m_snowflake;
}

bool addBefore = sd != ScrollDir::AFTER;
bool addBefore = sd != ScrollDir::AFTER && receivedMessages >= MESSAGES_PER_REQUEST;
bool addAfter = sd != ScrollDir::BEFORE;

Message msg;
msg.m_author = GetFrontend()->GetPleaseWaitText();
msg.m_author = "";
msg.m_message = "";
msg.m_dateFull = "";
msg.m_dateCompact = "";

if (receivedMessages < MESSAGES_PER_REQUEST)
{
msg.m_type = MessageType::CHANNEL_HEADER;
msg.m_snowflake = 1;
msg.m_author = "#" + channelName;
m_messages[msg.m_snowflake] = msg;
}

msg.m_author = GetFrontend()->GetPleaseWaitText();
if (addBefore && addedMessages)
{
msg.m_type = MessageType::GAP_UP;
Expand Down
4 changes: 2 additions & 2 deletions src/discord/MessageCache.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ struct MessageChunkList
Snowflake m_guild = 0;

MessageChunkList();
void ProcessRequest(ScrollDir::eScrollDir sd, Snowflake anchor, nlohmann::json& j);
void ProcessRequest(ScrollDir::eScrollDir sd, Snowflake anchor, nlohmann::json& j, const std::string& channelName);
void AddMessage(const Message& msg);
void EditMessage(const Message& msg);
void DeleteMessage(Snowflake message);
Expand All @@ -32,7 +32,7 @@ class MessageCache
void GetLoadedMessages(Snowflake channel, Snowflake guild, std::list<Message>& out);

// note: scroll dir used to add gap message
void ProcessRequest(Snowflake channel, ScrollDir::eScrollDir sd, Snowflake anchor, nlohmann::json& j);
void ProcessRequest(Snowflake channel, ScrollDir::eScrollDir sd, Snowflake anchor, nlohmann::json& j, const std::string& channelName);

void AddMessage(Snowflake channel, const Message& msg);
void EditMessage(Snowflake channel, const Message& msg);
Expand Down
1 change: 1 addition & 0 deletions src/discord/MessageType.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,5 +44,6 @@ namespace MessageType
LOADING_PINNED_MESSAGES,
UNSENT_MESSAGE,
SENDING_MESSAGE,
CHANNEL_HEADER,
};
}
1 change: 1 addition & 0 deletions src/discord/Util.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ std::string FormatTimeLong(time_t time, bool relativity = false); // relativity=
std::string FormatTimeShort(time_t time);
std::string FormatTimeShorter(time_t time);
void SplitURL(const std::string& url, std::string& domainOut, std::string& resourceOut);
std::string CreateChannelLink(Snowflake guild, Snowflake channel);
std::string CreateMessageLink(Snowflake guild, Snowflake channel, Snowflake message);
float CompareFuzzy(const std::string& item, const char* query); // returns a "closeness" factor, =0 if no match, >0 if match. The closeness is used for sorting matches
float GetAppVersion();
Expand Down
8 changes: 6 additions & 2 deletions src/resource.h
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,11 @@
#define IDI_SHIFT_RIGHT 81
#define IDI_SHIFT_RIGHT_2K 82
#define IDI_ICON_2K 83
#define IDI_HEADER_1 84
#define IDI_HEADER_2 85
#define IDI_HEADER_3 86
#define IDI_HEADER_4 87
#define IDI_HEADER_5 88
#define IDB_TARGET 200
#define IDB_CHANNEL 201
#define IDB_CATEGORY 202
Expand Down Expand Up @@ -292,7 +297,6 @@
#define IDC_ENABLE_TLS_CHECKS 871
#define IDC_CHECK_UPDATES 872
#define IDC_EDIT_ABOUTME 873
#define IDC_CHECK1 874
#define IDC_DISABLE_FORMATTING 874
#define ID_FILE_PREFERENCES 1001
#define ID_FILE_STOPALLSPEECH 1002
Expand Down Expand Up @@ -370,7 +374,7 @@
//
#ifdef APSTUDIO_INVOKED
#ifndef APSTUDIO_READONLY_SYMBOLS
#define _APS_NEXT_RESOURCE_VALUE 84
#define _APS_NEXT_RESOURCE_VALUE 89
#define _APS_NEXT_COMMAND_VALUE 1069
#define _APS_NEXT_CONTROL_VALUE 875
#define _APS_NEXT_SYMED_VALUE 40000
Expand Down
10 changes: 10 additions & 0 deletions src/resource.rc
Original file line number Diff line number Diff line change
Expand Up @@ -372,6 +372,16 @@ IDI_SHIFT_RIGHT_2K ICON "../res/icon_shift_right_2k.ico"

IDI_ICON_2K ICON "../res/icon_discord_2k.ico"

IDI_HEADER_1 ICON "../res/icon_header_1.ico"

IDI_HEADER_2 ICON "../res/icon_header_2.ico"

IDI_HEADER_3 ICON "../res/icon_header_3.ico"

IDI_HEADER_4 ICON "../res/icon_header_4.ico"

IDI_HEADER_5 ICON "../res/icon_header_5.ico"


/////////////////////////////////////////////////////////////////////////////
//
Expand Down
89 changes: 88 additions & 1 deletion src/windows/MessageList.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1514,6 +1514,24 @@ bool MessageList::IsActionMessage(MessageType::eType msgType)
case MessageType::CANT_VIEW_MSG_HISTORY:
case MessageType::LOADING_PINNED_MESSAGES:
case MessageType::NO_PINNED_MESSAGES:
case MessageType::CHANNEL_HEADER:
return true;
}

return false;
}

bool MessageList::IsClientSideMessage(MessageType::eType msgType)
{
switch (msgType)
{
case MessageType::GAP_UP:
case MessageType::GAP_DOWN:
case MessageType::GAP_AROUND:
case MessageType::CANT_VIEW_MSG_HISTORY:
case MessageType::LOADING_PINNED_MESSAGES:
case MessageType::NO_PINNED_MESSAGES:
case MessageType::CHANNEL_HEADER:
return true;
}

Expand All @@ -1523,6 +1541,7 @@ bool MessageList::IsActionMessage(MessageType::eType msgType)
// This is a horrible mess!
void MessageList::DetermineMessageData(
Snowflake guildID, /* IN */
Snowflake channelID, /* IN */
MessageType::eType msgType, /* IN */
Snowflake id, /* IN */
Snowflake authorId, /* IN */
Expand Down Expand Up @@ -1584,6 +1603,19 @@ void MessageList::DetermineMessageData(
break;
}

case MessageType::CHANNEL_HEADER:
{
Channel* pChan = GetDiscordInstance()->GetChannel(channelID);
if (!pChan) {
messagePart1 = TEXT("Unknown channel");
break;
}

messagePart1 = TEXT("Welcome to the beginning of the ");
messagePart2 = TEXT(" channel.");
break;
}

case MessageType::CHANNEL_PINNED_MESSAGE:
{
messagePart1 = TEXT("");
Expand Down Expand Up @@ -1842,6 +1874,7 @@ int MessageList::DrawMessageReply(HDC hdc, MessageItem& item, RECT& rc)
if (isActionMessage)
DetermineMessageData(
m_guildID,
m_channelID,
refMsg.m_type,
refMsg.m_snowflake,
refMsg.m_author_snowflake,
Expand Down Expand Up @@ -2045,7 +2078,12 @@ void MessageList::DrawMessage(HDC hdc, MessageItem& item, RECT& msgRect, RECT& c
if (!isFlashed)
bkgdColor = GetSysColor(COLOR_3DFACE);

DrawEdge(hdc, &rect2, BDR_RAISED, edgeFlags);
if (item.m_msg.m_type != MessageType::CHANNEL_HEADER) {
DrawEdge(hdc, &rect2, BDR_RAISED, edgeFlags);
}
else if (edgeFlags & BF_MIDDLE) {
FillRect(hdc, &rect2, GetSysColorBrush(COLOR_3DFACE));
}
break;
}
case MS_FLAT: {
Expand Down Expand Up @@ -2079,6 +2117,14 @@ void MessageList::DrawMessage(HDC hdc, MessageItem& item, RECT& msgRect, RECT& c
swapped = true;
}

// note: Intentionally perform the swap again
if (item.m_msg.m_type == MessageType::CHANNEL_HEADER) {
COLORREF tmp = c1;
c1 = c2;
c2 = tmp;
swapped = !swapped;
}

bkgdColor = c2;

if (isChainCont) {
Expand Down Expand Up @@ -2132,6 +2178,7 @@ void MessageList::DrawMessage(HDC hdc, MessageItem& item, RECT& msgRect, RECT& c
if (isActionMessage)
DetermineMessageData(
m_guildID,
m_channelID,
item.m_msg.m_type,
item.m_msg.m_snowflake,
item.m_msg.m_author_snowflake,
Expand All @@ -2156,11 +2203,42 @@ void MessageList::DrawMessage(HDC hdc, MessageItem& item, RECT& msgRect, RECT& c
int dateOffset = 0;
int sizePart2 = 0, sizeClick = 0, sizePart3 = 0;
int actionIconSize = ScaleByDPI(16);
int oldMode = 0;
bool restoreOldMode = false;

if (isActionMessage)
{
if (item.m_msg.m_type == MessageType::CHANNEL_HEADER && Supports32BitIcons() && GetDeviceCaps(hdc, BITSPIXEL) >= 16)
{
restoreOldMode = true;
oldMode = SetBkMode(hdc, TRANSPARENT);

HICON hics[5];
int sz = ScaleByDPI(64);

for (int i = 0; i < 5; i++) {
hics[i] = (HICON) LoadImage(g_hInstance, MAKEINTRESOURCE(IDI_HEADER_1 + i), IMAGE_ICON, sz, sz, LR_SHARED | LR_CREATEDIBSECTION);
}

// Render on the left side.
int x = 0;
for (int i = 0; i < 2; i++) {
DrawIconEx(hdc, x, rc.top, hics[i], sz, sz, 0, NULL, DI_NORMAL | DI_COMPAT);
x += sz;
}

x = rc.right;
for (int i = 4; i >= 2; i--) {
x -= sz;
DrawIconEx(hdc, x, rc.top, hics[i], sz, sz, 0, NULL, DI_NORMAL | DI_COMPAT);
}

rc.top = rc.bottom - item.m_authHeight;
}

SelectObject(hdc, g_MessageTextFont);
RECT rca = rc;

RECT rcMeasure;
rca.right -= ScaleByDPI(75); // TODO: Figure out why I have to do this.
rcMeasure = rca;
Expand Down Expand Up @@ -2489,6 +2567,9 @@ void MessageList::DrawMessage(HDC hdc, MessageItem& item, RECT& msgRect, RECT& c
}
}

if (restoreOldMode)
SetBkMode(hdc, oldMode);

// draw available embeds, if any:
RECT embedRect = item.m_messageRect;
auto& embedVec = item.m_embedData;
Expand Down Expand Up @@ -2890,6 +2971,8 @@ LRESULT CALLBACK MessageList::WndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARA

if (!pRCMsg) break;

if (IsClientSideMessage(pRCMsg->m_msg.m_type)) break;

HMENU menu = GetSubMenu(LoadMenu(g_hInstance, MAKEINTRESOURCE(IDR_MESSAGE_CONTEXT)), 0);

pThis->m_rightClickedMessage = pRCMsg->m_msg.m_snowflake;
Expand Down Expand Up @@ -3699,6 +3782,10 @@ void MessageList::AdjustHeightInfo(const MessageItem& msg, int& height, int& tex

if (!IsActionMessage(msg.m_msg.m_type) && !IsCompact() && !isChainCont && height < minHeight)
height = minHeight;

if (msg.m_msg.m_type == MessageType::CHANNEL_HEADER) {
height = ScaleByDPI(64);
}
}

bool MessageList::ShouldBeDateGap(time_t oldTime, time_t newTime)
Expand Down
2 changes: 2 additions & 0 deletions src/windows/MessageList.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -481,6 +481,7 @@ class MessageList
protected:
friend class MessageItem;
static bool IsActionMessage(MessageType::eType msgType);
static bool IsClientSideMessage(MessageType::eType msgType);

private:
void HitTestAuthor(POINT pt, BOOL& hit);
Expand Down Expand Up @@ -511,6 +512,7 @@ class MessageList
// This is a horrible mess!
static void DetermineMessageData(
Snowflake guildID, /* IN */
Snowflake channelID, /* IN */
MessageType::eType msgType, /* IN */
Snowflake id, /* IN */
Snowflake authorId, /* IN */
Expand Down
8 changes: 8 additions & 0 deletions src/windows/WinUtils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,14 @@ std::string MakeStringFromUnicodeString(LPCWSTR wstr)
return final_str;
}

std::string CreateChannelLink(Snowflake guild, Snowflake channel)
{
std::string guildStr = std::to_string(guild);
if (!guild) guildStr = "@me";

return "https://discord.com/channels/" + guildStr + "/" + std::to_string(channel);
}

std::string CreateMessageLink(Snowflake guild, Snowflake channel, Snowflake message)
{
std::string guildStr = std::to_string(guild);
Expand Down
5 changes: 5 additions & 0 deletions vs/DiscordMessenger.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,11 @@
<Image Include="..\res\icon_file_2k.ico" />
<Image Include="..\res\icon_groupdm.ico" />
<Image Include="..\res\icon_groupdm_2k.ico" />
<Image Include="..\res\icon_header_1.ico" />
<Image Include="..\res\icon_header_2.ico" />
<Image Include="..\res\icon_header_3.ico" />
<Image Include="..\res\icon_header_4.ico" />
<Image Include="..\res\icon_header_5.ico" />
<Image Include="..\res\icon_image_error.ico" />
<Image Include="..\res\icon_jump.ico" />
<Image Include="..\res\icon_jump_2k.ico" />
Expand Down
Loading

0 comments on commit 5a291fa

Please sign in to comment.