Skip to content

Commit b4a6c73

Browse files
committed
Theme for macOS, PanelTheme #153 #161
1 parent c2557ee commit b4a6c73

File tree

10 files changed

+99
-54
lines changed

10 files changed

+99
-54
lines changed

README.md

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,13 @@ Alpha. Expect API breakages.
9090
| runOnUIThread ||| [#113](https://github.com/HumbleUI/JWM/issues/113) |
9191
| terminate ||||
9292
| Show notification ||||
93-
| System Theme ||||
93+
94+
### Theme
95+
| | Windows | macOS | X11 |
96+
|-------------------|---------|-------|-----|
97+
| isHighContrast ||||
98+
| isDark | [#161](https://github.com/HumbleUI/JWM/issues/161) |||
99+
| isInvereted | [#161](https://github.com/HumbleUI/JWM/issues/161) |||
94100

95101
### Window
96102

examples/java/Example.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ public class Example implements Consumer<Event> {
2626
public PanelMouseCursors panelMouseCursors;
2727
public PanelRendering panelRendering;
2828
public PanelEvents panelEvents;
29+
public PanelTheme panelTheme;
2930

3031
public Window window;
3132

@@ -44,6 +45,7 @@ public Example() {
4445
panelMouseCursors = new PanelMouseCursors(window);
4546
panelRendering = new PanelRendering(window);
4647
panelEvents = new PanelEvents(window);
48+
panelTheme = new PanelTheme(window);
4749

4850
var scale = window.getScreen().getScale();
4951
int count = App._windows.size() - 1;
@@ -112,7 +114,8 @@ public void paint(String reason) {
112114
panelRendering.paint (canvas, PADDING + (panelWidth + PADDING) * 2, PADDING + (panelHeight + PADDING) * 1, panelWidth, panelHeight, scale);
113115

114116
// Third row
115-
panelEvents.paint (canvas, PADDING + (panelWidth + PADDING) * 0, PADDING + (panelHeight + PADDING) * 2, panelWidth * 3 + PADDING * 2, panelHeight, scale);
117+
panelEvents.paint (canvas, PADDING + (panelWidth + PADDING) * 0, PADDING + (panelHeight + PADDING) * 2, panelWidth * 2 + PADDING, panelHeight, scale);
118+
panelTheme.paint (canvas, PADDING + (panelWidth + PADDING) * 2, PADDING + (panelHeight + PADDING) * 2, panelWidth, panelHeight, scale);
116119

117120
// Colored bars
118121
try (var paint = new Paint()) {

examples/java/PanelTheme.java

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package io.github.humbleui.jwm.examples;
2+
3+
import io.github.humbleui.jwm.*;
4+
import org.jetbrains.skija.*;
5+
6+
public class PanelTheme extends Panel {
7+
8+
public PanelTheme(Window window) {
9+
super(window);
10+
}
11+
12+
@Override
13+
public void paintImpl(Canvas canvas, int width, int height, float scale) {
14+
try (var paint = new Paint()) {
15+
paint.setColor(0xFFFFFFFF);
16+
canvas.drawString("isHighContrast", Example.PADDING, Example.PADDING * 2, Example.FONT12, paint);
17+
canvas.drawString("" + Theme.isHighContrast(), width / 2 + Example.PADDING / 2, Example.PADDING * 2, Example.FONT12, paint);
18+
19+
canvas.drawString("isDark", Example.PADDING, Example.PADDING * 4, Example.FONT12, paint);
20+
canvas.drawString("" + Theme.isDark(), width / 2 + Example.PADDING / 2, Example.PADDING * 4, Example.FONT12, paint);
21+
22+
canvas.drawString("isInverted", Example.PADDING, Example.PADDING * 6, Example.FONT12, paint);
23+
canvas.drawString("" + Theme.isInverted(), width / 2 + Example.PADDING / 2, Example.PADDING * 6, Example.FONT12, paint);
24+
}
25+
}
26+
}

macos/cc/ThemeMac.mm

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
#import <Cocoa/Cocoa.h>
2+
#include <jni.h>
3+
4+
const CFStringRef WhiteOnBlack = CFSTR("whiteOnBlack");
5+
const CFStringRef UniversalAccessDomain = CFSTR("com.apple.universalaccess");
6+
7+
extern "C" JNIEXPORT bool JNICALL Java_io_github_humbleui_jwm_Theme__1nIsHighContrast
8+
(JNIEnv* env, jclass jclass) {
9+
NSWorkspace* workspace = [NSWorkspace sharedWorkspace];
10+
if ([workspace respondsToSelector:@selector (accessibilityDisplayShouldIncreaseContrast)]) {
11+
return workspace.accessibilityDisplayShouldIncreaseContrast;
12+
}
13+
return false;
14+
}
15+
16+
extern "C" JNIEXPORT bool JNICALL Java_io_github_humbleui_jwm_Theme__1nIsDark
17+
(JNIEnv* env, jclass jclass) {
18+
if (@available(macOS 10.14, *)) {
19+
NSAppearanceName appearance = [[NSApp effectiveAppearance] bestMatchFromAppearancesWithNames:@[
20+
NSAppearanceNameAqua, NSAppearanceNameDarkAqua
21+
]];
22+
return [appearance isEqual:NSAppearanceNameDarkAqua];
23+
}
24+
return false;
25+
}
26+
27+
extern "C" JNIEXPORT bool JNICALL Java_io_github_humbleui_jwm_Theme__1nIsInverted
28+
(JNIEnv* env, jclass jclass) {
29+
CFPreferencesAppSynchronize(UniversalAccessDomain);
30+
Boolean keyExistsAndHasValidFormat = false;
31+
Boolean is_inverted = CFPreferencesGetAppBooleanValue(WhiteOnBlack, UniversalAccessDomain, &keyExistsAndHasValidFormat);
32+
if (!keyExistsAndHasValidFormat)
33+
return false;
34+
return is_inverted;
35+
}

shared/cc/Theme.hh

Lines changed: 0 additions & 8 deletions
This file was deleted.

shared/java/Theme.java

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,21 +4,33 @@
44
import io.github.humbleui.jwm.impl.*;
55

66
public class Theme {
7-
87
/**
9-
* <p>>Check whether OS currently uses high contrast mode.</p>
8+
* <p>Check whether OS currently uses high contrast mode.</p>
109
*
11-
* @return bool;
10+
* @return bool on Windows macOS, false otherwise
1211
*/
1312
public static boolean isHighContrast() {
14-
assert _onUIThread();
13+
assert App._onUIThread();
14+
if (Platform.CURRENT != Platform.WINDOWS && Platform.CURRENT != Platform.MACOS)
15+
return false;
1516
return _nIsHighContrast();
1617
}
1718

19+
public static boolean isDark() {
20+
assert App._onUIThread();
21+
if (Platform.CURRENT != Platform.MACOS)
22+
return false;
23+
return _nIsDark();
24+
}
1825

19-
@ApiStatus.Internal public static boolean _onUIThread() {
20-
return App._onUIThread();
26+
public static boolean isInverted() {
27+
assert App._onUIThread();
28+
if (Platform.CURRENT != Platform.MACOS)
29+
return false;
30+
return _nIsInverted();
2131
}
2232

2333
@ApiStatus.Internal public static native boolean _nIsHighContrast();
34+
@ApiStatus.Internal public static native boolean _nIsDark();
35+
@ApiStatus.Internal public static native boolean _nIsInverted();
2436
}

windows/CMakeLists.txt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,6 @@ set(JWM_WINDOWS_SOURCES_CXX
4646
cc/LayerWGL.cc
4747
cc/LayerWGL.hh
4848
cc/ThemeWin32.cc
49-
cc/ThemeWin32.hh
5049
cc/PlatformWin32.hh
5150
cc/ScreenWin32.cc
5251
cc/ScreenWin32.hh

windows/cc/AppWin32.hh

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
#include <D3D12/DX12Common.hh>
66
#include <ContextWGL.hh>
77
#include <ScreenWin32.hh>
8-
#include <ThemeWin32.hh>
98
#include <jni.h>
109

1110
namespace jwm {
@@ -26,13 +25,11 @@ namespace jwm {
2625
ContextWGL& getContextWGL() { return _wglContext; }
2726
DX12Common& getDx12Common() { return _dx12common; }
2827
JNIEnv* getJniEnv() const { return _jniEnv; }
29-
ThemeWin32& getTheme() { return _theme; }
3028

3129
private:
3230
std::vector<ScreenWin32> _screens;
3331
WindowManagerWin32 _windowManager;
3432
ClipboardWin32 _clipboard;
35-
ThemeWin32 _theme;
3633
ContextWGL _wglContext;
3734
DX12Common _dx12common;
3835
JNIEnv* _jniEnv;

windows/cc/ThemeWin32.cc

Lines changed: 9 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
#include "ThemeWin32.hh"
2-
31
#include <Uxtheme.h>
42
#include <impl/Library.hh>
53
#include <AppWin32.hh>
@@ -8,29 +6,23 @@
86
#include <stdio.h>
97
#include <Log.hh>
108
#include <PlatformWin32.hh>
11-
#include "Theme.hh"
129

10+
using namespace jwm;
11+
12+
// JNI
1313

14-
bool jwm::ThemeWin32::isHighContrast() {
14+
extern "C" JNIEXPORT bool JNICALL Java_io_github_humbleui_jwm_Theme__1nIsHighContrast
15+
(JNIEnv* env, jclass jclass) {
1516
HIGHCONTRASTA highContrast;
1617
highContrast.cbSize = sizeof(HIGHCONTRASTA);
1718
highContrast.dwFlags = 0;
1819
highContrast.lpszDefaultScheme = nullptr;
1920
bool isOk = SystemParametersInfoA(SPI_GETHIGHCONTRAST, 0, &highContrast, 0);
2021
if (!isOk) {
21-
JWM_VERBOSE("Failed to get SystemParametersInfoA for high contrast");
22+
JWM_LOG("Failed to get SystemParametersInfoA for high contrast");
2223
return false;
2324
}
24-
JWM_VERBOSE("is HighContrast? '"
25-
<< ((HCF_HIGHCONTRASTON & highContrast.dwFlags) == 1) << "'");
26-
return (HCF_HIGHCONTRASTON & highContrast.dwFlags) == 1;
27-
}
28-
29-
// JNI
30-
31-
extern "C" JNIEXPORT bool JNICALL Java_io_github_humbleui_jwm_Theme__1nIsHighContrast
32-
(JNIEnv* env, jclass jclass) {
33-
jwm::AppWin32& app = jwm::AppWin32::getInstance();
34-
jwm::ThemeWin32& theme = app.getTheme();
35-
return theme.isHighContrast();
25+
bool result = (HCF_HIGHCONTRASTON & highContrast.dwFlags) == 1;
26+
JWM_VERBOSE("is HighContrast? '" << result << "'");
27+
return result;
3628
}

windows/cc/ThemeWin32.hh

Lines changed: 0 additions & 17 deletions
This file was deleted.

0 commit comments

Comments
 (0)