Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add minimize to tray support. #113

Merged
merged 13 commits into from
Jul 20, 2024
16 changes: 12 additions & 4 deletions src/discord/LocalSettings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,15 @@ bool LocalSettings::Load()
if (j.contains("StartMaximized"))
m_bStartMaximized = j["StartMaximized"];

if (j.contains("OpenOnStartup"))
m_bOpenOnStartup = j["OpenOnStartup"];

if (j.contains("StartMinimized"))
m_bStartMinimized = j["StartMinimized"];

if (j.contains("MinimizeToNotif"))
m_bMinimizeToNotif = j["MinimizeToNotif"];

if (j.contains("EnableTLSVerification"))
m_bEnableTLSVerification = j["EnableTLSVerification"];

Expand Down Expand Up @@ -134,9 +143,6 @@ bool LocalSettings::Load()

if (j.contains("AddExtraHeaders"))
m_bAddExtraHeaders = j["AddExtraHeaders"];

if (j.contains("MinimizeToNotif"))
m_bMinimizeToNotif = j["MinimizeToNotif"];
return true;
}

Expand All @@ -155,6 +161,9 @@ bool LocalSettings::Save()
j["ReplyMentionDefault"] = m_bReplyMentionDefault;
j["StartMaximized"] = m_bStartMaximized;
j["SaveWindowSize"] = m_bSaveWindowSize;
j["OpenOnStartup"] = m_bOpenOnStartup;
j["StartMinimized"] = m_bStartMinimized;
j["MinimizeToNotif"] = m_bMinimizeToNotif;
j["CheckUpdates"] = m_bCheckUpdates;
j["EnableTLSVerification"] = m_bEnableTLSVerification;
j["DisableFormatting"] = m_bDisableFormatting;
Expand All @@ -170,7 +179,6 @@ bool LocalSettings::Save()
j["ShowEmbedContent"] = m_bShowEmbedContent;
j["EnableNotifications"] = m_bEnableNotifications;
j["FlashOnNotification"] = m_bFlashOnNotification;
j["MinimizeToNotif"] = m_bMinimizeToNotif;

if (m_bSaveWindowSize) {
j["WindowWidth"] = m_width;
Expand Down
14 changes: 14 additions & 0 deletions src/discord/LocalSettings.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,18 @@ class LocalSettings
bool GetStartMaximized() const {
return m_bStartMaximized;
}
void SetOpenOnStartup(bool b) {
m_bOpenOnStartup = b;
}
bool GetOpenOnStartup() const {
return m_bOpenOnStartup;
}
void SetStartMinimized(bool b) {
m_bStartMinimized = b;
}
bool GetStartMinimized() const {
iProgramMC marked this conversation as resolved.
Show resolved Hide resolved
return m_bStartMinimized;
}
void SetMinimizeToNotif(bool b) {
m_bMinimizeToNotif = b;
}
Expand Down Expand Up @@ -199,6 +211,8 @@ class LocalSettings
bool m_bReplyMentionDefault = true;
bool m_bSaveWindowSize = false;
bool m_bStartMaximized = false;
bool m_bOpenOnStartup = false;
bool m_bStartMinimized = false;
bool m_bMinimizeToNotif = true;
bool m_bIsFirstStart = false;
bool m_bCheckUpdates = false;
Expand Down
7 changes: 5 additions & 2 deletions src/resource.h
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@
#define IDI_SCROLL 98
#define IDI_NOTIFICATION 99
#define IDI_NOTIFICATION_2K 100
#define IDR_NOTIFICATION_CONTEXT 101
#define IDB_TARGET 200
#define IDB_CHANNEL 201
#define IDB_CATEGORY 202
Expand Down Expand Up @@ -475,6 +476,8 @@
#define IDM_BLOCK 1065
#define IDM_COPYID 1066
#define ID_ACTIONS_QUICKSWITCHER 1067
#define ID_NOTIFICATION_SHOW 1068
#define ID_NOTIFICATION_EXIT 1069
#define IDR_MAIN_ACCELS 1201
#define IDA_SEARCH 1301
#define IDA_QUICKSWITCHER 1302
Expand All @@ -484,8 +487,8 @@
//
#ifdef APSTUDIO_INVOKED
#ifndef APSTUDIO_READONLY_SYMBOLS
#define _APS_NEXT_RESOURCE_VALUE 101
#define _APS_NEXT_COMMAND_VALUE 1069
#define _APS_NEXT_RESOURCE_VALUE 102
#define _APS_NEXT_COMMAND_VALUE 1079
#define _APS_NEXT_CONTROL_VALUE 904
#define _APS_NEXT_SYMED_VALUE 40000
#endif
Expand Down
20 changes: 19 additions & 1 deletion src/resource.rc
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,24 @@ BEGIN
END
END

IDR_NOTIFICATION_CONTEXT MENU
BEGIN
POPUP "Notification"
BEGIN
MENUITEM "Show", ID_NOTIFICATION_SHOW
POPUP "Status"
BEGIN
MENUITEM "Online", ID_ONLINESTATUSPLACEHOLDER_ONLINE, CHECKED
MENUITEM SEPARATOR
MENUITEM "Idle", ID_ONLINESTATUSPLACEHOLDER_IDLE
MENUITEM "Do not disturb", ID_ONLINESTATUSPLACEHOLDER_DONOTDISTURB
MENUITEM "Invisible", ID_ONLINESTATUSPLACEHOLDER_INVISIBLE
END
MENUITEM SEPARATOR
MENUITEM "Exit", ID_NOTIFICATION_EXIT
END
END


/////////////////////////////////////////////////////////////////////////////
//
Expand Down Expand Up @@ -683,7 +701,7 @@ BEGIN
CONTROL "Save window size",IDC_SAVE_WINDOW_SIZE,"Button",BS_AUTOCHECKBOX | WS_TABSTOP,12,18,236,10
CONTROL "Start maximized",IDC_START_MAXIMIZED,"Button",BS_AUTOCHECKBOX | WS_TABSTOP,12,32,236,10
CONTROL "Open Discord Messenger when your computer starts",IDC_OPEN_ON_STARTUP,
"Button",BS_AUTOCHECKBOX | WS_DISABLED | WS_TABSTOP,12,46,242,10
"Button",BS_AUTOCHECKBOX | WS_TABSTOP,12,46,242,10
CONTROL "Open minimized to notification area",IDC_START_MINIMIZED,
"Button",BS_AUTOCHECKBOX | WS_DISABLED | WS_TABSTOP,32,60,222,10
CONTROL "Minimize to notification area when clicking the close button",IDC_MINIMIZE_TO_NOTIF,
Expand Down
68 changes: 62 additions & 6 deletions src/windows/Main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -416,6 +416,14 @@ int OnSSLError(const std::string& url)
return MessageBox(g_Hwnd, buffer, TmGetTString(IDS_SSL_ERROR_TITLE), MB_ABORTRETRYIGNORE | MB_ICONWARNING);
}

