Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Library: Port 'Text Colors' to Python #735

Merged
merged 2 commits into from
Nov 11, 2023
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 60 additions & 0 deletions src/Library/demos/Text Colors/main.py
Original file line number Diff line number Diff line change
@@ -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")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Specifying the version for Pango is not necessary, there is only one version.

Copy link
Contributor Author

@UrtsiSantsi UrtsiSantsi Nov 3, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Only one so far 😄 - there is a pango2 pull request by Matthias Clasen. Is this a problem in reality, specifying a version even if there is only one or just a personal preference?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And what about Glib and Gio?

Copy link
Contributor

@theCapypara theCapypara Nov 3, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh sorry, I wasn't aware Pango 2 was a thing. In that case we should keep this 👍
In general my comment here: #732 (comment)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See also #738 - I'll think about / do some research if specifying the Pango version makes sense, but in doubt we keep it.

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()