OLD | NEW |
(Empty) | |
| 1 // Copyright 2016 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/today_extension/notification_center_url_button.h" |
| 6 |
| 7 #import "base/mac/foundation_util.h" |
| 8 #import "base/mac/scoped_block.h" |
| 9 #import "base/mac/scoped_nsobject.h" |
| 10 #include "base/strings/sys_string_conversions.h" |
| 11 #import "ios/chrome/common/physical_web/physical_web_device.h" |
| 12 #import "ios/chrome/today_extension/ui_util.h" |
| 13 #include "net/base/escape.h" |
| 14 #include "ui/base/l10n/l10n_util.h" |
| 15 |
| 16 namespace { |
| 17 const CGFloat kSubtitleFontSize = 14; |
| 18 } |
| 19 |
| 20 @implementation NotificationCenterURLButton { |
| 21 base::mac::ScopedBlock<URLActionBlock> _openURLBlock; |
| 22 base::scoped_nsobject<NSString> _url; |
| 23 base::scoped_nsobject<UILabel> _subtitleLabel; |
| 24 base::scoped_nsobject<UIView> _bottomSeparator; |
| 25 } |
| 26 |
| 27 - (void)openURL:(id)sender { |
| 28 _openURLBlock.get()(_url); |
| 29 } |
| 30 |
| 31 - (NSString*)unescapeURLString:(NSString*)urlString { |
| 32 std::string escapedURL = base::SysNSStringToUTF8(urlString); |
| 33 std::string unescapedURL = |
| 34 net::UnescapeURLComponent(escapedURL, net::UnescapeRule::SPACES); |
| 35 NSString* unescapedURLString = base::SysUTF8ToNSString(unescapedURL); |
| 36 return unescapedURLString; |
| 37 } |
| 38 |
| 39 // Create a button with contextual URL layout. |
| 40 - (instancetype)initWithTitle:(NSString*)title |
| 41 url:(NSString*)url |
| 42 icon:(NSString*)icon |
| 43 leftInset:(CGFloat)leftInset |
| 44 block:(URLActionBlock)block { |
| 45 self = [super initWithTitle:title |
| 46 icon:nil |
| 47 target:nil |
| 48 action:@selector(openURL:) |
| 49 backgroundColor:[UIColor clearColor] |
| 50 inkColor:ui_util::InkColor() |
| 51 titleColor:ui_util::TitleColor()]; |
| 52 if (self) { |
| 53 _openURLBlock.reset(block, base::scoped_policy::RETAIN); |
| 54 [self setContentVerticalAlignment:UIControlContentVerticalAlignmentTop]; |
| 55 |
| 56 _url.reset([url copy]); |
| 57 |
| 58 UIImage* iconImage = [[UIImage imageNamed:icon] |
| 59 imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate]; |
| 60 [self setImage:iconImage forState:UIControlStateNormal]; |
| 61 [[self imageView] setTintColor:ui_util::NormalTintColor()]; |
| 62 |
| 63 CGFloat chromeIconXOffset = leftInset + ui_util::ChromeIconOffset(); |
| 64 |
| 65 _subtitleLabel.reset([[UILabel alloc] initWithFrame:CGRectZero]); |
| 66 [self setTranslatesAutoresizingMaskIntoConstraints:NO]; |
| 67 [_subtitleLabel setTranslatesAutoresizingMaskIntoConstraints:NO]; |
| 68 [_subtitleLabel setText:url]; |
| 69 [self addSubview:_subtitleLabel]; |
| 70 |
| 71 [self setTitle:title url:url]; |
| 72 |
| 73 UIFont* subtitleFont = |
| 74 [UIFont fontWithName:@"Helvetica" size:kSubtitleFontSize]; |
| 75 [_subtitleLabel setFont:subtitleFont]; |
| 76 [_subtitleLabel setTextColor:ui_util::TextColor()]; |
| 77 |
| 78 _bottomSeparator.reset([[UIView alloc] initWithFrame:CGRectZero]); |
| 79 [_bottomSeparator setBackgroundColor:ui_util::BackgroundColor()]; |
| 80 [_bottomSeparator setHidden:YES]; |
| 81 [[self contentView] addSubview:_bottomSeparator]; |
| 82 |
| 83 [self setContentEdgeInsets:UIEdgeInsetsMake( |
| 84 ui_util::kSecondLineVerticalPadding, |
| 85 chromeIconXOffset, 0, chromeIconXOffset)]; |
| 86 if ([UIView userInterfaceLayoutDirectionForSemanticContentAttribute: |
| 87 self.semanticContentAttribute] == |
| 88 UIUserInterfaceLayoutDirectionRightToLeft) { |
| 89 // RTL |
| 90 [[self titleLabel] setTextAlignment:NSTextAlignmentNatural]; |
| 91 [_subtitleLabel setTextAlignment:NSTextAlignmentNatural]; |
| 92 [self setTitleEdgeInsets:UIEdgeInsetsMake(0, 0, 0, |
| 93 ui_util::ChromeTextOffset())]; |
| 94 [self setContentHorizontalAlignment: |
| 95 UIControlContentHorizontalAlignmentRight]; |
| 96 } else { |
| 97 // LTR |
| 98 [[self titleLabel] setTextAlignment:NSTextAlignmentNatural]; |
| 99 [_subtitleLabel setTextAlignment:NSTextAlignmentNatural]; |
| 100 [self setTitleEdgeInsets:UIEdgeInsetsMake(0, ui_util::ChromeTextOffset(), |
| 101 0, 0)]; |
| 102 [self setContentHorizontalAlignment: |
| 103 UIControlContentHorizontalAlignmentLeft]; |
| 104 } |
| 105 |
| 106 [self setIsAccessibilityElement:YES]; |
| 107 [self setAccessibilityTraits:UIAccessibilityTraitLink]; |
| 108 } |
| 109 |
| 110 return self; |
| 111 } |
| 112 |
| 113 - (void)layoutSubviews { |
| 114 [super layoutSubviews]; |
| 115 CGRect frame = [_subtitleLabel frame]; |
| 116 CGRect titleFrame = [[self titleLabel] frame]; |
| 117 CGRect separatorFrame = CGRectZero; |
| 118 |
| 119 frame.origin.y = CGRectGetMaxY(titleFrame); |
| 120 frame.size.height = 18; |
| 121 |
| 122 separatorFrame.size.height = 1.0 / [[UIScreen mainScreen] scale]; |
| 123 separatorFrame.origin.y = |
| 124 [self frame].size.height - separatorFrame.size.height; |
| 125 |
| 126 if ([UIView userInterfaceLayoutDirectionForSemanticContentAttribute: |
| 127 self.semanticContentAttribute] == |
| 128 UIUserInterfaceLayoutDirectionRightToLeft) { |
| 129 // RTL |
| 130 // Align on right |
| 131 frame.origin.x = 10; |
| 132 frame.size.width = CGRectGetMaxX(titleFrame) - frame.origin.x; |
| 133 separatorFrame.origin.x = 0; |
| 134 separatorFrame.size.width = CGRectGetMaxX([self imageView].frame); |
| 135 } else { |
| 136 // LTR |
| 137 // Align on left |
| 138 frame.origin.x = titleFrame.origin.x; |
| 139 frame.size.width = self.frame.size.width - frame.origin.x - 10; |
| 140 separatorFrame.origin.x = [self imageView].frame.origin.x; |
| 141 separatorFrame.size.width = self.frame.size.width - separatorFrame.origin.x; |
| 142 } |
| 143 |
| 144 [_subtitleLabel setFrame:frame]; |
| 145 [_bottomSeparator setFrame:separatorFrame]; |
| 146 } |
| 147 |
| 148 - (void)setTitle:(NSString*)title url:(NSString*)url { |
| 149 _url.reset([url copy]); |
| 150 NSString* subtitle = nil; |
| 151 if (![title length]) { |
| 152 title = [self unescapeURLString:url]; |
| 153 } else { |
| 154 subtitle = [self unescapeURLString:url]; |
| 155 } |
| 156 [super setTitle:title forState:UIControlStateNormal]; |
| 157 if (subtitle) { |
| 158 [_subtitleLabel setText:subtitle]; |
| 159 } else { |
| 160 [_subtitleLabel setText:@""]; |
| 161 } |
| 162 } |
| 163 |
| 164 - (void)setHighlighted:(BOOL)highlighted { |
| 165 [super setHighlighted:highlighted]; |
| 166 if (highlighted) { |
| 167 [_subtitleLabel setTextColor:[UIColor colorWithWhite:1 alpha:1]]; |
| 168 [[self imageView] setTintColor:ui_util::ActiveTintColor()]; |
| 169 } else { |
| 170 [_subtitleLabel setTextColor:ui_util::TextColor()]; |
| 171 [[self imageView] setTintColor:ui_util::NormalTintColor()]; |
| 172 } |
| 173 } |
| 174 |
| 175 - (void)setSeparatorVisible:(BOOL)visible { |
| 176 [_bottomSeparator setHidden:!visible]; |
| 177 } |
| 178 |
| 179 @end |
OLD | NEW |