import GLib from "gi://GLib"; import { sh, bash } from "../functions/utils.js"; const now = () => GLib.DateTime.new_now_local().format("%Y-%m-%d_%H-%M-%S"); class Recorder extends Service { static { Service.register( this, {}, { timer: ["int"], recording: ["boolean"], }, ); } #recordings = Utils.HOME + "/Videos/Screencasting"; #screenshots = Utils.HOME + "/Pictures/Screenshots"; #file = ""; #interval = 0; recording = false; timer = 0; async start() { if (this.recording) return; Utils.ensureDirectory(this.#recordings); this.#file = `${this.#recordings}/${now()}.mp4`; sh( `wf-recorder -g "${await sh("slurp")}" -f ${this.#file} --pixel-format yuv420p`, ); this.recording = true; this.changed("recording"); this.timer = 0; this.#interval = Utils.interval(1000, () => { this.changed("timer"); this.timer++; }); } async stop() { if (!this.recording) return; await bash("killall -INT wf-recorder"); this.recording = false; this.changed("recording"); GLib.source_remove(this.#interval); Utils.notify({ iconName: "video-x-generic-symbolic", summary: "Screenrecord", body: this.#file, actions: { "Show in Files": () => sh(`xdg-open ${this.#recordings}`), View: () => sh(`xdg-open ${this.#file}`), }, }); } async screenshot(full = false) { const file = `${this.#screenshots}/${now()}.png`; Utils.ensureDirectory(this.#screenshots); if (full) { await sh(`grim ${file}`); } else { const size = await sh("slurp -b#00000066 -w 0"); if (!size) return; await sh(`grim -g "${size}" ${file}`); } bash(`wl-copy < ${file}`); Utils.notify({ image: file, summary: "Screenshot", body: file, actions: { "Show in Files": () => sh(`xdg-open ${this.#screenshots}`), View: () => sh(`xdg-open ${file}`), Edit: () => sh(`swappy -f ${file}`), }, timeout: 5000, }); } } const service = new Recorder(); export default service;