Change way of updating fonts in View.

This makes the whole process more easy to follow. It also fixes a crash
bug that was occuring when Wayland output scale changed.
This commit is contained in:
Bartosz Taudul 2024-03-01 23:43:53 +01:00
parent bdfcc5b814
commit b329eb1fcc
No known key found for this signature in database
GPG Key ID: B7FE2008B7575DF3
5 changed files with 28 additions and 25 deletions

View File

@ -14,7 +14,7 @@ ImFont* s_bigFont;
ImFont* s_smallFont; ImFont* s_smallFont;
ImFont* s_fixedWidth; ImFont* s_fixedWidth;
void LoadFonts( float scale, ImFont*& cb_fixedWidth, ImFont*& cb_bigFont, ImFont*& cb_smallFont ) void LoadFonts( float scale )
{ {
static const ImWchar rangesBasic[] = { static const ImWchar rangesBasic[] = {
0x0020, 0x00FF, // Basic Latin + Latin Supplement 0x0020, 0x00FF, // Basic Latin + Latin Supplement
@ -49,10 +49,10 @@ void LoadFonts( float scale, ImFont*& cb_fixedWidth, ImFont*& cb_bigFont, ImFont
io.Fonts->Clear(); io.Fonts->Clear();
io.Fonts->AddFontFromMemoryCompressedTTF( tracy::DroidSans_compressed_data, tracy::DroidSans_compressed_size, round( 15.0f * scale ), &configBasic, rangesBasic ); io.Fonts->AddFontFromMemoryCompressedTTF( tracy::DroidSans_compressed_data, tracy::DroidSans_compressed_size, round( 15.0f * scale ), &configBasic, rangesBasic );
io.Fonts->AddFontFromMemoryCompressedTTF( tracy::FontAwesomeSolid_compressed_data, tracy::FontAwesomeSolid_compressed_size, round( 14.0f * scale ), &configMerge, rangesIcons ); io.Fonts->AddFontFromMemoryCompressedTTF( tracy::FontAwesomeSolid_compressed_data, tracy::FontAwesomeSolid_compressed_size, round( 14.0f * scale ), &configMerge, rangesIcons );
s_fixedWidth = cb_fixedWidth = io.Fonts->AddFontFromMemoryCompressedTTF( tracy::FiraCodeRetina_compressed_data, tracy::FiraCodeRetina_compressed_size, round( 15.0f * scale ), &configFixed, rangesFixed ); s_fixedWidth = io.Fonts->AddFontFromMemoryCompressedTTF( tracy::FiraCodeRetina_compressed_data, tracy::FiraCodeRetina_compressed_size, round( 15.0f * scale ), &configFixed, rangesFixed );
s_bigFont = cb_bigFont = io.Fonts->AddFontFromMemoryCompressedTTF( tracy::DroidSans_compressed_data, tracy::DroidSans_compressed_size, round( 21.0f * scale ), &configBasic ); s_bigFont = io.Fonts->AddFontFromMemoryCompressedTTF( tracy::DroidSans_compressed_data, tracy::DroidSans_compressed_size, round( 21.0f * scale ), &configBasic );
io.Fonts->AddFontFromMemoryCompressedTTF( tracy::FontAwesomeSolid_compressed_data, tracy::FontAwesomeSolid_compressed_size, round( 20.0f * scale ), &configMerge, rangesIcons ); io.Fonts->AddFontFromMemoryCompressedTTF( tracy::FontAwesomeSolid_compressed_data, tracy::FontAwesomeSolid_compressed_size, round( 20.0f * scale ), &configMerge, rangesIcons );
s_smallFont = cb_smallFont = io.Fonts->AddFontFromMemoryCompressedTTF( tracy::DroidSans_compressed_data, tracy::DroidSans_compressed_size, round( 10.0f * scale ), &configBasic ); s_smallFont = io.Fonts->AddFontFromMemoryCompressedTTF( tracy::DroidSans_compressed_data, tracy::DroidSans_compressed_size, round( 10.0f * scale ), &configBasic );
ImGui_ImplOpenGL3_DestroyFontsTexture(); ImGui_ImplOpenGL3_DestroyFontsTexture();
ImGui_ImplOpenGL3_CreateFontsTexture(); ImGui_ImplOpenGL3_CreateFontsTexture();

View File

@ -7,6 +7,6 @@ extern ImFont* s_bigFont;
extern ImFont* s_smallFont; extern ImFont* s_smallFont;
extern ImFont* s_fixedWidth; extern ImFont* s_fixedWidth;
void LoadFonts( float scale, ImFont*& cb_fixedWidth, ImFont*& cb_bigFont, ImFont*& cb_smallFont ); void LoadFonts( float scale );
#endif #endif

View File

@ -133,9 +133,10 @@ static void RunOnMainThread( const std::function<void()>& cb, bool forceDelay =
mainThreadTasks.Queue( cb, forceDelay ); mainThreadTasks.Queue( cb, forceDelay );
} }
static void SetupDPIScale( float scale, ImFont*& cb_fixedWidth, ImFont*& cb_bigFont, ImFont*& cb_smallFont ) static void SetupDPIScale( float scale )
{ {
LoadFonts( scale, cb_fixedWidth, cb_bigFont, cb_smallFont ); LoadFonts( scale );
if( view ) view->UpdateFont( s_fixedWidth, s_smallFont, s_bigFont );
#ifdef __APPLE__ #ifdef __APPLE__
// No need to upscale the style on macOS, but we need to downscale the fonts. // No need to upscale the style on macOS, but we need to downscale the fonts.
@ -164,9 +165,9 @@ static void SetupDPIScale( float scale, ImFont*& cb_fixedWidth, ImFont*& cb_bigF
delete[] scaleIcon; delete[] scaleIcon;
} }
static void SetupScaleCallback( float scale, ImFont*& cb_fixedWidth, ImFont*& cb_bigFont, ImFont*& cb_smallFont ) static void SetupScaleCallback( float scale )
{ {
RunOnMainThread( [scale, &cb_fixedWidth, &cb_bigFont, &cb_smallFont] { SetupDPIScale( scale * dpiScale, cb_fixedWidth, cb_bigFont, cb_smallFont ); }, true ); RunOnMainThread( [scale] { SetupDPIScale( scale * dpiScale ); }, true );
} }
static void LoadConfig() static void LoadConfig()
@ -204,7 +205,7 @@ static void ScaleChanged( float scale )
if ( dpiScale == scale ) return; if ( dpiScale == scale ) return;
dpiScale = scale; dpiScale = scale;
SetupDPIScale( dpiScale, s_fixedWidth, s_bigFont, s_smallFont ); SetupDPIScale( dpiScale );
} }
int main( int argc, char** argv ) int main( int argc, char** argv )
@ -327,7 +328,7 @@ int main( int argc, char** argv )
} }
} }
SetupDPIScale( dpiScale, s_fixedWidth, s_bigFont, s_smallFont ); SetupDPIScale( dpiScale );
tracy::UpdateTextureRGBAMips( zigzagTex, (void**)zigzagPx, zigzagX, zigzagY, 6 ); tracy::UpdateTextureRGBAMips( zigzagTex, (void**)zigzagPx, zigzagX, zigzagY, 6 );
for( auto& v : zigzagPx ) free( v ); for( auto& v : zigzagPx ) free( v );

