Initial commit

This commit is contained in:
Franek 2025-02-06 12:05:55 +01:00
commit 82da1c1ecb
24 changed files with 829 additions and 0 deletions

2
.gitignore vendored Normal file
View File

@ -0,0 +1,2 @@
node_modules/
@girs/

52
app.ts Normal file
View File

@ -0,0 +1,52 @@
import style from "./styles/style.scss"
import { App, Gtk } from "astal/gtk4"
import { GLib } from "astal";
import Hyprland from "gi://AstalHyprland";
import QuickSettings from "widget/quick_settings/quick_settings";
import BluetoothWindow from "./widget/quick_settings/bluetooth";
import Launcher from "widget/launcher/launcher";
import Bar from "widget/bar/bar";
const hypr = Hyprland.get_default();
const windows = new Map<number, Gtk.Window[]>();
const setupBars = async (monitor_id: number) => {
const components = [Bar, Launcher, QuickSettings, BluetoothWindow];
const windows = await Promise.all(
components.map(item => Promise.resolve(item(monitor_id)) as Promise<Gtk.Window>)
);
return windows;
};
App.start({
css: style,
async main() {
App.add_icons(`${GLib.get_user_data_dir()}/icons/Astal`);
const monitors = App.get_monitors();
for (const monitor of monitors) {
const index = monitors.indexOf(monitor);
windows.set(index, await setupBars(index));
hypr.connect("monitor-added", async (_, monitor: Hyprland.Monitor) => {
if (!windows.has(monitor.id)) windows.set(monitor.id, await setupBars(monitor.id))
});
hypr.connect("monitor-removed", (_, monitor_id: number) => {
const monitorWindows = windows.get(monitor_id)
if (monitorWindows) {
for (const monitorWindow of monitorWindows) {
monitorWindow.destroy();
};
windows.delete(monitor_id);
};
});
}
},
})

21
env.d.ts vendored Normal file
View File

@ -0,0 +1,21 @@
declare const SRC: string
declare module "inline:*" {
const content: string
export default content
}
declare module "*.scss" {
const content: string
export default content
}
declare module "*.blp" {
const content: string
export default content
}
declare module "*.css" {
const content: string
export default content
}

6
lib/icons.tsx Normal file
View File

@ -0,0 +1,6 @@
export default {
battery: {
charging: "battery-symbolic",
discharging: "battery-caution-symbolic"
}
};

16
lib/utils.ts Normal file
View File

@ -0,0 +1,16 @@
import { Gtk, Gdk } from "astal/gtk4";
export type If<Condition, Then, Else> = Condition extends true ? Then : Else;
export type Belongs<T, U> = {
[K in keyof T]: T[K] extends U ? K : never;
}[keyof T];
export const hideWindow = (self: Gtk.Window, keyval: number) => {
if (keyval === Gdk.KEY_Escape) self.hide();
}
export const openOnButton = (event: Gdk.ButtonEvent, keyval: number) => (action: () => void) => {
if (event.get_button() !== keyval) return;
action();
}

24
lib/widgets/datetime.tsx Normal file
View File

@ -0,0 +1,24 @@
import { Variable, GLib } from "astal";
import { Widget } from "astal/gtk4";
type Props = {
format: string,
interval?: number
} & Widget.LabelProps;
export default function DateTime({ format, interval, ...props }: Props) {
const shouldPoll = typeof interval === "number" && interval >= 1;
const currentTime = () => {
const dateTime = GLib.DateTime.new_now_local();
return dateTime.format(format)!;
}
if (shouldPoll) {
const pollTime = new Variable(currentTime()).poll(interval || 1000, currentTime);
return <label label={pollTime()} {...props} />
} else {
const time = new Variable(currentTime())
return <label label={time()} {...props} />
}
}

6
package.json Normal file
View File

@ -0,0 +1,6 @@
{
"name": "astal-shell",
"dependencies": {
"astal": "/nix/store/52w3zsyjrj7xrwcf3gxi3hv2y9lnc9pv-astal-gjs/share/astal/gjs"
}
}

5
settings.json Normal file
View File

