feat: generalize workspace indicator for Hyprland + Sway and clean

This commit is contained in:
Nydragon 2024-09-15 00:54:47 +02:00
parent b0ceb423d9
commit 4480985672
Signed by: nydragon
SSH key fingerprint: SHA256:iQnIC12spf4QjWSbarmkD2No1cLMlu6TWoV7K6cYF5g
4 changed files with 67 additions and 28 deletions

34
src/provider/Sway.qml Normal file
View file

@ -0,0 +1,34 @@
pragma Singleton
import Quickshell
import QtQuick
import Quickshell.Io
Singleton {
id: root
property int activeWorkspace: 1
property var dispatch: m => {
sender.command = ["swaymsg", ...m];
sender.running = true;
}
Process {
id: sender
command: []
}
Process {
command: ["swaymsg", "-mtsubscribe", "[\"workspace\"]"]
running: true
stdout: SplitParser {
onRead: data => {
const parsed = JSON.parse(data);
if (parsed.change == "focus") {
root.activeWorkspace = parsed.current.num;
}
}
}
}
}

View file

@ -6,11 +6,10 @@ import QtQuick.Layouts
Rectangle { Rectangle {
id: root id: root
required property bool focused
required property int wnum required property int wnum
property bool hovered: false property bool hovered: false
property bool active: focused || hovered property bool active: workspaces.active === wnum || hovered
property int activeMargin: 3 property int activeMargin: 3
property int inactiveMargin: 5 property int inactiveMargin: 5
@ -29,20 +28,14 @@ Rectangle {
onEntered: () => { onEntered: () => {
root.hovered = true; root.hovered = true;
} }
onExited: () => { onExited: () => {
root.hovered = false; root.hovered = false;
} }
onClicked: () => { onClicked: () => {
switcher.running = true; workspaces.switchWorkspace(wnum);
} }
} }
Process {
id: switcher
command: ["swaymsg", "workspace", `${root.wnum}`]
}
states: State { states: State {
name: "focused" name: "focused"
when: root.active when: root.active

View file

@ -1,12 +1,28 @@
import QtQuick import QtQuick
import Quickshell.Io import Quickshell.Io
import Quickshell.Hyprland
import "root:provider"
Item { Item {
id: root id: root
property int active: 1 // currently active workspace property int active: 1 // currently active workspace
property int amount: 10 // amount of workspaces property int amount: 10 // amount of workspaces
property string name: "" // name of the current desktop property string name: "unknown" // name of the current desktop
property var switchWorkspace: w => {
console.log(`We are switching from workspace ${active} to ${w}`);
switch (root.name) {
case "sway":
Sway.dispatch(["workspace", w]);
break;
case "Hyprland":
Hyprland.dispatch(`workspace ${w}`);
break;
default:
console.log("unhandled");
}
}
Process { Process {
command: ["env"] command: ["env"]
@ -14,23 +30,20 @@ Item {
stdout: SplitParser { stdout: SplitParser {
onRead: data => { onRead: data => {
if (data.startsWith("XDG_CURRENT_DESKTOP=")) if (data.startsWith("XDG_CURRENT_DESKTOP=")) {
root.name = data.slice(20); root.name = data.slice(20);
switch (root.name) {
case "sway":
root.active = Qt.binding(() => Sway.activeWorkspace ?? root.active);
break;
case "Hyprland":
root.active = Qt.binding(() => Hyprland.focusedMonitor?.activeWorkspace?.id ?? root.active);
break;
default:
console.log("This desktop is unhandled:", root.name);
} }
} }
} }
Process {
command: ["swaymsg", "-mtsubscribe", "[\"workspace\"]"]
running: root.name === "sway"
stdout: SplitParser {
onRead: data => {
const parsed = JSON.parse(data);
if (parsed.change == "focus") {
root.active = parsed.current.num;
}
}
} }
} }
} }

View file

@ -6,10 +6,10 @@ import "../../base"
BRectangle { BRectangle {
id: root id: root
height: 100 + layout.spacing * (workspace.amount - 1) height: 100 + layout.spacing * (workspaces.amount - 1)
WorkspaceIPC { WorkspaceIPC {
id: workspace id: workspaces
} }
ColumnLayout { ColumnLayout {
@ -21,12 +21,11 @@ BRectangle {
spacing: 1 spacing: 1
Repeater { Repeater {
model: workspace.amount model: workspaces.amount
WorkspaceElem { WorkspaceElem {
required property int modelData required property int modelData
wnum: modelData + 1 wnum: modelData + 1
focused: workspace.active === (modelData + 1)
} }
} }
} }