Skip to content

Commit

Permalink
Preload Favicons
Browse files Browse the repository at this point in the history
  • Loading branch information
zenden2k committed Mar 18, 2024
1 parent 9be6e43 commit 28ea9f3
Show file tree
Hide file tree
Showing 8 changed files with 75 additions and 29 deletions.
44 changes: 33 additions & 11 deletions Source/Func/MyEngineList.cpp
Expand Up @@ -29,14 +29,16 @@ char CMyEngineList::DefaultServer[] = "default";

char CMyEngineList::RandomServer[] = "random";

CMyEngineList::CMyEngineList()
CMyEngineList::CMyEngineList(TaskDispatcher* taskDispatcher): iconBitmapUtils_(std::make_unique<IconBitmapUtils>()),
taskDispatcher_(taskDispatcher)
{
}

CMyEngineList::~CMyEngineList()
{
for ( const auto& it: serverIcons_) {
DestroyIcon(it.second);
for (const auto& it: serverIcons_) {
DestroyIcon(it.second.icon);
DeleteObject(it.second.bm);
}
}

Expand Down Expand Up @@ -66,7 +68,8 @@ bool CMyEngineList::loadFromFile(const CString& filename)
return CUploadEngineList::loadFromFile(WCstringToUtf8(filename), Settings->ServersSettings);
}