@ -0,0 +1,5 @@
{
"SHELL": "fish",
"IDLE_INHIBIT_SCRIPT": "$HOME/.config/hypr/scripts/idle-inhibitor.py",
"RANDOM_WALLPAPER_SCRIPT": "$HOME/.config/hypr/scripts/random-wallpaper.sh"
}

6
styles/_variables.scss Normal file
View File

@ -0,0 +1,6 @@
$fg-color: #{"@theme_fg_color"};
$bg-color: #{"@theme_bg_color"};
$secondary-fg-color: #{"@insensitive_fg_color"};
$secondary-bg-color: #{"@insensitive_bg_color"};
$font-family: "SFProText Nerd Font", "SFProDisplay Nerd Font", sans-serif;
$font-family-monospace: "JetBrains Mono", monospace;

6
styles/classes.scss Normal file
View File

@ -0,0 +1,6 @@
@use '_variables' as *;
@use 'mixins';
.material-icon {
@include mixins.material-icon;
}

View File

@ -0,0 +1,30 @@
@use '../variables' as *;
window.bar {
color: $fg-color;
font-weight: bold;
>box {
border-radius: 10px;
}
>.inner {
border-bottom-left-radius: 0;
border-bottom-right-radius: 0;
border: 1px solid $fg-color;
background: $bg-color;
padding: 0.25rem 1rem;
margin: 0;
}
.tray .item {
padding: 0;
margin: 0;
background: $bg-color;
image {
padding: 0;
margin: 0;
}
}
}

View File

@ -0,0 +1,3 @@
@forward '_bar';
@forward '_launcher';
@forward '_quick_settings';

View File

@ -0,0 +1,53 @@
@use '../variables' as *;
.launcher {
entry {
background: $bg-color;
color: $fg-color;
padding: 8px 12px;
border: none;
margin-bottom: 6px;
&:focus {
outline: 2px solid $bg-color;
}
}
.application {
padding: 10px;
background-color: $bg-color;
border-radius: 8px;
transition: background-color 0.2s ease;
&:hover {
background-color: $bg-color;
}
image {
margin-right: 12px;
}
box {
.name {
color: $fg-color;
font-size: 14px;
font-weight: 600;
}
.description {
color: $fg-color;
font-size: 12px;
}
}
}
.not-found {
color: $fg-color;
font-size: 14px;
image {
margin-bottom: 8px;
}
}
}

View File

@ -0,0 +1,66 @@
@use '../variables' as *;
window.quick_settings {
color: $fg-color;
font-weight: bold;
>box {
border-radius: 10px;
margin: 8px;
}
>.inner {
border-bottom-left-radius: 0;
border-bottom-right-radius: 0;
background: $bg-color;
padding: 0.75rem 3rem;
margin: 0;
}
button {
padding: 0.75rem 1rem;
}
}
window.qs_bluetooth {
color: $fg-color;
>.inner {
background: $bg-color;
padding: 0.75rem 3rem;
border-radius: 10px;
}
.device {
padding: 1rem 3rem;
}
button {
padding: 0.75rem 0.5rem;
}
}
window.quick_settings button, window.qs_bluetooth button {
background: $bg-color;
border: 2px solid $fg-color;
margin: 5px;
transition: 400ms cubic-bezier(0.05, 0.7, 0.1, 1);
&:hover {
background: $fg-color;
* {
color: $bg-color;
}
}
&.active {
background: $fg-color;
border: 0.5px solid $bg-color;
* {
color: $bg-color;
}
}
}

8
styles/mixins.scss Normal file
View File

@ -0,0 +1,8 @@
@mixin full-rounding {
border-radius: 100%;
}
@mixin material-icon {
font-family: "Material Symbols Rounded", "MaterialSymbolsRounded", "Material Symbols Outlined",
"Material Symbols Sharp";
}

12
styles/style.scss Normal file
View File

@ -0,0 +1,12 @@
@use '_variables' as *;
@use 'mixins';
@use 'classes';
@use 'components';
* {
font-family: $font-family;
}
image {
padding: 0 0.25rem;
}

19
tsconfig.json Normal file
View File

