Highlight selected NSBrowserCell and draw a focus ring on its NSText?

What am i trying to do

I am trying to add editing functionality in place in the NSKrowser Connection Kit. I would like this behavior to be functionally and visually similar to the Finder implementation.

The visual effect I'm aiming for

Finder's implementation

What am i still

NSBrowserCell subclass

The arrows indicate the focus ring and cell selection in the Finder implementation, as well as its absence in mine.

I tried

  • Setting the background color of the cell in the controller, in it drawInteriorWithFrame method
  • The same for the field editor
  • setFocusRingType:NSFocusRingTypeDefault for the field and cell editor in both the controller and the drawing method
  • Manually draw highlight color in paint method
  • Various combinations of the above and, undoubtedly, some of them I forgot.

The best I have been able to do is get the area surrounding the cell image painted in the highlight color.

Are there any fundamental points that I miss here? Can anyone suggest a starting point for this? Is drawInteriorWithFrame place for this?

I have some editing work - I just have problems with the visual aspects.

Code for editing:

 // In the main controller int selectedColumn = [browser selectedColumn]; int selectedRow = [browser selectedRowInColumn:selectedColumn]; NSMatrix *theMatrix = [browser matrixInColumn:selectedColumn]; NSRect cellFrame = [theMatrix cellFrameAtRow:selectedRow column:0]; NSText *fieldEditor = [[browser window] fieldEditor:YES forObject:editingCell]; [cell editWithFrame:cellFrame inView:theMatrix editor:fieldEditor delegate:self event:nil]; 

And in my subclass of NSBrowserCell:

 - (void)drawInteriorWithFrame:(NSRect)cellFrame inView:(NSView *)controlView { image = [[self representedObject] iconWithSize:[self imageSize]]; [self setImage:image]; NSRect imageFrame, highlightRect, textFrame; // Divide the cell into 2 parts, the image part (on the left) and the text part. NSDivideRect(cellFrame, &imageFrame, &textFrame, ICON_INSET_HORIZ + ICON_TEXT_SPACING + [self imageSize].width, NSMinXEdge); imageFrame.origin.x += ICON_INSET_HORIZ; imageFrame.size = [self imageSize]; [super drawInteriorWithFrame:cellFrame inView:controlView]; } - (void)editWithFrame:(NSRect)aRect inView:(NSView *)controlView editor:(NSText *)textObj delegate:(id)anObject event:(NSEvent *)theEvent { NSRect imageRect, textRect; NSDivideRect(aRect , &imageRect, &textRect, 20, NSMinXEdge); self.editing = YES; [super editWithFrame: textRect inView: controlView editor:textObj delegate:anObject event:theEvent]; } 
+4
source share
2 answers

You must draw the focus ring yourself. Add the following to drawWithFrame in a subclass of NSBrowserCell

 [NSGraphicsContext saveGraphicsState]; [[controlView superview] lockFocus]; NSSetFocusRingStyle(NSFocusRingAbove); [[NSBezierPath bezierPathWithRect:NSInsetRect(frame,-1,-1)] fill]; [[controlView superview] unlockFocus]; [NSGraphicsContext restoreGraphicsState]; 
+3
source

Try the subclass field editor object and override the drawRect function as follows:

 - (void)drawRect:(NSRect)rect { [super drawRect:rect]; NSSetFocusRingStyle(NSFocusRingOnly); NSRectFill([self bounds]); } 

Hope this helps.

-1
source

Source: https://habr.com/ru/post/1400796/


All Articles