Files
kte/ImGuiRenderer.h
Kyle Isom b60a8dc491 Optimize ImGui renderer: viewport culling and width caching
Large files made idle CPU spike because every frame re-measured every
line, recompiled the search regex per line, and ran full per-line
rendering work even for off-screen rows.

- Render only visible rows (with a small margin for smooth scrolling)
  and advance the layout cursor to preserve total content height.
- Hoist the search std::regex compilation out of the per-line loop.
- Cache the max line width on the renderer; reset only when the buffer
  or font changes, not on every edit.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-04-20 15:12:44 -07:00

38 lines
1.1 KiB
C++

/*
* ImGuiRenderer - ImGui-based renderer for GUI mode
*/
#pragma once
#include <cstdint>
#include <string>
#include "Renderer.h"
struct ImFont;
class Buffer;
class ImGuiRenderer final : public Renderer {
public:
ImGuiRenderer() = default;
~ImGuiRenderer() override = default;
void Draw(Editor &ed) override;
private:
// Per-window scroll tracking for two-way sync between Buffer offsets and ImGui scroll.
// These must be per-instance (not static) so each window maintains independent state.
long prev_buf_rowoffs_ = -1;
long prev_buf_coloffs_ = -1;
float prev_scroll_y_ = -1.0f;
float prev_scroll_x_ = -1.0f;
bool mouse_selecting_ = false;
// Max-line-width cache for the horizontal scrollbar. Measuring every line
// every frame is prohibitively expensive on large files; we only update the
// running max from visible lines and reset when buffer/version/font changes.
const Buffer *max_width_buf_ = nullptr;
std::uint64_t max_width_version_ = 0;
ImFont *max_width_font_ = nullptr;
float max_width_font_size_ = 0.0f;
float max_width_px_ = 0.0f;
};