ags/lib/widgets/datetime.tsx

24 lines
729 B
TypeScript
Raw Normal View History

2025-02-06 12:05:55 +01:00
import { Variable, GLib } from "astal";
import { Widget } from "astal/gtk4";
type Props = {
format: string,
interval?: number
} & Widget.LabelProps;
export default function DateTime({ format, interval, ...props }: Props) {
const shouldPoll = typeof interval === "number" && interval >= 1;
const currentTime = () => {
const dateTime = GLib.DateTime.new_now_local();
return dateTime.format(format)!;
}
if (shouldPoll) {
const pollTime = new Variable(currentTime()).poll(interval || 1000, currentTime);
return <label label={pollTime()} {...props} />
} else {
const time = new Variable(currentTime())
return <label label={time()} {...props} />
}
}