2 Commits

Author SHA1 Message Date
mamamiyear baec01610c feat: add screenshot OCR extraction 2026-07-08 00:42:49 +08:00
mamamiyear f90612976f feat: add text annotation tool 2026-07-07 23:49:24 +08:00
17 changed files with 1814 additions and 60 deletions
+99
View File
@@ -23,6 +23,7 @@ import (
"github.com/mmmy/snapgo/internal/infrastructure/display" "github.com/mmmy/snapgo/internal/infrastructure/display"
"github.com/mmmy/snapgo/internal/infrastructure/hotkey" "github.com/mmmy/snapgo/internal/infrastructure/hotkey"
llmpkg "github.com/mmmy/snapgo/internal/infrastructure/llm" llmpkg "github.com/mmmy/snapgo/internal/infrastructure/llm"
ocrpkg "github.com/mmmy/snapgo/internal/infrastructure/ocr"
"github.com/mmmy/snapgo/internal/infrastructure/oss" "github.com/mmmy/snapgo/internal/infrastructure/oss"
"github.com/mmmy/snapgo/internal/infrastructure/screencapture" "github.com/mmmy/snapgo/internal/infrastructure/screencapture"
sshpkg "github.com/mmmy/snapgo/internal/infrastructure/ssh" sshpkg "github.com/mmmy/snapgo/internal/infrastructure/ssh"
@@ -499,6 +500,48 @@ func (a *App) finishSummary(svc *application.CaptureSummaryService, summary stri
return nil return nil
} }
func (a *App) runOCRPipeline(pngBytes []byte) error {
a.mu.RLock()
cfg := a.cfg
a.mu.RUnlock()
if !cfg.IsOCRConfigured() {
err := fmt.Errorf("请先在文字提取配置页填写 OCR Provider 的 AccessKey ID 和 AccessKey Secret")
a.emitOperationStatus("ocr", "需要配置 OCR", err.Error(), "error")
wruntime.EventsEmit(a.ctx, "upload:failure", err.Error())
return err
}
providerID, ocrCfg, _ := cfg.ActiveOCRProvider()
recognizer, err := ocrpkg.NewClient(providerID, ocrCfg)
if err != nil {
a.emitOperationStatus("ocr", "OCR 配置错误", err.Error(), "error")
wruntime.EventsEmit(a.ctx, "upload:failure", err.Error())
return err
}
svc := &application.CaptureOCRService{
Recognizer: recognizer,
Clipboard: a.clip,
}
a.emitOperationStatus("ocr", "识别中", "正在提取截图文字", "running")
text, err := svc.Recognize(a.ctx, pngBytes)
if err != nil {
a.emitOperationStatus("ocr", "识别失败", err.Error(), "error")
wruntime.EventsEmit(a.ctx, "upload:failure", err.Error())
return err
}
if err := svc.CopyText(a.ctx, text); err != nil {
a.emitOperationStatus("ocr", "复制失败", err.Error(), "error")
wruntime.EventsEmit(a.ctx, "upload:failure", err.Error())
return err
}
a.emitOperationStatus("ocr", "提取完成", "文字已复制到剪贴板", "success")
wruntime.EventsEmit(a.ctx, "upload:success", "ocr text copied to clipboard")
return nil
}
func (a *App) consumePendingCapture() (*pendingCapture, error) { func (a *App) consumePendingCapture() (*pendingCapture, error) {
a.pendingMu.Lock() a.pendingMu.Lock()
pc := a.pending pc := a.pending
@@ -876,6 +919,35 @@ func (a *App) SummarizeRegion(result CaptureResult) error {
return a.runSummaryPipeline(cropped) return a.runSummaryPipeline(cropped)
} }
// ExtractTextRegion sends the selected screenshot to the configured OCR
// provider and copies the extracted text to the clipboard.
func (a *App) ExtractTextRegion(result CaptureResult) error {
pc, err := a.consumePendingCapture()
if err != nil {
slog.Warn("ExtractTextRegion: no pending capture", "err", err)
return err
}
defer func() {
a.capturing.Store(false)
a.dismissOverlay()
}()
a.dismissOverlay()
flushFrame()
slog.Info("ExtractTextRegion: capturing region",
"x", result.Rect.X, "y", result.Rect.Y,
"w", result.Rect.W, "h", result.Rect.H,
"annotations", len(result.Annotations))
cropped, err := a.captureSelectedPNG(result, pc)
if err != nil {
slog.Error("ExtractTextRegion: capture failed", "err", err)
wruntime.EventsEmit(a.ctx, "upload:failure", err.Error())
return err
}
return a.runOCRPipeline(cropped)
}
// SaveNativeRegionToRemote is the macOS-native overlay equivalent of // SaveNativeRegionToRemote is the macOS-native overlay equivalent of
// SaveRegionToRemote. The AppKit panel is already closed by the time this // SaveRegionToRemote. The AppKit panel is already closed by the time this
// runs (see saveRemoteSelection in native_overlay_darwin.go), so we only // runs (see saveRemoteSelection in native_overlay_darwin.go), so we only
@@ -933,6 +1005,33 @@ func (a *App) SummarizeNativeRegion(result CaptureResult) error {
return a.runSummaryPipeline(cropped) return a.runSummaryPipeline(cropped)
} }
// ExtractTextNativeRegion is the macOS-native overlay equivalent of
// ExtractTextRegion. The AppKit panel is already closed before this runs.
func (a *App) ExtractTextNativeRegion(result CaptureResult) error {
pc, err := a.consumePendingCapture()
if err != nil {
slog.Warn("ExtractTextNativeRegion: no pending capture", "err", err)
return err
}
defer func() {
a.capturing.Store(false)
hideDockIcon()
}()
flushFrame()
slog.Info("ExtractTextNativeRegion: capturing region",
"x", result.Rect.X, "y", result.Rect.Y,
"w", result.Rect.W, "h", result.Rect.H,
"annotations", len(result.Annotations))
cropped, err := a.captureSelectedPNG(result, pc)
if err != nil {
slog.Error("ExtractTextNativeRegion: capture failed", "err", err)
wruntime.EventsEmit(a.ctx, "upload:failure", err.Error())
return err
}
return a.runOCRPipeline(cropped)
}
func parseNativeAnnotations(raw string) []application.Annotation { func parseNativeAnnotations(raw string) []application.Annotation {
if raw == "" { if raw == "" {
return nil return nil
+28 -1
View File
@@ -23,6 +23,7 @@ import {
SaveRegionImage, SaveRegionImage,
SaveRegionToRemote, SaveRegionToRemote,
SummarizeRegion, SummarizeRegion,
ExtractTextRegion,
CancelRegion, CancelRegion,
GetConfig, GetConfig,
} from '../wailsjs/go/main/App' } from '../wailsjs/go/main/App'
@@ -58,7 +59,7 @@ async function loadThemePreference() {
// `SettingsTab` is hoisted to the App shell so the sidebar (which lives // `SettingsTab` is hoisted to the App shell so the sidebar (which lives
// here) and the inner SettingsView (which renders the matching card) can // here) and the inner SettingsView (which renders the matching card) can
// share a single source of truth without an event bus. // share a single source of truth without an event bus.
type SettingsTab = 'general' | 's3' | 'ssh' | 'llm' type SettingsTab = 'general' | 's3' | 'ssh' | 'llm' | 'ocr'
const activeTab = ref<SettingsTab>('general') const activeTab = ref<SettingsTab>('general')
// Sidebar entries are declarative so adding a destination type later is // Sidebar entries are declarative so adding a destination type later is
@@ -68,6 +69,7 @@ const sidebarItems: Array<{ id: SettingsTab; label: string }> = [
{ id: 's3', label: '对象存储' }, { id: 's3', label: '对象存储' },
{ id: 'ssh', label: '远程主机' }, { id: 'ssh', label: '远程主机' },
{ id: 'llm', label: '智能识图' }, { id: 'llm', label: '智能识图' },
{ id: 'ocr', label: '文字提取' },
] ]
interface OverlayPayload { interface OverlayPayload {
@@ -233,6 +235,28 @@ async function onOverlaySummarize(rect: {
} }
} }
async function onOverlayOCR(rect: {
rect: {
x: number
y: number
w: number
h: number
}
annotations: Array<{
tool: string
color: string
points: Array<{ x: number; y: number }>
}>
}) {
mode.value = 'settings'
overlayPayload.value = null
try {
await ExtractTextRegion(rect as any)
} catch {
/* Surfaced via upload:failure */
}
}
async function onOverlayCancel() { async function onOverlayCancel() {
mode.value = 'settings' mode.value = 'settings'
overlayPayload.value = null overlayPayload.value = null
@@ -264,6 +288,8 @@ onMounted(() => {
'success', 'success',
url === 'summary copied to clipboard' url === 'summary copied to clipboard'
? 'Summary copied to clipboard' ? 'Summary copied to clipboard'
: url === 'ocr text copied to clipboard'
? 'OCR text copied to clipboard'
: `Copied: ${url}` : `Copied: ${url}`
) )
}) })
@@ -303,6 +329,7 @@ onUnmounted(() => {
@save="onOverlaySave" @save="onOverlaySave"
@save-remote="onOverlaySaveRemote" @save-remote="onOverlaySaveRemote"
@summarize="onOverlaySummarize" @summarize="onOverlaySummarize"
@ocr="onOverlayOCR"
@cancel="onOverlayCancel" @cancel="onOverlayCancel"
/> />
+141 -7
View File
@@ -1,5 +1,5 @@
<script setup lang="ts"> <script setup lang="ts">
import { computed, onMounted, onUnmounted, ref } from 'vue' import { computed, nextTick, onMounted, onUnmounted, ref } from 'vue'
// Inline SVG markup imported as raw strings via Vite's `?raw` suffix. // Inline SVG markup imported as raw strings via Vite's `?raw` suffix.
// Rationale: rendering through v-html lets the icon inherit `currentColor` // Rationale: rendering through v-html lets the icon inherit `currentColor`
// from the toolbar button, so styling stays in CSS without bundling extra // from the toolbar button, so styling stays in CSS without bundling extra
@@ -23,7 +23,7 @@ interface Rect {
h: number h: number
} }
type Tool = 'pen' | 'rect' | 'ellipse' type Tool = 'pen' | 'rect' | 'ellipse' | 'text'
interface Point { interface Point {
x: number x: number
y: number y: number
@@ -32,6 +32,7 @@ interface Annotation {
tool: Tool tool: Tool
color: string color: string
points: Point[] points: Point[]
text?: string
} }
const emit = defineEmits<{ const emit = defineEmits<{
@@ -55,6 +56,10 @@ const emit = defineEmits<{
e: 'summarize', e: 'summarize',
payload: { rect: Rect; annotations: Annotation[] } payload: { rect: Rect; annotations: Annotation[] }
): void ): void
(
e: 'ocr',
payload: { rect: Rect; annotations: Annotation[] }
): void
(e: 'cancel'): void (e: 'cancel'): void
}>() }>()
@@ -64,6 +69,8 @@ const draftAnnotation = ref<Annotation | null>(null)
const activeTool = ref<Tool>('pen') const activeTool = ref<Tool>('pen')
const activeColor = ref('#ef4444') const activeColor = ref('#ef4444')
const paletteOpen = ref(false) const paletteOpen = ref(false)
const textDraft = ref<{ point: Point; value: string } | null>(null)
const textInputRef = ref<HTMLInputElement | null>(null)
type ResizeHandle = 'n' | 's' | 'e' | 'w' | 'nw' | 'ne' | 'sw' | 'se' type ResizeHandle = 'n' | 's' | 'e' | 'w' | 'nw' | 'ne' | 'sw' | 'se'
type DragMode = 'idle' | 'creating' | 'moving' | 'resizing' | 'annotating' type DragMode = 'idle' | 'creating' | 'moving' | 'resizing' | 'annotating'
@@ -115,8 +122,8 @@ const sizeLabel = computed(() => {
return `${Math.round(rect.value.w)} × ${Math.round(rect.value.h)}` return `${Math.round(rect.value.w)} × ${Math.round(rect.value.h)}`
}) })
const MARK_TOOLBAR_W = 190 const MARK_TOOLBAR_W = 222
const ACTION_TOOLBAR_W = 216 const ACTION_TOOLBAR_W = 252
const TOOLBAR_GROUP_GAP = 8 const TOOLBAR_GROUP_GAP = 8
const rightToolbarPos = computed(() => { const rightToolbarPos = computed(() => {
@@ -227,6 +234,7 @@ function hitHandle(p: Point): ResizeHandle | null {
function onMouseDown(e: MouseEvent) { function onMouseDown(e: MouseEvent) {
if (e.button !== 0) return if (e.button !== 0) return
commitTextDraft()
paletteOpen.value = false paletteOpen.value = false
const p = pointFromEvent(e) const p = pointFromEvent(e)
const handle = hitHandle(p) const handle = hitHandle(p)
@@ -253,6 +261,10 @@ function onSelectionMouseDown(e: MouseEvent) {
if (e.button !== 0 || !rect.value) return if (e.button !== 0 || !rect.value) return
e.stopPropagation() e.stopPropagation()
paletteOpen.value = false paletteOpen.value = false
if (activeTool.value === 'text') {
beginTextAnnotation(localPoint(pointFromEvent(e)))
return
}
if (e.detail >= 2) { if (e.detail >= 2) {
removeSinglePointAnnotation() removeSinglePointAnnotation()
onCopy() onCopy()
@@ -371,8 +383,13 @@ function onSummarize() {
emitAction('summarize') emitAction('summarize')
} }
function emitAction(action: 'confirm' | 'copy' | 'save' | 'save-remote' | 'summarize') { function onOCR() {
emitAction('ocr')
}
function emitAction(action: 'confirm' | 'copy' | 'save' | 'save-remote' | 'summarize' | 'ocr') {
if (!rect.value) return if (!rect.value) return
commitTextDraft()
const payload = { const payload = {
rect: { ...rect.value }, rect: { ...rect.value },
annotations: annotations.value, annotations: annotations.value,
@@ -382,6 +399,7 @@ function emitAction(action: 'confirm' | 'copy' | 'save' | 'save-remote' | 'summa
if (action === 'save') emit('save', payload) if (action === 'save') emit('save', payload)
if (action === 'save-remote') emit('save-remote', payload) if (action === 'save-remote') emit('save-remote', payload)
if (action === 'summarize') emit('summarize', payload) if (action === 'summarize') emit('summarize', payload)
if (action === 'ocr') emit('ocr', payload)
} }
function onCancel() { function onCancel() {
@@ -389,6 +407,13 @@ function onCancel() {
} }
function onKeydown(e: KeyboardEvent) { function onKeydown(e: KeyboardEvent) {
if (textDraft.value) {
if (e.key === 'Escape') {
e.preventDefault()
cancelTextDraft()
}
return
}
if (e.key === 'Escape') { if (e.key === 'Escape') {
e.preventDefault() e.preventDefault()
onCancel() onCancel()
@@ -399,6 +424,7 @@ function onKeydown(e: KeyboardEvent) {
} }
function undoAnnotation() { function undoAnnotation() {
cancelTextDraft()
annotations.value.pop() annotations.value.pop()
} }
@@ -409,6 +435,45 @@ function removeSinglePointAnnotation() {
} }
} }
function beginTextAnnotation(point: Point) {
commitTextDraft()
dragMode.value = 'idle'
draftAnnotation.value = null
textDraft.value = { point, value: '' }
void nextTick(() => {
textInputRef.value?.focus()
textInputRef.value?.select()
})
}
function commitTextDraft() {
if (!textDraft.value) return
const text = textDraft.value.value.trim()
if (text !== '') {
annotations.value.push({
tool: 'text',
color: activeColor.value,
points: [textDraft.value.point],
text,
})
}
textDraft.value = null
}
function cancelTextDraft() {
textDraft.value = null
}
function onTextDraftKeydown(e: KeyboardEvent) {
if (e.key === 'Enter') {
e.preventDefault()
commitTextDraft()
} else if (e.key === 'Escape') {
e.preventDefault()
cancelTextDraft()
}
}
onMounted(() => { onMounted(() => {
window.addEventListener('keydown', onKeydown) window.addEventListener('keydown', onKeydown)
}) })
@@ -475,6 +540,17 @@ onUnmounted(() => {
stroke-width="3" stroke-width="3"
fill="none" fill="none"
/> />
<text
v-else-if="annotation.tool === 'text' && annotation.points.length >= 1"
:x="annotation.points[0].x"
:y="annotation.points[0].y"
:fill="annotation.color"
font-size="20"
font-weight="600"
dominant-baseline="hanging"
>
{{ annotation.text }}
</text>
</g> </g>
</svg> </svg>
@@ -490,6 +566,22 @@ onUnmounted(() => {
@mousedown="onSelectionMouseDown" @mousedown="onSelectionMouseDown"
/> />
<input
v-if="rect && textDraft"
ref="textInputRef"
v-model="textDraft.value"
class="text-editor"
:style="{
left: rect.x + textDraft.point.x + 'px',
top: rect.y + textDraft.point.y + 'px',
color: activeColor,
}"
spellcheck="false"
@mousedown.stop
@keydown.stop="onTextDraftKeydown"
@blur="commitTextDraft"
/>
<div <div
v-for="handle in handles" v-for="handle in handles"
:key="handle.name" :key="handle.name"
@@ -554,6 +646,18 @@ onUnmounted(() => {
<circle cx="12" cy="12" r="7" /> <circle cx="12" cy="12" r="7" />
</svg> </svg>
</button> </button>
<button
class="icon-btn"
:class="{ active: activeTool === 'text' }"
title="文字标记"
@click="selectTool('text')"
>
<svg viewBox="0 0 24 24" aria-hidden="true">
<path d="M5 5h14" />
<path d="M12 5v14" />
<path d="M9 19h6" />
</svg>
</button>
<div class="color-wrap"> <div class="color-wrap">
<button <button
class="icon-btn color-btn" class="icon-btn color-btn"
@@ -621,6 +725,21 @@ onUnmounted(() => {
@click="onSaveRemote" @click="onSaveRemote"
v-html="saveRemoteIcon" v-html="saveRemoteIcon"
/> />
<button
class="action-btn"
data-tip="提取文字"
aria-label="提取文字"
@click="onOCR"
>
<svg viewBox="0 0 24 24" aria-hidden="true">
<path d="M5 8V5h3" />
<path d="M16 5h3v3" />
<path d="M19 16v3h-3" />
<path d="M8 19H5v-3" />
<path d="M9 15l3-7 3 7" />
<path d="M10 13h4" />
</svg>
</button>
<button <button
class="action-btn" class="action-btn"
data-tip="复制总结" data-tip="复制总结"
@@ -671,6 +790,21 @@ onUnmounted(() => {
cursor: crosshair; cursor: crosshair;
} }
.text-editor {
position: absolute;
z-index: 6;
min-width: 120px;
max-width: 320px;
height: 28px;
padding: 2px 6px;
border: 1px solid currentColor;
border-radius: 4px;
outline: none;
background: rgba(15, 23, 42, 0.72);
box-shadow: 0 4px 18px rgba(0, 0, 0, 0.35);
font: 600 20px/1.2 -apple-system, BlinkMacSystemFont, "SF Pro Text", sans-serif;
}
.resize-handle { .resize-handle {
position: absolute; position: absolute;
width: 10px; width: 10px;
@@ -725,10 +859,10 @@ onUnmounted(() => {
} }
.mark-toolbar { .mark-toolbar {
width: 190px; width: 222px;
} }
.action-toolbar { .action-toolbar {
width: 216px; width: 252px;
} }
.icon-btn, .icon-btn,
+163 -1
View File
@@ -9,6 +9,7 @@
* • "s3" — S3-compatible object-storage credentials. * • "s3" — S3-compatible object-storage credentials.
* • "ssh" — SSH/SCP destination for the "save remote" button. * • "ssh" — SSH/SCP destination for the "save remote" button.
* • "llm" — multimodal screenshot summary provider settings. * • "llm" — multimodal screenshot summary provider settings.
* • "ocr" — cloud OCR provider settings for text extraction.
* *
* State flow: load() pulls config from Go on mount, save() pushes back. * State flow: load() pulls config from Go on mount, save() pushes back.
*/ */
@@ -23,7 +24,7 @@ import {
// Tab discriminator shared with the App shell. Kept as a string union so // Tab discriminator shared with the App shell. Kept as a string union so
// the parent can pass the value without importing a type from this view. // the parent can pass the value without importing a type from this view.
type TabId = 'general' | 's3' | 'ssh' | 'llm' type TabId = 'general' | 's3' | 'ssh' | 'llm' | 'ocr'
type ThemeMode = 'auto' | 'light' | 'dark' type ThemeMode = 'auto' | 'light' | 'dark'
const props = defineProps<{ const props = defineProps<{
@@ -74,12 +75,30 @@ interface LLMConfig {
providers: Record<string, LLMProviderConfig> providers: Record<string, LLMProviderConfig>
} }
interface OCRProviderConfig {
label: string
endpoint: string
accessKeyId: string
accessKeySecret: string
region: string
service: string
action: string
version: string
timeoutSecs: number
}
interface OCRConfig {
activeProvider: string
providers: Record<string, OCRProviderConfig>
}
interface AppConfig { interface AppConfig {
hotkey: string hotkey: string
theme: ThemeMode theme: ThemeMode
s3: S3Config s3: S3Config
ssh: SSHConfig ssh: SSHConfig
llm: LLMConfig llm: LLMConfig
ocr: OCRConfig
} }
const llmProviderOptions = [ const llmProviderOptions = [
@@ -88,6 +107,11 @@ const llmProviderOptions = [
{ id: 'openai', label: 'ChatGPT / OpenAI-compatible' }, { id: 'openai', label: 'ChatGPT / OpenAI-compatible' },
] ]
const ocrProviderOptions = [
{ id: 'aliyun', label: '阿里云文字识别' },
{ id: 'volcengine', label: '火山引擎文字识别' },
]
const themeOptions: Array<{ id: ThemeMode; label: string }> = [ const themeOptions: Array<{ id: ThemeMode; label: string }> = [
{ id: 'light', label: '浅色' }, { id: 'light', label: '浅色' },
{ id: 'dark', label: '深色' }, { id: 'dark', label: '深色' },
@@ -165,6 +189,33 @@ function defaultConfig(): AppConfig {
}, },
}, },
}, },
ocr: {
activeProvider: 'aliyun',
providers: {
aliyun: {
label: '阿里云文字识别',
endpoint: 'https://ocr-api.cn-hangzhou.aliyuncs.com',
accessKeyId: '',
accessKeySecret: '',
region: '',
service: '',
action: 'RecognizeGeneral',
version: '2021-07-07',
timeoutSecs: 30,
},
volcengine: {
label: '火山引擎文字识别',
endpoint: 'https://visual.volcengineapi.com',
accessKeyId: '',
accessKeySecret: '',
region: 'cn-north-1',
service: 'cv',
action: 'OCRNormal',
version: '2020-08-26',
timeoutSecs: 30,
},
},
},
} }
} }
@@ -198,6 +249,15 @@ const activeLLMProvider = computed(() => {
return config.value.llm.providers[id] return config.value.llm.providers[id]
}) })
const activeOCRProvider = computed(() => {
const id = config.value.ocr.activeProvider || 'aliyun'
if (!config.value.ocr.providers[id]) {
config.value.ocr.activeProvider = 'aliyun'
return config.value.ocr.providers.aliyun
}
return config.value.ocr.providers[id]
})
function normalizeTheme(theme: unknown): ThemeMode { function normalizeTheme(theme: unknown): ThemeMode {
return theme === 'light' || theme === 'dark' || theme === 'auto' return theme === 'light' || theme === 'dark' || theme === 'auto'
? theme ? theme
@@ -225,6 +285,11 @@ async function load() {
...defaultConfig().llm.providers, ...defaultConfig().llm.providers,
...((cfg as any).llm?.providers ?? {}), ...((cfg as any).llm?.providers ?? {}),
} }
merged.ocr = { ...merged.ocr, ...(cfg as any).ocr }
merged.ocr.providers = {
...defaultConfig().ocr.providers,
...((cfg as any).ocr?.providers ?? {}),
}
merged.theme = normalizeTheme((cfg as any).theme) merged.theme = normalizeTheme((cfg as any).theme)
config.value = merged config.value = merged
emit('theme-change', config.value.theme) emit('theme-change', config.value.theme)
@@ -586,6 +651,103 @@ onMounted(load)
</div> </div>
</section> </section>
<!-- OCR tab cloud OCR provider settings for screenshot text extraction. -->
<section v-if="props.tab === 'ocr'" class="card">
<h2>文字提取</h2>
<div class="grid">
<label class="field full">
<span>Provider</span>
<select v-model="config.ocr.activeProvider">
<option
v-for="provider in ocrProviderOptions"
:key="provider.id"
:value="provider.id"
>
{{ provider.label }}
</option>
</select>
</label>
<label class="field full">
<span>Endpoint *</span>
<input
v-model="activeOCRProvider.endpoint"
placeholder="https://ocr-api.cn-hangzhou.aliyuncs.com"
spellcheck="false"
/>
</label>
<label class="field">
<span>AccessKey ID *</span>
<input v-model="activeOCRProvider.accessKeyId" spellcheck="false" />
</label>
<label class="field">
<span>AccessKey Secret *</span>
<input v-model="activeOCRProvider.accessKeySecret" type="password" />
</label>
<label
v-if="config.ocr.activeProvider === 'volcengine'"
class="field"
>
<span>Region *</span>
<input
v-model="activeOCRProvider.region"
placeholder="cn-north-1"
spellcheck="false"
/>
</label>
<label
v-if="config.ocr.activeProvider === 'volcengine'"
class="field"
>
<span>Service *</span>
<input
v-model="activeOCRProvider.service"
placeholder="cv"
spellcheck="false"
/>
</label>
<label class="field">
<span>Action *</span>
<input
v-model="activeOCRProvider.action"
placeholder="RecognizeGeneral / OCRNormal"
spellcheck="false"
/>
</label>
<label class="field">
<span>Version *</span>
<input
v-model="activeOCRProvider.version"
placeholder="2021-07-07 / 2020-08-26"
spellcheck="false"
/>
</label>
<label class="field">
<span>Timeout (sec)</span>
<input
v-model.number="activeOCRProvider.timeoutSecs"
type="number"
min="5"
max="120"
placeholder="30"
/>
</label>
</div>
<p v-if="config.ocr.activeProvider === 'aliyun'" class="hint">
阿里云默认使用 OCR API <code>RecognizeGeneral</code>请求体为截图
PNG 原始数据并使用 ACS3-HMAC-SHA256 签名
</p>
<p v-else class="hint">
火山引擎默认使用视觉智能的 <code>OCRNormal</code>截图以
<code>image_base64</code> 发送并使用 Region/Service 参与签名
</p>
<div class="actions">
<button class="btn primary" :disabled="saving" @click="save">
{{ saving ? 'Saving' : 'Save' }}
</button>
</div>
</section>
<transition name="fade"> <transition name="fade">
<div <div
v-if="message" v-if="message"
+4
View File
@@ -17,6 +17,10 @@ export function CopyNativeRegionImage(arg1:main.CaptureResult):Promise<void>;
export function CopyRegionImage(arg1:main.CaptureResult):Promise<void>; export function CopyRegionImage(arg1:main.CaptureResult):Promise<void>;
export function ExtractTextNativeRegion(arg1:main.CaptureResult):Promise<void>;
export function ExtractTextRegion(arg1:main.CaptureResult):Promise<void>;
export function GetConfig():Promise<domain.AppConfig>; export function GetConfig():Promise<domain.AppConfig>;
export function QuitApp():Promise<void>; export function QuitApp():Promise<void>;
+8
View File
@@ -30,6 +30,14 @@ export function CopyRegionImage(arg1) {
return window['go']['main']['App']['CopyRegionImage'](arg1); return window['go']['main']['App']['CopyRegionImage'](arg1);
} }
export function ExtractTextNativeRegion(arg1) {
return window['go']['main']['App']['ExtractTextNativeRegion'](arg1);
}
export function ExtractTextRegion(arg1) {
return window['go']['main']['App']['ExtractTextRegion'](arg1);
}
export function GetConfig() { export function GetConfig() {
return window['go']['main']['App']['GetConfig'](); return window['go']['main']['App']['GetConfig']();
} }
+66
View File
@@ -18,6 +18,7 @@ export namespace application {
tool: string; tool: string;
color: string; color: string;
points: Point[]; points: Point[];
text?: string;
static createFrom(source: any = {}) { static createFrom(source: any = {}) {
return new Annotation(source); return new Annotation(source);
@@ -28,6 +29,7 @@ export namespace application {
this.tool = source["tool"]; this.tool = source["tool"];
this.color = source["color"]; this.color = source["color"];
this.points = this.convertValues(source["points"], Point); this.points = this.convertValues(source["points"], Point);
this.text = source["text"];
} }
convertValues(a: any, classs: any, asMap: boolean = false): any { convertValues(a: any, classs: any, asMap: boolean = false): any {
@@ -53,6 +55,66 @@ export namespace application {
export namespace domain { export namespace domain {
export class OCRProviderConfig {
label: string;
endpoint: string;
accessKeyId: string;
accessKeySecret: string;
region: string;
service: string;
action: string;
version: string;
timeoutSecs: number;
static createFrom(source: any = {}) {
return new OCRProviderConfig(source);
}
constructor(source: any = {}) {
if ('string' === typeof source) source = JSON.parse(source);
this.label = source["label"];
this.endpoint = source["endpoint"];
this.accessKeyId = source["accessKeyId"];
this.accessKeySecret = source["accessKeySecret"];
this.region = source["region"];
this.service = source["service"];
this.action = source["action"];
this.version = source["version"];
this.timeoutSecs = source["timeoutSecs"];
}
}
export class OCRConfig {
activeProvider: string;
providers: Record<string, OCRProviderConfig>;
static createFrom(source: any = {}) {
return new OCRConfig(source);
}
constructor(source: any = {}) {
if ('string' === typeof source) source = JSON.parse(source);
this.activeProvider = source["activeProvider"];
this.providers = this.convertValues(source["providers"], OCRProviderConfig, true);
}
convertValues(a: any, classs: any, asMap: boolean = false): any {
if (!a) {
return a;
}
if (a.slice && a.map) {
return (a as any[]).map(elem => this.convertValues(elem, classs));
} else if ("object" === typeof a) {
if (asMap) {
for (const key of Object.keys(a)) {
a[key] = new classs(a[key]);
}
return a;
}
return new classs(a);
}
return a;
}
}
export class LLMProviderConfig { export class LLMProviderConfig {
label: string; label: string;
baseUrl: string; baseUrl: string;
@@ -173,6 +235,7 @@ export namespace domain {
s3: S3Config; s3: S3Config;
ssh: SSHConfig; ssh: SSHConfig;
llm: LLMConfig; llm: LLMConfig;
ocr: OCRConfig;
static createFrom(source: any = {}) { static createFrom(source: any = {}) {
return new AppConfig(source); return new AppConfig(source);
@@ -185,6 +248,7 @@ export namespace domain {
this.s3 = this.convertValues(source["s3"], S3Config); this.s3 = this.convertValues(source["s3"], S3Config);
this.ssh = this.convertValues(source["ssh"], SSHConfig); this.ssh = this.convertValues(source["ssh"], SSHConfig);
this.llm = this.convertValues(source["llm"], LLMConfig); this.llm = this.convertValues(source["llm"], LLMConfig);
this.ocr = this.convertValues(source["ocr"], OCRConfig);
} }
convertValues(a: any, classs: any, asMap: boolean = false): any { convertValues(a: any, classs: any, asMap: boolean = false): any {
@@ -209,6 +273,8 @@ export namespace domain {
} }
export namespace main { export namespace main {
+1 -1
View File
@@ -12,6 +12,7 @@ require (
golang.design/x/clipboard v0.7.0 golang.design/x/clipboard v0.7.0
golang.design/x/hotkey v0.4.1 golang.design/x/hotkey v0.4.1
golang.org/x/crypto v0.33.0 golang.org/x/crypto v0.33.0
golang.org/x/image v0.12.0
) )
require ( require (
@@ -52,7 +53,6 @@ require (
github.com/wailsapp/go-webview2 v1.0.22 // indirect github.com/wailsapp/go-webview2 v1.0.22 // indirect
github.com/wailsapp/mimetype v1.4.1 // indirect github.com/wailsapp/mimetype v1.4.1 // indirect
golang.org/x/exp v0.0.0-20190731235908-ec7cb31e5a56 // indirect golang.org/x/exp v0.0.0-20190731235908-ec7cb31e5a56 // indirect
golang.org/x/image v0.12.0 // indirect
golang.org/x/mobile v0.0.0-20230301163155-e0f57694e12c // indirect golang.org/x/mobile v0.0.0-20230301163155-e0f57694e12c // indirect
golang.org/x/net v0.35.0 // indirect golang.org/x/net v0.35.0 // indirect
golang.org/x/sys v0.30.0 // indirect golang.org/x/sys v0.30.0 // indirect
+97
View File
@@ -8,8 +8,15 @@ import (
"image/draw" "image/draw"
"image/png" "image/png"
"math" "math"
"os"
"strconv" "strconv"
"strings" "strings"
"sync"
xfont "golang.org/x/image/font"
"golang.org/x/image/font/basicfont"
"golang.org/x/image/font/opentype"
"golang.org/x/image/math/fixed"
) )
// Annotation describes a user-drawn mark relative to the selected screenshot. // Annotation describes a user-drawn mark relative to the selected screenshot.
@@ -18,6 +25,7 @@ type Annotation struct {
Tool string `json:"tool"` Tool string `json:"tool"`
Color string `json:"color"` Color string `json:"color"`
Points []Point `json:"points"` Points []Point `json:"points"`
Text string `json:"text,omitempty"`
} }
type Point struct { type Point struct {
@@ -55,6 +63,8 @@ func ApplyAnnotations(pngBytes []byte, annotations []Annotation, scale float64)
drawRectOutline(dst, ann.Points, scale, width, c) drawRectOutline(dst, ann.Points, scale, width, c)
case "ellipse": case "ellipse":
drawEllipseOutline(dst, ann.Points, scale, width, c) drawEllipseOutline(dst, ann.Points, scale, width, c)
case "text":
drawTextAnnotation(dst, ann, scale, c)
} }
} }
@@ -180,3 +190,90 @@ func ordered(a, b float64) (float64, float64) {
} }
return b, a return b, a
} }
func drawTextAnnotation(img *image.RGBA, ann Annotation, scale float64, c color.RGBA) {
if len(ann.Points) == 0 {
return
}
text := strings.TrimSpace(ann.Text)
if text == "" {
return
}
face := annotationFontFace(20 * scale)
if face == nil {
face = basicfont.Face7x13
}
origin := scalePoint(ann.Points[0], scale)
metrics := face.Metrics()
lineHeight := metrics.Height
if lineHeight <= 0 {
lineHeight = fixed.I(int(math.Ceil(24 * scale)))
}
d := &xfont.Drawer{
Dst: img,
Src: image.NewUniform(c),
Face: face,
}
baselineY := fixed.I(int(math.Round(origin.Y))) + metrics.Ascent
x := fixed.I(int(math.Round(origin.X)))
for _, line := range strings.Split(text, "\n") {
line = strings.TrimRight(line, "\r")
if line != "" {
d.Dot = fixed.Point26_6{X: x, Y: baselineY}
d.DrawString(line)
}
baselineY += lineHeight
}
}
var (
annotationFontOnce sync.Once
annotationFont *opentype.Font
)
func annotationFontFace(size float64) xfont.Face {
if size <= 0 {
size = 20
}
annotationFontOnce.Do(func() {
annotationFont = loadAnnotationFont()
})
if annotationFont == nil {
return basicfont.Face7x13
}
face, err := opentype.NewFace(annotationFont, &opentype.FaceOptions{
Size: size,
DPI: 72,
Hinting: xfont.HintingFull,
})
if err != nil {
return basicfont.Face7x13
}
return face
}
func loadAnnotationFont() *opentype.Font {
paths := []string{
"/System/Library/Fonts/Supplemental/Arial Unicode.ttf",
"/Library/Fonts/Arial Unicode.ttf",
"/System/Library/Fonts/Hiragino Sans GB.ttc",
"/System/Library/Fonts/PingFang.ttc",
"/System/Library/Fonts/Helvetica.ttc",
}
for _, path := range paths {
data, err := os.ReadFile(path)
if err != nil {
continue
}
if collection, err := opentype.ParseCollection(data); err == nil && collection.NumFonts() > 0 {
if font, err := collection.Font(0); err == nil {
return font
}
}
if font, err := opentype.Parse(data); err == nil {
return font
}
}
return nil
}
+42
View File
@@ -51,3 +51,45 @@ func TestApplyAnnotationsNoAnnotationsReturnsOriginalBytes(t *testing.T) {
t.Fatalf("expected original bytes") t.Fatalf("expected original bytes")
} }
} }
func TestApplyAnnotationsDrawsTextIntoPNG(t *testing.T) {
src := image.NewRGBA(image.Rect(0, 0, 120, 80))
draw.Draw(src, src.Bounds(), &image.Uniform{C: color.White}, image.Point{}, draw.Src)
var buf bytes.Buffer
if err := png.Encode(&buf, src); err != nil {
t.Fatalf("encode source: %v", err)
}
out, err := ApplyAnnotations(buf.Bytes(), []Annotation{
{
Tool: "text",
Color: "#ef4444",
Points: []Point{
{X: 10, Y: 10},
},
Text: "T",
},
}, 1)
if err != nil {
t.Fatalf("apply annotations: %v", err)
}
img, err := png.Decode(bytes.NewReader(out))
if err != nil {
t.Fatalf("decode output: %v", err)
}
found := false
for y := 8; y < 40 && !found; y++ {
for x := 8; x < 40; x++ {
got := color.RGBAModel.Convert(img.At(x, y)).(color.RGBA)
if got.R > 180 && got.G < 180 && got.B < 180 {
found = true
break
}
}
}
if !found {
t.Fatalf("expected red text pixels near annotation point")
}
}
+57
View File
@@ -0,0 +1,57 @@
package application
import (
"context"
"fmt"
"strings"
"github.com/mmmy/snapgo/internal/infrastructure/clipboard"
)
// OCRRecognizer describes a provider capable of extracting text from a PNG
// screenshot.
type OCRRecognizer interface {
RecognizeText(ctx context.Context, pngBytes []byte) (string, error)
}
// CaptureOCRService wires screenshot bytes -> OCR provider -> clipboard. It
// leaves progress reporting to the caller so native and web overlays can share
// the same status HUD.
type CaptureOCRService struct {
Recognizer OCRRecognizer
Clipboard clipboard.Writer
}
// Recognize asks the configured OCR provider to extract text from the PNG.
func (s *CaptureOCRService) Recognize(ctx context.Context, pngBytes []byte) (string, error) {
if s.Recognizer == nil {
return "", fmt.Errorf("ocr is not configured")
}
if len(pngBytes) == 0 {
return "", fmt.Errorf("empty screenshot")
}
text, err := s.Recognizer.RecognizeText(ctx, pngBytes)
if err != nil {
return "", err
}
text = strings.TrimSpace(text)
if text == "" {
return "", fmt.Errorf("ocr result is empty")
}
return text, nil
}
// CopyText writes the extracted text to the clipboard.
func (s *CaptureOCRService) CopyText(_ context.Context, text string) error {
text = strings.TrimSpace(text)
if text == "" {
return fmt.Errorf("empty ocr text")
}
if s.Clipboard == nil {
return fmt.Errorf("clipboard is not configured")
}
if err := s.Clipboard.WriteText(text); err != nil {
return fmt.Errorf("clipboard write failed: %w", err)
}
return nil
}
+50
View File
@@ -0,0 +1,50 @@
package application
import (
"context"
"testing"
)
type fakeOCRRecognizer struct {
image []byte
text string
}
func (f *fakeOCRRecognizer) RecognizeText(_ context.Context, pngBytes []byte) (string, error) {
f.image = append([]byte(nil), pngBytes...)
if f.text == "" {
return "extracted text", nil
}
return f.text, nil
}
func TestCaptureOCRServiceRecognizesAndCopies(t *testing.T) {
ctx := context.Background()
recognizer := &fakeOCRRecognizer{}
clip := &fakeClipboard{}
svc := &CaptureOCRService{Recognizer: recognizer, Clipboard: clip}
text, err := svc.Recognize(ctx, []byte("png"))
if err != nil {
t.Fatalf("recognize: %v", err)
}
if string(recognizer.image) != "png" {
t.Fatalf("expected screenshot bytes forwarded, got %q", string(recognizer.image))
}
if text != "extracted text" {
t.Fatalf("expected extracted text, got %q", text)
}
if err := svc.CopyText(ctx, text); err != nil {
t.Fatalf("copy text: %v", err)
}
if clip.text != "extracted text" {
t.Fatalf("expected ocr text copied, got %q", clip.text)
}
}
func TestCaptureOCRServiceRejectsEmptyResult(t *testing.T) {
svc := &CaptureOCRService{Recognizer: &fakeOCRRecognizer{text: " \n "}}
if _, err := svc.Recognize(context.Background(), []byte("png")); err == nil {
t.Fatalf("expected empty OCR result to fail")
}
}
+126
View File
@@ -107,6 +107,29 @@ type LLMConfig struct {
Providers map[string]LLMProviderConfig `json:"providers"` Providers map[string]LLMProviderConfig `json:"providers"`
} }
// OCRProviderConfig stores one cloud OCR endpoint that accepts a screenshot
// image and returns extracted text. Aliyun and Volcengine both authenticate
// with an AccessKey pair, but their signing schemes use slightly different
// endpoint metadata; keeping Region/Service/Action/Version configurable lets
// the presets track official API changes without a schema rewrite.
type OCRProviderConfig struct {
Label string `json:"label"`
Endpoint string `json:"endpoint"`
AccessKeyID string `json:"accessKeyId"`
AccessKeySecret string `json:"accessKeySecret"`
Region string `json:"region"`
Service string `json:"service"`
Action string `json:"action"`
Version string `json:"version"`
TimeoutSecs int `json:"timeoutSecs"`
}
// OCRConfig controls screenshot text extraction.
type OCRConfig struct {
ActiveProvider string `json:"activeProvider"`
Providers map[string]OCRProviderConfig `json:"providers"`
}
// SSH authentication method identifiers stored in SSHConfig.AuthMethod. // SSH authentication method identifiers stored in SSHConfig.AuthMethod.
const ( const (
// SSHAuthBuiltin is the legacy combined method (password → agent → key // SSHAuthBuiltin is the legacy combined method (password → agent → key
@@ -129,6 +152,12 @@ const (
LLMProviderOpenAI = "openai" LLMProviderOpenAI = "openai"
) )
// Built-in OCR provider identifiers.
const (
OCRProviderAliyun = "aliyun"
OCRProviderVolcengine = "volcengine"
)
// Theme preference identifiers stored in AppConfig.Theme. // Theme preference identifiers stored in AppConfig.Theme.
const ( const (
ThemeAuto = "auto" ThemeAuto = "auto"
@@ -172,6 +201,9 @@ type AppConfig struct {
// LLM holds provider settings for the "copy summary" screenshot action. // LLM holds provider settings for the "copy summary" screenshot action.
LLM LLMConfig `json:"llm"` LLM LLMConfig `json:"llm"`
// OCR holds provider settings for the "extract text" screenshot action.
OCR OCRConfig `json:"ocr"`
} }
// DefaultAppConfig returns sane zero-value defaults used on first launch. // DefaultAppConfig returns sane zero-value defaults used on first launch.
@@ -190,6 +222,7 @@ func DefaultAppConfig() AppConfig {
StrictHostKey: false, StrictHostKey: false,
}, },
LLM: DefaultLLMConfig(), LLM: DefaultLLMConfig(),
OCR: DefaultOCRConfig(),
} }
cfg.Normalize() cfg.Normalize()
return cfg return cfg
@@ -234,6 +267,33 @@ func DefaultLLMConfig() LLMConfig {
} }
} }
// DefaultOCRConfig returns the built-in OCR provider presets. Users only need
// to fill AccessKey credentials for the common regions/actions, while advanced
// accounts can still override endpoint metadata from the settings UI.
func DefaultOCRConfig() OCRConfig {
return OCRConfig{
ActiveProvider: OCRProviderAliyun,
Providers: map[string]OCRProviderConfig{
OCRProviderAliyun: {
Label: "阿里云文字识别",
Endpoint: "https://ocr-api.cn-hangzhou.aliyuncs.com",
Action: "RecognizeGeneral",
Version: "2021-07-07",
TimeoutSecs: 30,
},
OCRProviderVolcengine: {
Label: "火山引擎文字识别",
Endpoint: "https://visual.volcengineapi.com",
Region: "cn-north-1",
Service: "cv",
Action: "OCRNormal",
Version: "2020-08-26",
TimeoutSecs: 30,
},
},
}
}
// Normalize fills defaults into configs written by older app versions while // Normalize fills defaults into configs written by older app versions while
// preserving any user-provided provider fields. // preserving any user-provided provider fields.
func (c *AppConfig) Normalize() { func (c *AppConfig) Normalize() {
@@ -295,6 +355,43 @@ func (c *AppConfig) Normalize() {
} }
c.LLM.Providers[id] = current c.LLM.Providers[id] = current
} }
defaultOCR := DefaultOCRConfig()
if c.OCR.ActiveProvider == "" {
c.OCR.ActiveProvider = defaultOCR.ActiveProvider
}
if c.OCR.Providers == nil {
c.OCR.Providers = map[string]OCRProviderConfig{}
}
for id, def := range defaultOCR.Providers {
current, ok := c.OCR.Providers[id]
if !ok {
c.OCR.Providers[id] = def
continue
}
if current.Label == "" {
current.Label = def.Label
}
if current.Endpoint == "" {
current.Endpoint = def.Endpoint
}
if current.Region == "" {
current.Region = def.Region
}
if current.Service == "" {
current.Service = def.Service
}
if current.Action == "" {
current.Action = def.Action
}
if current.Version == "" {
current.Version = def.Version
}
if current.TimeoutSecs == 0 {
current.TimeoutSecs = def.TimeoutSecs
}
c.OCR.Providers[id] = current
}
} }
// IsS3Configured reports whether the user has filled the mandatory S3 fields. // IsS3Configured reports whether the user has filled the mandatory S3 fields.
@@ -329,3 +426,32 @@ func (c AppConfig) IsLLMConfigured() bool {
_, provider, ok := c.ActiveLLMProvider() _, provider, ok := c.ActiveLLMProvider()
return ok && provider.BaseURL != "" && provider.APIKey != "" && provider.Model != "" return ok && provider.BaseURL != "" && provider.APIKey != "" && provider.Model != ""
} }
// ActiveOCRProvider returns the selected OCR provider config plus a boolean
// indicating whether the selection exists.
func (c AppConfig) ActiveOCRProvider() (string, OCRProviderConfig, bool) {
id := c.OCR.ActiveProvider
if id == "" {
id = OCRProviderAliyun
}
provider, ok := c.OCR.Providers[id]
return id, provider, ok
}
// IsOCRConfigured reports whether the selected OCR provider has the fields
// required to sign and send a request.
func (c AppConfig) IsOCRConfigured() bool {
id, provider, ok := c.ActiveOCRProvider()
if !ok ||
provider.Endpoint == "" ||
provider.AccessKeyID == "" ||
provider.AccessKeySecret == "" ||
provider.Action == "" ||
provider.Version == "" {
return false
}
if id == OCRProviderVolcengine && (provider.Region == "" || provider.Service == "") {
return false
}
return true
}
+568
View File
@@ -0,0 +1,568 @@
// Package ocr contains cloud OCR adapters.
package ocr
import (
"bytes"
"context"
"crypto/hmac"
"crypto/rand"
"crypto/sha256"
"encoding/base64"
"encoding/hex"
"encoding/json"
"errors"
"fmt"
"io"
"net/http"
"net/url"
"sort"
"strings"
"time"
"github.com/mmmy/snapgo/internal/application"
"github.com/mmmy/snapgo/internal/domain"
)
// Client calls one configured OCR provider.
type Client struct {
providerID string
cfg domain.OCRProviderConfig
httpClient *http.Client
now func() time.Time
nonce func() string
}
var _ application.OCRRecognizer = (*Client)(nil)
// NewClient validates cfg and returns a reusable OCR client.
func NewClient(providerID string, cfg domain.OCRProviderConfig) (*Client, error) {
if strings.TrimSpace(cfg.Endpoint) == "" {
return nil, fmt.Errorf("ocr endpoint is required")
}
if strings.TrimSpace(cfg.AccessKeyID) == "" {
return nil, fmt.Errorf("ocr access key id is required")
}
if strings.TrimSpace(cfg.AccessKeySecret) == "" {
return nil, fmt.Errorf("ocr access key secret is required")
}
if strings.TrimSpace(cfg.Action) == "" {
return nil, fmt.Errorf("ocr action is required")
}
if strings.TrimSpace(cfg.Version) == "" {
return nil, fmt.Errorf("ocr version is required")
}
switch providerID {
case domain.OCRProviderAliyun:
case domain.OCRProviderVolcengine:
if strings.TrimSpace(cfg.Region) == "" {
return nil, fmt.Errorf("volcengine ocr region is required")
}
if strings.TrimSpace(cfg.Service) == "" {
return nil, fmt.Errorf("volcengine ocr service is required")
}
default:
return nil, fmt.Errorf("unsupported ocr provider %q", providerID)
}
timeout := cfg.TimeoutSecs
if timeout <= 0 {
timeout = 30
}
return &Client{
providerID: providerID,
cfg: cfg,
httpClient: &http.Client{Timeout: time.Duration(timeout) * time.Second},
now: time.Now,
nonce: randomNonce,
}, nil
}
// RecognizeText extracts visible text from a PNG screenshot.
func (c *Client) RecognizeText(ctx context.Context, pngBytes []byte) (string, error) {
if len(pngBytes) == 0 {
return "", fmt.Errorf("empty screenshot")
}
switch c.providerID {
case domain.OCRProviderAliyun:
return c.recognizeAliyun(ctx, pngBytes)
case domain.OCRProviderVolcengine:
return c.recognizeVolcengine(ctx, pngBytes)
default:
return "", fmt.Errorf("unsupported ocr provider %q", c.providerID)
}
}
func (c *Client) recognizeAliyun(ctx context.Context, pngBytes []byte) (string, error) {
endpoint, err := parseEndpoint(c.cfg.Endpoint)
if err != nil {
return "", err
}
req, err := http.NewRequestWithContext(ctx, http.MethodPost, endpoint.String(), bytes.NewReader(pngBytes))
if err != nil {
return "", fmt.Errorf("build aliyun ocr request: %w", err)
}
req.Header.Set("Accept", "application/json")
req.Header.Set("Content-Type", "application/octet-stream")
c.signAliyun(req, pngBytes)
return c.doOCRRequest(req, "aliyun ocr")
}
func (c *Client) recognizeVolcengine(ctx context.Context, pngBytes []byte) (string, error) {
endpoint, err := parseEndpoint(c.cfg.Endpoint)
if err != nil {
return "", err
}
query := endpoint.Query()
query.Set("Action", c.cfg.Action)
query.Set("Version", c.cfg.Version)
endpoint.RawQuery = query.Encode()
body := map[string]string{
"image_base64": base64.StdEncoding.EncodeToString(pngBytes),
}
payload, err := json.Marshal(body)
if err != nil {
return "", fmt.Errorf("marshal volcengine ocr request: %w", err)
}
req, err := http.NewRequestWithContext(ctx, http.MethodPost, endpoint.String(), bytes.NewReader(payload))
if err != nil {
return "", fmt.Errorf("build volcengine ocr request: %w", err)
}
req.Header.Set("Accept", "application/json")
req.Header.Set("Content-Type", "application/json")
c.signVolcengine(req, payload)
return c.doOCRRequest(req, "volcengine ocr")
}
func (c *Client) doOCRRequest(req *http.Request, label string) (string, error) {
resp, err := c.httpClient.Do(req)
if err != nil {
return "", fmt.Errorf("%s request: %w", label, err)
}
defer resp.Body.Close()
data, _ := io.ReadAll(io.LimitReader(resp.Body, 8<<20))
if resp.StatusCode < 200 || resp.StatusCode >= 300 {
return "", fmt.Errorf("%s status %d: %s", label, resp.StatusCode, compactBody(data))
}
text, err := parseOCRText(data)
if err != nil {
return "", fmt.Errorf("%s response: %w", label, err)
}
return text, nil
}
func (c *Client) signAliyun(req *http.Request, payload []byte) {
now := c.now().UTC()
payloadHash := sha256Hex(payload)
nonce := c.nonce()
req.Header.Set("x-acs-action", c.cfg.Action)
req.Header.Set("x-acs-version", c.cfg.Version)
req.Header.Set("x-acs-date", now.Format("2006-01-02T15:04:05Z"))
req.Header.Set("x-acs-signature-nonce", nonce)
req.Header.Set("x-acs-content-sha256", payloadHash)
signedHeaders := []string{
"content-type",
"host",
"x-acs-action",
"x-acs-content-sha256",
"x-acs-date",
"x-acs-signature-nonce",
"x-acs-version",
}
canonicalHeaders := canonicalHeaders(req, signedHeaders)
canonicalRequest := strings.Join([]string{
req.Method,
canonicalURI(req.URL),
canonicalQuery(req.URL),
canonicalHeaders,
strings.Join(signedHeaders, ";"),
payloadHash,
}, "\n")
stringToSign := "ACS3-HMAC-SHA256\n" + sha256Hex([]byte(canonicalRequest))
signature := hmacSHA256Hex([]byte(c.cfg.AccessKeySecret), []byte(stringToSign))
req.Header.Set(
"Authorization",
fmt.Sprintf("ACS3-HMAC-SHA256 Credential=%s,SignedHeaders=%s,Signature=%s",
c.cfg.AccessKeyID,
strings.Join(signedHeaders, ";"),
signature,
),
)
}
func (c *Client) signVolcengine(req *http.Request, payload []byte) {
now := c.now().UTC()
xDate := now.Format("20060102T150405Z")
shortDate := now.Format("20060102")
payloadHash := sha256Hex(payload)
req.Header.Set("X-Date", xDate)
req.Header.Set("X-Content-Sha256", payloadHash)
signedHeaders := []string{
"content-type",
"host",
"x-content-sha256",
"x-date",
}
canonicalHeaders := canonicalHeaders(req, signedHeaders)
canonicalRequest := strings.Join([]string{
req.Method,
canonicalURI(req.URL),
canonicalQuery(req.URL),
canonicalHeaders,
strings.Join(signedHeaders, ";"),
payloadHash,
}, "\n")
scope := strings.Join([]string{shortDate, c.cfg.Region, c.cfg.Service, "request"}, "/")
stringToSign := strings.Join([]string{
"HMAC-SHA256",
xDate,
scope,
sha256Hex([]byte(canonicalRequest)),
}, "\n")
signingKey := volcengineSigningKey(c.cfg.AccessKeySecret, shortDate, c.cfg.Region, c.cfg.Service)
signature := hmacSHA256Hex(signingKey, []byte(stringToSign))
req.Header.Set(
"Authorization",
fmt.Sprintf("HMAC-SHA256 Credential=%s/%s, SignedHeaders=%s, Signature=%s",
c.cfg.AccessKeyID,
scope,
strings.Join(signedHeaders, ";"),
signature,
),
)
}
func parseEndpoint(raw string) (*url.URL, error) {
endpoint := strings.TrimSpace(raw)
if endpoint == "" {
return nil, fmt.Errorf("ocr endpoint is required")
}
if !strings.Contains(endpoint, "://") {
endpoint = "https://" + endpoint
}
parsed, err := url.Parse(endpoint)
if err != nil {
return nil, fmt.Errorf("parse ocr endpoint: %w", err)
}
if parsed.Scheme == "" || parsed.Host == "" {
return nil, fmt.Errorf("invalid ocr endpoint %q", raw)
}
if parsed.Path == "" {
parsed.Path = "/"
}
return parsed, nil
}
func parseOCRText(data []byte) (string, error) {
var root any
if err := json.Unmarshal(data, &root); err != nil {
return "", fmt.Errorf("decode json: %w", err)
}
if msg := responseError(root); msg != "" {
return "", errors.New(msg)
}
parts := collectOCRParts(root, "")
if len(parts) == 0 {
return "", fmt.Errorf("no text found")
}
return strings.Join(dedupeNonEmpty(parts), "\n"), nil
}
func responseError(v any) string {
obj, ok := v.(map[string]any)
if !ok {
return ""
}
if meta, ok := obj["ResponseMetadata"].(map[string]any); ok {
if errObj, ok := meta["Error"].(map[string]any); ok {
if msg := stringValue(errObj["Message"]); msg != "" {
return msg
}
if code := stringValue(errObj["Code"]); code != "" {
return code
}
}
}
if errObj, ok := obj["Error"].(map[string]any); ok {
if msg := stringValue(errObj["Message"]); msg != "" {
return msg
}
if msg := stringValue(errObj["message"]); msg != "" {
return msg
}
}
if code, ok := numericCode(obj["code"]); ok && code != 0 && code != 10000 {
if msg := stringValue(obj["message"]); msg != "" {
return msg
}
if msg := stringValue(obj["msg"]); msg != "" {
return msg
}
return fmt.Sprintf("provider code %.0f", code)
}
if code := stringValue(obj["Code"]); code != "" && !isSuccessCode(code) {
if msg := stringValue(obj["Message"]); msg != "" {
return msg
}
return code
}
return ""
}
func collectOCRParts(v any, parentKey string) []string {
switch value := v.(type) {
case string:
text := strings.TrimSpace(value)
if text == "" {
return nil
}
if looksLikeJSON(text) {
var nested any
if err := json.Unmarshal([]byte(text), &nested); err == nil {
return collectOCRParts(nested, parentKey)
}
}
if isTextKey(parentKey) || isContainerTextKey(parentKey) {
return []string{text}
}
return nil
case []any:
parts := make([]string, 0, len(value))
for _, item := range value {
parts = append(parts, collectOCRParts(item, parentKey)...)
}
return parts
case map[string]any:
parts := make([]string, 0)
for _, key := range priorityOCRKeys() {
if child, ok := value[key]; ok {
parts = append(parts, collectOCRParts(child, key)...)
}
}
keys := make([]string, 0, len(value))
for key := range value {
keys = append(keys, key)
}
sort.Strings(keys)
for _, key := range keys {
if isPriorityOCRKey(key) || isMetadataKey(key) {
continue
}
parts = append(parts, collectOCRParts(value[key], key)...)
}
return parts
default:
return nil
}
}
func priorityOCRKeys() []string {
return []string{
"Data",
"data",
"Result",
"result",
"content",
"Content",
"text",
"Text",
"DetectedText",
"detected_text",
"line_text",
"LineText",
"line_texts",
"LineTexts",
"word",
"Word",
"words",
"Words",
"words_result",
"WordsResult",
"prism_wordsInfo",
"prism_words_info",
"ocr_infos",
"OCRInfos",
"items",
"Items",
"blocks",
"Blocks",
"regions",
"Regions",
}
}
func isPriorityOCRKey(key string) bool {
for _, item := range priorityOCRKeys() {
if item == key {
return true
}
}
return false
}
func isTextKey(key string) bool {
switch normalizeKey(key) {
case "content", "text", "detectedtext", "linetext", "word", "words", "value":
return true
default:
return false
}
}
func isContainerTextKey(key string) bool {
switch normalizeKey(key) {
case "data", "result", "linetexts", "texts":
return true
default:
return false
}
}
func isMetadataKey(key string) bool {
switch normalizeKey(key) {
case "requestid", "request", "code", "status", "statuscode", "success",
"error", "errors", "message", "msg", "cost", "angle", "probability",
"confidence", "height", "width", "left", "top", "right", "bottom",
"x", "y", "responsemetadata":
return true
default:
return false
}
}
func normalizeKey(key string) string {
key = strings.ToLower(key)
key = strings.ReplaceAll(key, "_", "")
key = strings.ReplaceAll(key, "-", "")
return key
}
func dedupeNonEmpty(parts []string) []string {
seen := map[string]struct{}{}
out := make([]string, 0, len(parts))
for _, part := range parts {
for _, line := range strings.Split(part, "\n") {
line = strings.TrimSpace(line)
if line == "" {
continue
}
if _, ok := seen[line]; ok {
continue
}
seen[line] = struct{}{}
out = append(out, line)
}
}
return out
}
func looksLikeJSON(text string) bool {
return strings.HasPrefix(text, "{") || strings.HasPrefix(text, "[")
}
func stringValue(v any) string {
if s, ok := v.(string); ok {
return strings.TrimSpace(s)
}
return ""
}
func numericCode(v any) (float64, bool) {
switch n := v.(type) {
case float64:
return n, true
case int:
return float64(n), true
case json.Number:
f, err := n.Float64()
return f, err == nil
default:
return 0, false
}
}
func isSuccessCode(code string) bool {
switch strings.ToLower(strings.TrimSpace(code)) {
case "", "ok", "success", "200", "10000":
return true
default:
return false
}
}
func canonicalURI(u *url.URL) string {
if u == nil || u.EscapedPath() == "" {
return "/"
}
return u.EscapedPath()
}
func canonicalQuery(u *url.URL) string {
if u == nil || u.RawQuery == "" {
return ""
}
values, _ := url.ParseQuery(u.RawQuery)
return values.Encode()
}
func canonicalHeaders(req *http.Request, signedHeaders []string) string {
lines := make([]string, 0, len(signedHeaders))
for _, key := range signedHeaders {
var value string
if key == "host" {
value = req.URL.Host
} else {
value = req.Header.Get(key)
}
lines = append(lines, key+":"+normalizeHeaderValue(value)+"\n")
}
return strings.Join(lines, "")
}
func normalizeHeaderValue(value string) string {
return strings.Join(strings.Fields(strings.TrimSpace(value)), " ")
}
func volcengineSigningKey(secret, date, region, service string) []byte {
kDate := hmacSHA256([]byte(secret), []byte(date))
kRegion := hmacSHA256(kDate, []byte(region))
kService := hmacSHA256(kRegion, []byte(service))
return hmacSHA256(kService, []byte("request"))
}
func hmacSHA256(key, data []byte) []byte {
mac := hmac.New(sha256.New, key)
mac.Write(data)
return mac.Sum(nil)
}
func hmacSHA256Hex(key, data []byte) string {
return hex.EncodeToString(hmacSHA256(key, data))
}
func sha256Hex(data []byte) string {
sum := sha256.Sum256(data)
return hex.EncodeToString(sum[:])
}
func randomNonce() string {
buf := make([]byte, 16)
if _, err := rand.Read(buf); err != nil {
return fmt.Sprintf("%d", time.Now().UnixNano())
}
return hex.EncodeToString(buf)
}
func compactBody(data []byte) string {
text := strings.TrimSpace(string(data))
if text == "" {
return "<empty body>"
}
if len(text) > 800 {
return text[:800] + "..."
}
return text
}
+150
View File
@@ -0,0 +1,150 @@
package ocr
import (
"bytes"
"context"
"encoding/base64"
"encoding/json"
"io"
"net/http"
"strings"
"testing"
"time"
"github.com/mmmy/snapgo/internal/domain"
)
type roundTripFunc func(*http.Request) (*http.Response, error)
func (f roundTripFunc) RoundTrip(req *http.Request) (*http.Response, error) {
return f(req)
}
func TestAliyunRecognizeTextSignsRawPNGRequest(t *testing.T) {
var requestBody []byte
transport := roundTripFunc(func(r *http.Request) (*http.Response, error) {
if r.URL.Host != "ocr-api.cn-hangzhou.aliyuncs.com" {
t.Fatalf("unexpected host %s", r.URL.Host)
}
if got := r.Header.Get("x-acs-action"); got != "RecognizeGeneral" {
t.Fatalf("unexpected action %q", got)
}
if got := r.Header.Get("x-acs-version"); got != "2021-07-07" {
t.Fatalf("unexpected version %q", got)
}
if got := r.Header.Get("x-acs-date"); got != "2026-07-08T01:02:03Z" {
t.Fatalf("unexpected date %q", got)
}
if got := r.Header.Get("Authorization"); !strings.Contains(got, "ACS3-HMAC-SHA256 Credential=ak") {
t.Fatalf("unexpected auth header %q", got)
}
var err error
requestBody, err = io.ReadAll(r.Body)
if err != nil {
t.Fatalf("read body: %v", err)
}
return &http.Response{
StatusCode: http.StatusOK,
Header: http.Header{"Content-Type": []string{"application/json"}},
Body: io.NopCloser(bytes.NewBufferString(
`{"Data":"{\"content\":\"第一行\\n第二行\"}"}`,
)),
}, nil
})
client, err := NewClient(domain.OCRProviderAliyun, domain.OCRProviderConfig{
Endpoint: "https://ocr-api.cn-hangzhou.aliyuncs.com",
AccessKeyID: "ak",
AccessKeySecret: "sk",
Action: "RecognizeGeneral",
Version: "2021-07-07",
})
if err != nil {
t.Fatalf("new client: %v", err)
}
client.httpClient.Transport = transport
client.now = func() time.Time { return time.Date(2026, 7, 8, 1, 2, 3, 0, time.UTC) }
client.nonce = func() string { return "nonce" }
text, err := client.RecognizeText(context.Background(), []byte("png"))
if err != nil {
t.Fatalf("recognize text: %v", err)
}
if string(requestBody) != "png" {
t.Fatalf("expected raw png body, got %q", string(requestBody))
}
if text != "第一行\n第二行" {
t.Fatalf("unexpected OCR text %q", text)
}
}
func TestVolcengineRecognizeTextSignsBase64JSONRequest(t *testing.T) {
var requestBody map[string]string
transport := roundTripFunc(func(r *http.Request) (*http.Response, error) {
if got := r.URL.Query().Get("Action"); got != "OCRNormal" {
t.Fatalf("unexpected action %q", got)
}
if got := r.URL.Query().Get("Version"); got != "2020-08-26" {
t.Fatalf("unexpected version %q", got)
}
if got := r.Header.Get("X-Date"); got != "20260708T010203Z" {
t.Fatalf("unexpected x-date %q", got)
}
if got := r.Header.Get("Authorization"); !strings.Contains(got, "HMAC-SHA256 Credential=ak/20260708/cn-north-1/cv/request") {
t.Fatalf("unexpected auth header %q", got)
}
if err := json.NewDecoder(r.Body).Decode(&requestBody); err != nil {
t.Fatalf("decode request: %v", err)
}
return &http.Response{
StatusCode: http.StatusOK,
Header: http.Header{"Content-Type": []string{"application/json"}},
Body: io.NopCloser(bytes.NewBufferString(
`{"ResponseMetadata":{"RequestId":"r"},"Result":{"LineTexts":["你好","世界"]}}`,
)),
}, nil
})
client, err := NewClient(domain.OCRProviderVolcengine, domain.OCRProviderConfig{
Endpoint: "https://visual.volcengineapi.com",
AccessKeyID: "ak",
AccessKeySecret: "sk",
Region: "cn-north-1",
Service: "cv",
Action: "OCRNormal",
Version: "2020-08-26",
})
if err != nil {
t.Fatalf("new client: %v", err)
}
client.httpClient.Transport = transport
client.now = func() time.Time { return time.Date(2026, 7, 8, 1, 2, 3, 0, time.UTC) }
text, err := client.RecognizeText(context.Background(), []byte("png"))
if err != nil {
t.Fatalf("recognize text: %v", err)
}
if requestBody["image_base64"] != base64.StdEncoding.EncodeToString([]byte("png")) {
t.Fatalf("expected base64 image body, got %#v", requestBody)
}
if text != "你好\n世界" {
t.Fatalf("unexpected OCR text %q", text)
}
}
func TestParseOCRTextSupportsWordsResult(t *testing.T) {
text, err := parseOCRText([]byte(`{"data":{"words_result":[{"words":"foo"},{"words":"bar"}]}}`))
if err != nil {
t.Fatalf("parse ocr text: %v", err)
}
if text != "foo\nbar" {
t.Fatalf("unexpected OCR text %q", text)
}
}
func TestParseOCRTextReturnsProviderError(t *testing.T) {
_, err := parseOCRText([]byte(`{"ResponseMetadata":{"Error":{"Code":"BadRequest","Message":"bad image"}}}`))
if err == nil || !strings.Contains(err.Error(), "bad image") {
t.Fatalf("expected provider error, got %v", err)
}
}
+12
View File
@@ -86,6 +86,18 @@ func nativeOverlaySummarize(x, y, w, h C.int, annotationsJSON *C.char) {
}() }()
} }
//export nativeOverlayOCR
func nativeOverlayOCR(x, y, w, h C.int, annotationsJSON *C.char) {
app := consumeNativeOverlayApp()
if app == nil {
return
}
result := nativeCaptureResult(x, y, w, h, annotationsJSON)
go func() {
_ = app.ExtractTextNativeRegion(result)
}()
}
//export nativeOverlayCancel //export nativeOverlayCancel
func nativeOverlayCancel() { func nativeOverlayCancel() {
app := consumeNativeOverlayApp() app := consumeNativeOverlayApp()
+172 -20
View File
@@ -16,6 +16,7 @@ extern void nativeOverlayCopy(int x, int y, int w, int h, const char *annotation
extern void nativeOverlaySave(int x, int y, int w, int h, const char *annotationsJSON, const char *dir); extern void nativeOverlaySave(int x, int y, int w, int h, const char *annotationsJSON, const char *dir);
extern void nativeOverlaySaveRemote(int x, int y, int w, int h, const char *annotationsJSON); extern void nativeOverlaySaveRemote(int x, int y, int w, int h, const char *annotationsJSON);
extern void nativeOverlaySummarize(int x, int y, int w, int h, const char *annotationsJSON); extern void nativeOverlaySummarize(int x, int y, int w, int h, const char *annotationsJSON);
extern void nativeOverlayOCR(int x, int y, int w, int h, const char *annotationsJSON);
extern void nativeOverlayCancel(void); extern void nativeOverlayCancel(void);
static NSWindow *nativeOverlayWindow = nil; static NSWindow *nativeOverlayWindow = nil;
@@ -103,25 +104,35 @@ static id nativeOverlayKeyMonitor = nil;
@property(strong) NSButton *clipboardButton; @property(strong) NSButton *clipboardButton;
@property(strong) NSButton *saveButton; @property(strong) NSButton *saveButton;
@property(strong) NSButton *saveRemoteButton; @property(strong) NSButton *saveRemoteButton;
@property(strong) NSButton *ocrButton;
@property(strong) NSButton *summaryButton; @property(strong) NSButton *summaryButton;
@property(strong) NSButton *uploadButton; @property(strong) NSButton *uploadButton;
@property(strong) NSButton *penButton; @property(strong) NSButton *penButton;
@property(strong) NSButton *rectButton; @property(strong) NSButton *rectButton;
@property(strong) NSButton *ellipseButton; @property(strong) NSButton *ellipseButton;
@property(strong) NSButton *textButton;
@property(strong) NSButton *colorButton; @property(strong) NSButton *colorButton;
@property(strong) NSButton *undoButton; @property(strong) NSButton *undoButton;
@property(strong) NSView *paletteView; @property(strong) NSView *paletteView;
@property(strong) NSView *actionToolbarBg; @property(strong) NSView *actionToolbarBg;
@property(strong) NSTextField *textEditor;
@property NSPoint textEditorLocalPoint;
@property(strong) NSTextField *sizeLabel; @property(strong) NSTextField *sizeLabel;
@property(strong) NSTextField *hintLabel; @property(strong) NSTextField *hintLabel;
- (void)syncControls; - (void)syncControls;
- (void)styleControls; - (void)styleControls;
- (void)selectText;
- (void)confirmSelection; - (void)confirmSelection;
- (void)copySelection; - (void)copySelection;
- (void)saveSelection; - (void)saveSelection;
- (void)saveRemoteSelection; - (void)saveRemoteSelection;
- (void)ocrSelection;
- (void)summarizeSelection; - (void)summarizeSelection;
- (void)cancelSelection; - (void)cancelSelection;
- (void)beginTextAnnotationAtPoint:(NSPoint)localPoint;
- (BOOL)isEditingText;
- (void)commitTextEditor;
- (void)cancelTextEditor;
@end @end
@implementation SnipNativeOverlayView @implementation SnipNativeOverlayView
@@ -138,13 +149,14 @@ static id nativeOverlayKeyMonitor = nil;
_activeColor = [NSColor colorWithCalibratedRed:239.0/255.0 green:68.0/255.0 blue:68.0/255.0 alpha:1.0]; _activeColor = [NSColor colorWithCalibratedRed:239.0/255.0 green:68.0/255.0 blue:68.0/255.0 alpha:1.0];
_annotations = [NSMutableArray array]; _annotations = [NSMutableArray array];
// Use SnipHoverButton for the 5 action buttons so we get hover-color // Use SnipHoverButton for the action buttons so we get hover-color
// animation. Annotation buttons stay as plain NSButton because they // animation. Annotation buttons stay as plain NSButton because they
// already have explicit on/off "active" styling. // already have explicit on/off "active" styling.
_cancelButton = [SnipHoverButton buttonWithTitle:@"" target:self action:@selector(cancelSelection)]; _cancelButton = [SnipHoverButton buttonWithTitle:@"" target:self action:@selector(cancelSelection)];
_clipboardButton = [SnipHoverButton buttonWithTitle:@"" target:self action:@selector(copySelection)]; _clipboardButton = [SnipHoverButton buttonWithTitle:@"" target:self action:@selector(copySelection)];
_saveButton = [SnipHoverButton buttonWithTitle:@"" target:self action:@selector(saveSelection)]; _saveButton = [SnipHoverButton buttonWithTitle:@"" target:self action:@selector(saveSelection)];
_saveRemoteButton = [SnipHoverButton buttonWithTitle:@"" target:self action:@selector(saveRemoteSelection)]; _saveRemoteButton = [SnipHoverButton buttonWithTitle:@"" target:self action:@selector(saveRemoteSelection)];
_ocrButton = [SnipHoverButton buttonWithTitle:@"" target:self action:@selector(ocrSelection)];
_summaryButton = [SnipHoverButton buttonWithTitle:@"" target:self action:@selector(summarizeSelection)]; _summaryButton = [SnipHoverButton buttonWithTitle:@"" target:self action:@selector(summarizeSelection)];
_uploadButton = [SnipHoverButton buttonWithTitle:@"" target:self action:@selector(confirmSelection)]; _uploadButton = [SnipHoverButton buttonWithTitle:@"" target:self action:@selector(confirmSelection)];
// Tooltip strings shown on hover for each action button. // Tooltip strings shown on hover for each action button.
@@ -153,11 +165,13 @@ static id nativeOverlayKeyMonitor = nil;
[_clipboardButton setToolTip:@"复制图片"]; [_clipboardButton setToolTip:@"复制图片"];
[_saveButton setToolTip:@"保存本地"]; [_saveButton setToolTip:@"保存本地"];
[_saveRemoteButton setToolTip:@"保存远端"]; [_saveRemoteButton setToolTip:@"保存远端"];
[_ocrButton setToolTip:@"提取文字"];
[_summaryButton setToolTip:@"复制总结"]; [_summaryButton setToolTip:@"复制总结"];
[_uploadButton setToolTip:@"上传云端"]; [_uploadButton setToolTip:@"上传云端"];
_penButton = [NSButton buttonWithTitle:@"" target:self action:@selector(selectPen)]; _penButton = [NSButton buttonWithTitle:@"" target:self action:@selector(selectPen)];
_rectButton = [NSButton buttonWithTitle:@"" target:self action:@selector(selectRect)]; _rectButton = [NSButton buttonWithTitle:@"" target:self action:@selector(selectRect)];
_ellipseButton = [NSButton buttonWithTitle:@"" target:self action:@selector(selectEllipse)]; _ellipseButton = [NSButton buttonWithTitle:@"" target:self action:@selector(selectEllipse)];
_textButton = [NSButton buttonWithTitle:@"" target:self action:@selector(selectText)];
_colorButton = [NSButton buttonWithTitle:@"" target:self action:@selector(togglePalette)]; _colorButton = [NSButton buttonWithTitle:@"" target:self action:@selector(togglePalette)];
_undoButton = [NSButton buttonWithTitle:@"" target:self action:@selector(undoAnnotation)]; _undoButton = [NSButton buttonWithTitle:@"" target:self action:@selector(undoAnnotation)];
_paletteView = [[NSView alloc] initWithFrame:NSZeroRect]; _paletteView = [[NSView alloc] initWithFrame:NSZeroRect];
@@ -174,10 +188,10 @@ static id nativeOverlayKeyMonitor = nil;
// Add the action toolbar background BEFORE the buttons so it sits // Add the action toolbar background BEFORE the buttons so it sits
// behind them in the view hierarchy (AppKit z-order = subview order). // behind them in the view hierarchy (AppKit z-order = subview order).
[self addSubview:_actionToolbarBg]; [self addSubview:_actionToolbarBg];
for (NSView *view in @[_cancelButton, _clipboardButton, _saveButton, _saveRemoteButton, _summaryButton, _uploadButton, _penButton, _rectButton, _ellipseButton, _colorButton, _undoButton, _paletteView, _sizeLabel, _hintLabel]) { for (NSView *view in @[_cancelButton, _clipboardButton, _saveButton, _saveRemoteButton, _ocrButton, _summaryButton, _uploadButton, _penButton, _rectButton, _ellipseButton, _textButton, _colorButton, _undoButton, _paletteView, _sizeLabel, _hintLabel]) {
[self addSubview:view]; [self addSubview:view];
} }
for (NSView *view in @[_cancelButton, _clipboardButton, _saveButton, _saveRemoteButton, _summaryButton, _uploadButton, _penButton, _rectButton, _ellipseButton, _colorButton, _undoButton, _paletteView, _actionToolbarBg]) { for (NSView *view in @[_cancelButton, _clipboardButton, _saveButton, _saveRemoteButton, _ocrButton, _summaryButton, _uploadButton, _penButton, _rectButton, _ellipseButton, _textButton, _colorButton, _undoButton, _paletteView, _actionToolbarBg]) {
[view setHidden:YES]; [view setHidden:YES];
} }
[_sizeLabel setHidden:YES]; [_sizeLabel setHidden:YES];
@@ -240,8 +254,8 @@ static id nativeOverlayKeyMonitor = nil;
} }
- (void)styleControls { - (void)styleControls {
// Cancel / Copy / Save / Save-remote / Upload — square icon buttons // Cancel / Copy / Save / Save-remote / OCR / Summary / Upload — square icon buttons
// rendered from the user-supplied SVG assets. All five default to white // rendered from the user-supplied SVG assets. All actions default to white
// and animate to a per-action accent color on hover (cancel = red, // and animate to a per-action accent color on hover (cancel = red,
// others = blue). The base "white default" replaces the previous blue // others = blue). The base "white default" replaces the previous blue
// upload tint so the toolbar reads as a uniform icon row. // upload tint so the toolbar reads as a uniform icon row.
@@ -251,6 +265,7 @@ static id nativeOverlayKeyMonitor = nil;
// save-remote.svg — provided by the user; its content is the same arrow // save-remote.svg — provided by the user; its content is the same arrow
// icon as the previous upload, so we reuse it verbatim here. // icon as the previous upload, so we reuse it verbatim here.
NSImage *saveRemoteIcon = [self iconFromSVG:@"<svg viewBox='0 0 1024 1024' xmlns='http://www.w3.org/2000/svg'><path d='M481.749333 353.792c16.682667-16.64 43.690667-16.64 60.373334 0l120.917333 120.832 3.541333 4.010667a42.666667 42.666667 0 0 1-63.872 56.32L554.666667 486.954667V896a42.666667 42.666667 0 0 1-85.333334 0v-409.173333l-48.170666 48.128a42.666667 42.666667 0 0 1-60.330667 0l-3.541333-4.010667a42.666667 42.666667 0 0 1 3.541333-56.32zM512 85.333333c122.538667 0 227.84 73.813333 273.92 179.370667A213.333333 213.333333 0 0 1 725.333333 682.666667a42.666667 42.666667 0 0 1-4.992-85.034667L725.333333 597.333333a128 128 0 0 0 36.394667-250.794666l-38.144-11.264-15.914667-36.437334a213.376 213.376 0 0 0-407.296 58.026667l-7.381333 58.368-57.173333 13.824A85.418667 85.418667 0 0 0 256 597.333333h42.666667a42.666667 42.666667 0 0 1 0 85.333334H256a170.666667 170.666667 0 0 1-40.277333-336.554667A298.709333 298.709333 0 0 1 512 85.333333z' fill='white'/></svg>"]; NSImage *saveRemoteIcon = [self iconFromSVG:@"<svg viewBox='0 0 1024 1024' xmlns='http://www.w3.org/2000/svg'><path d='M481.749333 353.792c16.682667-16.64 43.690667-16.64 60.373334 0l120.917333 120.832 3.541333 4.010667a42.666667 42.666667 0 0 1-63.872 56.32L554.666667 486.954667V896a42.666667 42.666667 0 0 1-85.333334 0v-409.173333l-48.170666 48.128a42.666667 42.666667 0 0 1-60.330667 0l-3.541333-4.010667a42.666667 42.666667 0 0 1 3.541333-56.32zM512 85.333333c122.538667 0 227.84 73.813333 273.92 179.370667A213.333333 213.333333 0 0 1 725.333333 682.666667a42.666667 42.666667 0 0 1-4.992-85.034667L725.333333 597.333333a128 128 0 0 0 36.394667-250.794666l-38.144-11.264-15.914667-36.437334a213.376 213.376 0 0 0-407.296 58.026667l-7.381333 58.368-57.173333 13.824A85.418667 85.418667 0 0 0 256 597.333333h42.666667a42.666667 42.666667 0 0 1 0 85.333334H256a170.666667 170.666667 0 0 1-40.277333-336.554667A298.709333 298.709333 0 0 1 512 85.333333z' fill='white'/></svg>"];
NSImage *ocrIcon = [self iconFromSVG:@"<svg viewBox='0 0 1024 1024' xmlns='http://www.w3.org/2000/svg'><path d='M192 160h192v64H256v128h-64V160zM640 160h192v192h-64V224H640v-64zM256 672v128h128v64H192V672h64zM832 672v192H640v-64h128V672h64zM469.333333 288h85.333334l170.666666 448h-78.933333l-39.253333-106.666667H416.853333L377.6 736H298.666667l170.666666-448zM439.466667 565.333333h144.896L512 368.981333 439.466667 565.333333z' fill='white'/></svg>"];
NSImage *summaryIcon = [self iconFromSVG:@"<svg viewBox='0 0 1024 1024' xmlns='http://www.w3.org/2000/svg'><path d='M256 128h448l192 192v576H256V128z m64 64v640h512V352H672V192H320z m416 45.248V288h50.752L736 237.248zM384 448h384v64H384v-64z m0 128h384v64H384v-64z m0 128h256v64H384v-64zM192 256v704h512v64H128V256h64z' fill='white'/></svg>"]; NSImage *summaryIcon = [self iconFromSVG:@"<svg viewBox='0 0 1024 1024' xmlns='http://www.w3.org/2000/svg'><path d='M256 128h448l192 192v576H256V128z m64 64v640h512V352H672V192H320z m416 45.248V288h50.752L736 237.248zM384 448h384v64H384v-64z m0 128h384v64H384v-64z m0 128h256v64H384v-64zM192 256v704h512v64H128V256h64z' fill='white'/></svg>"];
// upload.svg — newly replaced "send/cloud" icon supplied by the user. // upload.svg — newly replaced "send/cloud" icon supplied by the user.
NSImage *uploadIcon = [self iconFromSVG:@"<svg viewBox='0 0 1024 1024' xmlns='http://www.w3.org/2000/svg'><path d='M550.528 544.128L640 539.776V678.4l178.005333-178.005333L640 322.389333v129.024l-72.618667 10.922667c-155.52 23.424-251.733333 73.557333-312.874666 164.437333 84.906667-50.602667 178.261333-76.928 296.021333-82.645333zM554.666667 629.376c-27.392 1.322667-53.034667 3.968-77.141334 7.808l-8.192 1.365333c-136.704 23.637333-224.810667 87.168-310.954666 176.128-26.154667 27.008-51.882667 59.648-51.882667 59.648-14.848 18.176-24.021333 14.037333-20.352-9.173333 0 0 4.394667-31.402667 11.690667-65.792C144.938667 577.066667 250.581333 423.68 554.666667 377.941333V209.066667a38.4 38.4 0 0 1 65.536-27.136l291.328 291.285333a38.4 38.4 0 0 1 0 54.314667l-291.328 291.285333A38.4 38.4 0 0 1 554.666667 791.637333v-162.261333z' fill='white'/></svg>"]; NSImage *uploadIcon = [self iconFromSVG:@"<svg viewBox='0 0 1024 1024' xmlns='http://www.w3.org/2000/svg'><path d='M550.528 544.128L640 539.776V678.4l178.005333-178.005333L640 322.389333v129.024l-72.618667 10.922667c-155.52 23.424-251.733333 73.557333-312.874666 164.437333 84.906667-50.602667 178.261333-76.928 296.021333-82.645333zM554.666667 629.376c-27.392 1.322667-53.034667 3.968-77.141334 7.808l-8.192 1.365333c-136.704 23.637333-224.810667 87.168-310.954666 176.128-26.154667 27.008-51.882667 59.648-51.882667 59.648-14.848 18.176-24.021333 14.037333-20.352-9.173333 0 0 4.394667-31.402667 11.690667-65.792C144.938667 577.066667 250.581333 423.68 554.666667 377.941333V209.066667a38.4 38.4 0 0 1 65.536-27.136l291.328 291.285333a38.4 38.4 0 0 1 0 54.314667l-291.328 291.285333A38.4 38.4 0 0 1 554.666667 791.637333v-162.261333z' fill='white'/></svg>"];
@@ -259,10 +274,11 @@ static id nativeOverlayKeyMonitor = nil;
[self styleIconButton:_clipboardButton image:copyIcon]; [self styleIconButton:_clipboardButton image:copyIcon];
[self styleIconButton:_saveButton image:saveIcon]; [self styleIconButton:_saveButton image:saveIcon];
[self styleIconButton:_saveRemoteButton image:saveRemoteIcon]; [self styleIconButton:_saveRemoteButton image:saveRemoteIcon];
[self styleIconButton:_ocrButton image:ocrIcon];
[self styleIconButton:_summaryButton image:summaryIcon]; [self styleIconButton:_summaryButton image:summaryIcon];
[self styleIconButton:_uploadButton image:uploadIcon]; [self styleIconButton:_uploadButton image:uploadIcon];
// Configure hover colors for the 5 action buttons. White is the shared // Configure hover colors for the action buttons. White is the shared
// base; cancel turns red on hover and the rest turn blue, providing a // base; cancel turns red on hover and the rest turn blue, providing a
// glance-able cue for destructive vs. constructive actions. // glance-able cue for destructive vs. constructive actions.
NSColor *baseWhite = [NSColor whiteColor]; NSColor *baseWhite = [NSColor whiteColor];
@@ -272,18 +288,21 @@ static id nativeOverlayKeyMonitor = nil;
SnipHoverButton *clipboardHB = (SnipHoverButton *)_clipboardButton; SnipHoverButton *clipboardHB = (SnipHoverButton *)_clipboardButton;
SnipHoverButton *saveHB = (SnipHoverButton *)_saveButton; SnipHoverButton *saveHB = (SnipHoverButton *)_saveButton;
SnipHoverButton *saveRemoteHB = (SnipHoverButton *)_saveRemoteButton; SnipHoverButton *saveRemoteHB = (SnipHoverButton *)_saveRemoteButton;
SnipHoverButton *ocrHB = (SnipHoverButton *)_ocrButton;
SnipHoverButton *summaryHB = (SnipHoverButton *)_summaryButton; SnipHoverButton *summaryHB = (SnipHoverButton *)_summaryButton;
SnipHoverButton *uploadHB = (SnipHoverButton *)_uploadButton; SnipHoverButton *uploadHB = (SnipHoverButton *)_uploadButton;
cancelHB.baseColor = baseWhite; cancelHB.hoverColor = hoverRed; cancelHB.baseColor = baseWhite; cancelHB.hoverColor = hoverRed;
clipboardHB.baseColor = baseWhite; clipboardHB.hoverColor = hoverBlue; clipboardHB.baseColor = baseWhite; clipboardHB.hoverColor = hoverBlue;
saveHB.baseColor = baseWhite; saveHB.hoverColor = hoverBlue; saveHB.baseColor = baseWhite; saveHB.hoverColor = hoverBlue;
saveRemoteHB.baseColor = baseWhite; saveRemoteHB.hoverColor = hoverBlue; saveRemoteHB.baseColor = baseWhite; saveRemoteHB.hoverColor = hoverBlue;
ocrHB.baseColor = baseWhite; ocrHB.hoverColor = hoverBlue;
summaryHB.baseColor = baseWhite; summaryHB.hoverColor = hoverBlue; summaryHB.baseColor = baseWhite; summaryHB.hoverColor = hoverBlue;
uploadHB.baseColor = baseWhite; uploadHB.hoverColor = hoverBlue; uploadHB.baseColor = baseWhite; uploadHB.hoverColor = hoverBlue;
[self styleIconButton:_penButton image:[self iconFromSVG:@"<svg viewBox='0 0 1024 1024' xmlns='http://www.w3.org/2000/svg'><path d='M742.72 752.064c-29.696 28.576-42.976 48.96-42.976 77.12 0 98.4 143.776 142.464 229.728 65.536l-21.344-23.84c-66.976 59.936-176.384 26.4-176.384-41.696 0-16.832 9.28-31.104 33.184-54.08l13.44-12.736c61.024-58.016 75.328-102.72 29.312-179.84-43.84-73.44-96.8-88.288-162.784-50.56-47.232 27.04-68.64 47.456-208.32 190.4-70.624 72.288-153.92 77.088-217.344 26.784-59.712-47.328-79.872-127.36-42.176-188.64 24.512-39.84 62.656-72.896 136.64-124.48 5.952-4.192 27.04-18.816 31.104-21.664 12.16-8.448 21.536-15.104 30.4-21.536 107.552-78.272 140.8-139.136 86.048-219.904-76.992-113.472-202.304-90.016-367.744 61.472l21.6 23.616c153.088-140.224 256.896-159.616 319.68-67.104 41.632 61.408 16.896 106.688-78.4 176.032-8.64 6.304-17.92 12.832-29.888 21.184l-31.136 21.632c-77.504 54.08-117.984 89.184-145.536 133.984-46.784 76.032-22.176 173.664 49.536 230.496 76.224 60.416 177.952 54.56 260.096-29.504 136.16-139.36 158.08-160.224 201.312-184.96 50.656-28.96 84.416-19.52 119.424 39.168 36.896 61.824 27.52 91.392-23.808 140.16 0.096-0.064-10.976 10.368-13.632 12.96z' fill='white'/></svg>"]]; [self styleIconButton:_penButton image:[self iconFromSVG:@"<svg viewBox='0 0 1024 1024' xmlns='http://www.w3.org/2000/svg'><path d='M742.72 752.064c-29.696 28.576-42.976 48.96-42.976 77.12 0 98.4 143.776 142.464 229.728 65.536l-21.344-23.84c-66.976 59.936-176.384 26.4-176.384-41.696 0-16.832 9.28-31.104 33.184-54.08l13.44-12.736c61.024-58.016 75.328-102.72 29.312-179.84-43.84-73.44-96.8-88.288-162.784-50.56-47.232 27.04-68.64 47.456-208.32 190.4-70.624 72.288-153.92 77.088-217.344 26.784-59.712-47.328-79.872-127.36-42.176-188.64 24.512-39.84 62.656-72.896 136.64-124.48 5.952-4.192 27.04-18.816 31.104-21.664 12.16-8.448 21.536-15.104 30.4-21.536 107.552-78.272 140.8-139.136 86.048-219.904-76.992-113.472-202.304-90.016-367.744 61.472l21.6 23.616c153.088-140.224 256.896-159.616 319.68-67.104 41.632 61.408 16.896 106.688-78.4 176.032-8.64 6.304-17.92 12.832-29.888 21.184l-31.136 21.632c-77.504 54.08-117.984 89.184-145.536 133.984-46.784 76.032-22.176 173.664 49.536 230.496 76.224 60.416 177.952 54.56 260.096-29.504 136.16-139.36 158.08-160.224 201.312-184.96 50.656-28.96 84.416-19.52 119.424 39.168 36.896 61.824 27.52 91.392-23.808 140.16 0.096-0.064-10.976 10.368-13.632 12.96z' fill='white'/></svg>"]];
[self styleIconButton:_rectButton image:[self iconFromSVG:@"<svg viewBox='0 0 1024 1024' xmlns='http://www.w3.org/2000/svg'><path d='M96 96h832v832H96V96z m32 32v768h768V128H128z' fill='white'/></svg>"]]; [self styleIconButton:_rectButton image:[self iconFromSVG:@"<svg viewBox='0 0 1024 1024' xmlns='http://www.w3.org/2000/svg'><path d='M96 96h832v832H96V96z m32 32v768h768V128H128z' fill='white'/></svg>"]];
[self styleIconButton:_ellipseButton image:[self iconFromSVG:@"<svg viewBox='0 0 1024 1024' xmlns='http://www.w3.org/2000/svg'><path d='M512 928c229.76 0 416-186.24 416-416S741.76 96 512 96 96 282.24 96 512s186.24 416 416 416z m0-32C299.936 896 128 724.064 128 512S299.936 128 512 128s384 171.936 384 384-171.936 384-384 384z' fill='white'/></svg>"]]; [self styleIconButton:_ellipseButton image:[self iconFromSVG:@"<svg viewBox='0 0 1024 1024' xmlns='http://www.w3.org/2000/svg'><path d='M512 928c229.76 0 416-186.24 416-416S741.76 96 512 96 96 282.24 96 512s186.24 416 416 416z m0-32C299.936 896 128 724.064 128 512S299.936 128 512 128s384 171.936 384 384-171.936 384-384 384z' fill='white'/></svg>"]];
[self styleIconButton:_textButton image:[self iconFromSVG:@"<svg viewBox='0 0 1024 1024' xmlns='http://www.w3.org/2000/svg'><path d='M224 160h576v96H560v608h-96V256H224V160zM384 864h256v64H384v-64z' fill='white'/></svg>"]];
[self styleIconButton:_colorButton image:nil]; [self styleIconButton:_colorButton image:nil];
[self styleIconButton:_undoButton image:[self iconFromSVG:@"<svg viewBox='0 0 1024 1024' xmlns='http://www.w3.org/2000/svg'><g transform='translate(1024 0) scale(-1 1)'><path d='M838.976 288l-169.344-162.56a16 16 0 1 1 22.144-23.04l213.632 204.992-213.312 215.84a16 16 0 0 1-22.784-22.464L847.936 320H454.56c-164.192 0-297.28 128.96-297.28 288s133.088 288 297.28 288H704v32h-242.784C266.88 928 128 784.736 128 608S266.88 288 461.248 288h377.728z' fill='white'/></g></svg>"]]; [self styleIconButton:_undoButton image:[self iconFromSVG:@"<svg viewBox='0 0 1024 1024' xmlns='http://www.w3.org/2000/svg'><g transform='translate(1024 0) scale(-1 1)'><path d='M838.976 288l-169.344-162.56a16 16 0 1 1 22.144-23.04l213.632 204.992-213.312 215.84a16 16 0 0 1-22.784-22.464L847.936 320H454.56c-164.192 0-297.28 128.96-297.28 288s133.088 288 297.28 288H704v32h-242.784C266.88 928 128 784.736 128 608S266.88 288 461.248 288h377.728z' fill='white'/></g></svg>"]];
[_paletteView setWantsLayer:YES]; [_paletteView setWantsLayer:YES];
@@ -366,6 +385,19 @@ static id nativeOverlayKeyMonitor = nil;
if (points.count == 0) { if (points.count == 0) {
continue; continue;
} }
if ([tool isEqualToString:@"text"]) {
NSString *text = item[@"text"];
if (text.length == 0) {
continue;
}
NSPoint p = [points[0] pointValue];
NSDictionary *attrs = @{
NSFontAttributeName: [NSFont systemFontOfSize:20 weight:NSFontWeightSemibold],
NSForegroundColorAttributeName: color ?: [NSColor whiteColor]
};
[text drawAtPoint:NSMakePoint(_selection.origin.x + p.x, _selection.origin.y + p.y) withAttributes:attrs];
continue;
}
[color setStroke]; [color setStroke];
NSBezierPath *path = [NSBezierPath bezierPath]; NSBezierPath *path = [NSBezierPath bezierPath];
[path setLineWidth:3]; [path setLineWidth:3];
@@ -475,9 +507,8 @@ static id nativeOverlayKeyMonitor = nil;
- (void)mouseDown:(NSEvent *)event { - (void)mouseDown:(NSEvent *)event {
NSPoint p = [self convertPoint:[event locationInWindow] fromView:nil]; NSPoint p = [self convertPoint:[event locationInWindow] fromView:nil];
if ([event clickCount] == 2 && _hasSelection && NSPointInRect(p, _selection)) { if (_textEditor != nil) {
[self copySelection]; [self commitTextEditor];
return;
} }
NSString *handle = [self resizeHandleAtPoint:p]; NSString *handle = [self resizeHandleAtPoint:p];
if (handle != nil) { if (handle != nil) {
@@ -487,7 +518,23 @@ static id nativeOverlayKeyMonitor = nil;
_annotating = NO; _annotating = NO;
_resizeHandle = handle; _resizeHandle = handle;
_resizeStart = _selection; _resizeStart = _selection;
} else if (_hasSelection && NSPointInRect(p, _selection) && _activeTool != nil) { [self syncControls];
return;
}
if (_hasSelection && NSPointInRect(p, _selection) && [_activeTool isEqualToString:@"text"]) {
_creating = NO;
_moving = NO;
_resizing = NO;
_annotating = NO;
[self beginTextAnnotationAtPoint:[self localPoint:p]];
[self syncControls];
return;
}
if ([event clickCount] == 2 && _hasSelection && NSPointInRect(p, _selection)) {
[self copySelection];
return;
}
if (_hasSelection && NSPointInRect(p, _selection) && _activeTool != nil) {
_annotating = YES; _annotating = YES;
_creating = NO; _creating = NO;
_moving = NO; _moving = NO;
@@ -579,6 +626,16 @@ static id nativeOverlayKeyMonitor = nil;
} }
- (void)keyDown:(NSEvent *)event { - (void)keyDown:(NSEvent *)event {
if ([self isEditingText]) {
if ([event keyCode] == 53 || [[event charactersIgnoringModifiers] isEqualToString:@"\033"]) {
[self cancelTextEditor];
return;
}
if ([event keyCode] == 36 || [[event charactersIgnoringModifiers] isEqualToString:@"\r"]) {
[self commitTextEditor];
return;
}
}
if ([event keyCode] == 53 || [[event charactersIgnoringModifiers] isEqualToString:@"\033"]) { if ([event keyCode] == 53 || [[event charactersIgnoringModifiers] isEqualToString:@"\033"]) {
[self cancelSelection]; [self cancelSelection];
} else if (([event keyCode] == 36 || [[event charactersIgnoringModifiers] isEqualToString:@"\r"]) && _hasSelection) { } else if (([event keyCode] == 36 || [[event charactersIgnoringModifiers] isEqualToString:@"\r"]) && _hasSelection) {
@@ -598,12 +655,14 @@ static id nativeOverlayKeyMonitor = nil;
[_clipboardButton setHidden:!visible]; [_clipboardButton setHidden:!visible];
[_saveButton setHidden:!visible]; [_saveButton setHidden:!visible];
[_saveRemoteButton setHidden:!visible]; [_saveRemoteButton setHidden:!visible];
[_ocrButton setHidden:!visible];
[_summaryButton setHidden:!visible]; [_summaryButton setHidden:!visible];
[_uploadButton setHidden:!visible]; [_uploadButton setHidden:!visible];
[_actionToolbarBg setHidden:!visible]; [_actionToolbarBg setHidden:!visible];
[_penButton setHidden:!visible]; [_penButton setHidden:!visible];
[_rectButton setHidden:!visible]; [_rectButton setHidden:!visible];
[_ellipseButton setHidden:!visible]; [_ellipseButton setHidden:!visible];
[_textButton setHidden:!visible];
[_colorButton setHidden:!visible]; [_colorButton setHidden:!visible];
[_undoButton setHidden:!visible]; [_undoButton setHidden:!visible];
[_sizeLabel setHidden:!visible]; [_sizeLabel setHidden:!visible];
@@ -616,12 +675,12 @@ static id nativeOverlayKeyMonitor = nil;
[_sizeLabel setStringValue:[NSString stringWithFormat:@"%.0f × %.0f", _selection.size.width, _selection.size.height]]; [_sizeLabel setStringValue:[NSString stringWithFormat:@"%.0f × %.0f", _selection.size.width, _selection.size.height]];
[_sizeLabel setFrame:NSMakeRect(_selection.origin.x, MAX(0, _selection.origin.y - 26), 110, 22)]; [_sizeLabel setFrame:NSMakeRect(_selection.origin.x, MAX(0, _selection.origin.y - 26), 110, 22)];
// Action toolbar layout — 6 icon buttons, each 28x28, separated by 8px, // Action toolbar layout — 7 icon buttons, each 28x28, separated by 8px,
// with 4px outer padding. Total = 6*28 + 5*8 + 2*4 = 216px wide. // with 4px outer padding. Total = 7*28 + 6*8 + 2*4 = 252px wide.
CGFloat actionBtnSize = 28; CGFloat actionBtnSize = 28;
CGFloat actionGap = 8; CGFloat actionGap = 8;
CGFloat actionPad = 4; CGFloat actionPad = 4;
NSInteger actionCount = 6; NSInteger actionCount = 7;
CGFloat toolbarW = actionPad * 2 + actionBtnSize * actionCount + actionGap * (actionCount - 1); CGFloat toolbarW = actionPad * 2 + actionBtnSize * actionCount + actionGap * (actionCount - 1);
CGFloat toolbarH = 40; CGFloat toolbarH = 40;
CGFloat x = _selection.origin.x + _selection.size.width - toolbarW; CGFloat x = _selection.origin.x + _selection.size.width - toolbarW;
@@ -639,19 +698,21 @@ static id nativeOverlayKeyMonitor = nil;
[_clipboardButton setFrame:NSMakeRect(baseX + (actionBtnSize + actionGap) * 1, btnY, actionBtnSize, actionBtnSize)]; [_clipboardButton setFrame:NSMakeRect(baseX + (actionBtnSize + actionGap) * 1, btnY, actionBtnSize, actionBtnSize)];
[_saveButton setFrame:NSMakeRect(baseX + (actionBtnSize + actionGap) * 2, btnY, actionBtnSize, actionBtnSize)]; [_saveButton setFrame:NSMakeRect(baseX + (actionBtnSize + actionGap) * 2, btnY, actionBtnSize, actionBtnSize)];
[_saveRemoteButton setFrame:NSMakeRect(baseX + (actionBtnSize + actionGap) * 3, btnY, actionBtnSize, actionBtnSize)]; [_saveRemoteButton setFrame:NSMakeRect(baseX + (actionBtnSize + actionGap) * 3, btnY, actionBtnSize, actionBtnSize)];
[_summaryButton setFrame:NSMakeRect(baseX + (actionBtnSize + actionGap) * 4, btnY, actionBtnSize, actionBtnSize)]; [_ocrButton setFrame:NSMakeRect(baseX + (actionBtnSize + actionGap) * 4, btnY, actionBtnSize, actionBtnSize)];
[_uploadButton setFrame:NSMakeRect(baseX + (actionBtnSize + actionGap) * 5, btnY, actionBtnSize, actionBtnSize)]; [_summaryButton setFrame:NSMakeRect(baseX + (actionBtnSize + actionGap) * 5, btnY, actionBtnSize, actionBtnSize)];
[_uploadButton setFrame:NSMakeRect(baseX + (actionBtnSize + actionGap) * 6, btnY, actionBtnSize, actionBtnSize)];
// Mark toolbar sits to the LEFT of the action toolbar (same row) with an // Mark toolbar sits to the LEFT of the action toolbar (same row) with an
// 8px gap between the two groups, so they never overlap on tiny selections. // 8px gap between the two groups, so they never overlap on tiny selections.
CGFloat markW = 170; CGFloat markW = 202;
CGFloat markGap = 8; CGFloat markGap = 8;
CGFloat markX = MAX(0, x - markGap - markW); CGFloat markX = MAX(0, x - markGap - markW);
[_penButton setFrame:NSMakeRect(markX + 4, y + 4, 28, 28)]; [_penButton setFrame:NSMakeRect(markX + 4, y + 4, 28, 28)];
[_rectButton setFrame:NSMakeRect(markX + 36, y + 4, 28, 28)]; [_rectButton setFrame:NSMakeRect(markX + 36, y + 4, 28, 28)];
[_ellipseButton setFrame:NSMakeRect(markX + 68, y + 4, 28, 28)]; [_ellipseButton setFrame:NSMakeRect(markX + 68, y + 4, 28, 28)];
[_colorButton setFrame:NSMakeRect(markX + 104, y + 7, 22, 22)]; [_textButton setFrame:NSMakeRect(markX + 100, y + 4, 28, 28)];
[_undoButton setFrame:NSMakeRect(markX + 134, y + 4, 28, 28)]; [_colorButton setFrame:NSMakeRect(markX + 136, y + 7, 22, 22)];
[_undoButton setFrame:NSMakeRect(markX + 166, y + 4, 28, 28)];
[[_colorButton layer] setBackgroundColor:[_activeColor CGColor]]; [[_colorButton layer] setBackgroundColor:[_activeColor CGColor]];
[_undoButton setEnabled:_annotations.count > 0]; [_undoButton setEnabled:_annotations.count > 0];
[_paletteView setFrame:NSMakeRect(markX, MAX(0, y - 78), 150, 70)]; [_paletteView setFrame:NSMakeRect(markX, MAX(0, y - 78), 150, 70)];
@@ -659,7 +720,7 @@ static id nativeOverlayKeyMonitor = nil;
} }
- (void)updateToolButtonStates { - (void)updateToolButtonStates {
NSDictionary *buttons = @{@"pen": _penButton, @"rect": _rectButton, @"ellipse": _ellipseButton}; NSDictionary *buttons = @{@"pen": _penButton, @"rect": _rectButton, @"ellipse": _ellipseButton, @"text": _textButton};
for (NSString *tool in buttons) { for (NSString *tool in buttons) {
NSButton *button = buttons[tool]; NSButton *button = buttons[tool];
NSColor *bg = [tool isEqualToString:_activeTool] NSColor *bg = [tool isEqualToString:_activeTool]
@@ -676,7 +737,9 @@ static id nativeOverlayKeyMonitor = nil;
- (void)selectPen { [self toggleTool:@"pen"]; } - (void)selectPen { [self toggleTool:@"pen"]; }
- (void)selectRect { [self toggleTool:@"rect"]; } - (void)selectRect { [self toggleTool:@"rect"]; }
- (void)selectEllipse { [self toggleTool:@"ellipse"]; } - (void)selectEllipse { [self toggleTool:@"ellipse"]; }
- (void)selectText { [self toggleTool:@"text"]; }
- (void)undoAnnotation { - (void)undoAnnotation {
[self cancelTextEditor];
if (_annotations.count > 0) { if (_annotations.count > 0) {
[_annotations removeLastObject]; [_annotations removeLastObject];
[self syncControls]; [self syncControls];
@@ -684,6 +747,68 @@ static id nativeOverlayKeyMonitor = nil;
} }
} }
- (void)beginTextAnnotationAtPoint:(NSPoint)localPoint {
[self cancelTextEditor];
_textEditorLocalPoint = localPoint;
NSRect frame = NSMakeRect(_selection.origin.x + localPoint.x, _selection.origin.y + localPoint.y, 180, 30);
CGFloat maxX = self.bounds.size.width - frame.size.width - 8;
CGFloat maxY = self.bounds.size.height - frame.size.height - 8;
frame.origin.x = MAX(8, MIN(frame.origin.x, maxX));
frame.origin.y = MAX(8, MIN(frame.origin.y, maxY));
_textEditor = [[NSTextField alloc] initWithFrame:frame];
[_textEditor setBezeled:YES];
[_textEditor setBordered:YES];
[_textEditor setDrawsBackground:YES];
[_textEditor setBackgroundColor:[NSColor colorWithCalibratedWhite:0.08 alpha:0.78]];
[_textEditor setTextColor:_activeColor];
[_textEditor setFont:[NSFont systemFontOfSize:20 weight:NSFontWeightSemibold]];
[_textEditor setFocusRingType:NSFocusRingTypeNone];
[_textEditor setTarget:self];
[_textEditor setAction:@selector(commitTextEditorFromSender:)];
[self addSubview:_textEditor];
[[self window] makeFirstResponder:_textEditor];
}
- (BOOL)isEditingText {
return _textEditor != nil;
}
- (void)commitTextEditorFromSender:(id)sender {
[self commitTextEditor];
}
- (void)commitTextEditor {
if (_textEditor == nil) {
return;
}
NSString *text = [[_textEditor stringValue] stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
if (text.length > 0) {
[_annotations addObject:[@{
@"tool": @"text",
@"color": [self hexForColor:_activeColor],
@"nsColor": _activeColor,
@"points": [NSMutableArray arrayWithObject:[NSValue valueWithPoint:_textEditorLocalPoint]],
@"text": text
} mutableCopy]];
}
[_textEditor removeFromSuperview];
_textEditor = nil;
[[self window] makeFirstResponder:self];
[self syncControls];
[self setNeedsDisplay:YES];
}
- (void)cancelTextEditor {
if (_textEditor == nil) {
return;
}
[_textEditor removeFromSuperview];
_textEditor = nil;
[[self window] makeFirstResponder:self];
[self setNeedsDisplay:YES];
}
- (void)togglePalette { - (void)togglePalette {
[_paletteView setHidden:![_paletteView isHidden]]; [_paletteView setHidden:![_paletteView isHidden]];
if (![_paletteView isHidden] && _paletteView.subviews.count == 0) { if (![_paletteView isHidden] && _paletteView.subviews.count == 0) {
@@ -722,6 +847,7 @@ static id nativeOverlayKeyMonitor = nil;
} }
- (NSString *)annotationsJSON { - (NSString *)annotationsJSON {
[self commitTextEditor];
NSMutableArray *payload = [NSMutableArray array]; NSMutableArray *payload = [NSMutableArray array];
for (NSDictionary *item in _annotations) { for (NSDictionary *item in _annotations) {
NSMutableArray *points = [NSMutableArray array]; NSMutableArray *points = [NSMutableArray array];
@@ -729,7 +855,12 @@ static id nativeOverlayKeyMonitor = nil;
NSPoint p = [value pointValue]; NSPoint p = [value pointValue];
[points addObject:@{@"x": @(p.x), @"y": @(p.y)}]; [points addObject:@{@"x": @(p.x), @"y": @(p.y)}];
} }
[payload addObject:@{@"tool": item[@"tool"], @"color": item[@"color"], @"points": points}]; NSMutableDictionary *entry = [@{@"tool": item[@"tool"], @"color": item[@"color"], @"points": points} mutableCopy];
NSString *text = item[@"text"];
if (text.length > 0) {
entry[@"text"] = text;
}
[payload addObject:entry];
} }
NSData *data = [NSJSONSerialization dataWithJSONObject:payload options:0 error:nil]; NSData *data = [NSJSONSerialization dataWithJSONObject:payload options:0 error:nil];
if (data == nil) { if (data == nil) {
@@ -792,6 +923,7 @@ static id nativeOverlayKeyMonitor = nil;
} }
- (void)cancelSelection { - (void)cancelSelection {
[self cancelTextEditor];
[self closeOverlayWindow]; [self closeOverlayWindow];
nativeOverlayCancel(); nativeOverlayCancel();
} }
@@ -809,6 +941,18 @@ static id nativeOverlayKeyMonitor = nil;
nativeOverlaySaveRemote((int)llround(r.origin.x), (int)llround(r.origin.y), (int)llround(r.size.width), (int)llround(r.size.height), [json UTF8String]); nativeOverlaySaveRemote((int)llround(r.origin.x), (int)llround(r.origin.y), (int)llround(r.size.width), (int)llround(r.size.height), [json UTF8String]);
} }
// `ocrSelection` sends the screenshot to the configured OCR provider and
// copies the extracted text.
- (void)ocrSelection {
if (!_hasSelection) {
return;
}
NSRect r = [self globalRectForSelection:_selection];
[self closeOverlayWindow];
NSString *json = [self annotationsJSON];
nativeOverlayOCR((int)llround(r.origin.x), (int)llround(r.origin.y), (int)llround(r.size.width), (int)llround(r.size.height), [json UTF8String]);
}
// `summarizeSelection` uploads the screenshot to S3, sends it to the // `summarizeSelection` uploads the screenshot to S3, sends it to the
// configured multimodal model, and copies the generated summary. // configured multimodal model, and copies the generated summary.
- (void)summarizeSelection { - (void)summarizeSelection {
@@ -880,10 +1024,18 @@ static void snipShowNativeOverlay(int width, int height) {
nativeOverlayKeyMonitor = [NSEvent addLocalMonitorForEventsMatchingMask:NSEventMaskKeyDown handler:^NSEvent *(NSEvent *event) { nativeOverlayKeyMonitor = [NSEvent addLocalMonitorForEventsMatchingMask:NSEventMaskKeyDown handler:^NSEvent *(NSEvent *event) {
if ([event keyCode] == 53) { if ([event keyCode] == 53) {
if ([view isEditingText]) {
[view cancelTextEditor];
return nil;
}
[view cancelSelection]; [view cancelSelection];
return nil; return nil;
} }
if ([event keyCode] == 36 && [view hasSelection]) { if ([event keyCode] == 36 && [view hasSelection]) {
if ([view isEditingText]) {
[view commitTextEditor];
return nil;
}
[view confirmSelection]; [view confirmSelection];
return nil; return nil;
} }