From ba278abf1baf700bed245f7586b07e4216551c61 Mon Sep 17 00:00:00 2001
From: Christian Heller <c.heller@plomlompom.de>
Date: Sun, 27 Oct 2024 04:18:25 +0100
Subject: [PATCH] Browser.py: Fix alignment of by_1st header labels.

---
 browser.py | 43 +++++++++++++++++++++++++++++--------------
 1 file changed, 29 insertions(+), 14 deletions(-)

diff --git a/browser.py b/browser.py
index 677aca1..57c84c3 100755
--- a/browser.py
+++ b/browser.py
@@ -478,18 +478,22 @@ class VerticalLabel(Gtk.DrawingArea):
 
     def __init__(self, text):
         super().__init__()
-        self._text = text
-        self.set_draw_func(self.on_draw)
-
-    def on_draw(self, _, cairo_ctx, __, height):
-        """Create Pango Layout from ._text, rotate and re-size."""
-        layout = self.create_pango_layout()
-        layout.set_markup(self._text)
-        text_width, text_height = layout.get_pixel_size()
-        cairo_ctx.translate(0, text_width + (height - text_width))
+        self._layout = self.create_pango_layout()
+        self._layout.set_markup(text)
+        self._text_width, self._text_height = self._layout.get_pixel_size()
+        self.set_draw_func(self._on_draw)
+
+    def _on_draw(self, _, cairo_ctx, __, height):
+        """Show ._layout rotated by 90°, move and re-size appropriately."""
+        cairo_ctx.translate(0, self._text_width + (height - self._text_width))
         cairo_ctx.rotate(radians(-90))
-        PangoCairo.show_layout(cairo_ctx, layout)
-        self.set_size_request(text_height, text_width)
+        PangoCairo.show_layout(cairo_ctx, self._layout)
+        self.set_size_request(self._text_height, self._text_width)
+
+    @property
+    def width(self):
+        """Return (rotated) ._text_height."""
+        return self._text_height
 
 
 class Gallery:
@@ -916,22 +920,33 @@ class Gallery:
         vp_scroll = self._viewport.get_vadjustment()
         vp_top = vp_scroll.get_value()
         vp_bottom = vp_top + vp_height
-        max_slot_width = vp_width // self._per_row
+        side_offset, i_vlabels = 0, 0
+        while True:
+            widget = self._grid.get_child_at(i_vlabels, 0)
+            if isinstance(widget, VerticalLabel):
+                side_offset += widget.width
+            else:
+                break
+            i_vlabels += 1
+        max_slot_width = (vp_width - side_offset) // self._per_row
         slot_size = min(vp_height, max_slot_width)
         if self._col_headers_grid:
             self._col_headers_frame.remove(self._col_headers_grid)
             self._col_headers_grid = None
         if self._by_1st:
             self._col_headers_grid = Gtk.Grid()
+            spacer_box = Gtk.Box()
+            spacer_box.set_size_request(side_offset, -1)
+            self._col_headers_grid.attach(spacer_box, 0, 0, 1, 1)
             self._col_headers_frame.put(self._col_headers_grid, 0, 0)
             attr_name = self._sort_order[0].name
             attr_values = self.items_attrs[attr_name]['incl']
             for i, val in enumerate(attr_values):
-                label = Gtk.Label(label=f'<b>{attr_name}</b>: {val}',
+                label = Gtk.Label(label=f'<b>{attr_name}</b>: {val}', xalign=0,
                                   ellipsize=Pango.EllipsizeMode.MIDDLE)
                 label.set_use_markup(True)
                 label.set_size_request(slot_size, -1)
-                self._col_headers_grid.attach(label, i, 0, 1, 1)
+                self._col_headers_grid.attach(label, i + 1, 0, 1, 1)
         slot_size_sans_margin = slot_size - self._slot_margin
         for idx, slot in enumerate(self.slots):
             slot.ensure_slot_size(slot_size_sans_margin, self._slot_margin)
-- 
2.30.2