diff options
Diffstat (limited to 'src/utils/system.ts')
| -rw-r--r-- | src/utils/system.ts | 16 |
1 files changed, 13 insertions, 3 deletions
diff --git a/src/utils/system.ts b/src/utils/system.ts index 8180e48..7ae23dd 100644 --- a/src/utils/system.ts +++ b/src/utils/system.ts @@ -1,4 +1,4 @@ -import { bind, execAsync, GLib, Variable, type Gio } from "astal"; +import { bind, execAsync, GLib, Variable, type Binding, type Gio } from "astal"; import type AstalApps from "gi://AstalApps"; import { osIcons } from "./icons"; @@ -63,5 +63,15 @@ export const osIcon = (() => { })(); export const currentTime = Variable(GLib.DateTime.new_now_local()).poll(1000, () => GLib.DateTime.new_now_local()); -export const bindCurrentTime = (format: string, fallback?: (time: GLib.DateTime) => string) => - bind(currentTime).as(c => c.format(format) ?? fallback?.(c) ?? new Date().toLocaleString()); +export const bindCurrentTime = ( + format: Binding<string> | string, + fallback?: (time: GLib.DateTime) => string, + self?: JSX.Element +) => { + const fmt = (c: GLib.DateTime, format: string) => c.format(format) ?? fallback?.(c) ?? new Date().toLocaleString(); + if (typeof format === "string") return bind(currentTime).as(c => fmt(c, format)); + if (!self) throw new Error("bindCurrentTime: self is required when format is a Binding"); + const time = Variable.derive([currentTime, format], (c, f) => fmt(c, f)); + self?.connect("destroy", () => time.drop()); + return bind(time); +}; |