当NSView到达前台时获得通知

时间:2015-06-02 12:26:26

标签: swift cocoa events nstableview nstabview

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

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

现在我想知道如何判断表格视图是否在前面(即第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;
        });
    }
}

0 个答案:

没有答案