void CloseCleanup(HWND hWnd)
{
KillImageViewer();
ProfilePopout::Dismiss();
AutoComplete::DismissAutoCompleteWindowsIfNeeded(hWnd);
g_pLoadingMessage->Hide();
}

BOOL HandleCommand(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
switch (LOWORD(wParam))
Expand All @@ -439,7 +447,7 @@ BOOL HandleCommand(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
}
case ID_FILE_EXIT:
{
SendMessage(hWnd, WM_CLOSE, 0, 0);
SendMessage(hWnd, WM_CLOSEBYPASSTRAY, 0, 0);
return 0;
}
case ID_HELP_ABOUTDISCORDMESSENGER:
Expand Down Expand Up @@ -597,6 +605,12 @@ BOOL HandleCommand(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
if (!isClipboardClosed) CloseClipboard();
break;
}
case ID_NOTIFICATION_SHOW:
SendMessage(g_Hwnd, WM_RESTORE, 0, 0);
break;
case ID_NOTIFICATION_EXIT:
SendMessage(g_Hwnd, WM_CLOSEBYPASSTRAY, 0, 0);
break;
}

return DefWindowProc(hWnd, uMsg, wParam, lParam);
Expand All @@ -623,6 +637,39 @@ void ResetTryAgainInTime() {
g_tryAgainTimerElapse = 500;
}

const LPTSTR g_tStartupArg = TEXT(" --startup");
const LPSTR g_StartupArg = "--startup";

bool g_bFromStartup = false;

void CheckIfItsStartup(const LPSTR pCmdLine)
{
g_bFromStartup = StrStrA(pCmdLine, g_StartupArg);
}

void AddOrRemoveAppFromStartup()
{
HKEY hkey = NULL;
RegCreateKey(HKEY_CURRENT_USER, TEXT("Software\\Microsoft\\Windows\\CurrentVersion\\Run"), &hkey);

LPCTSTR value = TmGetTString(IDS_PROGRAM_NAME);

if (GetLocalSettings()->GetOpenOnStartup())
{
TCHAR tPath[MAX_PATH];
const DWORD length = GetModuleFileName(NULL, tPath, MAX_PATH);

const std::string sPath = "\"" + MakeStringFromTString(tPath) + "\"" + MakeStringFromTString(g_tStartupArg);
KnockKnockP marked this conversation as resolved.
Show resolved Hide resolved
const LPTSTR finalPath = ConvertCppStringToTString(sPath);

RegSetValueEx(hkey, value, 0, REG_SZ, (BYTE *)finalPath, (wcslen(finalPath) + 1) * sizeof(TCHAR));
}
else
{
RegDeleteValue(hkey, value);
}
}

int g_agerCounter = 0;

LRESULT CALLBACK WindowProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
Expand Down Expand Up @@ -1094,6 +1141,10 @@ LRESULT CALLBACK WindowProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
g_tryAgainTimer = 0;
}
g_pLoadingMessage->Hide();

if (g_bFromStartup && GetLocalSettings()->GetStartMinimized()) {
GetFrontend()->MinimizeWindow();
}
break;
case WM_CONNECTING: {
g_pLoadingMessage->Show();
Expand All @@ -1117,13 +1168,16 @@ LRESULT CALLBACK WindowProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
break;
}

