You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

273 lines
10 KiB

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