Skip to content

Commit da30385

Browse files
committed
Implement everything, even some examples!
1 parent 135a109 commit da30385

File tree

14 files changed

+674
-0
lines changed

14 files changed

+674
-0
lines changed

.gitignore

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# IntelliJ
2+
*.iml
3+
.idea/
4+
5+
# Eclipse
6+
.settings/
7+
.project
8+
.classpath
9+
.metadata
10+
11+
# Common
12+
target/
13+
.DS_Store
14+
__MACOSX
15+
16+
/webview*
17+
/libwebview*
18+
/WebView*

Example.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package dev.webview;
2+
3+
public class Example {
4+
5+
public static void main(String[] args) {
6+
Webview wv = new Webview(); // Can optionally be created with an AWT component to be painted on.
7+
8+
// Calling `await echo(1,2,3)` will return `[1,2,3]`
9+
wv.bind("echo", (arguments) -> {
10+
return arguments;
11+
});
12+
13+
wv.loadURL("https://google.com");
14+
15+
// Run the webview event loop, the webview is fully disposed when this returns.
16+
wv.run();
17+
}
18+
19+
}

README.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# Webview
2+
3+
A (new!) Java port of the [webview project](https://github.com/webview/webview). It uses JNA under the hood to interface with the webview library.
4+
5+
## How to use
6+
7+
1) Include the libary in your project (see the [JitPack page](https://jitpack.io/#Casterlabs/webview)).
8+
2) Copy and run the example in `Example.java`.
9+
3) Profit!
10+
11+
## TODO
12+
13+
Build our own DLLs and whatnot, the current ones are copied from the C# port.

SwingExample.java

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package dev.webview;
2+
3+
import java.awt.BorderLayout;
4+
import java.awt.Component;
5+
import java.awt.event.WindowAdapter;
6+
import java.awt.event.WindowEvent;
7+
8+
import javax.swing.JFrame;
9+
10+
public class SwingExample {
11+
12+
public static void main(String[] args) {
13+
JFrame frame = new JFrame();
14+
15+
frame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
16+
frame.setLayout(new BorderLayout());
17+
18+
// Using createAWT allows you to defer the creation of the webview until the
19+
// canvas is fully renderable.
20+
Component component = Webview.createAWT((wv) -> {
21+
// Calling `await echo(1,2,3)` will return `[1,2,3]`
22+
wv.bind("echo", (arguments) -> {
23+
return arguments;
24+
});
25+
26+
wv.loadURL("https://google.com");
27+
28+
frame.addWindowListener(new WindowAdapter() {
29+
@Override
30+
public void windowClosing(WindowEvent e) {
31+
wv.close();
32+
frame.dispose();
33+
System.exit(0);
34+
}
35+
});
36+
37+
// Run the webview event loop, the webview is fully disposed when this returns.
38+
wv.run();
39+
});
40+
41+
frame.getContentPane().add(component, BorderLayout.CENTER);
42+
43+
frame.setSize(800, 600);
44+
frame.setVisible(true);
45+
}
46+
47+
}