View File

@ -892,19 +892,19 @@ bool View::DrawImpl()
if( ImGui::Button( ICON_FA_MAGNIFYING_GLASS_PLUS ) ) ImGui::OpenPopup( "ZoomPopup" ); if( ImGui::Button( ICON_FA_MAGNIFYING_GLASS_PLUS ) ) ImGui::OpenPopup( "ZoomPopup" );
if( ImGui::BeginPopup( "ZoomPopup" ) ) if( ImGui::BeginPopup( "ZoomPopup" ) )
{ {
if( ImGui::Button( "50%" ) ) m_sscb( 1.f/2, m_fixedFont, m_bigFont, m_smallFont ); if( ImGui::Button( "50%" ) ) m_sscb( 1.f/2 );
if( ImGui::Button( "57%" ) ) m_sscb( 1.f/1.75f, m_fixedFont, m_bigFont, m_smallFont ); if( ImGui::Button( "57%" ) ) m_sscb( 1.f/1.75f );
if( ImGui::Button( "66%" ) ) m_sscb( 1.f/1.5f, m_fixedFont, m_bigFont, m_smallFont ); if( ImGui::Button( "66%" ) ) m_sscb( 1.f/1.5f );
if( ImGui::Button( "80%" ) ) m_sscb( 1.f/1.25f, m_fixedFont, m_bigFont, m_smallFont ); if( ImGui::Button( "80%" ) ) m_sscb( 1.f/1.25f );
if( ImGui::Button( "100%" ) ) m_sscb( 1.f, m_fixedFont, m_bigFont, m_smallFont ); if( ImGui::Button( "100%" ) ) m_sscb( 1.f );
if( ImGui::Button( "125%" ) ) m_sscb( 1.25f, m_fixedFont, m_bigFont, m_smallFont ); if( ImGui::Button( "125%" ) ) m_sscb( 1.25f );
if( ImGui::Button( "150%" ) ) m_sscb( 1.5f, m_fixedFont, m_bigFont, m_smallFont ); if( ImGui::Button( "150%" ) ) m_sscb( 1.5f );
if( ImGui::Button( "175%" ) ) m_sscb( 1.75f, m_fixedFont, m_bigFont, m_smallFont ); if( ImGui::Button( "175%" ) ) m_sscb( 1.75f );
if( ImGui::Button( "200%" ) ) m_sscb( 2.f, m_fixedFont, m_bigFont, m_smallFont ); if( ImGui::Button( "200%" ) ) m_sscb( 2.f );
if( ImGui::Button( "225%" ) ) m_sscb( 2.25f, m_fixedFont, m_bigFont, m_smallFont ); if( ImGui::Button( "225%" ) ) m_sscb( 2.25f );
if( ImGui::Button( "250%" ) ) m_sscb( 2.5f, m_fixedFont, m_bigFont, m_smallFont ); if( ImGui::Button( "250%" ) ) m_sscb( 2.5f );
if( ImGui::Button( "275%" ) ) m_sscb( 2.75f, m_fixedFont, m_bigFont, m_smallFont ); if( ImGui::Button( "275%" ) ) m_sscb( 2.75f );
if( ImGui::Button( "300%" ) ) m_sscb( 3.f, m_fixedFont, m_bigFont, m_smallFont ); if( ImGui::Button( "300%" ) ) m_sscb( 3.f );
ImGui::EndPopup(); ImGui::EndPopup();
} }
} }

