host_fetching_error_view_controller.mm 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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 "remoting/ios/app/host_fetching_error_view_controller.h"
  5. #import <MaterialComponents/MDCTypography.h>
  6. #import <MaterialComponents/MaterialButtons.h>
  7. #include "remoting/base/string_resources.h"
  8. #import "remoting/ios/app/remoting_theme.h"
  9. #include "ui/base/l10n/l10n_util.h"
  10. #if !defined(__has_feature) || !__has_feature(objc_arc)
  11. #error "This file requires ARC support."
  12. #endif
  13. static const CGFloat kPadding = 20;
  14. static const CGFloat kLineSpace = 30;
  15. // The actual width will be min(kMaxWidth, screen width)
  16. static const CGFloat kMaxWidth = 500;
  17. // Adjust for the padding already existed inside the button.
  18. static const CGFloat kButtonRightPaddingAdjustment = -10;
  19. static const CGFloat kButtonBottomPaddingAdjustment = -5;
  20. @implementation HostFetchingErrorViewController {
  21. UILabel* _label;
  22. }
  23. @synthesize onRetryCallback = _onRetryCallback;
  24. - (instancetype)init {
  25. if (self = [super init]) {
  26. // Label should be created right under init because it may be accessed
  27. // before the view is loaded.
  28. _label = [[UILabel alloc] initWithFrame:CGRectZero];
  29. }
  30. return self;
  31. }
  32. - (void)viewDidLoad {
  33. [super viewDidLoad];
  34. UIView* contentView = [[UIView alloc] initWithFrame:CGRectZero];
  35. contentView.backgroundColor = RemotingTheme.setupListBackgroundColor;
  36. contentView.translatesAutoresizingMaskIntoConstraints = NO;
  37. [self.view addSubview:contentView];
  38. _label.font = MDCTypography.body1Font;
  39. _label.numberOfLines = 0;
  40. _label.lineBreakMode = NSLineBreakByWordWrapping;
  41. _label.textColor = RemotingTheme.setupListTextColor;
  42. _label.translatesAutoresizingMaskIntoConstraints = NO;
  43. [contentView addSubview:_label];
  44. MDCButton* button = [[MDCButton alloc] initWithFrame:CGRectZero];
  45. [button setTitle:l10n_util::GetNSString(IDS_RETRY)
  46. forState:UIControlStateNormal];
  47. [button setBackgroundColor:UIColor.clearColor forState:UIControlStateNormal];
  48. [button setTitleColor:RemotingTheme.flatButtonTextColor
  49. forState:UIControlStateNormal];
  50. [button sizeToFit];
  51. [button addTarget:self
  52. action:@selector(didTapRetry:)
  53. forControlEvents:UIControlEventTouchUpInside];
  54. button.translatesAutoresizingMaskIntoConstraints = NO;
  55. [contentView addSubview:button];
  56. NSLayoutConstraint* maxWidthConstraint =
  57. [contentView.widthAnchor constraintEqualToConstant:kMaxWidth];
  58. maxWidthConstraint.priority = UILayoutPriorityDefaultHigh;
  59. [NSLayoutConstraint activateConstraints:@[
  60. maxWidthConstraint,
  61. // Trumps |maxWidthConstraint| when necessary.
  62. [contentView.widthAnchor
  63. constraintLessThanOrEqualToAnchor:self.view.widthAnchor],
  64. [contentView.centerXAnchor constraintEqualToAnchor:self.view.centerXAnchor],
  65. [contentView.centerYAnchor constraintEqualToAnchor:self.view.centerYAnchor],
  66. [_label.leadingAnchor constraintEqualToAnchor:contentView.leadingAnchor
  67. constant:kPadding],
  68. [_label.trailingAnchor constraintEqualToAnchor:contentView.trailingAnchor
  69. constant:-kPadding],
  70. [_label.topAnchor constraintEqualToAnchor:contentView.topAnchor
  71. constant:kPadding],
  72. [button.trailingAnchor
  73. constraintEqualToAnchor:contentView.trailingAnchor
  74. constant:-kPadding - kButtonRightPaddingAdjustment],
  75. [button.topAnchor constraintEqualToAnchor:_label.bottomAnchor
  76. constant:kLineSpace],
  77. [button.bottomAnchor
  78. constraintEqualToAnchor:contentView.bottomAnchor
  79. constant:-kPadding - kButtonBottomPaddingAdjustment],
  80. ]];
  81. }
  82. - (UILabel*)label {
  83. return _label;
  84. }
  85. #pragma mark - Private
  86. - (void)didTapRetry:(id)button {
  87. if (_onRetryCallback) {
  88. _onRetryCallback();
  89. }
  90. }
  91. @end