Use a channel and a goroutine instead of busy polling a boolean flag to reduce cpu consumption
This commit is contained in:
48
main.go
48
main.go
@@ -100,7 +100,6 @@ var (
|
|||||||
statusLabel *gtk.Label
|
statusLabel *gtk.Label
|
||||||
status string
|
status string
|
||||||
ignore string
|
ignore string
|
||||||
showWindowTrigger bool
|
|
||||||
desktopTrigger bool
|
desktopTrigger bool
|
||||||
pinnedTrigger bool
|
pinnedTrigger bool
|
||||||
)
|
)
|
||||||
@@ -148,6 +147,7 @@ func main() {
|
|||||||
|
|
||||||
// Gentle SIGTERM handler thanks to reiki4040 https://gist.github.com/reiki4040/be3705f307d3cd136e85
|
// Gentle SIGTERM handler thanks to reiki4040 https://gist.github.com/reiki4040/be3705f307d3cd136e85
|
||||||
// v0.2: we also need to support SIGUSR from now on
|
// v0.2: we also need to support SIGUSR from now on
|
||||||
|
showWindowChannel := make(chan interface{}, 1)
|
||||||
signalChan := make(chan os.Signal, 1)
|
signalChan := make(chan os.Signal, 1)
|
||||||
signal.Notify(signalChan, syscall.SIGTERM, syscall.SIGUSR1)
|
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.
|
// let's just set e helper variable here. We'll be checking it with glib.TimeoutAdd.
|
||||||
if !win.IsVisible() {
|
if !win.IsVisible() {
|
||||||
log.Debug("SIGUSR1 received, showing the window")
|
log.Debug("SIGUSR1 received, showing the window")
|
||||||
showWindowTrigger = true
|
showWindowChannel <- struct{}{}
|
||||||
} else {
|
} else {
|
||||||
log.Debug("SIGUSR1 received, hiding the window")
|
log.Debug("SIGUSR1 received, hiding the window")
|
||||||
restoreStateAndHide()
|
restoreStateAndHide()
|
||||||
@@ -492,23 +492,15 @@ func main() {
|
|||||||
log.Info(fmt.Sprintf("UI created in %v ms. Thank you for your patience.", t.Sub(timeStart).Milliseconds()))
|
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)
|
// Check if showing the window has been requested (SIGUSR1)
|
||||||
glib.TimeoutAdd(uint(1), func() bool {
|
go func() {
|
||||||
if showWindowTrigger && win != nil && !win.IsVisible() {
|
for {
|
||||||
win.ShowAll()
|
<-showWindowChannel
|
||||||
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
|
|
||||||
|
|
||||||
|
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
|
// some .desktop file changed
|
||||||
if desktopTrigger {
|
if desktopTrigger {
|
||||||
log.Debug(".desktop file changed")
|
log.Debug(".desktop file changed")
|
||||||
@@ -525,8 +517,26 @@ func main() {
|
|||||||
pinned, _ = loadTextFile(pinnedFile)
|
pinned, _ = loadTextFile(pinnedFile)
|
||||||
pinnedFlowBox = setUpPinnedFlowBox()
|
pinnedFlowBox = setUpPinnedFlowBox()
|
||||||
}
|
}
|
||||||
return true
|
|
||||||
|
// 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
|
||||||
})
|
})
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
|
||||||
go watchFiles()
|
go watchFiles()
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user