Skip to content

Commit

Permalink
Reduce startup time and decrease the usage of Win32 user objects.
Browse files Browse the repository at this point in the history
  • Loading branch information
sdottaka committed Aug 19, 2023
1 parent a121466 commit 3bb5701
Show file tree
Hide file tree
Showing 3 changed files with 96 additions and 60 deletions.
55 changes: 30 additions & 25 deletions Src/MainFrm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -893,9 +893,10 @@ bool CMainFrame::ShowTextOrTableMergeDoc(std::optional<bool> table, CDirDoc * pD
const fileopenflags_t dwFlags[], const String strDesc[], const String& sReportFile /*= _T("")*/,
const PackingInfo * infoUnpacker /*= nullptr*/, const OpenTextFileParams* pOpenParams /*= nullptr*/)
{
CMultiDocTemplate* pDiffTemplate = theApp.GetDiffTemplate();
if (m_pMenus[MENU_MERGEVIEW] == nullptr)
theApp.m_pDiffTemplate->m_hMenuShared = NewMergeViewMenu();
CMergeDoc * pMergeDoc = GetMergeDocForDiff<CMergeDoc>(theApp.m_pDiffTemplate, pDirDoc, nFiles, false);
pDiffTemplate->m_hMenuShared = NewMergeViewMenu();
CMergeDoc * pMergeDoc = GetMergeDocForDiff<CMergeDoc>(pDiffTemplate, pDirDoc, nFiles, false);

// Make local copies, so we can change encoding if we guess it below
FileLocation fileloc[3];
Expand Down Expand Up @@ -1005,9 +1006,10 @@ bool CMainFrame::ShowHexMergeDoc(CDirDoc * pDirDoc, int nFiles, const FileLocati
const fileopenflags_t dwFlags[], const String strDesc[], const String& sReportFile /*= _T("")*/,
const PackingInfo * infoUnpacker /*= nullptr*/, const OpenBinaryFileParams* pOpenParams /*= nullptr*/)
{
CMultiDocTemplate* pHexMergeTemplate = theApp.GetHexMergeTemplate();
if (m_pMenus[MENU_HEXMERGEVIEW] == nullptr)
theApp.m_pHexMergeTemplate->m_hMenuShared = NewHexMergeViewMenu();
CHexMergeDoc *pHexMergeDoc = GetMergeDocForDiff<CHexMergeDoc>(theApp.m_pHexMergeTemplate, pDirDoc, nFiles);
pHexMergeTemplate->m_hMenuShared = NewHexMergeViewMenu();
CHexMergeDoc *pHexMergeDoc = GetMergeDocForDiff<CHexMergeDoc>(pHexMergeTemplate, pDirDoc, nFiles);
if (pHexMergeDoc == nullptr)
return false;

Expand Down Expand Up @@ -1096,7 +1098,7 @@ bool CMainFrame::ShowTextMergeDoc(CDirDoc* pDirDoc, int nBuffers, const String t
FileLocation fileloc[3];
fileopenflags_t dwFlags[3] = {};
CDirDoc* pDirDoc2 = pDirDoc->GetMainView() ? pDirDoc :
static_cast<CDirDoc*>(theApp.m_pDirTemplate->CreateNewDocument());
static_cast<CDirDoc*>(theApp.GetDirTemplate()->CreateNewDocument());
for (int nBuffer = 0; nBuffer < nBuffers; ++nBuffer)
{
auto wTemp = std::make_shared<TempFile>(TempFile());
Expand Down Expand Up @@ -1321,9 +1323,10 @@ bool CMainFrame::DoFileOrFolderOpen(const PathContext * pFiles /*= nullptr*/,
if (tFiles.GetSize() < 2 || pathsType == paths::DOES_NOT_EXIST &&
!std::any_of(tFiles.begin(), tFiles.end(), [](const auto& path) { return path.empty() || paths::IsURL(path); }))
{
CMultiDocTemplate* pOpenTemplate = theApp.GetOpenTemplate();
if (m_pMenus[MENU_OPENVIEW] == nullptr)
theApp.m_pOpenTemplate->m_hMenuShared = NewOpenViewMenu();
COpenDoc *pOpenDoc = static_cast<COpenDoc *>(theApp.m_pOpenTemplate->CreateNewDocument());
pOpenTemplate->m_hMenuShared = NewOpenViewMenu();
COpenDoc *pOpenDoc = static_cast<COpenDoc *>(pOpenTemplate->CreateNewDocument());
if (dwFlags)
{
pOpenDoc->m_dwFlags[0] = dwFlags[0];
Expand All @@ -1334,8 +1337,8 @@ bool CMainFrame::DoFileOrFolderOpen(const PathContext * pFiles /*= nullptr*/,
pOpenDoc->m_bRecurse = bRecurse2;
if (infoUnpacker)
pOpenDoc->m_strUnpackerPipeline = infoUnpacker->GetPluginPipeline();
CFrameWnd *pFrame = theApp.m_pOpenTemplate->CreateNewFrame(pOpenDoc, nullptr);
theApp.m_pOpenTemplate->InitialUpdateFrame(pFrame, pOpenDoc);
CFrameWnd *pFrame = pOpenTemplate->CreateNewFrame(pOpenDoc, nullptr);
pOpenTemplate->InitialUpdateFrame(pFrame, pOpenDoc);
return true;
}

Expand Down Expand Up @@ -1387,16 +1390,17 @@ bool CMainFrame::DoFileOrFolderOpen(const PathContext * pFiles /*= nullptr*/,
// an archive. Don't open a new dirview if we are comparing files.
if (pDirDoc == nullptr)
{
CMultiDocTemplate* pDirTemplate = theApp.GetDirTemplate();
if (allowFolderCompare && pathsType == paths::IS_EXISTING_DIR)
{
CDirDoc::m_nDirsTemp = tFiles.GetSize();
if (m_pMenus[MENU_DIRVIEW] == nullptr)
theApp.m_pDirTemplate->m_hMenuShared = NewDirViewMenu();
pDirDoc = static_cast<CDirDoc*>(theApp.m_pDirTemplate->OpenDocumentFile(nullptr));
pDirTemplate->m_hMenuShared = NewDirViewMenu();
pDirDoc = static_cast<CDirDoc*>(pDirTemplate->OpenDocumentFile(nullptr));
}
else
{
pDirDoc = static_cast<CDirDoc*>(theApp.m_pDirTemplate->CreateNewDocument());
pDirDoc = static_cast<CDirDoc*>(pDirTemplate->CreateNewDocument());
}
}

Expand Down Expand Up @@ -1455,7 +1459,7 @@ bool CMainFrame::DoFileOpen(UINT nID, const PathContext* pFiles,
const OpenFileParams *pOpenParams /*= nullptr*/)
{
ASSERT(pFiles != nullptr);
CDirDoc* pDirDoc = static_cast<CDirDoc*>(theApp.m_pDirTemplate->CreateNewDocument());
CDirDoc* pDirDoc = static_cast<CDirDoc*>(theApp.GetDirTemplate()->CreateNewDocument());
FileLocation fileloc[3];
for (int pane = 0; pane < pFiles->GetSize(); pane++)
fileloc[pane].setPath((*pFiles)[pane]);
Expand Down Expand Up @@ -1724,25 +1728,25 @@ void CMainFrame::ApplyDiffOptions()
/// Get list of OpenDocs (documents underlying edit sessions)
OpenDocList &CMainFrame::GetAllOpenDocs()
{
return static_cast<OpenDocList &>(GetDocList(theApp.m_pOpenTemplate));
return static_cast<OpenDocList &>(GetDocList(theApp.GetOpenTemplate()));
}

/// Get list of MergeDocs (documents underlying edit sessions)
MergeDocList &CMainFrame::GetAllMergeDocs()
{
return static_cast<MergeDocList &>(GetDocList(theApp.m_pDiffTemplate));
return static_cast<MergeDocList &>(GetDocList(theApp.GetDiffTemplate()));
}

/// Get list of DirDocs (documents underlying a scan)
DirDocList &CMainFrame::GetAllDirDocs()
{
return static_cast<DirDocList &>(GetDocList(theApp.m_pDirTemplate));
return static_cast<DirDocList &>(GetDocList(theApp.GetDirTemplate()));
}

/// Get list of HexMergeDocs (documents underlying edit sessions)
HexMergeDocList &CMainFrame::GetAllHexMergeDocs()
{
return static_cast<HexMergeDocList &>(GetDocList(theApp.m_pHexMergeTemplate));
return static_cast<HexMergeDocList &>(GetDocList(theApp.GetHexMergeTemplate()));
}

std::vector<CImgMergeFrame *> CMainFrame::GetAllImgMergeFrames()
Expand Down Expand Up @@ -2000,7 +2004,7 @@ bool CMainFrame::DoFileNew(UINT nID, int nPanes,
const PrediffingInfo *infoPrediffer /*= nullptr*/,
const OpenFileParams *pOpenParams)
{
CDirDoc *pDirDoc = static_cast<CDirDoc*>(theApp.m_pDirTemplate->CreateNewDocument());
CDirDoc *pDirDoc = static_cast<CDirDoc*>(theApp.GetDirTemplate()->CreateNewDocument());

// Load emptyfile descriptors and open empty docs
// Use default codepage
Expand Down Expand Up @@ -2357,9 +2361,10 @@ CMainFrame * GetMainFrame()
*/
void CMainFrame::OnSaveProject()
{
CMultiDocTemplate* pOpenTemplate = theApp.GetOpenTemplate();
if (m_pMenus[MENU_OPENVIEW] == nullptr)
theApp.m_pOpenTemplate->m_hMenuShared = NewOpenViewMenu();
COpenDoc *pOpenDoc = static_cast<COpenDoc *>(theApp.m_pOpenTemplate->CreateNewDocument());
pOpenTemplate->m_hMenuShared = NewOpenViewMenu();
COpenDoc *pOpenDoc = static_cast<COpenDoc *>(pOpenTemplate->CreateNewDocument());

CFrameWnd * pFrame = GetActiveFrame();
FRAMETYPE frame = pFrame ? GetFrameType(pFrame) : FRAME_OTHER;
Expand Down Expand Up @@ -2430,8 +2435,8 @@ void CMainFrame::OnSaveProject()
}
}

CFrameWnd *pOpenFrame = theApp.m_pOpenTemplate->CreateNewFrame(pOpenDoc, nullptr);
theApp.m_pOpenTemplate->InitialUpdateFrame(pOpenFrame, pOpenDoc);
CFrameWnd *pOpenFrame = pOpenTemplate->CreateNewFrame(pOpenDoc, nullptr);
pOpenTemplate->InitialUpdateFrame(pOpenFrame, pOpenDoc);
}

/**
Expand Down Expand Up @@ -3289,9 +3294,9 @@ void CMainFrame::ReloadMenu()
// m_hMenuDefault is used to redraw the main menu when we close a child frame
// if this child frame had a different menu
pMainFrame->m_hMenuDefault = hNewDefaultMenu;
pApp->m_pOpenTemplate->m_hMenuShared = hNewDefaultMenu;
pApp->m_pDiffTemplate->m_hMenuShared = hNewMergeMenu;
pApp->m_pDirTemplate->m_hMenuShared = hNewDirMenu;
pApp->GetOpenTemplate()->m_hMenuShared = hNewDefaultMenu;
pApp->GetDiffTemplate()->m_hMenuShared = hNewMergeMenu;
pApp->GetDirTemplate()->m_hMenuShared = hNewDirMenu;

// force redrawing the menu bar
pMainFrame->DrawMenuBar();
Expand Down
97 changes: 62 additions & 35 deletions Src/Merge.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -419,41 +419,6 @@ BOOL CMergeApp::InitInstance()
return FALSE;
}

// Register the application's document templates. Document templates
// serve as the connection between documents, frame windows and views.

// Open view
m_pOpenTemplate = new CMultiDocTemplate(
IDR_MAINFRAME,
RUNTIME_CLASS(COpenDoc),
RUNTIME_CLASS(COpenFrame), // custom MDI child frame
RUNTIME_CLASS(COpenView));
AddDocTemplate(m_pOpenTemplate);

// Merge Edit view
m_pDiffTemplate = new CMultiDocTemplate(
IDR_MERGEDOCTYPE,
RUNTIME_CLASS(CMergeDoc),
RUNTIME_CLASS(CMergeEditFrame), // custom MDI child frame
RUNTIME_CLASS(CMergeEditSplitterView));
AddDocTemplate(m_pDiffTemplate);

// Merge Edit view
m_pHexMergeTemplate = new CMultiDocTemplate(
IDR_MERGEDOCTYPE,
RUNTIME_CLASS(CHexMergeDoc),
RUNTIME_CLASS(CHexMergeFrame), // custom MDI child frame
RUNTIME_CLASS(CHexMergeView));
AddDocTemplate(m_pHexMergeTemplate);

// Directory view
m_pDirTemplate = new CMultiDocTemplate(
IDR_DIRDOCTYPE,
RUNTIME_CLASS(CDirDoc),
RUNTIME_CLASS(CDirFrame), // custom MDI child frame
RUNTIME_CLASS(CDirView));
AddDocTemplate(m_pDirTemplate);

// create main MDI Frame window
CMainFrame* pMainFrame = new CMainFrame;
if (!pMainFrame->LoadFrame(IDR_MAINFRAME))
Expand Down Expand Up @@ -500,6 +465,66 @@ BOOL CMergeApp::InitInstance()
return bContinue;
}

CMultiDocTemplate* CMergeApp::GetOpenTemplate()
{
// Open view
if (!m_pOpenTemplate)
{
m_pOpenTemplate = new CMultiDocTemplate(
IDR_MAINFRAME,
RUNTIME_CLASS(COpenDoc),
RUNTIME_CLASS(COpenFrame), // custom MDI child frame
RUNTIME_CLASS(COpenView));
AddDocTemplate(m_pOpenTemplate);
}
return m_pOpenTemplate;
}

CMultiDocTemplate* CMergeApp::GetDiffTemplate()
{
// Merge Edit view
if (!m_pDiffTemplate)
{
m_pDiffTemplate = new CMultiDocTemplate(
IDR_MERGEDOCTYPE,
RUNTIME_CLASS(CMergeDoc),
RUNTIME_CLASS(CMergeEditFrame), // custom MDI child frame
RUNTIME_CLASS(CMergeEditSplitterView));
AddDocTemplate(m_pDiffTemplate);
}
return m_pDiffTemplate;
}

CMultiDocTemplate* CMergeApp::GetHexMergeTemplate()
{
if (!m_pHexMergeTemplate)
{
// Hex Merge view
m_pHexMergeTemplate = new CMultiDocTemplate(
IDR_MERGEDOCTYPE,
RUNTIME_CLASS(CHexMergeDoc),
RUNTIME_CLASS(CHexMergeFrame), // custom MDI child frame
RUNTIME_CLASS(CHexMergeView));
AddDocTemplate(m_pHexMergeTemplate);
}
return m_pHexMergeTemplate;
}

CMultiDocTemplate* CMergeApp::GetDirTemplate()
{
if (!m_pDirTemplate)
{
// Directory view
m_pDirTemplate = new CMultiDocTemplate(
IDR_DIRDOCTYPE,
RUNTIME_CLASS(CDirDoc),
RUNTIME_CLASS(CDirFrame), // custom MDI child frame
RUNTIME_CLASS(CDirView));
AddDocTemplate(m_pDirTemplate);
}
return m_pDirTemplate;
}

static void OpenContributersFile(int&)
{
CMergeApp::OpenFileToExternalEditor(paths::ConcatPath(env::GetProgPath(), ContributorsPath));
Expand Down Expand Up @@ -609,6 +634,8 @@ int CMergeApp::DoMessageBox(const tchar_t* lpszPrompt, UINT nType, UINT nIDPromp

bool CMergeApp::IsReallyIdle() const
{
if (!m_pDirTemplate)
return true;
bool idle = true;
POSITION pos = m_pDirTemplate->GetFirstDocPosition();
while (pos != nullptr)
Expand Down
4 changes: 4 additions & 0 deletions Src/Merge.h
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,10 @@ class CMergeApp : public CWinApp
static void SetupTempPath();
bool IsReallyIdle() const;
void RegisterIdleFunc(std::function<void()> func) { m_idleFuncs.push_back(func); };
CMultiDocTemplate* GetOpenTemplate();
CMultiDocTemplate* GetDiffTemplate();
CMultiDocTemplate* GetHexMergeTemplate();
CMultiDocTemplate* GetDirTemplate();

virtual UINT GetProfileInt(const tchar_t* lpszSection, const tchar_t* lpszEntry, int nDefault) override;
virtual BOOL WriteProfileInt(const tchar_t* lpszSection, const tchar_t* lpszEntry, int nValue) override;
Expand Down

0 comments on commit 3bb5701

Please sign in to comment.