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/bars/bookmark_editing_bar.h" |
| 6 |
| 7 #include "base/logging.h" |
| 8 #include "base/mac/objc_property_releaser.h" |
| 9 #include "base/mac/scoped_nsobject.h" |
| 10 #include "base/strings/sys_string_conversions.h" |
| 11 #import "ios/chrome/browser/ui/bookmarks/bookmark_extended_button.h" |
| 12 #import "ios/chrome/browser/ui/bookmarks/bookmark_utils_ios.h" |
| 13 #import "ios/chrome/browser/ui/rtl_geometry.h" |
| 14 #include "ios/chrome/grit/ios_strings.h" |
| 15 #import "ios/third_party/material_components_ios/src/components/Typography/src/M
aterialTypography.h" |
| 16 #include "ui/base/l10n/l10n_util.h" |
| 17 |
| 18 namespace { |
| 19 // The distance between buttons. |
| 20 CGFloat kInterButtonMargin = 24; |
| 21 } // namespace |
| 22 |
| 23 @interface BookmarkEditingBar () { |
| 24 base::mac::ObjCPropertyReleaser _propertyReleaser_BookmarkEditingBar; |
| 25 } |
| 26 @property(nonatomic, retain) BookmarkExtendedButton* cancelButton; |
| 27 // This label is slightly left off center, and reflects the number of bookmarks |
| 28 // selected for editing. |
| 29 @property(nonatomic, retain) UILabel* countLabel; |
| 30 @property(nonatomic, retain) BookmarkExtendedButton* deleteButton; |
| 31 @property(nonatomic, retain) UIView* dropShadow; |
| 32 @property(nonatomic, retain) BookmarkExtendedButton* editButton; |
| 33 @property(nonatomic, retain) BookmarkExtendedButton* moveButton; |
| 34 @end |
| 35 |
| 36 @implementation BookmarkEditingBar |
| 37 @synthesize cancelButton = _cancelButton; |
| 38 @synthesize countLabel = _countLabel; |
| 39 @synthesize deleteButton = _deleteButton; |
| 40 @synthesize dropShadow = _dropShadow; |
| 41 @synthesize editButton = _editButton; |
| 42 @synthesize moveButton = _moveButton; |
| 43 |
| 44 - (id)initWithFrame:(CGRect)outerFrame { |
| 45 self = [super initWithFrame:outerFrame]; |
| 46 if (self) { |
| 47 _propertyReleaser_BookmarkEditingBar.Init(self, [BookmarkEditingBar class]); |
| 48 self.backgroundColor = bookmark_utils_ios::blueColor(); |
| 49 |
| 50 CGRect bounds = self.contentView.bounds; |
| 51 |
| 52 // Add the cancel button to the leading side of the bar. |
| 53 CGFloat cancelButtonWidth = 24; |
| 54 CGFloat cancelButtonHeight = 24; |
| 55 CGFloat cancelButtonY = |
| 56 floor((bounds.size.height - cancelButtonHeight) / 2); |
| 57 CGFloat cancelButtonX = cancelButtonY; |
| 58 base::scoped_nsobject<BookmarkExtendedButton> button( |
| 59 [[BookmarkExtendedButton alloc] |
| 60 initWithFrame:LayoutRectGetRect(LayoutRectMake( |
| 61 cancelButtonX, |
| 62 CGRectGetWidth(self.contentView.bounds), |
| 63 cancelButtonY, cancelButtonWidth, |
| 64 cancelButtonHeight))]); |
| 65 self.cancelButton = button; |
| 66 self.cancelButton.extendedEdges = UIEdgeInsetsMakeDirected( |
| 67 cancelButtonY, cancelButtonX, cancelButtonY, cancelButtonX); |
| 68 self.cancelButton.autoresizingMask = |
| 69 UIViewAutoresizingFlexibleTrailingMargin(); |
| 70 self.cancelButton.backgroundColor = [UIColor clearColor]; |
| 71 UIImage* cancelImage = [UIImage imageNamed:@"bookmark_white_close"]; |
| 72 [self.cancelButton setBackgroundImage:cancelImage |
| 73 forState:UIControlStateNormal]; |
| 74 self.cancelButton.accessibilityLabel = |
| 75 l10n_util::GetNSString(IDS_IOS_BOOKMARK_NEW_CANCEL_BUTTON_LABEL); |
| 76 self.cancelButton.accessibilityIdentifier = @"Exit Edit Mode"; |
| 77 [self.contentView addSubview:self.cancelButton]; |
| 78 |
| 79 // Add the count label to the right of the cancel button. |
| 80 CGFloat labelX = bookmark_utils_ios::titleMargin; |
| 81 CGFloat labelY = 0; |
| 82 base::scoped_nsobject<UILabel> label([[UILabel alloc] |
| 83 initWithFrame:LayoutRectGetRect(LayoutRectMake( |
| 84 labelX, CGRectGetWidth(self.contentView.bounds), |
| 85 labelY, 150, CGRectGetHeight(bounds)))]); |
| 86 self.countLabel = label; |
| 87 self.countLabel.textColor = [UIColor whiteColor]; |
| 88 self.countLabel.autoresizingMask = |
| 89 UIViewAutoresizingFlexibleTrailingMargin(); |
| 90 self.countLabel.font = [MDCTypography titleFont]; |
| 91 self.countLabel.backgroundColor = [UIColor clearColor]; |
| 92 [self.contentView addSubview:self.countLabel]; |
| 93 |
| 94 // Add the edit button to the right side of the bar. |
| 95 CGFloat editButtonWidth = 24; |
| 96 CGFloat editButtonHeight = 24; |
| 97 // The margin is the same to the top, the bottom, and the right. |
| 98 CGFloat editButtonY = floor((bounds.size.height - editButtonHeight) / 2); |
| 99 CGFloat editButtonRightMargin = editButtonY; |
| 100 CGFloat editButtonX = |
| 101 bounds.size.width - editButtonRightMargin - editButtonWidth; |
| 102 button.reset([[BookmarkExtendedButton alloc] |
| 103 initWithFrame:LayoutRectGetRect(LayoutRectMake( |
| 104 editButtonX, CGRectGetWidth(self.contentView.bounds), |
| 105 editButtonY, editButtonWidth, editButtonHeight))]); |
| 106 self.editButton = button; |
| 107 self.editButton.extendedEdges = |
| 108 UIEdgeInsetsMakeDirected(editButtonY, kInterButtonMargin / 2.0, |
| 109 editButtonY, editButtonRightMargin); |
| 110 self.editButton.autoresizingMask = |
| 111 UIViewAutoresizingFlexibleLeadingMargin(); |
| 112 self.editButton.backgroundColor = [UIColor clearColor]; |
| 113 UIImage* editImage = [UIImage imageNamed:@"bookmark_white_edit"]; |
| 114 [self.editButton setBackgroundImage:editImage |
| 115 forState:UIControlStateNormal]; |
| 116 self.editButton.accessibilityLabel = |
| 117 l10n_util::GetNSString(IDS_IOS_BOOKMARK_NEW_EDIT_BUTTON_LABEL); |
| 118 self.editButton.accessibilityIdentifier = @"Edit_editing_bar"; |
| 119 [self.contentView addSubview:self.editButton]; |
| 120 |
| 121 // Add the move button to the same position as the edit button. |
| 122 button.reset( |
| 123 [[BookmarkExtendedButton alloc] initWithFrame:self.editButton.frame]); |
| 124 self.moveButton = button; |
| 125 self.moveButton.extendedEdges = |
| 126 UIEdgeInsetsMakeDirected(editButtonY, kInterButtonMargin / 2.0, |
| 127 editButtonY, editButtonRightMargin); |
| 128 self.moveButton.autoresizingMask = |
| 129 UIViewAutoresizingFlexibleLeadingMargin(); |
| 130 self.moveButton.backgroundColor = [UIColor clearColor]; |
| 131 UIImage* moveImage = [UIImage imageNamed:@"bookmark_white_move"]; |
| 132 [self.moveButton setBackgroundImage:moveImage |
| 133 forState:UIControlStateNormal]; |
| 134 self.moveButton.accessibilityLabel = |
| 135 l10n_util::GetNSString(IDS_IOS_BOOKMARK_NEW_MOVE_BUTTON_LABEL); |
| 136 self.moveButton.accessibilityIdentifier = @"Move"; |
| 137 [self.contentView addSubview:self.moveButton]; |
| 138 |
| 139 // Add the delete button to the left of the edit button. |
| 140 CGFloat deleteButtonWidth = 24; |
| 141 CGFloat deleteButtonHeight = 24; |
| 142 CGFloat deleteButtonY = |
| 143 floor((bounds.size.height - deleteButtonHeight) / 2); |
| 144 CGFloat deleteButtonX = |
| 145 CGRectGetLeadingLayoutOffsetInBoundingRect(self.editButton.frame, |
| 146 self.contentView.bounds) - |
| 147 kInterButtonMargin - deleteButtonWidth; |
| 148 button.reset([[BookmarkExtendedButton alloc] |
| 149 initWithFrame:LayoutRectGetRect(LayoutRectMake( |
| 150 deleteButtonX, |
| 151 CGRectGetWidth(self.contentView.bounds), |
| 152 deleteButtonY, deleteButtonWidth, |
| 153 deleteButtonHeight))]); |
| 154 self.deleteButton = button; |
| 155 self.deleteButton.extendedEdges = UIEdgeInsetsMakeDirected( |
| 156 deleteButtonY, deleteButtonY, deleteButtonY, kInterButtonMargin / 2.0); |
| 157 self.deleteButton.autoresizingMask = |
| 158 UIViewAutoresizingFlexibleLeadingMargin(); |
| 159 self.deleteButton.backgroundColor = [UIColor clearColor]; |
| 160 UIImage* deleteImage = [UIImage imageNamed:@"bookmark_white_delete"]; |
| 161 [self.deleteButton setBackgroundImage:deleteImage |
| 162 forState:UIControlStateNormal]; |
| 163 self.deleteButton.accessibilityLabel = |
| 164 l10n_util::GetNSString(IDS_IOS_BOOKMARK_NEW_DELETE_BUTTON_LABEL); |
| 165 self.deleteButton.accessibilityIdentifier = @"Delete"; |
| 166 [self.contentView addSubview:self.deleteButton]; |
| 167 |
| 168 UIView* shadow = |
| 169 bookmark_utils_ios::dropShadowWithWidth(self.bounds.size.width); |
| 170 shadow.frame = |
| 171 CGRectMake(0, CGRectGetHeight(self.bounds), |
| 172 CGRectGetWidth(shadow.frame), CGRectGetHeight(shadow.frame)); |
| 173 [self addSubview:shadow]; |
| 174 self.dropShadow = shadow; |
| 175 |
| 176 [self updateUIWithBookmarkCount:0 folderCount:0]; |
| 177 } |
| 178 return self; |
| 179 } |
| 180 |
| 181 - (void)setCancelTarget:(id)target action:(SEL)action { |
| 182 [self.cancelButton addTarget:target |
| 183 action:action |
| 184 forControlEvents:UIControlEventTouchUpInside]; |
| 185 } |
| 186 |
| 187 - (void)setEditTarget:(id)target action:(SEL)action { |
| 188 [self.editButton addTarget:target |
| 189 action:action |
| 190 forControlEvents:UIControlEventTouchUpInside]; |
| 191 } |
| 192 |
| 193 - (void)setMoveTarget:(id)target action:(SEL)action { |
| 194 [self.moveButton addTarget:target |
| 195 action:action |
| 196 forControlEvents:UIControlEventTouchUpInside]; |
| 197 } |
| 198 |
| 199 - (void)setDeleteTarget:(id)target action:(SEL)action { |
| 200 [self.deleteButton addTarget:target |
| 201 action:action |
| 202 forControlEvents:UIControlEventTouchUpInside]; |
| 203 } |
| 204 |
| 205 - (void)updateUIWithBookmarkCount:(int)bookmarkCount |
| 206 folderCount:(int)folderCount { |
| 207 DCHECK_GE(bookmarkCount, 0); |
| 208 DCHECK_GE(folderCount, 0); |
| 209 |
| 210 int editingCount = bookmarkCount + folderCount; |
| 211 |
| 212 if (editingCount == 0) { |
| 213 self.countLabel.text = |
| 214 l10n_util::GetNSString(IDS_IOS_BOOKMARK_NEW_ZERO_ITEM_LABEL); |
| 215 } else if (editingCount == 1) { |
| 216 self.countLabel.text = |
| 217 l10n_util::GetNSString(IDS_IOS_BOOKMARK_NEW_ONE_ITEM_LABEL); |
| 218 } else { |
| 219 NSString* editingCountString = |
| 220 [NSString stringWithFormat:@"%d", editingCount]; |
| 221 self.countLabel.text = |
| 222 l10n_util::GetNSStringF(IDS_IOS_BOOKMARK_NEW_MANY_ITEM_LABEL, |
| 223 base::SysNSStringToUTF16(editingCountString)); |
| 224 } |
| 225 |
| 226 // Hide delete, move, edit buttons. |
| 227 if (editingCount == 0) { |
| 228 self.editButton.hidden = YES; |
| 229 self.moveButton.hidden = YES; |
| 230 self.deleteButton.hidden = YES; |
| 231 return; |
| 232 } |
| 233 |
| 234 // Hide move button. Show delete/edit buttons. |
| 235 if (editingCount == 1 && folderCount == 0) { |
| 236 self.editButton.hidden = NO; |
| 237 self.moveButton.hidden = YES; |
| 238 self.deleteButton.hidden = NO; |
| 239 return; |
| 240 } |
| 241 |
| 242 // Hide edit button. Show delete/move buttons. |
| 243 self.editButton.hidden = YES; |
| 244 self.moveButton.hidden = NO; |
| 245 self.deleteButton.hidden = NO; |
| 246 } |
| 247 |
| 248 - (void)showShadow:(BOOL)show { |
| 249 self.dropShadow.hidden = !show; |
| 250 } |
| 251 |
| 252 @end |
OLD | NEW |