Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import Redux
import Shared
import UIKit

final class AutoTranslatePromptView: UIView, ThemeApplicable {
final class AutoTranslatePromptView: UIView, ThemeApplicable, Notifiable {
private struct UX {
static let borderThickness: CGFloat = 1.0
static let contentPadding = NSDirectionalEdgeInsets(
Expand All @@ -20,6 +20,8 @@ final class AutoTranslatePromptView: UIView, ThemeApplicable {
static let contentSpacing: CGFloat = 8
}

var notificationCenter: NotificationProtocol = NotificationCenter.default

private let windowUUID: WindowUUID

private var topBorderView: UIView = .build()
Expand Down Expand Up @@ -50,6 +52,12 @@ final class AutoTranslatePromptView: UIView, ThemeApplicable {
button.addTarget(self, action: #selector(self.didTapDismiss), for: .touchUpInside)
}

private lazy var messageEnableStack: UIStackView = .build { stack in
stack.axis = .horizontal
stack.alignment = .center
stack.spacing = UX.contentSpacing
}

private lazy var contentRow: UIStackView = .build { stack in
stack.axis = .horizontal
stack.alignment = .center
Expand All @@ -60,15 +68,22 @@ final class AutoTranslatePromptView: UIView, ThemeApplicable {
self.windowUUID = windowUUID
super.init(frame: .zero)
setupView()
startObservingNotifications(
withNotificationCenter: notificationCenter,
forObserver: self,
observing: [UIContentSizeCategory.didChangeNotification]
)
}

required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}

private func setupView() {
contentRow.addArrangedSubview(messageLabel)
contentRow.addArrangedSubview(enableButton)
messageEnableStack.addArrangedSubview(messageLabel)
messageEnableStack.addArrangedSubview(enableButton)

contentRow.addArrangedSubview(messageEnableStack)
contentRow.addArrangedSubview(closeButton)

addSubview(topBorderView)
Expand All @@ -85,6 +100,21 @@ final class AutoTranslatePromptView: UIView, ThemeApplicable {
contentRow.leadingAnchor.constraint(equalTo: leadingAnchor, constant: UX.contentPadding.leading),
contentRow.trailingAnchor.constraint(equalTo: trailingAnchor, constant: UX.contentPadding.trailing),
])

adjustLayout()
}

private func adjustLayout() {
let isAccessibility = UIApplication.shared.preferredContentSizeCategory.isAccessibilityCategory
if isAccessibility {
messageEnableStack.axis = .vertical
messageEnableStack.alignment = .leading
contentRow.alignment = .top
} else {
messageEnableStack.axis = .horizontal
messageEnableStack.alignment = .center
contentRow.alignment = .center
}
}

@objc
Expand All @@ -103,6 +133,16 @@ final class AutoTranslatePromptView: UIView, ThemeApplicable {
))
}

// MARK: - Notifiable

func handleNotifications(_ notification: Notification) {
switch notification.name {
case UIContentSizeCategory.didChangeNotification:
ensureMainThread { self.adjustLayout() }
default: break
}
}

// MARK: - ThemeApplicable

func applyTheme(theme: Theme) {
Expand Down
Loading