Skip to content

Commit

Permalink
Call AddMessageFilter in CLogWindow
Browse files Browse the repository at this point in the history
Fixed order of creation of CMessageLoop instance and LogWindow
Call Invalidate() in UI thread in CHistoryTreeControl
  • Loading branch information
zenden2k committed Jan 19, 2025
1 parent b2b08e0 commit 8c4da64
Show file tree
Hide file tree
Showing 7 changed files with 59 additions and 27 deletions.
4 changes: 3 additions & 1 deletion Source/Core/TempFileDeleter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ TempFileDeleter::~TempFileDeleter()
try {
cleanup();
} catch (const std::exception& ex) {
LOG(ERROR) << ex.what();
LOG(ERROR) << "Failed to clean up temporary files: " << ex.what();
} catch (...) {
LOG(ERROR) << "Unknown error during cleanup.";
}
}

Expand Down
28 changes: 19 additions & 9 deletions Source/Gui/Controls/HistoryTreeControl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
#include "Func/MyEngineList.h"
#include "Core/i18n/Translator.h"
#include "Core/AbstractServerIconCache.h"
#include "Core/TaskDispatcher.h"

// CHistoryTreeControl
CHistoryTreeControl::CHistoryTreeControl(std::shared_ptr<INetworkClientFactory> factory)
Expand All @@ -56,10 +57,7 @@ CHistoryTreeControl::~CHistoryTreeControl()
}
}

