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