Change file selector to work with callbacks.

This commit is contained in:
Bartosz Taudul 2022-10-06 17:35:07 +02:00
parent a4e39f3d5f
commit ec0757c03a
No known key found for this signature in database
GPG Key ID: B7FE2008B7575DF3
6 changed files with 82 additions and 94 deletions

View File

@ -575,12 +575,10 @@ 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() )
{ {
auto fn = tracy::Fileselector::OpenFile( "tracy", "Tracy Profiler trace file" ); tracy::Fileselector::OpenFile( "tracy", "Tracy Profiler trace file", []( const char* fn ) {
if( !fn.empty() )
{
try try
{ {
auto f = std::shared_ptr<tracy::FileRead>( tracy::FileRead::Open( fn.c_str() ) ); auto f = std::shared_ptr<tracy::FileRead>( tracy::FileRead::Open( fn ) );
if( f ) if( f )
{ {
loadThread = std::thread( [f] { loadThread = std::thread( [f] {
@ -609,7 +607,7 @@ static void DrawContents()
{ {
badVer.state = tracy::BadVersionState::ReadError; badVer.state = tracy::BadVersionState::ReadError;
} }
} } );
} }
if( badVer.state != tracy::BadVersionState::Ok ) if( badVer.state != tracy::BadVersionState::Ok )

View File

@ -21,34 +21,30 @@ void Shutdown()
#endif #endif
} }
std::string OpenFile( const char* ext, const char* desc ) void OpenFile( const char* ext, const char* desc, std::function<void(const char*)> callback )
{ {
std::string ret;
#ifndef TRACY_NO_FILESELECTOR #ifndef TRACY_NO_FILESELECTOR
nfdu8filteritem_t filter = { desc, ext }; nfdu8filteritem_t filter = { desc, ext };
nfdu8char_t* fn; nfdu8char_t* fn;
if( NFD_OpenDialogU8( &fn, &filter, 1, nullptr ) == NFD_OKAY ) if( NFD_OpenDialogU8( &fn, &filter, 1, nullptr ) == NFD_OKAY )
{ {
ret.assign( fn ); callback( (const char*)fn );
NFD_FreePathU8( fn ); NFD_FreePathU8( fn );
} }
#endif #endif
return ret;
} }
std::string SaveFile( const char* ext, const char* desc ) void SaveFile( const char* ext, const char* desc, std::function<void(const char*)> callback )
{ {
std::string ret;
#ifndef TRACY_NO_FILESELECTOR #ifndef TRACY_NO_FILESELECTOR
nfdu8filteritem_t filter = { desc, ext }; nfdu8filteritem_t filter = { desc, ext };
nfdu8char_t* fn; nfdu8char_t* fn;
if( NFD_SaveDialogU8( &fn, &filter, 1, nullptr, nullptr ) == NFD_OKAY ) if( NFD_SaveDialogU8( &fn, &filter, 1, nullptr, nullptr ) == NFD_OKAY )
{ {
ret.assign( fn ); callback( (const char*)fn );
NFD_FreePathU8( fn ); NFD_FreePathU8( fn );
} }
#endif #endif
return ret;
} }
} }

View File

@ -1,7 +1,7 @@
#ifndef __TRACYFILESELECTOR_HPP__ #ifndef __TRACYFILESELECTOR_HPP__
#define __TRACYFILESELECTOR_HPP__ #define __TRACYFILESELECTOR_HPP__
#include <string> #include <functional>
namespace tracy::Fileselector namespace tracy::Fileselector
{ {
@ -9,8 +9,8 @@ namespace tracy::Fileselector
void Init(); void Init();
void Shutdown(); void Shutdown();
std::string OpenFile( const char* ext, const char* desc ); void OpenFile( const char* ext, const char* desc, std::function<void(const char*)> callback );
std::string SaveFile( const char* ext, const char* desc ); void SaveFile( const char* ext, const char* desc, std::function<void(const char*)> callback );
} }

View File

@ -5356,23 +5356,20 @@ 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 );
auto fn = Fileselector::SaveFile( "asm", "Assembly file" ); Fileselector::SaveFile( "asm", "Assembly file", [this, &worker, start, stop]( const char* fn ) {
if( !fn.empty() )
{
FILE* f = nullptr; FILE* f = nullptr;
const auto sz = fn.size(); const auto sz = strlen( fn );
if( sz < 5 || memcmp( fn.c_str() + sz - 4, ".asm", 4 ) != 0 ) if( sz < 5 || memcmp( fn + sz - 4, ".asm", 4 ) != 0 )
{ {
char tmp[1024]; char tmp[1024];
sprintf( tmp, "%s.asm", fn.c_str() ); sprintf( tmp, "%s.asm", fn );
f = fopen( tmp, "wb" ); f = fopen( tmp, "wb" );
} }
else else
{ {
f = fopen( fn.c_str(), "wb" ); f = fopen( fn, "wb" );
} }
if( f ) if( !f ) return;
{
char tmp[16]; char tmp[16];
auto sym = worker.GetSymbolData( m_symAddr ); auto sym = worker.GetSymbolData( m_symAddr );
assert( sym ); assert( sym );
@ -5429,8 +5426,7 @@ void SourceView::Save( const Worker& worker, size_t start, size_t stop )
} }
} }
fclose( f ); fclose( f );
} } );
}
} }
#endif #endif

