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")
Somehow, the systemName: image will automatically change with the macOS appearance settings:
Image(systemName: "questionmark")
Note, this question scope is for:
macOSat theAppScenelevel which can be used with a standaloneMenuBarExtrautility.- does not use non-applicable approach, e.g. a
Viewlevel solution which is found in other StackOverflow questions… and which, does not work with the current Xcode 14.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??
