Files
MapLibreParticleLab/platform/macos/src/MLNAttributionButton.mm
Bart Louwers e4a4b9ec67 macOS Metal + AppKit app (#2205)
Co-authored-by: mwilsnd <mwilsnd@gmail.com>
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
2024-04-02 01:52:19 +02:00

56 lines
1.6 KiB
Plaintext

#import "MLNAttributionButton.h"
#import "MLNAttributionInfo.h"
#import "NSBundle+MLNAdditions.h"
#import "NSString+MLNAdditions.h"
@implementation MLNAttributionButton
- (instancetype)initWithAttributionInfo:(MLNAttributionInfo *)info {
if (self = [super initWithFrame:NSZeroRect]) {
self.bordered = NO;
self.bezelStyle = NSBezelStyleBadge;
// Extract any prefix consisting of intellectual property symbols.
NSScanner *scanner = [NSScanner scannerWithString:info.title.string];
NSCharacterSet *symbolSet = [NSCharacterSet characterSetWithCharactersInString:@"©℗®℠™ &"];
NSString *symbol;
[scanner scanCharactersFromSet:symbolSet intoString:&symbol];
// Remove the underline from the symbol for aesthetic reasons.
NSMutableAttributedString *title = info.title.mutableCopy;
[title removeAttribute:NSUnderlineStyleAttributeName range:NSMakeRange(0, symbol.length)];
self.attributedTitle = title;
[self sizeToFit];
_URL = info.URL;
if (_URL) {
self.toolTip = _URL.absoluteString;
}
self.target = self;
self.action = @selector(openURL:);
}
return self;
}
- (BOOL)wantsLayer {
return YES;
}
- (void)resetCursorRects {
if (self.URL) {
// The whole button gets a pointing hand cursor, just like a hyperlink.
[self addCursorRect:self.bounds cursor:[NSCursor pointingHandCursor]];
}
}
- (IBAction)openURL:(__unused id)sender {
if (self.URL) {
[[NSWorkspace sharedWorkspace] openURL:self.URL];
}
}
@end