dimmer: tentative to not use cairo_create() which is deprecated

However, this does not work...
This commit is contained in:
Vincent Bernat 2021-08-07 09:49:47 +02:00
parent ce96bd5b32
commit b77d74171e

View file

@ -1,4 +1,4 @@
#!/usr/bin/env -S python3 -W ignore::DeprecationWarning
#!/usr/bin/env -S python3
# -*- python -*-
"""Simple dimmer for xss-lock.
@ -42,28 +42,34 @@ def on_realize(widget):
def on_draw(widget, event, options, elapsed):
def dim(once=False):
cr = Gdk.cairo_create(window)
def dim(once=False, context=None):
if context is None:
dctx = window.begin_draw_frame(cairo.Region())
cctx = dctx.get_cairo_context()
else:
cctx = context
# Background
delta = options.end_opacity - options.start_opacity
current = elapsed[0] / options.delay
opacity = delta * current + options.start_opacity
cr.set_source_rgba(0, 0, 0, opacity)
cr.set_operator(cairo.OPERATOR_SOURCE)
cr.paint()
cctx.set_source_rgba(0, 0, 0, opacity)
cctx.set_operator(cairo.OPERATOR_SOURCE)
cctx.paint()
# Remaining time
remaining = str(round(options.delay - elapsed[0]))
wwidth, wheight = widget.get_default_size()
cr.set_source_rgba(1, 1, 1, opacity)
cr.select_font_face(
cctx.set_source_rgba(1, 1, 1, opacity)
cctx.select_font_face(
options.font, cairo.FONT_SLANT_NORMAL, cairo.FONT_WEIGHT_BOLD
)
cr.set_font_size(wheight // 4)
_, _, twidth, theight, _, _ = cr.text_extents(remaining)
cr.move_to(wwidth // 2 - twidth // 2, wheight // 2 + theight // 2)
cr.show_text(remaining)
cctx.set_font_size(wheight // 4)
_, _, twidth, theight, _, _ = cctx.text_extents(remaining)
cctx.move_to(wwidth // 2 - twidth // 2, wheight // 2 + theight // 2)
cctx.show_text(remaining)
if context is None:
window.end_draw_frame(dctx)
# Rearm timer
if not once:
@ -75,10 +81,10 @@ def on_draw(widget, event, options, elapsed):
if not elapsed:
# First time we are called.
elapsed.append(0)
dim()
dim(context=event)
else:
# Timers already running, just repaint
dim(once=True)
dim(once=True, context=event)
if __name__ == "__main__":