gtk: create a format_label function

This commit is contained in:
alfrix 2022-11-27 16:00:11 -03:00
parent 1df2d0e8f0
commit d0fc1e197d

View File

@ -10,6 +10,20 @@ gi.require_version("Gtk", "3.0")
from gi.repository import Gdk, GdkPixbuf, Gio, Gtk, Pango
def format_label(widget, lines=2):
if type(widget) == Gtk.Label:
return widget
if type(widget) in (Gtk.Container, Gtk.Bin, Gtk.Button, Gtk.Alignment, Gtk.Box):
for _ in widget.get_children():
lbl = format_label(_)
if lbl is not None:
lbl.set_line_wrap_mode(Pango.WrapMode.WORD_CHAR)
lbl.set_line_wrap(True)
lbl.set_ellipsize(True)
lbl.set_ellipsize(Pango.EllipsizeMode.END)
lbl.set_lines(lines)
class KlippyGtk:
labels = {}
@ -159,22 +173,7 @@ class KlippyGtk:
b.set_always_show_image(True)
if label is not None:
try:
# Get the label object
if image_name is None:
child = b.get_children()[0]
elif position == Gtk.PositionType.RIGHT:
child = b.get_children()[0].get_children()[0].get_children()[0]
else:
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: {label} image: {image_name} exception:{e}")
format_label(b, lines)
if style is not None:
b.get_style_context().add_class(style)
b.connect("clicked", self.screen.reset_screensaver_timeout)
@ -187,12 +186,11 @@ class KlippyGtk:
dialog.set_transient_for(screen)
dialog.set_modal(True)
for i, button in enumerate(buttons):
dialog.add_button(button_text=button['name'], response_id=button['response'])
button = dialog.get_children()[0].get_children()[0].get_children()[0].get_children()[i]
button.get_child().set_line_wrap_mode(Pango.WrapMode.WORD_CHAR)
button.get_child().set_line_wrap(True)
for button in buttons:
dialog.add_button(button['name'], button['response'])
button = dialog.get_widget_for_response(button['response'])
button.set_size_request((screen.width - 30) / 3, screen.height / 5)
format_label(button, 3)
dialog.connect("response", self.screen.reset_screensaver_timeout)
dialog.connect("response", callback, *args)