Skip to content

Commit

Permalink
* Add disable markdown option.
Browse files Browse the repository at this point in the history
This allows users of RTL scripts a way to keep their text readable, because currently the markdown engine doesn't support RTL scripts.

I wish there was a way that I didn't have to do all that to get markdown on win32.
  • Loading branch information
iProgramMC committed May 28, 2024
1 parent 0ba5ab4 commit 9ccf864
Show file tree
Hide file tree
Showing 8 changed files with 54 additions and 23 deletions.
10 changes: 7 additions & 3 deletions src/discord/FormattedText.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
#include <stack>
#include <algorithm>
#include "RectAndPoint.hpp"
#include "Util.hpp"
#include "LocalSettings.hpp"

//#define USE_STL_REGEX //-- way slower than Boost Regex

Expand Down Expand Up @@ -499,7 +499,7 @@ void FormattedText::Layout(DrawingContext* context, const Rect& rect, int offset
}

int maxWidth = 0;
if (wflags & (WORD_MLCODE | WORD_CODE)) {
if (wflags & (WORD_MLCODE | WORD_CODE | WORD_NOFORMAT)) {
maxWidth = rect.Width();
}

Expand Down Expand Up @@ -808,7 +808,6 @@ void FormattedText::TokenizeAll()
}
}


