Skip to content

Commit 4d61d1d

Browse files
committed
Edge: Inherit dark theme from Eclipse
Set ICoreWebView2Profile#put_PreferredColorScheme() based on the Eclipse theme, if this is dark. The same way it is done in Display.setData(USE_DARKMODE_EXPLORER_THEME_KEY, ...) via OS.SetPreferredAppMode(PreferredAppMode_...);
1 parent 03f829a commit 4d61d1d

File tree

6 files changed

+130
-0
lines changed

6 files changed

+130
-0
lines changed

bundles/org.eclipse.swt/Eclipse SWT Browser/win32/org/eclipse/swt/browser/Edge.java

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ class Edge extends WebBrowser {
4040

4141
// Display.getData() keys
4242
static final String APPLOCAL_DIR_KEY = "org.eclipse.swt.internal.win32.appLocalDir";
43+
static final String EDGE_USE_DARK_PREFERED_COLOR_SCHEME = "org.eclipse.swt.internal.win32.Edge.useDarkPreferedColorScheme"; //$NON-NLS-1$
4344

4445
// System.getProperty() keys
4546
static final String BROWSER_DIR_PROP = "org.eclipse.swt.browser.EdgeDir";
@@ -64,6 +65,7 @@ public WebViewEnvironment(ICoreWebView2Environment environment) {
6465
private static Map<Display, WebViewEnvironment> webViewEnvironments = new HashMap<>();
6566
ICoreWebView2Controller controller;
6667
ICoreWebView2Settings settings;
68+
ICoreWebView2Profile profile;
6769
ICoreWebView2Environment2 environment2;
6870
private final WebViewProvider webViewProvider = new WebViewProvider();
6971

@@ -285,6 +287,7 @@ class WebViewProvider {
285287
private CompletableFuture<ICoreWebView2_2> webView_2Future = new CompletableFuture<>();
286288
private CompletableFuture<ICoreWebView2_11> webView_11Future = new CompletableFuture<>();
287289
private CompletableFuture<ICoreWebView2_12> webView_12Future = new CompletableFuture<>();
290+
private CompletableFuture<ICoreWebView2_13> webView_13Future = new CompletableFuture<>();
288291

289292
private CompletableFuture<Void> lastWebViewTask = webViewFuture.thenRun(() -> {});
290293

@@ -295,6 +298,7 @@ ICoreWebView2 initializeWebView(ICoreWebView2Controller controller) {
295298
initializeWebView_2(webView);
296299
initializeWebView_11(webView);
297300
initializeWebView_12(webView);
301+
initializeWebView_13(webView);
298302
webViewFuture.complete(webView);
299303
return webView;
300304
}
@@ -329,6 +333,16 @@ private void initializeWebView_12(ICoreWebView2 webView) {
329333
}
330334
}
331335

336+
private void initializeWebView_13(ICoreWebView2 webView) {
337+
long[] ppv = new long[1];
338+
int hr = webView.QueryInterface(COM.IID_ICoreWebView2_13, ppv);
339+
if (hr == COM.S_OK) {
340+
webView_13Future.complete(new ICoreWebView2_13(ppv[0]));
341+
} else {
342+
webView_13Future.cancel(true);
343+
}
344+
}
345+
332346
ICoreWebView2 getWebView(boolean waitForPendingWebviewTasksToFinish) {
333347
if(waitForPendingWebviewTasksToFinish) {
334348
waitForFutureToFinish(lastWebViewTask);
@@ -372,6 +386,18 @@ boolean isWebView_12Available() {
372386
return !webView_12Future.isCancelled();
373387
}
374388

389+
ICoreWebView2_13 getWebView_13(boolean waitForPendingWebviewTasksToFinish) {
390+
if(waitForPendingWebviewTasksToFinish) {
391+
waitForFutureToFinish(lastWebViewTask);
392+
}
393+
return webView_13Future.join();
394+
}
395+
396+
boolean isWebView_13Available() {
397+
waitForFutureToFinish(webView_13Future);
398+
return !webView_13Future.isCancelled();
399+
}
400+
375401
/*
376402
* Schedule a given runnable in a queue to execute when the webView is free and
377403
* has finished all the pending tasks queued before it.
@@ -547,6 +573,18 @@ void setupBrowser(int hr, long pv) {
547573
settings.put_IsStatusBarEnabled(false);
548574
}
549575

576+
if (webViewProvider.isWebView_13Available()) {
577+
webViewProvider.getWebView_13(false).get_Profile(ppv);
578+
profile = new ICoreWebView2Profile(ppv[0]);
579+
580+
// Dark theme inherited from application theme
581+
if (Boolean.TRUE.equals(browser.getDisplay().getData(EDGE_USE_DARK_PREFERED_COLOR_SCHEME))) {
582+
profile.put_PreferredColorScheme(/* COREWEBVIEW2_PREFERRED_COLOR_SCHEME_DARK */ 2);
583+
} else {
584+
profile.put_PreferredColorScheme(/* COREWEBVIEW2_PREFERRED_COLOR_SCHEME_AUTO */ 0);
585+
}
586+
}
587+
550588
long[] token = new long[1];
551589
IUnknown handler;
552590
handler = newCallback(this::handleCloseRequested);

bundles/org.eclipse.swt/Eclipse SWT PI/win32/org/eclipse/swt/internal/ole/win32/COM.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@ public class COM extends OS {
8888
public static final GUID IID_ICoreWebView2_2 = IIDFromString("{9E8F0CF8-E670-4B5E-B2BC-73E061E3184C}"); //$NON-NLS-1$
8989
public static final GUID IID_ICoreWebView2_11 = IIDFromString("{0BE78E56-C193-4051-B943-23B460C08BDB}"); //$NON-NLS-1$
9090
public static final GUID IID_ICoreWebView2_12 = IIDFromString("{35D69927-BCFA-4566-9349-6B3E0D154CAC}"); //$NON-NLS-1$
91+
public static final GUID IID_ICoreWebView2_13 = IIDFromString("{F75F09A8-667E-4983-88D6-C8773F315E84}"); //$NON-NLS-1$
9192

9293
// IA2 related GUIDS
9394
public static final GUID IIDIAccessible2 = IIDFromString("{E89F726E-C4F4-4c19-BB19-B647D7FA8478}"); //$NON-NLS-1$
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
/*******************************************************************************
2+
* Copyright (c) 2024 SAP SE and others.
3+
*
4+
* This program and the accompanying materials
5+
* are made available under the terms of the Eclipse Public License 2.0
6+
* which accompanies this distribution, and is available at
7+
* https://www.eclipse.org/legal/epl-2.0/
8+
*
9+
* SPDX-License-Identifier: EPL-2.0
10+
*
11+
* Contributors:
12+
* SAP SE - initial implementation
13+
*******************************************************************************/
14+
package org.eclipse.swt.internal.ole.win32;
15+
16+
public class ICoreWebView2Profile extends IUnknown {
17+
18+
public ICoreWebView2Profile(long address) {
19+
super(address);
20+
}
21+
22+
public int get_ProfileName(long[] value) {
23+
return COM.VtblCall(3, address, value);
24+
}
25+
26+
public int get_IsInPrivateModeEnabled(long[] value) {
27+
return COM.VtblCall(4, address, value);
28+
}
29+
30+
public int get_ProfilePath(long[] value) {
31+
return COM.VtblCall(5, address, value);
32+
}
33+
34+
public int get_DefaultDownloadFolderPath(long[] value) {
35+
return COM.VtblCall(6, address, value);
36+
}
37+
38+
public int put_DefaultDownloadFolderPath(long[] value) {
39+
return COM.VtblCall(7, address, value);
40+
}
41+
42+
public int get_PreferredColorScheme(long[] value) {
43+
return COM.VtblCall(8, address, value);
44+
}
45+
46+
public int put_PreferredColorScheme(long value) {
47+
return COM.VtblCall(9, address, value);
48+
}
49+
50+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/*******************************************************************************
2+
* Copyright (c) 2024 SAP SE and others.
3+
*
4+
* This program and the accompanying materials
5+
* are made available under the terms of the Eclipse Public License 2.0
6+
* which accompanies this distribution, and is available at
7+
* https://www.eclipse.org/legal/epl-2.0/
8+
*
9+
* SPDX-License-Identifier: EPL-2.0
10+
*
11+
* Contributors:
12+
* SAP SE - initial implementation
13+
*******************************************************************************/
14+
package org.eclipse.swt.internal.ole.win32;
15+
16+
public class ICoreWebView2_13 extends ICoreWebView2_12 {
17+
18+
public ICoreWebView2_13(long address) {
19+
super(address);
20+
}
21+
22+
public int get_Profile(long[] value) {
23+
return COM.VtblCall(105, address, value);
24+
}
25+
26+
}

bundles/org.eclipse.swt/Eclipse SWT PI/win32/org/eclipse/swt/internal/win32/OS.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2285,6 +2285,7 @@ public static final void setTheme(boolean isDarkTheme) {
22852285
display.setData("org.eclipse.swt.internal.win32.Combo.useDarkTheme", isDarkTheme);
22862286
display.setData("org.eclipse.swt.internal.win32.ProgressBar.useColors", isDarkTheme);
22872287
display.setData("org.eclipse.swt.internal.win32.Text.useDarkThemeIcons", isDarkTheme);
2288+
display.setData("org.eclipse.swt.internal.win32.Edge.useDarkPreferedColorScheme", isDarkTheme);
22882289
}
22892290

22902291
public static final boolean SetWindowText (long hWnd, TCHAR lpString) {

bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/widgets/Display.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -270,6 +270,17 @@ public class Display extends Device implements Executor {
270270
*/
271271
static final String USE_DARKTHEME_TEXT_ICONS = "org.eclipse.swt.internal.win32.Text.useDarkThemeIcons"; //$NON-NLS-1$
272272
boolean textUseDarkthemeIcons = false;
273+
/**
274+
* Use dark prefered color scheme in Edge browser.
275+
* Note:<br>
276+
* <ul>
277+
* <li>When setting this property on the display, it is first AND'ed with !disableCustomThemeTweaks.
278+
* <li>The data is then read from withing Edge.
279+
* <li>This is to avoid adding public methods/members.
280+
* </ul>
281+
* Expects a <code>boolean</code> value.
282+
*/
283+
static final String EDGE_USE_DARK_PREFERED_COLOR_SCHEME = "org.eclipse.swt.internal.win32.Edge.useDarkPreferedColorScheme"; //$NON-NLS-1$
273284

274285
/* Custom icons */
275286
private HashMap<Integer, Long> sizeToSearchIconHandle = new HashMap<>();
@@ -4575,6 +4586,9 @@ public void setData (String key, Object value) {
45754586
case USE_DARKTHEME_TEXT_ICONS:
45764587
textUseDarkthemeIcons = !disableCustomThemeTweaks && _toBoolean(value);
45774588
break;
4589+
case EDGE_USE_DARK_PREFERED_COLOR_SCHEME:
4590+
value = !disableCustomThemeTweaks && _toBoolean(value);
4591+
break;
45784592
}
45794593

45804594
/* Remove the key/value pair */

0 commit comments

Comments
 (0)