diff options
| author | srdusr <trevorgray@srdusr.com> | 2024-06-13 13:11:05 +0200 |
|---|---|---|
| committer | srdusr <trevorgray@srdusr.com> | 2024-06-13 13:11:05 +0200 |
| commit | d0fbb19623e4fb6097e1ff3ee49c6a76a0928d0e (patch) | |
| tree | 937531ddf423d3935c6e20c8a9227e39ce782241 /.config/ags/service/wallpaper.ts | |
| parent | 4ccbe0270c25ecab492508b5b0209ae53b9c35bd (diff) | |
| download | dotfiles-d0fbb19623e4fb6097e1ff3ee49c6a76a0928d0e.tar.gz dotfiles-d0fbb19623e4fb6097e1ff3ee49c6a76a0928d0e.zip | |
Add ags
Diffstat (limited to '.config/ags/service/wallpaper.ts')
| -rw-r--r-- | .config/ags/service/wallpaper.ts | 98 |
1 files changed, 98 insertions, 0 deletions
diff --git a/.config/ags/service/wallpaper.ts b/.config/ags/service/wallpaper.ts new file mode 100644 index 0000000..0e4bdda --- /dev/null +++ b/.config/ags/service/wallpaper.ts @@ -0,0 +1,98 @@ +import options from "options" +import { dependencies, sh } from "lib/utils" + +export type Resolution = 1920 | 1366 | 3840 +export type Market = + | "random" + | "en-US" + | "ja-JP" + | "en-AU" + | "en-GB" + | "de-DE" + | "en-NZ" + | "en-CA" + +const WP = `${Utils.HOME}/.config/background` +const Cache = `${Utils.HOME}/Pictures/Wallpapers/Bing` + +class Wallpaper extends Service { + static { + Service.register(this, {}, { + "wallpaper": ["string"], + }) + } + + #blockMonitor = false + + #wallpaper() { + if (!dependencies("swww")) + return + + sh("hyprctl cursorpos").then(pos => { + sh([ + "swww", "img", + "--transition-type", "grow", + "--transition-pos", pos.replace(" ", ""), + WP, + ]).then(() => { + this.changed("wallpaper") + }) + }) + } + + async #setWallpaper(path: string) { + this.#blockMonitor = true + + await sh(`cp ${path} ${WP}`) + this.#wallpaper() + + this.#blockMonitor = false + } + + async #fetchBing() { + const res = await Utils.fetch("https://bing.biturl.top/", { + params: { + resolution: options.wallpaper.resolution.value, + format: "json", + image_format: "jpg", + index: "random", + mkt: options.wallpaper.market.value, + }, + }).then(res => res.text()) + + if (!res.startsWith("{")) + return console.warn("bing api", res) + + const { url } = JSON.parse(res) + const file = `${Cache}/${url.replace("https://www.bing.com/th?id=", "")}` + + if (dependencies("curl")) { + Utils.ensureDirectory(Cache) + await sh(`curl "${url}" --output ${file}`) + this.#setWallpaper(file) + } + } + + readonly random = () => { this.#fetchBing() } + readonly set = (path: string) => { this.#setWallpaper(path) } + get wallpaper() { return WP } + + constructor() { + super() + + if (!dependencies("swww")) + return this + + // gtk portal + Utils.monitorFile(WP, () => { + if (!this.#blockMonitor) + this.#wallpaper() + }) + + Utils.execAsync("swww-daemon") + .then(this.#wallpaper) + .catch(() => null) + } +} + +export default new Wallpaper |
