xss-dimmer: seperate timer handling from drawing

This commit is contained in:
Vincent Bernat 2021-12-08 00:28:21 +01:00
parent 77c1503555
commit a4bd93b969

View file

@ -43,20 +43,13 @@ def on_realize(widget):
def on_draw(widget, event, options, background, start): def on_draw(widget, event, options, background, start):
def _dim():
r = cairo.Region(cairo.RectangleInt(0, 0, *widget.get_size()))
dctx = window.begin_draw_frame(r)
cctx = dctx.get_cairo_context()
dim(cctx)
window.end_draw_frame(dctx)
def dim(cctx, once=False):
x, y = widget.get_position() x, y = widget.get_position()
wwidth, wheight = widget.get_size() wwidth, wheight = widget.get_size()
delta = options.end_opacity - options.start_opacity delta = options.end_opacity - options.start_opacity
elapsed = time.monotonic() - start elapsed = time.monotonic() - start
current = easing_functions[options.easing_function](elapsed / options.delay) current = easing_functions[options.easing_function](elapsed / options.delay)
opacity = delta * current + options.start_opacity opacity = delta * current + options.start_opacity
cctx = event
# Background # Background
cctx.set_operator(cairo.OPERATOR_SOURCE) cctx.set_operator(cairo.OPERATOR_SOURCE)
@ -73,8 +66,6 @@ def on_draw(widget, event, options, background, start):
cctx.restore() cctx.restore()
# Remaining time # Remaining time
if elapsed >= options.delay:
return
remaining = str(round(options.delay - elapsed)) remaining = str(round(options.delay - elapsed))
cctx.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
@ -91,20 +82,13 @@ def on_draw(widget, event, options, background, start):
cctx.text_path(remaining) cctx.text_path(remaining)
cctx.stroke() cctx.stroke()
# Rearm timer
if not once: def refresh(window, options, start):
window.queue_draw()
elapsed = time.monotonic() - start
if elapsed < options.delay:
next_step = min(options.step, options.delay - elapsed) next_step = min(options.step, options.delay - elapsed)
on_draw.timer = GLib.timeout_add(next_step * 1000, _dim) GLib.timeout_add(options.step * 1000, refresh, window, options, start)
window = widget.get_window()
dim(event)
if not hasattr(on_draw, "timer"):
# First time we are called.
dim(event)
else:
# Timers already running, just repaint
dim(event, once=True)
# See: https://easings.net/ # See: https://easings.net/
easing_functions = { easing_functions = {
@ -186,6 +170,9 @@ if __name__ == "__main__":
window.show_all() window.show_all()
# Schedule refresh with window.queue_draw()
refresh(window, options, now)
# Watch for locker window # Watch for locker window
xdisplay = display.Display() xdisplay = display.Display()
root = xdisplay.screen().root root = xdisplay.screen().root