SKFontWell.m 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535
  1. //
  2. // SKFontWell.m
  3. // Skim
  4. //
  5. // Created by Christiaan Hofman on 4/13/08.
  6. /*
  7. This software is Copyright (c) 2008-2018
  8. Christiaan Hofman. All rights reserved.
  9. Redistribution and use in source and binary forms, with or without
  10. modification, are permitted provided that the following conditions
  11. are met:
  12. - Redistributions of source code must retain the above copyright
  13. notice, this list of conditions and the following disclaimer.
  14. - Redistributions in binary form must reproduce the above copyright
  15. notice, this list of conditions and the following disclaimer in
  16. the documentation and/or other materials provided with the
  17. distribution.
  18. - Neither the name of Christiaan Hofman nor the names of any
  19. contributors may be used to endorse or promote products derived
  20. from this software without specific prior written permission.
  21. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  22. "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  23. LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  24. A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  25. OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  26. SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  27. LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  28. DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  29. THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  30. (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  31. OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  32. */
  33. #import "SKFontWell.h"
  34. //#import "NSGraphics_SKExtensions.h"
  35. #define SKNSFontPanelDescriptorsPboardType @"NSFontPanelDescriptorsPboardType"
  36. #define SKNSFontPanelFamiliesPboardType @"NSFontPanelFamiliesPboardType"
  37. #define SKNSFontCollectionFontDescriptors @"NSFontCollectionFontDescriptors"
  38. #define SKFontWellWillBecomeActiveNotification @"SKFontWellWillBecomeActiveNotification"
  39. #define FONTNAME_KEY @"fontName"
  40. #define FONTSIZE_KEY @"fontSize"
  41. #define TEXTCOLOR_KEY @"textColor"
  42. #define HASTEXTCOLOR_KEY @"hasTextColor"
  43. #define FONT_KEY @"font"
  44. #define ACTION_KEY @"action"
  45. #define TARGET_KEY @"target"
  46. static char SKFontWellFontNameObservationContext;
  47. static char SKFontWellFontSizeObservationContext;
  48. @interface SKFontWell (SKPrivate)
  49. - (void)changeActive:(id)sender;
  50. - (void)fontChanged;
  51. @end
  52. @implementation SKFontWell
  53. @dynamic isActive, fontName, fontSize, textColor, hasTextColor;
  54. + (void)initialize {
  55. // SKINITIALIZE;
  56. [self exposeBinding:FONTNAME_KEY];
  57. [self exposeBinding:FONTSIZE_KEY];
  58. [self exposeBinding:TEXTCOLOR_KEY];
  59. }
  60. + (NSSet *)keyPathsForValuesAffectingFontName {
  61. return [NSSet setWithObjects:FONT_KEY, nil];
  62. }
  63. + (NSSet *)keyPathsForValuesAffectingFontSize {
  64. return [NSSet setWithObjects:FONT_KEY, nil];
  65. }
  66. + (Class)cellClass {
  67. return [SKFontWellCell class];
  68. }
  69. - (Class)valueClassForBinding:(NSString *)binding {
  70. if ([binding isEqualToString:FONTNAME_KEY])
  71. return [NSString class];
  72. else if ([binding isEqualToString:FONTNAME_KEY])
  73. return [NSNumber class];
  74. else if ([binding isEqualToString:TEXTCOLOR_KEY])
  75. return [NSColor class];
  76. else
  77. return [super valueClassForBinding:binding];
  78. }
  79. - (void)commonInit {
  80. if ([self font] == nil)
  81. [self setFont:[NSFont systemFontOfSize:0.0]];
  82. [self fontChanged];
  83. [super setAction:@selector(changeActive:)];
  84. [super setTarget:self];
  85. bindingInfo = [[NSMutableDictionary alloc] init];
  86. [self registerForDraggedTypes:[NSArray arrayWithObjects:SKNSFontPanelDescriptorsPboardType, SKNSFontPanelFamiliesPboardType, NSPasteboardTypeColor, nil]];
  87. }
  88. - (id)initWithFrame:(NSRect)frame {
  89. self = [super initWithFrame:frame];
  90. if (self) {
  91. [self commonInit];
  92. }
  93. return self;
  94. }
  95. - (id)initWithCoder:(NSCoder *)decoder {
  96. self = [super initWithCoder:decoder];
  97. if (self) {
  98. NSButtonCell *oldCell = [self cell];
  99. if (NO == [oldCell isKindOfClass:[[self class] cellClass]]) {
  100. SKFontWellCell *newCell = [[[[self class] cellClass] alloc] init];
  101. [newCell setAlignment:[oldCell alignment]];
  102. [newCell setEditable:[oldCell isEditable]];
  103. [newCell setTarget:[oldCell target]];
  104. [newCell setAction:[oldCell action]];
  105. [self setCell:newCell];
  106. }
  107. action = NSSelectorFromString([decoder decodeObjectForKey:ACTION_KEY]);
  108. target = [decoder decodeObjectForKey:TARGET_KEY];
  109. [self commonInit];
  110. }
  111. return self;
  112. }
  113. - (void)encodeWithCoder:(NSCoder *)coder {
  114. [super encodeWithCoder:coder];
  115. [coder encodeObject:NSStringFromSelector(action) forKey:ACTION_KEY];
  116. [coder encodeConditionalObject:target forKey:TARGET_KEY];
  117. }
  118. - (void)dealloc {
  119. // SKENSURE_MAIN_THREAD(
  120. [self unbind:FONTNAME_KEY];
  121. [self unbind:FONTSIZE_KEY];
  122. // );
  123. [[NSNotificationCenter defaultCenter] removeObserver:self];
  124. // SKDESTROY(bindingInfo);
  125. // [super dealloc];
  126. }
  127. - (void)viewWillMoveToWindow:(NSWindow *)newWindow {
  128. [self deactivate];
  129. [super viewWillMoveToWindow:newWindow];
  130. }
  131. - (void)fontPickerWillBecomeActive:(NSNotification *)notification {
  132. id sender = [notification object];
  133. if (sender != self && [self isActive]) {
  134. [self deactivate];
  135. }
  136. }
  137. - (void)fontPanelWillClose:(NSNotification *)notification {
  138. [self deactivate];
  139. }
  140. - (void)notifyFontBinding {
  141. NSDictionary *info = [self infoForBinding:FONTNAME_KEY];
  142. [[info objectForKey:NSObservedObjectKey] setValue:[self fontName] forKeyPath:[info objectForKey:NSObservedKeyPathKey]];
  143. info = [self infoForBinding:FONTSIZE_KEY];
  144. [[info objectForKey:NSObservedObjectKey] setValue:[NSNumber numberWithDouble:[self fontSize]] forKeyPath:[info objectForKey:NSObservedKeyPathKey]];
  145. }
  146. - (void)notifyTextColorBinding {
  147. NSDictionary *info = [self infoForBinding:TEXTCOLOR_KEY];
  148. if (info) {
  149. id value = [self textColor];
  150. NSString *transformerName = [[info objectForKey:NSOptionsKey] objectForKey:NSValueTransformerNameBindingOption];
  151. if (transformerName && [transformerName isEqual:[NSNull null]] == NO) {
  152. NSValueTransformer *valueTransformer = [NSValueTransformer valueTransformerForName:transformerName];
  153. value = [valueTransformer reverseTransformedValue:value];
  154. }
  155. [[info objectForKey:NSObservedObjectKey] setValue:value forKeyPath:[info objectForKey:NSObservedKeyPathKey]];
  156. }
  157. }
  158. - (void)changeFontFromFontManager:(id)sender {
  159. if ([self isActive]) {
  160. [self setFont:[sender convertFont:[self font]]];
  161. [self notifyFontBinding];
  162. [self sendAction:[self action] to:[self target]];
  163. }
  164. }
  165. - (void)changeAttributesFromFontManager:(id)sender {
  166. if ([self isActive] && [self hasTextColor]) {
  167. [self setTextColor:[[sender convertAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[self textColor], NSForegroundColorAttributeName, nil]] valueForKey:NSForegroundColorAttributeName]];
  168. [self notifyTextColorBinding];
  169. [self sendAction:[self action] to:[self target]];
  170. }
  171. }
  172. - (void)changeActive:(id)sender {
  173. if ([self isEnabled]) {
  174. if ([self isActive])
  175. [self activate];
  176. else
  177. [self deactivate];
  178. }
  179. }
  180. - (void)activate {
  181. NSNotificationCenter *nc = [NSNotificationCenter defaultCenter];
  182. NSFontManager *fm = [NSFontManager sharedFontManager];
  183. [nc postNotificationName:SKFontWellWillBecomeActiveNotification object:self];
  184. [fm setSelectedFont:[self font] isMultiple:NO];
  185. [fm orderFrontFontPanel:self];
  186. [nc addObserver:self selector:@selector(fontPickerWillBecomeActive:)
  187. name:SKFontWellWillBecomeActiveNotification object:nil];
  188. [nc addObserver:self selector:@selector(fontPanelWillClose:)
  189. name:NSWindowWillCloseNotification object:[fm fontPanel:YES]];
  190. [self setState:NSOnState];
  191. [self setKeyboardFocusRingNeedsDisplayInRect:[self bounds]];
  192. [self setNeedsDisplay:YES];
  193. }
  194. - (void)deactivate {
  195. [[NSNotificationCenter defaultCenter] removeObserver:self];
  196. [self setState:NSOffState];
  197. [self setKeyboardFocusRingNeedsDisplayInRect:[self bounds]];
  198. [self setNeedsDisplay:YES];
  199. }
  200. - (void)fontChanged {
  201. if ([self isActive])
  202. [[NSFontManager sharedFontManager] setSelectedFont:[self font] isMultiple:NO];
  203. [self setTitle:[NSString stringWithFormat:@"%@ %li", [[self font] displayName], (long)[self fontSize]]];
  204. [self setNeedsDisplay:YES];
  205. }
  206. #pragma mark Accessors
  207. - (SEL)action { return action; }
  208. - (void)setAction:(SEL)newAction { action = newAction; }
  209. - (id)target { return target; }
  210. - (void)setTarget:(id)newTarget { target = newTarget; }
  211. - (BOOL)isActive {
  212. return [self state] == NSOnState;
  213. }
  214. - (void)setFont:(NSFont *)newFont {
  215. BOOL didChange = [[self font] isEqual:newFont] == NO;
  216. [super setFont:newFont];
  217. if (didChange)
  218. [self fontChanged];
  219. }
  220. - (NSString *)fontName {
  221. return [[self font] fontName];
  222. }
  223. - (void)setFontName:(NSString *)fontName {
  224. NSFont *newFont = [NSFont fontWithName:fontName size:[[self font] pointSize]];
  225. if (newFont)
  226. [self setFont:newFont];
  227. }
  228. - (CGFloat)fontSize {
  229. return [[self font] pointSize];
  230. }
  231. - (void)setFontSize:(CGFloat)pointSize {
  232. NSFont *newFont = [NSFont fontWithName:[[self font] fontName] size:pointSize];
  233. if (newFont)
  234. [self setFont:newFont];
  235. }
  236. - (NSColor *)textColor {
  237. return [[self cell] textColor];
  238. }
  239. - (void)setTextColor:(NSColor *)newTextColor {
  240. BOOL didChange = [[self textColor] isEqual:newTextColor] == NO;
  241. [[self cell] setTextColor:newTextColor];
  242. if (didChange)
  243. [self setNeedsDisplay:YES];
  244. }
  245. - (BOOL)hasTextColor {
  246. return [[self cell] hasTextColor];
  247. }
  248. - (void)setHasTextColor:(BOOL)newHasTextColor {
  249. if ([self hasTextColor] != newHasTextColor) {
  250. [[self cell] setHasTextColor:newHasTextColor];
  251. [self setNeedsDisplay:YES];
  252. }
  253. }
  254. #pragma mark Binding support
  255. - (void)bind:(NSString *)bindingName toObject:(id)observableController withKeyPath:(NSString *)keyPath options:(NSDictionary *)options {
  256. if ([bindingName isEqualToString:FONTNAME_KEY] || [bindingName isEqualToString:FONTSIZE_KEY]) {
  257. if ([bindingInfo objectForKey:bindingName])
  258. [self unbind:bindingName];
  259. // NSDictionary *bindingsData = [NSDictionary dictionaryWithObjectsAndKeys:observableController, NSObservedObjectKey, [[keyPath copy] autorelease], NSObservedKeyPathKey, [[options copy] autorelease], NSOptionsKey, nil];
  260. // [bindingInfo setObject:bindingsData forKey:bindingName];
  261. void *context = NULL;
  262. if ([bindingName isEqualToString:FONTNAME_KEY])
  263. context = &SKFontWellFontNameObservationContext;
  264. else if ([bindingName isEqualToString:FONTSIZE_KEY])
  265. context = &SKFontWellFontSizeObservationContext;
  266. [observableController addObserver:self forKeyPath:keyPath options:0 context:context];
  267. [self observeValueForKeyPath:keyPath ofObject:observableController change:nil context:context];
  268. } else {
  269. [super bind:bindingName toObject:observableController withKeyPath:keyPath options:options];
  270. }
  271. [self setNeedsDisplay:YES];
  272. }
  273. - (void)unbind:(NSString *)bindingName {
  274. if ([bindingName isEqualToString:FONTNAME_KEY] || [bindingName isEqualToString:FONTSIZE_KEY]) {
  275. NSDictionary *info = [self infoForBinding:bindingName];
  276. [[info objectForKey:NSObservedObjectKey] removeObserver:self forKeyPath:[info objectForKey:NSObservedKeyPathKey]];
  277. [bindingInfo removeObjectForKey:bindingName];
  278. } else {
  279. [super unbind:bindingName];
  280. }
  281. [self setNeedsDisplay:YES];
  282. }
  283. - (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context {
  284. NSString *key = nil;
  285. if (context == &SKFontWellFontNameObservationContext)
  286. key = FONTNAME_KEY;
  287. else if (context == &SKFontWellFontSizeObservationContext)
  288. key = FONTSIZE_KEY;
  289. if (key) {
  290. NSDictionary *info = [self infoForBinding:key];
  291. id value = [[info objectForKey:NSObservedObjectKey] valueForKeyPath:[info objectForKey:NSObservedKeyPathKey]];
  292. if (NSIsControllerMarker(value) == NO)
  293. [self setValue:value forKey:key];
  294. } else {
  295. [super observeValueForKeyPath:keyPath ofObject:object change:change context:context];
  296. }
  297. }
  298. - (NSDictionary *)infoForBinding:(NSString *)bindingName {
  299. return [bindingInfo objectForKey:bindingName] ?: [super infoForBinding:bindingName];
  300. }
  301. #pragma mark NSDraggingDestination protocol
  302. - (NSDragOperation)draggingEntered:(id <NSDraggingInfo>)sender {
  303. if ([self isEnabled] && [sender draggingSource] != self && [[sender draggingPasteboard] availableTypeFromArray:[NSArray arrayWithObjects:SKNSFontPanelDescriptorsPboardType, SKNSFontPanelFamiliesPboardType, ([self hasTextColor] ? NSPasteboardTypeColor : nil), nil]]) {
  304. [[self cell] setHighlighted:YES];
  305. [self setKeyboardFocusRingNeedsDisplayInRect:[self bounds]];
  306. [self setNeedsDisplay:YES];
  307. return NSDragOperationGeneric;
  308. } else
  309. return NSDragOperationNone;
  310. }
  311. - (void)draggingExited:(id <NSDraggingInfo>)sender {
  312. if ([self isEnabled] && [sender draggingSource] != self && [[sender draggingPasteboard] availableTypeFromArray:[NSArray arrayWithObjects:SKNSFontPanelDescriptorsPboardType, SKNSFontPanelFamiliesPboardType, ([self hasTextColor] ? NSPasteboardTypeColor : nil), nil]]) {
  313. [[self cell] setHighlighted:NO];
  314. [self setKeyboardFocusRingNeedsDisplayInRect:[self bounds]];
  315. [self setNeedsDisplay:YES];
  316. }
  317. }
  318. - (BOOL)prepareForDragOperation:(id <NSDraggingInfo>)sender {
  319. return [self isEnabled] && [sender draggingSource] != self && [[sender draggingPasteboard] availableTypeFromArray:[NSArray arrayWithObjects:SKNSFontPanelDescriptorsPboardType, SKNSFontPanelFamiliesPboardType, ([self hasTextColor] ? NSPasteboardTypeColor : nil), nil]];
  320. }
  321. - (BOOL)performDragOperation:(id <NSDraggingInfo>)sender{
  322. NSPasteboard *pboard = [sender draggingPasteboard];
  323. NSString *type = [pboard availableTypeFromArray:[NSArray arrayWithObjects:SKNSFontPanelDescriptorsPboardType, SKNSFontPanelFamiliesPboardType, ([self hasTextColor] ? NSPasteboardTypeColor : nil), nil]];
  324. NSFont *droppedFont = nil;
  325. NSColor *droppedColor = nil;
  326. @try {
  327. if ([type isEqualToString:SKNSFontPanelDescriptorsPboardType]) {
  328. NSData *data = [pboard dataForType:type];
  329. NSDictionary *dict = [data isKindOfClass:[NSData class]] ? [NSKeyedUnarchiver unarchiveObjectWithData:data] : nil;
  330. if ([dict isKindOfClass:[NSDictionary class]]) {
  331. NSArray *fontDescriptors = [dict objectForKey:SKNSFontCollectionFontDescriptors];
  332. NSFontDescriptor *fontDescriptor = ([fontDescriptors isKindOfClass:[NSArray class]] && [fontDescriptors count]) ? [fontDescriptors objectAtIndex:0] : nil;
  333. if ([fontDescriptor isKindOfClass:[NSFontDescriptor class]]) {
  334. NSNumber *size = [[fontDescriptor fontAttributes] objectForKey:NSFontSizeAttribute] ?: [dict objectForKey:NSFontSizeAttribute];
  335. CGFloat fontSize = [size respondsToSelector:@selector(doubleValue)] ? [size doubleValue] : [self fontSize];
  336. droppedFont = [NSFont fontWithDescriptor:fontDescriptor size:fontSize];
  337. }
  338. }
  339. } else if ([type isEqualToString:SKNSFontPanelFamiliesPboardType]) {
  340. NSArray *families = [pboard propertyListForType:type];
  341. NSString *family = ([families isKindOfClass:[NSArray class]] && [families count]) ? [families objectAtIndex:0] : nil;
  342. if ([family isKindOfClass:[NSString class]])
  343. droppedFont = [[NSFontManager sharedFontManager] convertFont:[self font] toFamily:family];
  344. } else if ([type isEqualToString:NSPasteboardTypeColor]) {
  345. droppedColor = [NSColor colorFromPasteboard:pboard];
  346. }
  347. }
  348. @catch (id exception) {
  349. NSLog(@"Ignoring exception %@ when dropping on SKFontWell failed", exception);
  350. }
  351. if (droppedFont) {
  352. [self setFont:droppedFont];
  353. [self notifyFontBinding];
  354. [self sendAction:[self action] to:[self target]];
  355. }
  356. if (droppedColor) {
  357. [self setTextColor:droppedColor];
  358. [self notifyTextColorBinding];
  359. [self sendAction:[self action] to:[self target]];
  360. }
  361. [[self cell] setHighlighted:NO];
  362. [self setKeyboardFocusRingNeedsDisplayInRect:[self bounds]];
  363. [self setNeedsDisplay:YES];
  364. return droppedFont != nil || droppedColor != nil;
  365. }
  366. @end
  367. @implementation SKFontWellCell
  368. @synthesize textColor, hasTextColor;
  369. - (void)commonInit {
  370. if (textColor == nil)
  371. [self setTextColor:[NSColor blackColor]];
  372. [self setBezelStyle:NSShadowlessSquareBezelStyle]; // this is mainly to make it selectable
  373. [self setButtonType:NSPushOnPushOffButton];
  374. [self setState:NSOffState];
  375. }
  376. - (id)initTextCell:(NSString *)aString {
  377. self = [super initTextCell:aString];
  378. if (self) {
  379. [self commonInit];
  380. }
  381. return self;
  382. }
  383. - (id)initWithCoder:(NSCoder *)decoder {
  384. self = [super initWithCoder:decoder];
  385. if (self) {
  386. [self setTextColor:[decoder decodeObjectForKey:TEXTCOLOR_KEY]];
  387. [self setHasTextColor:[decoder decodeBoolForKey:HASTEXTCOLOR_KEY]];
  388. [self commonInit];
  389. }
  390. return self;
  391. }
  392. - (void)encodeWithCoder:(NSCoder *)coder {
  393. [super encodeWithCoder:coder];
  394. [coder encodeConditionalObject:textColor forKey:TEXTCOLOR_KEY];
  395. [coder encodeObject:textColor forKey:HASTEXTCOLOR_KEY];
  396. }
  397. - (id)copyWithZone:(NSZone *)zone {
  398. SKFontWellCell *copy = [super copyWithZone:zone];
  399. copy->textColor = [textColor copyWithZone:zone];
  400. copy->hasTextColor = hasTextColor;
  401. return copy;
  402. }
  403. - (void)dealloc {
  404. // SKDESTROY(textColor);
  405. // [super dealloc];
  406. }
  407. - (void)drawBezelWithFrame:(NSRect)frame inView:(NSView *)controlView {
  408. // SKDrawTextFieldBezel(frame, controlView);
  409. if ([self state] == NSOnState) {
  410. [NSGraphicsContext saveGraphicsState];
  411. [[NSColor selectedControlColor] setFill];
  412. NSRectFillUsingOperation(frame, NSCompositePlusDarker);
  413. [NSGraphicsContext restoreGraphicsState];
  414. }
  415. if ([self isHighlighted]) {
  416. [NSGraphicsContext saveGraphicsState];
  417. [[NSColor colorWithCalibratedWhite:0.0 alpha:0.1] setFill];
  418. NSFrameRectWithWidthUsingOperation(frame, 1.0, NSCompositePlusDarker);
  419. [NSGraphicsContext restoreGraphicsState];
  420. }
  421. if ([self showsFirstResponder]) {
  422. [NSGraphicsContext saveGraphicsState];
  423. NSSetFocusRingStyle(NSFocusRingOnly);
  424. NSRectFill(frame);
  425. [NSGraphicsContext restoreGraphicsState];
  426. }
  427. }
  428. - (NSAttributedString *)attributedTitle {
  429. if ([self hasTextColor]) {
  430. NSMutableAttributedString *attrString = [[super attributedTitle] mutableCopy];
  431. [attrString addAttribute:NSForegroundColorAttributeName value:[self textColor] ? self.textColor : [NSColor blackColor] range:NSMakeRange(0, [attrString length])];
  432. if ([[[self textColor] colorUsingColorSpaceName:NSCalibratedRGBColorSpace] brightnessComponent] > 0.8) {
  433. NSShadow *shade = [[NSShadow alloc] init];
  434. [shade setShadowColor:[NSColor blackColor]];
  435. [shade setShadowBlurRadius:1.0];
  436. [attrString addAttribute:NSShadowAttributeName value:shade range:NSMakeRange(0, [attrString length])];
  437. }
  438. return attrString;
  439. } else {
  440. return [super attributedTitle];
  441. }
  442. }
  443. @end