mirror of
https://github.com/nwg-piotr/nwg-drawer.git
synced 2024-09-11 14:07:17 +03:00
watch .desktop & pinned file changes
This commit is contained in:
parent
e6088e7345
commit
c19814256f
1
Makefile
1
Makefile
@ -6,6 +6,7 @@ get:
|
|||||||
go get github.com/joshuarubin/go-sway
|
go get github.com/joshuarubin/go-sway
|
||||||
go get github.com/allan-simon/go-singleinstance
|
go get github.com/allan-simon/go-singleinstance
|
||||||
go get "github.com/sirupsen/logrus"
|
go get "github.com/sirupsen/logrus"
|
||||||
|
go get github.com/fsnotify/fsnotify
|
||||||
|
|
||||||
build:
|
build:
|
||||||
go build -o bin/nwg-drawer .
|
go build -o bin/nwg-drawer .
|
||||||
|
BIN
bin/nwg-drawer
BIN
bin/nwg-drawer
Binary file not shown.
5
go.mod
5
go.mod
@ -4,8 +4,9 @@ go 1.16
|
|||||||
|
|
||||||
require (
|
require (
|
||||||
github.com/allan-simon/go-singleinstance v0.0.0-20210120080615-d0997106ab37
|
github.com/allan-simon/go-singleinstance v0.0.0-20210120080615-d0997106ab37
|
||||||
github.com/dlasky/gotk3-layershell v0.0.0-20210331230524-5cca0b819261
|
github.com/dlasky/gotk3-layershell v0.0.0-20210827021656-e6ecab2731f7
|
||||||
github.com/gotk3/gotk3 v0.6.0
|
github.com/fsnotify/fsnotify v1.5.1
|
||||||
|
github.com/gotk3/gotk3 v0.6.1
|
||||||
github.com/joshuarubin/go-sway v0.0.4
|
github.com/joshuarubin/go-sway v0.0.4
|
||||||
github.com/sirupsen/logrus v1.8.1
|
github.com/sirupsen/logrus v1.8.1
|
||||||
)
|
)
|
||||||
|
14
go.sum
14
go.sum
@ -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.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 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
|
||||||
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
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-20210827021656-e6ecab2731f7 h1:LDo0kwt+oW9a4lWlj2OakIgMW1ySXXVRGFt8GHUdYYA=
|
||||||
github.com/dlasky/gotk3-layershell v0.0.0-20210331230524-5cca0b819261/go.mod h1:d56Gslp3IaiT8lqxD/lO1Msz1wYgD8D/HTKHgSdg9tU=
|
github.com/dlasky/gotk3-layershell v0.0.0-20210827021656-e6ecab2731f7/go.mod h1:JHLx2Wz4mAPVwn4PFhC69ydwyHP4A3wQvlg7HKVVc1U=
|
||||||
github.com/gotk3/gotk3 v0.5.3-0.20210223154815-289cfb6dbf32/go.mod h1:/hqFpkNa9T3JgNAE2fLvCdov7c5bw//FHNZrZ3Uv9/Q=
|
github.com/fsnotify/fsnotify v1.5.1 h1:mZcQUHVQUQWoPXXtuf9yuEXKudkV2sx1E06UadKWpgI=
|
||||||
github.com/gotk3/gotk3 v0.6.0 h1:Aqlq4/6VabNwtCyA9M9zFNad5yHAqCi5heWnZ9y+3dA=
|
github.com/fsnotify/fsnotify v1.5.1/go.mod h1:T3375wBYaZdLLcVNkcVbzGHY7f1l/uK5T5Ai1i3InKU=
|
||||||
github.com/gotk3/gotk3 v0.6.0/go.mod h1:/hqFpkNa9T3JgNAE2fLvCdov7c5bw//FHNZrZ3Uv9/Q=
|
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 h1:dpmIwQ/LytG+oMrjmaVKdk1aPdW2feXK/+wAcLKIx4A=
|
||||||
github.com/joshuarubin/go-sway v0.0.4/go.mod h1:qcDd6f25vJ0++wICwA1BainIcRC67p2Mb4lsrZ0k3/k=
|
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=
|
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=
|
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 h1:IqXQ59gzdXv58Jmm2xn0tSOR9i6HqroaOFRQ3wR/dJQ=
|
||||||
golang.org/x/sync v0.0.0-20190412183630-56d357773e84/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
|
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-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
26
main.go
@ -28,9 +28,8 @@ var (
|
|||||||
configDirectory string
|
configDirectory string
|
||||||
pinnedFile string
|
pinnedFile string
|
||||||
pinned []string
|
pinned []string
|
||||||
//src glib.SourceHandle
|
id2entry map[string]desktopEntry
|
||||||
id2entry map[string]desktopEntry
|
preferredApps map[string]interface{}
|
||||||
preferredApps map[string]interface{}
|
|
||||||
)
|
)
|
||||||
|
|
||||||
var categoryNames = [...]string{
|
var categoryNames = [...]string{
|
||||||
@ -101,6 +100,8 @@ var (
|
|||||||
status string
|
status string
|
||||||
ignore string
|
ignore string
|
||||||
showWindowTrigger bool
|
showWindowTrigger bool
|
||||||
|
desktopTrigger bool
|
||||||
|
pinnedTrigger bool
|
||||||
)
|
)
|
||||||
|
|
||||||
func defaultStringIfBlank(s, fallback string) string {
|
func defaultStringIfBlank(s, fallback string) string {
|
||||||
@ -447,9 +448,28 @@ func main() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
showWindowTrigger = false
|
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
|
return true
|
||||||
})
|
})
|
||||||
|
|
||||||
|
go watchFiles()
|
||||||
|
|
||||||
gtk.Main()
|
gtk.Main()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
11
tools.go
11
tools.go
@ -226,11 +226,17 @@ func getAppDirs() []string {
|
|||||||
"/var/lib/flatpak/exports/share/applications"}
|
"/var/lib/flatpak/exports/share/applications"}
|
||||||
|
|
||||||
for _, d := range flatpakDirs {
|
for _, d := range flatpakDirs {
|
||||||
if !isIn(dirs, d) {
|
if pathExists(d) && !isIn(dirs, d) {
|
||||||
dirs = append(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) {
|
func loadPreferredApps(path string) (map[string]interface{}, error) {
|
||||||
@ -336,6 +342,7 @@ func setUpCategories() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func parseDesktopFiles(desktopFiles []string) string {
|
func parseDesktopFiles(desktopFiles []string) string {
|
||||||
|
desktopEntries = nil
|
||||||
id2entry = make(map[string]desktopEntry)
|
id2entry = make(map[string]desktopEntry)
|
||||||
skipped := 0
|
skipped := 0
|
||||||
hidden := 0
|
hidden := 0
|
||||||
|
@ -67,7 +67,7 @@ func setUpPinnedFlowBox() *gtk.FlowBox {
|
|||||||
return true
|
return true
|
||||||
} else if btnEvent.Button() == 3 {
|
} else if btnEvent.Button() == 3 {
|
||||||
unpinItem(entry.DesktopID)
|
unpinItem(entry.DesktopID)
|
||||||
pinnedFlowBox = setUpPinnedFlowBox()
|
//pinnedFlowBox = setUpPinnedFlowBox()
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
return false
|
return false
|
||||||
@ -87,6 +87,7 @@ func setUpPinnedFlowBox() *gtk.FlowBox {
|
|||||||
item.(*gtk.Widget).SetCanFocus(false)
|
item.(*gtk.Widget).SetCanFocus(false)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
flowBox.ShowAll()
|
||||||
|
|
||||||
return flowBox
|
return flowBox
|
||||||
}
|
}
|
||||||
@ -264,7 +265,6 @@ func flowBoxButton(entry desktopEntry) *gtk.Button {
|
|||||||
return true
|
return true
|
||||||
} else if btnEvent.Button() == 3 {
|
} else if btnEvent.Button() == 3 {
|
||||||
pinItem(ID)
|
pinItem(ID)
|
||||||
pinnedFlowBox = setUpPinnedFlowBox()
|
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
return false
|
return false
|
||||||
|
61
watcher.go
Normal file
61
watcher.go
Normal 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
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user