From 3c9e805e330cc6a58b3956889d3b8efee2c3b88e Mon Sep 17 00:00:00 2001 From: Urtsi Santsi Date: Mon, 30 Oct 2023 12:19:57 +0200 Subject: [PATCH 1/2] Library: Port 'Text Colors' to Python --- src/Library/demos/Text Colors/main.py | 60 +++++++++++++++++++++++++++ 1 file changed, 60 insertions(+) create mode 100644 src/Library/demos/Text Colors/main.py diff --git a/src/Library/demos/Text Colors/main.py b/src/Library/demos/Text Colors/main.py new file mode 100644 index 000000000..f3ef0de3a --- /dev/null +++ b/src/Library/demos/Text Colors/main.py @@ -0,0 +1,60 @@ +# Pango is a text layout library. It can e.g. be used for formatting text +# https://gjs-docs.gnome.org/pango10~1.0/ +import gi + +gi.require_version("Gtk", "4.0") +gi.require_version("Pango", "1.0") +from gi.repository import Gtk, Pango +import workbench + +label: Gtk.Label = workbench.builder.get_object("label") + + +def updateAttributes(): + # A Pango Attribute List is used to style the label + label.set_attributes(rainbowAttributes(label.get_label())) + + +# Generates an Attribute List that styles the label in rainbow colors. +# The `text` parameter is needed to detect string length + position of spaces +def rainbowAttributes(text): + RAINBOW_COLORS = ( + "#D00", + "#C50", + "#E90", + "#090", + "#24E", + "#55E", + "#C3C", + ) + + # Create a color array with the length needed to color all the letters + colorArray = [] + i = 0 + while i < len(text): + colorArray += RAINBOW_COLORS + i = len(colorArray) + + # Independent variable from `i` in the following loop to avoid spaces "consuming" a color + colorIdx = 0 + + attrListString = "" + for i in range(len(text)): + # Skip space characters + if text[i] != " ": + startIdx = i + endIdx = i + 1 + + color = colorArray[colorIdx] + colorIdx += 1 + # See comment below + attrListString += f"{startIdx} {endIdx} foreground {color}," + + # For more info about the syntax for this function, see: + # https://docs.gtk.org/Pango/method.AttrList.to_string.html + print(attrListString) + return Pango.attr_list_from_string(attrListString) + + +label.connect("notify::label", lambda _, __: updateAttributes()) +updateAttributes() From 075ae98a6aa036bf4596b5f3ef84e496d3590124 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marco=20Capypara=20K=C3=B6pcke?= Date: Sat, 11 Nov 2023 17:28:25 +0100 Subject: [PATCH 2/2] python: Minor styling changes to Text Colors demo --- src/Library/demos/Text Colors/main.py | 35 +++++++++++++-------------- 1 file changed, 17 insertions(+), 18 deletions(-) diff --git a/src/Library/demos/Text Colors/main.py b/src/Library/demos/Text Colors/main.py index f3ef0de3a..95349c99d 100644 --- a/src/Library/demos/Text Colors/main.py +++ b/src/Library/demos/Text Colors/main.py @@ -10,15 +10,15 @@ label: Gtk.Label = workbench.builder.get_object("label") -def updateAttributes(): +def update_attributes(): # A Pango Attribute List is used to style the label - label.set_attributes(rainbowAttributes(label.get_label())) + label.set_attributes(rainbow_attributes(label.get_label())) # Generates an Attribute List that styles the label in rainbow colors. # The `text` parameter is needed to detect string length + position of spaces -def rainbowAttributes(text): - RAINBOW_COLORS = ( +def rainbow_attributes(text): + rainbow_colors = ( "#D00", "#C50", "#E90", @@ -29,32 +29,31 @@ def rainbowAttributes(text): ) # Create a color array with the length needed to color all the letters - colorArray = [] + color_array = [] i = 0 while i < len(text): - colorArray += RAINBOW_COLORS - i = len(colorArray) + color_array += rainbow_colors + i = len(color_array) # Independent variable from `i` in the following loop to avoid spaces "consuming" a color - colorIdx = 0 + color_idx = 0 - attrListString = "" + attr_list_string = "" for i in range(len(text)): # Skip space characters if text[i] != " ": - startIdx = i - endIdx = i + 1 + start_idx = i + end_idx = i + 1 - color = colorArray[colorIdx] - colorIdx += 1 + color = color_array[color_idx] + color_idx += 1 # See comment below - attrListString += f"{startIdx} {endIdx} foreground {color}," + attr_list_string += f"{start_idx} {end_idx} foreground {color}," # For more info about the syntax for this function, see: # https://docs.gtk.org/Pango/method.AttrList.to_string.html - print(attrListString) - return Pango.attr_list_from_string(attrListString) + return Pango.attr_list_from_string(attr_list_string) -label.connect("notify::label", lambda _, __: updateAttributes()) -updateAttributes() +label.connect("notify::label", lambda _, __: update_attributes()) +update_attributes()