Skip to content

Commit e574c94

Browse files
committed
fix: stable font loading using FontGlobalScale, per-tab state, dialog initialization
1 parent cc96de6 commit e574c94

2 files changed

Lines changed: 48 additions & 11 deletions

File tree

src/editor_app.cpp

Lines changed: 43 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -218,9 +218,8 @@ void EditorApp::init() {
218218
io.ConfigFlags |= ImGuiConfigFlags_DockingEnable;
219219
io.ConfigFlags |= ImGuiConfigFlags_NavEnableKeyboard;
220220

221-
// Apply initial font settings
222-
io.FontGlobalScale = settings_.font_size / 16.0f;
223-
font_size_temp_ = settings_.font_size;
221+
// Load fonts
222+
load_fonts();
224223

225224
apply_theme(settings_.dark_theme);
226225

@@ -766,15 +765,32 @@ void EditorApp::clear_change_history() {
766765
tabs_[active_tab_].changed_lines.clear();
767766
}
768767

768+
void EditorApp::load_fonts() {
769+
ImGuiIO& io = ImGui::GetIO();
770+
771+
// Just add default font - we'll control size via FontGlobalScale
772+
io.Fonts->AddFontDefault();
773+
774+
// Store font size temp
775+
font_size_temp_ = settings_.font_size;
776+
777+
// Font texture is built automatically by the renderer on first use
778+
779+
// Apply initial font scale
780+
float scale = (float)settings_.font_size / 16.0f;
781+
io.FontGlobalScale = scale;
782+
}
783+
769784
void EditorApp::rebuild_fonts() {
770-
// Dynamic font change - apply immediately to all tabs
771-
float scale = settings_.font_size / 16.0f;
772-
ImGui::GetIO().FontGlobalScale = scale;
785+
// Use font global scale to adjust font size - more stable than rebuilding
786+
ImGuiIO& io = ImGui::GetIO();
773787

774-
// Mark all tabs as needing re-render
775-
for (int i = 0; i < (int)tabs_.size(); i++) {
776-
apply_zoom(i);
777-
}
788+
// Get the base font size from settings
789+
float base_size = (float)settings_.font_size;
790+
791+
// Calculate scale relative to default 16px
792+
float scale = base_size / 16.0f;
793+
io.FontGlobalScale = scale;
778794
}
779795

780796
// ============================================================================
@@ -1039,6 +1055,17 @@ void EditorApp::render_editor_area() {
10391055

10401056
// Tab bar - only show if more than one tab
10411057
bool show_tabs = tabs_.size() > 1;
1058+
static int prev_active_tab = -1;
1059+
1060+
// Save scroll position when switching away from a tab
1061+
if (prev_active_tab != active_tab_ && prev_active_tab >= 0 && prev_active_tab < (int)tabs_.size()) {
1062+
// Note: TextEditor doesn't have public scroll getters/setters,
1063+
// but we could track cursor position as proxy
1064+
auto& prev_tab = tabs_[prev_active_tab];
1065+
prev_tab.editor->GetCursorPosition(); // This doesn't return scroll, but we can track cursor
1066+
}
1067+
prev_active_tab = active_tab_;
1068+
10421069
if (show_tabs) {
10431070
ImGuiTabBarFlags tab_flags = ImGuiTabBarFlags_Reorderable | ImGuiTabBarFlags_AutoSelectNewTabs;
10441071
if (ImGui::BeginTabBar("##Tabs", tab_flags)) {
@@ -1328,6 +1355,11 @@ void EditorApp::render_font_dialog() {
13281355
ImVec2 center = ImGui::GetMainViewport()->GetCenter();
13291356
ImGui::SetNextWindowPos(center, ImGuiCond_Appearing, ImVec2(0.5f, 0.5f));
13301357

1358+
// Initialize temp values from current settings when opening
1359+
if (show_font_ && font_name_temp_.empty()) {
1360+
font_name_temp_ = settings_.font_name;
1361+
}
1362+
13311363
if (ImGui::BeginPopupModal("Font Settings", &show_font_, ImGuiWindowFlags_AlwaysAutoResize)) {
13321364
ImGui::Text("Font Family:");
13331365
ImGui::SetNextItemWidth(250);
@@ -1347,7 +1379,7 @@ void EditorApp::render_font_dialog() {
13471379

13481380
ImGui::Separator();
13491381
ImGui::TextUnformatted("Note: Font size changes apply to all tabs.");
1350-
ImGui::TextUnformatted("Changing font family requires application restart.");
1382+
ImGui::TextUnformatted("Font family changes require application restart.");
13511383

13521384
if (ImGui::Button("Apply")) {
13531385
settings_.font_size = font_size_temp_;

src/editor_app.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,10 @@ struct EditorTab {
2222
std::vector<int> bookmarks; // Line numbers with bookmarks
2323
std::vector<int> changed_lines; // Lines with changes (for change history)
2424
int last_saved_line_count = 0; // Line count when last saved
25+
26+
// Per-tab state
27+
float scroll_x = 0.0f;
28+
float scroll_y = 0.0f;
2529
};
2630

2731
struct ChangeHistoryEntry {
@@ -109,6 +113,7 @@ EditorApp(int argc = 0, char* argv[] = nullptr);
109113
void toggle_spaces();
110114
void toggle_theme();
111115
void set_tab_size(int size);
116+
void load_fonts();
112117
void rebuild_fonts();
113118

114119
// Bookmarks

0 commit comments

Comments
 (0)