most_visited_tile_view.mm 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. // Copyright 2017 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. #import "ios/chrome/content_widget_extension/most_visited_tile_view.h"
  5. #import <NotificationCenter/NotificationCenter.h>
  6. #include "base/check.h"
  7. #import "ios/chrome/common/ui/favicon/favicon_view.h"
  8. #import "ios/chrome/common/ui/util/constraints_ui_util.h"
  9. #if !defined(__has_feature) || !__has_feature(objc_arc)
  10. #error "This file requires ARC support."
  11. #endif
  12. namespace {
  13. const NSInteger kLabelNumLines = 2;
  14. const CGFloat kFaviconSize = 48;
  15. const CGFloat kSpaceFaviconTitle = 8;
  16. // Width of a tile.
  17. const CGFloat kTileWidth = 73;
  18. }
  19. @implementation MostVisitedTileView
  20. @synthesize titleLabel = _titleLabel;
  21. #pragma mark - Public
  22. + (CGFloat)tileWidth {
  23. return kTileWidth;
  24. }
  25. - (instancetype)init {
  26. self = [super initWithFrame:CGRectZero];
  27. if (self) {
  28. UIVibrancyEffect* labelEffect = nil;
  29. labelEffect = [UIVibrancyEffect
  30. widgetEffectForVibrancyStyle:UIVibrancyEffectStyleSecondaryLabel];
  31. DCHECK(labelEffect);
  32. UIVisualEffectView* titleLabelEffectView =
  33. [[UIVisualEffectView alloc] initWithEffect:labelEffect];
  34. titleLabelEffectView.translatesAutoresizingMaskIntoConstraints = NO;
  35. _titleLabel = [[UILabel alloc] initWithFrame:CGRectZero];
  36. _titleLabel.font =
  37. [UIFont preferredFontForTextStyle:UIFontTextStyleCaption2];
  38. _titleLabel.textAlignment = NSTextAlignmentCenter;
  39. _titleLabel.isAccessibilityElement = NO;
  40. _titleLabel.numberOfLines = kLabelNumLines;
  41. _titleLabel.translatesAutoresizingMaskIntoConstraints = NO;
  42. [titleLabelEffectView.contentView addSubview:_titleLabel];
  43. AddSameConstraints(titleLabelEffectView, _titleLabel);
  44. _faviconView = [[FaviconView alloc] init];
  45. _faviconView.isAccessibilityElement = NO;
  46. _faviconView.font =
  47. [UIFont preferredFontForTextStyle:UIFontTextStyleHeadline];
  48. _faviconView.translatesAutoresizingMaskIntoConstraints = NO;
  49. UIStackView* stack = [[UIStackView alloc]
  50. initWithArrangedSubviews:@[ _faviconView, titleLabelEffectView ]];
  51. stack.axis = UILayoutConstraintAxisVertical;
  52. stack.spacing = kSpaceFaviconTitle;
  53. stack.alignment = UIStackViewAlignmentCenter;
  54. stack.translatesAutoresizingMaskIntoConstraints = NO;
  55. stack.isAccessibilityElement = NO;
  56. stack.userInteractionEnabled = NO;
  57. [self addSubview:stack];
  58. AddSameConstraints(self, stack);
  59. [NSLayoutConstraint activateConstraints:@[
  60. [stack.widthAnchor constraintEqualToConstant:kTileWidth],
  61. [_faviconView.widthAnchor constraintEqualToConstant:kFaviconSize],
  62. [_faviconView.heightAnchor constraintEqualToConstant:kFaviconSize],
  63. ]];
  64. self.translatesAutoresizingMaskIntoConstraints = NO;
  65. self.highlightableViews = @[ _faviconView, _titleLabel ];
  66. }
  67. return self;
  68. }
  69. @end