Detect macOS system Dark/Light apperance at the SwiftUI App/Scene level (for use with MenuBarExtra)?

71 views Asked by At

How to detect, if possible, the macOS system Dark/Light apperance at the SwiftUI App/Scene level? This particular case is for use with MenuBarExtra.

In general, one might expect the following to work:

@main
struct ModeCheckApp: App {    
    var body: some Scene {
        @Environment(\.colorScheme) var colorScheme: ColorScheme

        MenuBarExtra {
            Text(colorScheme == .dark ? "Dark" : "Light")
            Button("Print ColorScheme") { 
                print("colorScheme is '\(colorScheme)'")
            }
        } label: { 
            Image(colorScheme == .light ? "ImageA" : "ImageB")
        }
    }
}

The above approach runs, but never updates:

Runtime Warning: "Accessing Environment's value outside of being installed on a View. This will always read the default value and will not update."

Alternately, one might expect an xcasset Image with all of Any, Light, Dark images to automagically be selected. However, the approach only provides the Light appearance image -- same as the prior approach.

            Image("ImageC")

enter image description here

Somehow, the systemName: image will automatically change with the macOS appearance settings:

            Image(systemName: "questionmark")

Note, this question scope is for:

  1. macOS at the App Scene level which can be used with a standalone MenuBarExtra utility.
  2. does not use non-applicable approach, e.g. a View level solution which is found in other StackOverflow questions… and which, does not work with the current Xcode 14.3.
  3. can use developer provided images i.e. not the system provided images. Although, perhaps there is something the system images have that developer images can replicate??
0

There are 0 answers