void FormattedText::SetMessage(const std::string& msg)
{
m_rawMessage = msg;
Expand All @@ -817,6 +816,11 @@ void FormattedText::SetMessage(const std::string& msg)
m_tokens.clear();
m_words.clear();

if (GetLocalSettings()->DisableFormatting()) {
m_words.push_back(Word(WORD_NOFORMAT, m_rawMessage));
return;
}

SplitBlocks();
RegexNecessary();
TokenizeAll();
Expand Down
3 changes: 2 additions & 1 deletion src/discord/FormattedText.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ struct Token
#define WORD_EMOJI (1 << 17) // Unicode emoji. Unsupported as of yet.
#define WORD_CEMOJI (1 << 18) // Custom emoji.
#define WORD_HEADER1 (1 << 19) // Header 1 style. Currently only used by emoji.
#define WORD_NOFORMAT (1 << 20) // Not actually formatted.

struct Word
{
Expand All @@ -88,7 +89,7 @@ struct Word
return true;

// If this is a multiline code block, return true.
if ((m_flags & WORD_MLCODE) &&
if ((m_flags & (WORD_MLCODE | WORD_NOFORMAT)) &&
(m_size.x > layoutRect.Width() || ((m_flags & WORD_WRAPPED) && m_size.x < layoutRect.Width())))
return true;

Expand Down
4 changes: 4 additions & 0 deletions src/discord/LocalSettings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,9 @@ bool LocalSettings::Load()
if (j.contains("EnableTLSVerification"))
m_bEnableTLSVerification = j["EnableTLSVerification"];

if (j.contains("DisableFormatting"))
m_bDisableFormatting = j["DisableFormatting"];

if (m_bSaveWindowSize)
{
if (j.contains("WindowWidth"))
Expand Down Expand Up @@ -119,6 +122,7 @@ bool LocalSettings::Save()
j["SaveWindowSize"] = m_bSaveWindowSize;
j["CheckUpdates"] = m_bCheckUpdates;
j["EnableTLSVerification"] = m_bEnableTLSVerification;
j["DisableFormatting"] = m_bDisableFormatting;
j["RemindUpdateCheckOn"] = (long long)(m_remindUpdatesOn);
if (m_bSaveWindowSize) {
j["WindowWidth"] = m_width;
Expand Down
7 changes: 7 additions & 0 deletions src/discord/LocalSettings.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,12 @@ class LocalSettings
m_bEnableTLSVerification = b;
}
void StopUpdateCheckTemporarily();
bool DisableFormatting() const {
return m_bDisableFormatting;
}
void SetDisableFormatting(bool b) {
m_bDisableFormatting = b;
}

private:
std::string m_token;
Expand All @@ -106,6 +112,7 @@ class LocalSettings
bool m_bCheckUpdates = false;
bool m_bAskToCheckUpdates = true;
bool m_bEnableTLSVerification = true;
bool m_bDisableFormatting = false;
time_t m_remindUpdatesOn = 0;
int m_width = 1000;
int m_height = 700;
Expand Down
4 changes: 3 additions & 1 deletion src/resource.h
Original file line number Diff line number Diff line change
Expand Up @@ -292,6 +292,8 @@
#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
#define ID_FILE_EXIT 1003
Expand Down Expand Up @@ -370,7 +372,7 @@
#ifndef APSTUDIO_READONLY_SYMBOLS
#define _APS_NEXT_RESOURCE_VALUE 84
#define _APS_NEXT_COMMAND_VALUE 1069
#define _APS_NEXT_CONTROL_VALUE 874
#define _APS_NEXT_CONTROL_VALUE 875
#define _APS_NEXT_SYMED_VALUE 40000
#endif
#endif
23 changes: 13 additions & 10 deletions src/resource.rc
Original file line number Diff line number Diff line change
Expand Up @@ -428,19 +428,22 @@ IDD_DIALOG_APPEARANCE DIALOGEX 0, 0, 260, 242
STYLE DS_SETFONT | DS_FIXEDSYS | WS_CHILD
FONT 8, "MS Shell Dlg", 400, 0, 0x0
BEGIN
GROUPBOX "Message Display",IDC_MDISPLAY_BOX,6,6,245,41
GROUPBOX "Message Display",IDC_MDISPLAY_BOX,6,6,245,88
CONTROL "Cozy: Modern, beautiful, and easy on your eyes.",IDC_APPEARANCE_COZY,
"Button",BS_AUTORADIOBUTTON | WS_GROUP,12,18,174,10
CONTROL "Compact: Fit more messages on screen at one time. #IRC",IDC_APPEARANCE_COMPACT,
"Button",BS_AUTORADIOBUTTON,12,30,201,10
GROUPBOX "Message Style",IDC_MSTYLE_BOX,6,54,245,66
CONTROL "3-D Frame",IDC_RADIO_3D_FRAME,"Button",BS_AUTORADIOBUTTON | WS_GROUP,12,65,49,10
CONTROL "Gradient",IDC_RADIO_GRADIENT,"Button",BS_AUTORADIOBUTTON,12,78,43,10
CONTROL "Flat Color 1",IDC_RADIO_FLAT_1,"Button",BS_AUTORADIOBUTTON,12,91,53,10
CONTROL "Flat Color 2",IDC_RADIO_FLAT_2,"Button",BS_AUTORADIOBUTTON,12,104,53,10
GROUPBOX "Window Settings",IDC_STATIC,6,127,245,38
CONTROL "Save window size",IDC_SAVE_WINDOW_SIZE,"Button",BS_AUTOCHECKBOX | WS_TABSTOP,12,138,72,10
CONTROL "Start maximized",IDC_START_MAXIMIZED,"Button",BS_AUTOCHECKBOX | WS_TABSTOP,12,150,67,10
GROUPBOX "Message Style",IDC_MSTYLE_BOX,6,95,245,66
CONTROL "3-D Frame",IDC_RADIO_3D_FRAME,"Button",BS_AUTORADIOBUTTON | WS_GROUP,12,106,49,10
CONTROL "Gradient",IDC_RADIO_GRADIENT,"Button",BS_AUTORADIOBUTTON,12,119,43,10
CONTROL "Flat Color 1",IDC_RADIO_FLAT_1,"Button",BS_AUTORADIOBUTTON,12,132,53,10
CONTROL "Flat Color 2",IDC_RADIO_FLAT_2,"Button",BS_AUTORADIOBUTTON,12,145,53,10
GROUPBOX "Window Settings",IDC_STATIC,6,161,245,38
CONTROL "Save window size",IDC_SAVE_WINDOW_SIZE,"Button",BS_AUTOCHECKBOX | WS_TABSTOP,12,172,72,10
CONTROL "Start maximized",IDC_START_MAXIMIZED,"Button",BS_AUTOCHECKBOX | WS_TABSTOP,12,184,67,10
CONTROL "Disable markdown formatting",IDC_DISABLE_FORMATTING,
"Button",BS_AUTOCHECKBOX | WS_TABSTOP,12,45,228,10
LTEXT "Disable markdown formatting if:\n- You operate frequently with right-to-left scripts such as Arabic.\n- You are running into performance issues (message channels are taking too long to load, etc)",IDC_STATIC,12,57,232,34
END

IDD_DIALOG_PROFILE_POPOUT DIALOGEX 0, 0, 126, 134
Expand Down Expand Up @@ -613,7 +616,7 @@ BEGIN
LEFTMARGIN, 6
RIGHTMARGIN, 251
TOPMARGIN, 7
BOTTOMMARGIN, 192
BOTTOMMARGIN, 232
END

IDD_DIALOG_PROFILE_POPOUT, DIALOG
Expand Down
9 changes: 7 additions & 2 deletions src/windows/OptionsDialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -161,8 +161,9 @@ void WINAPI OnChildDialogInit(HWND hwndDlg)
mStyleId
);

CheckDlgButton(hwndDlg, IDC_SAVE_WINDOW_SIZE, GetLocalSettings()->GetSaveWindowSize() ? BST_CHECKED : BST_UNCHECKED);
CheckDlgButton(hwndDlg, IDC_START_MAXIMIZED, GetLocalSettings()->GetStartMaximized() ? BST_CHECKED : BST_UNCHECKED);
CheckDlgButton(hwndDlg, IDC_SAVE_WINDOW_SIZE, GetLocalSettings()->GetSaveWindowSize() ? BST_CHECKED : BST_UNCHECKED);
CheckDlgButton(hwndDlg, IDC_START_MAXIMIZED, GetLocalSettings()->GetStartMaximized() ? BST_CHECKED : BST_UNCHECKED);
CheckDlgButton(hwndDlg, IDC_DISABLE_FORMATTING, GetLocalSettings()->DisableFormatting() ? BST_CHECKED : BST_UNCHECKED);

break;
}
Expand Down Expand Up @@ -290,6 +291,10 @@ INT_PTR CALLBACK ChildDialogProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lPa
GetLocalSettings()->SetMessageStyle(MS_FLATBR);
SendMessage(g_Hwnd, WM_MSGLISTUPDATEMODE, 0, 0);
break;
case IDC_DISABLE_FORMATTING:
GetLocalSettings()->SetDisableFormatting(IsDlgButtonChecked(hWnd, IDC_DISABLE_FORMATTING));
SendMessage(g_Hwnd, WM_RECALCMSGLIST, 0, 0);
break;
case IDC_SAVE_WINDOW_SIZE:
GetLocalSettings()->SetSaveWindowSize(IsDlgButtonChecked(hWnd, IDC_SAVE_WINDOW_SIZE));
break;
Expand Down
17 changes: 11 additions & 6 deletions src/windows/TextInterface_Win32.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,20 +53,22 @@ Point MdMeasureString(DrawingContext* context, const String& word, int styleFlag
rect.left = rect.top = 0;
rect.right = maxWidth;
rect.bottom = 10000000;
DrawText(hdc, word.GetWrapped(), -1, &rect, DT_CALCRECT | DT_WORDBREAK);
DrawText(hdc, word.GetWrapped(), -1, &rect, DT_CALCRECT | DT_WORDBREAK | DT_NOPREFIX | DT_EDITCONTROL);
}
else {
DrawText(hdc, word.GetWrapped(), -1, &rect, DT_CALCRECT);
DrawText(hdc, word.GetWrapped(), -1, &rect, DT_CALCRECT | DT_NOPREFIX);
}

if (styleFlags & WORD_MLCODE) {
if (styleFlags & (WORD_MLCODE | WORD_NOFORMAT)) {
SIZE sz;
GetTextExtentPoint(hdc, word.GetWrapped(), word.GetSize(), &sz);

outWasWordWrapped = sz.cx > rect.right - rect.left;

rect.right += 8;
rect.bottom += 8;
if (styleFlags & WORD_MLCODE) {
rect.right += 8;
rect.bottom += 8;
}
}
}

Expand Down Expand Up @@ -183,7 +185,10 @@ void MdDrawString(DrawingContext* context, const Rect& rect, const String& str,
if (styleFlags & (WORD_CODE | WORD_MLCODE)) {
oldColorBG = SetBkColor(context->m_hdc, GetSysColor(COLOR_WINDOW));
setColorBG = true;
flags |= DT_WORDBREAK;
flags |= DT_WORDBREAK | DT_EDITCONTROL;
}
if (styleFlags & WORD_NOFORMAT) {
flags |= DT_WORDBREAK | DT_EDITCONTROL;
}

HFONT font = MdDetermineFont(styleFlags);
Expand Down

0 comments on commit 9ccf864

Please sign in to comment.