support watch file change for auto refresh on debug mode

This commit is contained in:
Star 2024-07-04 11:32:09 +08:00
parent 63e2baf723
commit b06e420b40
3 changed files with 58 additions and 11 deletions

7
go.mod
View File

@ -4,5 +4,12 @@ go 1.18
require ( require (
apigo.cc/apigo/plugin v1.0.2 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 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
)

View File

@ -2,6 +2,9 @@ package client
import ( import (
"apigo.cc/apigo/plugin" "apigo.cc/apigo/plugin"
"fmt"
"github.com/fsnotify/fsnotify"
"github.com/ssgo/u"
webview "github.com/webview/webview_go" webview "github.com/webview/webview_go"
"os" "os"
"path/filepath" "path/filepath"
@ -27,10 +30,45 @@ type Webview struct {
w webview.WebView w webview.WebView
isRunning bool isRunning bool
isAsync 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() { func (w *Webview) Run() {
w.isRunning = true 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() w.w.Run()
if w.isRunning { if w.isRunning {
w.isRunning = false w.isRunning = false
@ -53,12 +91,12 @@ func (w *Webview) LoadURL(url string) {
} }
func (w *Webview) LoadFile(file string) { func (w *Webview) LoadFile(file string) {
if filepath.IsAbs(file) { if !filepath.IsAbs(file) {
w.w.Navigate("file://" + file)
} else {
curPath, _ := os.Getwd() 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()) { 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 { if isDebug != nil {
isDebugV = *isDebug isDebugV = *isDebug
} }
w := &Webview{w: webview.New(isDebugV)} w := &Webview{w: webview.New(isDebugV), isDebug: isDebugV}
w.SetTitle(title) w.SetTitle(title)
w.SetSize(width, height, sizeMode) w.SetSize(width, height, sizeMode)
w.binds() w.binds()

View File

@ -3,18 +3,20 @@ module tests
go 1.18 go 1.18
require ( require (
apigo.cc/apigo/gojs v0.0.9 apigo.cc/apigo/gojs v0.0.10
current-plugin v0.0.0 current-plugin v0.0.0
github.com/ssgo/u v1.7.5 github.com/ssgo/u v1.7.6
) )
require ( require (
apigo.cc/apigo/plugin v1.0.2 // indirect apigo.cc/apigo/plugin v1.0.2 // indirect
apigo.cc/apigo/qjs v0.0.3 // indirect apigo.cc/apigo/quickjs-go v0.4.12 // indirect
github.com/ssgo/config v1.7.5 // indirect github.com/fsnotify/fsnotify v1.7.0 // indirect
github.com/ssgo/log v1.7.5 // indirect github.com/ssgo/config v1.7.6 // indirect
github.com/ssgo/standard v1.7.5 // 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 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 gopkg.in/yaml.v3 v3.0.1 // indirect
) )