Skip to content

Commit

Permalink
fix: update files in index when edited.
Browse files Browse the repository at this point in the history
- also watch package dir for to edit file index when files are changed
  added or removed

Signed-off-by: Rachel Powers <[email protected]>
  • Loading branch information
Ryex committed Sep 29, 2024
1 parent 507a73d commit 705bdc2
Show file tree
Hide file tree
Showing 13 changed files with 324 additions and 189 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ require (
github.com/chai2010/webp v1.1.1
github.com/davecgh/go-spew v1.1.1
github.com/dustin/go-humanize v1.0.1
github.com/fsnotify/fsnotify v1.7.0
github.com/schollz/progressbar/v3 v3.16.0
github.com/sirupsen/logrus v1.9.3
github.com/snowzach/rotatefilehook v0.0.0-20220211133110-53752135082d
Expand All @@ -22,7 +23,6 @@ require (
fyne.io/systray v1.11.0 // indirect
github.com/BurntSushi/toml v1.4.0 // indirect
github.com/fredbi/uri v1.1.0 // indirect
github.com/fsnotify/fsnotify v1.7.0 // indirect
github.com/fyne-io/gl-js v0.0.0-20220119005834-d2da28d9ccfe // indirect
github.com/fyne-io/glfw-js v0.0.0-20240101223322-6e1efdc71b7a // indirect
github.com/fyne-io/image v0.0.0-20220602074514-4956b0afb3d2 // indirect
Expand Down
33 changes: 27 additions & 6 deletions internal/gui/gui.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"fyne.io/fyne/v2/storage"
"fyne.io/fyne/v2/theme"
"fyne.io/fyne/v2/widget"
"github.com/fsnotify/fsnotify"
"github.com/ryex/dungeondraft-gopackager/internal/gui/bindings"
"github.com/ryex/dungeondraft-gopackager/internal/gui/layouts"
"github.com/ryex/dungeondraft-gopackager/internal/utils"
Expand All @@ -45,17 +46,24 @@ type App struct {

tagSaveTimer *time.Timer
resSaveTimers map[string]*time.Timer

packageWatcher *fsnotify.Watcher

pkgUpdateCounter int
packageUpdated binding.Int
}

//go:embed translation
var translations embed.FS

func NewApp() *App {
return &App{
app := &App{
operatingPath: binding.NewString(),
disableButtons: binding.NewBool(),
resSaveTimers: make(map[string]*time.Timer),
}
app.packageUpdated = binding.BindInt(&app.pkgUpdateCounter)
return app
}

func (a *App) Main() {
Expand All @@ -77,10 +85,16 @@ func (a *App) Main() {
a.clean()
}

func (a *App) clean() {
func (a *App) resetPkg() {
a.teardownPackageWatcher()
if a.pkg != nil {
a.pkg.Close()
}
a.pkg = nil
}

func (a *App) clean() {
a.resetPkg()
fmt.Println("Exited")
}

Expand Down Expand Up @@ -266,18 +280,24 @@ func (a *App) setErrContent(msg string, errs ...error) {
errContainer := container.NewVBox()
for i, err := range errs {
errText := multilineCanvasText(
fmt.Sprintf("%d) ", i)+err.Error(),
12,
fmt.Sprintf("%d) ", i+1)+err.Error(),
14,
fyne.TextStyle{Italic: true},
fyne.TextAlignLeading,
theme.Color(theme.ColorNameError),
)
errContainer.Add(errText)
}

msgContent := container.NewCenter(
msgContent := container.NewVBox(
layout.NewSpacer(),
msgText,
container.NewScroll(errContainer),
container.NewHBox(
layout.NewSpacer(),
container.NewVScroll(errContainer),
layout.NewSpacer(),
),
layout.NewSpacer(),
)

a.setMainContent(msgContent)
Expand Down Expand Up @@ -327,6 +347,7 @@ func (a *App) setWaitContent(msg string) (binding.Float, binding.String) {
activity,
msgText,
activityText,
progressBar,
layout.NewSpacer(),
)
a.setMainContent(activityContent)
Expand Down
112 changes: 90 additions & 22 deletions internal/gui/pack.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import (
"errors"
"fmt"
"path/filepath"
"sync"
"time"

"fyne.io/fyne/v2"
"fyne.io/fyne/v2/container"
Expand All @@ -15,25 +17,20 @@ import (
"fyne.io/fyne/v2/widget"

"github.com/davecgh/go-spew/spew"
"github.com/ryex/dungeondraft-gopackager/internal/gui/bindings"
"github.com/fsnotify/fsnotify"
"github.com/ryex/dungeondraft-gopackager/internal/gui/layouts"
"github.com/ryex/dungeondraft-gopackager/internal/utils"
"github.com/ryex/dungeondraft-gopackager/pkg/ddpackage"
"github.com/ryex/dungeondraft-gopackager/pkg/structures"
log "github.com/sirupsen/logrus"
)

func (a *App) loadUnpackedPath(path string) {
if a.pkg != nil {
a.pkg.Close()
}
a.pkg = nil

packjsonPath := filepath.Join(path, "pack.json")
if !utils.FileExists(packjsonPath) {
a.setNotAPackageContent(path)
return
}

activityProgress, activityStr := a.setWaitContent(lang.X(
"pack.wait",
"Loading unpacked resources from {{.Path}} (building index) ...",
Expand All @@ -43,6 +40,8 @@ func (a *App) loadUnpackedPath(path string) {
))
a.disableButtons.Set(true)

a.resetPkg()

go func() {
l := log.WithFields(log.Fields{
"path": path,
Expand Down Expand Up @@ -103,7 +102,81 @@ func (a *App) loadUnpackedPath(path string) {
err = nil
}
a.setUnpackedContent(pkg)
a.setupPackageWatcher()
}()
}

func (a *App) setupPackageWatcher() {
watcher, err := fsnotify.NewWatcher()
if err != nil {
log.WithError(err).Error("failed to setup filesystem watcher")
return
}
a.packageWatcher = watcher
paths := structures.NewSet[string]()
var eventTimer *time.Timer
lock := &sync.RWMutex{}

updatePackage := func() {
lock.Lock()
defer lock.Unlock()
eventTimer = nil
toUpdate := paths.AsSlice()
paths = structures.NewSet[string]()
if a.pkg != nil {
a.pkg.UpdateFromPaths(toUpdate)
}
a.packageUpdated.Set(a.pkgUpdateCounter + 1)
}

go func() {
for {
select {
case event, ok := <-watcher.Events:
if !ok {
return
}
if !event.Has(fsnotify.Chmod) {
path := event.Name
func() {
lock.Lock()
defer lock.Unlock()
if eventTimer != nil {
eventTimer.Stop()
}
paths.Add(path)
eventTimer = time.AfterFunc(
2*time.Second,
updatePackage,
)
}()
}
case err, ok := <-watcher.Errors:
if !ok {
return
}
log.WithError(err).Warn("filesystem watcher error")
}
}
}()

toWatchPath := a.pkg.UnpackedPath()
if toWatchPath != "" {
_, dirs, _ := utils.ListDir(toWatchPath)
for _, dir := range dirs {
log.WithField("package", toWatchPath).Infof("watching %s", dir)
err := watcher.Add(dir)
if err != nil {
log.WithError(err).WithField("package", toWatchPath).Warnf("failed to watch %s", dir)
}
}
}
}

func (a *App) teardownPackageWatcher() {
if a.packageWatcher != nil {
a.packageWatcher.Close()
}
}

func (a *App) setNotAPackageContent(path string) {
Expand Down Expand Up @@ -240,6 +313,7 @@ func (a *App) setUnpackedContent(pkg *ddpackage.Package) {
errDlg.Show()
return
}
a.pkg.UpdateFromPaths([]string{filepath.Join(options.Path, "pack.json")})
err = a.pkg.LoadUnpackedPackJSON(options.Path)
if err != nil {
errDlg := dialog.NewError(
Expand Down Expand Up @@ -291,31 +365,24 @@ func (a *App) setUnpackedContent(pkg *ddpackage.Package) {
func (a *App) packPackage(path string, options ddpackage.PackOptions, genThumbnails bool) {
a.disableButtons.Set(true)

thumbProgresVal := binding.NewFloat()
packProgresVal := binding.NewFloat()
progressVal := bindings.FloatMath(
func(d ...float64) float64 {
thumbP := d[0]
packP := d[1]
return thumbP*0.2 + packP*0.8
},
thumbProgresVal,
packProgresVal,
)
progressVal := binding.NewFloat()
taskStr := binding.NewString()
progressBar := widget.NewProgressBarWithData(progressVal)
taskLbl := widget.NewLabelWithData(taskStr)

targetPath := filepath.Join(path, a.pkg.Name()+".dungeondraft_pack")

progressDlg := dialog.NewCustomWithoutButtons(
lang.X("pack.packageProgressDlg.title", "Packing to {{.Path}}", map[string]any{"Path": targetPath}),
progressBar,
container.NewVBox(taskLbl, progressBar),
a.window,
)
progressDlg.Show()
go func() {
if genThumbnails {
taskStr.Set(lang.X("task.genthumbnails.text", "Generating thumbnails ..."))
err := a.pkg.GenerateThumbnails(func(p float64) {
thumbProgresVal.Set(p)
progressVal.Set(p)
})
if err != nil {
progressDlg.Hide()
Expand All @@ -332,11 +399,12 @@ func (a *App) packPackage(path string, options ddpackage.PackOptions, genThumbna
errDlg.Show()
return
}
progressVal.Set(1.0)
}
thumbProgresVal.Set(1.0)

taskStr.Set(lang.X("task.package.text", "Packaging resources ..."))
err := a.pkg.PackPackage(path, options, func(p float64) {
packProgresVal.Set(p)
progressVal.Set(p)
})
progressDlg.Hide()
if err != nil {
Expand Down
Loading

0 comments on commit 705bdc2

Please sign in to comment.