From ee820970a4651d0db8a295a3d3d3b67e002df0a8 Mon Sep 17 00:00:00 2001 From: retrozinndev Date: Sat, 26 Apr 2025 22:43:06 -0300 Subject: [PATCH] :sparkles: ags(runner/plugins): add wallpaper plugin with prefix `#` --- ags/app.ts | 4 ++- ags/runner/plugins/wallpapers.ts | 43 ++++++++++++++++++++++++++++++++ 2 files changed, 46 insertions(+), 1 deletion(-) create mode 100644 ags/runner/plugins/wallpapers.ts diff --git a/ags/app.ts b/ags/app.ts index 5665213..cf4e74a 100644 --- a/ags/app.ts +++ b/ags/app.ts @@ -17,6 +17,7 @@ import { PluginMedia } from "./runner/plugins/media"; import { Windows } from "./windows"; import { Notifications } from "./scripts/notifications"; import { GObject } from "astal"; +import { PluginWallpapers } from "./runner/plugins/wallpapers"; let osdTimer: (Time|undefined); @@ -27,7 +28,8 @@ const runnerPlugins: Array = [ PluginApps, PluginShell, PluginWebSearch, - PluginMedia + PluginMedia, + new PluginWallpapers() ]; App.start({ diff --git a/ags/runner/plugins/wallpapers.ts b/ags/runner/plugins/wallpapers.ts new file mode 100644 index 0000000..2d3eb9d --- /dev/null +++ b/ags/runner/plugins/wallpapers.ts @@ -0,0 +1,43 @@ +import { Gio } from "astal"; +import { Wallpaper } from "../../scripts/wallpaper"; +import { Runner } from "../Runner"; +import { ResultWidget, ResultWidgetProps } from "../../widget/runner/ResultWidget"; + + +export class PluginWallpapers implements Runner.Plugin { + prefix = "#"; + prioritize = true; + #files: (Array|undefined); + + init() { + this.#files = []; + const dir = Gio.File.new_for_path(Wallpaper.getDefault().wallpapersPath); + if(dir.query_file_type(null, null) === Gio.FileType.DIRECTORY) { + for(const file of dir.enumerate_children( + "standard::*", + Gio.FileQueryInfoFlags.NOFOLLOW_SYMLINKS, + null + )) { + this.#files.push(`${dir.get_path()}/${file.get_name()}`); + } + } + } + + handle(search: string) { + if(this.#files!.length > 0) + return this.#files!.filter(file => // not the best way to search, but it works + new RegExp(`${search.split('').map(c => + `.*(${c.toLowerCase()}|${c.toUpperCase()}).*`).join('')}` + ).test(file.split('/')[file.split('/').length-1]) + ).map(path => new ResultWidget({ + title: path.split('/')[path.split('/').length-1].replace(/\..*$/, ""), + onClick: () => Wallpaper.getDefault().setWallpaper(path) + } as ResultWidgetProps)); + + return new ResultWidget({ + title: "No wallpapers found!", + description: "Define the $WALLPAPERS variable on Hyprland or create a ~/wallpapers directory", + icon: "image-missing-symbolic" + } as ResultWidgetProps); + } +}