Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can dark mode be detected on macOS 10.14?

In macOS 10.14 users can choose to adopt a system-wide light or dark appearance and I need to adjust some colours manually depend of the current mode.

like image 991
93sauu Avatar asked Aug 03 '18 11:08

93sauu


People also ask

Does macOS Mojave have Dark Mode?

While in Dark mode, the dock, menu bar, and all of your Apple apps, including Safari, Mail, Calendar, Notes, the Mac App Store, Messages, and more will feature darker colors and themes. ‌Dark Mode‌ will need to be built into third-party Mac apps that don't already offer a dark option when macOS Mojave is released.

Why can't I get Dark Mode on my Mac?

To enable Dark Mode on a Mac, click the Apple logo in the top-left corner of your screen. Then select System Preferences and click the General button in the pop-up window. Finally, click Dark or Auto to enable Dark Mode. Click the Apple logo in the top-left corner of your Mac's screen.

Which macOS version has Dark Mode?

Dark Mode was first introduced to Mac with OS X 10.10 Yosemite, and it has become a staple of all subsequent versions of macOS.

How does Swift Detect Dark Mode?

For that, you can return a different color based on the current interface style. Dark mode can be detected by using the userInterfaceStyle property on the current trait collection.

What is macOS Mojave’s dark mode?

The feature Apple is promoting most heavily with macOS 10.14 Mojave is Dark mode, which the company advertises as “a dramatic new look that helps you focus on your work… as toolbars and menus recede into the background.” Let’s look at what Apple has done with Dark mode, after which you’ll have a better idea of what to think about while trying it.

How to turn on dark mode on Mac?

Turn on Dark Mode. From the Apple menu , choose System Preferences. Click General. From the Appearance options at the top of the window, select Dark. This feature is available only after upgrading macOS Mojave.

Does Xcode have a dark mode?

Comparatively evident is another dark mode subject to Xcode, conceivably alluding to a more widespread “dull mode” on the Mac. Right now, neither iOS or macOS has a “genuine” dark mode. On the Mac, the menu banish can be transformed from a light to dark appearance, and on iOS, a feature known as Smart Invert can to some degree emulate the target.

Why does my Desktop appear dark when I Turn on dark mode?

If you turn on Dark Mode while using Dynamic Desktop, the desktop may change to the dark still image. You can change this setting in Desktop & Screen Saver preferences .


2 Answers

Since the actual appearance object you usually get via effectiveAppearance is a composite appearance, asking for its name directly probably isn't a reliable solution.

Asking for the currentAppearance usually isn't a good idea, either, as a view may be explicitly set to light mode or you want to know whether a view is light or dark outside of a drawRect: where you might get incorrect results after a mode switch.

The solution I came up with looks like this:

BOOL appearanceIsDark(NSAppearance * appearance)
{
    if (@available(macOS 10.14, *)) {
        NSAppearanceName basicAppearance = [appearance bestMatchFromAppearancesWithNames:@[
            NSAppearanceNameAqua,
            NSAppearanceNameDarkAqua
        ]];
        return [basicAppearance isEqualToString:NSAppearanceNameDarkAqua];
    } else {
        return NO;
    }
}

You would use it like appearanceIsDark(someView.effectiveAppearance) since the appearance of a specific view may be different than that of another view if you explicitly set someView.appearance.

You could also create a category on NSAppearance and add a - (BOOL)isDark method to get someView.effectiveAppearance.isDark (better chose a name that is unlikely to be used by Apple in the future, e.g. by adding a vendor prefix).

like image 55
DarkDust Avatar answered Oct 20 '22 13:10

DarkDust


I have used the current appearance checking if the system is 10.14

+ (BOOL)isDarkMode {
    NSAppearance *appearance = NSAppearance.currentAppearance;
    if (@available(*, macOS 10.14)) {
        return appearance.name == NSAppearanceNameDarkAqua;
    }

    return NO;
}

And to detect the change of mode in a view the methods are:

- (void)updateLayer;
- (void)drawRect:(NSRect)dirtyRect;
- (void)layout;
- (void)updateConstraints;

And to detect the change of mode in a view controller the methods are:

- (void)updateViewConstraints;
- (void)viewWillLayout;
- (void)viewDidLayout;

Using notification:

// Monitor menu/dock theme changes...
[NSDistributedNotificationCenter.defaultCenter addObserver:self selector:@selector(themeChanged:) name:@"AppleInterfaceThemeChangedNotification" object: nil];

-(void)themeChanged:(NSNotification *) notification {
    NSLog (@"%@", notification);
}

For more information Dark Mode Documentation

like image 21
93sauu Avatar answered Oct 20 '22 15:10

93sauu