mirror of https://github.com/oxen-io/session-ios
You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
216 lines
6.7 KiB
Swift
216 lines
6.7 KiB
Swift
7 years ago
|
//
|
||
6 years ago
|
// Copyright (c) 2019 Open Whisper Systems. All rights reserved.
|
||
7 years ago
|
//
|
||
|
|
||
|
import Foundation
|
||
|
import UIKit
|
||
5 years ago
|
import SessionUIKit
|
||
7 years ago
|
|
||
7 years ago
|
@objc
|
||
7 years ago
|
public protocol NavBarLayoutDelegate: class {
|
||
7 years ago
|
func navBarCallLayoutDidChange(navbar: OWSNavigationBar)
|
||
|
}
|
||
|
|
||
7 years ago
|
@objc
|
||
7 years ago
|
public class OWSNavigationBar: UINavigationBar {
|
||
7 years ago
|
|
||
7 years ago
|
@objc
|
||
|
public weak var navBarLayoutDelegate: NavBarLayoutDelegate?
|
||
7 years ago
|
|
||
7 years ago
|
@objc
|
||
|
public let navbarWithoutStatusHeight: CGFloat = 44
|
||
7 years ago
|
|
||
7 years ago
|
@objc
|
||
|
public var callBannerHeight: CGFloat {
|
||
7 years ago
|
return OWSWindowManagerCallBannerHeight()
|
||
7 years ago
|
}
|
||
7 years ago
|
|
||
7 years ago
|
@objc
|
||
|
public var statusBarHeight: CGFloat {
|
||
7 years ago
|
return CurrentAppContext().statusBarHeight
|
||
7 years ago
|
}
|
||
|
|
||
7 years ago
|
@objc
|
||
|
public var fullWidth: CGFloat {
|
||
7 years ago
|
return UIScreen.main.bounds.size.width
|
||
|
}
|
||
|
|
||
7 years ago
|
public required init?(coder aDecoder: NSCoder) {
|
||
7 years ago
|
notImplemented()
|
||
7 years ago
|
}
|
||
|
|
||
7 years ago
|
@objc
|
||
|
public static let backgroundBlurMutingFactor: CGFloat = 0.5
|
||
7 years ago
|
var blurEffectView: UIVisualEffectView?
|
||
7 years ago
|
|
||
7 years ago
|
override init(frame: CGRect) {
|
||
|
super.init(frame: frame)
|
||
|
|
||
7 years ago
|
applyTheme()
|
||
|
|
||
|
NotificationCenter.default.addObserver(self, selector: #selector(callDidChange), name: .OWSWindowManagerCallDidChange, object: nil)
|
||
6 years ago
|
NotificationCenter.default.addObserver(self, selector: #selector(didChangeStatusBarFrame), name: UIApplication.didChangeStatusBarFrameNotification, object: nil)
|
||
7 years ago
|
NotificationCenter.default.addObserver(self,
|
||
|
selector: #selector(themeDidChange),
|
||
|
name: .ThemeDidChange,
|
||
|
object: nil)
|
||
|
}
|
||
|
|
||
6 years ago
|
// MARK: FirstResponder Stubbing
|
||
|
|
||
|
@objc
|
||
|
public weak var stubbedNextResponder: UIResponder?
|
||
|
|
||
|
override public var next: UIResponder? {
|
||
|
if let stubbedNextResponder = self.stubbedNextResponder {
|
||
|
return stubbedNextResponder
|
||
|
}
|
||
|
|
||
|
return super.next
|
||
|
}
|
||
|
|
||
7 years ago
|
// MARK: Theme
|
||
|
|
||
|
private func applyTheme() {
|
||
7 years ago
|
guard respectsTheme else {
|
||
|
return
|
||
|
}
|
||
|
|
||
5 years ago
|
backgroundColor = Colors.navigationBarBackground
|
||
5 years ago
|
|
||
5 years ago
|
tintColor = Colors.text
|
||
5 years ago
|
|
||
6 years ago
|
if UIAccessibility.isReduceTransparencyEnabled {
|
||
7 years ago
|
blurEffectView?.isHidden = true
|
||
7 years ago
|
let color = Theme.navbarBackgroundColor
|
||
|
let backgroundImage = UIImage(color: color)
|
||
|
self.setBackgroundImage(backgroundImage, for: .default)
|
||
|
} else {
|
||
7 years ago
|
// Make navbar more translucent than default. Navbars remove alpha from any assigned backgroundColor, so
|
||
|
// to achieve transparency, we have to assign a transparent image.
|
||
6 years ago
|
let color = Theme.navbarBackgroundColor
|
||
7 years ago
|
let backgroundImage = UIImage(color: color)
|
||
|
self.setBackgroundImage(backgroundImage, for: .default)
|
||
|
|
||
|
// remove hairline below bar.
|
||
|
self.shadowImage = UIImage()
|
||
|
}
|
||
7 years ago
|
}
|
||
|
|
||
7 years ago
|
@objc
|
||
|
public func themeDidChange() {
|
||
7 years ago
|
Logger.debug("")
|
||
7 years ago
|
applyTheme()
|
||
7 years ago
|
}
|
||
|
|
||
7 years ago
|
@objc
|
||
|
public var respectsTheme: Bool = true {
|
||
|
didSet {
|
||
|
themeDidChange()
|
||
|
}
|
||
|
}
|
||
|
|
||
7 years ago
|
// MARK: Layout
|
||
|
|
||
7 years ago
|
@objc
|
||
|
public func callDidChange() {
|
||
7 years ago
|
Logger.debug("")
|
||
7 years ago
|
self.navBarLayoutDelegate?.navBarCallLayoutDidChange(navbar: self)
|
||
7 years ago
|
}
|
||
|
|
||
7 years ago
|
@objc
|
||
|
public func didChangeStatusBarFrame() {
|
||
7 years ago
|
Logger.debug("")
|
||
7 years ago
|
self.navBarLayoutDelegate?.navBarCallLayoutDidChange(navbar: self)
|
||
7 years ago
|
}
|
||
|
|
||
7 years ago
|
public override func sizeThatFits(_ size: CGSize) -> CGSize {
|
||
7 years ago
|
guard OWSWindowManager.shared().hasCall() else {
|
||
|
return super.sizeThatFits(size)
|
||
|
}
|
||
7 years ago
|
|
||
7 years ago
|
if #available(iOS 11, *) {
|
||
|
return super.sizeThatFits(size)
|
||
7 years ago
|
} else if #available(iOS 10, *) {
|
||
|
// iOS10
|
||
|
// sizeThatFits is repeatedly called to determine how much space to reserve for that navbar.
|
||
7 years ago
|
// That is, increasing this causes the child view controller to be pushed down.
|
||
|
// (as of iOS11, this is not used and instead we use additionalSafeAreaInsets)
|
||
7 years ago
|
return CGSize(width: fullWidth, height: navbarWithoutStatusHeight + statusBarHeight)
|
||
7 years ago
|
} else {
|
||
|
// iOS9
|
||
|
// sizeThatFits is repeatedly called to determine how much space to reserve for that navbar.
|
||
|
// That is, increasing this causes the child view controller to be pushed down.
|
||
|
// (as of iOS11, this is not used and instead we use additionalSafeAreaInsets)
|
||
|
return CGSize(width: fullWidth, height: navbarWithoutStatusHeight + callBannerHeight + 20)
|
||
7 years ago
|
}
|
||
7 years ago
|
}
|
||
|
|
||
7 years ago
|
public override func layoutSubviews() {
|
||
6 years ago
|
guard CurrentAppContext().isMainApp else {
|
||
|
super.layoutSubviews()
|
||
|
return
|
||
|
}
|
||
|
guard OWSWindowManager.shared().hasCall() else {
|
||
|
super.layoutSubviews()
|
||
|
return
|
||
7 years ago
|
}
|
||
|
|
||
7 years ago
|
guard #available(iOS 11, *) else {
|
||
|
super.layoutSubviews()
|
||
|
return
|
||
|
}
|
||
|
|
||
7 years ago
|
self.frame = CGRect(x: 0, y: callBannerHeight, width: fullWidth, height: navbarWithoutStatusHeight)
|
||
|
self.bounds = CGRect(x: 0, y: 0, width: fullWidth, height: navbarWithoutStatusHeight)
|
||
7 years ago
|
|
||
|
super.layoutSubviews()
|
||
|
|
||
7 years ago
|
// This is only necessary on iOS11, which has some private views within that lay outside of the navbar.
|
||
|
// They aren't actually visible behind the call status bar, but they looks strange during present/dismiss
|
||
|
// animations for modal VC's
|
||
7 years ago
|
for subview in self.subviews {
|
||
|
let stringFromClass = NSStringFromClass(subview.classForCoder)
|
||
|
if stringFromClass.contains("BarBackground") {
|
||
7 years ago
|
subview.frame = self.bounds
|
||
7 years ago
|
} else if stringFromClass.contains("BarContentView") {
|
||
7 years ago
|
subview.frame = self.bounds
|
||
7 years ago
|
}
|
||
7 years ago
|
}
|
||
|
}
|
||
7 years ago
|
|
||
7 years ago
|
// MARK: Override Theme
|
||
7 years ago
|
|
||
|
@objc
|
||
7 years ago
|
public enum NavigationBarThemeOverride: Int {
|
||
|
case clear, alwaysDark
|
||
7 years ago
|
}
|
||
|
|
||
7 years ago
|
@objc
|
||
|
public func overrideTheme(type: NavigationBarThemeOverride) {
|
||
|
respectsTheme = false
|
||
|
|
||
|
barStyle = .black
|
||
6 years ago
|
titleTextAttributes = [NSAttributedString.Key.foregroundColor: Theme.darkThemePrimaryColor]
|
||
7 years ago
|
barTintColor = Theme.darkThemeBackgroundColor.withAlphaComponent(0.6)
|
||
|
tintColor = Theme.darkThemePrimaryColor
|
||
|
|
||
|
switch type {
|
||
|
case .clear:
|
||
|
blurEffectView?.isHidden = true
|
||
|
clipsToBounds = true
|
||
|
|
||
|
// Making a toolbar transparent requires setting an empty uiimage
|
||
|
setBackgroundImage(UIImage(), for: .default)
|
||
|
shadowImage = UIImage()
|
||
|
backgroundColor = .clear
|
||
|
case .alwaysDark:
|
||
|
blurEffectView?.isHidden = false
|
||
|
clipsToBounds = false
|
||
|
|
||
|
setBackgroundImage(nil, for: .default)
|
||
|
shadowImage = nil
|
||
7 years ago
|
}
|
||
7 years ago
|
}
|
||
7 years ago
|
}
|