taler-ios

iOS apps for GNU Taler (wallet)
Log | Files | Refs | README | LICENSE

UIScreen+screenSize.swift (1909B)


      1 /*
      2  * This file is part of GNU Taler, ©2022-25 Taler Systems S.A.
      3  * See LICENSE.md
      4  */
      5 /**
      6  * @author Marc Stibane
      7  */
      8 import SwiftUI
      9 
     10 fileprivate func maxValue<T: Comparable>(_ a: T, _ b: T) -> T {
     11     a > b ? a : b
     12 }
     13 fileprivate func maxSize(_ a: CGSize) -> CGFloat {
     14     maxValue(a.width, a.height)
     15 }
     16 fileprivate func minValue<T: Comparable>(_ a: T, _ b: T) -> T {
     17     a < b ? a : b
     18 }
     19 fileprivate func minSize(_ a: CGSize) -> CGFloat {
     20     minValue(a.width, a.height)
     21 }
     22 
     23 
     24 extension UIScreen {
     25     /// will flip in landscape mode
     26     static var screenSize: CGSize { UIScreen.main.bounds.size }
     27     /// we want portrait: height larger than width
     28     static var screenWidth: CGFloat { minSize(UIScreen.main.bounds.size) }
     29     static var screenHeight: CGFloat { maxSize(UIScreen.main.bounds.size) }
     30 
     31     // TODO: When an iPhone is connected to an external display, the UI in the external display is on another UIWindowScene
     32     // see https://dev.to/matsuji/an-anti-pattern-to-get-uiwindowscene-1j54
     33     static var keyWindow: UIWindow? {
     34         UIApplication.shared.connectedScenes
     35             .filter({$0.activationState == .foregroundActive})
     36             .compactMap({$0 as? UIWindowScene})
     37             .first?.windows
     38             .filter({$0.isKeyWindow}).first
     39     }
     40     static var hasNotch: Bool {
     41         let insets = keyWindow?.safeAreaInsets
     42         let bottom = insets?.bottom ?? 0
     43         return bottom > 0
     44     }
     45     static var hasIsland: Bool {
     46         let insets = keyWindow?.safeAreaInsets
     47         let left = insets?.left ?? 0
     48         let bottom = insets?.bottom ?? 0
     49         /// Portrait:   15PM 59,0,34    XS 38,0,29  8plus 20,0,0
     50         /// Landscape:      0,59,21         0,38,21         0,0,0
     51         return bottom > 30 || left > 50
     52     }
     53     static var horzInsets: CGFloat {
     54         let insets = keyWindow?.safeAreaInsets
     55         let left = insets?.left ?? 0
     56         return left
     57     }
     58 }