feat: add mosaic screenshot annotations
This commit is contained in:
+219
-9
@@ -142,8 +142,14 @@ static id nativeOverlayKeyMonitor = nil;
|
||||
@property(strong) NSColor *activeColor;
|
||||
@property CGFloat activeStrokeWidth;
|
||||
@property CGFloat activeFontSize;
|
||||
@property CGFloat mosaicBrushWidth;
|
||||
@property NSString *mosaicMode;
|
||||
@property(strong) NSMutableArray<NSDictionary *> *annotations;
|
||||
@property(strong) NSMutableDictionary *draftAnnotation;
|
||||
// A downsampled snapshot of the screen beneath the overlay. Drawing this
|
||||
// image back at full size with interpolation disabled produces a genuine
|
||||
// pixel-block preview instead of a translucent placeholder stroke.
|
||||
@property(strong) NSImage *mosaicPreviewImage;
|
||||
@property(strong) NSButton *cancelButton;
|
||||
// Use `clipboardButton` (not `copyButton`) to avoid ARC's "copy" method
|
||||
// family ownership rule which would otherwise treat the synthesized getter
|
||||
@@ -159,12 +165,15 @@ static id nativeOverlayKeyMonitor = nil;
|
||||
@property(strong) NSButton *rectButton;
|
||||
@property(strong) NSButton *ellipseButton;
|
||||
@property(strong) NSButton *textButton;
|
||||
@property(strong) NSButton *mosaicButton;
|
||||
@property(strong) NSButton *undoButton;
|
||||
@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) NSMutableArray<NSButton *> *mosaicModeButtons;
|
||||
@property(strong) NSMutableArray<NSButton *> *mosaicWidthButtons;
|
||||
@property(strong) NSView *actionToolbarBg;
|
||||
@property(strong) NSTextField *textEditor;
|
||||
@property NSPoint textEditorLocalPoint;
|
||||
@@ -195,6 +204,8 @@ static id nativeOverlayKeyMonitor = nil;
|
||||
- (void)selectStrokeWidth:(NSButton *)sender;
|
||||
- (void)selectFontSize:(NSButton *)sender;
|
||||
- (void)selectToolColor:(NSButton *)sender;
|
||||
- (void)selectMosaicMode:(NSButton *)sender;
|
||||
- (void)selectMosaicWidth:(NSButton *)sender;
|
||||
- (BOOL)isEditingText;
|
||||
- (void)commitTextEditor;
|
||||
- (void)cancelTextEditor;
|
||||
@@ -214,6 +225,8 @@ static id nativeOverlayKeyMonitor = 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;
|
||||
_mosaicBrushWidth = 24.0;
|
||||
_mosaicMode = @"brush";
|
||||
_annotations = [NSMutableArray array];
|
||||
_textEditorAnnotationIndex = -1;
|
||||
_selectedTextAnnotationIndex = -1;
|
||||
@@ -243,12 +256,15 @@ static id nativeOverlayKeyMonitor = nil;
|
||||
_rectButton = [NSButton buttonWithTitle:@"" target:self action:@selector(selectRect)];
|
||||
_ellipseButton = [NSButton buttonWithTitle:@"" target:self action:@selector(selectEllipse)];
|
||||
_textButton = [NSButton buttonWithTitle:@"" target:self action:@selector(selectText)];
|
||||
_mosaicButton = [NSButton buttonWithTitle:@"" target:self action:@selector(selectMosaic)];
|
||||
_undoButton = [NSButton buttonWithTitle:@"" target:self action:@selector(undoAnnotation)];
|
||||
_toolSettingsView = [[SnipToolSettingsView alloc] initWithFrame:NSZeroRect];
|
||||
_settingsDivider = [[NSView alloc] initWithFrame:NSZeroRect];
|
||||
_strokeSettingButtons = [NSMutableArray array];
|
||||
_fontSettingButtons = [NSMutableArray array];
|
||||
_colorSettingButtons = [NSMutableArray array];
|
||||
_mosaicModeButtons = [NSMutableArray array];
|
||||
_mosaicWidthButtons = [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];
|
||||
@@ -262,10 +278,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, _undoButton, _toolSettingsView, _sizeLabel, _hintLabel]) {
|
||||
for (NSView *view in @[_cancelButton, _clipboardButton, _saveButton, _saveRemoteButton, _ftpButton, _ocrButton, _summaryButton, _uploadButton, _penButton, _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, _undoButton, _toolSettingsView, _actionToolbarBg]) {
|
||||
for (NSView *view in @[_cancelButton, _clipboardButton, _saveButton, _saveRemoteButton, _ftpButton, _ocrButton, _summaryButton, _uploadButton, _penButton, _rectButton, _ellipseButton, _textButton, _mosaicButton, _undoButton, _toolSettingsView, _actionToolbarBg]) {
|
||||
[view setHidden:YES];
|
||||
}
|
||||
[_sizeLabel setHidden:YES];
|
||||
@@ -381,6 +397,8 @@ 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:_mosaicButton image:[self iconFromSVG:@"<svg viewBox='0 0 1024 1024' xmlns='http://www.w3.org/2000/svg'><path d='M128 128h320v320H128V128zm448 0h320v320H576V128zM128 576h320v320H128V576zm448 0h320v320H576V576z' fill='white'/></svg>"]];
|
||||
[_mosaicButton setToolTip:@"马赛克标记"];
|
||||
[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>"]];
|
||||
|
||||
[_toolSettingsView setWantsLayer:NO];
|
||||
@@ -415,6 +433,37 @@ static id nativeOverlayKeyMonitor = nil;
|
||||
[_toolSettingsView addSubview:button];
|
||||
}
|
||||
|
||||
for (NSUInteger i = 0; i < 2; i++) {
|
||||
NSButton *button = [NSButton buttonWithTitle:@"" target:self action:@selector(selectMosaicMode:)];
|
||||
[button setTag:(NSInteger)i];
|
||||
[button setBordered:NO];
|
||||
[button setWantsLayer:YES];
|
||||
[[button layer] setCornerRadius:5];
|
||||
NSString *svg = i == 0
|
||||
? @"<svg viewBox='0 0 24 24' xmlns='http://www.w3.org/2000/svg'><g fill='none' stroke='black' stroke-width='1.8' stroke-linecap='round' stroke-linejoin='round'><path d='M9 11V5.5a2 2 0 0 1 4 0V10'/><path d='M13 10V8a2 2 0 0 1 4 0v3'/><path d='M17 10a2 2 0 0 1 4 0v4.5c0 4-2.5 6.5-6.5 6.5h-1.2a6 6 0 0 1-4.7-2.3L4 13.2a1.9 1.9 0 0 1 2.8-2.5L9 12.5'/></g></svg>"
|
||||
: @"<svg viewBox='0 0 24 24' xmlns='http://www.w3.org/2000/svg'><g fill='none' stroke='black' stroke-width='1.8' stroke-linejoin='round'><rect x='2.5' y='2.5' width='19' height='19' rx='2'/></g><g fill='black'><rect x='6' y='6' width='4' height='4' rx='.5'/><rect x='14' y='6' width='4' height='4' rx='.5'/><rect x='6' y='14' width='4' height='4' rx='.5'/><rect x='14' y='14' width='4' height='4' rx='.5'/></g></svg>";
|
||||
[button setImage:[self iconFromSVG:svg]];
|
||||
[button setImagePosition:NSImageOnly];
|
||||
[button setImageScaling:NSImageScaleProportionallyDown];
|
||||
NSString *accessibilityLabel = i == 0 ? @"涂抹马赛克" : @"框选马赛克";
|
||||
[button setToolTip:accessibilityLabel];
|
||||
[button setAccessibilityLabel:accessibilityLabel];
|
||||
[_mosaicModeButtons addObject:button];
|
||||
[_toolSettingsView addSubview:button];
|
||||
}
|
||||
for (NSNumber *width in @[@16, @24, @36, @52]) {
|
||||
NSButton *button = [NSButton buttonWithTitle:@"●" target:self action:@selector(selectMosaicWidth:)];
|
||||
[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:MIN(22, 6 + [width doubleValue] / 3) weight:NSFontWeightBold] range:NSMakeRange(0, title.length)];
|
||||
[button setAttributedTitle:title];
|
||||
[_mosaicWidthButtons 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],
|
||||
@@ -629,6 +678,63 @@ static id nativeOverlayKeyMonitor = nil;
|
||||
}
|
||||
continue;
|
||||
}
|
||||
if ([tool hasPrefix:@"mosaic-"]) {
|
||||
[NSGraphicsContext saveGraphicsState];
|
||||
NSBezierPath *clipPath = nil;
|
||||
if ([tool isEqualToString:@"mosaic-brush"]) {
|
||||
clipPath = [NSBezierPath bezierPath];
|
||||
CGFloat radius = [self strokeWidthForItem:item] / 2.0;
|
||||
for (NSUInteger i = 0; i < points.count; i++) {
|
||||
NSPoint local = [points[i] pointValue];
|
||||
NSPoint p = NSMakePoint(_selection.origin.x + local.x, _selection.origin.y + local.y);
|
||||
[clipPath appendBezierPathWithOvalInRect:NSMakeRect(p.x - radius, p.y - radius, radius * 2, radius * 2)];
|
||||
if (i == 0) continue;
|
||||
NSPoint previousLocal = [points[i - 1] pointValue];
|
||||
NSPoint previous = NSMakePoint(_selection.origin.x + previousLocal.x, _selection.origin.y + previousLocal.y);
|
||||
CGFloat dx = p.x - previous.x, dy = p.y - previous.y;
|
||||
CGFloat length = hypot(dx, dy);
|
||||
if (length <= 0.001) continue;
|
||||
CGFloat nx = -dy / length * radius, ny = dx / length * radius;
|
||||
NSBezierPath *segment = [NSBezierPath bezierPath];
|
||||
[segment moveToPoint:NSMakePoint(previous.x + nx, previous.y + ny)];
|
||||
[segment lineToPoint:NSMakePoint(p.x + nx, p.y + ny)];
|
||||
[segment lineToPoint:NSMakePoint(p.x - nx, p.y - ny)];
|
||||
[segment lineToPoint:NSMakePoint(previous.x - nx, previous.y - ny)];
|
||||
[segment closePath];
|
||||
[clipPath appendBezierPath:segment];
|
||||
}
|
||||
} else if (points.count >= 2) {
|
||||
NSPoint a = [points[0] pointValue], b = [[points lastObject] pointValue];
|
||||
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));
|
||||
clipPath = [NSBezierPath bezierPathWithRect:r];
|
||||
}
|
||||
if (clipPath != nil) {
|
||||
[clipPath addClip];
|
||||
if (_mosaicPreviewImage != nil) {
|
||||
NSDictionary *hints = @{NSImageHintInterpolation: @(NSImageInterpolationNone)};
|
||||
[_mosaicPreviewImage drawInRect:self.bounds
|
||||
fromRect:NSMakeRect(0, 0, _mosaicPreviewImage.size.width, _mosaicPreviewImage.size.height)
|
||||
operation:NSCompositingOperationCopy
|
||||
fraction:1.0
|
||||
respectFlipped:YES
|
||||
hints:hints];
|
||||
} else {
|
||||
// Screen capture can be unavailable before Screen Recording
|
||||
// permission is granted. A checkerboard fallback still reads
|
||||
// as pixelation and never resembles a marker stroke.
|
||||
CGFloat tile = 8.0;
|
||||
for (CGFloat y = 0; y < self.bounds.size.height; y += tile) {
|
||||
for (CGFloat x = 0; x < self.bounds.size.width; x += tile) {
|
||||
BOOL alternate = (((NSInteger)(x / tile) + (NSInteger)(y / tile)) % 2) == 0;
|
||||
[[NSColor colorWithCalibratedWhite:(alternate ? 0.42 : 0.68) alpha:1.0] setFill];
|
||||
NSRectFill(NSMakeRect(x, y, tile, tile));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
[NSGraphicsContext restoreGraphicsState];
|
||||
continue;
|
||||
}
|
||||
[color setStroke];
|
||||
CGFloat strokeWidth = [self strokeWidthForItem:item];
|
||||
NSBezierPath *path = [NSBezierPath bezierPath];
|
||||
@@ -805,7 +911,7 @@ static id nativeOverlayKeyMonitor = nil;
|
||||
@"tool": _activeTool,
|
||||
@"color": [self hexForColor:_activeColor],
|
||||
@"nsColor": _activeColor,
|
||||
@"strokeWidth": @(_activeStrokeWidth),
|
||||
@"strokeWidth": @([_activeTool isEqualToString:@"mosaic-brush"] ? _mosaicBrushWidth : _activeStrokeWidth),
|
||||
@"points": [NSMutableArray arrayWithObject:[NSValue valueWithPoint:local]]
|
||||
} mutableCopy];
|
||||
} else if (_hasSelection && NSPointInRect(p, _selection)) {
|
||||
@@ -869,7 +975,7 @@ static id nativeOverlayKeyMonitor = nil;
|
||||
} else if (_annotating && _draftAnnotation != nil) {
|
||||
NSMutableArray *points = _draftAnnotation[@"points"];
|
||||
NSPoint local = [self localPoint:p];
|
||||
if ([_activeTool isEqualToString:@"pen"]) {
|
||||
if ([_activeTool isEqualToString:@"pen"] || [_activeTool isEqualToString:@"mosaic-brush"]) {
|
||||
[points addObject:[NSValue valueWithPoint:local]];
|
||||
} else {
|
||||
if (points.count == 1) {
|
||||
@@ -939,6 +1045,7 @@ static id nativeOverlayKeyMonitor = nil;
|
||||
[_rectButton setHidden:!visible];
|
||||
[_ellipseButton setHidden:!visible];
|
||||
[_textButton setHidden:!visible];
|
||||
[_mosaicButton setHidden:!visible];
|
||||
[_undoButton setHidden:!visible];
|
||||
[_toolSettingsView setHidden:!visible || _activeTool == nil];
|
||||
[_sizeLabel setHidden:!visible];
|
||||
@@ -981,20 +1088,23 @@ 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 = 178;
|
||||
CGFloat markW = 212;
|
||||
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)];
|
||||
[_undoButton setFrame:NSMakeRect(markX + 144, y + 4, 28, 28)];
|
||||
[_mosaicButton setFrame:NSMakeRect(markX + 140, y + 4, 28, 28)];
|
||||
[_undoButton setFrame:NSMakeRect(markX + 178, 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;
|
||||
BOOL isMosaic = [_activeTool hasPrefix:@"mosaic-"];
|
||||
BOOL mosaicBrush = [_mosaicMode isEqualToString:@"brush"];
|
||||
CGFloat settingsW = isMosaic ? (mosaicBrush ? 304 : 112) : (isText ? 462 : 398);
|
||||
CGFloat settingsH = 58;
|
||||
CGFloat settingsX = MAX(8, MIN(markX, self.bounds.size.width - settingsW - 8));
|
||||
CGFloat settingsY = y + toolbarH + 4;
|
||||
@@ -1005,6 +1115,7 @@ static id nativeOverlayKeyMonitor = nil;
|
||||
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;
|
||||
[_toolSettingsView setFrame:NSMakeRect(settingsX, settingsY, settingsW, settingsH)];
|
||||
[_toolSettingsView setArrowX:MAX(18, MIN(activeCenter - settingsX, settingsW - 18))];
|
||||
[_toolSettingsView setNeedsDisplay:YES];
|
||||
@@ -1012,21 +1123,34 @@ static id nativeOverlayKeyMonitor = nil;
|
||||
CGFloat contentY = 18;
|
||||
CGFloat xCursor = 14;
|
||||
for (NSButton *button in _strokeSettingButtons) {
|
||||
[button setHidden:isText];
|
||||
[button setHidden:isText || isMosaic];
|
||||
[button setFrame:NSMakeRect(xCursor, contentY, 32, 32)];
|
||||
xCursor += 42;
|
||||
}
|
||||
xCursor = 14;
|
||||
for (NSButton *button in _fontSettingButtons) {
|
||||
[button setHidden:!isText];
|
||||
[button setHidden:!isText || isMosaic];
|
||||
[button setFrame:NSMakeRect(xCursor, contentY, 40, 32)];
|
||||
xCursor += 48;
|
||||
}
|
||||
for (NSUInteger i = 0; i < _mosaicModeButtons.count; i++) {
|
||||
NSButton *button = _mosaicModeButtons[i];
|
||||
[button setHidden:!isMosaic];
|
||||
[button setFrame:NSMakeRect(14 + i * 38, contentY + 1, 36, 30)];
|
||||
}
|
||||
for (NSUInteger i = 0; i < _mosaicWidthButtons.count; i++) {
|
||||
NSButton *button = _mosaicWidthButtons[i];
|
||||
[button setHidden:!isMosaic || !mosaicBrush];
|
||||
[button setFrame:NSMakeRect(122 + i * 42, contentY, 32, 32)];
|
||||
}
|
||||
CGFloat groupW = isText ? (4 * 40 + 3 * 8) : (3 * 32 + 2 * 10);
|
||||
CGFloat dividerX = 14 + groupW + 12;
|
||||
[_settingsDivider setHidden:isMosaic && !mosaicBrush];
|
||||
if (isMosaic) dividerX = 102;
|
||||
[_settingsDivider setFrame:NSMakeRect(dividerX, contentY + 3, 1, 26)];
|
||||
CGFloat colorX = dividerX + 18;
|
||||
for (NSButton *button in _colorSettingButtons) {
|
||||
[button setHidden:isMosaic];
|
||||
[button setFrame:NSMakeRect(colorX, contentY + 5, 22, 22)];
|
||||
colorX += 34;
|
||||
}
|
||||
@@ -1043,6 +1167,8 @@ static id nativeOverlayKeyMonitor = nil;
|
||||
: [NSColor clearColor];
|
||||
[[button layer] setBackgroundColor:[bg CGColor]];
|
||||
}
|
||||
BOOL mosaicActive = [_activeTool hasPrefix:@"mosaic-"];
|
||||
[[_mosaicButton layer] setBackgroundColor:[(mosaicActive ? [NSColor colorWithCalibratedWhite:1.0 alpha:0.14] : [NSColor clearColor]) CGColor]];
|
||||
[self updateToolSettingsStates];
|
||||
}
|
||||
|
||||
@@ -1070,6 +1196,23 @@ static id nativeOverlayKeyMonitor = nil;
|
||||
[button setAttributedTitle:title];
|
||||
}
|
||||
}
|
||||
for (NSButton *button in _mosaicModeButtons) {
|
||||
BOOL active = (button.tag == 0 && [_mosaicMode isEqualToString:@"brush"]) || (button.tag == 1 && [_mosaicMode isEqualToString:@"rect"]);
|
||||
[self styleButton:button background:(active ? [NSColor whiteColor] : clear) foreground:(active ? activeFg : normalFg)];
|
||||
if (@available(macOS 10.14, *)) {
|
||||
[button setContentTintColor:(active ? activeFg : normalFg)];
|
||||
}
|
||||
[button setAccessibilityValue:(active ? @"已选择" : @"未选择")];
|
||||
}
|
||||
for (NSButton *button in _mosaicWidthButtons) {
|
||||
BOOL active = fabs((CGFloat)button.tag - _mosaicBrushWidth) < 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");
|
||||
@@ -1090,6 +1233,7 @@ static id nativeOverlayKeyMonitor = nil;
|
||||
- (void)selectRect { [self selectTool:@"rect"]; }
|
||||
- (void)selectEllipse { [self selectTool:@"ellipse"]; }
|
||||
- (void)selectText { [self selectTool:@"text"]; }
|
||||
- (void)selectMosaic { [self selectTool:[_mosaicMode isEqualToString:@"brush"] ? @"mosaic-brush" : @"mosaic-rect"]; }
|
||||
- (void)undoAnnotation {
|
||||
if (_annotations.count == 0 && _textEditor == nil) {
|
||||
return;
|
||||
@@ -1228,6 +1372,16 @@ static id nativeOverlayKeyMonitor = nil;
|
||||
[self updateToolSettingsStates];
|
||||
}
|
||||
|
||||
- (void)selectMosaicMode:(NSButton *)sender {
|
||||
_mosaicMode = sender.tag == 0 ? @"brush" : @"rect";
|
||||
[self selectMosaic];
|
||||
}
|
||||
|
||||
- (void)selectMosaicWidth:(NSButton *)sender {
|
||||
_mosaicBrushWidth = MAX(8.0, (CGFloat)sender.tag);
|
||||
[self updateToolSettingsStates];
|
||||
}
|
||||
|
||||
- (void)selectFontSize:(NSButton *)sender {
|
||||
_activeFontSize = MAX(8.0, (CGFloat)sender.tag);
|
||||
if (_textEditor != nil) {
|
||||
@@ -1421,6 +1575,60 @@ static NSScreen *snipScreenContainingCursor(void) {
|
||||
return [[NSScreen screens] firstObject];
|
||||
}
|
||||
|
||||
// snipMosaicPreviewImage captures the target display before the transparent
|
||||
// overlay becomes visible, then downsamples it to one sample per 10 logical
|
||||
// points. The view scales this tiny image back with nearest-neighbour
|
||||
// interpolation inside each mosaic mask, yielding stable, screen-derived
|
||||
// pixel blocks while the pointer is moving.
|
||||
static NSImage *snipMosaicPreviewImage(NSScreen *screen) {
|
||||
NSSize fullSize = [screen frame].size;
|
||||
NSUInteger screenIndex = [[NSScreen screens] indexOfObjectIdenticalTo:screen];
|
||||
if (screenIndex == NSNotFound) screenIndex = 0;
|
||||
NSString *temporaryPath = [NSTemporaryDirectory() stringByAppendingPathComponent:
|
||||
[NSString stringWithFormat:@"snapgo-mosaic-%@.png", [[NSUUID UUID] UUIDString]]];
|
||||
NSTask *captureTask = [[NSTask alloc] init];
|
||||
[captureTask setExecutableURL:[NSURL fileURLWithPath:@"/usr/sbin/screencapture"]];
|
||||
[captureTask setArguments:@[@"-x", @"-D", [NSString stringWithFormat:@"%lu", (unsigned long)screenIndex + 1], temporaryPath]];
|
||||
NSError *launchError = nil;
|
||||
if (![captureTask launchAndReturnError:&launchError]) return nil;
|
||||
[captureTask waitUntilExit];
|
||||
NSImage *fullImage = [[NSImage alloc] initWithContentsOfFile:temporaryPath];
|
||||
[[NSFileManager defaultManager] removeItemAtPath:temporaryPath error:nil];
|
||||
if ([captureTask terminationStatus] != 0 || fullImage == nil) return nil;
|
||||
|
||||
NSInteger pixelW = MAX(1, (NSInteger)ceil(fullSize.width / 10.0));
|
||||
NSInteger pixelH = MAX(1, (NSInteger)ceil(fullSize.height / 10.0));
|
||||
NSBitmapImageRep *bitmap = [[NSBitmapImageRep alloc]
|
||||
initWithBitmapDataPlanes:NULL
|
||||
pixelsWide:pixelW
|
||||
pixelsHigh:pixelH
|
||||
bitsPerSample:8
|
||||
samplesPerPixel:4
|
||||
hasAlpha:YES
|
||||
isPlanar:NO
|
||||
colorSpaceName:NSCalibratedRGBColorSpace
|
||||
bytesPerRow:0
|
||||
bitsPerPixel:0];
|
||||
if (bitmap == nil) return nil;
|
||||
|
||||
NSGraphicsContext *context = [NSGraphicsContext graphicsContextWithBitmapImageRep:bitmap];
|
||||
[NSGraphicsContext saveGraphicsState];
|
||||
[NSGraphicsContext setCurrentContext:context];
|
||||
[context setImageInterpolation:NSImageInterpolationHigh];
|
||||
[fullImage drawInRect:NSMakeRect(0, 0, pixelW, pixelH)
|
||||
fromRect:NSMakeRect(0, 0, fullSize.width, fullSize.height)
|
||||
operation:NSCompositingOperationCopy
|
||||
fraction:1.0
|
||||
respectFlipped:NO
|
||||
hints:nil];
|
||||
[context flushGraphics];
|
||||
[NSGraphicsContext restoreGraphicsState];
|
||||
|
||||
NSImage *result = [[NSImage alloc] initWithSize:NSMakeSize(pixelW, pixelH)];
|
||||
[result addRepresentation:bitmap];
|
||||
return result;
|
||||
}
|
||||
|
||||
static void snipShowNativeOverlay(int width, int height) {
|
||||
dispatch_async(dispatch_get_main_queue(), ^{
|
||||
NSScreen *screen = snipScreenContainingCursor();
|
||||
@@ -1430,6 +1638,7 @@ static void snipShowNativeOverlay(int width, int height) {
|
||||
}
|
||||
|
||||
NSRect frame = [screen frame];
|
||||
NSImage *mosaicPreview = snipMosaicPreviewImage(screen);
|
||||
[NSApp activateIgnoringOtherApps:YES];
|
||||
|
||||
nativeOverlayWindow = [[SnipNativeOverlayPanel alloc]
|
||||
@@ -1450,6 +1659,7 @@ static void snipShowNativeOverlay(int width, int height) {
|
||||
|
||||
SnipNativeOverlayView *view = [[SnipNativeOverlayView alloc]
|
||||
initWithFrame:NSMakeRect(0, 0, frame.size.width, frame.size.height)];
|
||||
[view setMosaicPreviewImage:mosaicPreview];
|
||||
[nativeOverlayWindow setContentView:view];
|
||||
[nativeOverlayWindow makeKeyAndOrderFront:nil];
|
||||
[nativeOverlayWindow makeFirstResponder:view];
|
||||
|
||||
Reference in New Issue
Block a user