diff --git a/iOS/Master/Cell/MasterTableViewCell.swift b/iOS/Master/Cell/MasterTableViewCell.swift index d973faf1f..4ab775021 100644 --- a/iOS/Master/Cell/MasterTableViewCell.swift +++ b/iOS/Master/Cell/MasterTableViewCell.swift @@ -42,11 +42,7 @@ class MasterTableViewCell : UITableViewCell { var disclosureExpanded = false { didSet { - if disclosureExpanded { - accessoryButton?.setImage(AppAssets.chevronDownImage, for: .normal) - } else { - accessoryButton?.setImage(AppAssets.chevronRightImage, for: .normal) - } + updateDisclosureImage() } } @@ -110,7 +106,7 @@ class MasterTableViewCell : UITableViewCell { private let unreadCountView = MasterUnreadCountView(frame: CGRect.zero) private var showingEditControl = false - private var accessoryButton: UIButton? + private var disclosureButton: UIButton? required init?(coder: NSCoder) { super.init(coder: coder) @@ -124,26 +120,15 @@ class MasterTableViewCell : UITableViewCell { override func layoutSubviews() { super.layoutSubviews() - let layout = MasterTableViewCellLayout(cellSize: bounds.size, shouldShowImage: shouldShowImage, label: titleView, unreadCountView: unreadCountView, showingEditingControl: showingEditControl, indent: indent) + let layout = MasterTableViewCellLayout(cellSize: bounds.size, shouldShowImage: shouldShowImage, label: titleView, unreadCountView: unreadCountView, showingEditingControl: showingEditControl, indent: indent, shouldShowDisclosure: true) layoutWith(layout) } @objc func buttonPressed(_ sender: UIButton) { - - guard allowDisclosureSelection else { - return + if allowDisclosureSelection { + disclosureExpanded = !disclosureExpanded + delegate?.disclosureSelected(self, expanding: disclosureExpanded) } - - if !disclosureExpanded { - sender.setImage(AppAssets.chevronDownImage, for: .normal) - delegate?.disclosureSelected(self, expanding: true) - } else { - sender.setImage(AppAssets.chevronRightImage, for: .normal) - delegate?.disclosureSelected(self, expanding: false) - } - - disclosureExpanded = !disclosureExpanded - } } @@ -151,28 +136,29 @@ class MasterTableViewCell : UITableViewCell { private extension MasterTableViewCell { func commonInit() { - addAccessoryView() addSubviewAtInit(unreadCountView) addSubviewAtInit(faviconImageView) addSubviewAtInit(titleView) + addDisclosureView() } - func addAccessoryView() { + func addDisclosureView() { - let button = UIButton(type: .roundedRect) - button.frame = CGRect(x: 0, y: 0, width: 25.0, height: 25.0) + disclosureButton = UIButton(type: .roundedRect) + disclosureButton!.tintColor = AppAssets.chevronDisclosureColor + disclosureButton!.addTarget(self, action: #selector(buttonPressed(_:)), for: UIControl.Event.touchUpInside) + updateDisclosureImage() + addSubviewAtInit(disclosureButton!) + + } + + func updateDisclosureImage() { if disclosureExpanded { - button.setImage(AppAssets.chevronDownImage, for: .normal) + disclosureButton?.setImage(AppAssets.chevronDownImage, for: .normal) } else { - button.setImage(AppAssets.chevronRightImage, for: .normal) + disclosureButton?.setImage(AppAssets.chevronRightImage, for: .normal) } - - button.tintColor = AppAssets.chevronDisclosureColor - button.addTarget(self, action: #selector(buttonPressed(_:)), for: UIControl.Event.touchUpInside) - accessoryButton = button - accessoryView = button - } func addSubviewAtInit(_ view: UIView) { @@ -184,6 +170,7 @@ private extension MasterTableViewCell { faviconImageView.rs_setFrameIfNotEqual(layout.faviconRect) titleView.rs_setFrameIfNotEqual(layout.titleRect) unreadCountView.rs_setFrameIfNotEqual(layout.unreadCountRect) + disclosureButton?.rs_setFrameIfNotEqual(layout.disclosureButtonRect) } } diff --git a/iOS/Master/Cell/MasterTableViewCellLayout.swift b/iOS/Master/Cell/MasterTableViewCellLayout.swift index 3f06f02ee..26418f20e 100644 --- a/iOS/Master/Cell/MasterTableViewCellLayout.swift +++ b/iOS/Master/Cell/MasterTableViewCellLayout.swift @@ -15,26 +15,19 @@ struct MasterTableViewCellLayout { private static let imageMarginLeft = CGFloat(integerLiteral: 8) private static let imageMarginRight = CGFloat(integerLiteral: 8) private static let unreadCountMarginLeft = CGFloat(integerLiteral: 8) - private static let unreadCountMarginRight = CGFloat(integerLiteral: 8) - - private static let chevronWidth = CGFloat(integerLiteral: 40) + private static let unreadCountMarginRight = CGFloat(integerLiteral: 0) + private static let disclosureButtonSize = CGSize(width: 44, height: 44) let faviconRect: CGRect let titleRect: CGRect let unreadCountRect: CGRect + let disclosureButtonRect: CGRect - init(cellSize: CGSize, shouldShowImage: Bool, label: UILabel, unreadCountView: MasterUnreadCountView, showingEditingControl: Bool, indent: Bool) { + init(cellSize: CGSize, shouldShowImage: Bool, label: UILabel, unreadCountView: MasterUnreadCountView, showingEditingControl: Bool, indent: Bool, shouldShowDisclosure: Bool) { - let adjustedWidth: CGFloat = { - if showingEditingControl { - return floor(cellSize.width) - } else { - return floor(cellSize.width) - MasterTableViewCellLayout.chevronWidth - } - }() - - let bounds = CGRect(x: 0.0, y: 0.0, width: adjustedWidth, height: floor(cellSize.height)) + let bounds = CGRect(x: 0.0, y: 0.0, width: floor(cellSize.width), height: floor(cellSize.height)) + // Favicon var rFavicon = CGRect.zero if shouldShowImage { var indentX = showingEditingControl ? MasterTableViewCellLayout.imageMarginLeft + 40 : MasterTableViewCellLayout.imageMarginLeft @@ -44,6 +37,7 @@ struct MasterTableViewCellLayout { } self.faviconRect = rFavicon + // Title let labelSize = SingleLineUILabelSizer.size(for: label.text ?? "", font: label.font!) var rLabel = CGRect(x: 0.0, y: 0.0, width: labelSize.width, height: labelSize.height) @@ -52,26 +46,52 @@ struct MasterTableViewCellLayout { } rLabel = MasterTableViewCellLayout.centerVertically(rLabel, bounds) + // Unread Count let unreadCountSize = unreadCountView.intrinsicContentSize let unreadCountIsHidden = unreadCountView.unreadCount < 1 var rUnread = CGRect.zero if !unreadCountIsHidden { + rUnread.size = unreadCountSize - rUnread.origin.x = (bounds.maxX - unreadCountSize.width) - MasterTableViewCellLayout.unreadCountMarginRight + rUnread.origin.x = bounds.maxX - + (unreadCountSize.width + MasterTableViewCellLayout.unreadCountMarginRight + MasterTableViewCellLayout.disclosureButtonSize.width) rUnread = MasterTableViewCellLayout.centerVertically(rUnread, bounds) + + // Cap the Title width based on the unread indicator button let labelMaxX = rUnread.minX - MasterTableViewCellLayout.unreadCountMarginLeft if rLabel.maxX > labelMaxX { rLabel.size.width = labelMaxX - rLabel.minX } + } self.unreadCountRect = rUnread + + // Disclosure Button + var rDisclosure = CGRect.zero + if shouldShowDisclosure { + + rDisclosure.size = MasterTableViewCellLayout.disclosureButtonSize + rDisclosure.origin.x = bounds.maxX - MasterTableViewCellLayout.disclosureButtonSize.width + rDisclosure = MasterTableViewCellLayout.centerVertically(rDisclosure, bounds) + + // Cap the Title width based on the disclosure button + let labelMaxX = rDisclosure.minX + if rLabel.maxX > labelMaxX { + rLabel.size.width = labelMaxX - rLabel.minX + } + + } + self.disclosureButtonRect = rDisclosure + + // Cap the Title width based on total width if rLabel.maxX > bounds.maxX { rLabel.size.width = bounds.maxX - rLabel.minX } self.titleRect = rLabel + } // Ideally this will be implemented in RSCore (see RSGeometry)