summaryrefslogtreecommitdiffhomepage
path: root/ios/MullvadVPN/TranslucentButtonBlurView.swift
blob: 78c916d5ec332196f2301979a8ba0cbb6b2c5d63 (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
//
//  TranslucentButtonBlurView.swift
//  MullvadVPN
//
//  Created by pronebird on 20/03/2019.
//  Copyright © 2019 Mullvad VPN AB. All rights reserved.
//

import UIKit

class TranslucentButtonBlurView: UIVisualEffectView {
    init(button: AppButton) {
        let effect = UIBlurEffect(style: button.style.blurEffectStyle)

        super.init(effect: effect)

        button.translatesAutoresizingMaskIntoConstraints = false

        contentView.addSubview(button)

        NSLayoutConstraint.activate([
            button.topAnchor.constraint(equalTo: contentView.topAnchor),
            button.leadingAnchor.constraint(equalTo: contentView.leadingAnchor),
            button.trailingAnchor.constraint(equalTo: contentView.trailingAnchor),
            button.bottomAnchor.constraint(equalTo: contentView.bottomAnchor)
        ])

        layer.cornerRadius = UIMetrics.controlCornerRadius
        layer.maskedCorners = button.style.cornerMask(effectiveUserInterfaceLayoutDirection)
        layer.masksToBounds = true
    }

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

private extension AppButton.Style {
    func cornerMask(_ userInterfaceLayoutDirection: UIUserInterfaceLayoutDirection) -> CACornerMask {
        switch (self, userInterfaceLayoutDirection) {
        case (.translucentDangerSplitLeft, .leftToRight), (.translucentDangerSplitRight, .rightToLeft):
            return [.layerMinXMinYCorner, .layerMinXMaxYCorner]
        case (.translucentDangerSplitRight, .leftToRight), (.translucentDangerSplitLeft, .rightToLeft):
            return [.layerMaxXMinYCorner, .layerMaxXMaxYCorner]
        default:
            return [
                .layerMinXMinYCorner, .layerMinXMaxYCorner,
                .layerMaxXMinYCorner, .layerMaxXMaxYCorner
            ]
        }
    }

    var blurEffectStyle: UIBlurEffect.Style {
        switch self {
        case .translucentDangerSplitLeft, .translucentDangerSplitRight, .translucentDanger:
            if #available(iOS 13.0, *) {
                return .systemUltraThinMaterialDark
            } else {
                return .dark
            }
        default:
            return .light
        }
    }
}