From 669705e539ea68d5a080b9f2111ce15b9fff9aaf Mon Sep 17 00:00:00 2001 From: Artem Bochkarev Date: Sun, 7 Oct 2018 16:15:45 +0700 Subject: [PATCH] nsdefaults: use awt.Color unstead of strings, impl getHighlightColor --- .../ui/mac/foundation/NSDefaults.java | 95 +++++++++++++++---- 1 file changed, 76 insertions(+), 19 deletions(-) diff --git a/platform/util/src/com/intellij/ui/mac/foundation/NSDefaults.java b/platform/util/src/com/intellij/ui/mac/foundation/NSDefaults.java index e5e2cabfeb8b..b83617d0fd75 100644 --- a/platform/util/src/com/intellij/ui/mac/foundation/NSDefaults.java +++ b/platform/util/src/com/intellij/ui/mac/foundation/NSDefaults.java @@ -4,13 +4,21 @@ package com.intellij.ui.mac.foundation; import com.intellij.openapi.diagnostic.Logger; import com.intellij.openapi.util.SystemInfo; import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; +import java.awt.*; import java.util.ArrayList; +import java.util.HashMap; import java.util.List; +import java.util.Map; public class NSDefaults { private static final Logger LOG = Logger.getInstance(NSDefaults.class); + private static final Map ourAccentColId2Color; + private static final Color ourDefaultAccentColor = new Color(46, 125, 246); // blue + private static final Color ourDefaultHighlightColor = new Color(185, 215, 251); // blue + // NOTE: skip call of Foundation.invoke(myDefaults, "synchronize") (when read settings) // It waits for any pending asynchronous updates to the defaults database and returns; this method is unnecessary and shouldn't be used. @@ -18,6 +26,17 @@ public class NSDefaults { public static final String ourTouchBarNode = "PresentationModePerApp"; public static final String ourTouchBarShowFnValue = "functionKeys"; + static { + ourAccentColId2Color = new HashMap(); + ourAccentColId2Color.put(5, new Color(138, 69, 146)); // purple + ourAccentColId2Color.put(6, new Color(229, 94, 156)); // pink + ourAccentColId2Color.put(0, new Color(207, 71, 69)); // red + ourAccentColId2Color.put(1, new Color(232, 135, 58)); // orange + ourAccentColId2Color.put(2, new Color(243, 185, 75)); // yellow + ourAccentColId2Color.put(3, new Color(120, 183, 86)); // green + ourAccentColId2Color.put(-1, new Color(152, 152, 152)); // graphite + } + private static class Path { private final @NotNull ArrayList myPath = new ArrayList(); @@ -281,28 +300,46 @@ public class NSDefaults { } } - public static String getAccentColor() { + public static @NotNull Color getHighlightColor() { + // Blue : default (key doesn't exist) + // 0.968627 0.831373 1.000000 Purple + // 1.000000 0.749020 0.823529 Pink + // 1.000000 0.733333 0.721569 Red + // 1.000000 0.874510 0.701961 Orange + // 1.000000 0.937255 0.690196 Yellow + // 0.752941 0.964706 0.678431 Green + // 0.847059 0.847059 0.862745 Graphite + + final String sval = _getHighlightColor(); + if (sval == null) + return ourDefaultHighlightColor; + + final String[] spl = sval.split(" "); + if (spl != null && spl.length >= 3) { + final float r,g,b; + try { + r = Float.parseFloat(spl[0]); + g = Float.parseFloat(spl[1]); + b = Float.parseFloat(spl[2]); + return new Color(r, g, b); + } catch (NumberFormatException nfe) { + LOG.error(nfe); + } catch (NullPointerException npe) { + LOG.error(npe); + } + } + + LOG.error("incorrect format of registry value 'AppleHighlightColor': " + sval); + return ourDefaultHighlightColor; + } + + public static @Nullable Color getAccentColor() { if (!SystemInfo.isMac || !SystemInfo.isOsVersionAtLeast("10.14")) return null; - // Blue : default (key doesn't exist) - // Purple : 5 - // Pink : 6 - // Red : 0 - // Orange : 1 - // Yellow : 2 - // Green : 3 - // Graphite: -1 + final int nval = _getAccentColor(); - switch (nval) { - case 5: return "purple"; - case 6: return "pink"; - case 0: return "red"; - case 1: return "orange"; - case 2: return "yellow"; - case 3: return "green"; - case -1: return "graphite"; - default: return "blue"; - } + final Color result = ourAccentColId2Color.get(nval); + return result == null ? ourDefaultAccentColor : result; } private static int _getAccentColor() { @@ -327,6 +364,26 @@ public class NSDefaults { } } + private static String _getHighlightColor() { + final Foundation.NSAutoreleasePool pool = new Foundation.NSAutoreleasePool(); + try { + final ID defaults = Foundation.invoke("NSUserDefaults", "standardUserDefaults"); + if (defaults == null || defaults.equals(ID.NIL)) + return null; + final ID domObj = Foundation.invoke(defaults, "persistentDomainForName:", Foundation.nsString("Apple Global Domain")); + if (domObj == null || domObj.equals(ID.NIL)) + return null; + + final ID strObj = Foundation.invoke(domObj, "objectForKey:", Foundation.nsString("AppleHighlightColor")); + if (strObj == null || strObj.equals(ID.NIL)) + return null; + + return Foundation.toStringViaUTF8(strObj); + } finally { + pool.drain(); + } + } + // for debug private List _listAllKeys() { List res = new ArrayList(100);