@ -0,0 +1,19 @@
{
"$schema": "https://json.schemastore.org/tsconfig",
"compilerOptions": {
"experimentalDecorators": true,
"strict": true,
"target": "ES2022",
"module": "ES2022",
"moduleResolution": "Bundler",
"noEmit": true,
"baseUrl": ".",
"allowImportingTsExtensions": true,
"jsx": "react-jsx",
"jsxImportSource": "astal/gtk4",
"paths": {
"@/*": ["./*"],
"@lib/*": ["lib/*"]
}
}
}

81
widget/bar/bar.tsx Normal file
View File

@ -0,0 +1,81 @@
import { App, Astal, Gdk, Gtk } from "astal/gtk4"
import { bind, Binding } from "astal"
import PowerProfiles from "gi://AstalPowerProfiles";
import Battery from "gi://AstalBattery";
import DateTime from "@lib/widgets/datetime";
import Workspace from "./workspace";
import TrayView from "./tray";
type Substitution = { charging?: string, idle?: string }
const getBatteryIcon = (percentage: number, charging: Binding<boolean>) => {
const levels = Array.from({ length: 10 }, (_, i) => (i + 1) * 10);
const level = levels.find((level) => percentage <= level)!;
const substitutions: Record<number, Substitution> = {
100: { charging: "battery-level-100-charged-symbolic" }
};
return charging.as(c => c
? substitutions[level]?.charging || `battery-level-${level}-charging-symbolic`
: substitutions[level]?.idle || `battery-level-${level}-symbolic`
);
};
const BatteryInfo = ({ battery }: { battery: Battery.Device }) => {
const percentage = bind(battery, "percentage").as(p => Math.floor(p * 100));
const charging = bind(battery, "charging");
const power = PowerProfiles.get_default();
return (
<box>
{percentage.as(p => <button
hasFrame={false}
tooltipText={bind(power, "active_profile")}
>
<box>
<label>{percentage.as(p => `${p}%`)}</label>
<image iconName={getBatteryIcon(p, charging)} />
</box>
</button>)}
</box>
);
};
export default function Bar(monitor_id: number) {
const { BOTTOM, LEFT, RIGHT } = Astal.WindowAnchor
const CENTER = Gtk.Align.CENTER;
const battery = Battery.get_default();
const openQuickSettings = (_self: Gtk.Button, state: Gdk.ButtonEvent) => {
if (state.get_button() === Gdk.BUTTON_PRIMARY)
App.get_window("quick_settings")?.show()
}
return <window
visible
name={`bar_${monitor_id}`}
monitor={monitor_id}
cssClasses={["bar"]}
exclusivity={Astal.Exclusivity.EXCLUSIVE}
anchor={BOTTOM | LEFT | RIGHT}
application={App}>
<box halign={CENTER} cssClasses={["inner"]}>
{[
Workspace(),
<menubutton hasFrame={false}>
<DateTime format="%d.%m.%Y %H:%M:%S" interval={1000} />
<popover>
<Gtk.Calendar />
</popover>
</menubutton>,
BatteryInfo({ battery }),
<button hasFrame={false} iconName={"applications-system-symbolic"} onButtonPressed={openQuickSettings} />,
TrayView()
]}
</box>
</window>
}

View File

@ -0,0 +1,43 @@
import { bind, Variable } from "astal";
import Network from "gi://AstalNetwork";
export default function NetworkButton() {
const network = Network.get_default();
const state = Variable.derive(
[bind(network, "primary"), bind(network, "wifi"), bind(network, "wired")],
(primary, wifi, wired) => {
switch (primary) {
case Network.Primary.WIFI:
return {
icon: wifi.iconName,
label: wifi.ssid,
tooltip: `Connected to ${wifi.ssid}`,
};
case Network.Primary.WIRED:
return {
icon: wired.iconName,
label: "Wired",
tooltip: {
[Network.Internet.CONNECTED]: `Connected to Ethernet`,
[Network.Internet.CONNECTING]: `Connecting to Ethernet...`,
[Network.Internet.DISCONNECTED]: `Connected to Ethernet (no internet!)`,
}[wired.internet],
};
default:
return {
icon: "network-disconnected-symbolic",
label: "Disconnected",
tooltip: "No connection",
};
}
}
)
const binding = bind(state);
return <box tooltipText={binding.as((t) => t.tooltip)} spacing={5}>
<image iconName={binding.as((i) => i.icon)} />
<label label={binding.as((l) => l.label)} />
</box>
}

