Skip to content

Commit 0391266

Browse files
committed
Update platform detection to include arch.
This is for M1 mac support. Future commit :^)
1 parent ce59e79 commit 0391266

File tree

6 files changed

+87
-39
lines changed

6 files changed

+87
-39
lines changed

src/main/java/dev/webview/Platform.java

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

src/main/java/dev/webview/Webview.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,13 @@
2323
import co.casterlabs.rakurai.json.element.JsonElement;
2424
import co.casterlabs.rakurai.json.serialization.JsonParseException;
2525
import dev.webview.WebviewNative.BindCallback;
26+
import dev.webview.platform.OperatingSystem;
27+
import dev.webview.platform.Platform;
2628
import lombok.NonNull;
2729

2830
public class Webview implements Closeable, Runnable {
2931
private static final WebviewNative N;
3032

31-
public static final Platform PLATFORM = Platform.get();
32-
3333
static {
3434
WebviewNative.runSetup();
3535
N = Native.load("webview", WebviewNative.class);
@@ -79,7 +79,7 @@ private void updateSize() {
7979
// There is a random margin on Windows that isn't visible, so we must
8080
// compensate.
8181
// TODO figure out why this is caused.
82-
if (PLATFORM == Platform.WINDOWS) {
82+
if (Platform.os == OperatingSystem.WINDOWS) {
8383
width -= 16;
8484
height -= 39;
8585
}

src/main/java/dev/webview/WebviewNative.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ public interface WebviewNative extends Library {
1919
static void runSetup() {
2020
String[] libraries = null;
2121

22-
switch (Webview.PLATFORM) {
22+
switch (Platform.os) {
2323
case LINUX: {
2424
libraries = new String[] {
2525
"libwebview.so"
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package dev.webview.platform;
2+
3+
import java.util.regex.Pattern;
4+
5+
import lombok.AllArgsConstructor;
6+
7+
@AllArgsConstructor
8+
public enum Arch {
9+
// @formatter:off
10+
AMD64 ("amd64", "amd64|x86_64"),
11+
X86 ("x86", "x86|i386|i486|i586|i686|i786"),
12+
AARCH64 ("aarch64", "arm64|aarch64"),
13+
ARM32 ("arm32", "arm");
14+
// @formatter:on
15+
16+
private String str;
17+
private String regex;
18+
19+
static Arch get() {
20+
String osArch = System.getProperty("os.arch").toLowerCase();
21+
22+
for (Arch arch : values()) {
23+
if (Pattern.compile(arch.regex).matcher(osArch).find()) {
24+
return arch;
25+
}
26+
}
27+
28+
throw new UnsupportedOperationException("Unknown cpu arch: " + osArch);
29+
}
30+
31+
@Override
32+
public String toString() {
33+
return this.str;
34+
}
35+
36+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package dev.webview.platform;
2+
3+
import java.util.regex.Pattern;
4+
5+
import lombok.AllArgsConstructor;
6+
7+
@AllArgsConstructor
8+
public enum OperatingSystem {
9+
// @formatter:off
10+
MACOSX ("macOS", "mac|darwin"),
11+
LINUX ("Linux", "nux"),
12+
WINDOWS ("Windows", "win");
13+
// @formatter:on
14+
15+
private String str;
16+
private String regex;
17+
18+
static OperatingSystem get() {
19+
String osName = System.getProperty("os.name").toLowerCase();
20+
21+
for (OperatingSystem os : values()) {
22+
if (Pattern.compile(os.regex).matcher(osName).find()) {
23+
return os;
24+
}
25+
}
26+
27+
throw new UnsupportedOperationException("Unknown operating system: " + osName);
28+
}
29+
30+
@Override
31+
public String toString() {
32+
return this.str;
33+
}
34+
35+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package dev.webview.platform;
2+
3+
public class Platform {
4+
public static final Arch arch;
5+
public static final OperatingSystem os;
6+
7+
static {
8+
arch = Arch.get();
9+
os = OperatingSystem.get();
10+
}
11+
12+
}

0 commit comments

Comments
 (0)