2023-11-01 22:11:11 +01:00
|
|
|
// This file Copyright © Transmission authors and contributors.
|
2022-01-20 12:27:56 -06:00
|
|
|
// It may be used under the MIT (SPDX: MIT) license.
|
|
|
|
// License text can be found in the licenses/ folder.
|
2011-10-22 20:45:20 +00:00
|
|
|
|
2022-04-25 04:34:30 +08:00
|
|
|
#import "CocoaCompatibility.h"
|
|
|
|
|
2011-10-22 20:45:20 +00:00
|
|
|
#import "NSImageAdditions.h"
|
2022-06-09 05:35:51 +08:00
|
|
|
#import "NSApplicationAdditions.h"
|
2011-10-22 20:45:20 +00:00
|
|
|
|
|
|
|
@implementation NSImage (NSImageAdditions)
|
|
|
|
|
2022-10-19 20:28:21 +01:00
|
|
|
static CGFloat const kIconSize = 16.0;
|
|
|
|
static CGFloat const kBorderWidth = 1.25;
|
2022-06-09 05:35:51 +08:00
|
|
|
|
|
|
|
+ (NSImage*)discIconWithColor:(NSColor*)color insetFactor:(CGFloat)insetFactor
|
|
|
|
{
|
2022-10-19 20:28:21 +01:00
|
|
|
return [NSImage imageWithSize:NSMakeSize(kIconSize, kIconSize) flipped:NO drawingHandler:^BOOL(NSRect rect) {
|
2022-06-09 05:35:51 +08:00
|
|
|
//shape
|
2022-10-19 20:28:21 +01:00
|
|
|
rect = NSInsetRect(rect, kBorderWidth / 2 + rect.size.width * insetFactor / 2, kBorderWidth / 2 + rect.size.height * insetFactor / 2);
|
2022-06-09 05:35:51 +08:00
|
|
|
NSBezierPath* bp = [NSBezierPath bezierPathWithOvalInRect:rect];
|
2022-10-19 20:28:21 +01:00
|
|
|
bp.lineWidth = kBorderWidth;
|
2022-06-09 05:35:51 +08:00
|
|
|
|
|
|
|
//border
|
2022-06-29 07:20:42 +03:00
|
|
|
CGFloat fractionOfBlendedColor = NSApp.darkMode ? 0.15 : 0.3;
|
2022-06-09 05:35:51 +08:00
|
|
|
NSColor* borderColor = [color blendedColorWithFraction:fractionOfBlendedColor ofColor:NSColor.controlTextColor];
|
|
|
|
[borderColor setStroke];
|
|
|
|
[bp stroke];
|
|
|
|
|
|
|
|
//inside
|
|
|
|
[color setFill];
|
|
|
|
[bp fill];
|
|
|
|
|
|
|
|
return YES;
|
|
|
|
}];
|
|
|
|
}
|
|
|
|
|
2021-08-15 12:41:48 +03:00
|
|
|
- (NSImage*)imageWithColor:(NSColor*)color
|
2011-10-22 20:45:20 +00:00
|
|
|
{
|
2021-08-15 12:41:48 +03:00
|
|
|
NSImage* coloredImage = [self copy];
|
2017-01-24 20:53:16 +03:00
|
|
|
|
2011-10-22 20:45:20 +00:00
|
|
|
[coloredImage lockFocus];
|
2017-01-24 20:53:16 +03:00
|
|
|
|
2011-10-22 20:45:20 +00:00
|
|
|
[color set];
|
2017-01-24 20:53:16 +03:00
|
|
|
|
2021-08-15 12:41:48 +03:00
|
|
|
NSSize const size = coloredImage.size;
|
2021-10-31 18:18:27 +03:00
|
|
|
NSRectFillUsingOperation(NSMakeRect(0.0, 0.0, size.width, size.height), NSCompositingOperationSourceAtop);
|
2017-01-24 20:53:16 +03:00
|
|
|
|
2011-10-22 20:45:20 +00:00
|
|
|
[coloredImage unlockFocus];
|
2017-01-24 20:53:16 +03:00
|
|
|
|
2017-07-29 23:14:22 +07:00
|
|
|
return coloredImage;
|
2011-10-22 20:45:20 +00:00
|
|
|
}
|
|
|
|
|
2021-12-06 02:42:54 +03:00
|
|
|
+ (NSImage*)systemSymbol:(NSString*)symbolName withFallback:(NSString*)fallbackName
|
|
|
|
{
|
|
|
|
if (@available(macOS 11.0, *))
|
|
|
|
{
|
|
|
|
return [NSImage imageWithSystemSymbolName:symbolName accessibilityDescription:nil];
|
|
|
|
}
|
|
|
|
|
|
|
|
return [NSImage imageNamed:fallbackName];
|
|
|
|
}
|
|
|
|
|
2022-04-25 04:34:30 +08:00
|
|
|
+ (NSImage*)largeSystemSymbol:(NSString*)symbolName withFallback:(NSString*)fallbackName
|
|
|
|
{
|
|
|
|
#ifdef __MAC_11_0
|
|
|
|
if (@available(macOS 11.0, *))
|
|
|
|
{
|
|
|
|
return [[NSImage imageWithSystemSymbolName:symbolName accessibilityDescription:nil]
|
|
|
|
imageWithSymbolConfiguration:[NSImageSymbolConfiguration configurationWithScale:NSImageSymbolScaleLarge]];
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
return [NSImage imageNamed:fallbackName];
|
|
|
|
}
|
|
|
|
|
2011-10-22 20:45:20 +00:00
|
|
|
@end
|