feat: add line and arrow annotations

This commit is contained in:
2026-07-12 21:09:23 +08:00
parent b82108db38
commit 50551de105
4 changed files with 220 additions and 23 deletions
+55 -15
View File
@@ -162,6 +162,8 @@ static id nativeOverlayKeyMonitor = nil;
@property(strong) NSButton *summaryButton;
@property(strong) NSButton *uploadButton;
@property(strong) NSButton *penButton;
@property(strong) NSButton *lineButton;
@property(strong) NSButton *arrowButton;
@property(strong) NSButton *rectButton;
@property(strong) NSButton *ellipseButton;
@property(strong) NSButton *textButton;
@@ -253,6 +255,8 @@ static id nativeOverlayKeyMonitor = nil;
[_summaryButton setToolTip:@"复制总结"];
[_uploadButton setToolTip:@"上传 S3"];
_penButton = [NSButton buttonWithTitle:@"" target:self action:@selector(selectPen)];
_lineButton = [NSButton buttonWithTitle:@"" target:self action:@selector(selectLine)];
_arrowButton = [NSButton buttonWithTitle:@"" target:self action:@selector(selectArrow)];
_rectButton = [NSButton buttonWithTitle:@"" target:self action:@selector(selectRect)];
_ellipseButton = [NSButton buttonWithTitle:@"" target:self action:@selector(selectEllipse)];
_textButton = [NSButton buttonWithTitle:@"" target:self action:@selector(selectText)];
@@ -278,10 +282,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, _ftpButton, _ocrButton, _summaryButton, _uploadButton, _penButton, _rectButton, _ellipseButton, _textButton, _mosaicButton, _undoButton, _toolSettingsView, _sizeLabel, _hintLabel]) {
for (NSView *view in @[_cancelButton, _clipboardButton, _saveButton, _saveRemoteButton, _ftpButton, _ocrButton, _summaryButton, _uploadButton, _penButton, _lineButton, _arrowButton, _rectButton, _ellipseButton, _textButton, _mosaicButton, _undoButton, _toolSettingsView, _sizeLabel, _hintLabel]) {
[self addSubview:view];
}
for (NSView *view in @[_cancelButton, _clipboardButton, _saveButton, _saveRemoteButton, _ftpButton, _ocrButton, _summaryButton, _uploadButton, _penButton, _rectButton, _ellipseButton, _textButton, _mosaicButton, _undoButton, _toolSettingsView, _actionToolbarBg]) {
for (NSView *view in @[_cancelButton, _clipboardButton, _saveButton, _saveRemoteButton, _ftpButton, _ocrButton, _summaryButton, _uploadButton, _penButton, _lineButton, _arrowButton, _rectButton, _ellipseButton, _textButton, _mosaicButton, _undoButton, _toolSettingsView, _actionToolbarBg]) {
[view setHidden:YES];
}
[_sizeLabel setHidden:YES];
@@ -393,7 +397,12 @@ static id nativeOverlayKeyMonitor = nil;
summaryHB.baseColor = baseWhite; summaryHB.hoverColor = hoverBlue;
uploadHB.baseColor = baseWhite; uploadHB.hoverColor = hoverBlue;
[self styleIconButton:_penButton image:[self iconFromSVG:@"<svg viewBox='0 0 1024 1024' xmlns='http://www.w3.org/2000/svg'><path d='M742.72 752.064c-29.696 28.576-42.976 48.96-42.976 77.12 0 98.4 143.776 142.464 229.728 65.536l-21.344-23.84c-66.976 59.936-176.384 26.4-176.384-41.696 0-16.832 9.28-31.104 33.184-54.08l13.44-12.736c61.024-58.016 75.328-102.72 29.312-179.84-43.84-73.44-96.8-88.288-162.784-50.56-47.232 27.04-68.64 47.456-208.32 190.4-70.624 72.288-153.92 77.088-217.344 26.784-59.712-47.328-79.872-127.36-42.176-188.64 24.512-39.84 62.656-72.896 136.64-124.48 5.952-4.192 27.04-18.816 31.104-21.664 12.16-8.448 21.536-15.104 30.4-21.536 107.552-78.272 140.8-139.136 86.048-219.904-76.992-113.472-202.304-90.016-367.744 61.472l21.6 23.616c153.088-140.224 256.896-159.616 319.68-67.104 41.632 61.408 16.896 106.688-78.4 176.032-8.64 6.304-17.92 12.832-29.888 21.184l-31.136 21.632c-77.504 54.08-117.984 89.184-145.536 133.984-46.784 76.032-22.176 173.664 49.536 230.496 76.224 60.416 177.952 54.56 260.096-29.504 136.16-139.36 158.08-160.224 201.312-184.96 50.656-28.96 84.416-19.52 119.424 39.168 36.896 61.824 27.52 91.392-23.808 140.16 0.096-0.064-10.976 10.368-13.632 12.96z' fill='white'/></svg>"]];
[self styleIconButton:_penButton image:[self iconFromSVG:@"<svg viewBox='0 0 24 24' xmlns='http://www.w3.org/2000/svg'><g fill='none' stroke='white' stroke-width='1.8' stroke-linecap='round' stroke-linejoin='round'><path d='m14.5 5.5 4-4 4 4-4 4'/><path d='m17.5 8.5-7.8 7.8'/><path d='M11 14.8c.8 2.8-.7 5.5-3.7 6.3-1.8.5-3.7.1-5.3-.9 2.1-.4 2.8-1.5 3.1-3.3.4-2.5 3.3-3.8 5.9-2.1Z'/></g></svg>"]];
[self styleIconButton:_lineButton image:[self iconFromSVG:@"<svg viewBox='0 0 24 24' xmlns='http://www.w3.org/2000/svg'><path d='M5 19 19 5' fill='none' stroke='white' stroke-width='2' stroke-linecap='round'/></svg>"]];
[self styleIconButton:_arrowButton image:[self iconFromSVG:@"<svg viewBox='0 0 24 24' xmlns='http://www.w3.org/2000/svg'><g fill='none' stroke='white' stroke-width='2' stroke-linecap='round' stroke-linejoin='round'><path d='M5 19 19 5'/><path d='M11 5h8v8'/></g></svg>"]];
[_penButton setToolTip:@"画笔标记"];
[_lineButton setToolTip:@"直线标记"];
[_arrowButton setToolTip:@"箭头标记"];
[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>"]];
@@ -752,12 +761,35 @@ static id nativeOverlayKeyMonitor = nil;
} else if (points.count >= 2) {
NSPoint a = [points[0] pointValue];
NSPoint b = [[points lastObject] pointValue];
NSPoint start = NSMakePoint(_selection.origin.x + a.x, _selection.origin.y + a.y);
NSPoint end = NSMakePoint(_selection.origin.x + b.x, _selection.origin.y + b.y);
NSRect r = NSMakeRect(
_selection.origin.x + MIN(a.x, b.x),
_selection.origin.y + MIN(a.y, b.y),
fabs(b.x - a.x),
fabs(b.y - a.y));
if ([tool isEqualToString:@"rect"]) {
if ([tool isEqualToString:@"line"] || [tool isEqualToString:@"arrow"]) {
NSBezierPath *linePath = [NSBezierPath bezierPath];
[linePath moveToPoint:start];
[linePath lineToPoint:end];
[linePath setLineWidth:strokeWidth];
[linePath setLineCapStyle:NSLineCapStyleRound];
[linePath setLineJoinStyle:NSLineJoinStyleRound];
if ([tool isEqualToString:@"arrow"]) {
CGFloat dx = end.x - start.x, dy = end.y - start.y;
CGFloat length = hypot(dx, dy);
if (length >= 1.0) {
CGFloat headLength = MIN(length * 0.45, MAX(10.0, strokeWidth * 4.0));
CGFloat angle = atan2(dy, dx), spread = M_PI / 6.0;
NSPoint left = NSMakePoint(end.x - headLength * cos(angle - spread), end.y - headLength * sin(angle - spread));
NSPoint right = NSMakePoint(end.x - headLength * cos(angle + spread), end.y - headLength * sin(angle + spread));
[linePath moveToPoint:left];
[linePath lineToPoint:end];
[linePath lineToPoint:right];
}
}
[linePath stroke];
} else if ([tool isEqualToString:@"rect"]) {
NSBezierPath *rectPath = [NSBezierPath bezierPathWithRect:r];
[rectPath setLineWidth:strokeWidth];
[rectPath stroke];
@@ -1042,6 +1074,8 @@ static id nativeOverlayKeyMonitor = nil;
[_uploadButton setHidden:!visible];
[_actionToolbarBg setHidden:!visible];
[_penButton setHidden:!visible];
[_lineButton setHidden:!visible];
[_arrowButton setHidden:!visible];
[_rectButton setHidden:!visible];
[_ellipseButton setHidden:!visible];
[_textButton setHidden:!visible];
@@ -1088,15 +1122,17 @@ static id nativeOverlayKeyMonitor = nil;
// 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 = 212;
CGFloat markW = 280;
CGFloat markGap = 8;
CGFloat markX = MAX(0, x - markGap - markW);
[_penButton setFrame:NSMakeRect(markX + 4, y + 4, 28, 28)];
[_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)];
[_mosaicButton setFrame:NSMakeRect(markX + 140, y + 4, 28, 28)];
[_undoButton setFrame:NSMakeRect(markX + 178, y + 4, 28, 28)];
[_lineButton setFrame:NSMakeRect(markX + 38, y + 4, 28, 28)];
[_arrowButton setFrame:NSMakeRect(markX + 72, y + 4, 28, 28)];
[_rectButton setFrame:NSMakeRect(markX + 106, y + 4, 28, 28)];
[_ellipseButton setFrame:NSMakeRect(markX + 140, y + 4, 28, 28)];
[_textButton setFrame:NSMakeRect(markX + 174, y + 4, 28, 28)];
[_mosaicButton setFrame:NSMakeRect(markX + 208, y + 4, 28, 28)];
[_undoButton setFrame:NSMakeRect(markX + 246, y + 4, 28, 28)];
[_undoButton setEnabled:YES];
[[_undoButton layer] setOpacity:_annotations.count > 0 ? 1.0 : 0.35];
@@ -1112,10 +1148,12 @@ static id nativeOverlayKeyMonitor = nil;
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;
if (isMosaic) activeCenter = markX + 154;
if ([_activeTool isEqualToString:@"line"]) activeCenter = markX + 52;
if ([_activeTool isEqualToString:@"arrow"]) activeCenter = markX + 86;
if ([_activeTool isEqualToString:@"rect"]) activeCenter = markX + 120;
if ([_activeTool isEqualToString:@"ellipse"]) activeCenter = markX + 154;
if ([_activeTool isEqualToString:@"text"]) activeCenter = markX + 188;
if (isMosaic) activeCenter = markX + 222;
[_toolSettingsView setFrame:NSMakeRect(settingsX, settingsY, settingsW, settingsH)];
[_toolSettingsView setArrowX:MAX(18, MIN(activeCenter - settingsX, settingsW - 18))];
[_toolSettingsView setNeedsDisplay:YES];
@@ -1159,7 +1197,7 @@ static id nativeOverlayKeyMonitor = nil;
}
- (void)updateToolButtonStates {
NSDictionary *buttons = @{@"pen": _penButton, @"rect": _rectButton, @"ellipse": _ellipseButton, @"text": _textButton};
NSDictionary *buttons = @{@"pen": _penButton, @"line": _lineButton, @"arrow": _arrowButton, @"rect": _rectButton, @"ellipse": _ellipseButton, @"text": _textButton};
for (NSString *tool in buttons) {
NSButton *button = buttons[tool];
NSColor *bg = [tool isEqualToString:_activeTool]
@@ -1230,6 +1268,8 @@ static id nativeOverlayKeyMonitor = nil;
[self syncControls];
}
- (void)selectPen { [self selectTool:@"pen"]; }
- (void)selectLine { [self selectTool:@"line"]; }
- (void)selectArrow { [self selectTool:@"arrow"]; }
- (void)selectRect { [self selectTool:@"rect"]; }
- (void)selectEllipse { [self selectTool:@"ellipse"]; }
- (void)selectText { [self selectTool:@"text"]; }