wallpapers: filter by size

This commit is contained in:
2 * r + 2 * t 2025-04-04 21:41:34 +11:00
parent aed36b160f
commit a793205cc2
4 changed files with 33 additions and 6 deletions

View file

@ -133,6 +133,7 @@ export default {
{
recursive: true, // Whether to search recursively
path: "~/Pictures/Wallpapers", // Path to search
threshold: 0.8, // The threshold to filter wallpapers by size (e.g. 0.8 means wallpaper must be at least 80% of the screen size), 0 to disable
},
],
},

View file

@ -29,12 +29,12 @@ const isCorrectType = (v: any, type: string | string[] | number[], path: string)
v.splice(0, v.length, ...v.filter((item, i) => isCorrectType(item, type, `${path}[${i}]`)));
} else {
const valid = v.filter((item, i) =>
Object.entries(type).some(([k, t]) => {
if (!item[k]) {
Object.entries(type).every(([k, t]) => {
if (!item.hasOwnProperty(k)) {
console.warn(`Invalid shape for ${path}[${i}]: ${JSON.stringify(item)} != ${arrType}`);
return false;
}
return !isCorrectType(item[k], t as any, `${path}[${i}].${k}`);
return isCorrectType(item[k], t as any, `${path}[${i}].${k}`);
})
);
v.splice(0, v.length, ...valid); // In-place filter

View file

@ -75,7 +75,7 @@ export default {
"gpu.interval": NUM,
"memory.interval": NUM,
"storage.interval": NUM,
"wallpapers.paths": OBJ_ARR({ recursive: BOOL, path: STR }),
"wallpapers.paths": OBJ_ARR({ recursive: BOOL, path: STR, threshold: NUM }),
"calendar.webcals": ARR(STR),
"calendar.upcomingDays": NUM,
"calendar.notify": BOOL,

View file

@ -2,6 +2,7 @@ import { monitorDirectory } from "@/utils/system";
import Thumbnailer from "@/utils/thumbnailer";
import { execAsync, GObject, property, register } from "astal";
import { wallpapers as config } from "config";
import Monitors from "./monitors";
export interface IWallpaper {
path: string;
@ -35,10 +36,35 @@ export default class Wallpapers extends GObject.Object {
return this.#categories;
}
#listDir(path: { path: string; recursive: boolean }, type: "f" | "d") {
async #listDir(path: { path: string; recursive: boolean; threshold: number }, type: "f" | "d") {
const absPath = path.path.replace("~", HOME);
const maxDepth = path.recursive ? "" : "-maxdepth 1";
return execAsync(`find ${absPath} ${maxDepth} -path '*/.*' -prune -o -type ${type} -print`);
const files = await execAsync(`find ${absPath} ${maxDepth} -path '*/.*' -prune -o -type ${type} -print`);
if (path.threshold > 0) {
const data = (
await execAsync([
"fish",
"-c",
`identify -ping -format '%i %w %h\n' ${files.replaceAll("\n", " ")} ; true`,
])
).split("\n");
return data
.filter(l => l && this.#filterSize(l, path.threshold))
.map(l => l.split(" ").slice(0, -2).join(" "))
.join("\n");
}
return files;
}
#filterSize(line: string, threshold: number) {
const [width, height] = line.split(" ").slice(-2).map(Number);
const mWidth = Math.max(...Monitors.get_default().list.map(m => m.width));
const mHeight = Math.max(...Monitors.get_default().list.map(m => m.height));
return width >= mWidth * threshold && height >= mHeight * threshold;
}
async update() {