[Build] Start refactor
This commit is contained in:
82
configs/userland/astal/util/brightness.ts
Normal file
82
configs/userland/astal/util/brightness.ts
Normal file
@@ -0,0 +1,82 @@
|
||||
import GObject, { register, property } from "astal/gobject"
|
||||
import { monitorFile, readFileAsync } from "astal/file"
|
||||
import { exec, execAsync } from "astal/process"
|
||||
|
||||
const get = (args: string) => Number(exec(`brightnessctl ${args}`))
|
||||
const screen = exec(`bash -c "ls -w1 /sys/class/backlight | head -1"`)
|
||||
const kbd = exec(`bash -c "ls -w1 /sys/class/leds | head -1"`)
|
||||
|
||||
@register({ GTypeName: "Brightness" })
|
||||
export default class Brightness extends GObject.Object {
|
||||
static instance: Brightness
|
||||
static get_default() {
|
||||
if (!this.instance)
|
||||
this.instance = new Brightness()
|
||||
|
||||
return this.instance
|
||||
}
|
||||
|
||||
#kbdMax = get(`--device ${kbd} max`)
|
||||
#kbd = get(`--device ${kbd} get`)
|
||||
#screenMax = get("max")
|
||||
#screen = get("get") / (get("max") || 1)
|
||||
#screenAvailable = true
|
||||
|
||||
@property(Boolean)
|
||||
get screenAvailable() { return this.#screenAvailable }
|
||||
|
||||
@property(Number)
|
||||
get kbd() { return this.#kbd }
|
||||
|
||||
set kbd(value) {
|
||||
if (value < 0 || value > this.#kbdMax)
|
||||
return
|
||||
|
||||
execAsync(`brightnessctl -d ${kbd} s ${value} -q`).then(() => {
|
||||
this.#kbd = value
|
||||
this.notify("kbd")
|
||||
})
|
||||
}
|
||||
|
||||
@property(Number)
|
||||
get screen() { return this.#screen }
|
||||
|
||||
set screen(percent) {
|
||||
if (percent < 0)
|
||||
percent = 0
|
||||
|
||||
if (percent > 1)
|
||||
percent = 1
|
||||
|
||||
execAsync(`brightnessctl set ${Math.floor(percent * 100)}% -q`).then(() => {
|
||||
this.#screen = percent
|
||||
this.notify("screen")
|
||||
})
|
||||
}
|
||||
|
||||
constructor() {
|
||||
super()
|
||||
|
||||
const screenPath = `/sys/class/backlight/${screen}/brightness`
|
||||
const kbdPath = `/sys/class/leds/${kbd}/brightness`
|
||||
|
||||
monitorFile(screenPath, async f => {
|
||||
const v = await readFileAsync(f)
|
||||
this.#screen = Number(v) / this.#screenMax
|
||||
this.notify("screen")
|
||||
})
|
||||
|
||||
monitorFile(kbdPath, async f => {
|
||||
const v = await readFileAsync(f)
|
||||
this.#kbd = Number(v) / this.#kbdMax
|
||||
this.notify("kbd")
|
||||
})
|
||||
|
||||
// Check if there is a screen available
|
||||
try {
|
||||
get( 'g -c backlight' );
|
||||
} catch ( _ ) {
|
||||
this.#screenAvailable = false;
|
||||
}
|
||||
}
|
||||
}
|
||||
6
configs/userland/astal/util/colours.scss
Normal file
6
configs/userland/astal/util/colours.scss
Normal file
@@ -0,0 +1,6 @@
|
||||
$fg-color: #E6E6E6;
|
||||
$bg-color: #141414;
|
||||
$accent-color: #EBC405;
|
||||
$accent-color-2: #B81C15;
|
||||
$shadow-color: rgba(40, 40, 40, 0.3);
|
||||
$monospace-font: Source Code Pro
|
||||
28
configs/userland/astal/util/hyprland.ts
Normal file
28
configs/userland/astal/util/hyprland.ts
Normal file
@@ -0,0 +1,28 @@
|
||||
// ┌ ┐
|
||||
// │ From https://github.com/Neurarian/matshell │
|
||||
// └ ┘
|
||||
import { App, Gdk } from "astal/gtk4";
|
||||
import Hyprland from "gi://AstalHyprland";
|
||||
|
||||
/* Match Hyprland monitor to GDK monitor
|
||||
THIS MAY NOT WORK AS INTENDED IF YOU HAVE MONITORS OF THE SAME MODEL
|
||||
I did not find a more elegant solution to this.
|
||||
On my setup GDK coordinates and hyprland coordinates are flipped,
|
||||
so I cant match by coordinates. */
|
||||
|
||||
export function hyprToGdk(monitor: Hyprland.Monitor): Gdk.Monitor | null {
|
||||
const monitors = App.get_monitors();
|
||||
if (!monitors || monitors.length === 0) return null;
|
||||
|
||||
for (let gdkmonitor of monitors) {
|
||||
if (
|
||||
monitor &&
|
||||
gdkmonitor &&
|
||||
monitor.get_name() === gdkmonitor.get_connector()
|
||||
)
|
||||
return gdkmonitor;
|
||||
}
|
||||
|
||||
// Default monitor with null safety
|
||||
return monitors.length > 0 ? monitors[0] : null;
|
||||
}
|
||||
83
configs/userland/astal/util/notifd.ts
Normal file
83
configs/userland/astal/util/notifd.ts
Normal file
@@ -0,0 +1,83 @@
|
||||
// ┌ ┐
|
||||
// │ From https://github.com/Neurarian/matshell │
|
||||
// └ ┘
|
||||
|
||||
import Notifd from "gi://AstalNotifd";
|
||||
import { GLib } from "astal";
|
||||
import { Gtk, Gdk } from "astal/gtk4";
|
||||
|
||||
type TimeoutManager = {
|
||||
setupTimeout: () => void;
|
||||
clearTimeout: () => void;
|
||||
handleHover: () => void;
|
||||
handleHoverLost: () => void;
|
||||
cleanup: () => void;
|
||||
};
|
||||
|
||||
export const createTimeoutManager = (
|
||||
dismissCallback: () => void,
|
||||
timeoutDelay: number,
|
||||
): TimeoutManager => {
|
||||
let isHovered = false;
|
||||
let timeoutId: number | null = null;
|
||||
|
||||
const clearTimeout = () => {
|
||||
if (timeoutId !== null) {
|
||||
GLib.source_remove(timeoutId);
|
||||
timeoutId = null;
|
||||
}
|
||||
};
|
||||
|
||||
const setupTimeout = () => {
|
||||
clearTimeout();
|
||||
|
||||
if (!isHovered) {
|
||||
timeoutId = GLib.timeout_add(GLib.PRIORITY_DEFAULT, timeoutDelay, () => {
|
||||
clearTimeout();
|
||||
dismissCallback();
|
||||
return GLib.SOURCE_REMOVE;
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
return {
|
||||
setupTimeout,
|
||||
clearTimeout,
|
||||
handleHover: () => {
|
||||
isHovered = true;
|
||||
clearTimeout();
|
||||
},
|
||||
handleHoverLost: () => {
|
||||
isHovered = false;
|
||||
setupTimeout();
|
||||
},
|
||||
cleanup: clearTimeout,
|
||||
};
|
||||
};
|
||||
|
||||
export const time = (time: number, format = "%H:%M") =>
|
||||
GLib.DateTime.new_from_unix_local(time).format(format)!;
|
||||
|
||||
export const urgency = (notification: Notifd.Notification) => {
|
||||
const { LOW, NORMAL, CRITICAL } = Notifd.Urgency;
|
||||
|
||||
switch (notification.urgency) {
|
||||
case LOW:
|
||||
return "low";
|
||||
case CRITICAL:
|
||||
return "critical";
|
||||
case NORMAL:
|
||||
default:
|
||||
return "normal";
|
||||
}
|
||||
};
|
||||
|
||||
export const isIcon = (icon: string) => {
|
||||
const display = Gdk.Display.get_default();
|
||||
if (!display) return false;
|
||||
const iconTheme = Gtk.IconTheme.get_for_display(display);
|
||||
return iconTheme.has_icon(icon);
|
||||
};
|
||||
|
||||
export const fileExists = (path: string) =>
|
||||
GLib.file_test(path, GLib.FileTest.EXISTS);
|
||||
3
configs/userland/astal/util/state.ts
Normal file
3
configs/userland/astal/util/state.ts
Normal file
@@ -0,0 +1,3 @@
|
||||
import { Variable } from "astal";
|
||||
|
||||
export const quickActionsState = Variable( false );
|
||||
Reference in New Issue
Block a user