Bug 562330 - [Dark Theme][Windows 10] Dark themed scrollbar breaks styling of Tree/Table selection color

Change-Id: I6bb4717c4a6923094903a6c54611bf40666791eb
Signed-off-by: Alexandr Miloslavskiy <alexandr.miloslavskiy@syntevo.com>
diff --git a/bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/widgets/Display.java b/bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/widgets/Display.java
index 59061d9..c150d49 100644
--- a/bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/widgets/Display.java
+++ b/bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/widgets/Display.java
@@ -4498,12 +4498,12 @@
 	return errorHandler;
 }
 
+boolean isDarkModeExplorerTheme() {
+	return Boolean.valueOf(System.getProperty(ENABLE_DARK_SCROLLBARS));
+}
+
 char[] getExplorerTheme() {
-	String enableDarkScrollBar = System.getProperty(ENABLE_DARK_SCROLLBARS);
-	if (enableDarkScrollBar != null && Boolean.valueOf(enableDarkScrollBar)) {
-		return DARKMODE_EXPLORER;
-	}
-	return EXPLORER;
+	return isDarkModeExplorerTheme() ? DARKMODE_EXPLORER : EXPLORER;
 }
 
 int shiftedKey (int key) {
diff --git a/bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/widgets/Table.java b/bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/widgets/Table.java
index 2f7f9bc..007e4d2 100644
--- a/bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/widgets/Table.java
+++ b/bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/widgets/Table.java
@@ -86,7 +86,7 @@
 	boolean [] columnVisible;
 	long headerToolTipHandle, hwndHeader;
 	boolean ignoreCustomDraw, ignoreDrawForeground, ignoreDrawBackground, ignoreDrawFocus, ignoreDrawSelection, ignoreDrawHot;
-	boolean customDraw, dragStarted, explorerTheme, firstColumnImage, fixScrollWidth, tipRequested, wasSelected, wasResized, painted;
+	boolean customDraw, dragStarted, explorerTheme, darkExplorerTheme, firstColumnImage, fixScrollWidth, tipRequested, wasSelected, wasResized, painted;
 	boolean ignoreActivate, ignoreSelect, ignoreShrink, ignoreResize, ignoreColumnMove, ignoreColumnResize, fullRowSelect, settingItemHeight;
 	boolean headerItemDragging;
 	int itemHeight, lastIndexOf, lastWidth, sortDirection, resizeCount, selectionForeground, hotIndex;
@@ -1498,6 +1498,7 @@
 	/* Use the Explorer theme */
 	if (OS.IsAppThemed ()) {
 		explorerTheme = true;
+		darkExplorerTheme = display.isDarkModeExplorerTheme();
 		OS.SetWindowTheme (handle, display.getExplorerTheme(), null);
 	}
 
@@ -3539,8 +3540,24 @@
 				focusRect = textRect;
 			}
 		}
+
+		// Draw selection background
 		if (explorerTheme) {
-			if (!ignoreDrawHot || drawDrophilited || (!ignoreDrawSelection && clrSelectionBk != -1)) {
+			boolean backgroundWanted = !ignoreDrawHot || drawDrophilited || (!ignoreDrawSelection && clrSelectionBk != -1);
+
+			/*
+			 * With 'DarkMode_Explorer' theme, Windows draws selection background in "paint"
+			 * step instead of "erase" step. In this case, the code below is not needed,
+			 * because Windows background drawing happens after rather then before SWT.EraseItem.
+			 * On top of this, windows draws selection in a smaller rect and with a different color.
+			 * Proceeding with code below will result in two selection backgrounds visible.
+			 * Finally, as of Windows 10 version 1909, 'DarkMode_Explorer::LISTVIEW' is not yet
+			 * present, and 'OpenThemeData("LISTVIEW")' opens wrong theme data.
+			 */
+			if (darkExplorerTheme)
+				backgroundWanted = false;
+
+			if (backgroundWanted) {
 				RECT pClipRect = new RECT ();
 				OS.SetRect (pClipRect, nmcd.left, nmcd.top, nmcd.right, nmcd.bottom);
 				RECT rect = new RECT ();