diff --git a/style.scss b/style.scss index 01ed3d4..36a6ca5 100644 --- a/style.scss +++ b/style.scss @@ -55,13 +55,19 @@ window.Bar { background-color: transparent; transition: all 0.3s cubic-bezier(0.25, 0.8, 0.25, 1); - &.active { + &.active-focused-monitor { min-width: 45px; // Horizontal expansion, slightly less wide background-color: wal.$color2; color: wal.$background; } - &:hover:not(.active) { + &.active-unfocused-monitor { + min-width: 45px; + background-color: rgba(wal.$color4, 0.35); + color: wal.$foreground; + } + + &:hover:not(.active-focused-monitor):not(.active-unfocused-monitor) { background-color: rgba(wal.$color2, 0.2); } } diff --git a/tsconfig.json b/tsconfig.json index 2bb1f01..3eb304c 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -1,14 +1,14 @@ { "$schema": "https://json.schemastore.org/tsconfig", "compilerOptions": { - "strict": true, - "module": "ES2022", - "target": "ES2020", - "lib": ["ES2023"], - "moduleResolution": "Bundler", - // "checkJs": true, - // "allowJs": true, "jsx": "react-jsx", - "jsxImportSource": "ags/gtk4" + "jsxImportSource": "ags/gtk4", + "lib": [ + "ES2023" + ], + "module": "ES2022", + "moduleResolution": "Bundler", + "strict": true, + "target": "ES2020" } -} +} \ No newline at end of file diff --git a/widget/Bar.tsx b/widget/Bar.tsx index e6b495a..0f402a6 100644 --- a/widget/Bar.tsx +++ b/widget/Bar.tsx @@ -6,10 +6,10 @@ import Media from "./Media" import Controls from "./Controls" import ThemeSwitcher from "./ThemeSwitcher" -function Left() { +function Left({ gdkmonitor }: { gdkmonitor: Gdk.Monitor }) { return ( - + ) } @@ -58,7 +58,7 @@ export default function Bar(gdkmonitor: Gdk.Monitor) { application={app} > - +
diff --git a/widget/Workspaces.tsx b/widget/Workspaces.tsx index 781ebca..fd39935 100644 --- a/widget/Workspaces.tsx +++ b/widget/Workspaces.tsx @@ -1,36 +1,115 @@ import { execAsync, createSubprocess } from "ags/process" import GLib from "gi://GLib?version=2.0" +import { Gdk } from "ags/gtk4" const HIS = GLib.getenv("HYPRLAND_INSTANCE_SIGNATURE") const sock = `${GLib.getenv("XDG_RUNTIME_DIR")}/hypr/${HIS}/.socket2.sock` -export default function Workspaces() { - // Listen to the Hyprland socket for instant workspace updates - const activeWs = createSubprocess( - 1, - [ - "sh", - "-c", - `hyprctl activeworkspace -j | grep '"id":' | head -1 | awk '{print $2}' | tr -d ','; nc -U ${sock}`, - ], - (out, prev) => { - const match = out.match(/(?:^|\n)workspace>>(\d+)/) - if (match) return parseInt(match[1]) +type WorkspaceState = { + focusedMonitor: string + activeByMonitor: Record +} - const num = parseInt(out) - if (!isNaN(num) && num > 0) return num +const INITIAL_STATE: WorkspaceState = { + focusedMonitor: "", + activeByMonitor: {}, +} - return prev - }, +function parseMonitors(json: string): WorkspaceState { + try { + const monitors = JSON.parse(json) as Array<{ + name?: string + focused?: boolean + activeWorkspace?: { id?: number } + }> + + const activeByMonitor: Record = {} + let focusedMonitor = "" + + for (const mon of monitors) { + const name = mon?.name ?? "" + const ws = Number(mon?.activeWorkspace?.id ?? 0) + + if (name && ws > 0) activeByMonitor[name] = ws + if (mon?.focused && name) focusedMonitor = name + } + + if (!focusedMonitor && monitors[0]?.name) focusedMonitor = monitors[0].name + + return { focusedMonitor, activeByMonitor } + } catch { + return INITIAL_STATE + } +} + +function updateState(out: string, prev: WorkspaceState): WorkspaceState { + let state: WorkspaceState = { + focusedMonitor: prev.focusedMonitor, + activeByMonitor: { ...prev.activeByMonitor }, + } + + // Initial/refresh snapshots injected by shell command + const monmapMatches = out.match(/__MONMAP__>>(.*)/g) + if (monmapMatches?.length) { + const latest = monmapMatches[monmapMatches.length - 1] + const payload = latest.replace("__MONMAP__>>", "") + const parsed = parseMonitors(payload) + if (Object.keys(parsed.activeByMonitor).length > 0) state = parsed + } + + // Hyprland event: focusedmon>>MONITOR,WORKSPACE_ID + const focusedMonMatches = Array.from( + out.matchAll(/(?:^|\n)focusedmon>>([^,\n]+),(\d+)/g), + ) + for (const m of focusedMonMatches) { + const mon = m[1] + const ws = parseInt(m[2]) + if (mon && !Number.isNaN(ws) && ws > 0) { + state.focusedMonitor = mon + state.activeByMonitor[mon] = ws + } + } + + // Hyprland event: workspace>>WORKSPACE_ID + // Applies to currently focused monitor. + const workspaceMatches = Array.from(out.matchAll(/(?:^|\n)workspace>>(\d+)/g)) + for (const m of workspaceMatches) { + const ws = parseInt(m[1]) + if (!Number.isNaN(ws) && ws > 0 && state.focusedMonitor) { + state.activeByMonitor[state.focusedMonitor] = ws + } + } + + return state +} + +export default function Workspaces({ + gdkmonitor, +}: { + gdkmonitor: Gdk.Monitor +}) { + const monitorName = gdkmonitor.get_connector() + + const wsState = createSubprocess( + INITIAL_STATE, + ["sh", "-c", `echo "__MONMAP__>>$(hyprctl monitors -j)"; nc -U ${sock}`], + updateState, ) return ( {Array.from({ length: 9 }, (_, i) => i + 1).map((i) => (