Guest User

Untitled

a guest
Jul 16th, 2018
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.74 KB | None | 0 0
  1. @import <AppKit/CPView.j>
  2.  
  3. @implementation CPView (sizeToFit)
  4.  
  5. - (void)sizeToFit
  6. {
  7. [self sizeToFitWithInset:[self hasThemeAttribute:@"content-inset"] && [self currentValueForThemeAttribute:@"content-inset"] || CGInsetMakeZero()];
  8. }
  9.  
  10. - (void)sizeToFitWithInset:(CGInset)inset
  11. {
  12. var result = CGSizeMake(inset.left, inset.top),
  13. rect = {origin: CGPointMakeZero(), size: result},
  14. roomForAnchoredSubviews = CGSizeMakeZero();
  15.  
  16. for (var i = 0, count = [_subviews count]; i < count; i++)
  17. {
  18. var subview = _subviews[i],
  19. autoresizingMask = [subview autoresizingMask];
  20.  
  21. if (autoresizingMask & CPViewMinYMargin)
  22. {
  23. // bottom-anchored view
  24. var overlap = CGRectIntersection(rect, [subview frame]);
  25. if (!CGRectIsEmpty(overlap))
  26. roomForAnchoredSubviews.height += CGRectGetHeight(overlap);
  27. }
  28. else if (autoresizingMask & CPViewMinXMargin)
  29. {
  30. // right-anchored view
  31. var overlap = CGRectIntersection(rect, [subview frame]);
  32. if (!CGRectIsEmpty(overlap))
  33. roomForAnchoredSubviews.width += CGRectGetWidth(overlap);
  34. }
  35. else
  36. {
  37. var frame = [subview frame],
  38. maxX = CGRectGetMaxX(frame),
  39. maxY = CGRectGetMaxY(frame);
  40.  
  41. if (result.width < maxX)
  42. result.width = maxX;
  43. if (result.height < maxY)
  44. result.height = maxY;
  45. }
  46.  
  47. [self setFrameSize:CGSizeMake(result.width + roomForAnchoredSubviews.width, result.height + roomForAnchoredSubviews.height)];
  48. }
  49.  
  50. result.width += roomForAnchoredSubviews.width + inset.right;
  51. result.height += roomForAnchoredSubviews.height + inset.bottom;
  52.  
  53. [self setFrameSize:result];
  54. }
  55.  
  56. - (BOOL)hasThemeAttribute:(CPString)attributeName
  57. {
  58. return !![[self _themeAttributeDictionary] objectForKey:attributeName];
  59. }
  60.  
  61. @end
Add Comment
Please, Sign In to add comment