kge: : font sticks; one font per frame from the focused window

`: font NAME` was reverted on the next frame because the per-window
chooser re-applied the config's code/writing font, and with two windows
in different modes the font flipped every frame.

Fonts per edit mode now live on the primary Editor (EditorFonts),
shared with secondary windows like the buffer list. `: font` sets the
font for the current buffer's mode, and the frontend picks one font per
frame from the window with keyboard focus.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-11-23 11:36:38 -08:00
co-authored by Claude Opus 5.5
parent 6dcfaa93ef
commit bcefbc978f
9 changed files with 122 additions and 28 deletions
+26 -17
View File
@@ -157,6 +157,8 @@ GUIFrontend::Init(int &argc, char **argv, Editor &ed)
// Load GUI configuration (fullscreen, columns/rows, font size, theme, background)
config_ = GUIConfig::Load();
ed.Fonts().code_font = config_.code_font;
ed.Fonts().writing_font = config_.writing_font;
if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_TIMER) != 0) {
return false;
@@ -360,6 +362,7 @@ GUIFrontend::OpenNewWindow_(Editor &primary)
// Secondary editor shares the primary's buffer list
ws->editor.SetSharedBuffers(&primary.Buffers(), primary.Swap());
ws->editor.SetSharedFonts(&primary.Fonts());
ws->editor.SetDimensions(primary.Rows(), primary.Cols());
// Open a new untitled buffer and switch to it in the new window.
@@ -568,23 +571,6 @@ GUIFrontend::Step(Editor &ed, bool &running)
running = false;
}
// Switch font based on current buffer's edit mode (deferred to next frame)
{
Buffer *cur = wed.CurrentBuffer();
if (cur) {
auto &fr = kte::Fonts::FontRegistry::Instance();
const std::string &expected =
(cur->GetEditMode() == EditMode::Writing)
? config_.writing_font
: config_.code_font;
if (fr.CurrentFontName() != expected && fr.HasFont(expected)) {
float sz = fr.CurrentFontSize();
if (sz <= 0.0f) sz = config_.font_size;
fr.RequestLoadFont(expected, sz);
}
}
}
// Draw. Every frame repaints the whole window, so a full-redraw
// request (C-l, reload) needs nothing more than clearing.
(void) wed.ConsumeFullRedraw();
@@ -601,6 +587,29 @@ GUIFrontend::Step(Editor &ed, bool &running)
SDL_GL_SwapWindow(ws.window);
}
// Pick the font for the focused window's buffer's edit mode (applied at
// the start of the next Step). There is one loaded font for all windows,
// so choose once: when each window asked for its own buffer's mode, a
// code window and a writing window swapped the font every frame.
{
SDL_Window *focus = SDL_GetKeyboardFocus();
Editor *fed = &ed;
for (std::size_t wi = 1; wi < windows_.size(); ++wi) {
if (windows_[wi]->alive && windows_[wi]->window == focus)
fed = &windows_[wi]->editor;
}
if (const Buffer *cur = fed->CurrentBuffer()) {
auto &fr = kte::Fonts::FontRegistry::Instance();
const std::string &expected = ed.Fonts().ForMode(cur->GetEditMode());
if (fr.CurrentFontName() != expected && fr.HasFont(expected)) {
float sz = fr.CurrentFontSize();
if (sz <= 0.0f)
sz = config_.font_size;
fr.RequestLoadFont(expected, sz);
}
}
}
// Handle deferred new-window requests (must happen outside the render loop
// to avoid corrupting an in-progress ImGui frame).
for (std::size_t wi = 0; wi < windows_.size(); ++wi) {