This repository has been archived on 2026-11-02. You can view files and clone it. You cannot open issues or pull requests or push a commit.
Files
dotfiles/mutable_config/quickshell/components/CommandPalette/CommandPalette.qml
T
2026-09-17 19:56:15 +02:00

350 lines
14 KiB
QML

import QtQuick
import Quickshell
import Quickshell.Wayland
import Quickshell.Services.Pipewire
import Quickshell.Io
import "../Shared"
PanelWindow {
id: paletteWindow
property QtObject rootState
property bool enterQueued: false
property var usageCounts: ({})
property string lastUsedApp: ""
function launchApp(index) {
if (index < 0 || index >= appModel.count) return;
let item = appModel.get(index);
let desktopName = item.desktopName;
let appPath = item.appPath;
logHistoryProcess.command = ["bash", "-c", "echo '" + desktopName + "' >> ~/.cache/quickshell-app-history.txt"];
logHistoryProcess.restart();
launchProcess.command = [
"bash", "-c",
"EXEC=$(awk -F= '/^Exec=/ {print $2; exit}' \"$1\" | sed 's/ %[a-zA-Z]//g'); if command -v uwsm &>/dev/null; then eval \"uwsm app -- $EXEC\"; elif command -v niri &>/dev/null; then eval \"niri msg action spawn -- $EXEC\"; else nohup eval \"$EXEC\" </dev/null >/dev/null 2>&1 & fi",
"_", appPath
];
launchProcess.restart();
rootState.commandPaletteOpen = false;
}
visible: rootState.commandPaletteOpen
anchors {
top: true
bottom: true
left: true
right: true
}
exclusiveZone: 0
color: "transparent"
WlrLayershell.layer: WlrLayer.Overlay
WlrLayershell.namespace: "quickshell-palette"
WlrLayershell.keyboardFocus: WlrKeyboardFocus.Exclusive
MouseArea {
anchors.fill: parent
onClicked: {
rootState.commandPaletteOpen = false
rootState.commandPaletteOsdMode = false
}
}
Rectangle {
id: container
// Sized appropriately for a 2560x1440 screen
width: 800
height: Math.min(600, (appList.count > 0 ? appList.contentHeight : 0) + (searchInput.text.length > 0 ? 50 + Theme.geometry.spacing : 0) + Theme.geometry.padding * 2)
anchors.centerIn: parent
radius: Theme.geometry.radius
color: Theme.colors.background
border.color: Theme.colors.border
border.width: 1
Behavior on scale { SpringAnimation { spring: 3; damping: 0.2 } }
Behavior on opacity { NumberAnimation { duration: Theme.animation.durationFast } }
Behavior on height { NumberAnimation { duration: Theme.animation.durationFast; easing.type: Easing.OutCubic } }
opacity: rootState.commandPaletteOpen ? 1 : 0
MouseArea {
anchors.fill: parent
}
// --- App Launcher ---
Column {
anchors.fill: parent
anchors.margins: Theme.geometry.padding
spacing: searchInput.text.length > 0 ? Theme.geometry.spacing : 0
Rectangle {
width: parent.width
height: searchInput.text.length > 0 ? 50 : 0
opacity: searchInput.text.length > 0 ? 1 : 0
clip: true
color: Theme.colors.surface
radius: Theme.geometry.radius
Behavior on height { NumberAnimation { duration: Theme.animation.durationFast } }
Behavior on opacity { NumberAnimation { duration: Theme.animation.durationFast } }
TextInput {
id: searchInput
anchors.fill: parent
anchors.margins: Theme.geometry.padding
verticalAlignment: TextInput.AlignVCenter
font.family: Theme.font.family
font.pixelSize: 18
color: Theme.colors.text
clip: true
Text {
text: "Search apps..."
color: Theme.colors.textMuted
font.family: Theme.font.family
font.pixelSize: 18
anchors.verticalCenter: parent.verticalCenter
visible: searchInput.text.length === 0
}
onVisibleChanged: {
if (visible) forceActiveFocus();
}
onTextChanged: {
searchTimer.restart();
}
Keys.onEscapePressed: (event) => {
rootState.commandPaletteOpen = false;
event.accepted = true;
}
Keys.onUpPressed: (event) => {
if (appList.currentIndex > 0) {
appList.currentIndex--;
appList.positionViewAtIndex(appList.currentIndex, ListView.Contain);
}
event.accepted = true;
}
Keys.onDownPressed: (event) => {
if (appList.currentIndex < appList.count - 1) {
appList.currentIndex++;
appList.positionViewAtIndex(appList.currentIndex, ListView.Contain);
}
event.accepted = true;
}
Keys.onReturnPressed: (event) => {
if (appList.count > 0 && appList.currentIndex >= 0 && appList.currentIndex < appModel.count) {
paletteWindow.launchApp(appList.currentIndex);
} else {
paletteWindow.enterQueued = true;
}
event.accepted = true;
}
}
}
Timer {
id: searchTimer
interval: 200
onTriggered: searchProcess.restart()
}
Process {
id: historyProcess
command: ["bash", "-c", "cat ~/.cache/quickshell-app-history.txt 2>/dev/null || true"]
running: false
function restart() {
running = false;
running = true;
}
stdout: StdioCollector {
onStreamFinished: {
let lines = this.text.split('\n').filter(l => l.trim() !== "");
let counts = {};
let lastUsed = "";
for (let i = 0; i < lines.length; i++) {
let app = lines[i];
counts[app] = (counts[app] || 0) + 1;
lastUsed = app;
}
paletteWindow.usageCounts = counts;
paletteWindow.lastUsedApp = lastUsed;
// Now that history is loaded, run the search
searchProcess.restart();
}
}
}
Process {
id: searchProcess
command: [
"bash",
"-c",
"fd -L -t f -e desktop -0 . /usr/share/applications ~/.local/share/applications /var/lib/flatpak/exports/share/applications /run/current-system/sw/share/applications ~/.nix-profile/share/applications 2>/dev/null | xargs -0 -r awk -F= 'BEGIN { OFS=\"|\" } FNR==1 { if(file!=\"\" && name!=\"\") print icon, name, file; name=\"\"; icon=\"\"; file=FILENAME } /^Name=/ && name==\"\" { name=substr($0, 6) } /^Icon=/ && icon==\"\" { icon=substr($0, 6) } END { if(file!=\"\" && name!=\"\") print icon, name, file }' | { [ -z \"$1\" ] && cat || rg -i \"$1\"; } | head -n 20",
"_",
searchInput.text
]
running: false
function restart() {
running = false;
running = true;
}
stdout: StdioCollector {
onStreamFinished: {
let lines = this.text.split('\n').filter(l => l.trim() !== "");
let results = [];
for (let i = 0; i < lines.length; i++) {
let parts = lines[i].split('|');
if (parts.length >= 3) {
let desktopName = parts[2].split('/').pop();
// Calculate frecency score
let count = paletteWindow.usageCounts[desktopName] || 0;
let isLast = (desktopName === paletteWindow.lastUsedApp);
// Give a massive boost to the absolute last used app, then weight by total usage
let score = count * 10 + (isLast ? 10000 : 0);
results.push({
"iconName": parts[0],
"appName": parts[1],
"appPath": parts[2],
"desktopName": desktopName,
"score": score
});
}
}
// Sort by score (descending), then alphabetically
results.sort((a, b) => {
if (b.score !== a.score) return b.score - a.score;
return a.appName.localeCompare(b.appName);
});
appModel.clear();
for (let i = 0; i < results.length; i++) {
appModel.append(results[i]);
}
appList.currentIndex = 0;
if (paletteWindow.enterQueued && appModel.count > 0) {
paletteWindow.enterQueued = false;
paletteWindow.launchApp(0);
}
}
}
}
ListView {
id: appList
width: parent.width
height: parent.height - (searchInput.text.length > 0 ? 50 + Theme.geometry.spacing : 0)
clip: true
spacing: Theme.geometry.spacing
model: ListModel { id: appModel }
delegate: Rectangle {
width: appList.width
height: 36
color: ListView.isCurrentItem ? Theme.colors.surfaceHover : "transparent"
radius: Theme.geometry.radius
Item {
anchors.fill: parent
anchors.leftMargin: Theme.geometry.padding
anchors.rightMargin: Theme.geometry.padding
Image {
id: appIcon
width: 24
height: 24
source: model.iconName ? Quickshell.iconPath(model.iconName) : ""
anchors.left: parent.left
anchors.verticalCenter: parent.verticalCenter
fillMode: Image.PreserveAspectFit
Rectangle {
anchors.fill: parent
color: Theme.colors.primary
radius: 12
visible: parent.status === Image.Error || parent.status === Image.Null || model.iconName === ""
Text {
anchors.centerIn: parent
text: model.appName.charAt(0).toUpperCase()
color: Theme.colors.background
font.bold: true
font.pixelSize: 12
}
}
}
Text {
text: model.appName
color: Theme.colors.text
font.family: Theme.font.family
font.pixelSize: 14
anchors.left: appIcon.right
anchors.leftMargin: Theme.geometry.spacing
anchors.verticalCenter: parent.verticalCenter
}
}
MouseArea {
anchors.fill: parent
hoverEnabled: true
onEntered: appList.currentIndex = index
onClicked: {
appList.currentIndex = index;
paletteWindow.launchApp(index);
}
}
}
}
}
Process {
id: launchProcess
running: false
function restart() {
running = false;
running = true;
}
}
Process {
id: logHistoryProcess
running: false
function restart() {
running = false;
running = true;
}
}
}
onVisibleChanged: {
if (visible) {
container.scale = 1.0;
searchInput.text = "";
paletteWindow.enterQueued = false;
appModel.clear(); // Clear so it re-populates clean
// Read history first, then search
historyProcess.restart();
} else {
container.scale = 0.95;
}
}
}