Skip to content

Commit

Permalink
Theme for macOS, PanelTheme #153 #161
Browse files Browse the repository at this point in the history
  • Loading branch information
tonsky committed Oct 4, 2021
1 parent c2557ee commit b4a6c73
Show file tree
Hide file tree
Showing 10 changed files with 99 additions and 54 deletions.
8 changes: 7 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,13 @@ Alpha. Expect API breakages.
| runOnUIThread ||| [#113](https://github.com/HumbleUI/JWM/issues/113) |
| terminate ||||
| Show notification ||||
| System Theme ||||

### Theme
| | Windows | macOS | X11 |
|-------------------|---------|-------|-----|
| isHighContrast ||||
| isDark | [#161](https://github.com/HumbleUI/JWM/issues/161) |||
| isInvereted | [#161](https://github.com/HumbleUI/JWM/issues/161) |||

### Window

Expand Down
5 changes: 4 additions & 1 deletion examples/java/Example.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ public class Example implements Consumer<Event> {
public PanelMouseCursors panelMouseCursors;
public PanelRendering panelRendering;
public PanelEvents panelEvents;
public PanelTheme panelTheme;

public Window window;

Expand All @@ -44,6 +45,7 @@ public Example() {
panelMouseCursors = new PanelMouseCursors(window);
panelRendering = new PanelRendering(window);
panelEvents = new PanelEvents(window);
panelTheme = new PanelTheme(window);

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

// Third row
panelEvents.paint (canvas, PADDING + (panelWidth + PADDING) * 0, PADDING + (panelHeight + PADDING) * 2, panelWidth * 3 + PADDING * 2, panelHeight, scale);
panelEvents.paint (canvas, PADDING + (panelWidth + PADDING) * 0, PADDING + (panelHeight + PADDING) * 2, panelWidth * 2 + PADDING, panelHeight, scale);
panelTheme.paint (canvas, PADDING + (panelWidth + PADDING) * 2, PADDING + (panelHeight + PADDING) * 2, panelWidth, panelHeight, scale);

// Colored bars
try (var paint = new Paint()) {
Expand Down
26 changes: 26 additions & 0 deletions examples/java/PanelTheme.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package io.github.humbleui.jwm.examples;

import io.github.humbleui.jwm.*;
import org.jetbrains.skija.*;

public class PanelTheme extends Panel {

public PanelTheme(Window window) {
super(window);
}

@Override
public void paintImpl(Canvas canvas, int width, int height, float scale) {
try (var paint = new Paint()) {
paint.setColor(0xFFFFFFFF);
canvas.drawString("isHighContrast", Example.PADDING, Example.PADDING * 2, Example.FONT12, paint);
canvas.drawString("" + Theme.isHighContrast(), width / 2 + Example.PADDING / 2, Example.PADDING * 2, Example.FONT12, paint);

canvas.drawString("isDark", Example.PADDING, Example.PADDING * 4, Example.FONT12, paint);
canvas.drawString("" + Theme.isDark(), width / 2 + Example.PADDING / 2, Example.PADDING * 4, Example.FONT12, paint);

canvas.drawString("isInverted", Example.PADDING, Example.PADDING * 6, Example.FONT12, paint);
canvas.drawString("" + Theme.isInverted(), width / 2 + Example.PADDING / 2, Example.PADDING * 6, Example.FONT12, paint);
}
}
}
35 changes: 35 additions & 0 deletions macos/cc/ThemeMac.mm
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
#import <Cocoa/Cocoa.h>
#include <jni.h>

const CFStringRef WhiteOnBlack = CFSTR("whiteOnBlack");
const CFStringRef UniversalAccessDomain = CFSTR("com.apple.universalaccess");

extern "C" JNIEXPORT bool JNICALL Java_io_github_humbleui_jwm_Theme__1nIsHighContrast
(JNIEnv* env, jclass jclass) {
NSWorkspace* workspace = [NSWorkspace sharedWorkspace];
if ([workspace respondsToSelector:@selector (accessibilityDisplayShouldIncreaseContrast)]) {
return workspace.accessibilityDisplayShouldIncreaseContrast;
}
return false;
}

extern "C" JNIEXPORT bool JNICALL Java_io_github_humbleui_jwm_Theme__1nIsDark
(JNIEnv* env, jclass jclass) {
if (@available(macOS 10.14, *)) {
NSAppearanceName appearance = [[NSApp effectiveAppearance] bestMatchFromAppearancesWithNames:@[
NSAppearanceNameAqua, NSAppearanceNameDarkAqua
]];
return [appearance isEqual:NSAppearanceNameDarkAqua];
}
return false;
}

extern "C" JNIEXPORT bool JNICALL Java_io_github_humbleui_jwm_Theme__1nIsInverted
(JNIEnv* env, jclass jclass) {
CFPreferencesAppSynchronize(UniversalAccessDomain);
Boolean keyExistsAndHasValidFormat = false;
Boolean is_inverted = CFPreferencesGetAppBooleanValue(WhiteOnBlack, UniversalAccessDomain, &keyExistsAndHasValidFormat);
if (!keyExistsAndHasValidFormat)
return false;
return is_inverted;
}
8 changes: 0 additions & 8 deletions shared/cc/Theme.hh

This file was deleted.

24 changes: 18 additions & 6 deletions shared/java/Theme.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,33 @@
import io.github.humbleui.jwm.impl.*;

public class Theme {

/**
* <p>>Check whether OS currently uses high contrast mode.</p>
* <p>Check whether OS currently uses high contrast mode.</p>
*
* @return bool;
* @return bool on Windows macOS, false otherwise
*/
public static boolean isHighContrast() {
assert _onUIThread();
assert App._onUIThread();
if (Platform.CURRENT != Platform.WINDOWS && Platform.CURRENT != Platform.MACOS)
return false;
return _nIsHighContrast();
}

public static boolean isDark() {
assert App._onUIThread();
if (Platform.CURRENT != Platform.MACOS)
return false;
return _nIsDark();
}

@ApiStatus.Internal public static boolean _onUIThread() {
return App._onUIThread();
public static boolean isInverted() {
assert App._onUIThread();
if (Platform.CURRENT != Platform.MACOS)
return false;
return _nIsInverted();
}

@ApiStatus.Internal public static native boolean _nIsHighContrast();
@ApiStatus.Internal public static native boolean _nIsDark();
@ApiStatus.Internal public static native boolean _nIsInverted();
}
1 change: 0 additions & 1 deletion windows/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@ set(JWM_WINDOWS_SOURCES_CXX
cc/LayerWGL.cc
cc/LayerWGL.hh
cc/ThemeWin32.cc
cc/ThemeWin32.hh
cc/PlatformWin32.hh
cc/ScreenWin32.cc
cc/ScreenWin32.hh
Expand Down
3 changes: 0 additions & 3 deletions windows/cc/AppWin32.hh
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
#include <D3D12/DX12Common.hh>
#include <ContextWGL.hh>
#include <ScreenWin32.hh>
#include <ThemeWin32.hh>
#include <jni.h>

namespace jwm {
Expand All @@ -26,13 +25,11 @@ namespace jwm {
ContextWGL& getContextWGL() { return _wglContext; }
DX12Common& getDx12Common() { return _dx12common; }
JNIEnv* getJniEnv() const { return _jniEnv; }
ThemeWin32& getTheme() { return _theme; }

private:
std::vector<ScreenWin32> _screens;
WindowManagerWin32 _windowManager;
ClipboardWin32 _clipboard;
ThemeWin32 _theme;
ContextWGL _wglContext;
DX12Common _dx12common;
JNIEnv* _jniEnv;
Expand Down
26 changes: 9 additions & 17 deletions windows/cc/ThemeWin32.cc
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
#include "ThemeWin32.hh"

#include <Uxtheme.h>
#include <impl/Library.hh>
#include <AppWin32.hh>
Expand All @@ -8,29 +6,23 @@
#include <stdio.h>
#include <Log.hh>
#include <PlatformWin32.hh>
#include "Theme.hh"

using namespace jwm;

// JNI

bool jwm::ThemeWin32::isHighContrast() {
extern "C" JNIEXPORT bool JNICALL Java_io_github_humbleui_jwm_Theme__1nIsHighContrast
(JNIEnv* env, jclass jclass) {
HIGHCONTRASTA highContrast;
highContrast.cbSize = sizeof(HIGHCONTRASTA);
highContrast.dwFlags = 0;
highContrast.lpszDefaultScheme = nullptr;
bool isOk = SystemParametersInfoA(SPI_GETHIGHCONTRAST, 0, &highContrast, 0);
if (!isOk) {
JWM_VERBOSE("Failed to get SystemParametersInfoA for high contrast");
JWM_LOG("Failed to get SystemParametersInfoA for high contrast");
return false;
}
JWM_VERBOSE("is HighContrast? '"
<< ((HCF_HIGHCONTRASTON & highContrast.dwFlags) == 1) << "'");
return (HCF_HIGHCONTRASTON & highContrast.dwFlags) == 1;
}

// JNI

extern "C" JNIEXPORT bool JNICALL Java_io_github_humbleui_jwm_Theme__1nIsHighContrast
(JNIEnv* env, jclass jclass) {
jwm::AppWin32& app = jwm::AppWin32::getInstance();
jwm::ThemeWin32& theme = app.getTheme();
return theme.isHighContrast();
bool result = (HCF_HIGHCONTRASTON & highContrast.dwFlags) == 1;
JWM_VERBOSE("is HighContrast? '" << result << "'");
return result;
}
17 changes: 0 additions & 17 deletions windows/cc/ThemeWin32.hh

This file was deleted.

0 comments on commit b4a6c73

Please sign in to comment.