FLEXExplorerToolbar.m 11 KB

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