mirror of
https://github.com/vincentbernat/i3wm-configuration.git
synced 2025-06-30 13:24:21 +02:00
xss-dimmer: seperate timer handling from drawing
This commit is contained in:
parent
77c1503555
commit
a4bd93b969
1 changed files with 46 additions and 59 deletions
|
@ -43,20 +43,13 @@ def on_realize(widget):
|
|||
|
||||
|
||||
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()
|
||||
wwidth, wheight = widget.get_size()
|
||||
delta = options.end_opacity - options.start_opacity
|
||||
elapsed = time.monotonic() - start
|
||||
current = easing_functions[options.easing_function](elapsed / options.delay)
|
||||
opacity = delta * current + options.start_opacity
|
||||
cctx = event
|
||||
|
||||
# Background
|
||||
cctx.set_operator(cairo.OPERATOR_SOURCE)
|
||||
|
@ -73,8 +66,6 @@ def on_draw(widget, event, options, background, start):
|
|||
cctx.restore()
|
||||
|
||||
# Remaining time
|
||||
if elapsed >= options.delay:
|
||||
return
|
||||
remaining = str(round(options.delay - elapsed))
|
||||
cctx.select_font_face(
|
||||
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.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)
|
||||
on_draw.timer = GLib.timeout_add(next_step * 1000, _dim)
|
||||
|
||||
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)
|
||||
|
||||
GLib.timeout_add(options.step * 1000, refresh, window, options, start)
|
||||
|
||||
# See: https://easings.net/
|
||||
easing_functions = {
|
||||
|
@ -186,6 +170,9 @@ if __name__ == "__main__":
|
|||
|
||||
window.show_all()
|
||||
|
||||
# Schedule refresh with window.queue_draw()
|
||||
refresh(window, options, now)
|
||||
|
||||
# Watch for locker window
|
||||
xdisplay = display.Display()
|
||||
root = xdisplay.screen().root
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue