sidebar: ai: allow pasting image

This commit is contained in:
end-4 2025-08-22 16:52:02 +07:00
parent 63365c2109
commit db83864d78

View file

@ -9,6 +9,7 @@ import QtQuick.Controls
import QtQuick.Layouts
import Qt5Compat.GraphicalEffects
import Quickshell
import Quickshell.Io
Item {
id: root
@ -210,6 +211,26 @@ Inline w/ backslash and round brackets \\(e^{i\\pi} + 1 = 0\\)
}
}
Process {
id: decodeImageAndAttachProc
property string imageDecodePath: Directories.cliphistDecode
property string imageDecodeFileName: "image"
property string imageDecodeFilePath: `${imageDecodePath}/${imageDecodeFileName}`
function handleEntry(entry: string) {
imageDecodeFileName = parseInt(entry.match(/^(\d+)\t/)[1])
decodeImageAndAttachProc.exec(["bash", "-c",
`[ -f ${imageDecodeFilePath} ] || echo '${StringUtils.shellSingleQuoteEscape(entry)}' | cliphist decode > '${imageDecodeFilePath}'`
])
}
onExited: (exitCode, exitStatus) => {
if (exitCode === 0) {
Ai.attachFile(imageDecodeFilePath);
} else {
console.error("[AiChat] Failed to decode image in clipboard content")
}
}
}
component StatusItem: MouseArea {
id: statusItem
property string icon
@ -610,6 +631,17 @@ Inline w/ backslash and round brackets \\(e^{i\\pi} + 1 = 0\\)
root.handleInput(inputText)
event.accepted = true
}
} else if ((event.modifiers & Qt.ControlModifier) && event.key === Qt.Key_V) {
// Try image paste first
const currentClipboardEntry = Cliphist.entries[0]
if (/^\d+\t\[\[.*binary data.*\d+x\d+.*\]\]$/.test(currentClipboardEntry)) { // First entry = currently copied entry = image?
print("CLIPBOARD IZ IMAGE, ATTACHING I REPEAT ATTACHING")
decodeImageAndAttachProc.handleEntry(currentClipboardEntry)
event.accepted = true;
return;
}
event.accepted = false;
// fall through to normal paste for text
}
}
}