Skip to content

Commit

Permalink
Add ability to mask StatusNotifier icons
Browse files Browse the repository at this point in the history
Allows users to use icons as a mask and paint with a custom colour.
  • Loading branch information
elParaguayo committed Dec 23, 2024
1 parent 08a9de2 commit 016ec1a
Showing 1 changed file with 50 additions and 0 deletions.
50 changes: 50 additions & 0 deletions qtile_extras/widget/statusnotifier.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import os
from typing import TYPE_CHECKING

import cairocffi
from libqtile.log_utils import logger
from libqtile.widget.helpers.status_notifier import StatusNotifierItem, host
from libqtile.widget.statusnotifier import StatusNotifier as QtileStatusNotifier
Expand Down Expand Up @@ -77,6 +78,11 @@ class StatusNotifier(QtileStatusNotifier, DbusMenuMixin):

_screenshots = [("statusnotifier.png", "Widget showing Remmina icon and context menu.")]

defaults = [
("mask", False, "Use icon as mask. Use 'foreground' to set icon colour."),
("foreground", "fff", "Colour for masked icons"),
]

def __init__(self, **config):
QtileStatusNotifier.__init__(self, **config)
self.add_defaults(DbusMenuMixin.defaults)
Expand Down Expand Up @@ -109,3 +115,47 @@ def show_menu(self):
if not self.selected_item:
return
self.selected_item.get_menu(callback=self.display_menu)

def _draw_icon(self, icon, x, y):
ctx = self.drawer.ctx
ctx.save()

if self.mask:
ctx.translate(x, y)
self.drawer.set_source_rgb(self.foreground)
ctx.set_operator(cairocffi.OPERATOR_SOURCE)
ctx.mask(cairocffi.SurfacePattern(icon))
ctx.fill()

else:
ctx.set_source_surface(icon, x, y)
ctx.paint()

ctx.restore()

def draw(self):
self.drawer.clear(self.background or self.bar.background)

if self.bar.horizontal:
xoffset = self.padding
yoffset = (self.bar.height - self.icon_size) // 2

for item in self.available_icons:
icon = item.get_icon(self.icon_size)
self._draw_icon(icon, xoffset, yoffset)

xoffset += self.icon_size + self.padding

self.drawer.draw(offsetx=self.offset, offsety=self.offsety, width=self.length)

else:
xoffset = (self.bar.width - self.icon_size) // 2
yoffset = self.padding

for item in self.available_icons:
icon = item.get_icon(self.icon_size)
self._draw_icon(icon, xoffset, yoffset)

yoffset += self.icon_size + self.padding

self.drawer.draw(offsety=self.offset, offsetx=self.offsetx, height=self.length)

0 comments on commit 016ec1a

Please sign in to comment.