#231 do not change to dark mode programmatically, which may result in distorted colors due to SWT not being able to update the colors correctly. Tell macOS to start the app in dark more automatically using Info.plist - this now works with updated SWT

This commit is contained in:
Anton Keks 2020-05-17 13:31:26 -07:00
parent 0f04db0658
commit 1300d0d49e
2 changed files with 2 additions and 33 deletions

View File

@ -24,5 +24,7 @@
<string>NSApplication</string>
<key>NSHighResolutionCapable</key>
<true/>
<key>NSRequiresAquaSystemAppearance</key>
<false/>
</dict>
</plist>

View File

@ -1,7 +1,6 @@
package net.azib.ipscan.gui;
import net.azib.ipscan.config.LoggerFactory;
import net.azib.ipscan.config.Platform;
import net.azib.ipscan.config.Version;
import net.azib.ipscan.core.UserErrorException;
import net.azib.ipscan.di.Injector;
@ -13,7 +12,6 @@ import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.MessageBox;
import org.eclipse.swt.widgets.Shell;
import java.lang.reflect.Method;
import java.util.logging.Level;
import java.util.logging.Logger;
@ -43,7 +41,6 @@ public class GUI implements AutoCloseable {
public void showMainWindow(Injector injector) {
mainWindow = injector.require(MainWindow.class);
if (Platform.MAC_OS) setMacDarkAppearanceIfNeeded();
LOG.fine("Main window created: " + (System.currentTimeMillis() - startTime));
@ -63,36 +60,6 @@ public class GUI implements AutoCloseable {
}
}
private void setMacDarkAppearanceIfNeeded() {
try {
// changing the appearance works only after the shell has been created
Class os = Class.forName("org.eclipse.swt.internal.cocoa.OS");
Boolean isDarkMode = (Boolean) os.getMethod("isSystemDarkAppearance").invoke(null);
Boolean isAppDarkAppearance = (Boolean) os.getMethod("isAppDarkAppearance").invoke(null);
LOG.info("Dark appearance flags before: " + isDarkMode + ", " + isAppDarkAppearance);
if (isDarkMode && !isAppDarkAppearance) {
os.getMethod("setTheme", boolean.class).invoke(null, true);
isDarkMode = (Boolean) os.getMethod("isSystemDarkAppearance").invoke(null);
isAppDarkAppearance = (Boolean) os.getMethod("isAppDarkAppearance").invoke(null);
LOG.info("Dark appearance flags after: " + isDarkMode + ", " + isAppDarkAppearance);
if (isAppDarkAppearance) {
// workaround for a bug in SWT: colors need to be reinited after changing the appearance
Method initColors = display.getClass().getDeclaredMethod("initColors");
initColors.setAccessible(true);
initColors.invoke(display);
LOG.info("initColors called");
}
else {
os.getMethod("setTheme", boolean.class).invoke(null, false);
LOG.info("Dark appearance reset back because it didn't work");
}
}
}
catch (Exception e) {
e.printStackTrace();
}
}
public void showMessage(int flags, String title, String localizedMessage) {
Shell parent = Display.getDefault().getActiveShell();
if (parent == null && mainWindow != null) parent = mainWindow.getShell();