Skip to content

Commit 1a304af

Browse files
authored
Add Go binding (#1)
* Merge LICENSE files of this repo and core library's repo * Remove myself from LICENSE I haven't made any noteworthy contributions to the Go bindings. * Add Go bindings * Add content to README * Fix Go docs URL and add reference to core library
1 parent bc18a89 commit 1a304af

File tree

17 files changed

+26790
-1
lines changed

17 files changed

+26790
-1
lines changed

.github/workflows/ci.yaml

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
name: CI Pipeline
2+
on: [push, pull_request]
3+
4+
jobs:
5+
build:
6+
strategy:
7+
matrix:
8+
image:
9+
- macos-latest
10+
- ubuntu-latest
11+
- windows-latest
12+
runs-on: ${{ matrix.image }}
13+
steps:
14+
- uses: actions/checkout@v3
15+
- name: Install dependencies
16+
if: runner.os == 'Linux'
17+
run: sudo apt-get update && sudo apt-get install -y libwebkit2gtk-4.0-dev
18+
- name: Build examples
19+
run: go build ./examples/basic ./examples/bind
20+
- name: Run tests
21+
run: go test

LICENSE

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
MIT License
22

3+
Copyright (c) 2017 Serge Zaitsev
34
Copyright (c) 2020 webview
45

56
Permission is hereby granted, free of charge, to any person obtaining a copy

README.md

Lines changed: 48 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,50 @@
11
# webview_go
22

3-
For the foreseeable future, golang bindings for webview_core will be maintained in https://github.com/webview/webview
3+
[![GoDoc](https://godoc.org/github.com/webview/webview_go?status.svg)](https://godoc.org/github.com/webview/webview_go)
4+
[![Go Report Card](https://goreportcard.com/badge/github.com/webview/webview_go)](https://goreportcard.com/report/github.com/webview/webview_go)
5+
6+
Go language binding for the [webview library][webview].
7+
8+
> [!NOTE]
9+
> Versions <= 0.1.1 are available in the [old repository][webview].
10+
11+
### Getting Started
12+
13+
See [Go package documentation][go-docs] for the Go API documentation, or simply read the source code.
14+
15+
Start with creating a new directory structure for your project.
16+
17+
```sh
18+
mkdir my-project && cd my-project
19+
```
20+
21+
Create a new Go module.
22+
23+
```sh
24+
go mod init example.com/app
25+
```
26+
27+
Save one of the example programs into your project directory.
28+
29+
```sh
30+
curl -sSLo main.go "https://raw.githubusercontent.com/webview/webview_go/master/examples/basic/main.go"
31+
```
32+
33+
Install dependencies.
34+
35+
```sh
36+
go get github.com/webview/webview_go
37+
```
38+
39+
Build the example. On Windows, add `-ldflags="-H windowsgui"` to the command line.
40+
41+
```sh
42+
go build
43+
```
44+
45+
### Notes
46+
47+
Calling `Eval()` or `Dispatch()` before `Run()` does not work because the webview instance has only been configured and not yet started.
48+
49+
[go-docs]: https://pkg.go.dev/github.com/webview/webview_go
50+
[webview]: https://github.com/webview/webview

examples/basic/main.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package main
2+
3+
import webview "github.com/webview/webview_go"
4+
5+
func main() {
6+
w := webview.New(false)
7+
defer w.Destroy()
8+
w.SetTitle("Basic Example")
9+
w.SetSize(480, 320, webview.HintNone)
10+
w.SetHtml("Thanks for using webview!")
11+
w.Run()
12+
}

examples/bind/main.go

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package main
2+
3+
import webview "github.com/webview/webview_go"
4+
5+
const html = `<button id="increment">Tap me</button>
6+
<div>You tapped <span id="count">0</span> time(s).</div>
7+
<script>
8+
const [incrementElement, countElement] =
9+
document.querySelectorAll("#increment, #count");
10+
document.addEventListener("DOMContentLoaded", () => {
11+
incrementElement.addEventListener("click", () => {
12+
window.increment().then(result => {
13+
countElement.textContent = result.count;
14+
});
15+
});
16+
});
17+
</script>`
18+
19+
type IncrementResult struct {
20+
Count uint `json:"count"`
21+
}
22+
23+
func main() {
24+
var count uint = 0
25+
w := webview.New(false)
26+
defer w.Destroy()
27+
w.SetTitle("Bind Example")
28+
w.SetSize(480, 320, webview.HintNone)
29+
30+
// A binding that increments a value and immediately returns the new value.
31+
w.Bind("increment", func() IncrementResult {
32+
count++
33+
return IncrementResult{Count: count}
34+
})
35+
36+
w.SetHtml(html)
37+
w.Run()
38+
}

glue.c

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
#include "webview.h"
2+
3+
#include <stdlib.h>
4+
#include <stdint.h>
5+
6+
struct binding_context {
7+
webview_t w;
8+
uintptr_t index;
9+
};
10+
11+
void _webviewDispatchGoCallback(void *);
12+
void _webviewBindingGoCallback(webview_t, char *, char *, uintptr_t);
13+
14+
static void _webview_dispatch_cb(webview_t w, void *arg) {
15+
_webviewDispatchGoCallback(arg);
16+
}
17+
18+
static void _webview_binding_cb(const char *id, const char *req, void *arg) {
19+
struct binding_context *ctx = (struct binding_context *) arg;
20+
_webviewBindingGoCallback(ctx->w, (char *)id, (char *)req, ctx->index);
21+
}
22+
23+
void CgoWebViewDispatch(webview_t w, uintptr_t arg) {
24+
webview_dispatch(w, _webview_dispatch_cb, (void *)arg);
25+
}
26+
27+
void CgoWebViewBind(webview_t w, const char *name, uintptr_t index) {
28+
struct binding_context *ctx = calloc(1, sizeof(struct binding_context));
29+
ctx->w = w;
30+
ctx->index = index;
31+
webview_bind(w, name, _webview_binding_cb, (void *)ctx);
32+
}

go.mod

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
module github.com/webview/webview_go
2+
3+
go 1.13

go.sum

Whitespace-only changes.

libs/mswebview2/LICENSE

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
Copyright (C) Microsoft Corporation. All rights reserved.
2+
3+
Redistribution and use in source and binary forms, with or without
4+
modification, are permitted provided that the following conditions are
5+
met:
6+
7+
* Redistributions of source code must retain the above copyright
8+
notice, this list of conditions and the following disclaimer.
9+
* Redistributions in binary form must reproduce the above
10+
copyright notice, this list of conditions and the following disclaimer
11+
in the documentation and/or other materials provided with the
12+
distribution.
13+
* The name of Microsoft Corporation, or the names of its contributors
14+
may not be used to endorse or promote products derived from this
15+
software without specific prior written permission.
16+
17+
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18+
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19+
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
20+
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
21+
OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22+
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23+
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24+
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25+
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26+
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27+
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

0 commit comments

Comments
 (0)