From d5f281320f18d32d71de169012e56dbda4db5fdc Mon Sep 17 00:00:00 2001 From: Alexander Ljungberg Date: Mon, 9 May 2011 14:37:54 -0400 Subject: [PATCH] [CPView _isVisible] convenience method. --- AppKit/CPView.j | 8 ++++++++ Tests/AppKit/CPViewTest.j | 30 ++++++++++++++++++++++++++++++ 2 files changed, 38 insertions(+) diff --git a/AppKit/CPView.j b/AppKit/CPView.j index bd2a5be03..58b37ea70 100644 --- a/AppKit/CPView.j +++ b/AppKit/CPView.j @@ -1284,6 +1284,14 @@ var CPViewFlags = { }, return view !== nil; } +/*! + Returns YES if the view is not hidden, has no hidden ancestor and doesn't belong to a hidden window. +*/ +- (BOOL)_isVisible +{ + return ![self isHiddenOrHasHiddenAncestor] && [[self window] isVisible]; +} + /*! Called when the return value of isHiddenOrHasHiddenAncestor becomes YES, e.g. when this view becomes hidden due to a setHidden:YES message to diff --git a/Tests/AppKit/CPViewTest.j b/Tests/AppKit/CPViewTest.j index 01aa80af2..ac95d54a9 100644 --- a/Tests/AppKit/CPViewTest.j +++ b/Tests/AppKit/CPViewTest.j @@ -73,4 +73,34 @@ [self assertFalse:[view hasThemeAttribute:@"foobar"] message:[view className] + " should not have theme attribute \"" + firstKey + "\""]; } +- (void)testIsVisible +{ + [self assertFalse:[view _isVisible] message:"view must belong to a window to be visible"]; + var theWindow = [[CPWindow alloc] initWithContentRect:CGRectMakeZero() styleMask:CPBorderlessBridgeWindowMask], + contentView = [theWindow contentView]; + + [self assertFalse:[contentView _isVisible] message:"view must belong to a visible window to be visible"]; + [theWindow orderFront:self]; + // Fake this because we don't have the DOM in unit tests. + [theWindow._isVisible = YES]; + [self assertTrue:[contentView _isVisible] message:"view is the content view of a visible window, hence visible"]; + + [self assertFalse:[view _isVisible] message:"view must belong to a window to be visible"]; + [contentView addSubview:view]; + [self assertTrue:[view _isVisible] message:"view is a subview of a visible content view, hence visible"]; + + [view setHidden:YES]; + [self assertFalse:[view _isVisible] message:"view is hidden"]; + + [view setHidden:NO]; + [self assertTrue:[view _isVisible] message:"view is not hidden again"]; + + [contentView setHidden:YES]; + [self assertFalse:[view _isVisible] message:"a superview is hidden"]; + + [contentView setHidden:NO]; + [contentView removeFromSuperview]; + [self assertFalse:[view _isVisible] message:"a superview does not belong to a visible window"]; +} + @end