watch .desktop & pinned file changes

This commit is contained in:
piotr 2021-09-24 03:01:55 +02:00
parent e6088e7345
commit c19814256f
8 changed files with 107 additions and 15 deletions

View File

@ -6,6 +6,7 @@ get:
go get github.com/joshuarubin/go-sway
go get github.com/allan-simon/go-singleinstance
go get "github.com/sirupsen/logrus"
go get github.com/fsnotify/fsnotify
build:
go build -o bin/nwg-drawer .

Binary file not shown.

5
go.mod
View File

@ -4,8 +4,9 @@ go 1.16
require (
github.com/allan-simon/go-singleinstance v0.0.0-20210120080615-d0997106ab37
github.com/dlasky/gotk3-layershell v0.0.0-20210331230524-5cca0b819261
github.com/gotk3/gotk3 v0.6.0
github.com/dlasky/gotk3-layershell v0.0.0-20210827021656-e6ecab2731f7
github.com/fsnotify/fsnotify v1.5.1
github.com/gotk3/gotk3 v0.6.1
github.com/joshuarubin/go-sway v0.0.4
github.com/sirupsen/logrus v1.8.1
)

14
go.sum
View File

@ -3,11 +3,12 @@ github.com/allan-simon/go-singleinstance v0.0.0-20210120080615-d0997106ab37/go.m
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/dlasky/gotk3-layershell v0.0.0-20210331230524-5cca0b819261 h1:eoXn91ckLWKMXmQKX34UHEF2XMyQpRnnP80fDiu+kys=
github.com/dlasky/gotk3-layershell v0.0.0-20210331230524-5cca0b819261/go.mod h1:d56Gslp3IaiT8lqxD/lO1Msz1wYgD8D/HTKHgSdg9tU=
github.com/gotk3/gotk3 v0.5.3-0.20210223154815-289cfb6dbf32/go.mod h1:/hqFpkNa9T3JgNAE2fLvCdov7c5bw//FHNZrZ3Uv9/Q=
github.com/gotk3/gotk3 v0.6.0 h1:Aqlq4/6VabNwtCyA9M9zFNad5yHAqCi5heWnZ9y+3dA=
github.com/gotk3/gotk3 v0.6.0/go.mod h1:/hqFpkNa9T3JgNAE2fLvCdov7c5bw//FHNZrZ3Uv9/Q=
github.com/dlasky/gotk3-layershell v0.0.0-20210827021656-e6ecab2731f7 h1:LDo0kwt+oW9a4lWlj2OakIgMW1ySXXVRGFt8GHUdYYA=
github.com/dlasky/gotk3-layershell v0.0.0-20210827021656-e6ecab2731f7/go.mod h1:JHLx2Wz4mAPVwn4PFhC69ydwyHP4A3wQvlg7HKVVc1U=
github.com/fsnotify/fsnotify v1.5.1 h1:mZcQUHVQUQWoPXXtuf9yuEXKudkV2sx1E06UadKWpgI=
github.com/fsnotify/fsnotify v1.5.1/go.mod h1:T3375wBYaZdLLcVNkcVbzGHY7f1l/uK5T5Ai1i3InKU=
github.com/gotk3/gotk3 v0.6.1 h1:GJ400a0ecEEWrzjBvzBzH+pB/esEMIGdB9zPSmBdoeo=
github.com/gotk3/gotk3 v0.6.1/go.mod h1:/hqFpkNa9T3JgNAE2fLvCdov7c5bw//FHNZrZ3Uv9/Q=
github.com/joshuarubin/go-sway v0.0.4 h1:dpmIwQ/LytG+oMrjmaVKdk1aPdW2feXK/+wAcLKIx4A=
github.com/joshuarubin/go-sway v0.0.4/go.mod h1:qcDd6f25vJ0++wICwA1BainIcRC67p2Mb4lsrZ0k3/k=
github.com/joshuarubin/lifecycle v1.0.0 h1:N/lPEC8f+dBZ1Tn99vShqp36LwB+LI7XNAiNadZeLUQ=
@ -26,5 +27,6 @@ go.uber.org/multierr v1.1.0 h1:HoEmRHQPVSqub6w2z2d2EOVs2fjyFRGyofhKuyDq0QI=
go.uber.org/multierr v1.1.0/go.mod h1:wR5kodmAFQ0UK8QlbwjlSNy0Z68gJhDJUG5sjR94q/0=
golang.org/x/sync v0.0.0-20190412183630-56d357773e84 h1:IqXQ59gzdXv58Jmm2xn0tSOR9i6HqroaOFRQ3wR/dJQ=
golang.org/x/sync v0.0.0-20190412183630-56d357773e84/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sys v0.0.0-20191026070338-33540a1f6037 h1:YyJpGZS1sBuBCzLAR1VEpK193GlqGZbnPFnPV/5Rsb4=
golang.org/x/sys v0.0.0-20191026070338-33540a1f6037/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c h1:F1jZWGFhYfh0Ci55sIpILtKKK8p3i2/krTr0H1rg74I=
golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=

