Files
Classroom_Desktop/lib/classroom_icons.py
T
ekklesiaandClaude Opus 5 a1a218b29f fix: draw classroom icons at the display's real pixel density
Icons went soft under UI_SCALE while the clock text beside them stayed
sharp, which is visible on a 24" monitor even if it hides on a TV.

new_from_file_at_scale rasterises an SVG at its *logical* size. The
artwork is vector, but by the time sway scaled the session it was
already a fixed raster, and the compositor enlarged that. Text never had
the problem because GTK draws it at the real density.

GTK 3 has no fractional scaling, so sway advertises the ceiling integer
scale and scales the finished buffer down. Measured under sway: scale
factor 1 at output scale 1.0, and 2 at 1.25, 1.5 and 2.0. So each icon
is now rasterised at size x scale_factor physical pixels and handed to
GTK as a cairo surface carrying that device scale. The icon lands at the
real density; its logical size, and the whole dock layout, is unchanged.

Icons redraw on notify::scale-factor as well as at realize, since the
scale factor is not known until a widget is on a display and changes if
UI_SCALE is changed under a running session.

Browser, DVD, LIVE and all six DVD control icons go through it, in a
shared module so the dock and the DVD session cannot diverge. The
throbber is drawn rather than loaded and had the same flaw, so it draws
on a device-scaled surface too rather than being the one soft thing left
in a sharp dock.

The artwork is untouched. Verified by rendering the dock under sway at
1.0, 1.25, 1.5 and 2.0 against the previous build: apparent icon size is
identical at every scale (28, 35, 42, 56 px), 1.0 is unchanged
pixel-for-pixel, and peak edge contrast on the Chrome icon goes from 60
to 99 at 1.25, 49 to 83 at 1.5, and 37 to 82 at 2.0 out of 255. The DVD
control bar was checked the same way.

Also records that Wonder Ink session-only cookies are confirmed working
on the endpoint, which settles the question 0.1.34 shipped as unverified
and tells us the session is in cookies rather than localStorage.

Co-Authored-By: Claude Opus 5 <[email protected]>
2026-09-06 14:19:38 -05:00

96 lines
2.9 KiB
Python

"""Icons that stay sharp when the compositor scales the session.
new_from_file_at_scale rasterises an SVG at its *logical* size. Under
sway's output scaling the compositor then enlarges that finished raster,
which is why icons went soft while the clock text and the dock surface
stayed crisp: those are drawn by GTK at the display's real pixel
density, and the icon was not.
GTK 3 has no fractional scaling, so sway advertises the ceiling integer
scale and scales the finished buffer down. Measured under sway: scale
factor 1 at output scale 1.0, and 2 at 1.25, 1.5 and 2.0.
So the fix is to rasterise at size * scale_factor physical pixels and
hand GTK a cairo surface carrying that device scale. The icon lands at
the real density, while its logical size — and therefore the whole dock
layout — is exactly what it was before.
"""
from pathlib import Path
import gi
gi.require_version("Gtk", "3.0")
gi.require_version("Gdk", "3.0")
gi.require_version("GdkPixbuf", "2.0")
from gi.repository import Gdk, GdkPixbuf, GLib, Gtk # noqa: E402
ICON_DIR = Path("/etc/ekklesia-classroom/icons")
def render_surface(path, size, scale, window=None):
"""An SVG at `size` logical pixels, drawn at `scale` times the density.
The box semantics match the old custom_icon(): the artwork is fitted
inside a square of the requested size with its aspect preserved, so
a wide logo like the DVD wordmark keeps the proportions it has now.
"""
pixbuf = GdkPixbuf.Pixbuf.new_from_file_at_scale(
str(path),
size * scale,
size * scale,
True,
)
return Gdk.cairo_surface_create_from_pixbuf(pixbuf, scale, window)
class ScalableIcon(Gtk.Image):
"""A custom SVG icon, redrawn whenever the display density changes."""
def __init__(self, filename, size, directory=None):
super().__init__()
self.directory = directory or ICON_DIR
self.size = size
self.drawn_at = None
self.usable = False
self.set_icon(filename)
# The scale factor is not known until the widget is on a display,
# and it changes if UI_SCALE is changed under a running session.
self.connect("realize", lambda *_: self.redraw())
self.connect("notify::scale-factor", lambda *_: self.redraw())
def set_icon(self, filename):
self.path = self.directory / filename
self.usable = self.path.exists()
self.drawn_at = None
self.redraw()
def redraw(self):
if not self.usable:
return
scale = self.get_scale_factor() or 1
if scale == self.drawn_at:
return
try:
surface = render_surface(
self.path,
self.size,
scale,
self.get_window(),
)
except (GLib.Error, MemoryError):
self.usable = False
return
self.set_from_surface(surface)
self.drawn_at = scale