support watch file change for auto refresh on debug mode
This commit is contained in:
parent
63e2baf723
commit
b06e420b40
7
go.mod
7
go.mod
@ -4,5 +4,12 @@ go 1.18
|
||||
|
||||
require (
|
||||
apigo.cc/apigo/plugin v1.0.2
|
||||
github.com/fsnotify/fsnotify v1.7.0
|
||||
github.com/ssgo/u v1.7.6
|
||||
github.com/webview/webview_go v0.0.0-20240217094020-f8e8d34d29dd
|
||||
)
|
||||
|
||||
require (
|
||||
golang.org/x/sys v0.4.0 // indirect
|
||||
gopkg.in/yaml.v3 v3.0.1 // indirect
|
||||
)
|
||||
|
48
plugin.go
48
plugin.go
@ -2,6 +2,9 @@ package client
|
||||
|
||||
import (
|
||||
"apigo.cc/apigo/plugin"
|
||||
"fmt"
|
||||
"github.com/fsnotify/fsnotify"
|
||||
"github.com/ssgo/u"
|
||||
webview "github.com/webview/webview_go"
|
||||
"os"
|
||||
"path/filepath"
|
||||
@ -27,10 +30,45 @@ type Webview struct {
|
||||
w webview.WebView
|
||||
isRunning bool
|
||||
isAsync bool
|
||||
isDebug bool
|
||||
webRoot string
|
||||
}
|
||||
|
||||
func addAllDirForWatcher(filename string, watcher *fsnotify.Watcher) {
|
||||
watcher.Add(filename)
|
||||
for _, f := range u.ReadDirN(filename) {
|
||||
if f.IsDir {
|
||||
addAllDirForWatcher(f.FullName, watcher)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (w *Webview) Run() {
|
||||
w.isRunning = true
|
||||
if w.isDebug && w.webRoot != "" {
|
||||
if watcher, err := fsnotify.NewWatcher(); err == nil {
|
||||
fmt.Println("watching...", w.webRoot)
|
||||
go func() {
|
||||
for w.isRunning {
|
||||
select {
|
||||
case event, ok := <-watcher.Events:
|
||||
if !ok {
|
||||
break
|
||||
}
|
||||
if event.Has(fsnotify.Write) {
|
||||
fmt.Println("file changed:", event.Name)
|
||||
w.Eval("location.reload()")
|
||||
}
|
||||
case _, ok := <-watcher.Errors:
|
||||
if !ok {
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
}()
|
||||
addAllDirForWatcher(w.webRoot, watcher)
|
||||
}
|
||||
}
|
||||
w.w.Run()
|
||||
if w.isRunning {
|
||||
w.isRunning = false
|
||||
@ -53,12 +91,12 @@ func (w *Webview) LoadURL(url string) {
|
||||
}
|
||||
|
||||
func (w *Webview) LoadFile(file string) {
|
||||
if filepath.IsAbs(file) {
|
||||
w.w.Navigate("file://" + file)
|
||||
} else {
|
||||
if !filepath.IsAbs(file) {
|
||||
curPath, _ := os.Getwd()
|
||||
w.w.Navigate("file://" + filepath.Join(curPath, file))
|
||||
file = filepath.Join(curPath, file)
|
||||
}
|
||||
w.webRoot = filepath.Dir(file)
|
||||
w.w.Navigate("file://" + file)
|
||||
}
|
||||
|
||||
func (w *Webview) Dispatch(f func()) {
|
||||
@ -124,7 +162,7 @@ func newWindow(title string, width int, height int, sizeMode *string, isDebug *b
|
||||
if isDebug != nil {
|
||||
isDebugV = *isDebug
|
||||
}
|
||||
w := &Webview{w: webview.New(isDebugV)}
|
||||
w := &Webview{w: webview.New(isDebugV), isDebug: isDebugV}
|
||||
w.SetTitle(title)
|
||||
w.SetSize(width, height, sizeMode)
|
||||
w.binds()
|
||||
|
14
tests/go.mod
14
tests/go.mod
@ -3,18 +3,20 @@ module tests
|
||||
go 1.18
|
||||
|
||||
require (
|
||||
apigo.cc/apigo/gojs v0.0.9
|
||||
apigo.cc/apigo/gojs v0.0.10
|
||||
current-plugin v0.0.0
|
||||
github.com/ssgo/u v1.7.5
|
||||
github.com/ssgo/u v1.7.6
|
||||
)
|
||||
|
||||
require (
|
||||
apigo.cc/apigo/plugin v1.0.2 // indirect
|
||||
apigo.cc/apigo/qjs v0.0.3 // indirect
|
||||
github.com/ssgo/config v1.7.5 // indirect
|
||||
github.com/ssgo/log v1.7.5 // indirect
|
||||
github.com/ssgo/standard v1.7.5 // indirect
|
||||
apigo.cc/apigo/quickjs-go v0.4.12 // indirect
|
||||
github.com/fsnotify/fsnotify v1.7.0 // indirect
|
||||
github.com/ssgo/config v1.7.6 // indirect
|
||||
github.com/ssgo/log v1.7.6 // indirect
|
||||
github.com/ssgo/standard v1.7.6 // indirect
|
||||
github.com/webview/webview_go v0.0.0-20240217094020-f8e8d34d29dd // indirect
|
||||
golang.org/x/sys v0.4.0 // indirect
|
||||
gopkg.in/yaml.v3 v3.0.1 // indirect
|
||||
)
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user