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

Parse png buttons #2

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
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
39 changes: 39 additions & 0 deletions labwc-gtktheme.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@

Copyright (C) @Misko_2083 2019
Copyright (C) Johan Malm 2019-2022

https://docs.gtk.org/gtk3/css-overview.html

"""

import os
Expand All @@ -17,6 +20,7 @@
import gi
gi.require_version("Gtk", "3.0")
from gi.repository import Gtk
from base64 import b64decode

def parse(tokens):
"""
Expand Down Expand Up @@ -112,6 +116,39 @@ def resolve_labels(theme):
return resolve_labels(theme)
return theme

def create_one_icon(lines, nodename, filename):
"""
The data contains entries like the one below (base64 strings truncated).
We merely split the string at 'base64,' and '\"' to get the x1 size icons.
TODO: Parse this better, maybe with a recursive descendant approach.

button.close.titlebutton {
background-image: -gtk-scaled(url("data:image/png;base64,iVBORw0..."),url("data:image/png;base64,iVBORw0..."));
}
"""
theme = {}
inside = False
for line in lines:
if f"{nodename} {{" in line:
inside = True
continue
if inside:
if "}" in line or "{" in line:
inside = False
continue
key, value = line.strip().split(":", maxsplit=1)
key = key.replace(" ", "");
if key == "background-image":
value = value.split("base64,", maxsplit=1)[1]
value = value.split('\"', maxsplit=1)[0]
with open(filename, 'wb') as f:
f.write(b64decode(value))

def create_icons(lines, themedir):
create_one_icon(lines, "button.close.titlebutton", themedir + "/close-active.png")
create_one_icon(lines, "button.maximize.titlebutton", themedir + "/max-active.png")
create_one_icon(lines, "button.minimize.titlebutton", themedir + "/iconify-active.png")

def main():
""" main """
parser = argparse.ArgumentParser(prog="labwc-gtktheme")
Expand Down Expand Up @@ -191,5 +228,7 @@ def main():
# window.label.text.justify: center
# osd.border.width: 1

create_icons(lines, themedir);

if __name__ == '__main__':
main()