aboutsummaryrefslogtreecommitdiffstats
path: root/desktop/modules/clipboard
diff options
context:
space:
mode:
Diffstat (limited to 'desktop/modules/clipboard')
-rw-r--r--desktop/modules/clipboard/Clip.js41
-rw-r--r--desktop/modules/clipboard/ClipboardModule.qml39
-rw-r--r--desktop/modules/clipboard/ClipboardPage.qml194
-rw-r--r--desktop/modules/clipboard/README.md29
-rw-r--r--desktop/modules/clipboard/test_clip.js47
5 files changed, 350 insertions, 0 deletions
diff --git a/desktop/modules/clipboard/Clip.js b/desktop/modules/clipboard/Clip.js
new file mode 100644
index 0000000..2745bfa
--- /dev/null
+++ b/desktop/modules/clipboard/Clip.js
@@ -0,0 +1,41 @@
+.pragma library
+
+// Copyright (C) 2026 Danilo M. <danix@danix.xyz>
+//
+// This program is free software; you can redistribute it and/or modify
+// it under the terms of the GNU General Public License version 2 as
+// published by the Free Software Foundation.
+//
+// This program is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+// GNU General Public License for more details.
+
+// cliphist's list output, parsed and filtered. Pure, so test_clip.js runs it
+// under node.
+
+// One entry per line of `cliphist list`: "<id>\t<preview>". The whole line is
+// kept, because decode and delete take it back verbatim on stdin. An image is
+// a preview cliphist writes as "[[ binary data <size> <type> <WxH> ]]".
+function parse(text) {
+ const out = [];
+ for (const line of text.split("\n")) {
+ const tab = line.indexOf("\t");
+ if (tab <= 0) continue;
+ const preview = line.slice(tab + 1);
+ out.push({
+ line: line,
+ id: line.slice(0, tab),
+ text: preview,
+ image: /^\[\[ binary data .* (png|jpe?g|gif|webp|bmp) /.test(preview),
+ });
+ }
+ return out;
+}
+
+// Case-insensitive substring match on the preview; an empty query keeps all.
+function filter(entries, query) {
+ const q = query.trim().toLowerCase();
+ if (q === "") return entries;
+ return entries.filter(e => e.text.toLowerCase().indexOf(q) >= 0);
+}
diff --git a/desktop/modules/clipboard/ClipboardModule.qml b/desktop/modules/clipboard/ClipboardModule.qml
new file mode 100644
index 0000000..25a8cc3
--- /dev/null
+++ b/desktop/modules/clipboard/ClipboardModule.qml
@@ -0,0 +1,39 @@
+// Copyright (C) 2026 Danilo M. <danix@danix.xyz>
+//
+// This program is free software; you can redistribute it and/or modify
+// it under the terms of the GNU General Public License version 2 as
+// published by the Free Software Foundation.
+//
+// This program is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+// GNU General Public License for more details.
+
+import QtQuick
+import "../.."
+
+// The clipboard history kept by `wl-paste --watch cliphist store`, which
+// autostart runs. Nothing runs here while the drawer is closed: the page
+// reads cliphist when it opens.
+Module {
+ name: "clipboard"
+ label: "Clipboard"
+
+ // fa-clipboard, present in Inconsolata Nerd Font.
+ icon: ""
+
+ page: Component {
+ Page {
+ id: pg
+ title: "Clipboard"
+ ClipboardPage {
+ width: parent.width
+ // Page wraps its content in a ScrollView. The list is
+ // virtualized and scrolls itself, so it takes exactly the
+ // visible height and the outer view never scrolls.
+ height: pg.height - 57
+ onCopied: pg.dismiss()
+ }
+ }
+ }
+}
diff --git a/desktop/modules/clipboard/ClipboardPage.qml b/desktop/modules/clipboard/ClipboardPage.qml
new file mode 100644
index 0000000..945557c
--- /dev/null
+++ b/desktop/modules/clipboard/ClipboardPage.qml
@@ -0,0 +1,194 @@
+// Copyright (C) 2026 Danilo M. <danix@danix.xyz>
+//
+// This program is free software; you can redistribute it and/or modify
+// it under the terms of the GNU General Public License version 2 as
+// published by the Free Software Foundation.
+//
+// This program is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+// GNU General Public License for more details.
+
+import Quickshell
+import Quickshell.Io
+import QtQuick
+import QtQuick.Controls
+import "../.."
+import "Clip.js" as Clip
+
+// Search, a newest-first list, copy on click or Enter, delete per row, clear
+// all behind a confirmation. Entries go to cliphist as arguments or stdin,
+// never inside a command string: clipboard text is untrusted.
+Item {
+ id: page
+
+ signal copied()
+
+ property var entries: []
+ readonly property var shown: Clip.filter(entries, search.text)
+
+ // Thumbnails decode here, on tmpfs, and are reused until a reboot.
+ readonly property string thumbDir:
+ (Quickshell.env("XDG_RUNTIME_DIR") || "/tmp") + "/qs-clip"
+
+ function copy(entry) {
+ Quickshell.execDetached(["sh", "-c", 'printf %s "$1" | cliphist decode | wl-copy',
+ "sh", entry.line]);
+ page.copied();
+ }
+
+ function remove(entry) {
+ Quickshell.execDetached(["sh", "-c", 'printf %s "$1" | cliphist delete', "sh", entry.line]);
+ page.entries = page.entries.filter(e => e.id !== entry.id);
+ }
+
+ Process {
+ running: true
+ command: ["cliphist", "list"]
+ stdout: StdioCollector { onStreamFinished: page.entries = Clip.parse(this.text) }
+ }
+
+ Row {
+ id: bar
+ anchors { top: parent.top; left: parent.left; right: parent.right }
+ spacing: 8
+
+ TextField {
+ id: search
+ width: bar.width - clear.width - bar.spacing
+ height: 32
+ placeholderText: "Search"
+ color: Theme.text
+ font { family: Theme.fontFamily; pixelSize: Theme.fontSize - 2 }
+ background: Rectangle {
+ radius: 6
+ color: Qt.alpha(Theme.surface, 0.9)
+ border.width: 1
+ border.color: Qt.alpha(Theme.text, 0.15)
+ }
+ Component.onCompleted: forceActiveFocus()
+ onTextChanged: list.currentIndex = 0
+
+ Keys.onUpPressed: list.decrementCurrentIndex()
+ Keys.onDownPressed: list.incrementCurrentIndex()
+ Keys.onReturnPressed: if (list.currentIndex >= 0 && page.shown.length) page.copy(page.shown[list.currentIndex])
+ Keys.onEnterPressed: if (list.currentIndex >= 0 && page.shown.length) page.copy(page.shown[list.currentIndex])
+ }
+
+ // Two clicks: the first arms it for 3 seconds.
+ Button {
+ id: clear
+ anchors.verticalCenter: parent.verticalCenter
+ danger: true
+ text: confirm.running ? "Confirm?" : "Clear all"
+ enabled: page.entries.length > 0
+ onClicked: {
+ if (!confirm.running) { confirm.start(); return; }
+ confirm.stop();
+ Quickshell.execDetached(["cliphist", "wipe"]);
+ page.entries = [];
+ }
+ Timer { id: confirm; interval: 3000 }
+ }
+ }
+
+ ListView {
+ id: list
+ anchors { top: bar.bottom; topMargin: 10; left: parent.left; right: parent.right; bottom: parent.bottom }
+ clip: true
+ spacing: 4
+ model: page.shown
+ highlightMoveDuration: 0
+ boundsBehavior: Flickable.StopAtBounds
+ ScrollBar.vertical: ScrollBar {}
+
+ Text {
+ anchors.centerIn: parent
+ visible: page.shown.length === 0
+ text: page.entries.length === 0 ? "Clipboard history is empty" : "No match"
+ color: Theme.subtext
+ font { family: Theme.fontFamily; pixelSize: Theme.fontSize - 2 }
+ }
+
+ delegate: Rectangle {
+ id: row
+ required property var modelData
+ required property int index
+
+ width: list.width
+ height: modelData.image ? 96 : 36
+ radius: 8
+ color: rowArea.containsMouse || ListView.isCurrentItem
+ ? Qt.alpha(Theme.accent, 0.18) : Qt.alpha(Theme.surface, 0.35)
+
+ // Decoded once per entry, then reused from thumbDir. The id and
+ // line are arguments, the paths are built by the shell from them.
+ property string thumb: ""
+ Process {
+ running: row.modelData.image
+ command: ["sh", "-c",
+ 'mkdir -p "$1" && f="$1/$2.png" && { [ -s "$f" ] || printf %s "$3" | cliphist decode > "$f"; } && printf %s "$f"',
+ "sh", page.thumbDir, row.modelData.id, row.modelData.line]
+ stdout: StdioCollector { onStreamFinished: row.thumb = this.text }
+ }
+
+ MouseArea {
+ id: rowArea
+ anchors.fill: parent
+ hoverEnabled: true
+ cursorShape: Qt.PointingHandCursor
+ onClicked: page.copy(row.modelData)
+ }
+
+ Image {
+ id: thumbImg
+ visible: row.modelData.image
+ anchors { left: parent.left; leftMargin: 8; verticalCenter: parent.verticalCenter }
+ width: 140
+ height: 80
+ fillMode: Image.PreserveAspectFit
+ asynchronous: true
+ sourceSize: Qt.size(280, 160)
+ source: row.thumb ? "file://" + row.thumb : ""
+ }
+
+ Text {
+ anchors {
+ left: row.modelData.image ? thumbImg.right : parent.left
+ leftMargin: 10
+ right: del.left; rightMargin: 8
+ verticalCenter: parent.verticalCenter
+ }
+ elide: Text.ElideRight
+ maximumLineCount: 1
+ textFormat: Text.PlainText
+ // One line: a multi-line entry shows its first line's worth.
+ text: row.modelData.text.replace(/\s+/g, " ")
+ color: row.modelData.image ? Theme.subtext : Theme.text
+ font { family: Theme.fontFamily; pixelSize: Theme.fontSize - 3 }
+ }
+
+ Rectangle {
+ id: del
+ anchors { right: parent.right; rightMargin: 6; verticalCenter: parent.verticalCenter }
+ width: 26; height: 26; radius: 13
+ color: delArea.containsMouse ? Qt.alpha(Theme.red, 0.3) : "transparent"
+
+ Text {
+ anchors.centerIn: parent
+ text: ""
+ font { family: Theme.iconFamily; pixelSize: 12 }
+ color: delArea.containsMouse ? Theme.red : Theme.subtext
+ }
+
+ MouseArea {
+ id: delArea
+ anchors.fill: parent
+ hoverEnabled: true
+ cursorShape: Qt.PointingHandCursor
+ onClicked: page.remove(row.modelData)
+ }
+ }
+ }
+ }
+}
diff --git a/desktop/modules/clipboard/README.md b/desktop/modules/clipboard/README.md
new file mode 100644
index 0000000..4502349
--- /dev/null
+++ b/desktop/modules/clipboard/README.md
@@ -0,0 +1,29 @@
+# clipboard
+
+The clipboard history as a drawer page, replacing the rofi list. The history
+itself is cliphist's, fed by `wl-paste --watch cliphist store` from autostart;
+this module only reads and edits it. CTRL+SHIFT+L opens the drawer straight on
+this page (`ipc call drawer open clipboard`).
+
+- The search field has focus on open and filters as you type, ignoring case.
+- Up and Down move the highlight; Enter or a click copies the entry and closes
+ the drawer.
+- `×` on a row deletes that entry; "Clear all" asks once more ("Confirm?",
+ 3 seconds) and then runs `cliphist wipe`.
+
+Nothing runs while the drawer is closed: the page runs `cliphist list` when
+it opens. 750 entries measured, 42 of them images.
+
+**Entries are untrusted text.** Copy and delete pass the cliphist line to
+`sh -c` as `$1` and print it into `cliphist decode` or `cliphist delete`; it
+never becomes part of a command string.
+
+**Images show as thumbnails.** A row whose preview is cliphist's
+`[[ binary data <size> <type> <WxH> ]]` decodes the entry once into
+`$XDG_RUNTIME_DIR/qs-clip/<id>.png` and reuses the file after. That directory
+is tmpfs, so a reboot clears it. The list is a `ListView`, so only rows that
+are created decode.
+
+`Clip.js` holds the parsing and the filter, tested with:
+
+ node desktop/modules/clipboard/test_clip.js
diff --git a/desktop/modules/clipboard/test_clip.js b/desktop/modules/clipboard/test_clip.js
new file mode 100644
index 0000000..e4525f8
--- /dev/null
+++ b/desktop/modules/clipboard/test_clip.js
@@ -0,0 +1,47 @@
+// Copyright (C) 2026 Danilo M. <danix@danix.xyz>
+//
+// This program is free software; you can redistribute it and/or modify
+// it under the terms of the GNU General Public License version 2 as
+// published by the Free Software Foundation.
+//
+// This program is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+// GNU General Public License for more details.
+
+// Run: node desktop/modules/clipboard/test_clip.js
+// Clip.js is a QML library, so its .pragma line is stripped before node
+// evaluates it, as test_perf.js does for Perf.js.
+
+const fs = require("fs");
+const assert = require("assert");
+
+const src = fs.readFileSync(__dirname + "/Clip.js", "utf8")
+ .replace(/^\.pragma library$/m, "");
+const C = new Function(src + "\nreturn { parse, filter };")();
+
+const list = [
+ "812\tgit push --force-with-lease",
+ "811\t[[ binary data 2 MiB png 2560x1080 ]]",
+ "810\tA line\twith a tab",
+ "",
+ "garbage without a tab",
+].join("\n");
+const e = C.parse(list);
+
+assert.strictEqual(e.length, 3);
+assert.deepStrictEqual(e[0], { line: "812\tgit push --force-with-lease", id: "812",
+ text: "git push --force-with-lease", image: false });
+assert.strictEqual(e[1].image, true);
+// Only the first tab separates the id; the rest is the text, and the line is
+// kept whole for decode.
+assert.strictEqual(e[2].text, "A line\twith a tab");
+assert.strictEqual(e[2].line, "810\tA line\twith a tab");
+// Text that merely mentions binary data is not an image.
+assert.strictEqual(C.parse("9\tthe [[ binary data ]] marker")[0].image, false);
+
+assert.strictEqual(C.filter(e, "").length, 3);
+assert.deepStrictEqual(C.filter(e, " PUSH ").map(x => x.id), ["812"]);
+assert.deepStrictEqual(C.filter(e, "nothing"), []);
+
+console.log("test_clip: ok");