Respond to mouse events in text field in view-based table view

后端 未结 6 1333
甜味超标
甜味超标 2020-12-02 16:11

I have text fields inside a custom view inside an NSOutlineView. Editing one of these cells requires a single click, a pause, and another single click. The firs

6条回答
  •  广开言路
    2020-12-02 16:12

    I wrote the following to support the case for when you have a more complex NSTableViewCell with multiple text fields or where the text field doesn't occupy the whole cell. There a trick in here for flipping y values because when you switch between the NSOutlineView or NSTableView and it's NSTableCellViews the coordinate system gets flipped.

    - (void)mouseDown:(NSEvent *)theEvent
    {
        [super mouseDown: theEvent];
    
        NSPoint thePoint = [self.window.contentView convertPoint: theEvent.locationInWindow
                                                          toView: self];
        NSInteger row = [self rowAtPoint: thePoint];
        if (row != -1) {
            NSView *view = [self viewAtColumn: 0
                                          row: row
                              makeIfNecessary: NO];
    
            thePoint = [view convertPoint: thePoint
                                 fromView: self];
            if ([view isFlipped] != [self isFlipped])
                thePoint.y = RectGetHeight(view.bounds) - thePoint.y;
    
            view = [view hitTest: thePoint];
            if ([view isKindOfClass: [NSTextField class]]) {
                NSTextField *textField = (NSTextField *)view;
                if (textField.isEnabled && textField.window.firstResponder != textField)
    
                    dispatch_async(dispatch_get_main_queue(), ^{
                        [textField selectText: nil];
                    });
            }
        }
    }
    

提交回复
热议问题