View File

@ -149,12 +149,10 @@ 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() )
{ {
auto fn = Fileselector::OpenFile( "tracy", "Tracy Profiler trace file" ); Fileselector::OpenFile( "tracy", "Tracy Profiler trace file", [this]( const char* fn ) {
if( !fn.empty() )
{
try try
{ {
auto f = std::shared_ptr<tracy::FileRead>( tracy::FileRead::Open( fn.c_str() ) ); auto f = std::shared_ptr<tracy::FileRead>( tracy::FileRead::Open( fn ) );
if( f ) if( f )
{ {
m_compare.loadThread = std::thread( [this, f] { m_compare.loadThread = std::thread( [this, f] {
@ -179,7 +177,7 @@ void View::DrawCompare()
{ {
m_compare.badVer.state = BadVersionState::ReadError; m_compare.badVer.state = BadVersionState::ReadError;
} }
} } );
} }
tracy::BadVersion( m_compare.badVer, m_bigFont ); tracy::BadVersion( m_compare.badVer, m_bigFont );
ImGui::End(); ImGui::End();

View File

@ -121,25 +121,25 @@ bool View::DrawConnection()
ImGui::Separator(); ImGui::Separator();
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 auto cb = [this]( const char* fn ) {
auto fn = Fileselector::SaveFile( "tracy", "Tracy Profiler trace file" ); const auto sz = strlen( fn );
if( !fn.empty() ) if( sz < 7 || memcmp( fn + sz - 6, ".tracy", 6 ) != 0 )
#else
std::string fn = "trace.tracy";
#endif
{
const auto sz = fn.size();
if( sz < 7 || memcmp( fn.c_str() + sz - 6, ".tracy", 6 ) != 0 )
{ {
char tmp[1024]; char tmp[1024];
sprintf( tmp, "%s.tracy", fn.c_str() ); sprintf( tmp, "%s.tracy", fn );
m_filenameStaging = tmp; m_filenameStaging = tmp;
} }
else else
{ {
m_filenameStaging = std::move( fn ); m_filenameStaging = fn;
}
} }
};
#ifndef TRACY_NO_FILESELECTOR
Fileselector::SaveFile( "tracy", "Tracy Profiler trace file", cb );
#else
cb( "trace.tracy" );
#endif
} }
ImGui::SameLine( 0, 2 * ty ); ImGui::SameLine( 0, 2 * ty );