PageRenderTime 18ms CodeModel.GetById 11ms RepoModel.GetById 0ms 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
Possible License(s): Apache-2.0, BSD-3-Clause, GPL-2.0
  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. #if DEBUG || INCLUDE_UIVIEW_SUBTREE_DESCRIPTION
  20. static void AppendLabelFloat(NSMutableString *s, NSString *label, float f) {
  21. [s appendString:label];
  22. // Respects gcc warning about using == with floats.
  23. if (fabs(f - floor(f)) < 1.0e-8) { // Essentially integer.
  24. int d = f;
  25. // Respects gcc warning about casting floats to ints.
  26. [s appendFormat:@"%d", d];
  27. } else {
  28. [s appendFormat:@"%3.1f", f];
  29. }
  30. }
  31. static NSMutableString *SublayerDescriptionLine(CALayer *layer) {
  32. NSMutableString *result = [NSMutableString string];
  33. [result appendFormat:@"%@ %p {", [layer class], layer];
  34. CGRect frame = [layer frame];
  35. if (!CGRectIsEmpty(frame)) {
  36. AppendLabelFloat(result, @"x:", frame.origin.x);
  37. AppendLabelFloat(result, @" y:", frame.origin.y);
  38. AppendLabelFloat(result, @" w:", frame.size.width);
  39. AppendLabelFloat(result, @" h:", frame.size.height);
  40. }
  41. [result appendFormat:@"}"];
  42. if ([layer isHidden]) {
  43. [result appendString:@" hid"];
  44. }
  45. [result appendString:@"\n"];
  46. return result;
  47. }
  48. // |sublayersDescription| has a guard so we'll only call this if it is safe
  49. // to call.
  50. static NSMutableString *SublayerDescriptionAtLevel(CALayer *layer, int level) {
  51. NSMutableString *result = [NSMutableString string];
  52. for (int i = 0; i < level; ++i) {
  53. [result appendString:@" "];
  54. }
  55. [result appendString:SublayerDescriptionLine(layer)];
  56. // |sublayers| is defined in the QuartzCore framework, which isn't guaranteed
  57. // to be linked to this program. (So we don't include the header.)
  58. NSArray *layers = [layer performSelector:NSSelectorFromString(@"sublayers")];
  59. for (CALayer *l in layers) {
  60. [result appendString:SublayerDescriptionAtLevel(l, level+1)];
  61. }
  62. return result;
  63. }
  64. @implementation UIView (SubtreeDescription)
  65. // TODO: Consider flagging things which might help in debugging:
  66. // - alpha < 10%
  67. // - origin not zero
  68. // - non-opaque
  69. // - transform if not identity
  70. // - view not entirely within ancestor views
  71. // - (possibly) tag==0
  72. - (NSString *)gtm_subtreeDescriptionLine {
  73. NSMutableString *result = [NSMutableString string];
  74. [result appendFormat:@"%@ %p {", [self class], self];
  75. CGRect frame = [self frame];
  76. if (!CGRectIsEmpty(frame)) {
  77. AppendLabelFloat(result, @"x:", frame.origin.x);
  78. AppendLabelFloat(result, @" y:", frame.origin.y);
  79. AppendLabelFloat(result, @" w:", frame.size.width);
  80. AppendLabelFloat(result, @" h:", frame.size.height);
  81. }
  82. [result appendString:@"}"];
  83. if ([self isHidden]) {
  84. [result appendString:@" hid"];
  85. }
  86. if ([self respondsToSelector:@selector(myViewDescriptionLine)]) {
  87. NSString *customDescription =
  88. [self performSelector:@selector(myViewDescriptionLine)];
  89. if (customDescription != nil) {
  90. [result appendFormat:@" %@", customDescription];
  91. }
  92. }
  93. [result appendString:@"\n"];
  94. return result;
  95. }
  96. - (NSString *)gtm_subtreeDescriptionAtLevel:(int)level {
  97. NSMutableString *result = [NSMutableString string];
  98. for (int i = 0; i < level; ++i) {
  99. [result appendString:@" "];
  100. }
  101. [result appendString:[self gtm_subtreeDescriptionLine]];
  102. for (UIView *v in [self subviews]) {
  103. [result appendString:[v gtm_subtreeDescriptionAtLevel:level+1]];
  104. }
  105. return result;
  106. }
  107. - (NSString *)subtreeDescription {
  108. NSMutableString *result =
  109. [[[self gtm_subtreeDescriptionLine] mutableCopy] autorelease];
  110. for (UIView *v in [self subviews]) {
  111. [result appendString:[v gtm_subtreeDescriptionAtLevel:1]];
  112. }
  113. return result;
  114. }
  115. // for debugging dump the layer hierarchy, frames and isHidden.
  116. - (NSString *)sublayersDescription {
  117. CALayer *layer = [self layer];
  118. SEL sublayers = NSSelectorFromString(@"sublayers");
  119. if (![layer respondsToSelector:sublayers]) {
  120. return @"*** Sorry: This app is not linked with the QuartzCore framework.";
  121. }
  122. NSMutableString *result = SublayerDescriptionLine(layer);
  123. NSArray *layers = [layer performSelector:sublayers];
  124. for (CALayer *l in layers) {
  125. [result appendString:SublayerDescriptionAtLevel(l, 1)];
  126. }
  127. return result;
  128. }
  129. @end
  130. #endif // DEBUG