case WM_CLOSEBYPASSTRAY:
AddOrRemoveAppFromStartup();
CloseCleanup(hWnd);
DestroyWindow(hWnd);
break;

case WM_CLOSE:
KillImageViewer();
ProfilePopout::Dismiss();
AutoComplete::DismissAutoCompleteWindowsIfNeeded(hWnd);
g_pLoadingMessage->Hide();
CloseCleanup(hWnd);

if (GetLocalSettings()->GetStartMaximized())
if (GetLocalSettings()->GetMinimizeToNotif())
KnockKnockP marked this conversation as resolved.
Show resolved Hide resolved
{
GetFrontend()->MinimizeWindow();
return 1;
Expand Down Expand Up @@ -1597,6 +1651,8 @@ int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR pCmdLin
if (!ForceSingleInstance(pClassName))
return 0;

CheckIfItsStartup(pCmdLine);

g_hInstance = hInstance;
ri::InitReimplementation();

Expand Down
15 changes: 15 additions & 0 deletions src/windows/OptionsDialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,11 @@ void WINAPI OnChildDialogInit(HWND hwndDlg)
{
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_OPEN_ON_STARTUP, GetLocalSettings()->GetOpenOnStartup() ? BST_CHECKED : BST_UNCHECKED);

CheckDlgButton(hwndDlg, IDC_START_MINIMIZED, GetLocalSettings()->GetStartMinimized() ? BST_CHECKED : BST_UNCHECKED);
EnableWindow(GetDlgItem(hwndDlg, IDC_START_MINIMIZED), GetLocalSettings()->GetOpenOnStartup());

CheckDlgButton(hwndDlg, IDC_MINIMIZE_TO_NOTIF, GetLocalSettings()->GetMinimizeToNotif() ? BST_CHECKED : BST_UNCHECKED);
break;
}
Expand Down Expand Up @@ -492,6 +497,16 @@ INT_PTR CALLBACK ChildDialogProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lPa
case IDC_START_MAXIMIZED:
GetLocalSettings()->SetStartMaximized(IsDlgButtonChecked(hWnd, IDC_START_MAXIMIZED));
break;
case IDC_OPEN_ON_STARTUP:
{
const bool checked = IsDlgButtonChecked(hWnd, IDC_OPEN_ON_STARTUP);
GetLocalSettings()->SetOpenOnStartup(checked);
EnableWindow(GetDlgItem(hWnd, IDC_START_MINIMIZED), checked);
break;
}
case IDC_START_MINIMIZED:
GetLocalSettings()->SetStartMinimized(IsDlgButtonChecked(hWnd, IDC_START_MINIMIZED));
break;
case IDC_MINIMIZE_TO_NOTIF:
GetLocalSettings()->SetMinimizeToNotif(IsDlgButtonChecked(hWnd, IDC_MINIMIZE_TO_NOTIF));
break;
Expand Down
25 changes: 6 additions & 19 deletions src/windows/ShellNotification.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -198,19 +198,14 @@ void ShellNotification::OnNotification()
void ShellNotification::ShowContextMenu()
{
POINT cursor;
GetCursorPos(&cursor);

const HMENU popupMenu = CreatePopupMenu();
InsertMenu(popupMenu, 0, MF_BYPOSITION | MF_STRING, WM_DESTROY, TEXT("Exit"));
if (!GetCursorPos(&cursor)) {
DbgPrintW("Could not acquire cursor position.");
return;
}

const HMENU popupMenu = GetSubMenu(LoadMenu(g_hInstance, MAKEINTRESOURCE(IDR_NOTIFICATION_CONTEXT)), 0);
SetForegroundWindow(g_Hwnd);
const int command = TrackPopupMenu(popupMenu, TPM_LEFTBUTTON | TPM_RETURNCMD, cursor.x, cursor.y, 0, g_Hwnd, NULL);
PostMessage(g_Hwnd, WM_NULL, 0, 0);

if (command == WM_DESTROY)
{
PostMessage(g_Hwnd, WM_DESTROY, 0, 0);
}
TrackPopupMenu(popupMenu, TPM_LEFTBUTTON, cursor.x, cursor.y, 0, g_Hwnd, NULL);
}

void ShellNotification::Callback(WPARAM wParam, LPARAM lParam)
Expand Down Expand Up @@ -250,16 +245,8 @@ void ShellNotification::Callback(WPARAM wParam, LPARAM lParam)
break;

case WM_RBUTTONUP:
{
POINT pt;
if (!GetCursorPos(&pt)) {
DbgPrintW("Could not acquire cursor position.");
break;
}

ShowContextMenu();
break;
}
}


Expand Down
1 change: 1 addition & 0 deletions src/windows/WindowMessages.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ enum eWmUserMsgs
WM_NOTIFMANAGERCALLBACK,
WM_POSTINIT,
WM_RECREATEMEMBERLIST,
WM_CLOSEBYPASSTRAY,

WM_UPDATETEXTSIZE = WM_APP, // used by the MessageEditor
WM_RESTORE,
KnockKnockP marked this conversation as resolved.
Show resolved Hide resolved
Expand Down