Commit a9c24ace authored by Rob Nelson's avatar Rob Nelson
Browse files

Discordjail now generates tray icons by transforming the SVG XML directly...

Discordjail now generates tray icons by transforming the SVG XML directly instead of string-replace.
parent f5cdeab4
......@@ -8,6 +8,7 @@ from .http import download_file_to
from buildtools import log, http
from buildtools.twisted_utils import async_cmd
from lxml import etree
DISCORD_DL_API = "https://discordapp.com/api/download?platform=linux&format=tar.gz"
DISCORD_API_ENDPOINT = 'https://discordapp.com/api'
......@@ -42,7 +43,10 @@ class Discord(object):
self.remoteQuery = {}
self.icon_ids = ['tray', 'tray-unread']
self.icon_ids = [
('tray', ['#badge']),
('tray-unread', [])
]
self.asarfile: str = ''
self.coredir: str = ''
......@@ -58,16 +62,38 @@ class Discord(object):
return f'{name}.x64'
return name
def _replaceIcon(self, pakid, svgpath, pngpath) -> None:
def _replaceIcon(self, pakid, svgpath, pngpath, hideelements=[]) -> None:
with log.info('Patching %s...', pakid):
log.info('Patching SVG...')
'''
with open(svgpath, 'r') as rf:
with open(os.path.join(self.tmpdir, 'icon.svg'), 'w') as wf:
for line in rf:
line = line.replace('#123456', self.color)
wf.write(line)
'''
doc = None
with open(svgpath, 'rb') as f:
doc = etree.parse(f)
found = False
#print(etree.tostring(doc,pretty_print=True))
#for el in doc.xpath('.//style'):
NS='{http://www.w3.org/2000/svg}'
svg = doc.getroot()
for el in doc.findall(f'{NS}style'):
el.text += f'.account-color{{fill:{self.color};fill-opacity:1;}}'
found = True
assert found
for elsel in hideelements:
for el in doc.cssselect(elsel):
# We don't just hide it, we completely remove it.
el.getparent().remove(el)
basename, _ = os.path.splitext(os.path.basename(pakid))
path = os.path.join(self.tmpdir, f'{basename}.svg')
doc.write(path)
with log.info('Rendering SVG to PNG...'):
self.renderSVGToPNG(os.path.join(self.tmpdir, 'icon.svg'), pngpath)
self.renderSVGToPNG(path, pngpath)
assert os.path.isfile(self.asarfile)
with log.info('Patching %s[%s]...', self.asarfile, pakid):
asarfile = os.path.join(self.coredir, pakid)
......@@ -77,8 +103,8 @@ class Discord(object):
self.asarfile = os.path.join(self.modulesdir, 'discord_desktop_core', 'core.asar')
self.coredir = os.path.join(self.tmpdir, 'core')
os_utils.cmd(['node_modules/.bin/asar', 'extract', self.asarfile, self.coredir], echo=True, show_output=True)
for iconid in self.icon_ids:
self._replaceIcon(f'app/images/systemtray/linux/{iconid}.png', os.path.join('img', self.imagepack, f'{iconid}.svg'), os.path.join(self.homedir, '.config', 'discord', f'{iconid}.png'))
for iconid, hide in self.icon_ids:
self._replaceIcon(f'app/images/systemtray/linux/{iconid}.png', os.path.join('img', self.imagepack, f'tray-BASE.svg'), os.path.join(self.homedir, '.config', 'discord', f'{iconid}.png'))
os_utils.cmd(['node_modules/.bin/asar', 'pack', self.coredir, self.asarfile], echo=True, show_output=True)
def renderSVGToPNG(self, svgfile, pngfile, height=24, width=24) -> None:
......
......@@ -15,7 +15,11 @@
version="1.1"
id="SVGRoot"
inkscape:version="0.92.3 (2405546, 2018-03-11)"
sodipodi:docname="tray.svg">
sodipodi:docname="BASE.svg">
<style type="text/css">
.st0{fill:#3E3E3E;}
.st1{fill:#FFFFFF;}
</style>
<sodipodi:namedview
id="base"
pagecolor="#ffffff"
......@@ -24,7 +28,7 @@
inkscape:pageopacity="0.0"
inkscape:pageshadow="2"
inkscape:zoom="8"
inkscape:cx="4.252417"
inkscape:cx="-14.122583"
inkscape:cy="31.873106"
inkscape:document-units="px"
inkscape:current-layer="layer1"
......@@ -53,22 +57,18 @@
id="layer1"
inkscape:groupmode="layer"
inkscape:label="Layer 1">
<circle
style="opacity:1;fill:#123456;fill-opacity:1;stroke:none;stroke-width:0.75402832;stroke-opacity:1"
id="circle"
cx="32"
cy="32"
r="29" />
<path
style="fill:#ffffff;fill-opacity:1;stroke-width:0.48148087"
style="stroke-width:0.48148087"
d="m 25.692585,15.557211 c 0,0 -5.34528,-0.0958 -11.026754,4.093088 0,0 -5.6811612,10.208167 -5.6811612,22.822965 0,10e-7 3.3229332,5.680819 12.0377362,5.969709 0,0 1.443503,-1.73272 2.599056,-3.22531 -5.0074,-1.492591 -6.884508,-4.623157 -6.884508,-4.623157 0,0 0.432978,0.241067 1.203347,0.626252 0.04815,0 0.04905,0.04803 0.145341,0.09618 0.09629,0.09629 0.239279,0.09606 0.33557,0.192365 1.011109,0.577776 1.974926,1.011651 2.88974,1.348688 1.588886,0.674072 3.514385,1.250925 5.777344,1.684257 2.937034,0.577779 6.404438,0.77138 10.208137,0.04916 1.877775,-0.337037 3.75625,-0.867876 5.730321,-1.686394 1.348147,-0.481481 2.887803,-1.251251 4.524839,-2.31051 0,0 -1.973062,3.178715 -7.173055,4.623157 1.155553,1.492591 2.599056,3.176151 2.599056,3.176151 8.762951,-0.288889 12.037736,-5.968699 12.037736,-5.92055 0,-12.614797 -5.681161,-22.822965 -5.681161,-22.822965 C 43.700842,15.46142 38.307415,15.557211 38.307415,15.557211 l -0.530071,0.626254 c 6.692584,2.022219 9.823407,4.958726 9.823407,4.958726 -4.140735,-2.214812 -8.136843,-3.322049 -11.892393,-3.755381 -2.840737,-0.288894 -5.586405,-0.240854 -7.993809,0.09618 -0.240741,0 -0.432535,0.04916 -0.673275,0.04916 -1.396295,0.192592 -4.7186,0.625098 -8.955632,2.502874 -1.444443,0.625925 -2.310509,1.107164 -2.310509,1.107164 0,0 3.224413,-3.08185 10.302182,-5.104069 z m -1.060141,11.988577 c 2.262961,0 4.094215,1.927206 4.046065,4.334611 0,2.407403 -1.783104,4.332473 -4.046065,4.332473 -2.214811,0 -4.043926,-1.92507 -4.043926,-4.332473 0,-2.407405 1.780966,-4.334611 4.043926,-4.334611 z m 14.397406,0 c 2.214812,0 4.043927,1.927206 4.043927,4.334611 0,2.407403 -1.780968,4.332473 -4.043927,4.332473 -2.214813,0 -4.043927,-1.92507 -4.043927,-4.332473 0,-2.407405 1.780966,-4.334611 4.043927,-4.334611 z"
id="path1442"
inkscape:connector-curvature="0" />
id="discordlogo"
inkscape:connector-curvature="0"
class="account-color" />
<circle
style="opacity:1;fill:#ff0000;fill-opacity:1;stroke:none;stroke-width:0.74698794;stroke-opacity:1"
id="badge"
cx="53.625"
cy="10.375"
r="7.75"
inkscape:label="#badge" />
</g>
<style
id="style1440"
type="text/css">
.st0{fill:#3E3E3E;}
.st1{fill:#FFFFFF;}
</style>
</svg>
......@@ -16,6 +16,10 @@
id="SVGRoot"
inkscape:version="0.92.3 (2405546, 2018-03-11)"
sodipodi:docname="tray-unread.svg">
<style type="text/css">
.st0{fill:#3E3E3E;}
.st1{fill:#FFFFFF;}
</style>
<sodipodi:namedview
id="base"
pagecolor="#ffffff"
......@@ -54,8 +58,9 @@
inkscape:groupmode="layer"
inkscape:label="Layer 1">
<circle
style="opacity:1;fill:#123456;fill-opacity:1;stroke:none;stroke-width:0.75402832;stroke-opacity:1"
style="opacity:1;stroke:none;stroke-width:0.75402832;stroke-opacity:1"
id="circle"
class="account-color"
cx="32"
cy="32"
r="29" />
......@@ -66,15 +71,9 @@
inkscape:connector-curvature="0" />
<circle
style="opacity:1;fill:#ff0000;fill-opacity:1;stroke:none;stroke-width:0.74698794;stroke-opacity:1"
id="path815"
id="badge"
cx="53.625"
cy="10.375"
r="7.75" />
</g>
<style
id="style1440"
type="text/css">
.st0{fill:#3E3E3E;}
.st1{fill:#FFFFFF;}
</style>
</svg>
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment