Skip to content

Commit

Permalink
* Add attachment and embed settings.
Browse files Browse the repository at this point in the history
  • Loading branch information
iProgramMC committed Jun 17, 2024
1 parent e94f165 commit 3a91020
Show file tree
Hide file tree
Showing 6 changed files with 111 additions and 36 deletions.
2 changes: 1 addition & 1 deletion src/discord/Attachment.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ class Attachment
return m_previewWidth != m_width || m_previewHeight != m_height;
}

bool IsImage()
bool IsImage() const
{
return
m_contentType == ContentType::PNG ||
Expand Down
12 changes: 12 additions & 0 deletions src/discord/LocalSettings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,15 @@ bool LocalSettings::Load()
if (j.contains("UserScale"))
m_userScale = int(j["UserScale"]);

if (j.contains("ShowAttachmentImages"))
m_bShowAttachmentImages = j["ShowAttachmentImages"];

if (j.contains("ShowEmbedImages"))
m_bShowEmbedImages = j["ShowEmbedImages"];

if (j.contains("ShowEmbedContent"))
m_bShowEmbedContent = j["ShowEmbedContent"];

if (m_bSaveWindowSize)
{
if (j.contains("WindowWidth"))
Expand Down Expand Up @@ -148,6 +157,9 @@ bool LocalSettings::Save()
j["WatermarkAlignment"] = int(m_imageAlignment);
j["UserScale"] = m_userScale;
j["AddExtraHeaders"] = m_bAddExtraHeaders;
j["ShowAttachmentImages"] = m_bShowAttachmentImages;
j["ShowEmbedImages"] = m_bShowEmbedImages;
j["ShowEmbedContent"] = m_bShowEmbedContent;

if (m_bSaveWindowSize) {
j["WindowWidth"] = m_width;
Expand Down
21 changes: 21 additions & 0 deletions src/discord/LocalSettings.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,24 @@ class LocalSettings
void SetCompactMemberList(bool b) {
m_bCompactMemberList = b;
}
bool ShowAttachmentImages() const {
return m_bShowAttachmentImages;
}
bool ShowEmbedImages() const {
return m_bShowEmbedImages;
}
bool ShowEmbedContent() const {
return m_bShowEmbedContent;
}
void SetShowAttachmentImages(bool b) {
m_bShowAttachmentImages = b;
}
void SetShowEmbedImages(bool b) {
m_bShowEmbedImages = b;
}
void SetShowEmbedContent(bool b) {
m_bShowEmbedContent = b;
}

private:
std::string m_token;
Expand All @@ -171,6 +189,9 @@ class LocalSettings
bool m_bShowScrollBarOnGuildList = false;
bool m_bCompactMemberList = false;
bool m_bAddExtraHeaders = true;
bool m_bShowAttachmentImages = true;
bool m_bShowEmbedImages = true;
bool m_bShowEmbedContent = true;
time_t m_remindUpdatesOn = 0;
int m_width = 1000;
int m_height = 700;
Expand Down
6 changes: 3 additions & 3 deletions src/resource.rc
Original file line number Diff line number Diff line change
Expand Up @@ -667,12 +667,12 @@ BEGIN
"Button",BS_AUTOCHECKBOX | BS_TOP | BS_MULTILINE | WS_TABSTOP,12,18,236,18
GROUPBOX "Display Images",IDC_STATIC,6,43,248,41
CONTROL "When uploaded directly to Discord",IDC_IMAGES_WHEN_UPLOADED,
"Button",BS_AUTOCHECKBOX | WS_DISABLED | WS_TABSTOP,12,55,236,10
"Button",BS_AUTOCHECKBOX | WS_TABSTOP,12,55,236,10
CONTROL "When posted as links in chat",IDC_IMAGES_WHEN_EMBEDDED,
"Button",BS_AUTOCHECKBOX | WS_DISABLED | WS_TABSTOP,12,68,236,10
"Button",BS_AUTOCHECKBOX | WS_TABSTOP,12,68,236,10
GROUPBOX "Embed Settings",IDC_STATIC,6,90,248,27
CONTROL "Show embeds and preview links in chat",IDC_SHOW_EMBEDS,
"Button",BS_AUTOCHECKBOX | WS_DISABLED | WS_TABSTOP,12,101,236,10
"Button",BS_AUTOCHECKBOX | WS_TABSTOP,12,101,236,10
END

IDD_DIALOG_WINDOWSETTINGS DIALOGEX 0, 0, 260, 242
Expand Down
87 changes: 55 additions & 32 deletions src/windows/MessageList.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -227,10 +227,18 @@ void RichEmbedItem::Measure(HDC hdc, RECT& messageRect, bool isCompact)
int maxImgWidth = std::max(int(rcItem.right - rcItem.left), 10);

// Calculate the embedded image.
if (m_pEmbed->m_bHasThumbnail)
m_thumbnailSize = EnsureMaximumSize(m_pEmbed->m_thumbnailWidth, m_pEmbed->m_thumbnailHeight, maxImgWidth, maxHeight);
else if (m_pEmbed->m_bHasImage)
m_imageSize = EnsureMaximumSize(m_pEmbed->m_imageWidth, m_pEmbed->m_imageHeight, maxImgWidth, maxHeight);
if (GetLocalSettings()->ShowEmbedImages())
{
if (m_pEmbed->m_bHasThumbnail)
m_thumbnailSize = EnsureMaximumSize(m_pEmbed->m_thumbnailWidth, m_pEmbed->m_thumbnailHeight, maxImgWidth, maxHeight);
else if (m_pEmbed->m_bHasImage)
m_imageSize = EnsureMaximumSize(m_pEmbed->m_imageWidth, m_pEmbed->m_imageHeight, maxImgWidth, maxHeight);
}
else
{
m_thumbnailSize = { 0, 0 };
m_imageSize = { 0, 0 };
}

sz.cx = std::max({ sz.cx, m_imageSize.cx, m_thumbnailSize.cx });
sz.cy = m_providerSize.cy;
Expand Down Expand Up @@ -330,25 +338,27 @@ void RichEmbedItem::Draw(HDC hdc, RECT& messageRect, MessageList* pList)
DrawText(hdc, m_description.GetWrapped(), -1, &rcText, DT_WORDBREAK | DT_WORD_ELLIPSIS);
sizeY += m_descriptionSize.cy;
}
if (m_thumbnailSize.cy) {
m_thumbnailResourceID = GetAvatarCache()->MakeIdentifier(m_pEmbed->m_thumbnailUrl);
GetAvatarCache()->AddImagePlace(m_thumbnailResourceID, eImagePlace::ATTACHMENTS, m_pEmbed->m_thumbnailProxiedUrl);
bool hasAlpha = false;
HBITMAP hbm = GetAvatarCache()->GetBitmapSpecial(m_pEmbed->m_thumbnailUrl, hasAlpha);
if (sizeY) sizeY += gap;
m_thumbnailRect = { rc.left, rc.top + sizeY, rc.left + m_thumbnailSize.cx, rc.top + sizeY + m_thumbnailSize.cy };
DrawImageSpecial(hdc, hbm, m_thumbnailRect, hasAlpha);
sizeY += m_thumbnailSize.cy;
}
if (m_imageSize.cy) {
m_imageResourceID = GetAvatarCache()->MakeIdentifier(m_pEmbed->m_imageUrl);
GetAvatarCache()->AddImagePlace(m_imageResourceID, eImagePlace::ATTACHMENTS, m_pEmbed->m_imageProxiedUrl);
bool hasAlpha = false;
HBITMAP hbm = GetAvatarCache()->GetBitmapSpecial(m_pEmbed->m_imageUrl, hasAlpha);
if (sizeY) sizeY += gap;
m_imageRect = { rc.left, rc.top + sizeY, rc.left + m_imageSize.cx, rc.top + sizeY + m_imageSize.cy };
DrawImageSpecial(hdc, hbm, m_imageRect, hasAlpha);
sizeY += m_imageSize.cy;
if (GetLocalSettings()->ShowEmbedImages()) {
if (m_thumbnailSize.cy) {
m_thumbnailResourceID = GetAvatarCache()->MakeIdentifier(m_pEmbed->m_thumbnailUrl);
GetAvatarCache()->AddImagePlace(m_thumbnailResourceID, eImagePlace::ATTACHMENTS, m_pEmbed->m_thumbnailProxiedUrl);
bool hasAlpha = false;
HBITMAP hbm = GetAvatarCache()->GetBitmapSpecial(m_pEmbed->m_thumbnailUrl, hasAlpha);
if (sizeY) sizeY += gap;
m_thumbnailRect = { rc.left, rc.top + sizeY, rc.left + m_thumbnailSize.cx, rc.top + sizeY + m_thumbnailSize.cy };
DrawImageSpecial(hdc, hbm, m_thumbnailRect, hasAlpha);
sizeY += m_thumbnailSize.cy;
}
if (m_imageSize.cy) {
m_imageResourceID = GetAvatarCache()->MakeIdentifier(m_pEmbed->m_imageUrl);
GetAvatarCache()->AddImagePlace(m_imageResourceID, eImagePlace::ATTACHMENTS, m_pEmbed->m_imageProxiedUrl);
bool hasAlpha = false;
HBITMAP hbm = GetAvatarCache()->GetBitmapSpecial(m_pEmbed->m_imageUrl, hasAlpha);
if (sizeY) sizeY += gap;
m_imageRect = { rc.left, rc.top + sizeY, rc.left + m_imageSize.cx, rc.top + sizeY + m_imageSize.cy };
DrawImageSpecial(hdc, hbm, m_imageRect, hasAlpha);
sizeY += m_imageSize.cy;
}
}
if (m_footerSize.cy) {
SelectObject(hdc, g_ReplyTextFont);
Expand Down Expand Up @@ -2684,6 +2694,9 @@ void MessageList::DrawMessage(HDC hdc, MessageItem& item, RECT& msgRect, RECT& c
auto& embedVec = item.m_embedData;
size_t sz = embedVec.size();

if (!GetLocalSettings()->ShowEmbedContent())
sz = 0;

embedRect.right = msgRect.right - ScaleByDPI(10);
embedRect.top = embedRect.bottom;

Expand Down Expand Up @@ -2745,8 +2758,12 @@ void MessageList::DrawMessage(HDC hdc, MessageItem& item, RECT& msgRect, RECT& c
case ContentType::JPEG:
case ContentType::WEBP:
{
DrawImageAttachment(hdc, paintRect, attachItem, attachRect);
break;
if (GetLocalSettings()->ShowAttachmentImages())
{
DrawImageAttachment(hdc, paintRect, attachItem, attachRect);
break;
}
// FALLTHROUGH
}
default:
{
Expand Down Expand Up @@ -4079,11 +4096,14 @@ void MessageList::AdjustHeightInfo(const MessageItem& msg, int& height, int& tex

// also figure out embed size
embedheight = 0;
for (auto& emb : msg.m_embedData)
if (GetLocalSettings()->ShowEmbedContent())
{
int inc = emb.m_size.cy + ScaleByDPI(5);
height += inc;
embedheight += inc;
for (auto& emb : msg.m_embedData)
{
int inc = emb.m_size.cy + ScaleByDPI(5);
height += inc;
embedheight += inc;
}
}

// also figure out attachment size
Expand All @@ -4092,7 +4112,7 @@ void MessageList::AdjustHeightInfo(const MessageItem& msg, int& height, int& tex
{
// XXX improve?
int inc = 0;
if (att.m_contentType == ContentType::BLOB)
if (!GetLocalSettings()->ShowAttachmentImages() || !att.IsImage())
inc = ATTACHMENT_HEIGHT + ATTACHMENT_GAP;
else
inc = att.m_previewHeight + ATTACHMENT_GAP;
Expand Down Expand Up @@ -4370,8 +4390,11 @@ void MessageList::DetermineMessageMeasurements(MessageItem& mi, HDC _hdc, LPRECT
);

const bool isCompact = IsCompact();
for (auto& embed : mi.m_embedData)
embed.Measure(hdc, rect, isCompact);

if (GetLocalSettings()->ShowEmbedContent()) {
for (auto& embed : mi.m_embedData)
embed.Measure(hdc, rect, isCompact);
}

AdjustHeightInfo(mi, mi.m_height, mi.m_textHeight, mi.m_authHeight, mi.m_replyHeight, mi.m_attachHeight, mi.m_embedHeight);

Expand Down
19 changes: 19 additions & 0 deletions src/windows/OptionsDialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,13 @@ void WINAPI OnChildDialogInit(HWND hwndDlg)

break;
}
case PG_CHAT:
{
CheckDlgButton(hwndDlg, IDC_IMAGES_WHEN_UPLOADED, GetLocalSettings()->ShowAttachmentImages() ? BST_CHECKED : BST_UNCHECKED);
CheckDlgButton(hwndDlg, IDC_IMAGES_WHEN_EMBEDDED, GetLocalSettings()->ShowEmbedImages() ? BST_CHECKED : BST_UNCHECKED);
CheckDlgButton(hwndDlg, IDC_SHOW_EMBEDS, GetLocalSettings()->ShowEmbedContent() ? BST_CHECKED : BST_UNCHECKED);
break;
}
case PG_WINDOW:
{
CheckDlgButton(hwndDlg, IDC_SAVE_WINDOW_SIZE, GetLocalSettings()->GetSaveWindowSize() ? BST_CHECKED : BST_UNCHECKED);
Expand Down Expand Up @@ -439,6 +446,18 @@ INT_PTR CALLBACK ChildDialogProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lPa
GetLocalSettings()->SetDisableFormatting(IsDlgButtonChecked(hWnd, IDC_DISABLE_FORMATTING));
SendMessage(g_Hwnd, WM_RECALCMSGLIST, 0, 0);
break;
case IDC_IMAGES_WHEN_UPLOADED:
GetLocalSettings()->SetShowAttachmentImages(IsDlgButtonChecked(hWnd, IDC_IMAGES_WHEN_UPLOADED));
SendMessage(g_Hwnd, WM_RECALCMSGLIST, 0, 0);
break;
case IDC_IMAGES_WHEN_EMBEDDED:
GetLocalSettings()->SetShowEmbedImages(IsDlgButtonChecked(hWnd, IDC_IMAGES_WHEN_EMBEDDED));
SendMessage(g_Hwnd, WM_RECALCMSGLIST, 0, 0);
break;
case IDC_SHOW_EMBEDS:
GetLocalSettings()->SetShowEmbedContent(IsDlgButtonChecked(hWnd, IDC_SHOW_EMBEDS));
SendMessage(g_Hwnd, WM_RECALCMSGLIST, 0, 0);
break;
}
break;
}
Expand Down

0 comments on commit 3a91020

Please sign in to comment.