diff --git a/style/_wallpaper-manager.scss b/style/_wallpaper-manager.scss new file mode 100644 index 0000000..f019b69 --- /dev/null +++ b/style/_wallpaper-manager.scss @@ -0,0 +1,30 @@ +@use "./_variable.scss" as *; +@use "sass:math"; +@use "sass:list"; + +window.wallpaper-manager { + background: $bg-color; + color: $fg-color; + min-width: 400px; + margin: 10px; + padding: 20px; + border-radius: $rounded; + + .button { + border-radius: $rounded; + opacity: 0.5; + + &:hover, + &:focus { + opacity: 1; + } + } + + picture { + border-radius: $rounded; + + &:hover { + background: $inactive-color; + } + } +} diff --git a/style/main.scss b/style/main.scss index 30372f5..3ae50cd 100644 --- a/style/main.scss +++ b/style/main.scss @@ -12,3 +12,6 @@ // control center @use "./_control-center.scss"; + +// wallpaper manager +@use "./_wallpaper-manager.scss"; diff --git a/widgets/wallpaper-manager/index.tsx b/widgets/wallpaper-manager/index.tsx new file mode 100644 index 0000000..0819f95 --- /dev/null +++ b/widgets/wallpaper-manager/index.tsx @@ -0,0 +1,103 @@ +import { Astal, Gdk, Gtk } from "ags/gtk4"; +import app from "ags/gtk4/app"; +import { exec } from "ags/process"; +import Gio from "gi://Gio"; +import GLib from "gi://GLib"; + +export const WINDOW_NAME = "wallpaper-manager"; + +// @TODO wallpaper cache +const wallpaperPath = `/home/tux/Wallpapers/new`; +const imageFormats = [".jpeg", ".jpg", ".webp", ".png"]; + +export const WallpaperManager = (gdkmonitor: Gdk.Monitor) => { + const { TOP, BOTTOM, LEFT } = Astal.WindowAnchor; + const { VERTICAL } = Gtk.Orientation; + const wallpaperList = getWallpaperList(wallpaperPath); + + return ( + + + + + {wallpaperList.map((wall) => ( + + ))} + + + + ); +}; + +const onKey = ( + _e: Gtk.EventControllerKey, + keyval: number, + _: number, + mod: number, +) => { + if (keyval === Gdk.KEY_Escape) { + app.toggle_window(WINDOW_NAME); + return; + } +}; + +function getWallpaperList(path: string) { + const dir = Gio.file_new_for_path(path); + const fileEnum = dir.enumerate_children( + "standard::name", + Gio.FileQueryInfoFlags.NONE, + null, + ); + + const files: string[] = []; + let i = fileEnum.next_file(null); + while (i) { + let fileName = i.get_name(); + if (imageFormats.some((fmt) => fileName.endsWith(fmt))) { + files.push(fileName); + } + i = fileEnum.next_file(null); + } + return files; +} + +const setWallpaper = (name: string) => { + const hyprctl = GLib.find_program_in_path("hyprctl"); + const imagePath = `${wallpaperPath}/${name}`; + + if (!hyprctl) return; + + const preloadedWalls = exec([hyprctl, "hyprpaper", "listloaded"]); + const nWall = preloadedWalls.split("\n").length; + console.log(nWall); + + if (nWall >= 5) { + exec([hyprctl, "hyprpaper", "unload", "all"]); + } + + exec([hyprctl, "hyprpaper", "preload", imagePath]); + exec([hyprctl, "hyprpaper", "wallpaper", `,${imagePath}`]); + + app.toggle_window(WINDOW_NAME); +}; diff --git a/windows.ts b/windows.ts index 11b2e74..d8000a1 100644 --- a/windows.ts +++ b/windows.ts @@ -2,5 +2,6 @@ import { Bar } from "./widgets/bar"; import { Launcher } from "./widgets/launcher"; import { Notifications } from "./widgets/notifications"; import { ControlCenter } from "./widgets/control-center"; +import { WallpaperManager } from "./widgets/wallpaper-manager"; -export default [Bar, Launcher, Notifications, ControlCenter]; +export default [Bar, Launcher, Notifications, ControlCenter, WallpaperManager];