Skip to content

馃摵 Add screen control#11

Description

@superturboryan

Let's add a way to control the brightness of the device's screen.

We can also control whether it "falls asleep" with UIApplication.shared.isIdleTimerDisabled.

Rough draft:

public extension Control {
    
    /// 馃攩 Control the device's screen's brightness.
    @MainActor
    enum Screen {
            
        public static var isKeptAwake: Bool {
            get {
                UIApplication.shared.isIdleTimerDisabled
            }
            set {
                UIApplication.shared.isIdleTimerDisabled = newValue
            }
        }
        
        public static var brightness: CGFloat {
            get {
                UIScreen.main.brightness
            }
            set {
                UIScreen.main.brightness = min(max(newValue, 0.0), 1.0)
            }
        }
        
        public static var isDimmed: Bool {
            brightness == 0
        }
        
        public static func toggleDimmedScreen() {
            if isDimmed {
                brightness = dimScreenValue
            } else {
                dimScreenValue = brightness
                brightness = 0
            }
        }
        
        private static var dimScreenValue: CGFloat = 0
    }
}

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

enhancementNew feature or request

Projects

No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions