gtk: improve wrapping of labels

This commit is contained in:
alfrix
2022-08-17 07:36:36 -03:00
committed by Alfredo Monclus
parent 836e4a3aea
commit 38ceea5204
20 changed files with 62 additions and 69 deletions

View File

@@ -1,5 +1,4 @@
# -*- coding: utf-8 -*-
import contextlib
import gi
import logging
import os
@@ -164,9 +163,7 @@ class KlippyGtk:
b.connect("clicked", self.screen.reset_screensaver_timeout)
return b
def ButtonImage(self, image_name=None, label=None, style=None, scale=1.38,
position=Gtk.PositionType.TOP, word_wrap=True):
def ButtonImage(self, image_name=None, label=None, style=None, scale=1.38, position=Gtk.PositionType.TOP, lines=2):
b = Gtk.Button(label=label)
b.set_hexpand(True)
b.set_vexpand(True)
@@ -176,12 +173,17 @@ class KlippyGtk:
b.set_image_position(position)
b.set_always_show_image(True)
if word_wrap is True:
with contextlib.suppress(Exception):
# Get the label object
child = b.get_children()[0].get_children()[0].get_children()[1]
child.set_line_wrap_mode(Pango.WrapMode.WORD_CHAR)
child.set_line_wrap(True)
try:
# Get the label object
child = b.get_children()[0].get_children()[0].get_children()[1]
child.set_line_wrap_mode(Pango.WrapMode.WORD_CHAR)
child.set_line_wrap(True)
child.set_ellipsize(True)
child.set_ellipsize(Pango.EllipsizeMode.END)
child.set_lines(lines)
except Exception as e:
logging.debug(f"Unable to wrap and ellipsize label: {e}")
if style is not None:
b.get_style_context().add_class(style)
b.connect("clicked", self.screen.reset_screensaver_timeout)

View File

@@ -35,9 +35,9 @@ class Keypad(Gtk.Box):
for i in range(len(keys)):
k_id = f'button_{str(keys[i][0])}'
if keys[i][0] == "B":
self.labels[k_id] = self._gtk.ButtonImage("backspace", None, None, 1)
self.labels[k_id] = self._gtk.ButtonImage("backspace", scale=1)
elif keys[i][0] == "E":
self.labels[k_id] = self._gtk.ButtonImage("complete", None, None, 1)
self.labels[k_id] = self._gtk.ButtonImage("complete", scale=1)
else:
self.labels[k_id] = Gtk.Button(keys[i][0])
self.labels[k_id].connect('clicked', self.update_entry, keys[i][0])
@@ -49,7 +49,7 @@ class Keypad(Gtk.Box):
self.labels['entry'].props.xalign = 0.5
self.labels['entry'].connect("activate", self.update_entry, "E")
b = self._gtk.ButtonImage('cancel', _('Close'), None, .66, Gtk.PositionType.LEFT, False)
b = self._gtk.ButtonImage('cancel', _('Close'), None, .66, Gtk.PositionType.LEFT, 1)
b.connect("clicked", close_function)
self.add(self.labels['entry'])