Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix compile errors and allow override of title and token margin for OEXTokenAttachmentCell #5

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions OEXTokenAttachmentCell.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@ typedef NS_ENUM(NSUInteger, OEXTokenJoinStyle) {
- (NSSize)cellSizeForTitleSize:(NSSize)titleSize;
- (NSRect)titleRectForBounds:(NSRect)bounds;

- (CGFloat)titleMargin;
- (CGFloat)tokenMargin;

- (void)drawTokenWithFrame:(NSRect)rect inView:(NSView *)controlView;
- (void)drawTitleWithFrame:(NSRect)rect inView:(NSView *)controlView;

Expand Down
74 changes: 46 additions & 28 deletions OEXTokenAttachmentCell.m
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,17 @@ @implementation OEXTokenAttachmentCell
OEXTokenJoinStyle _joinStyle;
}

- (CGFloat)titleMargin
{
return kOEXTokenAttachmentTitleMargin;
}


- (CGFloat)tokenMargin
{
return kOEXTokenAttachmentTokenMargin;
}

#pragma mark - Geometry

- (NSPoint)cellBaselineOffset
Expand All @@ -35,54 +46,60 @@ - (NSSize)cellSizeForTitleSize:(NSSize)titleSize
{
NSSize size = titleSize;
// Add margins + height for the token rounded edges
size.width += size.height + kOEXTokenAttachmentTitleMargin * 2;
size.width += size.height + [self titleMargin] * 2;
NSRect rect = {NSZeroPoint, size};
return NSIntegralRect(rect).size;
}

- (NSRect)titleRectForBounds:(NSRect)bounds
{
bounds.size.width = MAX(bounds.size.width, kOEXTokenAttachmentTitleMargin * 2 + bounds.size.height);
return NSInsetRect(bounds, kOEXTokenAttachmentTitleMargin + bounds.size.height / 2, 0);
CGFloat titleMargin = [self titleMargin];
bounds.size.width = MAX(bounds.size.width, titleMargin * 2 + bounds.size.height);
return NSInsetRect(bounds, titleMargin + bounds.size.height / 2, 0);
}

#pragma mark - Drawing

- (void)drawWithFrame:(NSRect)cellFrame inView:(NSView *)controlView characterIndex:(NSUInteger)charIndex layoutManager:(NSLayoutManager *)layoutManager
{
_drawingMode = self.isHighlighted && controlView.window.isKeyWindow ? OEXTokenDrawingModeHighlighted : OEXTokenDrawingModeDefault;
_joinStyle = OEXTokenJoinStyleNone;

if ( [controlView respondsToSelector:@selector(selectedRanges)] )
[self _drawWithFrame:cellFrame inView:controlView characterIndex:charIndex layoutManager:layoutManager];
}

- (void)_drawWithFrame:(NSRect)cellFrame inView:(NSView *)controlView characterIndex:(NSUInteger)charIndex layoutManager:(NSLayoutManager *)layoutManager
{
_drawingMode = self.isHighlighted && controlView.window.isKeyWindow ? OEXTokenDrawingModeHighlighted : OEXTokenDrawingModeDefault;
_joinStyle = OEXTokenJoinStyleNone;

if ( [controlView respondsToSelector:@selector(selectedRanges)] )
{
for ( NSValue *rangeValue in [(id) controlView selectedRanges] )
{
for ( NSValue *rangeValue in [(id) controlView selectedRanges] )
{
NSRange range = rangeValue.rangeValue;
if ( ! NSLocationInRange(charIndex, range) )
continue;

if ( controlView.window.isKeyWindow )
_drawingMode = OEXTokenDrawingModeSelected;

// TODO: RTL is not supported yet
if ( range.location < charIndex )
_joinStyle |= OEXTokenJoinStyleLeft;
if ( NSMaxRange(range) > charIndex + 1 )
_joinStyle |= OEXTokenJoinStyleRight;
}
NSRange range = rangeValue.rangeValue;
if ( ! NSLocationInRange(charIndex, range) )
continue;

if ( controlView.window.isKeyWindow )
_drawingMode = OEXTokenDrawingModeSelected;

// TODO: RTL is not supported yet
if ( range.location < charIndex )
_joinStyle |= OEXTokenJoinStyleLeft;
if ( NSMaxRange(range) > charIndex + 1 )
_joinStyle |= OEXTokenJoinStyleRight;
}

[self drawTokenWithFrame:cellFrame inView:controlView];
}

[self drawTokenWithFrame:cellFrame inView:controlView];
}