pom.xml

Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
2+
<modelVersion>4.0.0</modelVersion>
3+
<groupId>dev.webview</groupId>
4+
<artifactId>Webview</artifactId>
5+
<version>1.0.0</version>
6+
7+
<properties>
8+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
9+
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
10+
</properties>
11+
12+
<build>
13+
<plugins>
14+
<plugin>
15+
<groupId>org.apache.maven.plugins</groupId>
16+
<artifactId>maven-compiler-plugin</artifactId>
17+
<version>3.8.1</version>
18+
<configuration>
19+
<source>11</source>
20+
<target>11</target>
21+
</configuration>
22+
</plugin>
23+
<plugin>
24+
<groupId>org.apache.maven.plugins</groupId>
25+
<artifactId>maven-jar-plugin</artifactId>
26+
<version>2.3.2</version>
27+
<configuration>
28+
<finalName>${project.name}-original</finalName>
29+
</configuration>
30+
</plugin>
31+
<plugin>
32+
<groupId>org.apache.maven.plugins</groupId>
33+
<artifactId>maven-shade-plugin</artifactId>
34+
<version>3.2.1</version>
35+
<executions>
36+
<execution>
37+
<id>shade</id>
38+
<phase>package</phase>
39+
<goals>
40+
<goal>shade</goal>
41+
</goals>
42+
</execution>
43+
</executions>
44+
<configuration>
45+
<shadedArtifactAttached>true</shadedArtifactAttached>
46+
<finalName>${project.name}</finalName>
47+
</configuration>
48+
</plugin>
49+
<plugin>
50+
<groupId>org.apache.maven.plugins</groupId>
51+
<artifactId>maven-source-plugin</artifactId>
52+
<version>3.1.0</version>
53+
<configuration>
54+
<finalName>${project.name}</finalName>
55+
</configuration>
56+
<executions>
57+
<execution>
58+
<id>attach-sources</id>
59+
<goals>
60+
<goal>jar</goal>
61+
</goals>
62+
</execution>
63+
</executions>
64+
</plugin>
65+
</plugins>
66+
</build>
67+
68+
<repositories>
69+
<repository>
70+
<id>jitpack.io</id>
71+
<url>https://jitpack.io</url>
72+
</repository>
73+
</repositories>
74+
75+
<dependencies>
76+
77+
<!-- Code related packages -->
78+
<dependency>
79+
<groupId>org.projectlombok</groupId>
80+
<artifactId>lombok</artifactId>
81+
<version>1.18.22</version>
82+
<scope>provided</scope>
83+
</dependency>
84+
<dependency>
85+
<!-- For Eclipse users -->
86+
<groupId>org.jetbrains</groupId>
87+
<artifactId>annotations</artifactId>
88+
<version>19.0.0</version>
89+
<scope>provided</scope>
90+
</dependency>
91+
92+
<dependency>
93+
<groupId>com.github.casterlabs.rakurai</groupId>
94+
<artifactId>Json</artifactId>
95+
<version>1.13.0</version>
96+
<scope>compile</scope>
97+
</dependency>
98+
<dependency>
99+
<groupId>com.github.casterlabs.rakurai</groupId>
100+
<artifactId>Util</artifactId>
101+
<version>1.13.0</version>
102+
<scope>compile</scope>
103+
</dependency>
104+
105+
<dependency>
106+
<groupId>net.java.dev.jna</groupId>
107+
<artifactId>jna</artifactId>
108+
<version>5.10.0</version>
109+
<scope>compile</scope>
110+
</dependency>
111+
<dependency>
112+
<groupId>net.java.dev.jna</groupId>
113+
<artifactId>jna-platform</artifactId>
114+
<version>5.10.0</version>
115+
<scope>compile</scope>
116+
</dependency>
117+
118+
</dependencies>
119+
120+
</project>
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package dev.webview;
2+
3+
import org.jetbrains.annotations.Nullable;
4+
5+
import lombok.NonNull;
6+
7+
public interface ConsumingProducer<C, P> {
8+
9+
public @Nullable P produce(@Nullable C consume) throws InterruptedException;
10+
11+
public static <C, P> ConsumingProducer<C, P> of(@NonNull Class<C> consumingClazz, @Nullable P result) {
12+
return (aVoid) -> {
13+
return result;
14+
};
15+
}
16+
17+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package dev.webview;
2+
3+
import lombok.AllArgsConstructor;
4+
5+
@AllArgsConstructor
6+
public class Pair<A, B> {
7+
public final A a;
8+
public final B b;
9+
10+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package dev.webview;
2+
3+
import java.util.regex.Pattern;
4+
5+
import lombok.AllArgsConstructor;
6+
7+
@AllArgsConstructor
8+
public enum Platform {
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 Platform get() {
19+
String osName = System.getProperty("os.name").toLowerCase();
20+
21+
for (Platform 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+
}

0 commit comments

Comments
 (0)