30
widget/bar/tray.tsx Normal file
View File

@ -0,0 +1,30 @@
import { hook } from "astal/gtk4";
import { bind } from "astal";
import Tray from "gi://AstalTray";
export default function TrayView() {
const tray = Tray.get_default();
const items = bind(tray, "items");
return <box spacing={items.as(items => items.length)} cssClasses={["tray"]}>
{items.as(items => items.map(item => {
const button = <menubutton
cssClasses={["item"]}
tooltipMarkup={bind(item, "tooltipMarkup")}
menuModel={bind(item, "menuModel")}
hasFrame={false}
primary
setup={self => self.insert_action_group("dbusmenu", item.actionGroup)}
>
<image gicon={item.gicon} />
</menubutton>;
hook(button, item, "notify::action-group", self => {
self.insert_action_group("dbusmenu", item.actionGroup);
});
return button;
}))}
</box>
}

18
widget/bar/workspace.tsx Normal file
View File

@ -0,0 +1,18 @@
import Hyprland from "gi://AstalHyprland";
import { bind, Variable } from "astal";
export default function Workspace() {
const hypr = Hyprland.get_default();
const lastWorkspaceId = Variable(hypr.focusedWorkspace.id);
return <button hasFrame={false}>
{bind(hypr, "focusedWorkspace").as(workspace => {
// This is a hack; sometimes when disconnecting monitor
// focusedWorkspace is null for a while.
// Therefore an error occurs when trying to reference id.
if (workspace) lastWorkspaceId.set(workspace.id)
return `workspace ${workspace?.id ?? lastWorkspaceId.get()}`
})}
</button>
}

View File

@ -0,0 +1,82 @@
import { Variable, bind } from "astal";
import { App, Astal, Gtk } from "astal/gtk4";
import { hideWindow } from "@lib/utils";
import Hyprland from "gi://AstalHyprland";
import Apps from "gi://AstalApps";
const hide = () => App.get_window("launcher")?.hide();
function AppButton({ app }: { app: Apps.Application }) {
return <button
cssClasses={["application"]}
onClicked={() => {
hide();
app.launch();
}}
>
<box>
<image iconName={app.iconName} />
<box valign={Gtk.Align.CENTER} vertical>
<label
cssClasses={["name"]}
xalign={0}
label={app.name}
/>
{app.description && <label
cssClasses={["description"]}
wrap
xalign={0}
label={app.description}
/>}
</box>
</box>
</button>
}
export default function Launcher(_monitor_id: number) {
const { TOP, BOTTOM, LEFT, RIGHT } = Astal.WindowAnchor;
const hypr = Hyprland.get_default();
const apps = new Apps.Apps();
const query = Variable("")
const list = query(text => apps.fuzzy_query(text).slice(0, 10))
const onEnter = () => {
hide()
list.get().at(0)!.launch()
}
return <window
visible={false}
monitor={bind(hypr, "focusedMonitor").as(monitor => monitor.id)}
name={"launcher"}
cssClasses={["launcher"]}
keymode={Astal.Keymode.EXCLUSIVE}
anchor={TOP | BOTTOM | LEFT | RIGHT}
onKeyPressed={hideWindow}
application={App}>
<box hexpand={false} vertical halign={Gtk.Align.CENTER} valign={Gtk.Align.CENTER}>
<box cssClasses={["launcher"]} vertical>
<entry
placeholderText="Search"
onChanged={self => query.set(self.text)}
onActivate={onEnter}
/>
<box spacing={6} vertical>
{list.as(list => list.map(app => (
<AppButton app={app} />
)))}
</box>
<box
halign={Gtk.Align.CENTER}
cssClasses={["not-found"]}
vertical
visible={list.as(l => l.length === 0)}>
<image iconName="system-search-symbolic" />
<label label="No match found" />
</box>
</box>
</box>
</window>
}

