Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions bun.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions packages/app/src/components/prompt-input.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ import { Persist, persisted } from "@/utils/persist"
import { SessionContextUsage } from "@/components/session-context-usage"
import { usePermission } from "@/context/permission"
import { useLanguage } from "@/context/language"
import { usePlatform } from "@/context/platform"
import { createTextFragment, getCursorPosition, setCursorPosition, setRangeEdge } from "./prompt-input/editor-dom"
import { createPromptAttachments, ACCEPTED_FILE_TYPES } from "./prompt-input/attachments"
import { navigatePromptHistory, prependHistoryEntry, promptLength } from "./prompt-input/history"
Expand Down Expand Up @@ -97,6 +98,7 @@ export const PromptInput: Component<PromptInputProps> = (props) => {
const command = useCommand()
const permission = usePermission()
const language = useLanguage()
const platform = usePlatform()
let editorRef!: HTMLDivElement
let fileInputRef!: HTMLInputElement
let scrollRef!: HTMLDivElement
Expand Down Expand Up @@ -766,6 +768,7 @@ export const PromptInput: Component<PromptInputProps> = (props) => {
setCursorPosition(editorRef, promptLength(prompt.current()))
},
addPart,
readClipboardImage: platform.readClipboardImage,
})

const { abort, handleSubmit } = createPromptSubmit({
Expand Down
11 changes: 11 additions & 0 deletions packages/app/src/components/prompt-input/attachments.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ type PromptAttachmentsInput = {
setDraggingType: (type: "image" | "@mention" | null) => void
focusEditor: () => void
addPart: (part: ContentPart) => void
readClipboardImage?: () => Promise<File | null>
}

export function createPromptAttachments(input: PromptAttachmentsInput) {
Expand Down Expand Up @@ -76,6 +77,16 @@ export function createPromptAttachments(input: PromptAttachmentsInput) {
}

const plainText = clipboardData.getData("text/plain") ?? ""

// Desktop: Browser clipboard has no images and no text, try platform's native clipboard for images
if (input.readClipboardImage && !plainText) {
const file = await input.readClipboardImage()
if (file) {
await addImageAttachment(file)
return
}
}

if (!plainText) return
input.addPart({ type: "text", content: plainText, start: 0, end: 0 })
}
Expand Down
3 changes: 3 additions & 0 deletions packages/app/src/context/platform.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,9 @@ export type Platform = {

/** Check if an editor app exists (desktop only) */
checkAppExists?(appName: string): Promise<boolean>

/** Read image from clipboard (desktop only) */
readClipboardImage?(): Promise<File | null>
}

export const { use: usePlatform, provider: PlatformProvider } = createSimpleContext({
Expand Down
1 change: 1 addition & 0 deletions packages/desktop/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
"@solid-primitives/i18n": "2.2.1",
"@solid-primitives/storage": "catalog:",
"@tauri-apps/api": "^2",
"@tauri-apps/plugin-clipboard-manager": "~2",
"@tauri-apps/plugin-deep-link": "~2",
"@tauri-apps/plugin-dialog": "~2",
"@tauri-apps/plugin-opener": "^2",
Expand Down
3 changes: 2 additions & 1 deletion packages/desktop/src-tauri/capabilities/default.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
{
"identifier": "http:default",
"allow": [{ "url": "http://*" }, { "url": "https://*" }, { "url": "http://*:*/*" }]
}
},
"clipboard-manager:allow-read-image"
]
}
24 changes: 24 additions & 0 deletions packages/desktop/src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import { fetch as tauriFetch } from "@tauri-apps/plugin-http"
import { Store } from "@tauri-apps/plugin-store"
import { Splash } from "@opencode-ai/ui/logo"
import { createSignal, Show, Accessor, JSX, createResource, onMount, onCleanup } from "solid-js"
import { readImage } from "@tauri-apps/plugin-clipboard-manager"

import { UPDATER_ENABLED } from "./updater"
import { initI18n, t } from "./i18n"
Expand Down Expand Up @@ -344,6 +345,29 @@ const createPlatform = (password: Accessor<string | null>): Platform => ({
checkAppExists: async (appName: string) => {
return commands.checkAppExists(appName)
},

async readClipboardImage() {
const image = await readImage().catch(() => null)
if (!image) return null
const bytes = await image.rgba().catch(() => null)
if (!bytes || bytes.length === 0) return null
const size = await image.size().catch(() => null)
if (!size) return null
const canvas = document.createElement("canvas")
canvas.width = size.width
canvas.height = size.height
const ctx = canvas.getContext("2d")
if (!ctx) return null
const imageData = ctx.createImageData(size.width, size.height)
imageData.data.set(bytes)
ctx.putImageData(imageData, 0, 0)
return new Promise<File | null>((resolve) => {
canvas.toBlob((blob) => {
if (!blob) return resolve(null)
resolve(new File([blob], `pasted-image-${Date.now()}.png`, { type: "image/png" }))
}, "image/png")
})
},
})

let menuTrigger = null as null | ((id: string) => void)
Expand Down
Loading