mirror of
https://github.com/kunkundi/crossdesk.git
synced 2025-12-16 20:17:10 +08:00
[fix] fix system_chinese_font_ usage to avoid dangling font pointer after closing stream window
This commit is contained in:
@@ -681,7 +681,7 @@ int Render::CreateMainWindow() {
|
|||||||
// for window region action
|
// for window region action
|
||||||
SDL_SetWindowHitTest(main_window_, HitTestCallback, this);
|
SDL_SetWindowHitTest(main_window_, HitTestCallback, this);
|
||||||
|
|
||||||
SetupFontAndStyle();
|
SetupFontAndStyle(true);
|
||||||
|
|
||||||
ImGuiStyle& style = ImGui::GetStyle();
|
ImGuiStyle& style = ImGui::GetStyle();
|
||||||
style.ScaleAllSizes(dpi_scale_);
|
style.ScaleAllSizes(dpi_scale_);
|
||||||
@@ -750,7 +750,7 @@ int Render::CreateStreamWindow() {
|
|||||||
// for window region action
|
// for window region action
|
||||||
SDL_SetWindowHitTest(stream_window_, HitTestCallback, this);
|
SDL_SetWindowHitTest(stream_window_, HitTestCallback, this);
|
||||||
|
|
||||||
SetupFontAndStyle();
|
SetupFontAndStyle(false);
|
||||||
|
|
||||||
SDL_SetRenderScale(stream_renderer_, dpi_scale_, dpi_scale_);
|
SDL_SetRenderScale(stream_renderer_, dpi_scale_, dpi_scale_);
|
||||||
|
|
||||||
@@ -793,7 +793,7 @@ int Render::DestroyStreamWindow() {
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
int Render::SetupFontAndStyle() {
|
int Render::SetupFontAndStyle(bool main_window) {
|
||||||
float font_size = 32.0f;
|
float font_size = 32.0f;
|
||||||
|
|
||||||
// Setup Dear ImGui style
|
// Setup Dear ImGui style
|
||||||
@@ -815,7 +815,11 @@ int Render::SetupFontAndStyle() {
|
|||||||
// Load system Chinese font as fallback
|
// Load system Chinese font as fallback
|
||||||
config.MergeMode = false;
|
config.MergeMode = false;
|
||||||
config.FontDataOwnedByAtlas = false;
|
config.FontDataOwnedByAtlas = false;
|
||||||
system_chinese_font_ = nullptr;
|
if (main_window) {
|
||||||
|
main_windows_system_chinese_font_ = nullptr;
|
||||||
|
} else {
|
||||||
|
stream_windows_system_chinese_font_ = nullptr;
|
||||||
|
}
|
||||||
|
|
||||||
#if defined(_WIN32)
|
#if defined(_WIN32)
|
||||||
// Windows: Try Microsoft YaHei (微软雅黑) first, then SimSun (宋体)
|
// Windows: Try Microsoft YaHei (微软雅黑) first, then SimSun (宋体)
|
||||||
@@ -841,20 +845,37 @@ int Render::SetupFontAndStyle() {
|
|||||||
std::ifstream font_file(font_paths[i], std::ios::binary);
|
std::ifstream font_file(font_paths[i], std::ios::binary);
|
||||||
if (font_file.good()) {
|
if (font_file.good()) {
|
||||||
font_file.close();
|
font_file.close();
|
||||||
system_chinese_font_ =
|
if (main_window) {
|
||||||
io.Fonts->AddFontFromFileTTF(font_paths[i], font_size, &config,
|
main_windows_system_chinese_font_ =
|
||||||
io.Fonts->GetGlyphRangesChineseFull());
|
io.Fonts->AddFontFromFileTTF(font_paths[i], font_size, &config,
|
||||||
if (system_chinese_font_ != nullptr) {
|
io.Fonts->GetGlyphRangesChineseFull());
|
||||||
LOG_INFO("Loaded system Chinese font: {}", font_paths[i]);
|
if (main_windows_system_chinese_font_ != nullptr) {
|
||||||
break;
|
LOG_INFO("Loaded system Chinese font: {}", font_paths[i]);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
stream_windows_system_chinese_font_ =
|
||||||
|
io.Fonts->AddFontFromFileTTF(font_paths[i], font_size, &config,
|
||||||
|
io.Fonts->GetGlyphRangesChineseFull());
|
||||||
|
if (stream_windows_system_chinese_font_ != nullptr) {
|
||||||
|
LOG_INFO("Loaded system Chinese font: {}", font_paths[i]);
|
||||||
|
break;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// If no system font found, use default font
|
// If no system font found, use default font
|
||||||
if (system_chinese_font_ == nullptr) {
|
if (main_window) {
|
||||||
system_chinese_font_ = io.Fonts->AddFontDefault(&config);
|
if (main_windows_system_chinese_font_ == nullptr) {
|
||||||
LOG_WARN("System Chinese font not found, using default font");
|
main_windows_system_chinese_font_ = io.Fonts->AddFontDefault(&config);
|
||||||
|
LOG_WARN("System Chinese font not found, using default font");
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
if (stream_windows_system_chinese_font_ == nullptr) {
|
||||||
|
stream_windows_system_chinese_font_ = io.Fonts->AddFontDefault(&config);
|
||||||
|
LOG_WARN("System Chinese font not found, using default font");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
ImGui::StyleColorsLight();
|
ImGui::StyleColorsLight();
|
||||||
|
|||||||
@@ -180,7 +180,7 @@ class Render {
|
|||||||
int DestroyMainWindow();
|
int DestroyMainWindow();
|
||||||
int CreateStreamWindow();
|
int CreateStreamWindow();
|
||||||
int DestroyStreamWindow();
|
int DestroyStreamWindow();
|
||||||
int SetupFontAndStyle();
|
int SetupFontAndStyle(bool main_window);
|
||||||
int DestroyMainWindowContext();
|
int DestroyMainWindowContext();
|
||||||
int DestroyStreamWindowContext();
|
int DestroyStreamWindowContext();
|
||||||
int DrawMainWindow();
|
int DrawMainWindow();
|
||||||
@@ -313,7 +313,8 @@ class Render {
|
|||||||
SDL_Window* main_window_ = nullptr;
|
SDL_Window* main_window_ = nullptr;
|
||||||
SDL_Renderer* main_renderer_ = nullptr;
|
SDL_Renderer* main_renderer_ = nullptr;
|
||||||
ImGuiContext* main_ctx_ = nullptr;
|
ImGuiContext* main_ctx_ = nullptr;
|
||||||
ImFont* system_chinese_font_ = nullptr; // System Chinese font for fallback
|
ImFont* main_windows_system_chinese_font_ = nullptr;
|
||||||
|
ImFont* stream_windows_system_chinese_font_ = nullptr;
|
||||||
bool exit_ = false;
|
bool exit_ = false;
|
||||||
const int sdl_refresh_ms_ = 16; // ~60 FPS
|
const int sdl_refresh_ms_ = 16; // ~60 FPS
|
||||||
#if _WIN32
|
#if _WIN32
|
||||||
|
|||||||
@@ -138,8 +138,8 @@ int Render::RequestPermissionWindow() {
|
|||||||
ImGui::SetWindowFontScale(0.3f);
|
ImGui::SetWindowFontScale(0.3f);
|
||||||
|
|
||||||
// use system font
|
// use system font
|
||||||
if (system_chinese_font_ != nullptr) {
|
if (main_windows_system_chinese_font_ != nullptr) {
|
||||||
ImGui::PushFont(system_chinese_font_);
|
ImGui::PushFont(main_windows_system_chinese_font_);
|
||||||
}
|
}
|
||||||
|
|
||||||
ImGui::SetCursorPosY(ImGui::GetCursorPosY() + ImGui::GetTextLineHeight() + 5.0f);
|
ImGui::SetCursorPosY(ImGui::GetCursorPosY() + ImGui::GetTextLineHeight() + 5.0f);
|
||||||
@@ -191,7 +191,7 @@ int Render::RequestPermissionWindow() {
|
|||||||
ImGui::SetWindowFontScale(0.45f);
|
ImGui::SetWindowFontScale(0.45f);
|
||||||
|
|
||||||
// pop system font
|
// pop system font
|
||||||
if (system_chinese_font_ != nullptr) {
|
if (main_windows_system_chinese_font_ != nullptr) {
|
||||||
ImGui::PopFont();
|
ImGui::PopFont();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -111,8 +111,8 @@ int Render::UpdateNotificationWindow() {
|
|||||||
float scrollable_height =
|
float scrollable_height =
|
||||||
window_height - UPDATE_NOTIFICATION_RESERVED_HEIGHT;
|
window_height - UPDATE_NOTIFICATION_RESERVED_HEIGHT;
|
||||||
|
|
||||||
if (system_chinese_font_ != nullptr) {
|
if (main_windows_system_chinese_font_ != nullptr) {
|
||||||
ImGui::PushFont(system_chinese_font_);
|
ImGui::PushFont(main_windows_system_chinese_font_);
|
||||||
}
|
}
|
||||||
// scrollable content area
|
// scrollable content area
|
||||||
ImGui::SetCursorPosX(window_width * 0.05f);
|
ImGui::SetCursorPosX(window_width * 0.05f);
|
||||||
@@ -163,7 +163,7 @@ int Render::UpdateNotificationWindow() {
|
|||||||
ImGui::EndChild();
|
ImGui::EndChild();
|
||||||
|
|
||||||
// pop system font
|
// pop system font
|
||||||
if (system_chinese_font_ != nullptr) {
|
if (main_windows_system_chinese_font_ != nullptr) {
|
||||||
ImGui::PopFont();
|
ImGui::PopFont();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user