View File

@ -0,0 +1,73 @@
import { App, Astal, Gtk } from "astal/gtk4";
import { bind, execAsync, Gio } from "astal";
import { hideWindow } from "@/lib/utils";
import Bluetooth from "gi://AstalBluetooth";
import Hyprland from "gi://AstalHyprland";
function DeviceMenu({ device, child }: { device: Bluetooth.Device, child?: JSX.Element }) {
const menu = Gio.Menu.new();
const copy_mac = Gio.MenuItem.new("Copy MAC address", "bt.copy_mac");
const toggle_state = Gio.MenuItem.new(device.get_connected() ? "Disconnect" : "Connect", "bt.toggle_state");
const action_group = Gio.SimpleActionGroup.new();
const copy_mac_action = Gio.SimpleAction.new("copy_mac", null);
copy_mac_action.connect("activate", () => execAsync(["wl-copy", device.address]));
const toggle_state_action = Gio.SimpleAction.new("toggle_state", null);
toggle_state_action.connect("activate", async () => {
if (device.connected)
await device.disconnect_device()
else
await device.connect_device()
})
menu.insert_item(1, copy_mac);
menu.insert_item(2, toggle_state);
action_group.insert(copy_mac_action);
action_group.insert(toggle_state_action);
return <menubutton
menuModel={menu}
setup={self => self.insert_action_group("bt", action_group)}
>
{child}
<popover>
<label label={"Copy MAC address"} />
<label label={"Toggle state"} />
</popover>
</menubutton>;
};
export default function BluetoothWindow(_monitor_id: number) {
const CENTER = Gtk.Align.CENTER;
const bt = Bluetooth.get_default();
const hypr = Hyprland.get_default();
const devices = bind(bt, "devices");
const connectedDevice = devices.as(devices => devices?.find(device => device.connected));
return <window
name={"qs_bluetooth"}
cssClasses={["qs_bluetooth"]}
monitor={bind(hypr, "focusedMonitor").as(monitor => monitor.id)}
exclusivity={Astal.Exclusivity.IGNORE}
keymode={Astal.Keymode.EXCLUSIVE}
application={App}
onKeyPressed={hideWindow}
>
<box vertical halign={CENTER} cssClasses={["inner"]} spacing={5}>
{devices.as(devices => devices.map(device =>
<DeviceMenu device={device}>
<box cssClasses={["device", "small-padding"].concat(device.connected ? ["active"] : [])} spacing={3}>
<image iconName={device.icon} />
<label>{device.alias}</label>
</box>
</DeviceMenu>
))}
</box>
</window>
}

View File

@ -0,0 +1,167 @@
import { bind, execAsync, Binding, Variable } from "astal";
import { App, Astal, Gdk, Gtk } from "astal/gtk4";
import Bluetooth from "gi://AstalBluetooth";
import Hyprland from "gi://AstalHyprland";
import Network from "gi://AstalNetwork";
import { SHELL, IDLE_INHIBIT_SCRIPT, RANDOM_WALLPAPER_SCRIPT } from "@/settings.json";
import { hideWindow, openOnButton } from "@lib/utils";
type ButtonProps = {
icon: string | Binding<string>,
command?: string,
label: string | Binding<string>,
binding?: Binding<boolean>,
bindingMethod?: (binding: boolean) => void
}
const isIdleInhibitorEnabled = async () => {
try {
await execAsync([SHELL, "-c", `pgrep -f ${IDLE_INHIBIT_SCRIPT}`]);
return true;
} catch {
return false;
}
};
function Preference(props: ButtonProps & JSX.IntrinsicElements["button"]) {
const onClicked = () => {
if (props.command) execAsync([ SHELL, "-c", props.command ]);
else if (typeof props.binding !== 'undefined' && props.bindingMethod)
props.bindingMethod(!props.binding.get())
}
return <button
{...props}
label={undefined}
onClicked={onClicked}
cssClasses={props.binding?.as(active => active ? ["active"] : [])}
>
<box orientation={Gtk.Orientation.HORIZONTAL} spacing={4}>
<image iconName={props.icon} />
<label>{props.label}</label>
</box>
</button>
}
async function toggleIdleInhibitor(state: Variable<boolean>, enable: boolean) {
try {
if (enable) {
execAsync([SHELL, "-c", IDLE_INHIBIT_SCRIPT]);
} else {
const pids = await execAsync([SHELL, "-c", `pgrep -f ${IDLE_INHIBIT_SCRIPT}`]);
if (pids) {
const pidList = pids.trim().split("\n");
for (const pid of pidList) {
await execAsync(["kill", "-9", pid]);
}
}
}
state.set(!state.get());
} catch {
console.error("Failed to change state of idle inhibitor");
}
}
export default async function QuickSettings(_monitor_id: number) {
const { BOTTOM, LEFT, RIGHT } = Astal.WindowAnchor
const CENTER = Gtk.Align.CENTER;
const hypr = Hyprland.get_default();
const bt = Bluetooth.get_default();
const net = Network.get_default();
const connectedBtDevice = bind(bt, "devices").as(devices => devices.find(device => device.connected));
const idleInhibitorEnabled = Variable(await isIdleInhibitorEnabled());
return <window
visible={false}
name={"quick_settings"}
monitor={bind(hypr, "focusedMonitor").as(monitor => monitor.id)}
cssClasses={["quick_settings"]}
keymode={Astal.Keymode.EXCLUSIVE}
exclusivity={Astal.Exclusivity.IGNORE}
anchor={BOTTOM | LEFT | RIGHT}
onKeyPressed={hideWindow}
application={App}>
<box halign={CENTER} cssClasses={["inner"]} vertical>
<box orientation={Gtk.Orientation.HORIZONTAL} halign={CENTER}>
<Preference
icon={"network-wireless-symbolic"}
label={bind(Variable.derive([
bind(net.wifi, "enabled"),
bind(net.wifi, "ssid")
], (enabled, ssid) => enabled && ssid ? ssid : "Wi-Fi"))}
binding={bind(net.wifi, "enabled")}
bindingMethod={enabled => net.wifi.set_enabled(enabled)}
/>
<Preference
icon={bind(bt, "isConnected").as(c => c
? "bluetooth-active-symbolic"
: "bluetooth-disabled-symbolic"
)}
label={connectedBtDevice.as(device => bt.isPowered && device ? device.alias : "Bluetooth")}
binding={bind(bt, "isPowered")}
bindingMethod={() => bt.toggle()}
onButtonPressed={(_self, event) =>
openOnButton(event, Gdk.BUTTON_SECONDARY)
(() => App.toggle_window("qs_bluetooth"))
}
/>
<Preference
icon={"uninterruptible-power-supply-symbolic"}
label={"Idle inhibitor"}
binding={bind(idleInhibitorEnabled)}
bindingMethod={toggleIdleInhibitor.bind(null, idleInhibitorEnabled)}
/>
<Preference
icon="preferences-desktop-wallpaper-symbolic"
command={RANDOM_WALLPAPER_SCRIPT}
label="Random wallpaper"
/>
</box>
<box orientation={Gtk.Orientation.HORIZONTAL} halign={CENTER}>
<Preference
icon="system-shutdown-symbolic"
command="systemctl poweroff"
label="Shutdown"
/>
<Preference
icon="system-reboot-symbolic"
command="systemctl reboot"
label="Reboot"
/>
<Preference
icon="preferences-system-privacy-symbolic"
command="systemctl suspend"
label="Suspend"
/>
<Preference
icon="application-exit-symbolic"
command="systemctl exit"
label="Exit session"
/>
</box>
<box orientation={Gtk.Orientation.HORIZONTAL} halign={CENTER}>
<Preference
icon="system-lock-screen-symbolic"
command="loginctl lock-session"
label="Lock Screen"
/>
<Preference
icon="system-log-out-symbolic"
command="loginctl terminate-user $(whoami)"
label="Log out"
/>
<Preference
icon="system-reboot-symbolic"
command="systemctl reboot --boot-loader-entry=menu"
label="Reboot to bootloader"
/>
</box>
</box>
</window>
}