部分应用实现

This commit is contained in:
suguo.yao 2022-04-21 16:14:59 +08:00
parent 1e90d00e9a
commit 68a2a21ca6
10 changed files with 201 additions and 1 deletions

1
.gitignore vendored Normal file
View File

@ -0,0 +1 @@
dest/

5
Makefile Normal file
View File

@ -0,0 +1,5 @@
macos:
mkdir -p dest/example.app/Contents/MacOS
# 用于放图标
mkdir -p dest/example.app/Contents/Resources
go build -o dest/example.app/Contents/MacOS/example webview/main.go

2
go.mod
View File

@ -3,7 +3,7 @@ module myschools.me/suguo/gui-demo
go 1.17
require (
github.com/webview/webview v0.0.0-20210330151455-f540d88dde4e
github.com/webview/webview v0.0.0-20220418180601-150aede5f486
github.com/zserge/lorca v0.1.10
)

2
go.sum
View File

@ -1,5 +1,7 @@
github.com/webview/webview v0.0.0-20210330151455-f540d88dde4e h1:z780M7mCrdt6KiICeW9SGirvQjxDlrVU+n99FO93nbI=
github.com/webview/webview v0.0.0-20210330151455-f540d88dde4e/go.mod h1:rpXAuuHgyEJb6kXcXldlkOjU6y4x+YcASKKXJNUhh0Y=
github.com/webview/webview v0.0.0-20220418180601-150aede5f486 h1:4jCUgve4ePmTf11eOy47cE5Q+LPNXapD6mvuJtFieZE=
github.com/webview/webview v0.0.0-20220418180601-150aede5f486/go.mod h1:rpXAuuHgyEJb6kXcXldlkOjU6y4x+YcASKKXJNUhh0Y=
github.com/zserge/lorca v0.1.10 h1:f/xBJ3D3ipcVRCcvN8XqZnpoKcOXV8I4vwqlFyw7ruc=
github.com/zserge/lorca v0.1.10/go.mod h1:bVmnIbIRlOcoV285KIRSe4bUABKi7R7384Ycuum6e4A=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=

1
readme.md Normal file
View File

@ -0,0 +1 @@
webview02 实现部分功能

22
webview/main.go Normal file
View File

@ -0,0 +1,22 @@
package main
import "github.com/webview/webview"
func main() {
debug := true
w := webview.New(debug)
defer w.Destroy()
w.SetTitle("zhengtan")
w.SetSize(1024, 768, webview.HintNone)
w.Navigate("http://redmine.myschools.me")
w.Bind("tt", TT)
w.Eval("")
w.Run()
}
func TT(a, b int) (int, error) {
if a > 0 && b > 0 {
return a + b, nil
}
return 0, nil
}

23
webview02/index.html Normal file
View File

@ -0,0 +1,23 @@
<!DOCTYPE html>
<html lang="">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width,initial-scale=1.0">
<link rel="icon" href="<%= BASE_URL %>favicon.ico">
</head>
<body>
<button onclick="hello('123')">发送</button>
<span id="label"></span>
</body>
<script>
function say(word) {
document.getElementById('label').innerHTML = word
}
</script>
</html>

27
webview02/main.go Normal file
View File

@ -0,0 +1,27 @@
package main
import (
"fmt"
"github.com/webview/webview"
)
func main() {
w := webview.New(true)
defer w.Destroy()
w.SetTitle("example")
w.SetSize(800, 600, webview.HintNone)
w.Navigate("http://192.168.0.19:8080")
w.Bind("hello", func(followers string) {
fmt.Println("CloudNativeFdn has", followers)
go say(w, followers)
})
w.Run()
}
func say(w webview.WebView, followers string) {
w.Dispatch(func() {
w.Eval("say('asdfasdfasd')")
})
}

8
webview02/readme.md Normal file
View File

@ -0,0 +1,8 @@
##### 功能
1. js调用golang方法存在的问题Bind存在多次如有返回需要考虑结构问题
2. golang调用js代码存在问题界面型如弹框无法实现
#### 其它
html二进制打包

111
webview03/main.go Normal file
View File

@ -0,0 +1,111 @@
package main
import (
"fmt"
"log"
"net"
"net/http"
"github.com/webview/webview"
)
var counter = 0
func handleRPC(w webview.WebView, data string) {
switch {
case data == "add":
counter++
case data == "sub":
counter--
}
w.Eval(fmt.Sprintf(`counter.innerHTML = "Counter: " + %d;`, counter))
}
func main() {
url := startServer()
w := webview.New(true)
defer w.Destroy()
w.SetTitle("example")
w.SetSize(800, 600, webview.HintNone)
w.Bind("add", handleRPC)
w.Navigate(url)
w.Run()
}
func startServer() string {
ln, err := net.Listen("tcp", "127.0.0.1:0")
if err != nil {
log.Fatal(err)
}
go func() {
defer ln.Close()
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte(indexHTML))
})
log.Fatal(http.Serve(ln, nil))
}()
fmt.Println("http://" + ln.Addr().String())
return "http://" + ln.Addr().String()
}
var indexHTML = `
<!doctype html>
<html>
<head>
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<style>
* {
margin: 0; padding: 0;
box-sizing: border-box; user-select: none;}
body {
height: 100vh; display: flex;
align-items: center;justify-content: center;
background-color: #f1c40f;
font-family: 'Helvetika Neue', Arial, sans-serif;
font-size: 28px;}
.counter-container {
display: flex; flex-direction: column;
align-items: center;}
.counter {
text-transform: uppercase; color: #fff;
font-weight: bold; font-size: 3rem;}
.btn-row {
display: flex; align-items: center;
margin: 1rem;}
.btn {
cursor: pointer; min-width: 4em;
padding: 1em; border-radius: 5px;
text-align: center; margin: 0 1rem;
box-shadow: 0 6px #8b5e00; color: white;
background-color: #E4B702; position: relative;
font-weight: bold;}
.btn:hover {
box-shadow: 0 4px #8b5e00; top: 2px;}
.btn:active{
box-shadow: 0 1px #8b5e00; top: 5px;}
</style>
</head>
<body>
<!-- UI layout -->
<div class="counter-container">
<div class="counter"></div>
<div class="btn-row">
<div class="btn btn-incr">+1</div>
<div class="btn btn-decr">-1</div>
</div>
</div>
<script>
const counter = document.querySelector('.counter');
const btnIncr = document.querySelector('.btn-incr');
const btnDecr = document.querySelector('.btn-decr');
counter.innerHTML = "Counter: 0";
btnIncr.addEventListener('click', function() {
window.webkit.messageHandlers.external.postMessage('add');}, false);
btnDecr.addEventListener('click', function() {
external.invoke('sub');}, false);
</script>
</body>
</html>
`