From 058f8684aa827a4a4927cc96724d3a705f6e1649 Mon Sep 17 00:00:00 2001 From: Raphael Ludwig Date: Sun, 13 Feb 2022 23:21:21 +0100 Subject: [PATCH 1/2] Use a channel and a goroutine instead of busy polling a boolean flag to reduce cpu consumption --- main.go | 78 ++++++++++++++++++++++++++++++++------------------------- 1 file changed, 44 insertions(+), 34 deletions(-) diff --git a/main.go b/main.go index 93b20aa..c2218d7 100644 --- a/main.go +++ b/main.go @@ -100,7 +100,6 @@ var ( statusLabel *gtk.Label status string ignore string - showWindowTrigger bool desktopTrigger bool pinnedTrigger bool ) @@ -148,6 +147,7 @@ func main() { // Gentle SIGTERM handler thanks to reiki4040 https://gist.github.com/reiki4040/be3705f307d3cd136e85 // v0.2: we also need to support SIGUSR from now on + showWindowChannel := make(chan interface{}, 1) signalChan := make(chan os.Signal, 1) signal.Notify(signalChan, syscall.SIGTERM, syscall.SIGUSR1) @@ -164,7 +164,7 @@ func main() { // let's just set e helper variable here. We'll be checking it with glib.TimeoutAdd. if !win.IsVisible() { log.Debug("SIGUSR1 received, showing the window") - showWindowTrigger = true + showWindowChannel <- struct{}{} } else { log.Debug("SIGUSR1 received, hiding the window") restoreStateAndHide() @@ -492,41 +492,51 @@ func main() { log.Info(fmt.Sprintf("UI created in %v ms. Thank you for your patience.", t.Sub(timeStart).Milliseconds())) // Check if showing the window has been requested (SIGUSR1) - glib.TimeoutAdd(uint(1), func() bool { - if showWindowTrigger && win != nil && !win.IsVisible() { - win.ShowAll() - if fileSearchResultWrapper != nil { - fileSearchResultWrapper.Hide() - } - // focus 1st element - b := appFlowBox.GetChildAtIndex(0) - if b != nil { - button, err := b.GetChild() - if err == nil { - button.ToWidget().GrabFocus() + go func() { + for { + <-showWindowChannel + + log.Debug("SHOW WINDOW") + glib.TimeoutAdd(0, func() bool { + if win != nil && !win.IsVisible() { + + // Refresh files before displaying the root window + // 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() + } + + // Show window and focus the search box + win.ShowAll() + if fileSearchResultWrapper != nil { + fileSearchResultWrapper.Hide() + } + // focus 1st element + b := appFlowBox.GetChildAtIndex(0) + if b != nil { + button, err := b.GetChild() + if err == nil { + button.ToWidget().GrabFocus() + } + } } - } - } - showWindowTrigger = false - // some .desktop file changed - if desktopTrigger { - log.Debug(".desktop file changed") - desktopFiles = listDesktopFiles() - status = parseDesktopFiles(desktopFiles) - appFlowBox = setUpAppsFlowBox(nil, "") - desktopTrigger = false + return false + }) } - - // pinned file changed - if pinnedTrigger { - log.Debug("pinned file changed") - pinnedTrigger = false - pinned, _ = loadTextFile(pinnedFile) - pinnedFlowBox = setUpPinnedFlowBox() - } - return true - }) + }() go watchFiles() From 165b925da2a8d1c7f71800c0d3be8bd619306ae5 Mon Sep 17 00:00:00 2001 From: Raphael Ludwig Date: Mon, 14 Feb 2022 20:59:48 +0100 Subject: [PATCH 2/2] Fix regression that prevented pinned items to update while window is open --- main.go | 76 ++++++++++++++++++++++++++++-------------------------- watcher.go | 4 ++- 2 files changed, 42 insertions(+), 38 deletions(-) diff --git a/main.go b/main.go index c2218d7..2a1d5ed 100644 --- a/main.go +++ b/main.go @@ -101,7 +101,7 @@ var ( status string ignore string desktopTrigger bool - pinnedTrigger bool + pinnedItemsChanged chan interface{} = make(chan interface{}, 1) ) func defaultStringIfBlank(s, fallback string) string { @@ -494,47 +494,49 @@ func main() { // Check if showing the window has been requested (SIGUSR1) go func() { for { - <-showWindowChannel + select { + case <-showWindowChannel: + log.Debug("Showing window") + glib.TimeoutAdd(0, func() bool { + if win != nil && !win.IsVisible() { - log.Debug("SHOW WINDOW") - glib.TimeoutAdd(0, func() bool { - if win != nil && !win.IsVisible() { + // Refresh files before displaying the root window + // some .desktop file changed + if desktopTrigger { + log.Debug(".desktop file changed") + desktopFiles = listDesktopFiles() + status = parseDesktopFiles(desktopFiles) + appFlowBox = setUpAppsFlowBox(nil, "") + desktopTrigger = false + } - // Refresh files before displaying the root window - // 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() - } - - // Show window and focus the search box - win.ShowAll() - if fileSearchResultWrapper != nil { - fileSearchResultWrapper.Hide() - } - // focus 1st element - b := appFlowBox.GetChildAtIndex(0) - if b != nil { - button, err := b.GetChild() - if err == nil { - button.ToWidget().GrabFocus() + // Show window and focus the search box + win.ShowAll() + if fileSearchResultWrapper != nil { + fileSearchResultWrapper.Hide() + } + // focus 1st element + b := appFlowBox.GetChildAtIndex(0) + if b != nil { + button, err := b.GetChild() + if err == nil { + button.ToWidget().GrabFocus() + } } } - } - return false - }) + return false + }) + + case <-pinnedItemsChanged: + glib.TimeoutAdd(0, func() bool { + log.Debug("pinned file changed") + pinned, _ = loadTextFile(pinnedFile) + pinnedFlowBox = setUpPinnedFlowBox() + + return false + }) + } } }() diff --git a/watcher.go b/watcher.go index 97f626b..d873405 100644 --- a/watcher.go +++ b/watcher.go @@ -41,7 +41,9 @@ func watchFiles() { event.Op.String() == "RENAME") { desktopTrigger = true } else if event.Name == pinnedFile { - pinnedTrigger = true + // TODO: This can be used to propagate information about the changed file to the + // GUI to avoid recreating everything + pinnedItemsChanged <- struct{}{} } case err := <-watcher.Errors: