Extract file selector functionality.

This commit is contained in:
Bartosz Taudul 2022-10-05 22:50:17 +02:00
parent 5940af8995
commit 3ca61ad227
No known key found for this signature in database
GPG Key ID: B7FE2008B7575DF3
8 changed files with 103 additions and 54 deletions

View File

@ -120,6 +120,7 @@
<ClCompile Include="..\..\..\public\common\tracy_lz4hc.cpp" /> <ClCompile Include="..\..\..\public\common\tracy_lz4hc.cpp" />
<ClCompile Include="..\..\..\server\TracyBadVersion.cpp" /> <ClCompile Include="..\..\..\server\TracyBadVersion.cpp" />
<ClCompile Include="..\..\..\server\TracyColor.cpp" /> <ClCompile Include="..\..\..\server\TracyColor.cpp" />
<ClCompile Include="..\..\..\server\TracyFileselector.cpp" />
<ClCompile Include="..\..\..\server\TracyFilesystem.cpp" /> <ClCompile Include="..\..\..\server\TracyFilesystem.cpp" />
<ClCompile Include="..\..\..\server\TracyImGui.cpp" /> <ClCompile Include="..\..\..\server\TracyImGui.cpp" />
<ClCompile Include="..\..\..\server\TracyMemory.cpp" /> <ClCompile Include="..\..\..\server\TracyMemory.cpp" />
@ -255,6 +256,7 @@
<ClInclude Include="..\..\..\server\TracyEvent.hpp" /> <ClInclude Include="..\..\..\server\TracyEvent.hpp" />
<ClInclude Include="..\..\..\server\TracyFileHeader.hpp" /> <ClInclude Include="..\..\..\server\TracyFileHeader.hpp" />
<ClInclude Include="..\..\..\server\TracyFileRead.hpp" /> <ClInclude Include="..\..\..\server\TracyFileRead.hpp" />
<ClInclude Include="..\..\..\server\TracyFileselector.hpp" />
<ClInclude Include="..\..\..\server\TracyFilesystem.hpp" /> <ClInclude Include="..\..\..\server\TracyFilesystem.hpp" />
<ClInclude Include="..\..\..\server\TracyFileWrite.hpp" /> <ClInclude Include="..\..\..\server\TracyFileWrite.hpp" />
<ClInclude Include="..\..\..\server\TracyImGui.hpp" /> <ClInclude Include="..\..\..\server\TracyImGui.hpp" />

View File

@ -375,6 +375,9 @@
<ClCompile Include="..\..\..\server\TracyTimelineItemCpuData.cpp"> <ClCompile Include="..\..\..\server\TracyTimelineItemCpuData.cpp">
<Filter>server</Filter> <Filter>server</Filter>
</ClCompile> </ClCompile>
<ClCompile Include="..\..\..\server\TracyFileselector.cpp">
<Filter>server</Filter>
</ClCompile>
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<ClInclude Include="..\..\..\server\TracyEvent.hpp"> <ClInclude Include="..\..\..\server\TracyEvent.hpp">
@ -758,6 +761,9 @@
<ClInclude Include="..\..\..\server\TracyTimelineItemCpuData.hpp"> <ClInclude Include="..\..\..\server\TracyTimelineItemCpuData.hpp">
<Filter>server</Filter> <Filter>server</Filter>
</ClInclude> </ClInclude>
<ClInclude Include="..\..\..\server\TracyFileselector.hpp">
<Filter>server</Filter>
</ClInclude>
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<Natvis Include="DebugVis.natvis" /> <Natvis Include="DebugVis.natvis" />

View File

@ -14,10 +14,6 @@
#include <sys/stat.h> #include <sys/stat.h>
#include <locale.h> #include <locale.h>
#ifndef TRACY_NO_FILESELECTOR
# include "../nfd/nfd.h"
#endif
#ifdef _WIN32 #ifdef _WIN32
# include <windows.h> # include <windows.h>
#endif #endif
@ -36,6 +32,7 @@
#include "../../server/TracyBadVersion.hpp" #include "../../server/TracyBadVersion.hpp"
#include "../../server/TracyFileHeader.hpp" #include "../../server/TracyFileHeader.hpp"
#include "../../server/TracyFileRead.hpp" #include "../../server/TracyFileRead.hpp"
#include "../../server/TracyFileselector.hpp"
#include "../../server/TracyImGui.hpp" #include "../../server/TracyImGui.hpp"
#include "../../server/TracyMouse.hpp" #include "../../server/TracyMouse.hpp"
#include "../../server/TracyPrint.hpp" #include "../../server/TracyPrint.hpp"
@ -241,9 +238,7 @@ int main( int argc, char** argv )
view = std::make_unique<tracy::View>( RunOnMainThread, connectTo, port, s_fixedWidth, s_smallFont, s_bigFont, SetWindowTitleCallback, SetupScaleCallback ); view = std::make_unique<tracy::View>( RunOnMainThread, connectTo, port, s_fixedWidth, s_smallFont, s_bigFont, SetWindowTitleCallback, SetupScaleCallback );
} }
#ifndef TRACY_NO_FILESELECTOR tracy::Fileselector::Init();
NFD_Init();
#endif
backend.Show(); backend.Show();
backend.Run(); backend.Run();
@ -256,9 +251,7 @@ int main( int argc, char** argv )
tracy::FreeTexture( iconTex, RunOnMainThread ); tracy::FreeTexture( iconTex, RunOnMainThread );
free( iconPx ); free( iconPx );
#ifndef TRACY_NO_FILESELECTOR tracy::Fileselector::Shutdown();
NFD_Quit();
#endif
return 0; return 0;
} }
@ -582,14 +575,12 @@ static void DrawContents()
#ifndef TRACY_NO_FILESELECTOR #ifndef TRACY_NO_FILESELECTOR
if( ImGui::Button( ICON_FA_FOLDER_OPEN " Open saved trace" ) && !loadThread.joinable() ) if( ImGui::Button( ICON_FA_FOLDER_OPEN " Open saved trace" ) && !loadThread.joinable() )
{ {
nfdu8filteritem_t filter = { "Tracy Profiler trace file", "tracy" }; auto fn = tracy::Fileselector::OpenFile( "tracy", "Tracy Profiler trace file" );
nfdu8char_t* fn; if( !fn.empty() )
auto res = NFD_OpenDialogU8( &fn, &filter, 1, nullptr );
if( res == NFD_OKAY )
{ {
try try
{ {
auto f = std::shared_ptr<tracy::FileRead>( tracy::FileRead::Open( fn ) ); auto f = std::shared_ptr<tracy::FileRead>( tracy::FileRead::Open( fn.c_str() ) );
if( f ) if( f )
{ {
loadThread = std::thread( [f] { loadThread = std::thread( [f] {
@ -618,7 +609,6 @@ static void DrawContents()
{ {
badVer.state = tracy::BadVersionState::ReadError; badVer.state = tracy::BadVersionState::ReadError;
} }
NFD_FreePathU8( fn );
} }
} }

View File

@ -0,0 +1,54 @@
#include "TracyFileselector.hpp"
#ifndef TRACY_NO_FILESELECTOR
# include "../nfd/nfd.h"
#endif
namespace tracy::Fileselector
{
void Init()
{
#ifndef TRACY_NO_FILESELECTOR
NFD_Init();
#endif
}
void Shutdown()
{
#ifndef TRACY_NO_FILESELECTOR
NFD_Quit();
#endif
}
std::string OpenFile( const char* ext, const char* desc )
{
std::string ret;
#ifndef TRACY_NO_FILESELECTOR
nfdu8filteritem_t filter = { desc, ext };
nfdu8char_t* fn;
if( NFD_OpenDialogU8( &fn, &filter, 1, nullptr ) == NFD_OKAY )
{
ret.assign( fn );
NFD_FreePathU8( fn );
}
#endif
return ret;
}
std::string SaveFile( const char* ext, const char* desc )
{
std::string ret;
#ifndef TRACY_NO_FILESELECTOR
nfdu8filteritem_t filter = { desc, ext };
nfdu8char_t* fn;
if( NFD_SaveDialogU8( &fn, &filter, 1, nullptr, nullptr ) == NFD_OKAY )
{
ret.assign( fn );
NFD_FreePathU8( fn );
}
#endif
return ret;
}
}

View File

@ -0,0 +1,17 @@
#ifndef __TRACYFILESELECTOR_HPP__
#define __TRACYFILESELECTOR_HPP__
#include <string>
namespace tracy::Fileselector
{
void Init();
void Shutdown();
std::string OpenFile( const char* ext, const char* desc );
std::string SaveFile( const char* ext, const char* desc );
}
#endif

View File

@ -7,6 +7,7 @@
#include "imgui.h" #include "imgui.h"
#include "TracyCharUtil.hpp" #include "TracyCharUtil.hpp"
#include "TracyColor.hpp" #include "TracyColor.hpp"
#include "TracyFileselector.hpp"
#include "TracyFilesystem.hpp" #include "TracyFilesystem.hpp"
#include "TracyImGui.hpp" #include "TracyImGui.hpp"
#include "TracyMicroArchitecture.hpp" #include "TracyMicroArchitecture.hpp"
@ -19,10 +20,6 @@
#include "IconsFontAwesome6.h" #include "IconsFontAwesome6.h"
#ifndef TRACY_NO_FILESELECTOR
# include "../nfd/nfd.h"
#endif
namespace tracy namespace tracy
{ {
@ -5359,14 +5356,12 @@ void SourceView::Save( const Worker& worker, size_t start, size_t stop )
assert( start < m_asm.size() ); assert( start < m_asm.size() );
assert( start < stop ); assert( start < stop );
nfdu8filteritem_t filter = { "Assembly file", "asm" }; auto fn = Fileselector::SaveFile( "asm", "Assembly file" );
nfdu8char_t* fn; if( !fn.empty() )
auto res = NFD_SaveDialogU8( &fn, &filter, 1, nullptr, nullptr );
if( res == NFD_OKAY )
{ {
FILE* f = nullptr; FILE* f = nullptr;
const auto sz = strlen( fn ); const auto sz = fn.size();
if( sz < 5 || memcmp( fn + sz - 4, ".asm", 4 ) != 0 ) if( sz < 5 || memcmp( fn.c_str() + sz - 4, ".asm", 4 ) != 0 )
{ {
char tmp[1024]; char tmp[1024];
sprintf( tmp, "%s.asm", fn ); sprintf( tmp, "%s.asm", fn );
@ -5374,7 +5369,7 @@ void SourceView::Save( const Worker& worker, size_t start, size_t stop )
} }
else else
{ {
f = fopen( fn, "wb" ); f = fopen( fn.c_str(), "wb" );
} }
if( f ) if( f )
{ {
@ -5435,7 +5430,6 @@ void SourceView::Save( const Worker& worker, size_t start, size_t stop )
} }
fclose( f ); fclose( f );
} }
NFD_FreePathU8( fn );
} }
} }
#endif #endif

View File

@ -1,11 +1,8 @@
#include <numeric> #include <numeric>
#ifndef TRACY_NO_FILESELECTOR
# include "../nfd/nfd.h"
#endif
#include "TracyImGui.hpp" #include "TracyImGui.hpp"
#include "TracyFileRead.hpp" #include "TracyFileRead.hpp"
#include "TracyFileselector.hpp"
#include "TracyPrint.hpp" #include "TracyPrint.hpp"
#include "TracyView.hpp" #include "TracyView.hpp"
@ -152,14 +149,12 @@ void View::DrawCompare()
ImGui::TextWrapped( "Please load a second trace to compare results." ); ImGui::TextWrapped( "Please load a second trace to compare results." );
if( ImGui::Button( ICON_FA_FOLDER_OPEN " Open second trace" ) && !m_compare.loadThread.joinable() ) if( ImGui::Button( ICON_FA_FOLDER_OPEN " Open second trace" ) && !m_compare.loadThread.joinable() )
{ {
nfdu8filteritem_t filter = { "Tracy Profiler trace file", "tracy" }; auto fn = Fileselector::OpenFile( "tracy", "Tracy Profiler trace file" );
nfdu8char_t* fn; if( !fn.empty() )
auto res = NFD_OpenDialogU8( &fn, &filter, 1, nullptr );
if( res == NFD_OKAY )
{ {
try try
{ {
auto f = std::shared_ptr<tracy::FileRead>( tracy::FileRead::Open( fn ) ); auto f = std::shared_ptr<tracy::FileRead>( tracy::FileRead::Open( fn.c_str() ) );
if( f ) if( f )
{ {
m_compare.loadThread = std::thread( [this, f] { m_compare.loadThread = std::thread( [this, f] {
@ -184,7 +179,6 @@ void View::DrawCompare()
{ {
m_compare.badVer.state = BadVersionState::ReadError; m_compare.badVer.state = BadVersionState::ReadError;
} }
NFD_FreePathU8( fn );
} }
} }
tracy::BadVersion( m_compare.badVer, m_bigFont ); tracy::BadVersion( m_compare.badVer, m_bigFont );

View File

@ -1,7 +1,4 @@
#ifndef TRACY_NO_FILESELECTOR #include "TracyFileselector.hpp"
# include "../nfd/nfd.h"
#endif
#include "TracyImGui.hpp" #include "TracyImGui.hpp"
#include "TracyPrint.hpp" #include "TracyPrint.hpp"
#include "TracyTexture.hpp" #include "TracyTexture.hpp"
@ -125,28 +122,23 @@ bool View::DrawConnection()
if( ImGui::Button( ICON_FA_FLOPPY_DISK " Save trace" ) && m_saveThreadState.load( std::memory_order_relaxed ) == SaveThreadState::Inert ) if( ImGui::Button( ICON_FA_FLOPPY_DISK " Save trace" ) && m_saveThreadState.load( std::memory_order_relaxed ) == SaveThreadState::Inert )
{ {
#ifndef TRACY_NO_FILESELECTOR #ifndef TRACY_NO_FILESELECTOR
nfdu8filteritem_t filter = { "Tracy Profiler trace file", "tracy" }; auto fn = Fileselector::SaveFile( "tracy", "Tracy Profiler trace file" );
nfdu8char_t* fn; if( !fn.empty() )
auto res = NFD_SaveDialogU8( &fn, &filter, 1, nullptr, nullptr );
if( res == NFD_OKAY )
#else #else
const char* fn = "trace.tracy"; std::string fn = "trace.tracy";
#endif #endif
{ {
const auto sz = strlen( fn ); const auto sz = fn.size();
if( sz < 7 || memcmp( fn + sz - 6, ".tracy", 6 ) != 0 ) if( sz < 7 || memcmp( fn.c_str() + sz - 6, ".tracy", 6 ) != 0 )
{ {
char tmp[1024]; char tmp[1024];
sprintf( tmp, "%s.tracy", fn ); sprintf( tmp, "%s.tracy", fn.c_str() );
m_filenameStaging = tmp; m_filenameStaging = tmp;
} }
else else
{ {
m_filenameStaging = fn; m_filenameStaging = std::move( fn );
} }
#ifndef TRACY_NO_FILESELECTOR
NFD_FreePathU8( fn );
#endif
} }
} }