tracy/server/TracyFileselector.cpp

87 lines
2.0 KiB
C++
Raw Normal View History

2022-10-05 20:50:17 +00:00
#include "TracyFileselector.hpp"
#ifndef TRACY_NO_FILESELECTOR
2022-10-06 15:42:57 +00:00
# ifdef __EMSCRIPTEN__
# include <emscripten.h>
# else
# include "../nfd/nfd.h"
# endif
2022-10-05 20:50:17 +00:00
#endif
namespace tracy::Fileselector
{
void Init()
{
2022-10-06 15:42:57 +00:00
#if !defined TRACY_NO_FILESELECTOR && !defined __EMSCRIPTEN__
2022-10-05 20:50:17 +00:00
NFD_Init();
#endif
}
void Shutdown()
{
2022-10-06 15:42:57 +00:00
#if !defined TRACY_NO_FILESELECTOR && !defined __EMSCRIPTEN__
2022-10-05 20:50:17 +00:00
NFD_Quit();
#endif
}
2022-10-06 15:42:57 +00:00
#ifdef __EMSCRIPTEN__
static std::function<void(const char*)> s_openFileCallback;
extern "C" int nativeOpenFile()
{
s_openFileCallback( "upload.tracy" );
return 0;
}
#endif
void OpenFile( const char* ext, const char* desc, std::function<void(const char*)> callback )
2022-10-05 20:50:17 +00:00
{
#ifndef TRACY_NO_FILESELECTOR
2022-10-06 15:42:57 +00:00
# ifdef __EMSCRIPTEN__
s_openFileCallback = callback;
EM_ASM( {
var input = document.createElement( 'input' );
input.type = 'file';
input.accept = UTF8ToString( $0 );
input.onchange = (e) => {
var file = e.target.files[0];
var reader = new FileReader();
reader.readAsArrayBuffer( file );
reader.onload = () => {
var buf = reader.result;
var view = new Uint8Array( buf );
FS.createDataFile( '/', 'upload.tracy', view, true, true );
Module.ccall( 'nativeOpenFile', 'number', [], [] );
FS.unlink( '/upload.tracy' );
};
};
input.click();
}, ext );
# else
2022-10-05 20:50:17 +00:00
nfdu8filteritem_t filter = { desc, ext };
nfdu8char_t* fn;
if( NFD_OpenDialogU8( &fn, &filter, 1, nullptr ) == NFD_OKAY )
{
callback( (const char*)fn );
2022-10-05 20:50:17 +00:00
NFD_FreePathU8( fn );
}
2022-10-06 15:42:57 +00:00
# endif
2022-10-05 20:50:17 +00:00
#endif
}
void SaveFile( const char* ext, const char* desc, std::function<void(const char*)> callback )
2022-10-05 20:50:17 +00:00
{
2022-10-06 15:42:57 +00:00
#if !defined TRACY_NO_FILESELECTOR && !defined __EMSCRIPTEN__
2022-10-05 20:50:17 +00:00
nfdu8filteritem_t filter = { desc, ext };
nfdu8char_t* fn;
if( NFD_SaveDialogU8( &fn, &filter, 1, nullptr, nullptr ) == NFD_OKAY )
{
callback( (const char*)fn );
2022-10-05 20:50:17 +00:00
NFD_FreePathU8( fn );
}
#endif
}
}