summaryrefslogtreecommitdiffhomepage
path: root/ios/MullvadVPN/HeaderBarView.swift
blob: da5b1c41b69e86033fffc9cfe4ba46dc0bf378c9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
//
//  HeaderBarView.swift
//  MullvadVPN
//
//  Created by pronebird on 19/06/2020.
//  Copyright © 2020 Mullvad VPN AB. All rights reserved.
//

import Foundation
import UIKit

class HeaderBarView: UIView {
    private let brandNameImage = UIImage(named: "LogoText")!
        .backport_withTintColor(UIColor.HeaderBar.brandNameColor, renderingMode: .alwaysOriginal)

    let logoImageView: UIImageView = {
        let imageView = UIImageView(image: UIImage(named: "LogoIcon"))
        imageView.translatesAutoresizingMaskIntoConstraints = false
        return imageView
    }()

    lazy var brandNameImageView: UIImageView = {
        let imageView = UIImageView(image: brandNameImage)
        imageView.translatesAutoresizingMaskIntoConstraints = false
        imageView.contentMode = .scaleAspectFill
        return imageView
    }()

    let settingsButton = makeSettingsButton()

    class func makeSettingsButton() -> HeaderBarButton {
        let settingsImage = UIImage(named: "IconSettings")?
            .backport_withTintColor(UIColor.HeaderBar.buttonColor, renderingMode: .alwaysOriginal)

        let settingsButton = HeaderBarButton(type: .system)
        settingsButton.setImage(settingsImage, for: .normal)
        settingsButton.translatesAutoresizingMaskIntoConstraints = false
        settingsButton.accessibilityIdentifier = "SettingsButton"
        settingsButton.accessibilityLabel = NSLocalizedString(
            "HEADER_BAR_SETTINGS_BUTTON_ACCESSIBILITY_LABEL",
            tableName: "HeaderBar",
            value: "Settings",
            comment: ""
        )
        return settingsButton
    }

    private let borderLayer: CALayer = {
        let layer = CALayer()
        layer.backgroundColor = UIColor.HeaderBar.dividerColor.cgColor
        return layer
    }()

    var showsDivider = false {
        didSet {
            if showsDivider {
                layer.addSublayer(borderLayer)
            } else {
                borderLayer.removeFromSuperlayer()
            }
        }
    }

    override init(frame: CGRect) {
        super.init(frame: frame)

        layoutMargins = UIEdgeInsets(
            top: 0,
            left: UIMetrics.contentLayoutMargins.left,
            bottom: 0,
            right: UIMetrics.contentLayoutMargins.right
        )

        if #available(iOS 13.0, *) {
            accessibilityContainerType = .semanticGroup
        }

        let imageSize = brandNameImage.size
        let brandNameAspectRatio = imageSize.width / max(imageSize.height, 1)

        let constraints = [
            logoImageView.leadingAnchor.constraint(equalTo: layoutMarginsGuide.leadingAnchor),
            logoImageView.centerYAnchor.constraint(equalTo: brandNameImageView.centerYAnchor),
            logoImageView.widthAnchor.constraint(equalToConstant: 44),
            logoImageView.heightAnchor.constraint(equalTo: logoImageView.widthAnchor, multiplier: 1),

            brandNameImageView.leadingAnchor.constraint(equalTo: logoImageView.trailingAnchor, constant: 9),
            brandNameImageView.topAnchor.constraint(equalTo: layoutMarginsGuide.topAnchor, constant: 22),
            brandNameImageView.widthAnchor.constraint(equalTo: brandNameImageView.heightAnchor, multiplier: brandNameAspectRatio),
            brandNameImageView.heightAnchor.constraint(equalToConstant: 18),
            layoutMarginsGuide.bottomAnchor.constraint(equalTo: brandNameImageView.bottomAnchor, constant: 22),

            settingsButton.leadingAnchor.constraint(greaterThanOrEqualTo: brandNameImageView.trailingAnchor, constant: 8),
            settingsButton.trailingAnchor.constraint(equalTo: layoutMarginsGuide.trailingAnchor),
            settingsButton.centerYAnchor.constraint(equalTo: brandNameImageView.centerYAnchor)
        ]

        [logoImageView, brandNameImageView, settingsButton].forEach { addSubview($0) }

        NSLayoutConstraint.activate(constraints)
    }

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

    override func layoutSubviews() {
        super.layoutSubviews()

        borderLayer.frame = CGRect(x: 0, y: frame.maxY - 1, width: frame.width, height: 1)
    }
}