View File

@ -100,7 +100,7 @@ public:
}; };
using SetTitleCallback = void(*)( const char* ); using SetTitleCallback = void(*)( const char* );
using SetScaleCallback = void(*)( float, ImFont*&, ImFont*&, ImFont*& ); using SetScaleCallback = void(*)( float );
using AttentionCallback = void(*)(); using AttentionCallback = void(*)();
View( void(*cbMainThread)(const std::function<void()>&, bool), const char* addr, uint16_t port, ImFont* fixedWidth, ImFont* smallFont, ImFont* bigFont, SetTitleCallback stcb, SetScaleCallback sscb, AttentionCallback acb, const Config& config ); View( void(*cbMainThread)(const std::function<void()>&, bool), const char* addr, uint16_t port, ImFont* fixedWidth, ImFont* smallFont, ImFont* bigFont, SetTitleCallback stcb, SetScaleCallback sscb, AttentionCallback acb, const Config& config );
@ -110,6 +110,8 @@ public:
bool Draw(); bool Draw();
bool WasActive() const; bool WasActive() const;
void UpdateFont( ImFont* fixed, ImFont* small, ImFont* big ) { m_fixedFont = fixed; m_smallFont = small; m_bigFont = big; }
void NotifyRootWindowSize( float w, float h ) { m_rootWidth = w; m_rootHeight = h; } void NotifyRootWindowSize( float w, float h ) { m_rootWidth = w; m_rootHeight = h; }
void ViewSource( const char* fileName, int line ); void ViewSource( const char* fileName, int line );
void ViewSource( const char* fileName, int line, const char* functionName ); void ViewSource( const char* fileName, int line, const char* functionName );