xsecurelock: delay clock and weather to speedup startup

This commit is contained in:
Vincent Bernat 2021-12-09 13:05:50 +01:00
parent eeb7297389
commit 2447dcb575

View file

@ -63,56 +63,55 @@ def on_overlay_draw(widget, cctx, ctx):
# Clock # Clock
cctx.set_operator(cairo.OPERATOR_SOURCE) cctx.set_operator(cairo.OPERATOR_SOURCE)
wwidth, wheight = widget.get_parent().get_size() wwidth, wheight = widget.get_parent().get_size()
now = datetime.datetime.now().strftime("%H:%M") if ctx.clock:
cctx.select_font_face( now = ctx.clock
ctx.font_family, cairo.FONT_SLANT_NORMAL, cairo.FONT_WEIGHT_BOLD cctx.select_font_face(
) ctx.font_family, cairo.FONT_SLANT_NORMAL, cairo.FONT_WEIGHT_BOLD
cctx.set_font_size(ctx.clock_font_size) )
_, _, twidth, theight, _, _ = cctx.text_extents("00:00") cctx.set_font_size(ctx.clock_font_size)
text_position = wwidth // 2 - twidth // 2, wheight // 3 - theight // 2 _, _, twidth, theight, _, _ = cctx.text_extents(re.sub(r"\d", "8", now))
cctx.move_to(*text_position) text_position = wwidth // 2 - twidth // 2, wheight // 3 - theight // 2
cctx.set_source_rgba(1, 1, 1, 0.8) cctx.move_to(*text_position)
cctx.show_text(now) cctx.set_source_rgba(1, 1, 1, 0.8)
cctx.move_to(*text_position) cctx.show_text(now)
cctx.set_source_rgb(0, 0, 0) cctx.move_to(*text_position)
cctx.set_line_width(2) cctx.set_source_rgb(0, 0, 0)
cctx.text_path(now) cctx.set_line_width(2)
cctx.stroke() cctx.text_path(now)
cctx.stroke()
# Weather # Weather
try:
with open(ctx.weather_file) as wfile:
data = wfile.read()
except Exception:
return
# We can have polybar markups in it. We assume %{Tx} means to use # We can have polybar markups in it. We assume %{Tx} means to use
# Font Awesome 6 and we ignore font color change. The parsing is # Font Awesome 6 and we ignore font color change. The parsing is
# quite basic. # quite basic.
data = re.sub(r'%{F[#\d+-]+?}', '', data) if ctx.weather:
data = re.split(r'(%{T[1-9-]})', data) data = re.sub(r"%{F[#\d+-]+?}", "", ctx.weather)
font = ctx.font_family data = re.split(r"(%{T[1-9-]})", data)
cctx.move_to(20, wheight - 20) font = ctx.font_family
for chunk in data: cctx.move_to(20, wheight - 20)
if chunk == "%{T-}": for chunk in data:
font = ctx.font_family if chunk == "%{T-}":
continue font = ctx.font_family
elif chunk.startswith("%{T"): continue
font = "Font Awesome 6 Pro" elif chunk.startswith("%{T"):
continue font = "Font Awesome 6 Pro"
elif not chunk: continue
continue elif not chunk:
cctx.select_font_face(font, cairo.FONT_SLANT_NORMAL, cairo.FONT_WEIGHT_NORMAL) continue
cctx.set_font_size(ctx.weather_font_size) cctx.select_font_face(
cur_position = cctx.get_current_point() font, cairo.FONT_SLANT_NORMAL, cairo.FONT_WEIGHT_NORMAL
cctx.set_source_rgb(0, 0, 0) )
cctx.set_line_width(1) cctx.set_font_size(ctx.weather_font_size)
cctx.text_path(chunk) cur_position = cctx.get_current_point()
cctx.stroke() cctx.set_source_rgb(0, 0, 0)
cctx.move_to(*cur_position) cctx.set_line_width(1)
cctx.set_source_rgba(1, 1, 1, 0.8) cctx.text_path(chunk)
cctx.show_text(chunk) cctx.stroke()
cctx.move_to(*cur_position)
cctx.set_source_rgba(1, 1, 1, 0.8)
cctx.show_text(chunk)
def on_background_change(monitor, f1, f2, event, ctx): def on_background_change(monitor, f1, f2, event, ctx):
"""Update background when changed.""" """Update background when changed."""
if event not in ( if event not in (
@ -129,8 +128,9 @@ def on_background_change(monitor, f1, f2, event, ctx):
def on_clock_change(ctx): def on_clock_change(ctx):
ctx.overlay.queue_draw()
now = datetime.datetime.now() now = datetime.datetime.now()
ctx.clock = now.strftime("%H:%M")
ctx.overlay.queue_draw()
GLib.timeout_add((60 - now.second) * 1000, on_clock_change, ctx) GLib.timeout_add((60 - now.second) * 1000, on_clock_change, ctx)
@ -140,7 +140,12 @@ def on_weather_change(monitor, f1, f2, event, ctx):
Gio.FileMonitorEvent.RENAMED, Gio.FileMonitorEvent.RENAMED,
): ):
return return
ctx.overlay.queue_draw() try:
with open(ctx.weather_file) as wfile:
ctx.weather = wfile.read()
ctx.overlay.queue_draw()
except Exception:
pass
if __name__ == "__main__": if __name__ == "__main__":
@ -151,6 +156,8 @@ if __name__ == "__main__":
ctx.weather_file = os.getenv("XSECURELOCK_SAVER_WEATHER", None) ctx.weather_file = os.getenv("XSECURELOCK_SAVER_WEATHER", None)
ctx.font_family = os.getenv("XSECURELOCK_SAVER_FONT", "Iosevka Aile") ctx.font_family = os.getenv("XSECURELOCK_SAVER_FONT", "Iosevka Aile")
ctx.background = None ctx.background = None
ctx.weather = ""
ctx.clock = ""
ctx.position = [0, 0] ctx.position = [0, 0]
ctx.window = Gtk.Window() ctx.window = Gtk.Window()
@ -163,17 +170,19 @@ if __name__ == "__main__":
ctx.overlay = Gtk.DrawingArea() ctx.overlay = Gtk.DrawingArea()
ctx.overlay.connect("draw", on_overlay_draw, ctx) ctx.overlay.connect("draw", on_overlay_draw, ctx)
ctx.window.add(ctx.overlay) ctx.window.add(ctx.overlay)
on_clock_change(ctx)
gio_event_args = (None, None, None, Gio.FileMonitorEvent.CHANGES_DONE_HINT)
if ctx.background_image: if ctx.background_image:
gfile = Gio.File.new_for_path(ctx.background_image) gfile = Gio.File.new_for_path(ctx.background_image)
monitor = gfile.monitor_file(Gio.FileMonitorFlags.WATCH_MOVES, None) monitor = gfile.monitor_file(Gio.FileMonitorFlags.WATCH_MOVES, None)
monitor.connect("changed", on_background_change, ctx) monitor.connect("changed", on_background_change, ctx)
on_background_change(None, None, None, Gio.FileMonitorEvent.CHANGES_DONE_HINT, ctx) on_background_change(*gio_event_args, ctx)
if ctx.weather_file: if ctx.weather_file:
gfile = Gio.File.new_for_path(ctx.weather_file) gfile = Gio.File.new_for_path(ctx.weather_file)
monitor = gfile.monitor_file(Gio.FileMonitorFlags.WATCH_MOVES, None) monitor = gfile.monitor_file(Gio.FileMonitorFlags.WATCH_MOVES, None)
monitor.connect("changed", on_weather_change, ctx) monitor.connect("changed", on_weather_change, ctx)
GLib.timeout_add(1000, on_weather_change, *gio_event_args, ctx)
GLib.timeout_add(1000, on_clock_change, ctx)
ctx.window.show_all() ctx.window.show_all()