OLD | NEW |
(Empty) | |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #import "ios/chrome/browser/ui/bookmarks/bookmark_menu_cell.h" |
| 6 |
| 7 #include "base/mac/objc_property_releaser.h" |
| 8 #include "base/mac/scoped_nsobject.h" |
| 9 #import "ios/chrome/browser/ui/bookmarks/bookmark_menu_item.h" |
| 10 #import "ios/chrome/browser/ui/bookmarks/bookmark_utils_ios.h" |
| 11 #include "ios/chrome/browser/ui/rtl_geometry.h" |
| 12 #include "ios/chrome/browser/ui/ui_util.h" |
| 13 #import "ios/chrome/browser/ui/uikit_ui_util.h" |
| 14 #import "ios/third_party/material_components_ios/src/components/Ink/src/Material
Ink.h" |
| 15 #import "ios/third_party/material_components_ios/src/components/Palettes/src/Mat
erialPalettes.h" |
| 16 #import "ios/third_party/material_components_ios/src/components/Typography/src/M
aterialTypography.h" |
| 17 |
| 18 namespace { |
| 19 CGFloat kTrailingMargin = 16.0; |
| 20 } // namespace |
| 21 |
| 22 @interface BookmarkMenuCell () { |
| 23 base::mac::ObjCPropertyReleaser _propertyReleaser_BookmarkMenuCell; |
| 24 } |
| 25 // Set to YES if the cell can be selected. |
| 26 @property(nonatomic, assign) BOOL isSelectable; |
| 27 // Set to YES if the cell should animate on selection. |
| 28 @property(nonatomic, assign) BOOL shouldAnimateHighlight; |
| 29 // Icon view. |
| 30 @property(nonatomic, assign) UIImageView* iconView; |
| 31 // Text label. |
| 32 @property(nonatomic, assign) UILabel* itemLabel; |
| 33 // Separator view. Displayed at 1 pixel height on top for some cells. |
| 34 @property(nonatomic, assign) UIView* separatorView; |
| 35 @end |
| 36 |
| 37 @implementation BookmarkMenuCell |
| 38 @synthesize isSelectable = _isSelectable; |
| 39 @synthesize shouldAnimateHighlight = _shouldAnimateHighlight; |
| 40 @synthesize iconView = _iconView; |
| 41 @synthesize itemLabel = _itemLabel; |
| 42 @synthesize separatorView = _separatorView; |
| 43 @synthesize inkView = _inkView; |
| 44 |
| 45 - (instancetype)initWithStyle:(UITableViewCellStyle)style |
| 46 reuseIdentifier:(NSString*)reuseIdentifier { |
| 47 self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]; |
| 48 if (self) { |
| 49 _propertyReleaser_BookmarkMenuCell.Init(self, [BookmarkMenuCell class]); |
| 50 |
| 51 // Create icon view. |
| 52 base::scoped_nsobject<UIImageView> iconView([[UIImageView alloc] init]); |
| 53 _iconView = iconView; |
| 54 |
| 55 // Create item label. |
| 56 base::scoped_nsobject<UILabel> itemLabel([[UILabel alloc] init]); |
| 57 _itemLabel = itemLabel; |
| 58 |
| 59 self.contentView.layoutMargins = UIEdgeInsetsMakeDirected( |
| 60 0, bookmark_utils_ios::menuMargin, 0, kTrailingMargin); |
| 61 |
| 62 // Create stack view. |
| 63 UIStackView* contentStack = [[[UIStackView alloc] |
| 64 initWithArrangedSubviews:@[ iconView, itemLabel ]] autorelease]; |
| 65 [self.contentView addSubview:contentStack]; |
| 66 |
| 67 contentStack.spacing = bookmark_utils_ios::titleToIconDistance; |
| 68 contentStack.alignment = UIStackViewAlignmentCenter; |
| 69 |
| 70 // Configure stack view layout. |
| 71 contentStack.translatesAutoresizingMaskIntoConstraints = NO; |
| 72 UIView* contentView = self.contentView; |
| 73 |
| 74 UILayoutGuide* marginsGuide = contentView.layoutMarginsGuide; |
| 75 [NSLayoutConstraint activateConstraints:@[ |
| 76 [contentStack.centerYAnchor |
| 77 constraintEqualToAnchor:contentView.centerYAnchor], |
| 78 [contentStack.leadingAnchor |
| 79 constraintEqualToAnchor:marginsGuide.leadingAnchor], |
| 80 [contentStack.trailingAnchor |
| 81 constraintLessThanOrEqualToAnchor:marginsGuide.trailingAnchor] |
| 82 ]]; |
| 83 |
| 84 // Create the ink view. |
| 85 _inkView = [[MDCInkView alloc] initWithFrame:self.bounds]; |
| 86 [self addSubview:_inkView]; |
| 87 |
| 88 // Add separator view. |
| 89 base::scoped_nsobject<UIView> separatorView( |
| 90 [[UIView alloc] initWithFrame:CGRectZero]); |
| 91 [self.contentView addSubview:separatorView]; |
| 92 _separatorView = separatorView; |
| 93 |
| 94 CGFloat pixelSize = 1 / [[UIScreen mainScreen] scale]; |
| 95 [NSLayoutConstraint activateConstraints:@[ |
| 96 [_separatorView.leadingAnchor |
| 97 constraintEqualToAnchor:contentView.leadingAnchor], |
| 98 [_separatorView.trailingAnchor |
| 99 constraintEqualToAnchor:contentView.trailingAnchor], |
| 100 [_separatorView.heightAnchor constraintEqualToConstant:pixelSize], |
| 101 [_separatorView.topAnchor constraintEqualToAnchor:contentView.topAnchor], |
| 102 ]]; |
| 103 _separatorView.translatesAutoresizingMaskIntoConstraints = NO; |
| 104 |
| 105 // Configure background colors. |
| 106 _separatorView.backgroundColor = [UIColor colorWithWhite:0.0 alpha:.12]; |
| 107 self.backgroundColor = [UIColor clearColor]; |
| 108 } |
| 109 return self; |
| 110 } |
| 111 |
| 112 - (void)prepareForReuse { |
| 113 self.shouldAnimateHighlight = NO; |
| 114 self.isSelectable = NO; |
| 115 [self.inkView cancelAllAnimationsAnimated:NO]; |
| 116 [super prepareForReuse]; |
| 117 } |
| 118 |
| 119 - (void)setSelected:(BOOL)selected animated:(BOOL)animated { |
| 120 [super setSelected:selected animated:animated]; |
| 121 if (IsIPadIdiom()) |
| 122 return; // No background on selection on tablets. |
| 123 if (!self.isSelectable) |
| 124 return; |
| 125 if (selected) |
| 126 self.backgroundColor = [[MDCPalette greyPalette] tint100]; |
| 127 else |
| 128 self.backgroundColor = [UIColor clearColor]; |
| 129 } |
| 130 |
| 131 + (BOOL)requiresConstraintBasedLayout { |
| 132 return YES; |
| 133 } |
| 134 |
| 135 - (void)updateWithBookmarkMenuItem:(BookmarkMenuItem*)item |
| 136 primary:(BOOL)primary { |
| 137 self.itemLabel.text = [item titleForMenu]; |
| 138 self.itemLabel.font = [MDCTypography body2Font]; |
| 139 UIColor* textColor = nil; |
| 140 if (primary) |
| 141 textColor = [UIColor colorWithRed:66 / 255.0 |
| 142 green:129 / 255.0 |
| 143 blue:224 / 255.0 |
| 144 alpha:1]; |
| 145 else if (item.type == bookmarks::MenuItemSectionHeader) |
| 146 textColor = [UIColor colorWithWhite:0.0 alpha:0.54]; |
| 147 else |
| 148 textColor = [UIColor blackColor]; |
| 149 self.itemLabel.textColor = textColor; |
| 150 if (item.type == bookmarks::MenuItemDivider) { |
| 151 self.accessibilityElementsHidden = YES; |
| 152 } else { |
| 153 self.accessibilityIdentifier = [item accessibilityIdentifier]; |
| 154 self.accessibilityLabel = [item titleForMenu]; |
| 155 self.accessibilityTraits = [item accessibilityTraits]; |
| 156 } |
| 157 if (primary) |
| 158 self.accessibilityTraits |= UIAccessibilityTraitSelected; |
| 159 else |
| 160 self.accessibilityTraits &= ~UIAccessibilityTraitSelected; |
| 161 self.iconView.image = [item imagePrimary:primary]; |
| 162 self.separatorView.hidden = !(item.type == bookmarks::MenuItemDivider); |
| 163 |
| 164 self.isSelectable = (item.type != bookmarks::MenuItemSectionHeader) && |
| 165 (item.type != bookmarks::MenuItemDivider); |
| 166 self.shouldAnimateHighlight = !primary && self.isSelectable; |
| 167 |
| 168 // If there is no icon, hide the icon image view so that stack view redraws |
| 169 // without the icon, and vice versa. |
| 170 self.iconView.hidden = (self.iconView.image == nil); |
| 171 } |
| 172 |
| 173 + (NSString*)reuseIdentifier { |
| 174 return @"BookmarkMenuCell"; |
| 175 } |
| 176 |
| 177 @end |
OLD | NEW |