summaryrefslogtreecommitdiffhomepage
path: root/ios/MullvadVPN/DisconnectSplitButton.swift
blob: 61ac11579a70f7f95d575ce689b18ed2c84cf390 (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
//
//  DisconnectSplitButton.swift
//  MullvadVPN
//
//  Created by pronebird on 29/07/2020.
//  Copyright © 2020 Mullvad VPN AB. All rights reserved.
//

import Foundation
import UIKit

class DisconnectSplitButton: UIView {
    let primaryButton = AppButton(style: .translucentDangerSplitLeft)
    var secondaryButton = AppButton(style: .translucentDangerSplitRight)

    private let stackView: UIStackView

    private var secondaryButtonObserver: NSObjectProtocol?

    init() {
        stackView = UIStackView(arrangedSubviews: [primaryButton, secondaryButton])
        stackView.translatesAutoresizingMaskIntoConstraints = false
        stackView.axis = .horizontal
        stackView.distribution = .fill
        stackView.alignment = .fill
        stackView.spacing = 1

        primaryButton.titleLabel?.font = UIFont.systemFont(ofSize: 18, weight: .semibold)
        primaryButton.setContentHuggingPriority(.defaultLow, for: .horizontal)
        primaryButton.setContentHuggingPriority(.defaultLow, for: .vertical)
        primaryButton.setContentCompressionResistancePriority(.defaultHigh, for: .horizontal)
        primaryButton.setContentCompressionResistancePriority(.defaultHigh, for: .vertical)

        secondaryButton.setImage(UIImage(named: "IconReload"), for: .normal)
        secondaryButton.setContentHuggingPriority(.defaultHigh, for: .horizontal)
        secondaryButton.setContentHuggingPriority(.defaultHigh, for: .vertical)
        secondaryButton.setContentCompressionResistancePriority(UILayoutPriority(50), for: .horizontal)
        secondaryButton.setContentCompressionResistancePriority(UILayoutPriority(50), for: .vertical)

        super.init(frame: .zero)

        addSubview(stackView)

        secondaryButtonObserver = secondaryButton.observe(\.bounds, options: [.new]) { [weak self] (button, change) in
            self?.adjustTitleLabelPosition()
        }

        NSLayoutConstraint.activate([
            stackView.leadingAnchor.constraint(equalTo: leadingAnchor),
            stackView.trailingAnchor.constraint(equalTo: trailingAnchor),
            stackView.topAnchor.constraint(equalTo: topAnchor),
            stackView.bottomAnchor.constraint(equalTo: bottomAnchor),

            primaryButton.heightAnchor.constraint(equalTo: secondaryButton.heightAnchor),
            secondaryButton.widthAnchor.constraint(equalTo: secondaryButton.heightAnchor)
        ])
    }

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

    private func adjustTitleLabelPosition() {
        var contentInsets = AppButton.defaultContentInsets
        contentInsets.left = secondaryButton.frame.width + stackView.spacing
        contentInsets.right = 0

        primaryButton.contentEdgeInsets = contentInsets
    }
}