FLEXExplorerToolbar.m 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  1. //
  2. // FLEXExplorerToolbar.m
  3. // Flipboard
  4. //
  5. // Created by Ryan Olson on 4/4/14.
  6. // Copyright (c) 2014 Flipboard. All rights reserved.
  7. //
  8. #import "FLEXExplorerToolbar.h"
  9. #import "FLEXToolbarItem.h"
  10. #import "FLEXResources.h"
  11. #import "FLEXUtility.h"
  12. @interface FLEXExplorerToolbar ()
  13. @property (nonatomic, strong, readwrite) FLEXToolbarItem *selectItem;
  14. @property (nonatomic, strong, readwrite) FLEXToolbarItem *moveItem;
  15. @property (nonatomic, strong, readwrite) FLEXToolbarItem *globalsItem;
  16. @property (nonatomic, strong, readwrite) FLEXToolbarItem *closeItem;
  17. @property (nonatomic, strong, readwrite) FLEXToolbarItem *hierarchyItem;
  18. @property (nonatomic, strong, readwrite) UIView *dragHandle;
  19. @property (nonatomic, strong) UIImageView *dragHandleImageView;
  20. @property (nonatomic, strong) UIView *selectedViewDescriptionContainer;
  21. @property (nonatomic, strong) UIView *selectedViewDescriptionSafeAreaContainer;
  22. @property (nonatomic, strong) UIView *selectedViewColorIndicator;
  23. @property (nonatomic, strong) UILabel *selectedViewDescriptionLabel;
  24. @property (nonatomic, strong,readwrite) UIView *backgroundView;
  25. @end
  26. @implementation FLEXExplorerToolbar
  27. - (id)initWithFrame:(CGRect)frame
  28. {
  29. self = [super initWithFrame:frame];
  30. if (self) {
  31. self.backgroundView = [[UIView alloc] init];
  32. self.backgroundView.backgroundColor = [UIColor colorWithWhite:1.0 alpha:0.95];
  33. [self addSubview:self.backgroundView];
  34. self.dragHandle = [[UIView alloc] init];
  35. self.dragHandle.backgroundColor = [UIColor clearColor];
  36. [self addSubview:self.dragHandle];
  37. UIImage *dragHandle = [FLEXResources dragHandle];
  38. self.dragHandleImageView = [[UIImageView alloc] initWithImage:dragHandle];
  39. [self.dragHandle addSubview:self.dragHandleImageView];
  40. UIImage *globalsIcon = [FLEXResources globeIcon];
  41. self.globalsItem = [FLEXToolbarItem toolbarItemWithTitle:@"menu" image:globalsIcon];
  42. UIImage *listIcon = [FLEXResources listIcon];
  43. self.hierarchyItem = [FLEXToolbarItem toolbarItemWithTitle:@"views" image:listIcon];
  44. UIImage *selectIcon = [FLEXResources selectIcon];
  45. self.selectItem = [FLEXToolbarItem toolbarItemWithTitle:@"select" image:selectIcon];
  46. UIImage *moveIcon = [FLEXResources moveIcon];
  47. self.moveItem = [FLEXToolbarItem toolbarItemWithTitle:@"move" image:moveIcon];
  48. UIImage *closeIcon = [FLEXResources closeIcon];
  49. self.closeItem = [FLEXToolbarItem toolbarItemWithTitle:@"close" image:closeIcon];
  50. self.selectedViewDescriptionContainer = [[UIView alloc] init];
  51. self.selectedViewDescriptionContainer.backgroundColor = [UIColor colorWithWhite:0.9 alpha:0.95];
  52. self.selectedViewDescriptionContainer.hidden = YES;
  53. [self addSubview:self.selectedViewDescriptionContainer];
  54. self.selectedViewDescriptionSafeAreaContainer = [[UIView alloc] init];
  55. self.selectedViewDescriptionSafeAreaContainer.backgroundColor = [UIColor clearColor];
  56. [self.selectedViewDescriptionContainer addSubview:self.selectedViewDescriptionSafeAreaContainer];
  57. self.selectedViewColorIndicator = [[UIView alloc] init];
  58. self.selectedViewColorIndicator.backgroundColor = [UIColor redColor];
  59. [self.selectedViewDescriptionSafeAreaContainer addSubview:self.selectedViewColorIndicator];
  60. self.selectedViewDescriptionLabel = [[UILabel alloc] init];
  61. self.selectedViewDescriptionLabel.backgroundColor = [UIColor clearColor];
  62. self.selectedViewDescriptionLabel.font = [[self class] descriptionLabelFont];
  63. [self.selectedViewDescriptionSafeAreaContainer addSubview:self.selectedViewDescriptionLabel];
  64. self.toolbarItems = @[_globalsItem, _hierarchyItem, _selectItem, _moveItem, _closeItem];
  65. }
  66. return self;
  67. }
  68. - (void)layoutSubviews
  69. {
  70. [super layoutSubviews];
  71. CGRect safeArea = [self safeArea];
  72. // Drag Handle
  73. const CGFloat kToolbarItemHeight = [[self class] toolbarItemHeight];
  74. self.dragHandle.frame = CGRectMake(CGRectGetMinX(safeArea), CGRectGetMinY(safeArea), [[self class] dragHandleWidth], kToolbarItemHeight);
  75. CGRect dragHandleImageFrame = self.dragHandleImageView.frame;
  76. dragHandleImageFrame.origin.x = FLEXFloor((self.dragHandle.frame.size.width - dragHandleImageFrame.size.width) / 2.0);
  77. dragHandleImageFrame.origin.y = FLEXFloor((self.dragHandle.frame.size.height - dragHandleImageFrame.size.height) / 2.0);
  78. self.dragHandleImageView.frame = dragHandleImageFrame;
  79. // Toolbar Items
  80. CGFloat originX = CGRectGetMaxX(self.dragHandle.frame);
  81. CGFloat originY = CGRectGetMinY(safeArea);
  82. CGFloat height = kToolbarItemHeight;
  83. CGFloat width = FLEXFloor((CGRectGetWidth(safeArea) - CGRectGetWidth(self.dragHandle.frame)) / [self.toolbarItems count]);
  84. for (UIView *toolbarItem in self.toolbarItems) {
  85. toolbarItem.frame = CGRectMake(originX, originY, width, height);
  86. originX = CGRectGetMaxX(toolbarItem.frame);
  87. }
  88. // Make sure the last toolbar item goes to the edge to account for any accumulated rounding effects.
  89. UIView *lastToolbarItem = [self.toolbarItems lastObject];
  90. CGRect lastToolbarItemFrame = lastToolbarItem.frame;
  91. lastToolbarItemFrame.size.width = CGRectGetMaxX(safeArea) - lastToolbarItemFrame.origin.x;
  92. lastToolbarItem.frame = lastToolbarItemFrame;
  93. self.backgroundView.frame = CGRectMake(0, 0, CGRectGetWidth(self.bounds), kToolbarItemHeight);
  94. const CGFloat kSelectedViewColorDiameter = [[self class] selectedViewColorIndicatorDiameter];
  95. const CGFloat kDescriptionLabelHeight = [[self class] descriptionLabelHeight];
  96. const CGFloat kHorizontalPadding = [[self class] horizontalPadding];
  97. const CGFloat kDescriptionVerticalPadding = [[self class] descriptionVerticalPadding];
  98. const CGFloat kDescriptionContainerHeight = [[self class] descriptionContainerHeight];
  99. CGRect descriptionContainerFrame = CGRectZero;
  100. descriptionContainerFrame.size.width = CGRectGetWidth(self.bounds);
  101. descriptionContainerFrame.size.height = kDescriptionContainerHeight;
  102. descriptionContainerFrame.origin.x = CGRectGetMinX(self.bounds);
  103. descriptionContainerFrame.origin.y = CGRectGetMaxY(self.bounds) - kDescriptionContainerHeight;
  104. self.selectedViewDescriptionContainer.frame = descriptionContainerFrame;
  105. CGRect descriptionSafeAreaContainerFrame = CGRectZero;
  106. descriptionSafeAreaContainerFrame.size.width = CGRectGetWidth(safeArea);
  107. descriptionSafeAreaContainerFrame.size.height = kDescriptionContainerHeight;
  108. descriptionSafeAreaContainerFrame.origin.x = CGRectGetMinX(safeArea);
  109. descriptionSafeAreaContainerFrame.origin.y = CGRectGetMinY(safeArea);
  110. self.selectedViewDescriptionSafeAreaContainer.frame = descriptionSafeAreaContainerFrame;
  111. // Selected View Color
  112. CGRect selectedViewColorFrame = CGRectZero;
  113. selectedViewColorFrame.size.width = kSelectedViewColorDiameter;
  114. selectedViewColorFrame.size.height = kSelectedViewColorDiameter;
  115. selectedViewColorFrame.origin.x = kHorizontalPadding;
  116. selectedViewColorFrame.origin.y = FLEXFloor((kDescriptionContainerHeight - kSelectedViewColorDiameter) / 2.0);
  117. self.selectedViewColorIndicator.frame = selectedViewColorFrame;
  118. self.selectedViewColorIndicator.layer.cornerRadius = ceil(selectedViewColorFrame.size.height / 2.0);
  119. // Selected View Description
  120. CGRect descriptionLabelFrame = CGRectZero;
  121. CGFloat descriptionOriginX = CGRectGetMaxX(selectedViewColorFrame) + kHorizontalPadding;
  122. descriptionLabelFrame.size.height = kDescriptionLabelHeight;
  123. descriptionLabelFrame.origin.x = descriptionOriginX;
  124. descriptionLabelFrame.origin.y = kDescriptionVerticalPadding;
  125. descriptionLabelFrame.size.width = CGRectGetMaxX(self.selectedViewDescriptionContainer.bounds) - kHorizontalPadding - descriptionOriginX;
  126. self.selectedViewDescriptionLabel.frame = descriptionLabelFrame;
  127. }
  128. #pragma mark - Setter Overrides
  129. - (void)setToolbarItems:(NSArray<FLEXToolbarItem *> *)toolbarItems {
  130. if (_toolbarItems == toolbarItems) {
  131. return;
  132. }
  133. // Remove old toolbar items, if any
  134. for (FLEXToolbarItem *item in _toolbarItems) {
  135. [item removeFromSuperview];
  136. }
  137. // Trim to 5 items if necessary
  138. if (toolbarItems.count > 5) {
  139. toolbarItems = [toolbarItems subarrayWithRange:NSMakeRange(0, 5)];
  140. }
  141. for (FLEXToolbarItem *item in toolbarItems) {
  142. [self addSubview:item];
  143. }
  144. _toolbarItems = toolbarItems.copy;
  145. // Lay out new items
  146. [self setNeedsLayout];
  147. [self layoutIfNeeded];
  148. }
  149. - (void)setSelectedViewOverlayColor:(UIColor *)selectedViewOverlayColor
  150. {
  151. if (![_selectedViewOverlayColor isEqual:selectedViewOverlayColor]) {
  152. _selectedViewOverlayColor = selectedViewOverlayColor;
  153. self.selectedViewColorIndicator.backgroundColor = selectedViewOverlayColor;
  154. }
  155. }
  156. - (void)setSelectedViewDescription:(NSString *)selectedViewDescription
  157. {
  158. if (![_selectedViewDescription isEqual:selectedViewDescription]) {
  159. _selectedViewDescription = selectedViewDescription;
  160. self.selectedViewDescriptionLabel.text = selectedViewDescription;
  161. BOOL showDescription = [selectedViewDescription length] > 0;
  162. self.selectedViewDescriptionContainer.hidden = !showDescription;
  163. }
  164. }
  165. #pragma mark - Sizing Convenience Methods
  166. + (UIFont *)descriptionLabelFont
  167. {
  168. return [UIFont systemFontOfSize:12.0];
  169. }
  170. + (CGFloat)toolbarItemHeight
  171. {
  172. return 44.0;
  173. }
  174. + (CGFloat)dragHandleWidth
  175. {
  176. return 30.0;
  177. }
  178. + (CGFloat)descriptionLabelHeight
  179. {
  180. return ceil([[self descriptionLabelFont] lineHeight]);
  181. }
  182. + (CGFloat)descriptionVerticalPadding
  183. {
  184. return 2.0;
  185. }
  186. + (CGFloat)descriptionContainerHeight
  187. {
  188. return [self descriptionVerticalPadding] * 2.0 + [self descriptionLabelHeight];
  189. }
  190. + (CGFloat)selectedViewColorIndicatorDiameter
  191. {
  192. return ceil([self descriptionLabelHeight] / 2.0);
  193. }
  194. + (CGFloat)horizontalPadding
  195. {
  196. return 11.0;
  197. }
  198. - (CGSize)sizeThatFits:(CGSize)size
  199. {
  200. CGFloat height = 0.0;
  201. height += [[self class] toolbarItemHeight];
  202. height += [[self class] descriptionContainerHeight];
  203. return CGSizeMake(size.width, height);
  204. }
  205. - (CGRect)safeArea
  206. {
  207. CGRect safeArea = self.bounds;
  208. #if FLEX_AT_LEAST_IOS11_SDK
  209. if (@available(iOS 11, *)) {
  210. safeArea = UIEdgeInsetsInsetRect(self.bounds, self.safeAreaInsets);
  211. }
  212. #endif
  213. return safeArea;
  214. }
  215. @end