0

我在 aNSTableView的第二个选项卡内有一个NSTabView. 该表使用一些自定义事件代码来处理被点击的表中的按钮被识别。问题是,即使我打开了选项卡视图的第一个或第三个选项卡(即表格视图不可见),这些事件也会被拦截(因此,如果我在 1./3. 选项卡上有按钮,它们将不起作用)。

为了解决这个问题,我在我的自定义表格视图类中添加了一个标志,以仅在标志为真时执行自定义事件代码,如果不只是返回原始事件。

现在我想知道如何判断表格视图是否在前面(即标签号 2 已打开)?有什么优雅的方法来检查这个还是我需要直接从标签栏得到这个?

万一这很重要,这是我的自定义表格代码...

class ButtonTableView : NSTableView
{
    var isAtForeground:Bool = false;

    override init(frame frameRect:NSRect) {
        super.init(frame: frameRect);
    }

    required init?(coder:NSCoder) {
        super.init(coder: coder);
        addEventInterception();
    }

    /// Get the row number (if any) that coincides with a specific point - where the point is in window coordinates.
    func rowContainingWindowPoint(windowPoint:CGPoint) -> Int? {
        var rowNum:Int?;
        var tableRectInWindowCoords = convertRect(bounds, toView: nil);
        if (CGRectContainsPoint(tableRectInWindowCoords, windowPoint))
        {
            let tabPt = convertPoint(windowPoint, fromView: nil);
            let indexOfClickedRow = rowAtPoint(tabPt);
            if (indexOfClickedRow > -1 && indexOfClickedRow < numberOfRows)
            {
                rowNum = indexOfClickedRow;
            }
        }
        return rowNum;
    }

    func addEventInterception() {
        NSEvent.addLocalMonitorForEventsMatchingMask(.LeftMouseDownMask, handler:
        {
            (theEvent) -> NSEvent! in

            /* Don't bother if the table view is not in the foreground! */
            if (!self.isAtForeground) { return theEvent; }

            var e:NSEvent? = theEvent;
            let p:NSPoint = theEvent.locationInWindow;

            /* Did the mouse-down event take place on a row in the table? */
            if let row = self.rowContainingWindowPoint(p)
            {
                let localPoint = self.convertPoint(p, fromView: nil);

                /* Get the column index that was clicked in. */
                let col = self.columnAtPoint(localPoint);

                /* Get the table cell view on which the mouse down event took place. */
                let tcv = self.viewAtColumn(col, row: row, makeIfNecessary: false) as! NSTableCellView;

                /* Did the click occur on the table view? */
                let tableBoundsInWindowCoords:NSRect = self.convertRect(self.bounds, toView: nil);
                if (CGRectContainsPoint(tableBoundsInWindowCoords, p))
                {
                    /* If clicked anywhere in the table cell view, only allow button areas to pass. */
                    /* subView is the NSTableCellView's last subview. */
                    var subView = tcv.subviews.last! as! NSView;
                    let subViewBoundsInWindowCoords:NSRect = subView.convertRect(subView.bounds, toView: nil);
                    /* Did the click occur on the subview part of the view? */
                    if (CGRectContainsPoint(subViewBoundsInWindowCoords, p))
                    {
                        /* Only allow button subviews to be clicked. */
                        if let button = subView as? NSButton
                        {
                            // Create a modified event, where the <location> property has been
                            // altered so that it looks like the click took place in the
                            // NSTableCellView itself.
                            let newLocation = tcv.convertPoint(tcv.bounds.origin, toView: nil);
                            e = theEvent.cloneEventButUseAdjustedWindowLocation(newLocation);
                            return e;
                        }
                    }
                    /* If we've reched here, the user clicked anywhere on the table but not one of the buttons. */
                    return nil;
                }
            }
            return e;
        });
    }
}
4

0 回答 0