26
main.go
View File

@ -28,9 +28,8 @@ var (
configDirectory string
pinnedFile string
pinned []string
//src glib.SourceHandle
id2entry map[string]desktopEntry
preferredApps map[string]interface{}
id2entry map[string]desktopEntry
preferredApps map[string]interface{}
)
var categoryNames = [...]string{
@ -101,6 +100,8 @@ var (
status string
ignore string
showWindowTrigger bool
desktopTrigger bool
pinnedTrigger bool
)
func defaultStringIfBlank(s, fallback string) string {
@ -447,9 +448,28 @@ func main() {
}
}
showWindowTrigger = false
// some .desktop file changed
if desktopTrigger {
log.Debug(".desktop file changed")
desktopFiles = listDesktopFiles()
status = parseDesktopFiles(desktopFiles)
appFlowBox = setUpAppsFlowBox(nil, "")
desktopTrigger = false
}
// pinned file changed
if pinnedTrigger {
log.Debug("pinned file changed")
pinnedTrigger = false
pinned, _ = loadTextFile(pinnedFile)
pinnedFlowBox = setUpPinnedFlowBox()
}
return true
})
go watchFiles()
gtk.Main()
}

View File

@ -226,11 +226,17 @@ func getAppDirs() []string {
"/var/lib/flatpak/exports/share/applications"}
for _, d := range flatpakDirs {
if !isIn(dirs, d) {
if pathExists(d) && !isIn(dirs, d) {
dirs = append(dirs, d)
}
}
return dirs
var confirmedDirs []string
for _, d := range dirs {
if pathExists(d) {
confirmedDirs = append(confirmedDirs, d)
}
}
return confirmedDirs
}
func loadPreferredApps(path string) (map[string]interface{}, error) {
@ -336,6 +342,7 @@ func setUpCategories() {
}
func parseDesktopFiles(desktopFiles []string) string {
desktopEntries = nil
id2entry = make(map[string]desktopEntry)
skipped := 0
hidden := 0

View File

@ -67,7 +67,7 @@ func setUpPinnedFlowBox() *gtk.FlowBox {
return true
} else if btnEvent.Button() == 3 {
unpinItem(entry.DesktopID)
pinnedFlowBox = setUpPinnedFlowBox()
//pinnedFlowBox = setUpPinnedFlowBox()
return true
}
return false
@ -87,6 +87,7 @@ func setUpPinnedFlowBox() *gtk.FlowBox {
item.(*gtk.Widget).SetCanFocus(false)
})
}
flowBox.ShowAll()
return flowBox
}
@ -264,7 +265,6 @@ func flowBoxButton(entry desktopEntry) *gtk.Button {
return true
} else if btnEvent.Button() == 3 {
pinItem(ID)
pinnedFlowBox = setUpPinnedFlowBox()
return true
}
return false

61
watcher.go Normal file
View File

@ -0,0 +1,61 @@
package main
import (
"fmt"
"os"
"path/filepath"
"strings"
"github.com/fsnotify/fsnotify"
)
// Thanks to Steve Domino https://medium.com/@skdomino/watch-this-file-watching-in-go-5b5a247cf71f
var watcher *fsnotify.Watcher
func watchFiles() {
// creates a new file watcher
watcher, _ = fsnotify.NewWatcher()
defer watcher.Close()
if err := watcher.Add(pinnedFile); err != nil {
fmt.Println("ERROR", err)
}
for _, fp := range appDirs {
if err := filepath.Walk(fp, watchDir); err != nil {
fmt.Println("ERROR", err)
}
}
done := make(chan bool)
go func() {
for {
select {
case event := <-watcher.Events:
if strings.HasSuffix(event.Name, ".desktop") &&
(event.Op.String() == "CREATE" ||
event.Op.String() == "REMOVE" ||
event.Op.String() == "RENAME") {
desktopTrigger = true
} else if event.Name == pinnedFile {
pinnedTrigger = true
}
case err := <-watcher.Errors:
fmt.Println("ERROR", err)
}
}
}()
<-done
}
func watchDir(path string, fi os.FileInfo, err error) error {
if fi.Mode().IsDir() {
return watcher.Add(path)
}
return nil
}