FLEXExplorerToolbar.m 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. //
  2. // FLEXExplorerToolbar.m
  3. // Flipboard
  4. //
  5. // Created by Ryan Olson on 4/4/14.
  6. // Copyright (c) 2020 FLEX Team. 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. CGFloat itemPadding = 0;
  81. #if TARGET_OS_TV
  82. itemPadding = 40;
  83. #endif
  84. // Toolbar Items
  85. CGFloat originX = CGRectGetMaxX(self.dragHandle.frame);
  86. CGFloat originY = CGRectGetMinY(safeArea);
  87. CGFloat height = kToolbarItemHeight;
  88. CGFloat width = FLEXFloor((CGRectGetWidth(safeArea) - CGRectGetWidth(self.dragHandle.frame)) / self.toolbarItems.count);
  89. width = width - itemPadding;
  90. for (FLEXExplorerToolbarItem *toolbarItem in self.toolbarItems) {
  91. toolbarItem.currentItem.frame = CGRectMake(originX, originY, width, height);
  92. originX = CGRectGetMaxX(toolbarItem.currentItem.frame) + itemPadding;
  93. }
  94. // Make sure the last toolbar item goes to the edge to account for any accumulated rounding effects.
  95. UIView *lastToolbarItem = self.toolbarItems.lastObject.currentItem;
  96. CGRect lastToolbarItemFrame = lastToolbarItem.frame;
  97. lastToolbarItemFrame.size.width = CGRectGetMaxX(safeArea) - lastToolbarItemFrame.origin.x;
  98. lastToolbarItem.frame = lastToolbarItemFrame;
  99. self.backgroundView.frame = CGRectMake(0, 0, CGRectGetWidth(self.bounds), kToolbarItemHeight);
  100. const CGFloat kSelectedViewColorDiameter = [[self class] selectedViewColorIndicatorDiameter];
  101. const CGFloat kDescriptionLabelHeight = [[self class] descriptionLabelHeight];
  102. const CGFloat kHorizontalPadding = [[self class] horizontalPadding];
  103. const CGFloat kDescriptionVerticalPadding = [[self class] descriptionVerticalPadding];
  104. const CGFloat kDescriptionContainerHeight = [[self class] descriptionContainerHeight];
  105. CGRect descriptionContainerFrame = CGRectZero;
  106. descriptionContainerFrame.size.width = CGRectGetWidth(self.bounds);
  107. descriptionContainerFrame.size.height = kDescriptionContainerHeight;
  108. descriptionContainerFrame.origin.x = CGRectGetMinX(self.bounds);
  109. descriptionContainerFrame.origin.y = CGRectGetMaxY(self.bounds) - kDescriptionContainerHeight;
  110. self.selectedViewDescriptionContainer.frame = descriptionContainerFrame;
  111. CGRect descriptionSafeAreaContainerFrame = CGRectZero;
  112. descriptionSafeAreaContainerFrame.size.width = CGRectGetWidth(safeArea);
  113. descriptionSafeAreaContainerFrame.size.height = kDescriptionContainerHeight;
  114. descriptionSafeAreaContainerFrame.origin.x = CGRectGetMinX(safeArea);
  115. descriptionSafeAreaContainerFrame.origin.y = CGRectGetMinY(safeArea);
  116. self.selectedViewDescriptionSafeAreaContainer.frame = descriptionSafeAreaContainerFrame;
  117. // Selected View Color
  118. CGRect selectedViewColorFrame = CGRectZero;
  119. selectedViewColorFrame.size.width = kSelectedViewColorDiameter;
  120. selectedViewColorFrame.size.height = kSelectedViewColorDiameter;
  121. selectedViewColorFrame.origin.x = kHorizontalPadding;
  122. selectedViewColorFrame.origin.y = FLEXFloor((kDescriptionContainerHeight - kSelectedViewColorDiameter) / 2.0);
  123. self.selectedViewColorIndicator.frame = selectedViewColorFrame;
  124. #if !TARGET_OS_TV
  125. self.selectedViewColorIndicator.layer.cornerRadius = ceil(selectedViewColorFrame.size.height / 2.0);
  126. #endif
  127. // Selected View Description
  128. CGRect descriptionLabelFrame = CGRectZero;
  129. CGFloat descriptionOriginX = CGRectGetMaxX(selectedViewColorFrame) + kHorizontalPadding;
  130. descriptionLabelFrame.size.height = kDescriptionLabelHeight;
  131. descriptionLabelFrame.origin.x = descriptionOriginX;
  132. descriptionLabelFrame.origin.y = kDescriptionVerticalPadding;
  133. descriptionLabelFrame.size.width = CGRectGetMaxX(self.selectedViewDescriptionContainer.bounds) - kHorizontalPadding - descriptionOriginX;
  134. self.selectedViewDescriptionLabel.frame = descriptionLabelFrame;
  135. }
  136. #pragma mark - Setter Overrides
  137. - (void)setToolbarItems:(NSArray<FLEXExplorerToolbarItem *> *)toolbarItems {
  138. if (_toolbarItems == toolbarItems) {
  139. return;
  140. }
  141. // Remove old toolbar items, if any
  142. for (FLEXExplorerToolbarItem *item in _toolbarItems) {
  143. [item.currentItem removeFromSuperview];
  144. }
  145. // Trim to 5 items if necessary
  146. if (toolbarItems.count > 5) {
  147. toolbarItems = [toolbarItems subarrayWithRange:NSMakeRange(0, 5)];
  148. }
  149. for (FLEXExplorerToolbarItem *item in toolbarItems) {
  150. [self addSubview:item.currentItem];
  151. }
  152. _toolbarItems = toolbarItems.copy;
  153. // Lay out new items
  154. [self setNeedsLayout];
  155. [self layoutIfNeeded];
  156. }
  157. - (void)setSelectedViewOverlayColor:(UIColor *)selectedViewOverlayColor {
  158. if (![_selectedViewOverlayColor isEqual:selectedViewOverlayColor]) {
  159. _selectedViewOverlayColor = selectedViewOverlayColor;
  160. self.selectedViewColorIndicator.backgroundColor = selectedViewOverlayColor;
  161. }
  162. }
  163. - (void)setSelectedViewDescription:(NSString *)selectedViewDescription {
  164. if (![_selectedViewDescription isEqual:selectedViewDescription]) {
  165. _selectedViewDescription = selectedViewDescription;
  166. self.selectedViewDescriptionLabel.text = selectedViewDescription;
  167. BOOL showDescription = selectedViewDescription.length > 0;
  168. self.selectedViewDescriptionContainer.hidden = !showDescription;
  169. }
  170. }
  171. #pragma mark - Sizing Convenience Methods
  172. + (UIFont *)descriptionLabelFont {
  173. return [UIFont systemFontOfSize:12.0];
  174. }
  175. + (CGFloat)toolbarItemHeight {
  176. #if TARGET_OS_TV
  177. return 68.0;
  178. #endif
  179. return 44.0;
  180. }
  181. + (CGFloat)dragHandleWidth {
  182. return FLEXResources.dragHandle.size.width;
  183. }
  184. + (CGFloat)descriptionLabelHeight {
  185. return ceil([[self descriptionLabelFont] lineHeight]);
  186. }
  187. + (CGFloat)descriptionVerticalPadding {
  188. return 2.0;
  189. }
  190. + (CGFloat)descriptionContainerHeight {
  191. return [self descriptionVerticalPadding] * 2.0 + [self descriptionLabelHeight];
  192. }
  193. + (CGFloat)selectedViewColorIndicatorDiameter {
  194. return ceil([self descriptionLabelHeight] / 2.0);
  195. }
  196. + (CGFloat)horizontalPadding {
  197. return 11.0;
  198. }
  199. - (CGSize)sizeThatFits:(CGSize)size {
  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. CGRect safeArea = self.bounds;
  207. if (@available(iOS 11.0, *)) {
  208. safeArea = UIEdgeInsetsInsetRect(self.bounds, self.safeAreaInsets);
  209. }
  210. return safeArea;
  211. }
  212. @end