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/tools_menu/tools_menu_view_item.h" |
| 6 |
| 7 #include "base/i18n/rtl.h" |
| 8 #include "base/mac/objc_property_releaser.h" |
| 9 #import "ios/third_party/material_roboto_font_loader_ios/src/src/MaterialRobotoF
ontLoader.h" |
| 10 #include "ui/base/l10n/l10n_util.h" |
| 11 |
| 12 namespace { |
| 13 const CGFloat kToolsMenuItemHorizontalMargin = 16; |
| 14 // Increase the margin for RTL so the labels don't overlap the tools icon. |
| 15 const CGFloat kToolsMenuItemHorizontalMarginRTL = 25; |
| 16 static NSString* const kMenuItemCellID = @"MenuItemCellID"; |
| 17 } |
| 18 |
| 19 @implementation ToolsMenuViewItem { |
| 20 base::mac::ObjCPropertyReleaser _propertyReleaser_ToolsMenuViewItem; |
| 21 } |
| 22 |
| 23 @synthesize accessibilityIdentifier = _accessibilityIdentifier; |
| 24 @synthesize active = _active; |
| 25 @synthesize title = _title; |
| 26 @synthesize tag = _tag; |
| 27 @synthesize tableViewCell = _tableViewCell; |
| 28 |
| 29 - (id)init { |
| 30 self = [super init]; |
| 31 if (self) { |
| 32 _propertyReleaser_ToolsMenuViewItem.Init(self, [ToolsMenuViewItem class]); |
| 33 _active = YES; |
| 34 } |
| 35 |
| 36 return self; |
| 37 } |
| 38 |
| 39 + (NSString*)cellID { |
| 40 return kMenuItemCellID; |
| 41 } |
| 42 |
| 43 + (Class)cellClass { |
| 44 return [ToolsMenuViewCell class]; |
| 45 } |
| 46 |
| 47 + (instancetype)menuItemWithTitle:(NSString*)title |
| 48 accessibilityIdentifier:(NSString*)accessibilityIdentifier |
| 49 command:(int)commandID { |
| 50 ToolsMenuViewItem* menuItem = [[[self alloc] init] autorelease]; |
| 51 [menuItem setAccessibilityLabel:title]; |
| 52 [menuItem setAccessibilityIdentifier:accessibilityIdentifier]; |
| 53 [menuItem setTag:commandID]; |
| 54 [menuItem setTitle:title]; |
| 55 |
| 56 return menuItem; |
| 57 } |
| 58 |
| 59 @end |
| 60 |
| 61 @implementation ToolsMenuViewCell { |
| 62 base::mac::ObjCPropertyReleaser _propertyReleaser_ToolsMenuViewCell; |
| 63 } |
| 64 |
| 65 @synthesize title = _title; |
| 66 @synthesize horizontalMargin = _horizontalMargin; |
| 67 |
| 68 - (instancetype)initWithCoder:(NSCoder*)aDecoder { |
| 69 self = [super initWithCoder:aDecoder]; |
| 70 if (self) |
| 71 [self commonInitialization]; |
| 72 |
| 73 return self; |
| 74 } |
| 75 |
| 76 - (instancetype)initWithFrame:(CGRect)frame { |
| 77 self = [super initWithFrame:frame]; |
| 78 if (self) |
| 79 [self commonInitialization]; |
| 80 |
| 81 return self; |
| 82 } |
| 83 |
| 84 - (void)commonInitialization { |
| 85 _propertyReleaser_ToolsMenuViewCell.Init(self, [ToolsMenuViewCell class]); |
| 86 _horizontalMargin = !base::i18n::IsRTL() ? kToolsMenuItemHorizontalMargin |
| 87 : kToolsMenuItemHorizontalMarginRTL; |
| 88 [self setBackgroundColor:[UIColor whiteColor]]; |
| 89 [self setOpaque:YES]; |
| 90 } |
| 91 |
| 92 - (void)prepareForReuse { |
| 93 [super prepareForReuse]; |
| 94 [_title setText:nil]; |
| 95 [self setAccessibilityIdentifier:nil]; |
| 96 [self setAccessibilityLabel:nil]; |
| 97 } |
| 98 |
| 99 - (void)initializeTitleView { |
| 100 _title = [[UILabel alloc] initWithFrame:self.bounds]; |
| 101 [_title setTranslatesAutoresizingMaskIntoConstraints:NO]; |
| 102 [_title setFont:[[MDFRobotoFontLoader sharedInstance] regularFontOfSize:16]]; |
| 103 [_title setBackgroundColor:[self backgroundColor]]; |
| 104 [_title setTextAlignment:(base::i18n::IsRTL() ? NSTextAlignmentRight |
| 105 : NSTextAlignmentLeft)]; |
| 106 [_title setOpaque:YES]; |
| 107 } |
| 108 |
| 109 - (void)initializeViews { |
| 110 if (_title) |
| 111 return; |
| 112 |
| 113 [self initializeTitleView]; |
| 114 |
| 115 UIView* contentView = [self contentView]; |
| 116 [contentView setBackgroundColor:[self backgroundColor]]; |
| 117 [contentView setOpaque:YES]; |
| 118 [contentView addSubview:_title]; |
| 119 |
| 120 NSDictionary* view = @{ @"title" : _title }; |
| 121 NSString* vertical = @"V:|-(0)-[title]-(0)-|"; |
| 122 NSString* horizontal = @"H:|-(margin)-[title]-(25)-|"; |
| 123 NSDictionary* metrics = @{ @"margin" : @(self.horizontalMargin) }; |
| 124 |
| 125 [contentView |
| 126 addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:vertical |
| 127 options:0 |
| 128 metrics:nil |
| 129 views:view]]; |
| 130 [contentView |
| 131 addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:horizontal |
| 132 options:0 |
| 133 metrics:metrics |
| 134 views:view]]; |
| 135 } |
| 136 |
| 137 - (void)configureForMenuItem:(ToolsMenuViewItem*)item { |
| 138 [self initializeViews]; |
| 139 |
| 140 UIAccessibilityTraits traits = [self accessibilityTraits] | |
| 141 UIAccessibilityTraitNotEnabled | |
| 142 UIAccessibilityTraitButton; |
| 143 UIColor* textColor = [UIColor lightGrayColor]; |
| 144 if ([item active]) { |
| 145 textColor = [UIColor blackColor]; |
| 146 traits &= ~UIAccessibilityTraitNotEnabled; |
| 147 } |
| 148 |
| 149 [_title setTextColor:textColor]; |
| 150 [_title setText:[item title]]; |
| 151 [self setAccessibilityIdentifier:[item accessibilityIdentifier]]; |
| 152 [self setAccessibilityLabel:[item accessibilityLabel]]; |
| 153 [self setAccessibilityTraits:traits]; |
| 154 [self setIsAccessibilityElement:YES]; |
| 155 [self setTag:[item tag]]; |
| 156 item.tableViewCell = self; |
| 157 } |
| 158 |
| 159 @end |
OLD | NEW |