FLEXExplorerToolbar.m 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  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 "FLEXToolbarItem.h"
  11. #import "FLEXResources.h"
  12. #import "FLEXUtility.h"
  13. @interface FLEXExplorerToolbar ()
  14. @property (nonatomic, readwrite) FLEXToolbarItem *selectItem;
  15. @property (nonatomic, readwrite) FLEXToolbarItem *moveItem;
  16. @property (nonatomic, readwrite) FLEXToolbarItem *globalsItem;
  17. @property (nonatomic, readwrite) FLEXToolbarItem *closeItem;
  18. @property (nonatomic, readwrite) FLEXToolbarItem *hierarchyItem;
  19. @property (nonatomic, readwrite) UIView *dragHandle;
  20. @property (nonatomic) UIImageView *dragHandleImageView;
  21. @property (nonatomic) UIView *selectedViewDescriptionContainer;
  22. @property (nonatomic) UIView *selectedViewDescriptionSafeAreaContainer;
  23. @property (nonatomic) UIView *selectedViewColorIndicator;
  24. @property (nonatomic) UILabel *selectedViewDescriptionLabel;
  25. @property (nonatomic,readwrite) UIView *backgroundView;
  26. @end
  27. @implementation FLEXExplorerToolbar
  28. - (id)initWithFrame:(CGRect)frame {
  29. self = [super initWithFrame:frame];
  30. if (self) {
  31. self.backgroundView = [UIView new];
  32. self.backgroundView.backgroundColor = [FLEXColor secondaryBackgroundColorWithAlpha:0.95];
  33. [self addSubview:self.backgroundView];
  34. self.dragHandle = [UIView new];
  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.dragHandleImageView.tintColor = [[FLEXColor iconColor] colorWithAlphaComponent:0.666];
  40. [self.dragHandle addSubview:self.dragHandleImageView];
  41. UIImage *globalsIcon = [FLEXResources globeIcon];
  42. self.globalsItem = [FLEXToolbarItem toolbarItemWithTitle:@"menu" image:globalsIcon];
  43. UIImage *listIcon = [FLEXResources listIcon];
  44. self.hierarchyItem = [FLEXToolbarItem toolbarItemWithTitle:@"views" image:listIcon];
  45. UIImage *selectIcon = [FLEXResources selectIcon];
  46. self.selectItem = [FLEXToolbarItem toolbarItemWithTitle:@"select" image:selectIcon];
  47. UIImage *moveIcon = [FLEXResources moveIcon];
  48. self.moveItem = [FLEXToolbarItem toolbarItemWithTitle:@"move" image:moveIcon];
  49. UIImage *closeIcon = [FLEXResources closeIcon];
  50. self.closeItem = [FLEXToolbarItem toolbarItemWithTitle:@"close" image:closeIcon];
  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. self.toolbarItems = @[_globalsItem, _hierarchyItem, _selectItem, _moveItem, _closeItem];
  66. }
  67. return self;
  68. }
  69. - (void)layoutSubviews {
  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. if (![_selectedViewOverlayColor isEqual:selectedViewOverlayColor]) {
  151. _selectedViewOverlayColor = selectedViewOverlayColor;
  152. self.selectedViewColorIndicator.backgroundColor = selectedViewOverlayColor;
  153. }
  154. }
  155. - (void)setSelectedViewDescription:(NSString *)selectedViewDescription {
  156. if (![_selectedViewDescription isEqual:selectedViewDescription]) {
  157. _selectedViewDescription = selectedViewDescription;
  158. self.selectedViewDescriptionLabel.text = selectedViewDescription;
  159. BOOL showDescription = selectedViewDescription.length > 0;
  160. self.selectedViewDescriptionContainer.hidden = !showDescription;
  161. }
  162. }
  163. #pragma mark - Sizing Convenience Methods
  164. + (UIFont *)descriptionLabelFont {
  165. return [UIFont systemFontOfSize:12.0];
  166. }
  167. + (CGFloat)toolbarItemHeight {
  168. return 44.0;
  169. }
  170. + (CGFloat)dragHandleWidth {
  171. return [FLEXResources dragHandle].size.width;
  172. }
  173. + (CGFloat)descriptionLabelHeight {
  174. return ceil([[self descriptionLabelFont] lineHeight]);
  175. }
  176. + (CGFloat)descriptionVerticalPadding {
  177. return 2.0;
  178. }
  179. + (CGFloat)descriptionContainerHeight {
  180. return [self descriptionVerticalPadding] * 2.0 + [self descriptionLabelHeight];
  181. }
  182. + (CGFloat)selectedViewColorIndicatorDiameter {
  183. return ceil([self descriptionLabelHeight] / 2.0);
  184. }
  185. + (CGFloat)horizontalPadding {
  186. return 11.0;
  187. }
  188. - (CGSize)sizeThatFits:(CGSize)size {
  189. CGFloat height = 0.0;
  190. height += [[self class] toolbarItemHeight];
  191. height += [[self class] descriptionContainerHeight];
  192. return CGSizeMake(size.width, height);
  193. }
  194. - (CGRect)safeArea {
  195. CGRect safeArea = self.bounds;
  196. if (@available(iOS 11.0, *)) {
  197. safeArea = UIEdgeInsetsInsetRect(self.bounds, self.safeAreaInsets);
  198. }
  199. return safeArea;
  200. }
  201. @end