Skip to content

Commit

Permalink
Implement everything, even some examples!
Browse files Browse the repository at this point in the history
  • Loading branch information
e3ndr committed Feb 26, 2022
1 parent 135a109 commit da30385
Show file tree
Hide file tree
Showing 14 changed files with 674 additions and 0 deletions.
18 changes: 18 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# IntelliJ
*.iml
.idea/

# Eclipse
.settings/
.project
.classpath
.metadata

# Common
target/
.DS_Store
__MACOSX

/webview*
/libwebview*
/WebView*
19 changes: 19 additions & 0 deletions Example.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package dev.webview;

public class Example {

public static void main(String[] args) {
Webview wv = new Webview(); // Can optionally be created with an AWT component to be painted on.

// Calling `await echo(1,2,3)` will return `[1,2,3]`
wv.bind("echo", (arguments) -> {
return arguments;
});

wv.loadURL("https://google.com");

// Run the webview event loop, the webview is fully disposed when this returns.
wv.run();
}

}
13 changes: 13 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# Webview

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.

## How to use

1) Include the libary in your project (see the [JitPack page](https://jitpack.io/#Casterlabs/webview)).
2) Copy and run the example in `Example.java`.
3) Profit!

## TODO

Build our own DLLs and whatnot, the current ones are copied from the C# port.
47 changes: 47 additions & 0 deletions SwingExample.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package dev.webview;

import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.JFrame;

public class SwingExample {

public static void main(String[] args) {
JFrame frame = new JFrame();

frame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
frame.setLayout(new BorderLayout());

// Using createAWT allows you to defer the creation of the webview until the
// canvas is fully renderable.
Component component = Webview.createAWT((wv) -> {
// Calling `await echo(1,2,3)` will return `[1,2,3]`
wv.bind("echo", (arguments) -> {
return arguments;
});

wv.loadURL("https://google.com");

frame.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
wv.close();
frame.dispose();
System.exit(0);
}
});

// Run the webview event loop, the webview is fully disposed when this returns.
wv.run();
});

frame.getContentPane().add(component, BorderLayout.CENTER);

frame.setSize(800, 600);
frame.setVisible(true);
}

}
120 changes: 120 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
<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">
<modelVersion>4.0.0</modelVersion>
<groupId>dev.webview</groupId>
<artifactId>Webview</artifactId>
<version>1.0.0</version>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
</properties>

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<configuration>
<source>11</source>
<target>11</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.3.2</version>
<configuration>
<finalName>${project.name}-original</finalName>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>3.2.1</version>
<executions>
<execution>
<id>shade</id>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
</execution>
</executions>
<configuration>
<shadedArtifactAttached>true</shadedArtifactAttached>
<finalName>${project.name}</finalName>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-source-plugin</artifactId>
<version>3.1.0</version>
<configuration>
<finalName>${project.name}</finalName>
</configuration>
<executions>
<execution>
<id>attach-sources</id>
<goals>
<goal>jar</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>

<repositories>
<repository>
<id>jitpack.io</id>
<url>https://jitpack.io</url>
</repository>
</repositories>

<dependencies>

<!-- Code related packages -->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.22</version>
<scope>provided</scope>
</dependency>
<dependency>
<!-- For Eclipse users -->
<groupId>org.jetbrains</groupId>
<artifactId>annotations</artifactId>
<version>19.0.0</version>
<scope>provided</scope>
</dependency>

<dependency>
<groupId>com.github.casterlabs.rakurai</groupId>
<artifactId>Json</artifactId>
<version>1.13.0</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>com.github.casterlabs.rakurai</groupId>
<artifactId>Util</artifactId>
<version>1.13.0</version>
<scope>compile</scope>
</dependency>

<dependency>
<groupId>net.java.dev.jna</groupId>
<artifactId>jna</artifactId>
<version>5.10.0</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>net.java.dev.jna</groupId>
<artifactId>jna-platform</artifactId>
<version>5.10.0</version>
<scope>compile</scope>
</dependency>

</dependencies>

</project>
17 changes: 17 additions & 0 deletions src/main/java/dev/webview/ConsumingProducer.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package dev.webview;

import org.jetbrains.annotations.Nullable;

import lombok.NonNull;

public interface ConsumingProducer<C, P> {

public @Nullable P produce(@Nullable C consume) throws InterruptedException;

public static <C, P> ConsumingProducer<C, P> of(@NonNull Class<C> consumingClazz, @Nullable P result) {
return (aVoid) -> {
return result;
};
}

}
10 changes: 10 additions & 0 deletions src/main/java/dev/webview/Pair.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package dev.webview;

import lombok.AllArgsConstructor;

@AllArgsConstructor
public class Pair<A, B> {
public final A a;
public final B b;

}
35 changes: 35 additions & 0 deletions src/main/java/dev/webview/Platform.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package dev.webview;

import java.util.regex.Pattern;

import lombok.AllArgsConstructor;

@AllArgsConstructor
public enum Platform {
// @formatter:off
MACOSX ("macOS", "mac|darwin"),
LINUX ("Linux", "nux"),
WINDOWS ("Windows", "win");
// @formatter:on

private String str;
private String regex;

static Platform get() {
String osName = System.getProperty("os.name").toLowerCase();

for (Platform os : values()) {
if (Pattern.compile(os.regex).matcher(osName).find()) {
return os;
}
}

throw new UnsupportedOperationException("Unknown operating system: " + osName);
}

@Override
public String toString() {
return this.str;
}

}
Loading

0 comments on commit da30385

Please sign in to comment.