Fix Code window gutter disappearing on click

The gutter (line numbers + fold markers) lived inside the scrolled
window, so clicking in the code let the text view scroll the whole
row horizontally: the scrolled window's hadj jumped to 76, exactly
the gutter width, and the gutter scrolled off-screen left. Resizing
did not bring it back.

Move the gutter out of the horizontal scroll: it now sits beside the
scrolled window in code_box, so it can never be scrolled away, and
only tracks the vertical scroll (on_gutter_draw shifts line positions
by the vadj value; gutter_line_at_y adds it back for fold/breakpoint
clicks; on_scroll_changed repaints). The scrolled window's shadow
is off so the first line starts exactly at the gutter's top edge.
Horizontal scrolling of long lines still works.

Verified live under Xvfb: click in the code leaves the gutter in
place; vertical scroll keeps line numbers aligned; clicking a fold
marker while scrolled collapses the region correctly.
This commit is contained in:
cnmcdee committed 2026-12-01 02:12:34 +00:00
1 parent b96e545f20
commit 2b0acc296f
1 file changed
+40 -12
+40 -12
View File
@@ -504,11 +504,23 @@ bool region_starts_at(CodeViewData* d, int line0) {
return false;
}
// How far the code has scrolled vertically. The gutter sits OUTSIDE
// the scrolled window (see codeview_new), so line y-ranges from the
// text view are in buffer coordinates and must be shifted by this
// amount to land on the gutter's widget coordinates.
double gutter_vscroll(CodeViewData* d) {
if (!d->scroll) return 0;
GtkAdjustment* vadj = gtk_scrolled_window_get_vadjustment(
GTK_SCROLLED_WINDOW(d->scroll));
return vadj ? gtk_adjustment_get_value(vadj) : 0;
}
// Buffer line (0-based) at a gutter y in widget coordinates; -1 if none.
int gutter_line_at_y(CodeViewData* d, double y) {
GtkTextBuffer* buf =
gtk_text_view_get_buffer(GTK_TEXT_VIEW(d->view));
int count = gtk_text_buffer_get_line_count(buf);
y += gutter_vscroll(d); // widget y -> buffer y
for (int ln = 0; ln < count; ++ln) {
GtkTextIter it;
gtk_text_buffer_get_iter_at_line(buf, &it, ln);
@@ -562,12 +574,17 @@ gboolean on_gutter_draw(GtkWidget* gutter, cairo_t* cr, gpointer user) {
cairo_select_font_face(cr, "monospace", CAIRO_FONT_SLANT_NORMAL,
CAIRO_FONT_WEIGHT_NORMAL);
cairo_set_font_size(cr, 11);
// The gutter does not scroll with the text, so buffer y-ranges
// are shifted by the vertical scroll to land on widget y.
const double voff = gutter_vscroll(d);
for (int ln = 0; ln < count; ++ln) {
GtkTextIter it;
gtk_text_buffer_get_iter_at_line(buf, &it, ln);
gint y, h;
gtk_text_view_get_line_yrange(GTK_TEXT_VIEW(d->view), &it, &y, &h);
gint by, h;
gtk_text_view_get_line_yrange(GTK_TEXT_VIEW(d->view), &it, &by, &h);
if (h <= 0) continue; // folded away
double y = by - voff; // widget y of this line's top
if (y + h < 0 || y > alloc.height) continue; // off-screen
bool paused = (d->paused_line == ln + 1);
if (paused) {
// Red gutter strip to match the red paused-line background.
@@ -629,10 +646,13 @@ gboolean on_gutter_press(GtkWidget*, GdkEventButton* ev, gpointer user) {
return TRUE;
}
void on_view_size_allocate(GtkWidget*, GtkAllocation* alloc,
gpointer user) {
void on_view_size_allocate(GtkWidget*, GtkAllocation*, gpointer user) {
// The gutter lives outside the scrolled window now (see
// codeview_new), so its height comes from the layout, not from
// the text view. A view resize just means the gutter needs a
// repaint (different lines visible).
CodeViewData* d = (CodeViewData*)user;
gtk_widget_set_size_request(d->gutter, 76, alloc->height);
gtk_widget_queue_draw(d->gutter);
}
void on_scroll_changed(GtkAdjustment*, gpointer user) {
@@ -711,13 +731,21 @@ GtkWidget* codeview_new() {
CodeViewData* d = new CodeViewData();
d->theme = theme_current();
// Outer box is vertical: code area on top, automatic-inspection
// pane at the bottom. The inner box keeps the old horizontal
// gutter + text view layout.
// pane at the bottom. The gutter sits BESIDE the scrolled window,
// not inside it: it used to live in the scrolled window's child
// box, so clicking in the code let the text view scroll the whole
// row horizontally (hadj jumped to 76, the gutter width) and the
// gutter scrolled off-screen. With the gutter outside the scroll
// it can never be scrolled away; it only tracks the vertical
// scroll (on_gutter_draw shifts line positions by the vadj value,
// and on_scroll_changed repaints it). The scrolled window's shadow
// is off so the text view's first line starts at exactly the
// gutter's top edge.
GtkWidget* box = gtk_box_new(GTK_ORIENTATION_VERTICAL, 0);
GtkWidget* code_box = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 0);
d->box = box;
d->gutter = gtk_drawing_area_new();
gtk_widget_set_size_request(d->gutter, 76, 100);
gtk_widget_set_size_request(d->gutter, 76, -1);
gtk_widget_add_events(d->gutter, GDK_BUTTON_PRESS_MASK);
g_signal_connect(d->gutter, "draw", G_CALLBACK(on_gutter_draw), d);
g_signal_connect(d->gutter, "button-press-event",
@@ -734,10 +762,10 @@ GtkWidget* codeview_new() {
d->scroll = gtk_scrolled_window_new(NULL, NULL);
gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(d->scroll),
GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
GtkWidget* inner = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 0);
gtk_box_pack_start(GTK_BOX(inner), d->gutter, FALSE, FALSE, 0);
gtk_box_pack_start(GTK_BOX(inner), d->view, TRUE, TRUE, 0);
gtk_container_add(GTK_CONTAINER(d->scroll), inner);
gtk_scrolled_window_set_shadow_type(GTK_SCROLLED_WINDOW(d->scroll),
GTK_SHADOW_NONE);
gtk_container_add(GTK_CONTAINER(d->scroll), d->view);
gtk_box_pack_start(GTK_BOX(code_box), d->gutter, FALSE, FALSE, 0);
gtk_box_pack_start(GTK_BOX(code_box), d->scroll, TRUE, TRUE, 0);
// Inspection pane: scope label + name/type/value list.
d->inspect_frame =