Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sheet background does not respect app's theme #9

Closed
SylarRuby opened this issue Dec 29, 2021 · 4 comments
Closed

Sheet background does not respect app's theme #9

SylarRuby opened this issue Dec 29, 2021 · 4 comments

Comments

@SylarRuby
Copy link

SylarRuby commented Dec 29, 2021

Hola. Thanks for provide the community with such an elegant sheet. No major issues, just something I've noticed.

The sheet's background changes when the system's theme changes: dark mode gives the sheet a dark color and light mode... white sheet. Apps can specify a theme in which the sheet does not respect. An example how an app could change the theme:

class Utilities: ObservableObject {

    // The default is to use the system's default.
    @AppStorage("theme") var theme: String = ""
    
    var userInterfaceStyle: ColorScheme? = .dark

    func overrideDisplayMode() {
        var userInterfaceStyle: UIUserInterfaceStyle

        if theme == "On" {
            userInterfaceStyle = .dark
        } else if theme == "Off" {
            userInterfaceStyle = .light
        } else {
            userInterfaceStyle = .unspecified
        }
        
        let scenes = UIApplication.shared.connectedScenes
        let windowScene = scenes.first as? UIWindowScene
        let window = windowScene?.windows.first
        
        window?.overrideUserInterfaceStyle = userInterfaceStyle
    }
}

Should the system is set to dark mode and the app to light, the content sheet's background is still dark. Ok I could use the .sheetBackground modifier you've provided us, with a combination with @Environment(\.colorScheme) var colorScheme. Seems a bit messy if we have many sheets within the app. Is there a way to make the sheet respect the app's theme? I'd rather not having to keep repeating this:

view.resizableSheet($state) { builder in
            builder.content { context in
                VStack {
                    Text("Demo")
                    Spacer()
                }
                    .padding()
                    .frame(height: 300)
            }.sheetBackground { _ in
                Color(colorScheme == .light ? "custom-white" : "custom-secondary")
            }
        }
@mtj0928
Copy link
Owner

mtj0928 commented Dec 29, 2021

@SylarRuby
Thank you for your report!!

I didn't notice the issue.
ResizableSheet creates a new window, but the window's overrideUserInterfaceStyle is not updated on your code.
I will add a new api to control the window soon.

As a workaround, you can specify color theme for all windows in windowScene like this.

let scenes = UIApplication.shared.connectedScenes
let windowScene = scenes.first as? UIWindowScene
windowScene?.windows.forEach({ window in
    window.overrideUserInterfaceStyle = userInterfaceStyle
})

@SylarRuby
Copy link
Author

Hi @mtj0928 No problem and that workaround works just fine. Thank you!

@mtj0928
Copy link
Owner

mtj0928 commented Dec 29, 2021

@SylarRuby I updated the API.

From 0.0.4, you can access window in ResizableSheetCenter via a static method.

let window = ResizableSheetCenter.resolve(for: windowScene)?.window

// you can update the window  here
window?.overrideUserInterfaceStyle = userInterfaceStyle

@SylarRuby
Copy link
Author

Thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants