#182 change app appearance to the same as system on Mac OS

It doesn't happen properly automatically - needs to be done after the window is created, only then SWT color reloading works properly
This commit is contained in:
Anton Keks 2020-03-14 12:42:48 -07:00
parent ea5cc4b6a0
commit 87e4e08197

View File

@ -2,16 +2,19 @@ package net.azib.ipscan.gui;
import net.azib.ipscan.config.LoggerFactory;
import net.azib.ipscan.config.MainComponent;
import net.azib.ipscan.config.Platform;
import net.azib.ipscan.config.Version;
import net.azib.ipscan.core.UserErrorException;
import net.azib.ipscan.util.GoogleAnalytics;
import org.eclipse.swt.SWT;
import org.eclipse.swt.SWTError;
import org.eclipse.swt.SWTException;
import org.eclipse.swt.internal.cocoa.OS;
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;
@ -42,6 +45,8 @@ public class GUI implements AutoCloseable {
public void showMainWindow(MainComponent mainComponent) {
// create the main window using dependency injection
mainWindow = mainComponent.createMainWindow();
if (Platform.MAC_OS) setMacDarkAppearanceIfNeeded();
LOG.fine("Main window created: " + (System.currentTimeMillis() - startTime));
while (!mainWindow.isDisposed()) {
@ -60,6 +65,20 @@ public class GUI implements AutoCloseable {
}
}
private void setMacDarkAppearanceIfNeeded() {
try {
// changing the appearance works only after the shell has been created
OS.setTheme(OS.isSystemDarkAppearance());
// workaround for a bug in SWT: colors need to be reinited after changing the appearance
Method initColor = display.getClass().getDeclaredMethod("initColors");
initColor.setAccessible(true);
initColor.invoke(display);
}
catch (Exception e) {
e.printStackTrace();
}
}
public void showMessage(int flags, String title, String localizedMessage) {
Shell parent = Display.getDefault().getActiveShell();
if (parent == null) parent = mainWindow.getShell();