Files
colorshell/ags/app.ts
T
retrozinndev 1d5fe06f8a 🔧 chore(scripts/config): stop using singleton instance, add type declarations(for completion)
this will make easier to work with multiple configuration files at the same time
2025-08-03 15:02:36 -03:00

169 lines
5.2 KiB
TypeScript

import { Wireplumber } from "./scripts/volume";
import { handleArguments } from "./scripts/arg-handler";
import { Time, timeout } from "ags/time";
import { Runner } from "./runner/Runner";
import { PluginApps } from "./runner/plugins/apps";
import { PluginShell } from "./runner/plugins/shell";
import { PluginWebSearch } from "./runner/plugins/websearch";
import { PluginMedia } from "./runner/plugins/media";
import { Windows } from "./windows";
import { Notifications } from "./scripts/notifications";
import { PluginWallpapers } from "./runner/plugins/wallpapers";
import { Wallpaper } from "./scripts/wallpaper";
import { Stylesheet } from "./scripts/stylesheet";
import { Clipboard } from "./scripts/clipboard";
import { PluginClipboard } from "./runner/plugins/clipboard";
import { Config } from "./scripts/config";
import { Scope } from "/usr/share/ags/js/gnim/src/jsx/scope";
import App from "ags/gtk4/app"
import GObject from "ags/gobject";
import AstalNotifd from "gi://AstalNotifd";
import GLib from "gi://GLib?version=2.0";
type ConfigEntries = {
workspaces?: Partial<{
/** this is the function that shows the Workspace's IDs
* around the current workspace if one breaks the crescent order.
* It basically helps keyboard navigation between workspaces.
* ---
* Example: 1(empty, current, shows ID), 2(empty, does not appear(makes
* the previous not to be in a crescent order)), 3(not empty, shows ID) */
enable_helper: boolean;
/** breaks `enable_helper`, makes all workspaces show their respective ID
* by default */
always_show_id: boolean;
}>;
clock?: Partial<{
/** use the same format as gnu's `date` command */
date_format: string;
}>;
notifications?: Partial<{
timeout_low: number;
timeout_normal: number;
timeout_critical: number;
}>;
night_light?: Partial<{
/** whether to save night light values to disk */
save_on_shutdown: boolean;
}>;
misc?: Partial<{
play_bell_on_volume_change: boolean;
}>;
};
export const generalConfig = new Config<keyof ConfigEntries, ConfigEntries[keyof ConfigEntries]>(
`${GLib.get_user_config_dir()}/colorshell/config.json`, {
notifications: {
timeout_low: 4000,
timeout_normal: 6000,
timeout_critical: 0
},
night_light: {
save_on_shutdown: true
},
workspaces: {
always_show_id: false,
enable_helper: true
},
clock: {
date_format: "%A %d, %H:%M"
},
misc: {
play_bell_on_volume_change: true
}
}
);
export const appScope: Scope = new Scope(null);
let osdTimer: (Time|undefined), osdTimeout = 3500;
let connections = new Map<GObject.Object, (Array<number> | number)>();
const runnerPlugins: Array<Runner.Plugin> = [
PluginApps,
PluginShell,
PluginWebSearch,
PluginMedia,
PluginWallpapers,
PluginClipboard
];
const defaultWindows: Array<string> = [ "bar" ];
App.start({
instanceName: "astal",
icons: "icons/",
requestHandler: (request: string, response: (result: any) => void): void => {
response(handleArguments(request));
},
main: (..._args: Array<string>) => {
console.log(`Colorshell: initialized instance as: "${ App.instanceName || "astal" }"`);
connections.set(App, App.connect("shutdown", () => appScope.dispose()));
Stylesheet.getDefault().compileApply();
// Init clipboard module
Clipboard.getDefault();
console.log("Initializing wallpaper handler");
Wallpaper.getDefault();
console.log("Adding runner plugins");
runnerPlugins.forEach(plugin => Runner.addPlugin(plugin));
connections.set(Wireplumber.getDefault(),
Wireplumber.getDefault().getDefaultSink().connect("notify::volume", () =>
triggerOSD())
);
connections.set(Notifications.getDefault(), [
Notifications.getDefault().connect("notification-added", (_, _notif: AstalNotifd.Notification) => {
Windows.getDefault().open("floating-notifications");
}),
Notifications.getDefault().connect("notification-removed", (_: Notifications, _id: number) => {
_.notifications.length === 0 && Windows.getDefault().close("floating-notifications");
})
]);
defaultWindows.forEach(w => Windows.getDefault().open(w));
appScope.onCleanup(() => {
console.log("Colorshell: disconnecting from GObjects because of ::shutdown");
connections.forEach((ids, obj) => Array.isArray(ids) ?
ids.forEach(id => obj.disconnect(id))
: obj.disconnect(ids));
});
}
});
function triggerOSD() {
if(Windows.getDefault().isOpen("control-center")) return;
Windows.getDefault().open("osd");
if(!osdTimer) {
osdTimer = timeout(osdTimeout, () => {
osdTimer = undefined;
Windows.getDefault().close("osd");
});
return;
}
osdTimer.cancel();
osdTimer = timeout(osdTimeout, () => {
Windows.getDefault().close("osd");
osdTimer = undefined;
});
}