-
-
Save codeswimmer/3363074 to your computer and use it in GitHub Desktop.
CRTransparentSearchFieldCell -- transparent search field cell
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#import <Cocoa/Cocoa.h> | |
@interface CRTransparentSearchFieldCell : NSSearchFieldCell { | |
BOOL _hasText; | |
} | |
@end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#import "CRTransparentSearchFieldCell.h" | |
@implementation CRTransparentSearchFieldCell | |
- (void)drawWithFrame:(NSRect)cellFrame inView:(NSView *)controlView { | |
NSRect rect = NSInsetRect(cellFrame, 0.5, 0.5); | |
NSBezierPath *path = [NSBezierPath bezierPathWithRoundedRect:rect | |
xRadius:rect.size.height/2.0 | |
yRadius:rect.size.height/2.0]; | |
[[NSColor colorWithDeviceWhite:0 alpha:0.4] set]; | |
[path stroke]; | |
NSRect searchButtonCellFrame = cellFrame; | |
searchButtonCellFrame.size = [[self searchButtonCell] cellSize]; | |
[[self searchButtonCell] drawWithFrame:searchButtonCellFrame | |
inView:controlView]; | |
if (_hasText) { | |
NSRect cancelButtonCellFrame = cellFrame; | |
cancelButtonCellFrame.origin.x = cellFrame.origin.x + | |
cellFrame.size.width - [[self cancelButtonCell] cellSize].width; | |
cancelButtonCellFrame.size = [[self cancelButtonCell] cellSize]; | |
[[self cancelButtonCell] drawWithFrame:cancelButtonCellFrame | |
inView:controlView]; | |
} | |
} | |
- (void)setStringValue:(NSString *)aString { | |
// We need _hasText, instead of querying [self stringValue] when drawing, | |
// because querying it will redraw cell, which freezes text cursor. | |
_hasText = ![aString isEqualToString:@""]; | |
[super setStringValue:aString]; | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This doesn't work as intended, see newer version :)