summaryrefslogtreecommitdiff
path: root/platform/macos/src/MGLAttributionButton.mm
blob: ed8bb18a6651b6d9cc44d4bd05df07f6d4677567 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#import "MGLAttributionButton.h"
#import "MGLAttributionInfo.h"

#import "NSBundle+MGLAdditions.h"
#import "NSString+MGLAdditions.h"

@implementation MGLAttributionButton

- (instancetype)initWithAttributionInfo:(MGLAttributionInfo *)info {
    if (self = [super initWithFrame:NSZeroRect]) {
        self.bordered = NO;
        self.bezelStyle = NSRegularSquareBezelStyle;
        
        // 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