HICON CMyEngineList::getIconForServer(const std::string& name) {
CMyEngineList::ServerIconCacheItem CMyEngineList::getIconBitmapForServer(const std::string& name) {
std::lock_guard lk(cacheMutex_);
const auto iconIt = serverIcons_.find(name);
if (iconIt != serverIcons_.end()) {
return iconIt->second;
Expand All @@ -85,12 +88,12 @@ HICON CMyEngineList::getIconForServer(const std::string& name) {
if (ued && !ued->PluginName.empty()) {
iconFileName = dataFolder + _T("Favicons\\") + Utf8ToWCstring(ued->PluginName) + _T(".ico");
if (!WinUtils::FileExists(iconFileName)) {
serverIcons_[name] = nullptr;
return nullptr;
serverIcons_[name]={};
return {};
}
} else {
serverIcons_[name] = nullptr;
return nullptr;
serverIcons_[name] = {};
return {};
}
}

Expand All @@ -104,10 +107,15 @@ HICON CMyEngineList::getIconForServer(const std::string& name) {
}

if ( !icon ) {
return nullptr;
return {};
}
serverIcons_[name] = icon;
return icon;
ServerIconCacheItem item(icon, iconBitmapUtils_->HIconToBitmapPARGB32(icon));
serverIcons_[name] = item;
return item;
}

HICON CMyEngineList::getIconForServer(const std::string& name) {
return getIconBitmapForServer(name).icon;
}

CString CMyEngineList::getIconNameForServer(const std::string& name) {
Expand All @@ -122,3 +130,17 @@ CString CMyEngineList::getIconNameForServer(const std::string& name) {
}
return U2W( IuCoreUtils::ExtractFileName(W2U(iconFileName)) );
}

void CMyEngineList::preLoadIcons() {
if (iconsPreload_) {
throw std::logic_error("preLoadIcons() should not be called twice");
}
iconsPreload_ = true;
taskDispatcher_->post([this] {
for (int i = 0; i < count(); i++) {
CUploadEngineData* ued = byIndex(i);
CString name = Utf8ToWCstring(ued->Name);
[[maybe_unused]] auto icon = getIconBitmapForServer(ued->Name);
}
});
}
33 changes: 29 additions & 4 deletions Source/Func/MyEngineList.h
Expand Up @@ -21,16 +21,31 @@
#ifndef IU_FUNC_MY_ENGINE_LIST_H
#define IU_FUNC_MY_ENGINE_LIST_H

#include <future>
#include <mutex>

#include "atlheaders.h"
#include "Library.h"

#include "Core/TaskDispatcher.h"
#include "Core/UploadEngineList.h"
#include "Gui/IconBitmapUtils.h"

class CMyEngineList: public CUploadEngineList
{
public:
CMyEngineList();
~CMyEngineList();
struct ServerIconCacheItem {
HICON icon{};
HBITMAP bm{};

ServerIconCacheItem() = default;

ServerIconCacheItem(HICON i, HBITMAP b): icon(i), bm(b) {

}
};
explicit CMyEngineList(TaskDispatcher* taskDispatcher);
~CMyEngineList() override;
CString errorStr() const;

using CUploadEngineListBase::byName;
Expand All @@ -40,11 +55,21 @@ class CMyEngineList: public CUploadEngineList
bool loadFromFile(const CString& filename);
HICON getIconForServer(const std::string& name);
CString getIconNameForServer(const std::string& name);
ServerIconCacheItem getIconBitmapForServer(const std::string& name);

/**
* @throws std::logic_error
*/
void preLoadIcons();
static char DefaultServer[];
static char RandomServer[];
private:
std::map<std::string, HICON> serverIcons_;
std::map<std::string, ServerIconCacheItem> serverIcons_;
CString m_ErrorStr;
std::unique_ptr<IconBitmapUtils> iconBitmapUtils_;
std::mutex cacheMutex_;
TaskDispatcher* taskDispatcher_;
bool iconsPreload_ = false;
DISALLOW_COPY_AND_ASSIGN(CMyEngineList);
};
#endif
#endif
2 changes: 1 addition & 1 deletion Source/Gui/Dialogs/ScreenshotDlg.cpp
Expand Up @@ -42,7 +42,7 @@ LRESULT CScreenshotDlg::OnInitDialog(UINT uMsg, WPARAM wParam, LPARAM lParam, BO
CenterWindow(GetParent());

CommandBox.m_bHyperLinks = false;
CommandBox.Init();
CommandBox.Init(GetSysColor(COLOR_WINDOW));
CommandBox.AddString(TR("Capture the Entire Screen"), _T(" "), IDC_FULLSCREEN, LOADICO(IDI_SCREENSHOT));
CommandBox.AddString(TR("Capture the Active Window"), _T(" "), IDC_SCRACTIVEWINDOW,LOADICO(IDI_WINDOW));
CommandBox.AddString(TR("Capture Selected Area"), _T(" "), IDC_REGIONSELECT,LOADICO(IDI_ICONREGION));
Expand Down
15 changes: 6 additions & 9 deletions Source/Gui/Dialogs/UploadSettings.cpp
Expand Up @@ -686,11 +686,9 @@ LRESULT CUploadSettings::OnServerDropDown(int idCtrl, LPNMHDR pnmh, BOOL& bHandl
CUploadEngineData* ued = m_EngineList->byIndex(i);
CString name = Utf8ToWCstring(ued->Name);
mi.dwTypeData = const_cast<LPWSTR>(name.GetString());
HICON hImageIcon = m_EngineList->getIconForServer(ued->Name);
HBITMAP bm = iconBitmapUtils_->HIconToBitmapPARGB32(hImageIcon);
bitmaps.push_back(bm);

mi.hbmpItem = bm;
auto cacheItem = m_EngineList->getIconBitmapForServer(ued->Name);

mi.hbmpItem = cacheItem.bm;

if ( mi.hbmpItem ) {
mi.fMask |= MIIM_BITMAP;
Expand Down Expand Up @@ -726,10 +724,9 @@ LRESULT CUploadSettings::OnServerDropDown(int idCtrl, LPNMHDR pnmh, BOOL& bHandl
CUploadEngineData* ued = m_EngineList->byIndex(i);
CString name = Utf8ToWCstring(ued->Name);
mi.dwTypeData = const_cast<LPWSTR>(name.GetString());
HICON hImageIcon = m_EngineList->getIconForServer(ued->Name);
HBITMAP bm = iconBitmapUtils_->HIconToBitmapPARGB32(hImageIcon);
bitmaps.push_back(bm);
mi.hbmpItem = bm;
auto cacheItem = m_EngineList->getIconBitmapForServer(ued->Name);

mi.hbmpItem = cacheItem.bm;
if ( mi.hbmpItem ) {
mi.fMask |= MIIM_BITMAP;
}
Expand Down
5 changes: 3 additions & 2 deletions Source/Gui/Dialogs/WelcomeDlg.cpp
Expand Up @@ -31,7 +31,8 @@
// CWelcomeDlg
CWelcomeDlg::CWelcomeDlg()
{
br = CreateSolidBrush(RGB(255, 255, 255));
br = GetSysColorBrush(COLOR_WINDOW);
//CreateSolidBrush(RGB(255, 255, 255));
QuickRegionPrint = false;
}

Expand Down Expand Up @@ -79,7 +80,7 @@ LRESULT CWelcomeDlg::OnInitDialog(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL&
return icon.m_hIcon;
};

ListBox.Init();
ListBox.Init(GetSysColor(COLOR_WINDOW));
ListBox.AddString(TR("Add Images"), TR("JPEG, PNG, GIF, BMP or any other file"), IDC_ADDIMAGES, loadBigIcon(IDI_IMAGES));

ListBox.AddString(TR("Add Files..."), 0, IDC_ADDFILES, loadSmallIcon(IDI_ICONADD));
Expand Down
2 changes: 1 addition & 1 deletion Source/Gui/Dialogs/WelcomeDlg.h
Expand Up @@ -99,7 +99,7 @@ class CWelcomeDlg :
LRESULT OnEraseBkg(UINT /*uMsg*/, WPARAM /*wParam*/, LPARAM /*lParam*/, BOOL& /*bHandled*/);
CHyperLinkControl ListBox;
LRESULT OnCtlColorMsgDlg(HDC hdc, HWND hwndChild);
CBrush br;
CBrushHandle br;
bool OnShow() override;
void SetInitialFocus() override;
bool QuickRegionPrint;
Expand Down
1 change: 1 addition & 0 deletions Source/Gui/Dialogs/WizardDlg.cpp
Expand Up @@ -350,6 +350,7 @@ LRESULT CWizardDlg::OnInitDialog(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL&
LoadUploadEngines(_T("userservers.xml"), ErrorStr);

Settings.fixInvalidServers();
enginelist_->preLoadIcons();
if ( isFirstRun_ ) {
CQuickSetupDlg quickSetupDialog;
if (quickSetupDialog.DoModal(m_hWnd) != IDOK){
Expand Down
2 changes: 1 addition & 1 deletion Source/Image Uploader.cpp
Expand Up @@ -137,7 +137,7 @@ class Application {
serviceLocator->setDialogProvider(scriptDialogProvider_.get());
serviceLocator->setTranslator(&lang_);
scriptsManager_ = std::make_unique<ScriptsManager>(serviceLocator->networkClientFactory());
engineList_ = std::make_unique<CMyEngineList>();
engineList_ = std::make_unique<CMyEngineList>(taskDispatcher_.get());
serviceLocator->setEngineList(engineList_.get());
serviceLocator->setMyEngineList(engineList_.get());
settings_.setEngineList(engineList_.get());
Expand Down

0 comments on commit 28ea9f3

Please sign in to comment.