void CHistoryTreeControl::CreateDownloader()
{
using namespace std::placeholders;

void CHistoryTreeControl::CreateDownloader() {
if(!m_FileDownloader) {
m_FileDownloader = std::make_unique<CFileDownloader>(networkClientFactory_, AppParams::instance()->tempDirectory());
m_FileDownloader->setOnConfigureNetworkClientCallback([this](auto* nm) { OnConfigureNetworkClient(nm); });
Expand All @@ -82,6 +80,11 @@ LRESULT CHistoryTreeControl::OnDestroy(UINT uMsg, WPARAM wParam, LPARAM lParam,B
return 0;
}

LRESULT CHistoryTreeControl::OnThumbLoaded(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled) {
Invalidate();
return 0;
}

void CHistoryTreeControl::abortLoadingThreads()
{
{
Expand Down Expand Up @@ -124,6 +127,7 @@ void CHistoryTreeControl::addSubEntry(TreeItem* res, HistoryItem* it, bool autoE
it2->hi = it;
it2->thumbnail = nullptr;
it2->ThumbnailRequested = false;
it2->treeItem = item;
}

DWORD CHistoryTreeControl::OnItemPrePaint(int /*idCtrl*/, LPNMCUSTOMDRAW lpNMCustomDraw)
Expand Down Expand Up @@ -709,8 +713,7 @@ void CHistoryTreeControl::DownloadThumb(HistoryTreeItem * it)
std::string thumbUrl = it->hi->thumbUrl;
if(thumbUrl.empty()) return ;
std::string cacheFile = ServiceLocator::instance()->localFileCache()->get(thumbUrl);
if(!cacheFile.empty() && IuCoreUtils::FileExists(cacheFile))
{
if(!cacheFile.empty()) {
it->thumbnailSource = cacheFile;
m_thumbLoadingQueueMutex.lock();
m_thumbLoadingQueue.push_back(it);
Expand Down Expand Up @@ -742,9 +745,16 @@ DWORD CHistoryTreeControl::Run()
// Try downloading it
DownloadThumb(it);
}
else
{
Invalidate();
else {
/* ServiceLocator::instance()->taskRunner()->runInGuiThread([this, treeItem = it->treeItem] {
RECT rc {};
if (GetItemRect(FindVisibleItemIndex(treeItem), &rc) != LB_ERR) {
InvalidateRect(&rc);
}
},
true);*/

PostMessage(WM_APP_MY_THUMBLOADED, 0, 0);
}
}
m_bIsRunning = false;
Expand Down
6 changes: 6 additions & 0 deletions Source/Gui/Controls/HistoryTreeControl.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
#include "Core/FileDownloader.h"
#include "CustomTreeControl.h"

constexpr auto WM_APP_MY_THUMBLOADED = WM_USER + 2000;

class INetworkClientFactory;

struct HistoryTreeItem
Expand All @@ -18,6 +20,8 @@ struct HistoryTreeItem
HBITMAP thumbnail;
bool ThumbnailRequested;
std::string thumbnailSource;
TreeItem* treeItem = nullptr;

HistoryTreeItem() :hi(nullptr), thumbnail(nullptr), ThumbnailRequested(false){

}
Expand Down Expand Up @@ -47,6 +51,7 @@ class CHistoryTreeControl :
BEGIN_MSG_MAP(CHistoryTreeControl)
MESSAGE_HANDLER(WM_DESTROY, OnDestroy)
MESSAGE_HANDLER(WM_GETDLGCODE, OnGetDlgCode)
MESSAGE_HANDLER(WM_APP_MY_THUMBLOADED, OnThumbLoaded)
CHAIN_MSG_MAP(CCustomTreeControlImpl<CHistoryTreeControl>)
END_MSG_MAP()

Expand All @@ -61,6 +66,7 @@ class CHistoryTreeControl :
// LRESULT NotifyHandler(int idCtrl, LPNMHDR pnmh, BOOL& bHandled);
LRESULT OnCreate(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled);
LRESULT OnDestroy(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled);
LRESULT OnThumbLoaded(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled);
void DrawTreeItem(HDC dc, RECT rc, UINT itemState, TreeItem* item) override;
DWORD OnItemPrePaint(int /*idCtrl*/, LPNMCUSTOMDRAW /*lpNMCustomDraw*/);
DWORD OnSubItemPrePaint(int /*idCtrl*/, LPNMCUSTOMDRAW /*lpNMCustomDraw*/);
Expand Down
8 changes: 4 additions & 4 deletions Source/Gui/Dialogs/AboutDlg.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -277,17 +277,17 @@ LRESULT CAboutDlg::OnContextMenu(UINT /*uMsg*/, WPARAM wParam, LPARAM /*lParam*/
HWND wnd = reinterpret_cast<HWND>(wParam);
HWND authorNameLabelWnd = GetDlgItem(IDC_AUTHORNAMELABEL);

if (wnd == authorNameLabelWnd) {
if (wnd == authorNameLabelWnd) {
RECT rc;
::GetWindowRect(authorNameLabelWnd, &rc);
POINT menuOrigin{ rc.left,rc.bottom };
POINT menuOrigin { rc.left, rc.bottom };

CMenu contextMenu;
contextMenu.CreatePopupMenu();
contextMenu.AppendMenu(MF_STRING, ID_COPYAUTHOREMAIL, TR("Copy e-mail"));
//contextMenu.SetMenuDefaultItem(ID_COPYAUTHOREMAIL, FALSE);
contextMenu.TrackPopupMenu(TPM_LEFTALIGN | TPM_LEFTBUTTON, menuOrigin.x, menuOrigin.y, m_hWnd);
}
}

return 0;
}
Expand Down
24 changes: 18 additions & 6 deletions Source/Gui/Dialogs/LogWindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,20 @@ LRESULT CLogWindow::OnInitDialog(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL&
DlgResize_Init();
MsgList.SubclassWindow(GetDlgItem(IDC_MSGLIST));

CMessageLoop* pLoop = _Module.GetMessageLoop();
ATLASSERT(pLoop != NULL);
pLoop->AddMessageFilter(this);

return 1; // Let the system set the focus
}

LRESULT CLogWindow::OnDestroy(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled) {
CMessageLoop* pLoop = _Module.GetMessageLoop();
ATLASSERT(pLoop != NULL);
pLoop->RemoveMessageFilter(this);
return 0;
}

BOOL CLogWindow::PreTranslateMessage(MSG* pMsg)
{
return CWindow::IsDialogMessage(pMsg);
Expand All @@ -68,19 +79,20 @@ void CLogWindow::WriteLogImpl(const DefaultLogger::LogEntry& entry)
auto* settings = ServiceLocator::instance()->basicSettings();
MsgList.AddString(entry.MsgType, entry.Sender.c_str(), entry.Msg.c_str(), entry.Info.c_str(), entry.Time.c_str());
if (entry.MsgType == ILogger::logError && settings->AutoShowLog) {
Show();
Show(false);
}
}

void CLogWindow::Show()
void CLogWindow::Show(bool setActive)
{
if (!IsWindowVisible()) {
ShowWindow(SW_SHOW);
setActive = true;
}
if (setActive) {
SetForegroundWindow(m_hWnd);
::SetActiveWindow(m_hWnd);
}
SetForegroundWindow(m_hWnd);
::SetActiveWindow(m_hWnd);
//BringWindowToTop();
//SetWindowPos(HWND_TOPMOST, 0,0,0,0, SWP_NOSIZE | SWP_NOMOVE);
}

LRESULT CLogWindow::OnContextMenu(UINT /*uMsg*/, WPARAM wParam, LPARAM lParam, BOOL& /*bHandled*/)
Expand Down
4 changes: 3 additions & 1 deletion Source/Gui/Dialogs/LogWindow.h
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ class CLogWindow : public CDialogImpl <CLogWindow>,

BEGIN_MSG_MAP(CLogWindow)
MESSAGE_HANDLER(WM_INITDIALOG, OnInitDialog)
MESSAGE_HANDLER(WM_DESTROY, OnDestroy)
COMMAND_HANDLER(IDCANCEL, BN_CLICKED, OnClickedCancel)
COMMAND_ID_HANDLER(IDC_COPYTEXTTOCLIPBOARD, OnCopyToClipboard)
MESSAGE_HANDLER(WM_CONTEXTMENU, OnContextMenu)
Expand All @@ -81,10 +82,11 @@ class CLogWindow : public CDialogImpl <CLogWindow>,
// LRESULT CommandHandler(WORD wNotifyCode, WORD wID, HWND hWndCtl, BOOL& bHandled);
// LRESULT NotifyHandler(int idCtrl, LPNMHDR pnmh, BOOL& bHandled);
LRESULT OnInitDialog(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled);
LRESULT OnDestroy(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled);
LRESULT OnWmWriteLog(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled);
LRESULT OnClickedCancel(WORD wNotifyCode, WORD wID, HWND hWndCtl, BOOL& bHandled);
CLogListBox MsgList;
void Show();
void Show(bool setActive = true);
LRESULT OnContextMenu(UINT /*uMsg*/, WPARAM /*wParam*/, LPARAM /*lParam*/, BOOL & /*bHandled*/);
LRESULT OnClearList(WORD /*wNotifyCode*/, WORD /*wID*/, HWND /*hWndCtl*/, BOOL & /*bHandled*/);
LRESULT OnCopyToClipboard(WORD /*wNotifyCode*/, WORD /*wID*/, HWND /*hWndCtl*/, BOOL & /*bHandled*/);
Expand Down
12 changes: 6 additions & 6 deletions Source/Image Uploader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@
CAppModule _Module;

class Application {
CMessageLoop theLoop_;
CLogWindow logWindow_;
WtlGuiSettings settings_;
CLang lang_;
Expand All @@ -78,6 +79,7 @@ class Application {
std::shared_ptr<NetworkDebugger> networkDebugger_;
MediaFoundationInitializer mediaFoundationInitializer_;
CString commonTempFolder_, tempFolder_;

public:
Application() {
srand(static_cast<unsigned>(time(nullptr)));
Expand All @@ -89,6 +91,7 @@ class Application {
CScriptUploadEngine::DestroyScriptEngine();
//ServiceLocator::instance()->setUploadManager(nullptr);
logWindow_.DestroyWindow();
_Module.RemoveMessageLoop();
// Remove temporary files
IuCommonFunctions::ClearTempFolder(tempFolder_);
std::vector<CString> folders;
Expand Down Expand Up @@ -128,6 +131,8 @@ class Application {
google::AddLogSink(myLogSink_.get());
serviceLocator->setSettings(&settings_);
serviceLocator->setNetworkClientFactory(std::make_shared<NetworkClientFactory>());

_Module.AddMessageLoop(&theLoop_);
logWindow_.Create(nullptr);
logWindow_.setLogger(logger_.get());

Expand Down Expand Up @@ -244,9 +249,6 @@ class Application {

GdiPlusInitializer gdiPlusInitializer;
initServices();

CMessageLoop theLoop;
_Module.AddMessageLoop(&theLoop);

CWizardDlg dlgMain(logger_, engineList_.get(), uploadEngineManager_.get(), uploadManager_.get(), scriptsManager_.get(), &settings_);
auto* serviceLocator = ServiceLocator::instance();
Expand Down Expand Up @@ -368,11 +370,10 @@ class Application {
dlgMain.ShowWindow(nCmdShow);
}

int nRet = theLoop.Run();
int nRet = theLoop_.Run();
if (dlgMain.m_hWnd) {
dlgMain.DestroyWindow();
}
_Module.RemoveMessageLoop();

return nRet;
}
Expand Down Expand Up @@ -418,7 +419,6 @@ int WINAPI _tWinMain(HINSTANCE hInstance, HINSTANCE /*hPrevInstance*/, LPTSTR lp
}
_Module.Term();


OleUninitialize();
//google::RemoveLogSink(&logSink);
google::ShutdownGoogleLogging();
Expand Down

0 comments on commit 8c4da64

Please sign in to comment.