- (void)drawWithFrame:(NSRect)cellFrame inView:(NSView *)controlView
{
[self drawWithFrame:cellFrame inView:controlView characterIndex:NSNotFound layoutManager:nil];
[self _drawWithFrame:cellFrame inView:controlView characterIndex:NSNotFound layoutManager:nil];
}

- (void)drawInteriorWithFrame:(NSRect)cellFrame inView:(NSView *)controlView
{
[self drawWithFrame:cellFrame inView:controlView characterIndex:NSNotFound layoutManager:nil];
[self _drawWithFrame:cellFrame inView:controlView characterIndex:NSNotFound layoutManager:nil];
}

- (void)drawTokenWithFrame:(NSRect)rect inView:(NSView *)controlView
Expand Down Expand Up @@ -173,10 +190,11 @@ - (NSColor *)tokenTitleColorForDrawingMode:(OEXTokenDrawingMode)drawingMode

- (NSBezierPath *)tokenPathForBounds:(NSRect)bounds joinStyle:(OEXTokenJoinStyle)jointStyle
{
bounds.size.width = MAX(bounds.size.width, kOEXTokenAttachmentTokenMargin * 2 + bounds.size.height);
CGFloat tokenMargin = [self tokenMargin];
bounds.size.width = MAX(bounds.size.width, tokenMargin * 2 + bounds.size.height);

CGFloat radius = bounds.size.height / 2;
CGRect innerRect = NSInsetRect(bounds, kOEXTokenAttachmentTokenMargin + radius, 0);
CGRect innerRect = NSInsetRect(bounds, tokenMargin + radius, 0);
CGFloat x0 = NSMinX(bounds);
CGFloat x1 = NSMinX(innerRect);
CGFloat x2 = NSMaxX(innerRect);
Expand Down
2 changes: 1 addition & 1 deletion OEXTokenField.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
/** The token field's delegate.
@discussion The delegate must adopt the `<OEXTokenFieldDelegate>` protocol.
*/
@property(nonatomic, assign) id <OEXTokenFieldDelegate> delegate;
@property (atomic, weak) id<OEXTokenFieldDelegate, NSTokenFieldDelegate> delegate;

@end

Expand Down
2 changes: 1 addition & 1 deletion OEXTokenFieldCell.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
/** The token field cell's delegate.
@discussion The delegate must adopt the `<OEXTokenFieldCellDelegate>` protocol.
*/
@property(nonatomic, assign) id <OEXTokenFieldCellDelegate> delegate;
@property(weak) id<OEXTokenFieldCellDelegate, NSTokenFieldCellDelegate> delegate;

/** @name Displaying Tokenized Attachment Cells */

Expand Down
10 changes: 5 additions & 5 deletions OEXTokenFieldCell.m
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

static int kOEXTokenFieldCellRepresentedObjectKey;

@interface OEXTokenFieldCell () <OEXTokenTextStorageDelegate>
@interface OEXTokenFieldCell () <OEXTokenTextStorageDelegate, NSTextStorageDelegate>
@end

@implementation OEXTokenFieldCell
Expand All @@ -33,9 +33,9 @@ - (NSAttributedString *)attributedStringValue

NSAttributedString *attrString = super.attributedStringValue;
NSRange range = NSMakeRange(0, attrString.length);
[attrString enumerateAttribute:NSAttachmentAttributeName inRange:range options:0 usingBlock:^(NSTextAttachment *attachment, NSRange range, BOOL *stop) {
[attrString enumerateAttribute:NSAttachmentAttributeName inRange:range options:0 usingBlock:^(NSTextAttachment *attachment, NSRange arange, BOOL *stop) {
if ( attachment ) {
[self updateTokenAttachment:attachment forAttributedString:[attrString attributedSubstringFromRange:range]];
[self updateTokenAttachment:attachment forAttributedString:[attrString attributedSubstringFromRange:arange]];
}
}];
return attrString;
Expand All @@ -48,8 +48,8 @@ - (void)setAttributedStringValue:(NSAttributedString *)attrString

NSMutableArray *objects = [NSMutableArray new];
NSRange range = NSMakeRange(0, attrString.length);
[attrString enumerateAttribute:NSAttachmentAttributeName inRange:range options:0 usingBlock:^(id attachment, NSRange range, BOOL *stop) {
id representedObject = [self representedObjectWithAttachment:attachment attributedString:[attrString attributedSubstringFromRange:range]];
[attrString enumerateAttribute:NSAttachmentAttributeName inRange:range options:0 usingBlock:^(id attachment, NSRange arange, BOOL *stop) {
id representedObject = [self representedObjectWithAttachment:attachment attributedString:[attrString attributedSubstringFromRange:arange]];
[objects addObject:representedObject];
}];

Expand Down
4 changes: 2 additions & 2 deletions OEXTokenTextStorage.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
*/
@interface OEXTokenTextStorage : NSTextStorage

@property(nonatomic, weak) id <OEXTokenTextStorageDelegate> delegate;
@property(weak) id <OEXTokenTextStorageDelegate, NSTextStorageDelegate> delegate;

- (id)initWithAttributedString:(NSAttributedString *)attrStr;

Expand All @@ -27,4 +27,4 @@
@optional
- (void)tokenTextStorage:(OEXTokenTextStorage *)textStorage updateTokenAttachment:(NSTextAttachment *)attachment forRange:(NSRange)range;

@end
@end
12 changes: 7 additions & 5 deletions OEXTokenTextStorage.m
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ @implementation OEXTokenTextStorage
NSMutableAttributedString *_string;
}

@dynamic delegate;

#pragma mark - init

- (id)initWithAttributedString:(NSAttributedString *)attrStr
Expand Down Expand Up @@ -51,8 +53,8 @@ - (void)setAttributes:(NSDictionary *)attrs range:(NSRange)range
{
[_string setAttributes:attrs range:range];
NSTextAttachment *attachment = attrs[NSAttachmentAttributeName];
if ( attachment && [_delegate respondsToSelector:@selector(tokenTextStorage:updateTokenAttachment:forRange:)] )
[_delegate tokenTextStorage:self updateTokenAttachment:attachment forRange:range];
if ( attachment && [self.delegate respondsToSelector:@selector(tokenTextStorage:updateTokenAttachment:forRange:)] )
[self.delegate tokenTextStorage:self updateTokenAttachment:attachment forRange:range];
[self edited:NSTextStorageEditedAttributes range:range changeInLength:0];
}

Expand All @@ -69,9 +71,9 @@ - (void)replaceCharactersInRange:(NSRange)range withAttributedString:(NSAttribut
[_string replaceCharactersInRange:range withAttributedString:attrString];
NSRange strRange = NSMakeRange(range.location, attrString.length);

[_string enumerateAttribute:NSAttachmentAttributeName inRange:strRange options:0 usingBlock:^(NSTextAttachment *attachment, NSRange range, BOOL *stop) {
if ( attachment && [_delegate respondsToSelector:@selector(tokenTextStorage:updateTokenAttachment:forRange:)] ) {
[_delegate tokenTextStorage:self updateTokenAttachment:attachment forRange:range];
[_string enumerateAttribute:NSAttachmentAttributeName inRange:strRange options:0 usingBlock:^(NSTextAttachment *attachment, NSRange arange, BOOL *stop) {
if ( attachment && [self.delegate respondsToSelector:@selector(tokenTextStorage:updateTokenAttachment:forRange:)] ) {
[self.delegate tokenTextStorage:self updateTokenAttachment:attachment forRange:arange];
}
}];
[self edited:NSTextStorageEditedAttributes | NSTextStorageEditedCharacters range:range changeInLength:strRange.length - range.length];
Expand Down