home · contact · privacy
Browser: Ellipsize by_1st view's vertical labels into slot height. master
authorChristian Heller <c.heller@plomlompom.de>
Mon, 28 Oct 2024 11:53:42 +0000 (12:53 +0100)
committerChristian Heller <c.heller@plomlompom.de>
Mon, 28 Oct 2024 11:53:42 +0000 (12:53 +0100)
browser.py

index 28ed0c64fa8e4af0a69a944346e559088f0ade98..bd60c4e5fb0c492431fbd85265beb763d0ee9dfd 100755 (executable)
@@ -476,19 +476,26 @@ class ImgItem(GalleryItem):
 class VerticalLabel(Gtk.DrawingArea):
     """Label of vertical text (rotated -90°)."""
 
-    def __init__(self, text):
+    def __init__(self, text, max_height_ref):
         super().__init__()
-        self._layout = self.create_pango_layout()
-        self._layout.set_markup(text)
-        self._text_width, self._text_height = self._layout.get_pixel_size()
+        self._text = text
+        self._max_height_ref = max_height_ref
+        test_layout = self.create_pango_layout()
+        test_layout.set_markup(text)
+        _, self._text_height = test_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))
+        """Create layout, rotate by 90°, size widget to measurements."""
+        layout = self.create_pango_layout()
+        layout.set_markup(self._text)
+        layout.set_width(self._max_height_ref[0] * Pango.SCALE)
+        layout.set_ellipsize(Pango.EllipsizeMode.MIDDLE)
+        text_width, _ = layout.get_pixel_size()
+        cairo_ctx.translate(0, text_width + (height - text_width))
         cairo_ctx.rotate(radians(-90))
-        PangoCairo.show_layout(cairo_ctx, self._layout)
-        self.set_size_request(self._text_height, self._text_width)
+        PangoCairo.show_layout(cairo_ctx, layout)
+        self.set_size_request(self._text_height, text_width)
 
     @property
     def width(self):
@@ -531,6 +538,7 @@ class Gallery:
         scroller = Gtk.ScrolledWindow(propagate_natural_height=True)
         self._col_headers_frame = Gtk.Fixed()
         self._col_headers_grid = None
+        self._vlabel_height_ref = [0]
         self.frame = Gtk.Box(orientation=OR_V)
         self.frame.append(self._col_headers_frame)
         self.frame.append(scroller)
@@ -801,7 +809,8 @@ class Gallery:
                 attr_name, attr_values = remaining[0]
                 if 1 == len(remaining):
                     for i, attr in enumerate(ancestors):
-                        vlabel = VerticalLabel(f'<b>{attr[0]}</b>: {attr[1]}')
+                        vlabel = VerticalLabel(f'<b>{attr[0]}</b>: {attr[1]}',
+                                               self._vlabel_height_ref)
                         self._grid.attach(vlabel, i, i_row_ref[0], 1, 1)
                     row = [None] * len(attr_values)
                     for item in items_of_parent:
@@ -943,6 +952,7 @@ class Gallery:
             i_vlabels += 1
         max_slot_width = (vp_width - side_offset) // self._per_row
         slot_size = min(vp_height, max_slot_width)
+        self._vlabel_height_ref[0] = slot_size
         if self._by_1st:
             i_widgets = 0
             while True: