204 lines
6.6 KiB
QML
204 lines
6.6 KiB
QML
import QtQuick
|
|
import Quickshell
|
|
import Quickshell.Wayland
|
|
import Quickshell.Io
|
|
import "../Shared"
|
|
|
|
PanelWindow {
|
|
id: pickerWindow
|
|
property QtObject rootState
|
|
property var wallpapers: []
|
|
property int currentIndex: 0
|
|
property string currentWallpaperPath: ""
|
|
|
|
readonly property string scriptPath: Quickshell.env("HOME") + "/.dotfiles/config/nushell/scripts/wallpaper-switch.nu"
|
|
|
|
visible: rootState.wallpaperPickerOpen
|
|
|
|
anchors {
|
|
top: true
|
|
bottom: true
|
|
left: true
|
|
right: true
|
|
}
|
|
|
|
exclusiveZone: 0
|
|
color: "transparent"
|
|
|
|
WlrLayershell.layer: WlrLayer.Overlay
|
|
WlrLayershell.namespace: "quickshell-wallpaper-picker"
|
|
WlrLayershell.keyboardFocus: WlrKeyboardFocus.Exclusive
|
|
|
|
Process {
|
|
id: listProcess
|
|
command: ["nu", pickerWindow.scriptPath, "--list-with-current"]
|
|
running: false
|
|
stdout: StdioCollector {
|
|
onStreamFinished: {
|
|
try {
|
|
let data = JSON.parse(this.text);
|
|
pickerWindow.wallpapers = data.wallpapers ?? [];
|
|
pickerWindow.currentWallpaperPath = data.current ?? "";
|
|
} catch (e) {
|
|
pickerWindow.wallpapers = [];
|
|
pickerWindow.currentWallpaperPath = "";
|
|
}
|
|
pickerWindow.selectInitialIndex();
|
|
}
|
|
}
|
|
}
|
|
|
|
Process {
|
|
id: applyProcess
|
|
running: false
|
|
}
|
|
|
|
function wrappedIndex(i) {
|
|
let len = wallpapers.length;
|
|
return len > 0 ? ((i % len) + len) % len : 0;
|
|
}
|
|
|
|
function selectInitialIndex() {
|
|
if (wallpapers.length === 0) return;
|
|
let idx = currentWallpaperPath.length > 0
|
|
? wallpapers.findIndex(w => w.path === currentWallpaperPath)
|
|
: -1;
|
|
currentIndex = idx >= 0 ? idx : 0;
|
|
}
|
|
|
|
function applySelection() {
|
|
if (wallpapers.length === 0) return;
|
|
applyProcess.command = ["nu", pickerWindow.scriptPath, "--set", wallpapers[currentIndex].path];
|
|
applyProcess.running = true;
|
|
rootState.wallpaperPickerOpen = false;
|
|
}
|
|
|
|
onVisibleChanged: {
|
|
if (visible) {
|
|
listProcess.running = true;
|
|
}
|
|
}
|
|
|
|
MouseArea {
|
|
anchors.fill: parent
|
|
onClicked: rootState.wallpaperPickerOpen = false
|
|
}
|
|
|
|
Rectangle {
|
|
anchors.fill: parent
|
|
color: Theme.colors.overlay
|
|
|
|
Behavior on opacity { NumberAnimation { duration: Theme.animation.durationNormal } }
|
|
opacity: pickerWindow.visible ? 1 : 0
|
|
|
|
MouseArea {
|
|
anchors.fill: parent
|
|
onClicked: rootState.wallpaperPickerOpen = false
|
|
}
|
|
|
|
// Fixed slots, no sliding: only which wallpaper each slot shows changes.
|
|
Row {
|
|
id: carousel
|
|
anchors.centerIn: parent
|
|
spacing: Theme.geometry.spacing * 3
|
|
|
|
Repeater {
|
|
model: 5
|
|
|
|
delegate: Item {
|
|
id: delegateRoot
|
|
readonly property int offset: index - 2
|
|
readonly property bool isCurrent: offset === 0
|
|
readonly property int wpIndex: pickerWindow.wrappedIndex(pickerWindow.currentIndex + offset)
|
|
readonly property var wp: pickerWindow.wallpapers.length > 0 ? pickerWindow.wallpapers[wpIndex] : null
|
|
|
|
width: isCurrent ? 260 : 170
|
|
height: 340
|
|
|
|
Surface {
|
|
anchors.centerIn: parent
|
|
width: isCurrent ? 240 : 150
|
|
height: isCurrent ? 320 : 200
|
|
color: Theme.colors.surface
|
|
tint: delegateRoot.isCurrent ? Theme.colors.primary : Theme.colors.border
|
|
opacity: delegateRoot.isCurrent ? 1.0 : 0.5
|
|
glow: delegateRoot.isCurrent
|
|
clip: true
|
|
|
|
Image {
|
|
anchors.fill: parent
|
|
anchors.margins: 4
|
|
source: delegateRoot.wp ? "file://" + delegateRoot.wp.thumb : ""
|
|
fillMode: Image.PreserveAspectCrop
|
|
asynchronous: true
|
|
cache: true
|
|
}
|
|
}
|
|
|
|
MouseArea {
|
|
anchors.fill: parent
|
|
cursorShape: Qt.PointingHandCursor
|
|
onClicked: {
|
|
if (delegateRoot.isCurrent) {
|
|
pickerWindow.applySelection();
|
|
} else {
|
|
pickerWindow.currentIndex = delegateRoot.wpIndex;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
Text {
|
|
anchors.top: carousel.bottom
|
|
anchors.topMargin: Theme.geometry.spacing * 2
|
|
anchors.horizontalCenter: parent.horizontalCenter
|
|
text: pickerWindow.wallpapers.length > 0
|
|
? (pickerWindow.wallpapers[pickerWindow.currentIndex]?.path ?? "").split("/").pop()
|
|
: "No wallpapers found"
|
|
color: Theme.colors.text
|
|
font.family: Theme.font.family
|
|
}
|
|
}
|
|
|
|
Item {
|
|
id: keyHandler
|
|
anchors.fill: parent
|
|
focus: true
|
|
|
|
// The layer-shell surface only gets real keyboard focus from the
|
|
// compositor asynchronously after it's first mapped, so a single
|
|
// forceActiveFocus() call right on visible-toggle can silently lose
|
|
// input the first time this panel is shown in a session. There's no
|
|
// reliable signal for "the compositor granted focus now" on a
|
|
// wlr-layer-shell surface, so just retry until it actually sticks.
|
|
Timer {
|
|
interval: 30
|
|
repeat: true
|
|
running: pickerWindow.visible && !keyHandler.activeFocus
|
|
onTriggered: keyHandler.forceActiveFocus()
|
|
}
|
|
|
|
Keys.onEscapePressed: (event) => {
|
|
rootState.wallpaperPickerOpen = false;
|
|
event.accepted = true;
|
|
}
|
|
|
|
Keys.onLeftPressed: (event) => {
|
|
pickerWindow.currentIndex = pickerWindow.wrappedIndex(pickerWindow.currentIndex - 1);
|
|
event.accepted = true;
|
|
}
|
|
|
|
Keys.onRightPressed: (event) => {
|
|
pickerWindow.currentIndex = pickerWindow.wrappedIndex(pickerWindow.currentIndex + 1);
|
|
event.accepted = true;
|
|
}
|
|
|
|
Keys.onReturnPressed: (event) => {
|
|
pickerWindow.applySelection();
|
|
event.accepted = true;
|
|
}
|
|
}
|
|
}
|