From 3ca61ad2278c9e41a6ad5c65df4c8b7afc047dd0 Mon Sep 17 00:00:00 2001 From: Bartosz Taudul Date: Wed, 5 Oct 2022 22:50:17 +0200 Subject: [PATCH] Extract file selector functionality. --- profiler/build/win32/Tracy.vcxproj | 2 + profiler/build/win32/Tracy.vcxproj.filters | 6 +++ profiler/src/main.cpp | 22 +++------ server/TracyFileselector.cpp | 54 ++++++++++++++++++++++ server/TracyFileselector.hpp | 17 +++++++ server/TracySourceView.cpp | 18 +++----- server/TracyView_Compare.cpp | 14 ++---- server/TracyView_ConnectionState.cpp | 24 ++++------ 8 files changed, 103 insertions(+), 54 deletions(-) create mode 100644 server/TracyFileselector.cpp create mode 100644 server/TracyFileselector.hpp diff --git a/profiler/build/win32/Tracy.vcxproj b/profiler/build/win32/Tracy.vcxproj index bc99f75a..834e38c5 100644 --- a/profiler/build/win32/Tracy.vcxproj +++ b/profiler/build/win32/Tracy.vcxproj @@ -120,6 +120,7 @@ + @@ -255,6 +256,7 @@ + diff --git a/profiler/build/win32/Tracy.vcxproj.filters b/profiler/build/win32/Tracy.vcxproj.filters index e6d4ba20..a97ea0b0 100644 --- a/profiler/build/win32/Tracy.vcxproj.filters +++ b/profiler/build/win32/Tracy.vcxproj.filters @@ -375,6 +375,9 @@ server + + server + @@ -758,6 +761,9 @@ server + + server + diff --git a/profiler/src/main.cpp b/profiler/src/main.cpp index 2756a028..d21ae81d 100644 --- a/profiler/src/main.cpp +++ b/profiler/src/main.cpp @@ -14,10 +14,6 @@ #include #include -#ifndef TRACY_NO_FILESELECTOR -# include "../nfd/nfd.h" -#endif - #ifdef _WIN32 # include #endif @@ -36,6 +32,7 @@ #include "../../server/TracyBadVersion.hpp" #include "../../server/TracyFileHeader.hpp" #include "../../server/TracyFileRead.hpp" +#include "../../server/TracyFileselector.hpp" #include "../../server/TracyImGui.hpp" #include "../../server/TracyMouse.hpp" #include "../../server/TracyPrint.hpp" @@ -241,9 +238,7 @@ int main( int argc, char** argv ) view = std::make_unique( RunOnMainThread, connectTo, port, s_fixedWidth, s_smallFont, s_bigFont, SetWindowTitleCallback, SetupScaleCallback ); } -#ifndef TRACY_NO_FILESELECTOR - NFD_Init(); -#endif + tracy::Fileselector::Init(); backend.Show(); backend.Run(); @@ -256,9 +251,7 @@ int main( int argc, char** argv ) tracy::FreeTexture( iconTex, RunOnMainThread ); free( iconPx ); -#ifndef TRACY_NO_FILESELECTOR - NFD_Quit(); -#endif + tracy::Fileselector::Shutdown(); return 0; } @@ -582,14 +575,12 @@ static void DrawContents() #ifndef TRACY_NO_FILESELECTOR if( ImGui::Button( ICON_FA_FOLDER_OPEN " Open saved trace" ) && !loadThread.joinable() ) { - nfdu8filteritem_t filter = { "Tracy Profiler trace file", "tracy" }; - nfdu8char_t* fn; - auto res = NFD_OpenDialogU8( &fn, &filter, 1, nullptr ); - if( res == NFD_OKAY ) + auto fn = tracy::Fileselector::OpenFile( "tracy", "Tracy Profiler trace file" ); + if( !fn.empty() ) { try { - auto f = std::shared_ptr( tracy::FileRead::Open( fn ) ); + auto f = std::shared_ptr( tracy::FileRead::Open( fn.c_str() ) ); if( f ) { loadThread = std::thread( [f] { @@ -618,7 +609,6 @@ static void DrawContents() { badVer.state = tracy::BadVersionState::ReadError; } - NFD_FreePathU8( fn ); } } diff --git a/server/TracyFileselector.cpp b/server/TracyFileselector.cpp new file mode 100644 index 00000000..9efc361a --- /dev/null +++ b/server/TracyFileselector.cpp @@ -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; +} + +} diff --git a/server/TracyFileselector.hpp b/server/TracyFileselector.hpp new file mode 100644 index 00000000..be082376 --- /dev/null +++ b/server/TracyFileselector.hpp @@ -0,0 +1,17 @@ +#ifndef __TRACYFILESELECTOR_HPP__ +#define __TRACYFILESELECTOR_HPP__ + +#include + +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 diff --git a/server/TracySourceView.cpp b/server/TracySourceView.cpp index 72312840..6834d3e4 100644 --- a/server/TracySourceView.cpp +++ b/server/TracySourceView.cpp @@ -7,6 +7,7 @@ #include "imgui.h" #include "TracyCharUtil.hpp" #include "TracyColor.hpp" +#include "TracyFileselector.hpp" #include "TracyFilesystem.hpp" #include "TracyImGui.hpp" #include "TracyMicroArchitecture.hpp" @@ -19,10 +20,6 @@ #include "IconsFontAwesome6.h" -#ifndef TRACY_NO_FILESELECTOR -# include "../nfd/nfd.h" -#endif - 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 < stop ); - nfdu8filteritem_t filter = { "Assembly file", "asm" }; - nfdu8char_t* fn; - auto res = NFD_SaveDialogU8( &fn, &filter, 1, nullptr, nullptr ); - if( res == NFD_OKAY ) + auto fn = Fileselector::SaveFile( "asm", "Assembly file" ); + if( !fn.empty() ) { FILE* f = nullptr; - const auto sz = strlen( fn ); - if( sz < 5 || memcmp( fn + sz - 4, ".asm", 4 ) != 0 ) + const auto sz = fn.size(); + if( sz < 5 || memcmp( fn.c_str() + sz - 4, ".asm", 4 ) != 0 ) { char tmp[1024]; sprintf( tmp, "%s.asm", fn ); @@ -5374,7 +5369,7 @@ void SourceView::Save( const Worker& worker, size_t start, size_t stop ) } else { - f = fopen( fn, "wb" ); + f = fopen( fn.c_str(), "wb" ); } if( f ) { @@ -5435,7 +5430,6 @@ void SourceView::Save( const Worker& worker, size_t start, size_t stop ) } fclose( f ); } - NFD_FreePathU8( fn ); } } #endif diff --git a/server/TracyView_Compare.cpp b/server/TracyView_Compare.cpp index ea1e3b3f..cb4200ca 100644 --- a/server/TracyView_Compare.cpp +++ b/server/TracyView_Compare.cpp @@ -1,11 +1,8 @@ #include -#ifndef TRACY_NO_FILESELECTOR -# include "../nfd/nfd.h" -#endif - #include "TracyImGui.hpp" #include "TracyFileRead.hpp" +#include "TracyFileselector.hpp" #include "TracyPrint.hpp" #include "TracyView.hpp" @@ -152,14 +149,12 @@ void View::DrawCompare() ImGui::TextWrapped( "Please load a second trace to compare results." ); if( ImGui::Button( ICON_FA_FOLDER_OPEN " Open second trace" ) && !m_compare.loadThread.joinable() ) { - nfdu8filteritem_t filter = { "Tracy Profiler trace file", "tracy" }; - nfdu8char_t* fn; - auto res = NFD_OpenDialogU8( &fn, &filter, 1, nullptr ); - if( res == NFD_OKAY ) + auto fn = Fileselector::OpenFile( "tracy", "Tracy Profiler trace file" ); + if( !fn.empty() ) { try { - auto f = std::shared_ptr( tracy::FileRead::Open( fn ) ); + auto f = std::shared_ptr( tracy::FileRead::Open( fn.c_str() ) ); if( f ) { m_compare.loadThread = std::thread( [this, f] { @@ -184,7 +179,6 @@ void View::DrawCompare() { m_compare.badVer.state = BadVersionState::ReadError; } - NFD_FreePathU8( fn ); } } tracy::BadVersion( m_compare.badVer, m_bigFont ); diff --git a/server/TracyView_ConnectionState.cpp b/server/TracyView_ConnectionState.cpp index 61f8d71d..53e8161e 100644 --- a/server/TracyView_ConnectionState.cpp +++ b/server/TracyView_ConnectionState.cpp @@ -1,7 +1,4 @@ -#ifndef TRACY_NO_FILESELECTOR -# include "../nfd/nfd.h" -#endif - +#include "TracyFileselector.hpp" #include "TracyImGui.hpp" #include "TracyPrint.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 ) { #ifndef TRACY_NO_FILESELECTOR - nfdu8filteritem_t filter = { "Tracy Profiler trace file", "tracy" }; - nfdu8char_t* fn; - auto res = NFD_SaveDialogU8( &fn, &filter, 1, nullptr, nullptr ); - if( res == NFD_OKAY ) + auto fn = Fileselector::SaveFile( "tracy", "Tracy Profiler trace file" ); + if( !fn.empty() ) #else - const char* fn = "trace.tracy"; + std::string fn = "trace.tracy"; #endif { - const auto sz = strlen( fn ); - if( sz < 7 || memcmp( fn + sz - 6, ".tracy", 6 ) != 0 ) + const auto sz = fn.size(); + if( sz < 7 || memcmp( fn.c_str() + sz - 6, ".tracy", 6 ) != 0 ) { char tmp[1024]; - sprintf( tmp, "%s.tracy", fn ); + sprintf( tmp, "%s.tracy", fn.c_str() ); m_filenameStaging = tmp; } else { - m_filenameStaging = fn; + m_filenameStaging = std::move( fn ); } -#ifndef TRACY_NO_FILESELECTOR - NFD_FreePathU8( fn ); -#endif } }