PageRenderTime 24ms CodeModel.GetById 1ms app.highlight 20ms RepoModel.GetById 1ms app.codeStats 0ms

/core/externals/update-engine/externals/google-toolbox-for-mac/iPhone/GTMUIView+SubtreeDescription.m

http://macfuse.googlecode.com/
Objective C | 145 lines | 99 code | 15 blank | 31 comment | 17 complexity | 44e8f98c35b78f86ffee1bfdb36c45fc MD5 | raw file
  1//
  2//  GTMUIView+SubtreeDescription.m
  3//
  4//  Copyright 2009 Google Inc.
  5//
  6//  Licensed under the Apache License, Version 2.0 (the "License"); you may not
  7//  use this file except in compliance with the License.  You may obtain a copy
  8//  of the License at
  9//
 10//  http://www.apache.org/licenses/LICENSE-2.0
 11//
 12//  Unless required by applicable law or agreed to in writing, software
 13//  distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 14//  WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
 15//  License for the specific language governing permissions and limitations
 16//  under the License.
 17//
 18#import "GTMUIView+SubtreeDescription.h"
 19
 20#if DEBUG || INCLUDE_UIVIEW_SUBTREE_DESCRIPTION
 21
 22static void AppendLabelFloat(NSMutableString *s, NSString *label, float f) {
 23  [s appendString:label];
 24  // Respects gcc warning about using == with floats.
 25  if (fabs(f - floor(f)) < 1.0e-8) { // Essentially integer.
 26    int d = f;
 27    // Respects gcc warning about casting floats to ints.
 28    [s appendFormat:@"%d", d];
 29  } else {
 30    [s appendFormat:@"%3.1f", f];
 31  }
 32}
 33
 34static NSMutableString *SublayerDescriptionLine(CALayer *layer) {
 35  NSMutableString *result = [NSMutableString string];
 36  [result appendFormat:@"%@ %p {", [layer class], layer];
 37  CGRect frame = [layer frame];
 38  if (!CGRectIsEmpty(frame)) {
 39    AppendLabelFloat(result, @"x:", frame.origin.x);
 40    AppendLabelFloat(result, @" y:", frame.origin.y);
 41    AppendLabelFloat(result, @" w:", frame.size.width);
 42    AppendLabelFloat(result, @" h:", frame.size.height);
 43  }
 44  [result appendFormat:@"}"];
 45  if ([layer isHidden]) {
 46    [result appendString:@" hid"];
 47  }
 48  [result appendString:@"\n"];
 49  return result;
 50}
 51
 52// |sublayersDescription| has a guard so we'll only call this if it is safe
 53// to call.
 54static NSMutableString *SublayerDescriptionAtLevel(CALayer *layer, int level) {
 55  NSMutableString *result = [NSMutableString string];
 56  for (int i = 0; i < level; ++i) {
 57    [result appendString:@"  "];
 58  }
 59  [result appendString:SublayerDescriptionLine(layer)];
 60  // |sublayers| is defined in the QuartzCore framework, which isn't guaranteed
 61  // to be linked to this program. (So we don't include the header.)
 62  NSArray *layers = [layer performSelector:NSSelectorFromString(@"sublayers")];
 63  for (CALayer *l in layers) {
 64    [result appendString:SublayerDescriptionAtLevel(l, level+1)];
 65  }
 66  return result;
 67}
 68
 69@implementation UIView (SubtreeDescription)
 70
 71// TODO: Consider flagging things which might help in debugging:
 72// - alpha < 10%
 73// - origin not zero
 74// - non-opaque
 75// - transform if not identity
 76// - view not entirely within ancestor views
 77// - (possibly) tag==0
 78- (NSString *)gtm_subtreeDescriptionLine {
 79  NSMutableString *result = [NSMutableString string];
 80  [result appendFormat:@"%@ %p {", [self class], self];
 81  CGRect frame = [self frame];
 82  if (!CGRectIsEmpty(frame)) {
 83    AppendLabelFloat(result, @"x:", frame.origin.x);
 84    AppendLabelFloat(result, @" y:", frame.origin.y);
 85    AppendLabelFloat(result, @" w:", frame.size.width);
 86    AppendLabelFloat(result, @" h:", frame.size.height);
 87  }
 88  [result appendString:@"}"];
 89  if ([self isHidden]) {
 90    [result appendString:@" hid"];
 91  }
 92
 93  if ([self respondsToSelector:@selector(myViewDescriptionLine)]) {
 94    NSString *customDescription =
 95      [self performSelector:@selector(myViewDescriptionLine)];
 96    if (customDescription != nil) {
 97      [result appendFormat:@" %@", customDescription];
 98    }
 99  }
100
101  [result appendString:@"\n"];
102  return result;
103}
104
105- (NSString *)gtm_subtreeDescriptionAtLevel:(int)level {
106  NSMutableString *result = [NSMutableString string];
107  for (int i = 0; i < level; ++i) {
108    [result appendString:@"  "];
109  }
110  [result appendString:[self gtm_subtreeDescriptionLine]];
111  for (UIView *v in [self subviews]) {
112    [result appendString:[v gtm_subtreeDescriptionAtLevel:level+1]];
113  }
114  return result;
115}
116
117- (NSString *)subtreeDescription {
118  NSMutableString *result =
119    [[[self gtm_subtreeDescriptionLine] mutableCopy] autorelease];
120  for (UIView *v in [self subviews]) {
121    [result appendString:[v gtm_subtreeDescriptionAtLevel:1]];
122  }
123  return result;
124}
125
126// for debugging dump the layer hierarchy, frames and isHidden.
127- (NSString *)sublayersDescription {
128  CALayer *layer = [self layer];
129  SEL sublayers = NSSelectorFromString(@"sublayers");
130  if (![layer respondsToSelector:sublayers]) {
131    return @"*** Sorry: This app is not linked with the QuartzCore framework.";
132  }
133  NSMutableString *result = SublayerDescriptionLine(layer);
134  NSArray *layers = [layer performSelector:sublayers];
135  for (CALayer *l in layers) {
136    [result appendString:SublayerDescriptionAtLevel(l, 1)];
137  }
138  return result;
139}
140
141@end
142
143#endif  // DEBUG
144
145