Compare commits
3 Commits
baec01610c
...
273af6a429
| Author | SHA1 | Date | |
|---|---|---|---|
| 273af6a429 | |||
| 18a9f53062 | |||
| 7b290d9300 |
@@ -3,10 +3,12 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"image"
|
||||
"image/png"
|
||||
"log/slog"
|
||||
"os"
|
||||
"path/filepath"
|
||||
@@ -23,6 +25,7 @@ import (
|
||||
"github.com/mmmy/snapgo/internal/infrastructure/display"
|
||||
"github.com/mmmy/snapgo/internal/infrastructure/hotkey"
|
||||
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/screencapture"
|
||||
sshpkg "github.com/mmmy/snapgo/internal/infrastructure/ssh"
|
||||
@@ -499,6 +502,48 @@ func (a *App) finishSummary(svc *application.CaptureSummaryService, summary stri
|
||||
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) {
|
||||
a.pendingMu.Lock()
|
||||
pc := a.pending
|
||||
@@ -519,7 +564,8 @@ func (a *App) captureSelectedPNG(result CaptureResult, pc *pendingCapture) ([]by
|
||||
return nil, err
|
||||
}
|
||||
if len(result.Annotations) > 0 {
|
||||
cropped, err = application.ApplyAnnotations(cropped, result.Annotations, pc.Display.Scale)
|
||||
scaleX, scaleY := annotationScalesForCapture(cropped, rect, pc.Display.Scale)
|
||||
cropped, err = application.ApplyAnnotationsWithScale(cropped, result.Annotations, scaleX, scaleY)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -527,6 +573,33 @@ func (a *App) captureSelectedPNG(result CaptureResult, pc *pendingCapture) ([]by
|
||||
return cropped, nil
|
||||
}
|
||||
|
||||
func annotationScalesForCapture(pngBytes []byte, rect RegionRect, fallback float64) (float64, float64) {
|
||||
if fallback <= 0 {
|
||||
fallback = 1
|
||||
}
|
||||
scaleX := fallback
|
||||
scaleY := fallback
|
||||
|
||||
cfg, err := png.DecodeConfig(bytes.NewReader(pngBytes))
|
||||
if err != nil {
|
||||
return scaleX, scaleY
|
||||
}
|
||||
if rect.W > 0 && cfg.Width > 0 {
|
||||
scaleX = saneAnnotationScale(float64(cfg.Width)/float64(rect.W), fallback)
|
||||
}
|
||||
if rect.H > 0 && cfg.Height > 0 {
|
||||
scaleY = saneAnnotationScale(float64(cfg.Height)/float64(rect.H), fallback)
|
||||
}
|
||||
return scaleX, scaleY
|
||||
}
|
||||
|
||||
func saneAnnotationScale(scale, fallback float64) float64 {
|
||||
if scale >= 0.25 && scale <= 8 {
|
||||
return scale
|
||||
}
|
||||
return fallback
|
||||
}
|
||||
|
||||
func (a *App) chooseSaveDirectory() (string, error) {
|
||||
return wruntime.OpenDirectoryDialog(a.ctx, wruntime.OpenDialogOptions{
|
||||
Title: "Save screenshot to folder",
|
||||
@@ -876,6 +949,35 @@ func (a *App) SummarizeRegion(result CaptureResult) error {
|
||||
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
|
||||
// SaveRegionToRemote. The AppKit panel is already closed by the time this
|
||||
// runs (see saveRemoteSelection in native_overlay_darwin.go), so we only
|
||||
@@ -933,6 +1035,33 @@ func (a *App) SummarizeNativeRegion(result CaptureResult) error {
|
||||
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 {
|
||||
if raw == "" {
|
||||
return nil
|
||||
|
||||
+40
-66
@@ -23,6 +23,7 @@ import {
|
||||
SaveRegionImage,
|
||||
SaveRegionToRemote,
|
||||
SummarizeRegion,
|
||||
ExtractTextRegion,
|
||||
CancelRegion,
|
||||
GetConfig,
|
||||
} from '../wailsjs/go/main/App'
|
||||
@@ -58,7 +59,7 @@ async function loadThemePreference() {
|
||||
// `SettingsTab` is hoisted to the App shell so the sidebar (which lives
|
||||
// here) and the inner SettingsView (which renders the matching card) can
|
||||
// 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')
|
||||
|
||||
// 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: 'ssh', label: '远程主机' },
|
||||
{ id: 'llm', label: '智能识图' },
|
||||
{ id: 'ocr', label: '文字提取' },
|
||||
]
|
||||
|
||||
interface OverlayPayload {
|
||||
@@ -77,6 +79,25 @@ interface OverlayPayload {
|
||||
}
|
||||
const overlayPayload = ref<OverlayPayload | null>(null)
|
||||
|
||||
interface OverlayAnnotation {
|
||||
tool: string
|
||||
color: string
|
||||
points: Array<{ x: number; y: number }>
|
||||
text?: string
|
||||
strokeWidth?: number
|
||||
fontSize?: number
|
||||
}
|
||||
|
||||
interface OverlayResult {
|
||||
rect: {
|
||||
x: number
|
||||
y: number
|
||||
w: number
|
||||
h: number
|
||||
}
|
||||
annotations: OverlayAnnotation[]
|
||||
}
|
||||
|
||||
const capturing = ref(false)
|
||||
|
||||
const toast = ref<{ kind: 'success' | 'error'; text: string } | null>(null)
|
||||
@@ -121,19 +142,7 @@ async function retryHotkey() {
|
||||
}
|
||||
}
|
||||
|
||||
async function onOverlayConfirm(rect: {
|
||||
rect: {
|
||||
x: number
|
||||
y: number
|
||||
w: number
|
||||
h: number
|
||||
}
|
||||
annotations: Array<{
|
||||
tool: string
|
||||
color: string
|
||||
points: Array<{ x: number; y: number }>
|
||||
}>
|
||||
}) {
|
||||
async function onOverlayConfirm(rect: OverlayResult) {
|
||||
// Optimistically swap back so the window does not visually lag the
|
||||
// Go-side hide. If upload fails, the toast surfaces the reason.
|
||||
mode.value = 'settings'
|
||||
@@ -145,19 +154,7 @@ async function onOverlayConfirm(rect: {
|
||||
}
|
||||
}
|
||||
|
||||
async function onOverlayCopy(rect: {
|
||||
rect: {
|
||||
x: number
|
||||
y: number
|
||||
w: number
|
||||
h: number
|
||||
}
|
||||
annotations: Array<{
|
||||
tool: string
|
||||
color: string
|
||||
points: Array<{ x: number; y: number }>
|
||||
}>
|
||||
}) {
|
||||
async function onOverlayCopy(rect: OverlayResult) {
|
||||
mode.value = 'settings'
|
||||
overlayPayload.value = null
|
||||
try {
|
||||
@@ -167,19 +164,7 @@ async function onOverlayCopy(rect: {
|
||||
}
|
||||
}
|
||||
|
||||
async function onOverlaySave(rect: {
|
||||
rect: {
|
||||
x: number
|
||||
y: number
|
||||
w: number
|
||||
h: number
|
||||
}
|
||||
annotations: Array<{
|
||||
tool: string
|
||||
color: string
|
||||
points: Array<{ x: number; y: number }>
|
||||
}>
|
||||
}) {
|
||||
async function onOverlaySave(rect: OverlayResult) {
|
||||
mode.value = 'settings'
|
||||
overlayPayload.value = null
|
||||
try {
|
||||
@@ -189,19 +174,7 @@ async function onOverlaySave(rect: {
|
||||
}
|
||||
}
|
||||
|
||||
async function onOverlaySaveRemote(rect: {
|
||||
rect: {
|
||||
x: number
|
||||
y: number
|
||||
w: number
|
||||
h: number
|
||||
}
|
||||
annotations: Array<{
|
||||
tool: string
|
||||
color: string
|
||||
points: Array<{ x: number; y: number }>
|
||||
}>
|
||||
}) {
|
||||
async function onOverlaySaveRemote(rect: OverlayResult) {
|
||||
mode.value = 'settings'
|
||||
overlayPayload.value = null
|
||||
try {
|
||||
@@ -211,19 +184,7 @@ async function onOverlaySaveRemote(rect: {
|
||||
}
|
||||
}
|
||||
|
||||
async function onOverlaySummarize(rect: {
|
||||
rect: {
|
||||
x: number
|
||||
y: number
|
||||
w: number
|
||||
h: number
|
||||
}
|
||||
annotations: Array<{
|
||||
tool: string
|
||||
color: string
|
||||
points: Array<{ x: number; y: number }>
|
||||
}>
|
||||
}) {
|
||||
async function onOverlaySummarize(rect: OverlayResult) {
|
||||
mode.value = 'settings'
|
||||
overlayPayload.value = null
|
||||
try {
|
||||
@@ -233,6 +194,16 @@ async function onOverlaySummarize(rect: {
|
||||
}
|
||||
}
|
||||
|
||||
async function onOverlayOCR(rect: OverlayResult) {
|
||||
mode.value = 'settings'
|
||||
overlayPayload.value = null
|
||||
try {
|
||||
await ExtractTextRegion(rect as any)
|
||||
} catch {
|
||||
/* Surfaced via upload:failure */
|
||||
}
|
||||
}
|
||||
|
||||
async function onOverlayCancel() {
|
||||
mode.value = 'settings'
|
||||
overlayPayload.value = null
|
||||
@@ -264,6 +235,8 @@ onMounted(() => {
|
||||
'success',
|
||||
url === 'summary copied to clipboard'
|
||||
? 'Summary copied to clipboard'
|
||||
: url === 'ocr text copied to clipboard'
|
||||
? 'OCR text copied to clipboard'
|
||||
: `Copied: ${url}`
|
||||
)
|
||||
})
|
||||
@@ -303,6 +276,7 @@ onUnmounted(() => {
|
||||
@save="onOverlaySave"
|
||||
@save-remote="onOverlaySaveRemote"
|
||||
@summarize="onOverlaySummarize"
|
||||
@ocr="onOverlayOCR"
|
||||
@cancel="onOverlayCancel"
|
||||
/>
|
||||
|
||||
|
||||
@@ -33,6 +33,8 @@ interface Annotation {
|
||||
color: string
|
||||
points: Point[]
|
||||
text?: string
|
||||
strokeWidth?: number
|
||||
fontSize?: number
|
||||
}
|
||||
|
||||
const emit = defineEmits<{
|
||||
@@ -56,6 +58,10 @@ const emit = defineEmits<{
|
||||
e: 'summarize',
|
||||
payload: { rect: Rect; annotations: Annotation[] }
|
||||
): void
|
||||
(
|
||||
e: 'ocr',
|
||||
payload: { rect: Rect; annotations: Annotation[] }
|
||||
): void
|
||||
(e: 'cancel'): void
|
||||
}>()
|
||||
|
||||
@@ -64,28 +70,45 @@ const annotations = ref<Annotation[]>([])
|
||||
const draftAnnotation = ref<Annotation | null>(null)
|
||||
const activeTool = ref<Tool>('pen')
|
||||
const activeColor = ref('#ef4444')
|
||||
const paletteOpen = ref(false)
|
||||
const textDraft = ref<{ point: Point; value: string } | null>(null)
|
||||
const activeStrokeWidth = ref(3)
|
||||
const activeFontSize = ref(20)
|
||||
const textDraft = ref<{
|
||||
point: Point
|
||||
value: string
|
||||
color: string
|
||||
fontSize: number
|
||||
index: number | null
|
||||
} | null>(null)
|
||||
const textInputRef = ref<HTMLInputElement | null>(null)
|
||||
const selectedTextIndex = ref<number | null>(null)
|
||||
|
||||
type ResizeHandle = 'n' | 's' | 'e' | 'w' | 'nw' | 'ne' | 'sw' | 'se'
|
||||
type DragMode = 'idle' | 'creating' | 'moving' | 'resizing' | 'annotating'
|
||||
type DragMode =
|
||||
| 'idle'
|
||||
| 'creating'
|
||||
| 'moving'
|
||||
| 'resizing'
|
||||
| 'annotating'
|
||||
| 'moving-text'
|
||||
const dragMode = ref<DragMode>('idle')
|
||||
const resizeHandle = ref<ResizeHandle | null>(null)
|
||||
const dragAnchor = ref({ x: 0, y: 0 })
|
||||
const startRect = ref<Rect | null>(null)
|
||||
const textDragStartPoint = ref<Point | null>(null)
|
||||
const textDragOriginalPoint = ref<Point | null>(null)
|
||||
|
||||
const DEFAULT_TEXT_FONT_SIZE = 20
|
||||
const STROKE_WIDTHS = [2, 4, 6]
|
||||
const FONT_SIZES = [16, 20, 28, 36]
|
||||
|
||||
const colors = [
|
||||
'#ef4444',
|
||||
'#f97316',
|
||||
'#facc15',
|
||||
'#22c55e',
|
||||
'#06b6d4',
|
||||
'#3b82f6',
|
||||
'#8b5cf6',
|
||||
'#ec4899',
|
||||
'#ffffff',
|
||||
'#111827',
|
||||
'#9ca3af',
|
||||
'#ffffff',
|
||||
]
|
||||
|
||||
const maskPath = computed(() => {
|
||||
@@ -118,10 +141,32 @@ const sizeLabel = computed(() => {
|
||||
return `${Math.round(rect.value.w)} × ${Math.round(rect.value.h)}`
|
||||
})
|
||||
|
||||
const MARK_TOOLBAR_W = 222
|
||||
const ACTION_TOOLBAR_W = 216
|
||||
const MARK_TOOLBAR_W = 178
|
||||
const ACTION_TOOLBAR_W = 252
|
||||
const TOOLBAR_GROUP_GAP = 8
|
||||
|
||||
const toolOrder: Tool[] = ['pen', 'rect', 'ellipse', 'text']
|
||||
|
||||
const toolSettingsPos = computed(() => {
|
||||
if (!leftToolbarPos.value || !rect.value || !activeTool.value) return null
|
||||
const menuW = activeTool.value === 'text' ? 462 : 398
|
||||
const menuH = 58
|
||||
const buttonIndex = toolOrder.indexOf(activeTool.value)
|
||||
const buttonCenter = 4 + buttonIndex * 34 + 14
|
||||
let x = leftToolbarPos.value.x
|
||||
let y = leftToolbarPos.value.y + 44
|
||||
if (y + menuH > props.height) {
|
||||
y = leftToolbarPos.value.y - menuH - 8
|
||||
}
|
||||
x = clamp(x, 8, Math.max(8, props.width - menuW - 8))
|
||||
return {
|
||||
x,
|
||||
y,
|
||||
w: menuW,
|
||||
arrowX: clamp(leftToolbarPos.value.x + buttonCenter - x, 18, menuW - 18),
|
||||
}
|
||||
})
|
||||
|
||||
const rightToolbarPos = computed(() => {
|
||||
if (!rect.value) return null
|
||||
return placeToolbar(rect.value, ACTION_TOOLBAR_W, 40, 'right')
|
||||
@@ -207,6 +252,89 @@ function insideRect(p: Point, r: Rect | null) {
|
||||
return p.x >= r.x && p.x <= r.x + r.w && p.y >= r.y && p.y <= r.y + r.h
|
||||
}
|
||||
|
||||
let measureCanvas: HTMLCanvasElement | null = null
|
||||
|
||||
function textFont(fontSize = DEFAULT_TEXT_FONT_SIZE) {
|
||||
return `600 ${fontSize}px -apple-system, BlinkMacSystemFont, "SF Pro Text", sans-serif`
|
||||
}
|
||||
|
||||
function textSize(annotation: Pick<Annotation, 'text' | 'fontSize'>) {
|
||||
const fontSize = annotation.fontSize || DEFAULT_TEXT_FONT_SIZE
|
||||
const lines = (annotation.text || ' ').split('\n')
|
||||
if (typeof document === 'undefined') {
|
||||
return {
|
||||
w: Math.max(1, ...lines.map((line) => line.length * fontSize)),
|
||||
h: Math.max(fontSize * 1.2, lines.length * fontSize * 1.2),
|
||||
}
|
||||
}
|
||||
measureCanvas ||= document.createElement('canvas')
|
||||
const ctx = measureCanvas.getContext('2d')
|
||||
if (!ctx) {
|
||||
return {
|
||||
w: Math.max(1, ...lines.map((line) => line.length * fontSize)),
|
||||
h: Math.max(fontSize * 1.2, lines.length * fontSize * 1.2),
|
||||
}
|
||||
}
|
||||
ctx.font = textFont(fontSize)
|
||||
return {
|
||||
w: Math.ceil(Math.max(1, ...lines.map((line) => ctx.measureText(line || ' ').width))),
|
||||
h: Math.ceil(Math.max(fontSize * 1.2, lines.length * fontSize * 1.2)),
|
||||
}
|
||||
}
|
||||
|
||||
function textEditorSize(draft: { value: string; fontSize: number }) {
|
||||
const size = textSize({
|
||||
text: draft.value || ' ',
|
||||
fontSize: draft.fontSize,
|
||||
})
|
||||
return {
|
||||
width: Math.max(180, Math.min(420, size.w + 28)),
|
||||
height: Math.max(30, Math.ceil(draft.fontSize * 1.6)),
|
||||
lineHeight: Math.ceil(draft.fontSize * 1.25),
|
||||
}
|
||||
}
|
||||
|
||||
function textRenderBox(annotation: Annotation) {
|
||||
const point = annotation.points[0] || { x: 0, y: 0 }
|
||||
const size = textSize(annotation)
|
||||
return {
|
||||
x: point.x - 4,
|
||||
y: point.y - 3,
|
||||
w: size.w + 8,
|
||||
h: size.h + 6,
|
||||
}
|
||||
}
|
||||
|
||||
function textAnnotationIndexAtPoint(p: Point) {
|
||||
if (!rect.value) return null
|
||||
for (let i = annotations.value.length - 1; i >= 0; i--) {
|
||||
const annotation = annotations.value[i]
|
||||
if (annotation.tool !== 'text' || annotation.points.length === 0) continue
|
||||
const point = annotation.points[0]
|
||||
const size = textSize(annotation)
|
||||
const x = rect.value.x + point.x
|
||||
const y = rect.value.y + point.y
|
||||
if (
|
||||
p.x >= x - 6 &&
|
||||
p.x <= x + size.w + 6 &&
|
||||
p.y >= y - 6 &&
|
||||
p.y <= y + size.h + 6
|
||||
) {
|
||||
return i
|
||||
}
|
||||
}
|
||||
return null
|
||||
}
|
||||
|
||||
function clampTextLocalPoint(point: Point, annotation: Pick<Annotation, 'text' | 'fontSize'>) {
|
||||
if (!rect.value) return point
|
||||
const size = textSize(annotation)
|
||||
return {
|
||||
x: clamp(point.x, 0, Math.max(0, rect.value.w - size.w)),
|
||||
y: clamp(point.y, 0, Math.max(0, rect.value.h - size.h)),
|
||||
}
|
||||
}
|
||||
|
||||
function hitHandle(p: Point): ResizeHandle | null {
|
||||
if (!rect.value) return null
|
||||
const tolerance = 9
|
||||
@@ -231,7 +359,8 @@ function hitHandle(p: Point): ResizeHandle | null {
|
||||
function onMouseDown(e: MouseEvent) {
|
||||
if (e.button !== 0) return
|
||||
commitTextDraft()
|
||||
paletteOpen.value = false
|
||||
textDragStartPoint.value = null
|
||||
textDragOriginalPoint.value = null
|
||||
const p = pointFromEvent(e)
|
||||
const handle = hitHandle(p)
|
||||
if (handle && rect.value) {
|
||||
@@ -250,15 +379,34 @@ function onMouseDown(e: MouseEvent) {
|
||||
dragAnchor.value = p
|
||||
rect.value = { x: p.x, y: p.y, w: 0, h: 0 }
|
||||
annotations.value = []
|
||||
selectedTextIndex.value = null
|
||||
draftAnnotation.value = null
|
||||
}
|
||||
|
||||
function onSelectionMouseDown(e: MouseEvent) {
|
||||
if (e.button !== 0 || !rect.value) return
|
||||
e.stopPropagation()
|
||||
paletteOpen.value = false
|
||||
commitTextDraft()
|
||||
const p = pointFromEvent(e)
|
||||
const hitTextIndex = textAnnotationIndexAtPoint(p)
|
||||
if (hitTextIndex !== null) {
|
||||
selectedTextIndex.value = hitTextIndex
|
||||
const annotation = annotations.value[hitTextIndex]
|
||||
activeTool.value = 'text'
|
||||
activeColor.value = annotation.color
|
||||
activeFontSize.value = annotation.fontSize || DEFAULT_TEXT_FONT_SIZE
|
||||
if (e.detail >= 2) {
|
||||
beginTextAnnotation(annotation.points[0], hitTextIndex)
|
||||
return
|
||||
}
|
||||
dragMode.value = 'moving-text'
|
||||
textDragStartPoint.value = localPoint(p)
|
||||
textDragOriginalPoint.value = { ...annotation.points[0] }
|
||||
return
|
||||
}
|
||||
selectedTextIndex.value = null
|
||||
if (activeTool.value === 'text') {
|
||||
beginTextAnnotation(localPoint(pointFromEvent(e)))
|
||||
beginTextAnnotation(localPoint(p))
|
||||
return
|
||||
}
|
||||
if (e.detail >= 2) {
|
||||
@@ -266,7 +414,6 @@ function onSelectionMouseDown(e: MouseEvent) {
|
||||
onCopy()
|
||||
return
|
||||
}
|
||||
const p = pointFromEvent(e)
|
||||
const handle = hitHandle(p)
|
||||
if (handle) {
|
||||
dragMode.value = 'resizing'
|
||||
@@ -280,6 +427,7 @@ function onSelectionMouseDown(e: MouseEvent) {
|
||||
draftAnnotation.value = {
|
||||
tool: activeTool.value,
|
||||
color: activeColor.value,
|
||||
strokeWidth: activeStrokeWidth.value,
|
||||
points: [local],
|
||||
}
|
||||
}
|
||||
@@ -297,6 +445,21 @@ function onMouseMove(e: MouseEvent) {
|
||||
}
|
||||
} else if (dragMode.value === 'resizing') {
|
||||
resizeSelection(p)
|
||||
} else if (
|
||||
dragMode.value === 'moving-text' &&
|
||||
selectedTextIndex.value !== null &&
|
||||
textDragStartPoint.value &&
|
||||
textDragOriginalPoint.value
|
||||
) {
|
||||
const annotation = annotations.value[selectedTextIndex.value]
|
||||
if (annotation?.tool === 'text') {
|
||||
const local = localPoint(p)
|
||||
const next = {
|
||||
x: textDragOriginalPoint.value.x + local.x - textDragStartPoint.value.x,
|
||||
y: textDragOriginalPoint.value.y + local.y - textDragStartPoint.value.y,
|
||||
}
|
||||
annotation.points[0] = clampTextLocalPoint(next, annotation)
|
||||
}
|
||||
} else if (dragMode.value === 'annotating' && draftAnnotation.value) {
|
||||
if (activeTool.value === 'pen') {
|
||||
draftAnnotation.value.points.push(localPoint(p))
|
||||
@@ -324,6 +487,8 @@ function onMouseUp() {
|
||||
dragMode.value = 'idle'
|
||||
resizeHandle.value = null
|
||||
startRect.value = null
|
||||
textDragStartPoint.value = null
|
||||
textDragOriginalPoint.value = null
|
||||
}
|
||||
|
||||
function resizeSelection(p: Point) {
|
||||
@@ -356,7 +521,38 @@ function selectTool(tool: Tool) {
|
||||
|
||||
function chooseColor(color: string) {
|
||||
activeColor.value = color
|
||||
paletteOpen.value = false
|
||||
if (textDraft.value) {
|
||||
textDraft.value.color = color
|
||||
} else if (
|
||||
activeTool.value === 'text' &&
|
||||
selectedTextIndex.value !== null &&
|
||||
annotations.value[selectedTextIndex.value]?.tool === 'text'
|
||||
) {
|
||||
annotations.value[selectedTextIndex.value].color = color
|
||||
}
|
||||
}
|
||||
|
||||
function chooseStrokeWidth(width: number) {
|
||||
activeStrokeWidth.value = width
|
||||
}
|
||||
|
||||
function chooseFontSize(size: number) {
|
||||
activeFontSize.value = size
|
||||
if (textDraft.value) {
|
||||
textDraft.value.fontSize = size
|
||||
textDraft.value.point = clampTextLocalPoint(textDraft.value.point, {
|
||||
text: textDraft.value.value,
|
||||
fontSize: size,
|
||||
})
|
||||
} else if (
|
||||
activeTool.value === 'text' &&
|
||||
selectedTextIndex.value !== null &&
|
||||
annotations.value[selectedTextIndex.value]?.tool === 'text'
|
||||
) {
|
||||
const annotation = annotations.value[selectedTextIndex.value]
|
||||
annotation.fontSize = size
|
||||
annotation.points[0] = clampTextLocalPoint(annotation.points[0], annotation)
|
||||
}
|
||||
}
|
||||
|
||||
function onConfirm() {
|
||||
@@ -379,7 +575,11 @@ function onSummarize() {
|
||||
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
|
||||
commitTextDraft()
|
||||
const payload = {
|
||||
@@ -391,6 +591,7 @@ function emitAction(action: 'confirm' | 'copy' | 'save' | 'save-remote' | 'summa
|
||||
if (action === 'save') emit('save', payload)
|
||||
if (action === 'save-remote') emit('save-remote', payload)
|
||||
if (action === 'summarize') emit('summarize', payload)
|
||||
if (action === 'ocr') emit('ocr', payload)
|
||||
}
|
||||
|
||||
function onCancel() {
|
||||
@@ -415,8 +616,16 @@ function onKeydown(e: KeyboardEvent) {
|
||||
}
|
||||
|
||||
function undoAnnotation() {
|
||||
if (!textDraft.value && annotations.value.length === 0) return
|
||||
cancelTextDraft()
|
||||
if (annotations.value.length === 0) return
|
||||
annotations.value.pop()
|
||||
if (
|
||||
selectedTextIndex.value !== null &&
|
||||
selectedTextIndex.value >= annotations.value.length
|
||||
) {
|
||||
selectedTextIndex.value = null
|
||||
}
|
||||
}
|
||||
|
||||
function removeSinglePointAnnotation() {
|
||||
@@ -426,11 +635,24 @@ function removeSinglePointAnnotation() {
|
||||
}
|
||||
}
|
||||
|
||||
function beginTextAnnotation(point: Point) {
|
||||
function beginTextAnnotation(point: Point, index: number | null = null) {
|
||||
commitTextDraft()
|
||||
dragMode.value = 'idle'
|
||||
draftAnnotation.value = null
|
||||
textDraft.value = { point, value: '' }
|
||||
const existing = index !== null ? annotations.value[index] : null
|
||||
const color = existing?.color || activeColor.value
|
||||
const fontSize = existing?.fontSize || activeFontSize.value
|
||||
if (existing) {
|
||||
activeColor.value = color
|
||||
activeFontSize.value = fontSize
|
||||
}
|
||||
textDraft.value = {
|
||||
point: clampTextLocalPoint(point, existing || { text: '', fontSize }),
|
||||
value: existing?.text || '',
|
||||
color,
|
||||
fontSize,
|
||||
index,
|
||||
}
|
||||
void nextTick(() => {
|
||||
textInputRef.value?.focus()
|
||||
textInputRef.value?.select()
|
||||
@@ -440,13 +662,31 @@ function beginTextAnnotation(point: Point) {
|
||||
function commitTextDraft() {
|
||||
if (!textDraft.value) return
|
||||
const text = textDraft.value.value.trim()
|
||||
const draft = textDraft.value
|
||||
if (draft.index !== null) {
|
||||
const target = annotations.value[draft.index]
|
||||
if (target?.tool === 'text') {
|
||||
if (text !== '') {
|
||||
target.points = [clampTextLocalPoint(draft.point, { text, fontSize: draft.fontSize })]
|
||||
target.text = text
|
||||
target.color = draft.color
|
||||
target.fontSize = draft.fontSize
|
||||
selectedTextIndex.value = draft.index
|
||||
} else {
|
||||
annotations.value.splice(draft.index, 1)
|
||||
selectedTextIndex.value = null
|
||||
}
|
||||
}
|
||||
} else if (text !== '') {
|
||||
const point = clampTextLocalPoint(draft.point, { text, fontSize: draft.fontSize })
|
||||
annotations.value.push({
|
||||
tool: 'text',
|
||||
color: activeColor.value,
|
||||
points: [textDraft.value.point],
|
||||
color: draft.color,
|
||||
points: [point],
|
||||
text,
|
||||
fontSize: draft.fontSize,
|
||||
})
|
||||
selectedTextIndex.value = annotations.value.length - 1
|
||||
}
|
||||
textDraft.value = null
|
||||
}
|
||||
@@ -506,7 +746,7 @@ onUnmounted(() => {
|
||||
v-if="annotation.tool === 'pen'"
|
||||
:points="annotation.points.map((p) => `${p.x},${p.y}`).join(' ')"
|
||||
:stroke="annotation.color"
|
||||
stroke-width="3"
|
||||
:stroke-width="annotation.strokeWidth || 3"
|
||||
stroke-linecap="round"
|
||||
stroke-linejoin="round"
|
||||
fill="none"
|
||||
@@ -518,7 +758,7 @@ onUnmounted(() => {
|
||||
:width="Math.abs(annotation.points[1].x - annotation.points[0].x)"
|
||||
:height="Math.abs(annotation.points[1].y - annotation.points[0].y)"
|
||||
:stroke="annotation.color"
|
||||
stroke-width="3"
|
||||
:stroke-width="annotation.strokeWidth || 3"
|
||||
fill="none"
|
||||
/>
|
||||
<ellipse
|
||||
@@ -528,20 +768,30 @@ onUnmounted(() => {
|
||||
:rx="Math.abs(annotation.points[1].x - annotation.points[0].x) / 2"
|
||||
:ry="Math.abs(annotation.points[1].y - annotation.points[0].y) / 2"
|
||||
:stroke="annotation.color"
|
||||
stroke-width="3"
|
||||
:stroke-width="annotation.strokeWidth || 3"
|
||||
fill="none"
|
||||
/>
|
||||
<template v-else-if="annotation.tool === 'text' && annotation.points.length >= 1">
|
||||
<rect
|
||||
v-if="selectedTextIndex === index && index < annotations.length"
|
||||
class="text-selection-box"
|
||||
:x="textRenderBox(annotation).x"
|
||||
:y="textRenderBox(annotation).y"
|
||||
:width="textRenderBox(annotation).w"
|
||||
:height="textRenderBox(annotation).h"
|
||||
/>
|
||||
<text
|
||||
v-else-if="annotation.tool === 'text' && annotation.points.length >= 1"
|
||||
class="annotation-text"
|
||||
:x="annotation.points[0].x"
|
||||
:y="annotation.points[0].y"
|
||||
:fill="annotation.color"
|
||||
font-size="20"
|
||||
:font-size="annotation.fontSize || DEFAULT_TEXT_FONT_SIZE"
|
||||
font-weight="600"
|
||||
dominant-baseline="hanging"
|
||||
>
|
||||
{{ annotation.text }}
|
||||
</text>
|
||||
</template>
|
||||
</g>
|
||||
</svg>
|
||||
|
||||
@@ -565,7 +815,11 @@ onUnmounted(() => {
|
||||
:style="{
|
||||
left: rect.x + textDraft.point.x + 'px',
|
||||
top: rect.y + textDraft.point.y + 'px',
|
||||
color: activeColor,
|
||||
color: textDraft.color,
|
||||
fontSize: textDraft.fontSize + 'px',
|
||||
width: textEditorSize(textDraft).width + 'px',
|
||||
height: textEditorSize(textDraft).height + 'px',
|
||||
lineHeight: textEditorSize(textDraft).lineHeight + 'px',
|
||||
}"
|
||||
spellcheck="false"
|
||||
@mousedown.stop
|
||||
@@ -649,15 +903,57 @@ onUnmounted(() => {
|
||||
<path d="M9 19h6" />
|
||||
</svg>
|
||||
</button>
|
||||
<div class="color-wrap">
|
||||
<button
|
||||
class="icon-btn color-btn"
|
||||
title="选择标记颜色"
|
||||
@click="paletteOpen = !paletteOpen"
|
||||
class="icon-btn"
|
||||
:class="{ muted: annotations.length === 0 }"
|
||||
:aria-disabled="annotations.length === 0"
|
||||
title="撤销上一处标记"
|
||||
@click="undoAnnotation"
|
||||
>
|
||||
<span :style="{ background: activeColor }" />
|
||||
<svg viewBox="0 0 24 24" aria-hidden="true">
|
||||
<path d="M9 7H4v5" />
|
||||
<path d="M4 7c3-3 8-4 12-1 4 3 4 9 0 12-2 1-4 2-7 1" />
|
||||
</svg>
|
||||
</button>
|
||||
<div v-if="paletteOpen" class="palette">
|
||||
</div>
|
||||
|
||||
<div
|
||||
v-if="toolSettingsPos && rect && rect.w >= 4 && rect.h >= 4"
|
||||
class="tool-settings-menu"
|
||||
:style="{
|
||||
left: toolSettingsPos.x + 'px',
|
||||
top: toolSettingsPos.y + 'px',
|
||||
width: toolSettingsPos.w + 'px',
|
||||
'--arrow-x': toolSettingsPos.arrowX + 'px',
|
||||
}"
|
||||
@mousedown.stop
|
||||
>
|
||||
<div v-if="activeTool !== 'text'" class="setting-group stroke-group">
|
||||
<button
|
||||
v-for="widthValue in STROKE_WIDTHS"
|
||||
:key="widthValue"
|
||||
class="stroke-choice"
|
||||
:class="{ active: activeStrokeWidth === widthValue }"
|
||||
:title="`${widthValue}px`"
|
||||
@click="chooseStrokeWidth(widthValue)"
|
||||
>
|
||||
<span :style="{ width: widthValue * 4 + 'px', height: widthValue * 4 + 'px' }" />
|
||||
</button>
|
||||
</div>
|
||||
<div v-else class="setting-group font-group">
|
||||
<button
|
||||
v-for="size in FONT_SIZES"
|
||||
:key="size"
|
||||
class="font-choice"
|
||||
:class="{ active: activeFontSize === size }"
|
||||
:title="`${size}px`"
|
||||
@click="chooseFontSize(size)"
|
||||
>
|
||||
{{ size }}
|
||||
</button>
|
||||
</div>
|
||||
<div class="settings-divider" />
|
||||
<div class="setting-group color-group">
|
||||
<button
|
||||
v-for="color in colors"
|
||||
:key="color"
|
||||
@@ -669,18 +965,6 @@ onUnmounted(() => {
|
||||
/>
|
||||
</div>
|
||||
</div>
|
||||
<button
|
||||
class="icon-btn"
|
||||
:disabled="annotations.length === 0"
|
||||
title="撤销上一处标记"
|
||||
@click="undoAnnotation"
|
||||
>
|
||||
<svg viewBox="0 0 24 24" aria-hidden="true">
|
||||
<path d="M9 7H4v5" />
|
||||
<path d="M4 7c3-3 8-4 12-1 4 3 4 9 0 12-2 1-4 2-7 1" />
|
||||
</svg>
|
||||
</button>
|
||||
</div>
|
||||
|
||||
<div
|
||||
v-if="rightToolbarPos && rect && rect.w >= 4 && rect.h >= 4"
|
||||
@@ -716,6 +1000,21 @@ onUnmounted(() => {
|
||||
@click="onSaveRemote"
|
||||
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
|
||||
class="action-btn"
|
||||
data-tip="复制总结"
|
||||
@@ -761,6 +1060,17 @@ onUnmounted(() => {
|
||||
pointer-events: none;
|
||||
}
|
||||
|
||||
.annotation-text {
|
||||
font-family: -apple-system, BlinkMacSystemFont, "SF Pro Text", sans-serif;
|
||||
}
|
||||
|
||||
.text-selection-box {
|
||||
fill: transparent;
|
||||
stroke: #3b82f6;
|
||||
stroke-width: 1.5;
|
||||
stroke-dasharray: 4 3;
|
||||
}
|
||||
|
||||
.selection-hit-area {
|
||||
position: absolute;
|
||||
cursor: crosshair;
|
||||
@@ -769,10 +1079,10 @@ onUnmounted(() => {
|
||||
.text-editor {
|
||||
position: absolute;
|
||||
z-index: 6;
|
||||
box-sizing: border-box;
|
||||
min-width: 120px;
|
||||
max-width: 320px;
|
||||
height: 28px;
|
||||
padding: 2px 6px;
|
||||
max-width: 420px;
|
||||
padding: 0 10px;
|
||||
border: 1px solid currentColor;
|
||||
border-radius: 4px;
|
||||
outline: none;
|
||||
@@ -835,15 +1145,17 @@ onUnmounted(() => {
|
||||
}
|
||||
|
||||
.mark-toolbar {
|
||||
width: 222px;
|
||||
width: 178px;
|
||||
}
|
||||
.action-toolbar {
|
||||
width: 216px;
|
||||
width: 252px;
|
||||
}
|
||||
|
||||
.icon-btn,
|
||||
.action-btn,
|
||||
.swatch {
|
||||
.swatch,
|
||||
.stroke-choice,
|
||||
.font-choice {
|
||||
font-family: inherit;
|
||||
}
|
||||
|
||||
@@ -936,14 +1248,14 @@ onUnmounted(() => {
|
||||
background: transparent;
|
||||
cursor: pointer;
|
||||
}
|
||||
.icon-btn:hover:not(:disabled),
|
||||
.icon-btn:hover:not(.muted),
|
||||
.icon-btn.active {
|
||||
color: #fff;
|
||||
background: rgba(255, 255, 255, 0.12);
|
||||
}
|
||||
.icon-btn:disabled {
|
||||
.icon-btn.muted {
|
||||
opacity: 0.35;
|
||||
cursor: not-allowed;
|
||||
cursor: default;
|
||||
}
|
||||
.icon-btn svg {
|
||||
width: 18px;
|
||||
@@ -955,36 +1267,88 @@ onUnmounted(() => {
|
||||
stroke-linejoin: round;
|
||||
}
|
||||
|
||||
.color-wrap {
|
||||
position: relative;
|
||||
.tool-settings-menu {
|
||||
position: absolute;
|
||||
z-index: 5;
|
||||
box-sizing: border-box;
|
||||
display: flex;
|
||||
align-items: center;
|
||||
gap: 12px;
|
||||
min-height: 42px;
|
||||
padding: 8px 14px;
|
||||
color: #1f2937;
|
||||
background: rgba(255, 255, 255, 0.96);
|
||||
border-radius: 8px;
|
||||
box-shadow: 0 12px 30px rgba(0, 0, 0, 0.26);
|
||||
cursor: default;
|
||||
}
|
||||
.color-btn span {
|
||||
.tool-settings-menu::before {
|
||||
content: '';
|
||||
position: absolute;
|
||||
left: var(--arrow-x);
|
||||
top: -8px;
|
||||
transform: translateX(-50%) rotate(45deg);
|
||||
width: 16px;
|
||||
height: 16px;
|
||||
border: 1px solid rgba(255, 255, 255, 0.7);
|
||||
box-shadow: inset 0 0 0 1px rgba(0, 0, 0, 0.18);
|
||||
background: rgba(255, 255, 255, 0.96);
|
||||
border-radius: 3px;
|
||||
}
|
||||
.palette {
|
||||
position: absolute;
|
||||
left: 0;
|
||||
bottom: 36px;
|
||||
.setting-group {
|
||||
position: relative;
|
||||
z-index: 1;
|
||||
display: flex;
|
||||
align-items: center;
|
||||
gap: 10px;
|
||||
}
|
||||
.stroke-choice,
|
||||
.font-choice {
|
||||
display: grid;
|
||||
grid-template-columns: repeat(5, 22px);
|
||||
gap: 6px;
|
||||
padding: 8px;
|
||||
background: rgba(28, 28, 32, 0.96);
|
||||
border-radius: 8px;
|
||||
box-shadow: 0 8px 26px rgba(0, 0, 0, 0.45);
|
||||
place-items: center;
|
||||
width: 32px;
|
||||
height: 32px;
|
||||
padding: 0;
|
||||
border: 0;
|
||||
border-radius: 6px;
|
||||
color: #1f2937;
|
||||
background: transparent;
|
||||
cursor: pointer;
|
||||
}
|
||||
.stroke-choice:hover,
|
||||
.stroke-choice.active,
|
||||
.font-choice:hover,
|
||||
.font-choice.active {
|
||||
color: #2563eb;
|
||||
background: #eaf1ff;
|
||||
}
|
||||
.stroke-choice span {
|
||||
display: block;
|
||||
border-radius: 999px;
|
||||
background: currentColor;
|
||||
}
|
||||
.font-choice {
|
||||
min-width: 40px;
|
||||
width: auto;
|
||||
padding: 0 8px;
|
||||
font-size: 14px;
|
||||
font-weight: 700;
|
||||
}
|
||||
.settings-divider {
|
||||
position: relative;
|
||||
z-index: 1;
|
||||
width: 1px;
|
||||
height: 26px;
|
||||
background: #d1d5db;
|
||||
}
|
||||
.swatch {
|
||||
width: 22px;
|
||||
height: 22px;
|
||||
border: 1px solid rgba(255, 255, 255, 0.55);
|
||||
padding: 0;
|
||||
border: 1px solid rgba(17, 24, 39, 0.18);
|
||||
border-radius: 4px;
|
||||
cursor: pointer;
|
||||
}
|
||||
.swatch.selected {
|
||||
outline: 2px solid #fff;
|
||||
outline: 2px solid #2563eb;
|
||||
outline-offset: 2px;
|
||||
}
|
||||
|
||||
|
||||
@@ -9,6 +9,7 @@
|
||||
* • "s3" — S3-compatible object-storage credentials.
|
||||
* • "ssh" — SSH/SCP destination for the "save remote" button.
|
||||
* • "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.
|
||||
*/
|
||||
@@ -23,7 +24,7 @@ import {
|
||||
|
||||
// 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.
|
||||
type TabId = 'general' | 's3' | 'ssh' | 'llm'
|
||||
type TabId = 'general' | 's3' | 'ssh' | 'llm' | 'ocr'
|
||||
type ThemeMode = 'auto' | 'light' | 'dark'
|
||||
|
||||
const props = defineProps<{
|
||||
@@ -74,12 +75,30 @@ interface LLMConfig {
|
||||
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 {
|
||||
hotkey: string
|
||||
theme: ThemeMode
|
||||
s3: S3Config
|
||||
ssh: SSHConfig
|
||||
llm: LLMConfig
|
||||
ocr: OCRConfig
|
||||
}
|
||||
|
||||
const llmProviderOptions = [
|
||||
@@ -88,6 +107,11 @@ const llmProviderOptions = [
|
||||
{ id: 'openai', label: 'ChatGPT / OpenAI-compatible' },
|
||||
]
|
||||
|
||||
const ocrProviderOptions = [
|
||||
{ id: 'aliyun', label: '阿里云文字识别' },
|
||||
{ id: 'volcengine', label: '火山引擎文字识别' },
|
||||
]
|
||||
|
||||
const themeOptions: Array<{ id: ThemeMode; label: string }> = [
|
||||
{ id: 'light', 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]
|
||||
})
|
||||
|
||||
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 {
|
||||
return theme === 'light' || theme === 'dark' || theme === 'auto'
|
||||
? theme
|
||||
@@ -225,6 +285,11 @@ async function load() {
|
||||
...defaultConfig().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)
|
||||
config.value = merged
|
||||
emit('theme-change', config.value.theme)
|
||||
@@ -586,6 +651,103 @@ onMounted(load)
|
||||
</div>
|
||||
</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">
|
||||
<div
|
||||
v-if="message"
|
||||
|
||||
Vendored
+4
@@ -17,6 +17,10 @@ export function CopyNativeRegionImage(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 QuitApp():Promise<void>;
|
||||
|
||||
@@ -30,6 +30,14 @@ export function 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() {
|
||||
return window['go']['main']['App']['GetConfig']();
|
||||
}
|
||||
|
||||
@@ -19,6 +19,8 @@ export namespace application {
|
||||
color: string;
|
||||
points: Point[];
|
||||
text?: string;
|
||||
strokeWidth?: number;
|
||||
fontSize?: number;
|
||||
|
||||
static createFrom(source: any = {}) {
|
||||
return new Annotation(source);
|
||||
@@ -30,6 +32,8 @@ export namespace application {
|
||||
this.color = source["color"];
|
||||
this.points = this.convertValues(source["points"], Point);
|
||||
this.text = source["text"];
|
||||
this.strokeWidth = source["strokeWidth"];
|
||||
this.fontSize = source["fontSize"];
|
||||
}
|
||||
|
||||
convertValues(a: any, classs: any, asMap: boolean = false): any {
|
||||
@@ -55,6 +59,66 @@ export namespace application {
|
||||
|
||||
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 {
|
||||
label: string;
|
||||
baseUrl: string;
|
||||
@@ -175,6 +239,7 @@ export namespace domain {
|
||||
s3: S3Config;
|
||||
ssh: SSHConfig;
|
||||
llm: LLMConfig;
|
||||
ocr: OCRConfig;
|
||||
|
||||
static createFrom(source: any = {}) {
|
||||
return new AppConfig(source);
|
||||
@@ -187,6 +252,7 @@ export namespace domain {
|
||||
this.s3 = this.convertValues(source["s3"], S3Config);
|
||||
this.ssh = this.convertValues(source["ssh"], SSHConfig);
|
||||
this.llm = this.convertValues(source["llm"], LLMConfig);
|
||||
this.ocr = this.convertValues(source["ocr"], OCRConfig);
|
||||
}
|
||||
|
||||
convertValues(a: any, classs: any, asMap: boolean = false): any {
|
||||
@@ -211,6 +277,8 @@ export namespace domain {
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
export namespace main {
|
||||
@@ -281,4 +349,3 @@ export namespace main {
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
@@ -26,6 +26,8 @@ type Annotation struct {
|
||||
Color string `json:"color"`
|
||||
Points []Point `json:"points"`
|
||||
Text string `json:"text,omitempty"`
|
||||
StrokeWidth float64 `json:"strokeWidth,omitempty"`
|
||||
FontSize float64 `json:"fontSize,omitempty"`
|
||||
}
|
||||
|
||||
type Point struct {
|
||||
@@ -35,12 +37,22 @@ type Point struct {
|
||||
|
||||
// ApplyAnnotations decodes a PNG, draws all annotations, and re-encodes it.
|
||||
func ApplyAnnotations(pngBytes []byte, annotations []Annotation, scale float64) ([]byte, error) {
|
||||
return ApplyAnnotationsWithScale(pngBytes, annotations, scale, scale)
|
||||
}
|
||||
|
||||
// ApplyAnnotationsWithScale decodes a PNG, draws all annotations using the
|
||||
// actual device-pixel scale of the captured image, and re-encodes it.
|
||||
func ApplyAnnotationsWithScale(pngBytes []byte, annotations []Annotation, scaleX, scaleY float64) ([]byte, error) {
|
||||
if len(annotations) == 0 {
|
||||
return pngBytes, nil
|
||||
}
|
||||
if scale <= 0 {
|
||||
scale = 1
|
||||
if scaleX <= 0 {
|
||||
scaleX = 1
|
||||
}
|
||||
if scaleY <= 0 {
|
||||
scaleY = scaleX
|
||||
}
|
||||
strokeScale := math.Max(scaleX, scaleY)
|
||||
|
||||
src, err := png.Decode(bytes.NewReader(pngBytes))
|
||||
if err != nil {
|
||||
@@ -55,16 +67,20 @@ func ApplyAnnotations(pngBytes []byte, annotations []Annotation, scale float64)
|
||||
if err != nil {
|
||||
c = color.RGBA{R: 59, G: 130, B: 246, A: 255}
|
||||
}
|
||||
width := int(math.Max(2, math.Round(3*scale)))
|
||||
strokeWidth := ann.StrokeWidth
|
||||
if strokeWidth <= 0 {
|
||||
strokeWidth = 3
|
||||
}
|
||||
width := int(math.Max(1, math.Round(strokeWidth*strokeScale)))
|
||||
switch ann.Tool {
|
||||
case "pen":
|
||||
drawPolyline(dst, ann.Points, scale, width, c)
|
||||
drawPolyline(dst, ann.Points, scaleX, scaleY, width, c)
|
||||
case "rect":
|
||||
drawRectOutline(dst, ann.Points, scale, width, c)
|
||||
drawRectOutline(dst, ann.Points, scaleX, scaleY, width, c)
|
||||
case "ellipse":
|
||||
drawEllipseOutline(dst, ann.Points, scale, width, c)
|
||||
drawEllipseOutline(dst, ann.Points, scaleX, scaleY, width, c)
|
||||
case "text":
|
||||
drawTextAnnotation(dst, ann, scale, c)
|
||||
drawTextAnnotation(dst, ann, scaleX, scaleY, c)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -92,22 +108,22 @@ func parseHexColor(hex string) (color.RGBA, error) {
|
||||
}, nil
|
||||
}
|
||||
|
||||
func drawPolyline(img *image.RGBA, points []Point, scale float64, width int, c color.RGBA) {
|
||||
func drawPolyline(img *image.RGBA, points []Point, scaleX, scaleY float64, width int, c color.RGBA) {
|
||||
if len(points) == 1 {
|
||||
drawDot(img, scalePoint(points[0], scale), width, c)
|
||||
drawDot(img, scalePoint(points[0], scaleX, scaleY), width, c)
|
||||
return
|
||||
}
|
||||
for i := 1; i < len(points); i++ {
|
||||
drawLine(img, scalePoint(points[i-1], scale), scalePoint(points[i], scale), width, c)
|
||||
drawLine(img, scalePoint(points[i-1], scaleX, scaleY), scalePoint(points[i], scaleX, scaleY), width, c)
|
||||
}
|
||||
}
|
||||
|
||||
func drawRectOutline(img *image.RGBA, points []Point, scale float64, width int, c color.RGBA) {
|
||||
func drawRectOutline(img *image.RGBA, points []Point, scaleX, scaleY float64, width int, c color.RGBA) {
|
||||
if len(points) < 2 {
|
||||
return
|
||||
}
|
||||
a := scalePoint(points[0], scale)
|
||||
b := scalePoint(points[len(points)-1], scale)
|
||||
a := scalePoint(points[0], scaleX, scaleY)
|
||||
b := scalePoint(points[len(points)-1], scaleX, scaleY)
|
||||
x1, x2 := ordered(a.X, b.X)
|
||||
y1, y2 := ordered(a.Y, b.Y)
|
||||
drawLine(img, Point{X: x1, Y: y1}, Point{X: x2, Y: y1}, width, c)
|
||||
@@ -116,12 +132,12 @@ func drawRectOutline(img *image.RGBA, points []Point, scale float64, width int,
|
||||
drawLine(img, Point{X: x1, Y: y2}, Point{X: x1, Y: y1}, width, c)
|
||||
}
|
||||
|
||||
func drawEllipseOutline(img *image.RGBA, points []Point, scale float64, width int, c color.RGBA) {
|
||||
func drawEllipseOutline(img *image.RGBA, points []Point, scaleX, scaleY float64, width int, c color.RGBA) {
|
||||
if len(points) < 2 {
|
||||
return
|
||||
}
|
||||
a := scalePoint(points[0], scale)
|
||||
b := scalePoint(points[len(points)-1], scale)
|
||||
a := scalePoint(points[0], scaleX, scaleY)
|
||||
b := scalePoint(points[len(points)-1], scaleX, scaleY)
|
||||
x1, x2 := ordered(a.X, b.X)
|
||||
y1, y2 := ordered(a.Y, b.Y)
|
||||
rx := (x2 - x1) / 2
|
||||
@@ -180,8 +196,8 @@ func drawDot(img *image.RGBA, p Point, width int, c color.RGBA) {
|
||||
}
|
||||
}
|
||||
|
||||
func scalePoint(p Point, scale float64) Point {
|
||||
return Point{X: p.X * scale, Y: p.Y * scale}
|
||||
func scalePoint(p Point, scaleX, scaleY float64) Point {
|
||||
return Point{X: p.X * scaleX, Y: p.Y * scaleY}
|
||||
}
|
||||
|
||||
func ordered(a, b float64) (float64, float64) {
|
||||
@@ -191,7 +207,7 @@ func ordered(a, b float64) (float64, float64) {
|
||||
return b, a
|
||||
}
|
||||
|
||||
func drawTextAnnotation(img *image.RGBA, ann Annotation, scale float64, c color.RGBA) {
|
||||
func drawTextAnnotation(img *image.RGBA, ann Annotation, scaleX, scaleY float64, c color.RGBA) {
|
||||
if len(ann.Points) == 0 {
|
||||
return
|
||||
}
|
||||
@@ -199,16 +215,20 @@ func drawTextAnnotation(img *image.RGBA, ann Annotation, scale float64, c color.
|
||||
if text == "" {
|
||||
return
|
||||
}
|
||||
face := annotationFontFace(20 * scale)
|
||||
fontSize := ann.FontSize
|
||||
if fontSize <= 0 {
|
||||
fontSize = 20
|
||||
}
|
||||
face := annotationFontFace(fontSize * scaleY)
|
||||
if face == nil {
|
||||
face = basicfont.Face7x13
|
||||
}
|
||||
|
||||
origin := scalePoint(ann.Points[0], scale)
|
||||
origin := scalePoint(ann.Points[0], scaleX, scaleY)
|
||||
metrics := face.Metrics()
|
||||
lineHeight := metrics.Height
|
||||
if lineHeight <= 0 {
|
||||
lineHeight = fixed.I(int(math.Ceil(24 * scale)))
|
||||
lineHeight = fixed.I(int(math.Ceil(fontSize * 1.2 * scaleY)))
|
||||
}
|
||||
d := &xfont.Drawer{
|
||||
Dst: img,
|
||||
|
||||
@@ -93,3 +93,81 @@ func TestApplyAnnotationsDrawsTextIntoPNG(t *testing.T) {
|
||||
t.Fatalf("expected red text pixels near annotation point")
|
||||
}
|
||||
}
|
||||
|
||||
func TestApplyAnnotationsWithScaleDrawsTextAtDevicePosition(t *testing.T) {
|
||||
src := image.NewRGBA(image.Rect(0, 0, 220, 120))
|
||||
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 := ApplyAnnotationsWithScale(buf.Bytes(), []Annotation{
|
||||
{
|
||||
Tool: "text",
|
||||
Color: "#ef4444",
|
||||
Points: []Point{{X: 50, Y: 20}},
|
||||
Text: "T",
|
||||
FontSize: 20,
|
||||
},
|
||||
}, 2, 2)
|
||||
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)
|
||||
}
|
||||
minX := 999
|
||||
found := false
|
||||
for y := 0; y < img.Bounds().Dy(); y++ {
|
||||
for x := 0; x < img.Bounds().Dx(); x++ {
|
||||
got := color.RGBAModel.Convert(img.At(x, y)).(color.RGBA)
|
||||
if got.R > 180 && got.G < 180 && got.B < 180 {
|
||||
found = true
|
||||
if x < minX {
|
||||
minX = x
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
if !found {
|
||||
t.Fatalf("expected red text pixels")
|
||||
}
|
||||
if minX < 90 {
|
||||
t.Fatalf("expected text to be drawn near scaled x=100, min red x=%d", minX)
|
||||
}
|
||||
}
|
||||
|
||||
func TestApplyAnnotationsUsesStrokeWidth(t *testing.T) {
|
||||
src := image.NewRGBA(image.Rect(0, 0, 80, 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: "rect",
|
||||
Color: "#ef4444",
|
||||
Points: []Point{{X: 20, Y: 20}, {X: 60, Y: 60}},
|
||||
StrokeWidth: 8,
|
||||
},
|
||||
}, 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)
|
||||
}
|
||||
got := color.RGBAModel.Convert(img.At(20, 24)).(color.RGBA)
|
||||
if got.R < 180 || got.G > 180 || got.B > 180 {
|
||||
t.Fatalf("expected thick rectangle stroke to cover y=24, got %#v", got)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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
|
||||
}
|
||||
@@ -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")
|
||||
}
|
||||
}
|
||||
@@ -107,6 +107,29 @@ type LLMConfig struct {
|
||||
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.
|
||||
const (
|
||||
// SSHAuthBuiltin is the legacy combined method (password → agent → key
|
||||
@@ -129,6 +152,12 @@ const (
|
||||
LLMProviderOpenAI = "openai"
|
||||
)
|
||||
|
||||
// Built-in OCR provider identifiers.
|
||||
const (
|
||||
OCRProviderAliyun = "aliyun"
|
||||
OCRProviderVolcengine = "volcengine"
|
||||
)
|
||||
|
||||
// Theme preference identifiers stored in AppConfig.Theme.
|
||||
const (
|
||||
ThemeAuto = "auto"
|
||||
@@ -172,6 +201,9 @@ type AppConfig struct {
|
||||
|
||||
// LLM holds provider settings for the "copy summary" screenshot action.
|
||||
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.
|
||||
@@ -190,6 +222,7 @@ func DefaultAppConfig() AppConfig {
|
||||
StrictHostKey: false,
|
||||
},
|
||||
LLM: DefaultLLMConfig(),
|
||||
OCR: DefaultOCRConfig(),
|
||||
}
|
||||
cfg.Normalize()
|
||||
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
|
||||
// preserving any user-provided provider fields.
|
||||
func (c *AppConfig) Normalize() {
|
||||
@@ -295,6 +355,43 @@ func (c *AppConfig) Normalize() {
|
||||
}
|
||||
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.
|
||||
@@ -329,3 +426,32 @@ func (c AppConfig) IsLLMConfigured() bool {
|
||||
_, provider, ok := c.ActiveLLMProvider()
|
||||
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
|
||||
}
|
||||
|
||||
@@ -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
|
||||
}
|
||||
@@ -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)
|
||||
}
|
||||
}
|
||||
@@ -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
|
||||
func nativeOverlayCancel() {
|
||||
app := consumeNativeOverlayApp()
|
||||
|
||||
+530
-97
@@ -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 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 nativeOverlayOCR(int x, int y, int w, int h, const char *annotationsJSON);
|
||||
extern void nativeOverlayCancel(void);
|
||||
|
||||
static NSWindow *nativeOverlayWindow = nil;
|
||||
@@ -81,6 +82,50 @@ static id nativeOverlayKeyMonitor = nil;
|
||||
}
|
||||
@end
|
||||
|
||||
@interface SnipToolSettingsView : NSView
|
||||
@property CGFloat arrowX;
|
||||
@end
|
||||
|
||||
@implementation SnipToolSettingsView
|
||||
- (BOOL)isFlipped { return YES; }
|
||||
- (void)drawRect:(NSRect)dirtyRect {
|
||||
[[NSColor clearColor] setFill];
|
||||
NSRectFill(dirtyRect);
|
||||
|
||||
CGFloat arrowHalf = 8;
|
||||
CGFloat arrowH = 9;
|
||||
CGFloat radius = 8;
|
||||
NSRect body = NSMakeRect(0, arrowH, self.bounds.size.width, self.bounds.size.height - arrowH);
|
||||
CGFloat arrowX = MAX(18, MIN(_arrowX, self.bounds.size.width - 18));
|
||||
|
||||
NSBezierPath *path = [NSBezierPath bezierPath];
|
||||
[path moveToPoint:NSMakePoint(NSMinX(body) + radius, NSMinY(body))];
|
||||
[path lineToPoint:NSMakePoint(arrowX - arrowHalf, NSMinY(body))];
|
||||
[path lineToPoint:NSMakePoint(arrowX, 0)];
|
||||
[path lineToPoint:NSMakePoint(arrowX + arrowHalf, NSMinY(body))];
|
||||
[path lineToPoint:NSMakePoint(NSMaxX(body) - radius, NSMinY(body))];
|
||||
[path curveToPoint:NSMakePoint(NSMaxX(body), NSMinY(body) + radius)
|
||||
controlPoint1:NSMakePoint(NSMaxX(body) - radius / 2, NSMinY(body))
|
||||
controlPoint2:NSMakePoint(NSMaxX(body), NSMinY(body) + radius / 2)];
|
||||
[path lineToPoint:NSMakePoint(NSMaxX(body), NSMaxY(body) - radius)];
|
||||
[path curveToPoint:NSMakePoint(NSMaxX(body) - radius, NSMaxY(body))
|
||||
controlPoint1:NSMakePoint(NSMaxX(body), NSMaxY(body) - radius / 2)
|
||||
controlPoint2:NSMakePoint(NSMaxX(body) - radius / 2, NSMaxY(body))];
|
||||
[path lineToPoint:NSMakePoint(NSMinX(body) + radius, NSMaxY(body))];
|
||||
[path curveToPoint:NSMakePoint(NSMinX(body), NSMaxY(body) - radius)
|
||||
controlPoint1:NSMakePoint(NSMinX(body) + radius / 2, NSMaxY(body))
|
||||
controlPoint2:NSMakePoint(NSMinX(body), NSMaxY(body) - radius / 2)];
|
||||
[path lineToPoint:NSMakePoint(NSMinX(body), NSMinY(body) + radius)];
|
||||
[path curveToPoint:NSMakePoint(NSMinX(body) + radius, NSMinY(body))
|
||||
controlPoint1:NSMakePoint(NSMinX(body), NSMinY(body) + radius / 2)
|
||||
controlPoint2:NSMakePoint(NSMinX(body) + radius / 2, NSMinY(body))];
|
||||
[path closePath];
|
||||
|
||||
[[NSColor colorWithCalibratedWhite:1 alpha:0.96] setFill];
|
||||
[path fill];
|
||||
}
|
||||
@end
|
||||
|
||||
@interface SnipNativeOverlayView : NSView
|
||||
@property BOOL hasSelection;
|
||||
@property BOOL creating;
|
||||
@@ -94,6 +139,8 @@ static id nativeOverlayKeyMonitor = nil;
|
||||
@property NSString *resizeHandle;
|
||||
@property NSString *activeTool;
|
||||
@property(strong) NSColor *activeColor;
|
||||
@property CGFloat activeStrokeWidth;
|
||||
@property CGFloat activeFontSize;
|
||||
@property(strong) NSMutableArray<NSDictionary *> *annotations;
|
||||
@property(strong) NSMutableDictionary *draftAnnotation;
|
||||
@property(strong) NSButton *cancelButton;
|
||||
@@ -103,18 +150,28 @@ static id nativeOverlayKeyMonitor = nil;
|
||||
@property(strong) NSButton *clipboardButton;
|
||||
@property(strong) NSButton *saveButton;
|
||||
@property(strong) NSButton *saveRemoteButton;
|
||||
@property(strong) NSButton *ocrButton;
|
||||
@property(strong) NSButton *summaryButton;
|
||||
@property(strong) NSButton *uploadButton;
|
||||
@property(strong) NSButton *penButton;
|
||||
@property(strong) NSButton *rectButton;
|
||||
@property(strong) NSButton *ellipseButton;
|
||||
@property(strong) NSButton *textButton;
|
||||
@property(strong) NSButton *colorButton;
|
||||
@property(strong) NSButton *undoButton;
|
||||
@property(strong) NSView *paletteView;
|
||||
@property(strong) SnipToolSettingsView *toolSettingsView;
|
||||
@property(strong) NSView *settingsDivider;
|
||||
@property(strong) NSMutableArray<NSButton *> *strokeSettingButtons;
|
||||
@property(strong) NSMutableArray<NSButton *> *fontSettingButtons;
|
||||
@property(strong) NSMutableArray<NSButton *> *colorSettingButtons;
|
||||
@property(strong) NSView *actionToolbarBg;
|
||||
@property(strong) NSTextField *textEditor;
|
||||
@property NSPoint textEditorLocalPoint;
|
||||
@property NSInteger textEditorAnnotationIndex;
|
||||
@property(strong) NSColor *textEditorColor;
|
||||
@property NSInteger selectedTextAnnotationIndex;
|
||||
@property BOOL draggingTextAnnotation;
|
||||
@property NSPoint textDragStartLocalPoint;
|
||||
@property NSPoint textDragOriginalLocalPoint;
|
||||
@property(strong) NSTextField *sizeLabel;
|
||||
@property(strong) NSTextField *hintLabel;
|
||||
- (void)syncControls;
|
||||
@@ -124,9 +181,17 @@ static id nativeOverlayKeyMonitor = nil;
|
||||
- (void)copySelection;
|
||||
- (void)saveSelection;
|
||||
- (void)saveRemoteSelection;
|
||||
- (void)ocrSelection;
|
||||
- (void)summarizeSelection;
|
||||
- (void)cancelSelection;
|
||||
- (void)beginTextAnnotationAtPoint:(NSPoint)localPoint;
|
||||
- (void)beginEditingTextAnnotationAtIndex:(NSInteger)index;
|
||||
- (NSInteger)textAnnotationIndexAtPoint:(NSPoint)p;
|
||||
- (NSRect)textAnnotationRectForItem:(NSDictionary *)item;
|
||||
- (NSPoint)clampedTextLocalPoint:(NSPoint)localPoint text:(NSString *)text fontSize:(CGFloat)fontSize;
|
||||
- (void)selectStrokeWidth:(NSButton *)sender;
|
||||
- (void)selectFontSize:(NSButton *)sender;
|
||||
- (void)selectToolColor:(NSButton *)sender;
|
||||
- (BOOL)isEditingText;
|
||||
- (void)commitTextEditor;
|
||||
- (void)cancelTextEditor;
|
||||
@@ -144,15 +209,20 @@ static id nativeOverlayKeyMonitor = nil;
|
||||
|
||||
_activeTool = nil;
|
||||
_activeColor = [NSColor colorWithCalibratedRed:239.0/255.0 green:68.0/255.0 blue:68.0/255.0 alpha:1.0];
|
||||
_activeStrokeWidth = 3.0;
|
||||
_activeFontSize = 20.0;
|
||||
_annotations = [NSMutableArray array];
|
||||
_textEditorAnnotationIndex = -1;
|
||||
_selectedTextAnnotationIndex = -1;
|
||||
|
||||
// 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
|
||||
// already have explicit on/off "active" styling.
|
||||
_cancelButton = [SnipHoverButton buttonWithTitle:@"" target:self action:@selector(cancelSelection)];
|
||||
_clipboardButton = [SnipHoverButton buttonWithTitle:@"" target:self action:@selector(copySelection)];
|
||||
_saveButton = [SnipHoverButton buttonWithTitle:@"" target:self action:@selector(saveSelection)];
|
||||
_saveRemoteButton = [SnipHoverButton buttonWithTitle:@"" target:self action:@selector(saveRemoteSelection)];
|
||||
_ocrButton = [SnipHoverButton buttonWithTitle:@"" target:self action:@selector(ocrSelection)];
|
||||
_summaryButton = [SnipHoverButton buttonWithTitle:@"" target:self action:@selector(summarizeSelection)];
|
||||
_uploadButton = [SnipHoverButton buttonWithTitle:@"" target:self action:@selector(confirmSelection)];
|
||||
// Tooltip strings shown on hover for each action button.
|
||||
@@ -161,15 +231,19 @@ static id nativeOverlayKeyMonitor = nil;
|
||||
[_clipboardButton setToolTip:@"复制图片"];
|
||||
[_saveButton setToolTip:@"保存本地"];
|
||||
[_saveRemoteButton setToolTip:@"保存远端"];
|
||||
[_ocrButton setToolTip:@"提取文字"];
|
||||
[_summaryButton setToolTip:@"复制总结"];
|
||||
[_uploadButton setToolTip:@"上传云端"];
|
||||
_penButton = [NSButton buttonWithTitle:@"" target:self action:@selector(selectPen)];
|
||||
_rectButton = [NSButton buttonWithTitle:@"" target:self action:@selector(selectRect)];
|
||||
_ellipseButton = [NSButton buttonWithTitle:@"" target:self action:@selector(selectEllipse)];
|
||||
_textButton = [NSButton buttonWithTitle:@"" target:self action:@selector(selectText)];
|
||||
_colorButton = [NSButton buttonWithTitle:@"" target:self action:@selector(togglePalette)];
|
||||
_undoButton = [NSButton buttonWithTitle:@"" target:self action:@selector(undoAnnotation)];
|
||||
_paletteView = [[NSView alloc] initWithFrame:NSZeroRect];
|
||||
_toolSettingsView = [[SnipToolSettingsView alloc] initWithFrame:NSZeroRect];
|
||||
_settingsDivider = [[NSView alloc] initWithFrame:NSZeroRect];
|
||||
_strokeSettingButtons = [NSMutableArray array];
|
||||
_fontSettingButtons = [NSMutableArray array];
|
||||
_colorSettingButtons = [NSMutableArray array];
|
||||
// Dark rounded background container behind the action icon buttons,
|
||||
// mirroring the look of `.action-toolbar` in the Vue overlay.
|
||||
_actionToolbarBg = [[NSView alloc] initWithFrame:NSZeroRect];
|
||||
@@ -183,10 +257,10 @@ static id nativeOverlayKeyMonitor = nil;
|
||||
// Add the action toolbar background BEFORE the buttons so it sits
|
||||
// behind them in the view hierarchy (AppKit z-order = subview order).
|
||||
[self addSubview:_actionToolbarBg];
|
||||
for (NSView *view in @[_cancelButton, _clipboardButton, _saveButton, _saveRemoteButton, _summaryButton, _uploadButton, _penButton, _rectButton, _ellipseButton, _textButton, _colorButton, _undoButton, _paletteView, _sizeLabel, _hintLabel]) {
|
||||
for (NSView *view in @[_cancelButton, _clipboardButton, _saveButton, _saveRemoteButton, _ocrButton, _summaryButton, _uploadButton, _penButton, _rectButton, _ellipseButton, _textButton, _undoButton, _toolSettingsView, _sizeLabel, _hintLabel]) {
|
||||
[self addSubview:view];
|
||||
}
|
||||
for (NSView *view in @[_cancelButton, _clipboardButton, _saveButton, _saveRemoteButton, _summaryButton, _uploadButton, _penButton, _rectButton, _ellipseButton, _textButton, _colorButton, _undoButton, _paletteView, _actionToolbarBg]) {
|
||||
for (NSView *view in @[_cancelButton, _clipboardButton, _saveButton, _saveRemoteButton, _ocrButton, _summaryButton, _uploadButton, _penButton, _rectButton, _ellipseButton, _textButton, _undoButton, _toolSettingsView, _actionToolbarBg]) {
|
||||
[view setHidden:YES];
|
||||
}
|
||||
[_sizeLabel setHidden:YES];
|
||||
@@ -249,8 +323,8 @@ static id nativeOverlayKeyMonitor = nil;
|
||||
}
|
||||
|
||||
- (void)styleControls {
|
||||
// Cancel / Copy / Save / Save-remote / Upload — square icon buttons
|
||||
// rendered from the user-supplied SVG assets. All five default to white
|
||||
// Cancel / Copy / Save / Save-remote / OCR / Summary / Upload — square icon buttons
|
||||
// rendered from the user-supplied SVG assets. All actions default to white
|
||||
// and animate to a per-action accent color on hover (cancel = red,
|
||||
// others = blue). The base "white default" replaces the previous blue
|
||||
// upload tint so the toolbar reads as a uniform icon row.
|
||||
@@ -260,6 +334,7 @@ static id nativeOverlayKeyMonitor = nil;
|
||||
// save-remote.svg — provided by the user; its content is the same arrow
|
||||
// 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 *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>"];
|
||||
// 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>"];
|
||||
@@ -268,10 +343,11 @@ static id nativeOverlayKeyMonitor = nil;
|
||||
[self styleIconButton:_clipboardButton image:copyIcon];
|
||||
[self styleIconButton:_saveButton image:saveIcon];
|
||||
[self styleIconButton:_saveRemoteButton image:saveRemoteIcon];
|
||||
[self styleIconButton:_ocrButton image:ocrIcon];
|
||||
[self styleIconButton:_summaryButton image:summaryIcon];
|
||||
[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
|
||||
// glance-able cue for destructive vs. constructive actions.
|
||||
NSColor *baseWhite = [NSColor whiteColor];
|
||||
@@ -281,12 +357,14 @@ static id nativeOverlayKeyMonitor = nil;
|
||||
SnipHoverButton *clipboardHB = (SnipHoverButton *)_clipboardButton;
|
||||
SnipHoverButton *saveHB = (SnipHoverButton *)_saveButton;
|
||||
SnipHoverButton *saveRemoteHB = (SnipHoverButton *)_saveRemoteButton;
|
||||
SnipHoverButton *ocrHB = (SnipHoverButton *)_ocrButton;
|
||||
SnipHoverButton *summaryHB = (SnipHoverButton *)_summaryButton;
|
||||
SnipHoverButton *uploadHB = (SnipHoverButton *)_uploadButton;
|
||||
cancelHB.baseColor = baseWhite; cancelHB.hoverColor = hoverRed;
|
||||
clipboardHB.baseColor = baseWhite; clipboardHB.hoverColor = hoverBlue;
|
||||
saveHB.baseColor = baseWhite; saveHB.hoverColor = hoverBlue;
|
||||
saveRemoteHB.baseColor = baseWhite; saveRemoteHB.hoverColor = hoverBlue;
|
||||
ocrHB.baseColor = baseWhite; ocrHB.hoverColor = hoverBlue;
|
||||
summaryHB.baseColor = baseWhite; summaryHB.hoverColor = hoverBlue;
|
||||
uploadHB.baseColor = baseWhite; uploadHB.hoverColor = hoverBlue;
|
||||
|
||||
@@ -294,11 +372,62 @@ static id nativeOverlayKeyMonitor = nil;
|
||||
[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:_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:_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 layer] setCornerRadius:8];
|
||||
[[_paletteView layer] setBackgroundColor:[[NSColor colorWithCalibratedWhite:0.11 alpha:0.96] CGColor]];
|
||||
|
||||
[_toolSettingsView setWantsLayer:NO];
|
||||
[_settingsDivider setWantsLayer:YES];
|
||||
[[_settingsDivider layer] setBackgroundColor:[[NSColor colorWithCalibratedWhite:0.82 alpha:1] CGColor]];
|
||||
[_toolSettingsView addSubview:_settingsDivider];
|
||||
|
||||
for (NSNumber *width in @[@2, @4, @6]) {
|
||||
NSButton *button = [NSButton buttonWithTitle:@"●" target:self action:@selector(selectStrokeWidth:)];
|
||||
[button setTag:[width integerValue]];
|
||||
[button setBordered:NO];
|
||||
[button setWantsLayer:YES];
|
||||
[[button layer] setCornerRadius:6];
|
||||
NSMutableAttributedString *title = [[NSMutableAttributedString alloc] initWithString:@"●"];
|
||||
[title addAttribute:NSFontAttributeName value:[NSFont systemFontOfSize:8 + [width doubleValue] * 2 weight:NSFontWeightBold] range:NSMakeRange(0, title.length)];
|
||||
[button setAttributedTitle:title];
|
||||
[_strokeSettingButtons addObject:button];
|
||||
[_toolSettingsView addSubview:button];
|
||||
}
|
||||
|
||||
for (NSNumber *size in @[@16, @20, @28, @36]) {
|
||||
NSButton *button = [NSButton buttonWithTitle:[NSString stringWithFormat:@"%ld", (long)[size integerValue]] target:self action:@selector(selectFontSize:)];
|
||||
[button setTag:[size integerValue]];
|
||||
[button setBordered:NO];
|
||||
[button setWantsLayer:YES];
|
||||
[[button layer] setCornerRadius:6];
|
||||
NSMutableAttributedString *title = [[NSMutableAttributedString alloc] initWithString:[button title]];
|
||||
[title addAttribute:NSForegroundColorAttributeName value:[NSColor colorWithCalibratedWhite:0.12 alpha:1] range:NSMakeRange(0, title.length)];
|
||||
[title addAttribute:NSFontAttributeName value:[NSFont systemFontOfSize:13 weight:NSFontWeightBold] range:NSMakeRange(0, title.length)];
|
||||
[button setAttributedTitle:title];
|
||||
[_fontSettingButtons addObject:button];
|
||||
[_toolSettingsView addSubview:button];
|
||||
}
|
||||
|
||||
NSArray *colors = @[
|
||||
[NSColor colorWithCalibratedRed:239.0/255.0 green:68.0/255.0 blue:68.0/255.0 alpha:1.0],
|
||||
[NSColor colorWithCalibratedRed:250.0/255.0 green:204.0/255.0 blue:21.0/255.0 alpha:1.0],
|
||||
[NSColor colorWithCalibratedRed:34.0/255.0 green:197.0/255.0 blue:94.0/255.0 alpha:1.0],
|
||||
[NSColor colorWithCalibratedRed:59.0/255.0 green:130.0/255.0 blue:246.0/255.0 alpha:1.0],
|
||||
[NSColor colorWithCalibratedWhite:0.0 alpha:1.0],
|
||||
[NSColor colorWithCalibratedRed:156.0/255.0 green:163.0/255.0 blue:175.0/255.0 alpha:1.0],
|
||||
[NSColor whiteColor]
|
||||
];
|
||||
for (NSUInteger i = 0; i < colors.count; i++) {
|
||||
NSButton *button = [NSButton buttonWithTitle:@"" target:self action:@selector(selectToolColor:)];
|
||||
[button setTag:(NSInteger)i];
|
||||
[button setBordered:NO];
|
||||
[button setWantsLayer:YES];
|
||||
[[button layer] setCornerRadius:4];
|
||||
[[button layer] setBorderWidth:1];
|
||||
[[button layer] setBorderColor:[[NSColor colorWithCalibratedWhite:0.12 alpha:0.22] CGColor]];
|
||||
[[button layer] setBackgroundColor:[colors[i] CGColor]];
|
||||
[_colorSettingButtons addObject:button];
|
||||
[_toolSettingsView addSubview:button];
|
||||
}
|
||||
objc_setAssociatedObject(_toolSettingsView, "snapgoColors", colors, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
|
||||
}
|
||||
|
||||
- (void)drawRect:(NSRect)dirtyRect {
|
||||
@@ -364,12 +493,107 @@ static id nativeOverlayKeyMonitor = nil;
|
||||
}
|
||||
}
|
||||
|
||||
- (CGFloat)fontSizeForTextItem:(NSDictionary *)item {
|
||||
NSNumber *fontSize = item[@"fontSize"];
|
||||
if (fontSize != nil && [fontSize doubleValue] > 0) {
|
||||
return [fontSize doubleValue];
|
||||
}
|
||||
return 20.0;
|
||||
}
|
||||
|
||||
- (CGFloat)strokeWidthForItem:(NSDictionary *)item {
|
||||
NSNumber *strokeWidth = item[@"strokeWidth"];
|
||||
if (strokeWidth != nil && [strokeWidth doubleValue] > 0) {
|
||||
return [strokeWidth doubleValue];
|
||||
}
|
||||
return 3.0;
|
||||
}
|
||||
|
||||
- (NSDictionary *)textAttributesWithFontSize:(CGFloat)fontSize color:(NSColor *)color {
|
||||
return @{
|
||||
NSFontAttributeName: [NSFont systemFontOfSize:fontSize weight:NSFontWeightSemibold],
|
||||
NSForegroundColorAttributeName: color ?: [NSColor whiteColor]
|
||||
};
|
||||
}
|
||||
|
||||
- (NSSize)textSizeForString:(NSString *)text fontSize:(CGFloat)fontSize {
|
||||
NSString *measured = text.length > 0 ? text : @" ";
|
||||
NSDictionary *attrs = [self textAttributesWithFontSize:fontSize color:[NSColor whiteColor]];
|
||||
NSRect bounds = [measured boundingRectWithSize:NSMakeSize(CGFLOAT_MAX, CGFLOAT_MAX)
|
||||
options:NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading
|
||||
attributes:attrs];
|
||||
return NSMakeSize(ceil(MAX(1.0, bounds.size.width)), ceil(MAX(fontSize * 1.2, bounds.size.height)));
|
||||
}
|
||||
|
||||
- (NSSize)textEditorSizeForString:(NSString *)text fontSize:(CGFloat)fontSize {
|
||||
NSSize textSize = [self textSizeForString:text fontSize:fontSize];
|
||||
return NSMakeSize(MAX(180, MIN(420, textSize.width + 28)), MAX(30, ceil(fontSize * 1.6)));
|
||||
}
|
||||
|
||||
- (NSRect)textEditorFrameForLocalPoint:(NSPoint)localPoint text:(NSString *)text fontSize:(CGFloat)fontSize {
|
||||
NSSize editorSize = [self textEditorSizeForString:text fontSize:fontSize];
|
||||
NSPoint clampedLocal = [self clampedTextLocalPoint:localPoint text:text fontSize:fontSize];
|
||||
NSRect frame = NSMakeRect(_selection.origin.x + clampedLocal.x, _selection.origin.y + clampedLocal.y, editorSize.width, editorSize.height);
|
||||
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));
|
||||
return frame;
|
||||
}
|
||||
|
||||
- (void)resizeTextEditorForCurrentFont {
|
||||
if (_textEditor == nil) {
|
||||
return;
|
||||
}
|
||||
NSString *text = [_textEditor stringValue] ?: @"";
|
||||
NSRect frame = [self textEditorFrameForLocalPoint:_textEditorLocalPoint text:text fontSize:_activeFontSize];
|
||||
[_textEditor setFrame:frame];
|
||||
_textEditorLocalPoint = [self localPoint:frame.origin];
|
||||
}
|
||||
|
||||
- (NSRect)textAnnotationRectForItem:(NSDictionary *)item {
|
||||
if (![item[@"tool"] isEqualToString:@"text"]) {
|
||||
return NSZeroRect;
|
||||
}
|
||||
NSArray *points = item[@"points"];
|
||||
NSString *text = item[@"text"];
|
||||
if (points.count == 0 || text.length == 0) {
|
||||
return NSZeroRect;
|
||||
}
|
||||
NSPoint p = [points[0] pointValue];
|
||||
CGFloat fontSize = [self fontSizeForTextItem:item];
|
||||
NSSize size = [self textSizeForString:text fontSize:fontSize];
|
||||
return NSMakeRect(_selection.origin.x + p.x, _selection.origin.y + p.y, size.width, size.height);
|
||||
}
|
||||
|
||||
- (NSPoint)clampedTextLocalPoint:(NSPoint)localPoint text:(NSString *)text fontSize:(CGFloat)fontSize {
|
||||
NSSize size = [self textSizeForString:text fontSize:fontSize];
|
||||
CGFloat maxX = MAX(0, _selection.size.width - size.width);
|
||||
CGFloat maxY = MAX(0, _selection.size.height - size.height);
|
||||
return NSMakePoint(MAX(0, MIN(localPoint.x, maxX)), MAX(0, MIN(localPoint.y, maxY)));
|
||||
}
|
||||
|
||||
- (NSInteger)textAnnotationIndexAtPoint:(NSPoint)p {
|
||||
for (NSInteger i = (NSInteger)_annotations.count - 1; i >= 0; i--) {
|
||||
NSDictionary *item = _annotations[(NSUInteger)i];
|
||||
if (![item[@"tool"] isEqualToString:@"text"]) {
|
||||
continue;
|
||||
}
|
||||
NSRect hitRect = NSInsetRect([self textAnnotationRectForItem:item], -6, -6);
|
||||
if (!NSIsEmptyRect(hitRect) && NSPointInRect(p, hitRect)) {
|
||||
return i;
|
||||
}
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
- (void)drawAnnotations {
|
||||
NSMutableArray *items = [NSMutableArray arrayWithArray:_annotations];
|
||||
if (_draftAnnotation != nil) {
|
||||
[items addObject:_draftAnnotation];
|
||||
}
|
||||
for (NSDictionary *item in items) {
|
||||
for (NSUInteger index = 0; index < items.count; index++) {
|
||||
NSDictionary *item = items[index];
|
||||
NSString *tool = item[@"tool"];
|
||||
NSColor *color = item[@"nsColor"];
|
||||
NSArray *points = item[@"points"];
|
||||
@@ -382,16 +606,24 @@ static id nativeOverlayKeyMonitor = nil;
|
||||
continue;
|
||||
}
|
||||
NSPoint p = [points[0] pointValue];
|
||||
NSDictionary *attrs = @{
|
||||
NSFontAttributeName: [NSFont systemFontOfSize:20 weight:NSFontWeightSemibold],
|
||||
NSForegroundColorAttributeName: color ?: [NSColor whiteColor]
|
||||
};
|
||||
CGFloat fontSize = [self fontSizeForTextItem:item];
|
||||
NSDictionary *attrs = [self textAttributesWithFontSize:fontSize color:color];
|
||||
[text drawAtPoint:NSMakePoint(_selection.origin.x + p.x, _selection.origin.y + p.y) withAttributes:attrs];
|
||||
if ((NSInteger)index == _selectedTextAnnotationIndex && index < _annotations.count) {
|
||||
NSRect rect = NSInsetRect([self textAnnotationRectForItem:item], -4, -3);
|
||||
NSBezierPath *selectionPath = [NSBezierPath bezierPathWithRoundedRect:rect xRadius:4 yRadius:4];
|
||||
[selectionPath setLineWidth:1.5];
|
||||
CGFloat dash[] = {4.0, 3.0};
|
||||
[selectionPath setLineDash:dash count:2 phase:0];
|
||||
[[NSColor colorWithCalibratedRed:59.0/255.0 green:130.0/255.0 blue:246.0/255.0 alpha:0.95] setStroke];
|
||||
[selectionPath stroke];
|
||||
}
|
||||
continue;
|
||||
}
|
||||
[color setStroke];
|
||||
CGFloat strokeWidth = [self strokeWidthForItem:item];
|
||||
NSBezierPath *path = [NSBezierPath bezierPath];
|
||||
[path setLineWidth:3];
|
||||
[path setLineWidth:strokeWidth];
|
||||
[path setLineCapStyle:NSLineCapStyleRound];
|
||||
[path setLineJoinStyle:NSLineJoinStyleRound];
|
||||
if ([tool isEqualToString:@"pen"]) {
|
||||
@@ -412,11 +644,11 @@ static id nativeOverlayKeyMonitor = nil;
|
||||
fabs(b.y - a.y));
|
||||
if ([tool isEqualToString:@"rect"]) {
|
||||
NSBezierPath *rectPath = [NSBezierPath bezierPathWithRect:r];
|
||||
[rectPath setLineWidth:3];
|
||||
[rectPath setLineWidth:strokeWidth];
|
||||
[rectPath stroke];
|
||||
} else if ([tool isEqualToString:@"ellipse"]) {
|
||||
NSBezierPath *ellipsePath = [NSBezierPath bezierPathWithOvalInRect:r];
|
||||
[ellipsePath setLineWidth:3];
|
||||
[ellipsePath setLineWidth:strokeWidth];
|
||||
[ellipsePath stroke];
|
||||
}
|
||||
}
|
||||
@@ -501,6 +733,7 @@ static id nativeOverlayKeyMonitor = nil;
|
||||
if (_textEditor != nil) {
|
||||
[self commitTextEditor];
|
||||
}
|
||||
_draggingTextAnnotation = NO;
|
||||
NSString *handle = [self resizeHandleAtPoint:p];
|
||||
if (handle != nil) {
|
||||
_resizing = YES;
|
||||
@@ -512,6 +745,34 @@ static id nativeOverlayKeyMonitor = nil;
|
||||
[self syncControls];
|
||||
return;
|
||||
}
|
||||
NSInteger textIndex = _hasSelection ? [self textAnnotationIndexAtPoint:p] : -1;
|
||||
if (textIndex >= 0) {
|
||||
_selectedTextAnnotationIndex = textIndex;
|
||||
NSDictionary *item = _annotations[(NSUInteger)textIndex];
|
||||
_activeTool = @"text";
|
||||
_activeColor = item[@"nsColor"] ?: _activeColor;
|
||||
_activeFontSize = [self fontSizeForTextItem:item];
|
||||
_creating = NO;
|
||||
_moving = NO;
|
||||
_resizing = NO;
|
||||
_annotating = NO;
|
||||
if ([event clickCount] >= 2) {
|
||||
[self beginEditingTextAnnotationAtIndex:textIndex];
|
||||
[self syncControls];
|
||||
[self setNeedsDisplay:YES];
|
||||
return;
|
||||
}
|
||||
NSArray *points = item[@"points"];
|
||||
if (points.count > 0) {
|
||||
_draggingTextAnnotation = YES;
|
||||
_textDragStartLocalPoint = [self localPoint:p];
|
||||
_textDragOriginalLocalPoint = [points[0] pointValue];
|
||||
}
|
||||
[self syncControls];
|
||||
[self setNeedsDisplay:YES];
|
||||
return;
|
||||
}
|
||||
_selectedTextAnnotationIndex = -1;
|
||||
if (_hasSelection && NSPointInRect(p, _selection) && [_activeTool isEqualToString:@"text"]) {
|
||||
_creating = NO;
|
||||
_moving = NO;
|
||||
@@ -535,6 +796,7 @@ static id nativeOverlayKeyMonitor = nil;
|
||||
@"tool": _activeTool,
|
||||
@"color": [self hexForColor:_activeColor],
|
||||
@"nsColor": _activeColor,
|
||||
@"strokeWidth": @(_activeStrokeWidth),
|
||||
@"points": [NSMutableArray arrayWithObject:[NSValue valueWithPoint:local]]
|
||||
} mutableCopy];
|
||||
} else if (_hasSelection && NSPointInRect(p, _selection)) {
|
||||
@@ -553,6 +815,7 @@ static id nativeOverlayKeyMonitor = nil;
|
||||
_selection = NSMakeRect(p.x, p.y, 0, 0);
|
||||
[_annotations removeAllObjects];
|
||||
_draftAnnotation = nil;
|
||||
_selectedTextAnnotationIndex = -1;
|
||||
}
|
||||
[self syncControls];
|
||||
}
|
||||
@@ -583,6 +846,17 @@ static id nativeOverlayKeyMonitor = nil;
|
||||
top = MAX(0, MIN(top, self.bounds.size.height));
|
||||
bottom = MAX(0, MIN(bottom, self.bounds.size.height));
|
||||
_selection = NSMakeRect(MIN(left, right), MIN(top, bottom), fabs(right - left), fabs(bottom - top));
|
||||
} else if (_draggingTextAnnotation && _selectedTextAnnotationIndex >= 0 && _selectedTextAnnotationIndex < (NSInteger)_annotations.count) {
|
||||
NSMutableDictionary *item = (NSMutableDictionary *)_annotations[(NSUInteger)_selectedTextAnnotationIndex];
|
||||
NSMutableArray *points = (NSMutableArray *)item[@"points"];
|
||||
if (points.count > 0) {
|
||||
NSPoint local = [self localPoint:p];
|
||||
NSPoint delta = NSMakePoint(local.x - _textDragStartLocalPoint.x, local.y - _textDragStartLocalPoint.y);
|
||||
NSPoint next = NSMakePoint(_textDragOriginalLocalPoint.x + delta.x, _textDragOriginalLocalPoint.y + delta.y);
|
||||
NSString *text = item[@"text"] ?: @"";
|
||||
CGFloat fontSize = [self fontSizeForTextItem:item];
|
||||
points[0] = [NSValue valueWithPoint:[self clampedTextLocalPoint:next text:text fontSize:fontSize]];
|
||||
}
|
||||
} else if (_annotating && _draftAnnotation != nil) {
|
||||
NSMutableArray *points = _draftAnnotation[@"points"];
|
||||
NSPoint local = [self localPoint:p];
|
||||
@@ -604,6 +878,7 @@ static id nativeOverlayKeyMonitor = nil;
|
||||
_creating = NO;
|
||||
_moving = NO;
|
||||
_resizing = NO;
|
||||
_draggingTextAnnotation = NO;
|
||||
if (_annotating && _draftAnnotation != nil) {
|
||||
[_annotations addObject:_draftAnnotation];
|
||||
_draftAnnotation = nil;
|
||||
@@ -646,6 +921,7 @@ static id nativeOverlayKeyMonitor = nil;
|
||||
[_clipboardButton setHidden:!visible];
|
||||
[_saveButton setHidden:!visible];
|
||||
[_saveRemoteButton setHidden:!visible];
|
||||
[_ocrButton setHidden:!visible];
|
||||
[_summaryButton setHidden:!visible];
|
||||
[_uploadButton setHidden:!visible];
|
||||
[_actionToolbarBg setHidden:!visible];
|
||||
@@ -653,8 +929,8 @@ static id nativeOverlayKeyMonitor = nil;
|
||||
[_rectButton setHidden:!visible];
|
||||
[_ellipseButton setHidden:!visible];
|
||||
[_textButton setHidden:!visible];
|
||||
[_colorButton setHidden:!visible];
|
||||
[_undoButton setHidden:!visible];
|
||||
[_toolSettingsView setHidden:!visible || _activeTool == nil];
|
||||
[_sizeLabel setHidden:!visible];
|
||||
[_hintLabel setHidden:_hasSelection];
|
||||
|
||||
@@ -665,12 +941,12 @@ static id nativeOverlayKeyMonitor = nil;
|
||||
[_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)];
|
||||
|
||||
// Action toolbar layout — 6 icon buttons, each 28x28, separated by 8px,
|
||||
// with 4px outer padding. Total = 6*28 + 5*8 + 2*4 = 216px wide.
|
||||
// Action toolbar layout — 7 icon buttons, each 28x28, separated by 8px,
|
||||
// with 4px outer padding. Total = 7*28 + 6*8 + 2*4 = 252px wide.
|
||||
CGFloat actionBtnSize = 28;
|
||||
CGFloat actionGap = 8;
|
||||
CGFloat actionPad = 4;
|
||||
NSInteger actionCount = 6;
|
||||
NSInteger actionCount = 7;
|
||||
CGFloat toolbarW = actionPad * 2 + actionBtnSize * actionCount + actionGap * (actionCount - 1);
|
||||
CGFloat toolbarH = 40;
|
||||
CGFloat x = _selection.origin.x + _selection.size.width - toolbarW;
|
||||
@@ -688,23 +964,62 @@ static id nativeOverlayKeyMonitor = nil;
|
||||
[_clipboardButton setFrame:NSMakeRect(baseX + (actionBtnSize + actionGap) * 1, btnY, actionBtnSize, actionBtnSize)];
|
||||
[_saveButton setFrame:NSMakeRect(baseX + (actionBtnSize + actionGap) * 2, btnY, actionBtnSize, actionBtnSize)];
|
||||
[_saveRemoteButton setFrame:NSMakeRect(baseX + (actionBtnSize + actionGap) * 3, btnY, actionBtnSize, actionBtnSize)];
|
||||
[_summaryButton setFrame:NSMakeRect(baseX + (actionBtnSize + actionGap) * 4, btnY, actionBtnSize, actionBtnSize)];
|
||||
[_uploadButton setFrame:NSMakeRect(baseX + (actionBtnSize + actionGap) * 5, btnY, actionBtnSize, actionBtnSize)];
|
||||
[_ocrButton setFrame:NSMakeRect(baseX + (actionBtnSize + actionGap) * 4, 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
|
||||
// 8px gap between the two groups, so they never overlap on tiny selections.
|
||||
CGFloat markW = 202;
|
||||
CGFloat markW = 178;
|
||||
CGFloat markGap = 8;
|
||||
CGFloat markX = MAX(0, x - markGap - markW);
|
||||
[_penButton setFrame:NSMakeRect(markX + 4, y + 4, 28, 28)];
|
||||
[_rectButton setFrame:NSMakeRect(markX + 36, y + 4, 28, 28)];
|
||||
[_ellipseButton setFrame:NSMakeRect(markX + 68, y + 4, 28, 28)];
|
||||
[_textButton setFrame:NSMakeRect(markX + 100, 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]];
|
||||
[_undoButton setEnabled:_annotations.count > 0];
|
||||
[_paletteView setFrame:NSMakeRect(markX, MAX(0, y - 78), 150, 70)];
|
||||
[_rectButton setFrame:NSMakeRect(markX + 38, y + 4, 28, 28)];
|
||||
[_ellipseButton setFrame:NSMakeRect(markX + 72, y + 4, 28, 28)];
|
||||
[_textButton setFrame:NSMakeRect(markX + 106, y + 4, 28, 28)];
|
||||
[_undoButton setFrame:NSMakeRect(markX + 144, y + 4, 28, 28)];
|
||||
[_undoButton setEnabled:YES];
|
||||
[[_undoButton layer] setOpacity:_annotations.count > 0 ? 1.0 : 0.35];
|
||||
|
||||
if (_activeTool != nil) {
|
||||
BOOL isText = [_activeTool isEqualToString:@"text"];
|
||||
CGFloat settingsW = isText ? 462 : 398;
|
||||
CGFloat settingsH = 58;
|
||||
CGFloat settingsX = MAX(8, MIN(markX, self.bounds.size.width - settingsW - 8));
|
||||
CGFloat settingsY = y + toolbarH + 4;
|
||||
if (settingsY + settingsH > self.bounds.size.height) {
|
||||
settingsY = y - settingsH - 8;
|
||||
}
|
||||
CGFloat activeCenter = markX + 18;
|
||||
if ([_activeTool isEqualToString:@"rect"]) activeCenter = markX + 52;
|
||||
if ([_activeTool isEqualToString:@"ellipse"]) activeCenter = markX + 86;
|
||||
if ([_activeTool isEqualToString:@"text"]) activeCenter = markX + 120;
|
||||
[_toolSettingsView setFrame:NSMakeRect(settingsX, settingsY, settingsW, settingsH)];
|
||||
[_toolSettingsView setArrowX:MAX(18, MIN(activeCenter - settingsX, settingsW - 18))];
|
||||
[_toolSettingsView setNeedsDisplay:YES];
|
||||
|
||||
CGFloat contentY = 18;
|
||||
CGFloat xCursor = 14;
|
||||
for (NSButton *button in _strokeSettingButtons) {
|
||||
[button setHidden:isText];
|
||||
[button setFrame:NSMakeRect(xCursor, contentY, 32, 32)];
|
||||
xCursor += 42;
|
||||
}
|
||||
xCursor = 14;
|
||||
for (NSButton *button in _fontSettingButtons) {
|
||||
[button setHidden:!isText];
|
||||
[button setFrame:NSMakeRect(xCursor, contentY, 40, 32)];
|
||||
xCursor += 48;
|
||||
}
|
||||
CGFloat groupW = isText ? (4 * 40 + 3 * 8) : (3 * 32 + 2 * 10);
|
||||
CGFloat dividerX = 14 + groupW + 12;
|
||||
[_settingsDivider setFrame:NSMakeRect(dividerX, contentY + 3, 1, 26)];
|
||||
CGFloat colorX = dividerX + 18;
|
||||
for (NSButton *button in _colorSettingButtons) {
|
||||
[button setFrame:NSMakeRect(colorX, contentY + 5, 22, 22)];
|
||||
colorX += 34;
|
||||
}
|
||||
}
|
||||
[self updateToolButtonStates];
|
||||
}
|
||||
|
||||
@@ -717,46 +1032,125 @@ static id nativeOverlayKeyMonitor = nil;
|
||||
: [NSColor clearColor];
|
||||
[[button layer] setBackgroundColor:[bg CGColor]];
|
||||
}
|
||||
[self updateToolSettingsStates];
|
||||
}
|
||||
|
||||
- (void)toggleTool:(NSString *)tool {
|
||||
_activeTool = [_activeTool isEqualToString:tool] ? nil : tool;
|
||||
[self syncControls];
|
||||
}
|
||||
- (void)selectPen { [self toggleTool:@"pen"]; }
|
||||
- (void)selectRect { [self toggleTool:@"rect"]; }
|
||||
- (void)selectEllipse { [self toggleTool:@"ellipse"]; }
|
||||
- (void)selectText { [self toggleTool:@"text"]; }
|
||||
- (void)undoAnnotation {
|
||||
[self cancelTextEditor];
|
||||
if (_annotations.count > 0) {
|
||||
[_annotations removeLastObject];
|
||||
[self syncControls];
|
||||
[self setNeedsDisplay:YES];
|
||||
- (void)updateToolSettingsStates {
|
||||
NSColor *activeBg = [NSColor colorWithCalibratedRed:234.0/255.0 green:241.0/255.0 blue:1.0 alpha:1.0];
|
||||
NSColor *clear = [NSColor clearColor];
|
||||
NSColor *activeFg = [NSColor colorWithCalibratedRed:37.0/255.0 green:99.0/255.0 blue:235.0/255.0 alpha:1.0];
|
||||
NSColor *normalFg = [NSColor colorWithCalibratedWhite:0.12 alpha:1.0];
|
||||
|
||||
for (NSButton *button in _strokeSettingButtons) {
|
||||
BOOL active = fabs((CGFloat)button.tag - _activeStrokeWidth) < 0.1;
|
||||
[[button layer] setBackgroundColor:[(active ? activeBg : clear) CGColor]];
|
||||
NSMutableAttributedString *title = [[button attributedTitle] mutableCopy];
|
||||
if (title.length > 0) {
|
||||
[title addAttribute:NSForegroundColorAttributeName value:(active ? activeFg : normalFg) range:NSMakeRange(0, title.length)];
|
||||
[button setAttributedTitle:title];
|
||||
}
|
||||
}
|
||||
for (NSButton *button in _fontSettingButtons) {
|
||||
BOOL active = fabs((CGFloat)button.tag - _activeFontSize) < 0.1;
|
||||
[[button layer] setBackgroundColor:[(active ? activeBg : clear) CGColor]];
|
||||
NSMutableAttributedString *title = [[button attributedTitle] mutableCopy];
|
||||
if (title.length > 0) {
|
||||
[title addAttribute:NSForegroundColorAttributeName value:(active ? activeFg : normalFg) range:NSMakeRange(0, title.length)];
|
||||
[button setAttributedTitle:title];
|
||||
}
|
||||
}
|
||||
|
||||
NSString *activeHex = [self hexForColor:_activeColor];
|
||||
NSArray *colors = objc_getAssociatedObject(_toolSettingsView, "snapgoColors");
|
||||
for (NSUInteger i = 0; i < _colorSettingButtons.count; i++) {
|
||||
NSButton *button = _colorSettingButtons[i];
|
||||
NSString *hex = i < colors.count ? [self hexForColor:colors[i]] : @"";
|
||||
BOOL active = [hex isEqualToString:activeHex];
|
||||
[[button layer] setBorderWidth:active ? 2 : 1];
|
||||
[[button layer] setBorderColor:[(active ? activeFg : [NSColor colorWithCalibratedWhite:0.12 alpha:0.22]) CGColor]];
|
||||
}
|
||||
}
|
||||
|
||||
- (void)beginTextAnnotationAtPoint:(NSPoint)localPoint {
|
||||
- (void)selectTool:(NSString *)tool {
|
||||
_activeTool = tool;
|
||||
[self syncControls];
|
||||
}
|
||||
- (void)selectPen { [self selectTool:@"pen"]; }
|
||||
- (void)selectRect { [self selectTool:@"rect"]; }
|
||||
- (void)selectEllipse { [self selectTool:@"ellipse"]; }
|
||||
- (void)selectText { [self selectTool:@"text"]; }
|
||||
- (void)undoAnnotation {
|
||||
if (_annotations.count == 0 && _textEditor == nil) {
|
||||
return;
|
||||
}
|
||||
[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));
|
||||
if (_annotations.count == 0) {
|
||||
[self syncControls];
|
||||
[self setNeedsDisplay:YES];
|
||||
return;
|
||||
}
|
||||
[_annotations removeLastObject];
|
||||
if (_selectedTextAnnotationIndex >= (NSInteger)_annotations.count) {
|
||||
_selectedTextAnnotationIndex = -1;
|
||||
}
|
||||
[self syncControls];
|
||||
[self setNeedsDisplay:YES];
|
||||
}
|
||||
|
||||
- (void)beginTextAnnotationAtPoint:(NSPoint)localPoint {
|
||||
[self beginTextEditorAtLocalPoint:localPoint text:@"" color:_activeColor annotationIndex:-1];
|
||||
}
|
||||
|
||||
- (void)beginEditingTextAnnotationAtIndex:(NSInteger)index {
|
||||
if (index < 0 || index >= (NSInteger)_annotations.count) {
|
||||
return;
|
||||
}
|
||||
NSDictionary *item = _annotations[(NSUInteger)index];
|
||||
if (![item[@"tool"] isEqualToString:@"text"]) {
|
||||
return;
|
||||
}
|
||||
NSArray *points = item[@"points"];
|
||||
if (points.count == 0) {
|
||||
return;
|
||||
}
|
||||
NSColor *color = item[@"nsColor"] ?: _activeColor;
|
||||
_activeColor = color;
|
||||
_activeFontSize = [self fontSizeForTextItem:item];
|
||||
[self beginTextEditorAtLocalPoint:[points[0] pointValue]
|
||||
text:item[@"text"] ?: @""
|
||||
color:color
|
||||
annotationIndex:index];
|
||||
}
|
||||
|
||||
- (void)beginTextEditorAtLocalPoint:(NSPoint)localPoint text:(NSString *)text color:(NSColor *)color annotationIndex:(NSInteger)index {
|
||||
[self cancelTextEditor];
|
||||
CGFloat fontSize = _activeFontSize;
|
||||
if (index >= 0 && index < (NSInteger)_annotations.count) {
|
||||
fontSize = [self fontSizeForTextItem:_annotations[(NSUInteger)index]];
|
||||
_activeFontSize = fontSize;
|
||||
}
|
||||
NSPoint clampedLocal = [self clampedTextLocalPoint:localPoint text:text fontSize:fontSize];
|
||||
_textEditorLocalPoint = clampedLocal;
|
||||
_textEditorAnnotationIndex = index;
|
||||
_textEditorColor = color ?: _activeColor;
|
||||
|
||||
NSRect frame = [self textEditorFrameForLocalPoint:clampedLocal text:text fontSize:fontSize];
|
||||
_textEditorLocalPoint = [self localPoint:frame.origin];
|
||||
|
||||
_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 setTextColor:_textEditorColor];
|
||||
[_textEditor setFont:[NSFont systemFontOfSize:fontSize weight:NSFontWeightSemibold]];
|
||||
[_textEditor setStringValue:text ?: @""];
|
||||
[_textEditor setFocusRingType:NSFocusRingTypeNone];
|
||||
[_textEditor setTarget:self];
|
||||
[_textEditor setAction:@selector(commitTextEditorFromSender:)];
|
||||
[self addSubview:_textEditor];
|
||||
[[self window] makeFirstResponder:_textEditor];
|
||||
[_textEditor selectText:nil];
|
||||
}
|
||||
|
||||
- (BOOL)isEditingText {
|
||||
@@ -772,17 +1166,35 @@ static id nativeOverlayKeyMonitor = nil;
|
||||
return;
|
||||
}
|
||||
NSString *text = [[_textEditor stringValue] stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
|
||||
NSColor *textColor = _textEditorColor ?: _activeColor;
|
||||
if (_textEditorAnnotationIndex >= 0 && _textEditorAnnotationIndex < (NSInteger)_annotations.count) {
|
||||
if (text.length > 0) {
|
||||
NSMutableDictionary *item = (NSMutableDictionary *)_annotations[(NSUInteger)_textEditorAnnotationIndex];
|
||||
item[@"color"] = [self hexForColor:textColor];
|
||||
item[@"nsColor"] = textColor;
|
||||
item[@"points"] = [NSMutableArray arrayWithObject:[NSValue valueWithPoint:_textEditorLocalPoint]];
|
||||
item[@"text"] = text;
|
||||
item[@"fontSize"] = @(_activeFontSize);
|
||||
_selectedTextAnnotationIndex = _textEditorAnnotationIndex;
|
||||
} else {
|
||||
[_annotations removeObjectAtIndex:(NSUInteger)_textEditorAnnotationIndex];
|
||||
_selectedTextAnnotationIndex = -1;
|
||||
}
|
||||
} else if (text.length > 0) {
|
||||
[_annotations addObject:[@{
|
||||
@"tool": @"text",
|
||||
@"color": [self hexForColor:_activeColor],
|
||||
@"nsColor": _activeColor,
|
||||
@"color": [self hexForColor:textColor],
|
||||
@"nsColor": textColor,
|
||||
@"points": [NSMutableArray arrayWithObject:[NSValue valueWithPoint:_textEditorLocalPoint]],
|
||||
@"text": text
|
||||
@"text": text,
|
||||
@"fontSize": @(_activeFontSize)
|
||||
} mutableCopy]];
|
||||
_selectedTextAnnotationIndex = (NSInteger)_annotations.count - 1;
|
||||
}
|
||||
[_textEditor removeFromSuperview];
|
||||
_textEditor = nil;
|
||||
_textEditorAnnotationIndex = -1;
|
||||
_textEditorColor = nil;
|
||||
[[self window] makeFirstResponder:self];
|
||||
[self syncControls];
|
||||
[self setNeedsDisplay:YES];
|
||||
@@ -794,44 +1206,50 @@ static id nativeOverlayKeyMonitor = nil;
|
||||
}
|
||||
[_textEditor removeFromSuperview];
|
||||
_textEditor = nil;
|
||||
_textEditorAnnotationIndex = -1;
|
||||
_textEditorColor = nil;
|
||||
[[self window] makeFirstResponder:self];
|
||||
[self setNeedsDisplay:YES];
|
||||
}
|
||||
|
||||
- (void)togglePalette {
|
||||
[_paletteView setHidden:![_paletteView isHidden]];
|
||||
if (![_paletteView isHidden] && _paletteView.subviews.count == 0) {
|
||||
NSArray *colors = @[
|
||||
[NSColor colorWithCalibratedRed:239.0/255.0 green:68.0/255.0 blue:68.0/255.0 alpha:1.0],
|
||||
[NSColor colorWithCalibratedRed:249.0/255.0 green:115.0/255.0 blue:22.0/255.0 alpha:1.0],
|
||||
[NSColor colorWithCalibratedRed:250.0/255.0 green:204.0/255.0 blue:21.0/255.0 alpha:1.0],
|
||||
[NSColor colorWithCalibratedRed:34.0/255.0 green:197.0/255.0 blue:94.0/255.0 alpha:1.0],
|
||||
[NSColor colorWithCalibratedRed:59.0/255.0 green:130.0/255.0 blue:246.0/255.0 alpha:1.0],
|
||||
[NSColor colorWithCalibratedRed:139.0/255.0 green:92.0/255.0 blue:246.0/255.0 alpha:1.0],
|
||||
[NSColor colorWithCalibratedRed:236.0/255.0 green:72.0/255.0 blue:153.0/255.0 alpha:1.0],
|
||||
[NSColor whiteColor],
|
||||
[NSColor colorWithCalibratedWhite:0.07 alpha:1.0]
|
||||
];
|
||||
for (NSUInteger i = 0; i < colors.count; i++) {
|
||||
NSButton *button = [NSButton buttonWithTitle:@"" target:self action:@selector(selectPaletteColor:)];
|
||||
[button setBordered:NO];
|
||||
[button setWantsLayer:YES];
|
||||
[[button layer] setCornerRadius:4];
|
||||
[[button layer] setBackgroundColor:[colors[i] CGColor]];
|
||||
[button setTag:(NSInteger)i];
|
||||
[button setFrame:NSMakeRect(8 + (i % 5) * 28, 38 - (i / 5) * 28, 22, 22)];
|
||||
[_paletteView addSubview:button];
|
||||
}
|
||||
objc_setAssociatedObject(_paletteView, "snapgoColors", colors, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
|
||||
}
|
||||
- (void)selectStrokeWidth:(NSButton *)sender {
|
||||
_activeStrokeWidth = MAX(1.0, (CGFloat)sender.tag);
|
||||
[self updateToolSettingsStates];
|
||||
}
|
||||
|
||||
- (void)selectPaletteColor:(NSButton *)sender {
|
||||
NSArray *colors = objc_getAssociatedObject(_paletteView, "snapgoColors");
|
||||
- (void)selectFontSize:(NSButton *)sender {
|
||||
_activeFontSize = MAX(8.0, (CGFloat)sender.tag);
|
||||
if (_textEditor != nil) {
|
||||
[_textEditor setFont:[NSFont systemFontOfSize:_activeFontSize weight:NSFontWeightSemibold]];
|
||||
[self resizeTextEditorForCurrentFont];
|
||||
} else if ([_activeTool isEqualToString:@"text"] && _selectedTextAnnotationIndex >= 0 && _selectedTextAnnotationIndex < (NSInteger)_annotations.count) {
|
||||
NSMutableDictionary *item = (NSMutableDictionary *)_annotations[(NSUInteger)_selectedTextAnnotationIndex];
|
||||
item[@"fontSize"] = @(_activeFontSize);
|
||||
NSMutableArray *points = (NSMutableArray *)item[@"points"];
|
||||
if (points.count > 0) {
|
||||
NSString *text = item[@"text"] ?: @"";
|
||||
NSPoint p = [points[0] pointValue];
|
||||
points[0] = [NSValue valueWithPoint:[self clampedTextLocalPoint:p text:text fontSize:_activeFontSize]];
|
||||
}
|
||||
[self setNeedsDisplay:YES];
|
||||
}
|
||||
[self syncControls];
|
||||
}
|
||||
|
||||
- (void)selectToolColor:(NSButton *)sender {
|
||||
NSArray *colors = objc_getAssociatedObject(_toolSettingsView, "snapgoColors");
|
||||
if (sender.tag >= 0 && sender.tag < (NSInteger)colors.count) {
|
||||
_activeColor = colors[(NSUInteger)sender.tag];
|
||||
[_paletteView setHidden:YES];
|
||||
[self syncControls];
|
||||
if (_textEditor != nil) {
|
||||
_textEditorColor = _activeColor;
|
||||
[_textEditor setTextColor:_activeColor];
|
||||
} else if ([_activeTool isEqualToString:@"text"] && _selectedTextAnnotationIndex >= 0 && _selectedTextAnnotationIndex < (NSInteger)_annotations.count) {
|
||||
NSMutableDictionary *item = (NSMutableDictionary *)_annotations[(NSUInteger)_selectedTextAnnotationIndex];
|
||||
item[@"color"] = [self hexForColor:_activeColor];
|
||||
item[@"nsColor"] = _activeColor;
|
||||
[self setNeedsDisplay:YES];
|
||||
}
|
||||
[self updateToolSettingsStates];
|
||||
}
|
||||
}
|
||||
|
||||
@@ -848,6 +1266,9 @@ static id nativeOverlayKeyMonitor = nil;
|
||||
NSString *text = item[@"text"];
|
||||
if (text.length > 0) {
|
||||
entry[@"text"] = text;
|
||||
entry[@"fontSize"] = item[@"fontSize"] ?: @20.0;
|
||||
} else {
|
||||
entry[@"strokeWidth"] = item[@"strokeWidth"] ?: @3.0;
|
||||
}
|
||||
[payload addObject:entry];
|
||||
}
|
||||
@@ -872,8 +1293,8 @@ static id nativeOverlayKeyMonitor = nil;
|
||||
return;
|
||||
}
|
||||
NSRect r = [self globalRectForSelection:_selection];
|
||||
[self closeOverlayWindow];
|
||||
NSString *json = [self annotationsJSON];
|
||||
[self closeOverlayWindow];
|
||||
nativeOverlayConfirm((int)llround(r.origin.x), (int)llround(r.origin.y), (int)llround(r.size.width), (int)llround(r.size.height), [json UTF8String]);
|
||||
}
|
||||
|
||||
@@ -882,8 +1303,8 @@ static id nativeOverlayKeyMonitor = nil;
|
||||
return;
|
||||
}
|
||||
NSRect r = [self globalRectForSelection:_selection];
|
||||
[self closeOverlayWindow];
|
||||
NSString *json = [self annotationsJSON];
|
||||
[self closeOverlayWindow];
|
||||
nativeOverlayCopy((int)llround(r.origin.x), (int)llround(r.origin.y), (int)llround(r.size.width), (int)llround(r.size.height), [json UTF8String]);
|
||||
}
|
||||
|
||||
@@ -925,11 +1346,23 @@ static id nativeOverlayKeyMonitor = nil;
|
||||
return;
|
||||
}
|
||||
NSRect r = [self globalRectForSelection:_selection];
|
||||
[self closeOverlayWindow];
|
||||
NSString *json = [self annotationsJSON];
|
||||
[self closeOverlayWindow];
|
||||
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];
|
||||
NSString *json = [self annotationsJSON];
|
||||
[self closeOverlayWindow];
|
||||
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
|
||||
// configured multimodal model, and copies the generated summary.
|
||||
- (void)summarizeSelection {
|
||||
@@ -937,8 +1370,8 @@ static id nativeOverlayKeyMonitor = nil;
|
||||
return;
|
||||
}
|
||||
NSRect r = [self globalRectForSelection:_selection];
|
||||
[self closeOverlayWindow];
|
||||
NSString *json = [self annotationsJSON];
|
||||
[self closeOverlayWindow];
|
||||
nativeOverlaySummarize((int)llround(r.origin.x), (int)llround(r.origin.y), (int)llround(r.size.width), (int)llround(r.size.height), [json UTF8String]);
|
||||
}
|
||||
@end
|
||||
|
||||
Reference in New Issue
Block a user